mru.vim 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. " File: mru.vim
  2. " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
  3. " Version: 3.11
  4. " Last Modified: October 16, 2022
  5. " Copyright: Copyright (C) 2003-2022 Yegappan Lakshmanan
  6. " License: Permission is hereby granted to use and distribute this code,
  7. " with or without modifications, provided that this copyright
  8. " notice is copied with it. Like anything else that's free,
  9. " mru.vim is provided *as is* and comes with no warranty of any
  10. " kind, either expressed or implied. In no event will the copyright
  11. " holder be liable for any damages resulting from the use of this
  12. " software.
  13. "
  14. " ****************** Do not modify after this line ************************
  15. if exists('loaded_mru')
  16. finish
  17. endif
  18. let loaded_mru=1
  19. if v:version < 700
  20. " Needs Vim version 7.0 and above
  21. finish
  22. endif
  23. " Line continuation used here
  24. let s:cpo_save = &cpoptions
  25. set cpoptions&vim
  26. " MRU configuration variables {{{1
  27. " Maximum number of file names stored in the MRU list
  28. if !exists('MRU_Max_Entries')
  29. let MRU_Max_Entries = 100
  30. endif
  31. " Files to exclude from the MRU list
  32. if !exists('MRU_Exclude_Files')
  33. let MRU_Exclude_Files = ''
  34. endif
  35. " Files to include in the MRU list
  36. if !exists('MRU_Include_Files')
  37. let MRU_Include_Files = ''
  38. endif
  39. " Height of the MRU window
  40. " Default height is 8
  41. if !exists('MRU_Window_Height')
  42. let MRU_Window_Height = 8
  43. endif
  44. if !exists('MRU_Use_Current_Window')
  45. let MRU_Use_Current_Window = 0
  46. endif
  47. if !exists('MRU_Auto_Close')
  48. let MRU_Auto_Close = 1
  49. endif
  50. if !exists('g:MRU_File')
  51. if has('unix') || has('macunix')
  52. let s:MRU_File = $HOME . '/.vim_mru_files'
  53. else
  54. let s:MRU_File = $VIM . '/_vim_mru_files'
  55. if has('win32')
  56. " MS-Windows
  57. if !empty($USERPROFILE)
  58. let s:MRU_File = $USERPROFILE . '\_vim_mru_files'
  59. endif
  60. endif
  61. endif
  62. else
  63. let s:MRU_File = expand(g:MRU_File)
  64. endif
  65. " Option for enabling or disabling the MRU menu
  66. if !exists('MRU_Add_Menu')
  67. let MRU_Add_Menu = 1
  68. endif
  69. " Maximum number of file names to show in the MRU menu. If too many files are
  70. " listed in the menu, then Vim becomes slow when updating the menu. So set
  71. " this to a low value.
  72. if !exists('MRU_Max_Menu_Entries')
  73. let MRU_Max_Menu_Entries = 10
  74. endif
  75. " Maximum number of file names to show in a MRU sub-menu. If the MRU list
  76. " contains more file names than this setting, then the MRU menu is split into
  77. " one or more sub-menus.
  78. if !exists('MRU_Max_Submenu_Entries')
  79. let MRU_Max_Submenu_Entries = 10
  80. endif
  81. " When only a single matching filename is found in the MRU list, the following
  82. " option controls whether the file name is displayed in the MRU window or the
  83. " file is directly opened. When this variable is set to 0 and a single
  84. " matching file name is found, then the file is directly opened.
  85. if !exists('MRU_Window_Open_Always')
  86. let MRU_Window_Open_Always = 0
  87. endif
  88. " When opening a file from the MRU list, the file is opened in the current
  89. " tab. If the selected file has to be opened in a tab always, then set the
  90. " following variable to 1. If the file is already opened in a tab, then the
  91. " cursor will be moved to that tab.
  92. if !exists('MRU_Open_File_Use_Tabs')
  93. let MRU_Open_File_Use_Tabs = 0
  94. endif
  95. " Controls whether fuzzy matching is used for matching a user supplied pattern
  96. " against the file names in the MRU list.
  97. if !exists('MRU_FuzzyMatch')
  98. if exists('*matchfuzzy')
  99. " Fuzzy matching is supported only when matchfuzzy() function is present
  100. let MRU_FuzzyMatch = 1
  101. else
  102. let MRU_FuzzyMatch = 0
  103. endif
  104. endif
  105. " Controls whether the alternate file (:help alternate-file) is set when the
  106. " plugin is loaded to the first file in the MRU list. Default is to set the
  107. " alternate file.
  108. if !exists('MRU_Set_Alternate_File')
  109. let MRU_Set_Alternate_File = 0
  110. endif
  111. " Format of the file names displayed in the MRU window.
  112. " The default is to display the filename followed by the complete path to the
  113. " file in parenthesis. This variable controls the expressions used to format
  114. " and parse the path. This can be changed to display the filenames in a
  115. " different format. The 'formatter' specifies how to split/format the filename
  116. " and 'parser' specifies how to read the filename back; 'syntax' matches the
  117. " part to be highlighted.
  118. if !exists('MRU_Filename_Format')
  119. let MRU_Filename_Format = {
  120. \ 'formatter': 'fnamemodify(v:val, ":t") . " (" . v:val . ")"',
  121. \ 'parser': '(\zs.*\ze)',
  122. \ 'syntax': '^.\{-}\ze('
  123. \}
  124. endif
  125. let s:MRU_buf_name = '-RecentFiles-'
  126. " Control to temporarily lock the MRU list. Used to prevent files from
  127. " getting added to the MRU list when the ':vimgrep' command is executed.
  128. let s:mru_list_locked = 0
  129. " MRU_LoadList {{{1
  130. " Loads the latest list of file names from the MRU file
  131. func! s:MRU_LoadList() abort
  132. " If the MRU file is present, then load the list of filenames. Otherwise
  133. " start with an empty list.
  134. if filereadable(s:MRU_File)
  135. let s:MRU_files = readfile(s:MRU_File)
  136. if empty(s:MRU_files)
  137. " empty MRU file
  138. let s:MRU_files = []
  139. elseif s:MRU_files[0] =~# '^\s*" Most recently edited files in Vim'
  140. " Generated by the previous version of the MRU plugin.
  141. " Discard the list.
  142. let s:MRU_files = []
  143. elseif s:MRU_files[0] =~# '^#'
  144. " Remove the comment line
  145. call remove(s:MRU_files, 0)
  146. else
  147. " Unsupported format
  148. let s:MRU_files = []
  149. endif
  150. else
  151. let s:MRU_files = []
  152. endif
  153. " Refresh the MRU menu with the latest list of filenames
  154. call s:MRU_Refresh_Menu()
  155. endfunc
  156. " MRU_SaveList {{{1
  157. " Saves the MRU file names to the MRU file
  158. func! s:MRU_SaveList() abort
  159. let l = []
  160. call add(l, '# Most recently edited files in Vim (version 3.0)')
  161. call extend(l, s:MRU_files)
  162. call writefile(l, s:MRU_File)
  163. endfunc
  164. " MRU_AddFile {{{1
  165. " Adds a file to the MRU file list
  166. " acmd_bufnr - Buffer number of the file to add
  167. func! s:MRU_AddFile(acmd_bufnr) abort
  168. if s:mru_list_locked
  169. " MRU list is currently locked
  170. return
  171. endif
  172. " Get the full path to the filename
  173. let fname = fnamemodify(bufname(a:acmd_bufnr + 0), ':p')
  174. if empty(fname)
  175. return
  176. endif
  177. " Skip temporary buffers with buftype set. The buftype is set for buffers
  178. " used by plugins.
  179. if !empty(&buftype)
  180. return
  181. endif
  182. if !empty(g:MRU_Include_Files)
  183. " If MRU_Include_Files is set, include only files matching the
  184. " specified pattern
  185. if fname !~# g:MRU_Include_Files
  186. return
  187. endif
  188. endif
  189. if !empty(g:MRU_Exclude_Files)
  190. " Do not add files matching the pattern specified in the
  191. " MRU_Exclude_Files to the MRU list
  192. if fname =~# g:MRU_Exclude_Files
  193. return
  194. endif
  195. endif
  196. " If the filename is not already present in the MRU list and is not
  197. " readable then ignore it
  198. let idx = index(s:MRU_files, fname)
  199. if idx == -1
  200. if !filereadable(fname)
  201. " File is not readable and is not in the MRU list
  202. return
  203. endif
  204. endif
  205. " Load the latest MRU file list
  206. call s:MRU_LoadList()
  207. " Remove the new file name from the existing MRU list (if already present)
  208. call filter(s:MRU_files, 'v:val !=# fname')
  209. " Add the new file list to the beginning of the updated old file list
  210. call insert(s:MRU_files, fname, 0)
  211. " Trim the list
  212. if len(s:MRU_files) > g:MRU_Max_Entries
  213. call remove(s:MRU_files, g:MRU_Max_Entries, -1)
  214. endif
  215. " Save the updated MRU list
  216. call s:MRU_SaveList()
  217. " Refresh the MRU menu
  218. call s:MRU_Refresh_Menu()
  219. " If the MRU window is open, update the displayed MRU list
  220. let bname = s:MRU_buf_name
  221. let winnum = bufwinnr(bname)
  222. if winnum != -1
  223. let cur_winnr = winnr()
  224. call s:MRU_Open_Window('', '', 0)
  225. if winnr() != cur_winnr
  226. exe cur_winnr . 'wincmd w'
  227. endif
  228. endif
  229. endfunc
  230. " MRU_escape_filename {{{1
  231. " Escape special characters in a filename. Special characters in file names
  232. " that should be escaped (for security reasons)
  233. let s:esc_filename_chars = ' *?[{`$%#"|!<>();&' . "'\t\n"
  234. func! s:MRU_escape_filename(fname) abort
  235. if exists('*fnameescape')
  236. return fnameescape(a:fname)
  237. else
  238. return escape(a:fname, s:esc_filename_chars)
  239. endif
  240. endfunc
  241. " MRU_Edit_File {{{1
  242. " Edit the specified file
  243. " filename - Name of the file to edit
  244. " sanitized - Specifies whether the filename is already escaped for special
  245. " characters or not.
  246. " splitdir - command modifier for a split (topleft, belowright, etc.)
  247. " Used by the :MRU command and the "Recent Files" menu item
  248. func! s:MRU_Edit_File(filename, sanitized, splitdir) abort
  249. if !a:sanitized
  250. let esc_fname = s:MRU_escape_filename(a:filename)
  251. else
  252. let esc_fname = a:filename
  253. endif
  254. " If the user wants to always open the file in a tab, then open the file
  255. " in a tab. If it is already opened in a tab, then the cursor will be
  256. " moved to that tab.
  257. if g:MRU_Open_File_Use_Tabs
  258. call s:MRU_Open_File_In_Tab(a:filename, esc_fname)
  259. return
  260. endif
  261. " If the file is already open in one of the windows, jump to it
  262. let winnum = bufwinnr('^' . a:filename . '$')
  263. if winnum != -1
  264. if winnum != winnr()
  265. exe winnum . 'wincmd w'
  266. endif
  267. else
  268. if !empty(a:splitdir) || (!&hidden && (&modified || !empty(&buftype)
  269. \ || &previewwindow))
  270. " If a split command modifier is specified, always open the file
  271. " in a new window.
  272. " Or if the current buffer has unsaved changes or is a special buffer or
  273. " is the preview window. The 'hidden' option is also not set. So open
  274. " the file in a new window.
  275. if bufexists(esc_fname)
  276. exe a:splitdir . ' sbuffer ' . esc_fname
  277. else
  278. exe a:splitdir . ' split ' . esc_fname
  279. endif
  280. else
  281. " The current file can be replaced with the selected file.
  282. if bufexists(esc_fname)
  283. exe 'buffer ' . esc_fname
  284. else
  285. exe 'edit ' . esc_fname
  286. endif
  287. endif
  288. " Make the buffer a listed buffer (in case it was deleted before)
  289. setlocal buflisted
  290. endif
  291. endfunc
  292. " MRU_Open_File_In_Tab
  293. " Open a file in a tab. If the file is already opened in a tab, jump to the
  294. " tab. Otherwise, create a new tab and open the file.
  295. " fname : Name of the file to open
  296. " esc_fname : File name with special characters escaped
  297. func! s:MRU_Open_File_In_Tab(fname, esc_fname) abort
  298. " If the selected file is already open in the current tab or in
  299. " another tab, jump to it. Otherwise open it in a new tab
  300. if bufwinnr('^' . a:fname . '$') == -1
  301. let tabnum = -1
  302. let i = 1
  303. let bnum = bufnr('^' . a:fname . '$')
  304. while i <= tabpagenr('$')
  305. if index(tabpagebuflist(i), bnum) != -1
  306. let tabnum = i
  307. break
  308. endif
  309. let i += 1
  310. endwhile
  311. if tabnum != -1
  312. " Goto the tab containing the file
  313. exe 'tabnext ' . i
  314. else
  315. if (winnr('$') == 1) && empty(@%) && !&modified
  316. " Reuse the current tab if it contains a single new unmodified
  317. " file.
  318. if bufexists(a:esc_fname)
  319. exe 'buffer ' . a:esc_fname
  320. else
  321. exe 'edit ' . a:esc_fname
  322. endif
  323. else
  324. " Open a new tab as the last tab page
  325. if v:version >= 800
  326. if bufexists(a:esc_fname)
  327. exe '$tab sbuffer ' . a:esc_fname
  328. else
  329. exe '$tabnew ' . a:esc_fname
  330. endif
  331. else
  332. if bufexists(a:esc_fname)
  333. exe '99999tab sbuffer ' . a:esc_fname
  334. else
  335. exe '99999tabnew ' . a:esc_fname
  336. endif
  337. endif
  338. endif
  339. endif
  340. endif
  341. " Jump to the window containing the file
  342. let winnum = bufwinnr('^' . a:fname . '$')
  343. if winnum != winnr()
  344. exe winnum . 'wincmd w'
  345. endif
  346. endfunc
  347. " MRU_Window_Edit_File {{{1
  348. " fname : Name of the file to edit. May specify single or multiple
  349. " files.
  350. " edit_type : Specifies how to edit the file. Can be one of 'edit' or 'view'.
  351. " 'view' - Open the file as a read-only file
  352. " 'edit' - Edit the file as a regular file
  353. " multi : Specifies whether a single file or multiple files need to be
  354. " opened.
  355. " open_type : Specifies where to open the file.
  356. " useopen - If the file is already present in a window, then
  357. " jump to that window. Otherwise, open the file in
  358. " the previous window.
  359. " newwin_horiz - Open the file in a new horizontal window.
  360. " newwin_vert - Open the file in a new vertical window.
  361. " newtab - Open the file in a new tab. If the file is already
  362. " opened in a tab, then jump to that tab.
  363. " preview - Open the file in the preview window
  364. func! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type) abort
  365. let esc_fname = s:MRU_escape_filename(a:fname)
  366. if a:open_type ==# 'newwin_horiz'
  367. " Edit the file in a new horizontally split window below the previous
  368. " window
  369. wincmd p
  370. if bufexists(esc_fname)
  371. exe 'belowright sbuffer ' . esc_fname
  372. else
  373. exe 'belowright new ' . esc_fname
  374. endif
  375. elseif a:open_type ==# 'newwin_vert'
  376. " Edit the file in a new vertically split window right of the previous
  377. " window
  378. wincmd p
  379. if bufexists(esc_fname)
  380. exe 'vertical belowright sbuffer ' . esc_fname
  381. else
  382. exe 'belowright vnew ' . esc_fname
  383. endif
  384. elseif a:open_type ==# 'newtab' || g:MRU_Open_File_Use_Tabs
  385. call s:MRU_Open_File_In_Tab(a:fname, esc_fname)
  386. elseif a:open_type ==# 'preview'
  387. " Edit the file in the preview window
  388. exe 'topleft pedit ' . esc_fname
  389. else
  390. " If the selected file is already open in one of the windows,
  391. " jump to it
  392. let winnum = bufwinnr('^' . a:fname . '$')
  393. if winnum != -1 && g:MRU_Use_Current_Window == 0
  394. exe winnum . 'wincmd w'
  395. else
  396. if g:MRU_Auto_Close == 1 && g:MRU_Use_Current_Window == 0
  397. " Jump to the window from which the MRU window was opened
  398. if exists('s:MRU_last_buffer')
  399. let last_winnr = bufwinnr(s:MRU_last_buffer)
  400. if last_winnr != -1 && last_winnr != winnr()
  401. exe last_winnr . 'wincmd w'
  402. endif
  403. endif
  404. else
  405. if g:MRU_Use_Current_Window == 0
  406. " Goto the previous window
  407. " If MRU_Use_Current_Window is set to one, then the
  408. " current window is used to open the file
  409. wincmd p
  410. endif
  411. endif
  412. let split_window = 0
  413. if (!&hidden && (&modified || &previewwindow)) || a:multi
  414. " Current buffer has unsaved changes or is the preview window
  415. " or the user is opening multiple files
  416. " So open the file in a new window
  417. let split_window = 1
  418. endif
  419. if !empty(&buftype)
  420. " Current buffer is a special buffer (maybe used by a plugin)
  421. if g:MRU_Use_Current_Window == 0 ||
  422. \ bufnr('%') != bufnr(s:MRU_buf_name)
  423. let split_window = 1
  424. endif
  425. endif
  426. " Edit the file
  427. if split_window
  428. " Current buffer has unsaved changes or is a special buffer or
  429. " is the preview window. So open the file in a new window
  430. if a:edit_type ==# 'edit'
  431. if bufexists(esc_fname)
  432. exe 'sbuffer ' . esc_fname
  433. else
  434. exe 'split ' . esc_fname
  435. endif
  436. else
  437. exe 'sview ' . esc_fname
  438. endif
  439. else
  440. let mod = ''
  441. if g:MRU_Use_Current_Window
  442. let mod = 'keepalt '
  443. endif
  444. if a:edit_type ==# 'edit'
  445. if bufexists(esc_fname)
  446. exe mod . 'buffer ' . esc_fname
  447. else
  448. exe mod . 'edit ' . esc_fname
  449. endif
  450. else
  451. exe mod . 'view ' . esc_fname
  452. endif
  453. endif
  454. endif
  455. endif
  456. " Make the buffer a listed buffer (in case it was deleted before)
  457. setlocal buflisted
  458. endfunc
  459. " MRU_Select_File_Cmd {{{1
  460. " Open a file selected from the MRU window
  461. "
  462. " 'opt' has two values separated by comma. The first value specifies how to
  463. " edit the file and can be either 'edit' or 'view'. The second value
  464. " specifies where to open the file. It can take one of the following values:
  465. " 'useopen' to open file in the previous window
  466. " 'newwin_horiz' to open the file in a new horizontal split window
  467. " 'newwin_vert' to open the file in a new vertical split window.
  468. " 'newtab' to open the file in a new tab.
  469. " If multiple file names are selected using visual mode, then open multiple
  470. " files (either in split windows or tabs)
  471. func! s:MRU_Select_File_Cmd(opt) range abort
  472. let [edit_type, open_type] = split(a:opt, ',')
  473. if has('patch-8.2.1978') && mode() ==# 'n'
  474. let l:firstline = line('.')
  475. let l:lastline = l:firstline + v:count
  476. if l:lastline > line('$')
  477. let l:lastline = line('$')
  478. endif
  479. elseif has('patch-8.2.1978') && mode() !=# 'n'
  480. let l:firstline = line('.')
  481. let l:lastline = line('v')
  482. if l:firstline > l:lastline
  483. let [l:firstline, l:lastline] = [l:lastline, l:firstline]
  484. endif
  485. else
  486. let l:firstline = a:firstline
  487. let l:lastline = a:lastline
  488. endif
  489. let fnames = getline(l:firstline, l:lastline)
  490. if g:MRU_Auto_Close == 1 && g:MRU_Use_Current_Window == 0
  491. " Automatically close the window if the file window is
  492. " not used to display the MRU list.
  493. silent! close
  494. endif
  495. let multi = 0
  496. for f in fnames
  497. if empty(f)
  498. continue
  499. endif
  500. " The text in the MRU window contains the filename in parenthesis
  501. let file = matchstr(f, g:MRU_Filename_Format.parser)
  502. call s:MRU_Window_Edit_File(file, multi, edit_type, open_type)
  503. if l:firstline != l:lastline
  504. " Opening multiple files
  505. let multi = 1
  506. endif
  507. endfor
  508. endfunc
  509. " MRU_Warn_Msg {{{1
  510. " Display a warning message
  511. func! s:MRU_Warn_Msg(msg) abort
  512. echohl WarningMsg
  513. echo a:msg
  514. echohl None
  515. endfunc
  516. " MRU_Open_Window {{{1
  517. " Display the Most Recently Used file list in a temporary window.
  518. " If the 'pat' argument is not empty, then it specifies the pattern of files
  519. " to selectively display in the MRU window.
  520. " The 'splitdir' argument specifies the location (topleft, belowright, etc.)
  521. " of the MRU window.
  522. func! s:MRU_Open_Window(pat, splitdir, winsz) abort
  523. " Load the latest MRU file list
  524. call s:MRU_LoadList()
  525. " Check for empty MRU list
  526. if empty(s:MRU_files)
  527. call s:MRU_Warn_Msg('MRU file list is empty')
  528. return
  529. endif
  530. " Save the current buffer number. This is used later to open a file when a
  531. " entry is selected from the MRU window. The window number is not saved,
  532. " as the window number will change when new windows are opened.
  533. let s:MRU_last_buffer = bufnr('%')
  534. let bname = s:MRU_buf_name
  535. " If the window is already open, jump to it
  536. let winnum = bufwinnr(bname)
  537. if winnum != -1
  538. if winnr() != winnum
  539. " If not already in the window, jump to it
  540. exe winnum . 'wincmd w'
  541. endif
  542. setlocal modifiable
  543. " Delete the contents of the buffer to the black-hole register
  544. silent! %delete _
  545. else
  546. if g:MRU_Use_Current_Window
  547. " Reuse the current window
  548. " If the current buffer has unsaved changes or is a special buffer
  549. " or is the preview window and 'hidden' is not set, then open a
  550. " new window. Otherwise, open in the current window.
  551. if !&hidden && (&modified || !empty(&buftype) || &previewwindow)
  552. let split_window = 1
  553. else
  554. let split_window = 0
  555. endif
  556. " If the __MRU_Files__ buffer exists, then reuse it. Otherwise open
  557. " a new buffer
  558. let bufnum = bufnr(bname)
  559. if bufnum == -1
  560. if split_window
  561. let cmd = 'botright split ' . bname
  562. else
  563. let cmd = 'edit ' . bname
  564. endif
  565. else
  566. if split_window
  567. let cmd = 'botright sbuffer ' . bufnum
  568. else
  569. let cmd = 'buffer ' . bufnum
  570. endif
  571. endif
  572. exe cmd
  573. if bufnr('%') != bufnr(bname)
  574. " Failed to edit the MRU buffer
  575. return
  576. endif
  577. else
  578. " Open a new window at the bottom
  579. let cmd = 'silent! '
  580. if empty(a:splitdir)
  581. let cmd .= 'botright '
  582. else
  583. let cmd .= a:splitdir . ' '
  584. endif
  585. let sz = a:winsz
  586. if sz == 0
  587. let sz = g:MRU_Window_Height
  588. endif
  589. let cmd .= sz . 'split '
  590. " If the __MRU_Files__ buffer exists, then reuse it. Otherwise open
  591. " a new buffer
  592. let bufnum = bufnr(bname)
  593. if bufnum == -1
  594. let cmd .= bname
  595. else
  596. let cmd .= '+buffer' . bufnum
  597. endif
  598. exe cmd
  599. endif
  600. endif
  601. setlocal modifiable
  602. " Mark the buffer as scratch
  603. setlocal buftype=nofile
  604. if g:MRU_Use_Current_Window
  605. " avoid using mru buffer as alternate file
  606. setlocal bufhidden=wipe
  607. else
  608. setlocal bufhidden=delete
  609. endif
  610. setlocal noswapfile
  611. setlocal nobuflisted
  612. setlocal nowrap
  613. setlocal nonumber
  614. if exists('&relativenumber')
  615. setlocal norelativenumber
  616. endif
  617. if exists('&signcolumn')
  618. setlocal signcolumn=no
  619. endif
  620. setlocal foldcolumn=0
  621. " Set the 'filetype' to 'mru'. This allows the user to apply custom
  622. " syntax highlighting or other changes to the MRU bufer.
  623. setlocal filetype=mru
  624. if !g:MRU_Use_Current_Window
  625. " Use fixed height and width for the MRU window
  626. setlocal winfixheight winfixwidth
  627. endif
  628. " Setup the cpoptions properly for the maps to work
  629. let old_cpoptions = &cpoptions
  630. set cpoptions&vim
  631. " Create mappings to select and edit a file from the MRU list
  632. let l:cmd = has('patch-8.2.1978') ? '<cmd>' : ':'
  633. let l:silent = has('patch-8.2.1978') ? '' : '<silent> '
  634. execute 'nnoremap <buffer> ' . l:silent . '<CR> ' .
  635. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,useopen")<CR>'
  636. execute 'vnoremap <buffer> ' . l:silent . '<CR> ' .
  637. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,useopen")<CR>'
  638. execute 'nnoremap <buffer> ' . l:silent . 'o ' .
  639. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newwin_horiz")<CR>'
  640. execute 'vnoremap <buffer> ' . l:silent . 'o ' .
  641. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newwin_horiz")<CR>'
  642. execute 'nnoremap <buffer> ' . l:silent . '<S-CR> ' .
  643. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newwin_horiz")<CR>'
  644. execute 'vnoremap <buffer> ' . l:silent . '<S-CR> ' .
  645. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newwin_horiz")<CR>'
  646. execute 'nnoremap <buffer> ' . l:silent . 'O ' .
  647. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newwin_vert")<CR>'
  648. execute 'vnoremap <buffer> ' . l:silent . 'O ' .
  649. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newwin_vert")<CR>'
  650. execute 'nnoremap <buffer> ' . l:silent . 't ' .
  651. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newtab")<CR>'
  652. execute 'vnoremap <buffer> ' . l:silent . 't ' .
  653. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,newtab")<CR>'
  654. execute 'nnoremap <buffer> ' . l:silent . 'v ' .
  655. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("view,useopen")<CR>'
  656. execute 'nnoremap <buffer> ' . l:silent . 'p ' .
  657. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("view,preview")<CR>'
  658. vnoremap <buffer> <silent> p
  659. \ :<C-u>if line("'<") == line("'>")<Bar>
  660. \ call <SID>MRU_Select_File_Cmd('open,preview')<Bar>
  661. \ else<Bar>
  662. \ echoerr "Only a single file can be previewed"<Bar>
  663. \ endif<CR>
  664. execute 'nnoremap <buffer> ' . l:silent . 'u ' . l:cmd . 'MRU<CR>'
  665. execute 'nnoremap <buffer> ' . l:silent . '<2-LeftMouse> ' .
  666. \ l:cmd . 'call <SID>MRU_Select_File_Cmd("edit,useopen")<CR>'
  667. nnoremap <buffer> <silent> d
  668. \ :<C-U>call <SID>MRU_Delete_From_List()<CR>
  669. nnoremap <buffer> <silent> q :close<CR>
  670. " Restore the previous cpoptions settings
  671. let &cpoptions = old_cpoptions
  672. " Display the MRU list
  673. if empty(a:pat)
  674. " No search pattern specified. Display the complete list
  675. let m = copy(s:MRU_files)
  676. else
  677. " Display only the entries matching the specified pattern. First try
  678. " fuzzy matching or as a literal pattern.
  679. if g:MRU_FuzzyMatch
  680. let m = filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:pat)->empty()')
  681. else
  682. " do case insensitive file name comparison
  683. let spat = tolower(a:pat)
  684. let m = filter(copy(s:MRU_files), 'stridx(tolower(v:val), spat) != -1')
  685. endif
  686. if len(m) == 0
  687. " No match. Try using it as a regular expression
  688. let m = filter(copy(s:MRU_files), 'v:val =~# a:pat')
  689. endif
  690. endif
  691. " Get the tail part of the file name (without the directory) and display
  692. " it along with the full path in parenthesis.
  693. let output = map(m, g:MRU_Filename_Format.formatter)
  694. silent! 0put =output
  695. " Delete the empty line at the end of the buffer
  696. silent! $delete _
  697. " Move the cursor to the beginning of the file
  698. normal! gg
  699. " Add syntax highlighting for the file names
  700. if has_key(g:MRU_Filename_Format, 'syntax')
  701. exe "syntax match MRUFileName '" . g:MRU_Filename_Format.syntax . "'"
  702. highlight default link MRUFileName Identifier
  703. endif
  704. setlocal nomodifiable
  705. endfunc
  706. " MRU_Complete {{{1
  707. " Command-line completion function used by :MRU command
  708. func! s:MRU_Complete(ArgLead, CmdLine, CursorPos) abort
  709. " Load the latest MRU file
  710. call s:MRU_LoadList()
  711. if empty(a:ArgLead)
  712. " Return the complete list of MRU files
  713. return s:MRU_files
  714. else
  715. if g:MRU_FuzzyMatch
  716. " Return only the files fuzzy matching the specified pattern
  717. return filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:ArgLead)->empty()')
  718. else
  719. " Return only the files matching the specified pattern
  720. return filter(copy(s:MRU_files), 'v:val =~? a:ArgLead')
  721. endif
  722. endif
  723. endfunc
  724. " MRU_Cmd {{{1
  725. " Function to handle the MRU command
  726. " pat - File name pattern passed to the MRU command
  727. func! s:MRU_Cmd(pat, splitdir, winsz) abort
  728. if empty(a:pat)
  729. " No arguments specified. Open the MRU window
  730. call s:MRU_Open_Window('', a:splitdir, a:winsz)
  731. return
  732. endif
  733. " Load the latest MRU file
  734. call s:MRU_LoadList()
  735. " Empty MRU list
  736. if empty(s:MRU_files)
  737. call s:MRU_Warn_Msg('MRU file list is empty')
  738. return
  739. endif
  740. " If Vim supports fuzzy matching, then try fuzzy matching the pattern
  741. " against the file names. Otherwise, use the specified string as a literal
  742. " string and search for filenames containing the string. If only one
  743. " filename is found, then edit it (unless the user wants to open the MRU
  744. " window always)
  745. if g:MRU_FuzzyMatch
  746. let m = filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:pat)->empty()')
  747. else
  748. " do case insensitive file name comparison
  749. let spat = tolower(a:pat)
  750. let m = filter(copy(s:MRU_files), 'stridx(tolower(v:val), spat) != -1')
  751. endif
  752. if len(m) > 0
  753. if len(m) == 1 && !g:MRU_Window_Open_Always
  754. call s:MRU_Edit_File(m[0], 0, a:splitdir)
  755. return
  756. endif
  757. " More than one file matches. Try to find an accurate match
  758. let new_m = filter(m, 'v:val ==# a:pat')
  759. if len(new_m) == 1 && !g:MRU_Window_Open_Always
  760. call s:MRU_Edit_File(new_m[0], 0, a:splitdir)
  761. return
  762. endif
  763. " Couldn't find an exact match, open the MRU window with all the
  764. " files matching the pattern.
  765. call s:MRU_Open_Window(a:pat, a:splitdir, a:winsz)
  766. return
  767. endif
  768. " Use the specified string as a regular expression pattern and search
  769. " for filenames matching the pattern
  770. let m = filter(copy(s:MRU_files), 'v:val =~? a:pat')
  771. if len(m) == 0
  772. " If an existing file (not present in the MRU list) is specified,
  773. " then open the file.
  774. if filereadable(a:pat)
  775. call s:MRU_Edit_File(a:pat, 0, a:splitdir)
  776. return
  777. endif
  778. " No filenames matching the specified pattern are found
  779. call s:MRU_Warn_Msg("MRU file list doesn't contain " .
  780. \ 'files matching ' . a:pat)
  781. return
  782. endif
  783. if len(m) == 1 && !g:MRU_Window_Open_Always
  784. call s:MRU_Edit_File(m[0], 0, a:splitdir)
  785. return
  786. endif
  787. call s:MRU_Open_Window(a:pat, a:splitdir, a:winsz)
  788. endfunc
  789. " MRU_Toggle {{{1
  790. " Toggle MRU
  791. " pat - File name pattern passed to the MRU command
  792. func! s:MRU_Toggle(pat, splitdir) abort
  793. " If the MRU window is open, close it
  794. let winnum = bufwinnr(s:MRU_buf_name)
  795. if winnum != -1
  796. exe winnum . 'wincmd w'
  797. if g:MRU_Use_Current_Window && !empty(expand('#'))
  798. silent! b #
  799. else
  800. silent! close
  801. endif
  802. else
  803. call s:MRU_Cmd(a:pat, a:splitdir, '')
  804. endif
  805. endfunction
  806. " MRU_add_files_to_menu {{{1
  807. " Adds a list of files to the "Recent Files" sub menu under the "File" menu.
  808. " prefix - Prefix to use for each of the menu entries
  809. " file_list - List of file names to add to the menu
  810. func! s:MRU_add_files_to_menu(prefix, file_list) abort
  811. for fname in a:file_list
  812. " Escape special characters in the filename
  813. let esc_fname = escape(fnamemodify(fname, ':t'), ".\\" .
  814. \ s:esc_filename_chars)
  815. let esc_fname = substitute(esc_fname, '&', '&&', 'g')
  816. " Truncate the directory name if it is long
  817. let dir_name = fnamemodify(fname, ':h')
  818. if v:version >= 800 || has('patch-7.4.1730')
  819. let len = strchars(dir_name)
  820. " Shorten long file names by adding only few characters from
  821. " the beginning and end.
  822. if len > 30
  823. let dir_name = strcharpart(dir_name, 0, 10) .
  824. \ '...' .
  825. \ strcharpart(dir_name, len - 20)
  826. endif
  827. else
  828. let len = strlen(dir_name)
  829. " Shorten long file names by adding only few characters from
  830. " the beginning and end.
  831. if len > 30
  832. let dir_name = strpart(dir_name, 0, 10) .
  833. \ '...' .
  834. \ strpart(dir_name, len - 20)
  835. endif
  836. endif
  837. let esc_dir_name = escape(dir_name, ".\\" . s:esc_filename_chars)
  838. let esc_dir_name = substitute(esc_dir_name, '&', '&&', 'g')
  839. let menu_path = '&File.&Recent\ Files.' . a:prefix . esc_fname .
  840. \ '\ (' . esc_dir_name . ')'
  841. let esc_mfname = s:MRU_escape_filename(fname)
  842. exe 'anoremenu <silent> ' . menu_path .
  843. \ " :call <SID>MRU_Edit_File('" . esc_mfname . "', 1, '')<CR>"
  844. exe 'tmenu ' . menu_path . ' Edit file ' . esc_mfname
  845. endfor
  846. endfunc
  847. " MRU_Refresh_Menu {{{1
  848. " Refresh the MRU menu
  849. func! s:MRU_Refresh_Menu() abort
  850. if !has('menu') || !g:MRU_Add_Menu
  851. " No support for menus
  852. return
  853. endif
  854. " Setup the cpoptions properly for the maps to work
  855. let old_cpoptions = &cpoptions
  856. set cpoptions&vim
  857. " Remove the MRU menu
  858. " To retain the teared-off MRU menu, we need to add a dummy entry
  859. silent! unmenu &File.&Recent\ Files
  860. " The menu priority of the File menu is 10. If the MRU plugin runs
  861. " first before menu.vim, the File menu order may not be correct.
  862. " So specify the priority of the File menu here.
  863. 10noremenu &File.&Recent\ Files.Dummy <Nop>
  864. silent! unmenu! &File.&Recent\ Files
  865. anoremenu <silent> &File.&Recent\ Files.Refresh\ list
  866. \ :call <SID>MRU_LoadList()<CR>
  867. exe 'tmenu File.&Recent\ Files.Refresh\ list Reload the MRU file list from '
  868. \ . s:MRU_escape_filename(s:MRU_File)
  869. anoremenu File.&Recent\ Files.-SEP1- :
  870. " Add the filenames in the MRU list to the menu
  871. let entry_cnt = len(s:MRU_files)
  872. if entry_cnt > g:MRU_Max_Menu_Entries
  873. " Show only MRU_Max_Menu_Entries file names in the menu
  874. let mru_list = s:MRU_files[0 : g:MRU_Max_Menu_Entries - 1]
  875. let entry_cnt = g:MRU_Max_Menu_Entries
  876. else
  877. let mru_list = s:MRU_files
  878. endif
  879. if entry_cnt > g:MRU_Max_Submenu_Entries
  880. " Split the MRU menu into sub-menus
  881. for start_idx in range(0, entry_cnt, g:MRU_Max_Submenu_Entries)
  882. let last_idx = start_idx + g:MRU_Max_Submenu_Entries - 1
  883. if last_idx >= entry_cnt
  884. let last_idx = entry_cnt - 1
  885. endif
  886. let prefix = 'Files\ (' . (start_idx + 1) . '\.\.\.' .
  887. \ (last_idx + 1) . ').'
  888. call s:MRU_add_files_to_menu(prefix,
  889. \ mru_list[start_idx : last_idx])
  890. endfor
  891. else
  892. call s:MRU_add_files_to_menu('', mru_list)
  893. endif
  894. " Remove the dummy menu entry
  895. unmenu &File.&Recent\ Files.Dummy
  896. " Restore the previous cpoptions settings
  897. let &cpoptions = old_cpoptions
  898. endfunc
  899. " MRU_Refresh {{{1
  900. " Remove non-existing files from the MRU list
  901. func s:MRU_Refresh()
  902. call filter(s:MRU_files, 'filereadable(v:val)')
  903. call s:MRU_SaveList()
  904. call s:MRU_Refresh_Menu()
  905. endfunc
  906. " MRU_Delete_From_List {{{1
  907. " remove the entry under cursor in the MRU window from the MRU list
  908. func s:MRU_Delete_From_List()
  909. call filter(s:MRU_files,
  910. \ 'v:val != matchstr(getline("."), g:MRU_Filename_Format.parser)')
  911. setlocal modifiable
  912. del _
  913. setlocal nomodifiable
  914. call s:MRU_SaveList()
  915. call s:MRU_Refresh_Menu()
  916. endfunc
  917. " Return the list of file names in the MRU list {{{1
  918. func MruGetFiles(...)
  919. " Load the latest MRU list
  920. call s:MRU_LoadList()
  921. if a:0 == 1
  922. if g:MRU_FuzzyMatch
  923. " Return only the files fuzzy matching the specified pattern
  924. return filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:1)->empty()')
  925. endif
  926. " Return only the files matching the specified pattern
  927. return filter(copy(s:MRU_files), 'v:val =~? a:1')
  928. endif
  929. return copy(s:MRU_files)
  930. endfunc
  931. " Load the MRU list on plugin startup
  932. call s:MRU_LoadList()
  933. " Set the first entry in the MRU list as the alternate file
  934. " Credit to Martin Roa Villescas (https://github.com/mroavi) for the patch.
  935. " bufadd() is available starting from Vim 8.1.1610
  936. if g:MRU_Set_Alternate_File == 1 &&
  937. \ (v:version >= 802 || has('patch-8.1.1610') || has('nvim'))
  938. if !empty(s:MRU_files)
  939. let first_mru_file = s:MRU_files[0]
  940. if filereadable(first_mru_file)
  941. call bufadd(first_mru_file)
  942. let @# = first_mru_file
  943. endif
  944. endif
  945. endif
  946. " MRU autocommands {{{1
  947. " Autocommands to update the most recently used files
  948. augroup MRUAutoCmds
  949. au!
  950. autocmd BufRead * call s:MRU_AddFile(expand('<abuf>'))
  951. autocmd BufWritePost * call s:MRU_AddFile(expand('<abuf>'))
  952. autocmd BufEnter * call s:MRU_AddFile(expand('<abuf>'))
  953. " The ':vimgrep' command adds all the files searched to the buffer list.
  954. " This also modifies the MRU list, even though the user didn't edit the
  955. " files. Use the following autocmds to prevent this.
  956. autocmd QuickFixCmdPre *vimgrep* let s:mru_list_locked = 1
  957. autocmd QuickFixCmdPost *vimgrep* let s:mru_list_locked = 0
  958. augroup END
  959. " MRU custom commands {{{1
  960. if v:version >= 800
  961. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 MRU
  962. \ call s:MRU_Cmd(<q-args>, <q-mods>, <count>)
  963. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 Mru
  964. \ call s:MRU_Cmd(<q-args>, <q-mods>, <count>)
  965. command! -nargs=? -complete=customlist,s:MRU_Complete MRUToggle
  966. \ call s:MRU_Toggle(<q-args>, <q-mods>)
  967. else
  968. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 MRU
  969. \ call s:MRU_Cmd(<q-args>, '', <count>)
  970. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 Mru
  971. \ call s:MRU_Cmd(<q-args>, '', <count>)
  972. command! -nargs=? -complete=customlist,s:MRU_Complete MRUToggle
  973. \ call s:MRU_Toggle(<q-args>, '')
  974. endif
  975. command! -nargs=0 MruRefresh call s:MRU_Refresh()
  976. " FZF (fuzzy finder) integration {{{1
  977. func s:MRU_FZF_EditFile(fname) abort
  978. call s:MRU_Window_Edit_File(a:fname, 0, 'edit', 'useopen')
  979. endfunc
  980. func s:MRU_FZF_Run() abort
  981. if !exists('*fzf#run')
  982. call s:MRU_Warn_Msg('FZF plugin is not present')
  983. return
  984. endif
  985. " Load the latest MRU list
  986. call s:MRU_LoadList()
  987. call fzf#run(fzf#wrap({'source' : s:MRU_files,
  988. \ 'options' : '--no-sort',
  989. \ 'sink' : function('s:MRU_FZF_EditFile')}, 0))
  990. endfunc
  991. command! -nargs=0 FZFMru call s:MRU_FZF_Run()
  992. " }}}
  993. " restore 'cpoptions'
  994. let &cpoptions = s:cpo_save
  995. unlet s:cpo_save
  996. " vim:set sw=2 sts=2 foldenable foldmethod=marker: