mru.vim 33 KB

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