mru.vim 32 KB

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