mru.vim 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. setlocal 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. let mod = ''
  431. if g:MRU_Use_Current_Window
  432. let mod = 'keepalt '
  433. endif
  434. if a:edit_type ==# 'edit'
  435. if bufexists(esc_fname)
  436. exe mod . 'buffer ' . esc_fname
  437. else
  438. exe mod . 'edit ' . esc_fname
  439. endif
  440. else
  441. exe mod . 'view ' . esc_fname
  442. endif
  443. endif
  444. endif
  445. endif
  446. " Make the buffer a listed buffer (in case it was deleted before)
  447. setlocal buflisted
  448. endfunc
  449. " MRU_Select_File_Cmd {{{1
  450. " Open a file selected from the MRU window
  451. "
  452. " 'opt' has two values separated by comma. The first value specifies how to
  453. " edit the file and can be either 'edit' or 'view'. The second value
  454. " specifies where to open the file. It can take one of the following values:
  455. " 'useopen' to open file in the previous window
  456. " 'newwin_horiz' to open the file in a new horizontal split window
  457. " 'newwin_vert' to open the file in a new vertical split window.
  458. " 'newtab' to open the file in a new tab.
  459. " If multiple file names are selected using visual mode, then open multiple
  460. " files (either in split windows or tabs)
  461. func! s:MRU_Select_File_Cmd(opt) range abort
  462. let [edit_type, open_type] = split(a:opt, ',')
  463. let fnames = getline(a:firstline, a:lastline)
  464. if g:MRU_Auto_Close == 1 && g:MRU_Use_Current_Window == 0
  465. " Automatically close the window if the file window is
  466. " not used to display the MRU list.
  467. silent! close
  468. endif
  469. let multi = 0
  470. for f in fnames
  471. if empty(f)
  472. continue
  473. endif
  474. " The text in the MRU window contains the filename in parenthesis
  475. let file = matchstr(f, g:MRU_Filename_Format.parser)
  476. call s:MRU_Window_Edit_File(file, multi, edit_type, open_type)
  477. if a:firstline != a:lastline
  478. " Opening multiple files
  479. let multi = 1
  480. endif
  481. endfor
  482. endfunc
  483. " MRU_Warn_Msg {{{1
  484. " Display a warning message
  485. func! s:MRU_Warn_Msg(msg) abort
  486. echohl WarningMsg
  487. echo a:msg
  488. echohl None
  489. endfunc
  490. " MRU_Open_Window {{{1
  491. " Display the Most Recently Used file list in a temporary window.
  492. " If the 'pat' argument is not empty, then it specifies the pattern of files
  493. " to selectively display in the MRU window.
  494. " The 'splitdir' argument specifies the location (topleft, belowright, etc.)
  495. " of the MRU window.
  496. func! s:MRU_Open_Window(pat, splitdir, winsz) abort
  497. " Load the latest MRU file list
  498. call s:MRU_LoadList()
  499. " Check for empty MRU list
  500. if empty(s:MRU_files)
  501. call s:MRU_Warn_Msg('MRU file list is empty')
  502. return
  503. endif
  504. " Save the current buffer number. This is used later to open a file when a
  505. " entry is selected from the MRU window. The window number is not saved,
  506. " as the window number will change when new windows are opened.
  507. let s:MRU_last_buffer = bufnr('%')
  508. let bname = s:MRU_buf_name
  509. " If the window is already open, jump to it
  510. let winnum = bufwinnr(bname)
  511. if winnum != -1
  512. if winnr() != winnum
  513. " If not already in the window, jump to it
  514. exe winnum . 'wincmd w'
  515. endif
  516. setlocal modifiable
  517. " Delete the contents of the buffer to the black-hole register
  518. silent! %delete _
  519. else
  520. if g:MRU_Use_Current_Window
  521. " Reuse the current window
  522. " If the current buffer has unsaved changes or is a special buffer
  523. " or is the preview window and 'hidden' is not set, then open a
  524. " new window. Otherwise, open in the current window.
  525. if !&hidden && (&modified || !empty(&buftype) || &previewwindow)
  526. let split_window = 1
  527. else
  528. let split_window = 0
  529. endif
  530. " If the __MRU_Files__ buffer exists, then reuse it. Otherwise open
  531. " a new buffer
  532. let bufnum = bufnr(bname)
  533. if bufnum == -1
  534. if split_window
  535. let cmd = 'botright split edit ' . bname
  536. else
  537. let cmd = 'edit ' . bname
  538. endif
  539. else
  540. if split_window
  541. let cmd = 'botright sbuffer ' . bufnum
  542. else
  543. let cmd = 'buffer ' . bufnum
  544. endif
  545. endif
  546. exe cmd
  547. if bufnr('%') != bufnr(bname)
  548. " Failed to edit the MRU buffer
  549. return
  550. endif
  551. else
  552. " Open a new window at the bottom
  553. let cmd = 'silent! '
  554. if empty(a:splitdir)
  555. let cmd .= 'botright '
  556. else
  557. let cmd .= a:splitdir . ' '
  558. endif
  559. let sz = a:winsz
  560. if sz == 0
  561. let sz = g:MRU_Window_Height
  562. endif
  563. let cmd .= sz . 'split '
  564. " If the __MRU_Files__ buffer exists, then reuse it. Otherwise open
  565. " a new buffer
  566. let bufnum = bufnr(bname)
  567. if bufnum == -1
  568. let cmd .= bname
  569. else
  570. let cmd .= '+buffer' . bufnum
  571. endif
  572. exe cmd
  573. endif
  574. endif
  575. setlocal modifiable
  576. " Mark the buffer as scratch
  577. setlocal buftype=nofile
  578. if g:MRU_Use_Current_Window
  579. " avoid using mru buffer as alternate file
  580. setlocal bufhidden=wipe
  581. else
  582. setlocal bufhidden=delete
  583. endif
  584. setlocal noswapfile
  585. setlocal nobuflisted
  586. setlocal nowrap
  587. setlocal nonumber
  588. if exists('&relativenumber')
  589. setlocal norelativenumber
  590. endif
  591. if exists('&signcolumn')
  592. setlocal signcolumn=no
  593. endif
  594. setlocal foldcolumn=0
  595. " Set the 'filetype' to 'mru'. This allows the user to apply custom
  596. " syntax highlighting or other changes to the MRU bufer.
  597. setlocal filetype=mru
  598. " Use fixed height and width for the MRU window
  599. setlocal winfixheight winfixwidth
  600. " Setup the cpoptions properly for the maps to work
  601. let old_cpoptions = &cpoptions
  602. set cpoptions&vim
  603. " Create mappings to select and edit a file from the MRU list
  604. nnoremap <buffer> <silent> <CR>
  605. \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
  606. vnoremap <buffer> <silent> <CR>
  607. \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
  608. nnoremap <buffer> <silent> o
  609. \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
  610. vnoremap <buffer> <silent> o
  611. \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
  612. nnoremap <buffer> <silent> <S-CR>
  613. \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
  614. vnoremap <buffer> <silent> <S-CR>
  615. \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
  616. nnoremap <buffer> <silent> O
  617. \ :call <SID>MRU_Select_File_Cmd('edit,newwin_vert')<CR>
  618. vnoremap <buffer> <silent> O
  619. \ :call <SID>MRU_Select_File_Cmd('edit,newwin_vert')<CR>
  620. nnoremap <buffer> <silent> t
  621. \ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR>
  622. vnoremap <buffer> <silent> t
  623. \ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR>
  624. nnoremap <buffer> <silent> v
  625. \ :call <SID>MRU_Select_File_Cmd('view,useopen')<CR>
  626. nnoremap <buffer> <silent> p
  627. \ :call <SID>MRU_Select_File_Cmd('view,preview')<CR>
  628. vnoremap <buffer> <silent> p
  629. \ :<C-u>if line("'<") == line("'>")<Bar>
  630. \ call <SID>MRU_Select_File_Cmd('open,preview')<Bar>
  631. \ else<Bar>
  632. \ echoerr "Only a single file can be previewed"<Bar>
  633. \ endif<CR>
  634. nnoremap <buffer> <silent> u :MRU<CR>
  635. nnoremap <buffer> <silent> <2-LeftMouse>
  636. \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
  637. nnoremap <buffer> <silent> d
  638. \ :<C-U>call <SID>MRU_Delete_From_List()<CR>
  639. nnoremap <buffer> <silent> q :close<CR>
  640. " Restore the previous cpoptions settings
  641. let &cpoptions = old_cpoptions
  642. " Display the MRU list
  643. if empty(a:pat)
  644. " No search pattern specified. Display the complete list
  645. let m = copy(s:MRU_files)
  646. else
  647. " Display only the entries matching the specified pattern. First try
  648. " fuzzy matching or as a literal pattern.
  649. if g:MRU_FuzzyMatch
  650. let m = matchfuzzy(s:MRU_files, a:pat)
  651. else
  652. let m = filter(copy(s:MRU_files), 'stridx(v:val, a:pat) != -1')
  653. endif
  654. if len(m) == 0
  655. " No match. Try using it as a regular expression
  656. let m = filter(copy(s:MRU_files), 'v:val =~# a:pat')
  657. endif
  658. endif
  659. " Get the tail part of the file name (without the directory) and display
  660. " it along with the full path in parenthesis.
  661. let output = map(m, g:MRU_Filename_Format.formatter)
  662. silent! 0put =output
  663. " Delete the empty line at the end of the buffer
  664. silent! $delete _
  665. " Move the cursor to the beginning of the file
  666. normal! gg
  667. " Add syntax highlighting for the file names
  668. if has_key(g:MRU_Filename_Format, 'syntax')
  669. exe "syntax match MRUFileName '" . g:MRU_Filename_Format.syntax . "'"
  670. highlight default link MRUFileName Identifier
  671. endif
  672. setlocal nomodifiable
  673. endfunc
  674. " MRU_Complete {{{1
  675. " Command-line completion function used by :MRU command
  676. func! s:MRU_Complete(ArgLead, CmdLine, CursorPos) abort
  677. if empty(a:ArgLead)
  678. " Return the complete list of MRU files
  679. return s:MRU_files
  680. else
  681. if g:MRU_FuzzyMatch
  682. " Return only the files fuzzy matching the specified pattern
  683. return matchfuzzy(s:MRU_files, a:ArgLead)
  684. else
  685. " Return only the files matching the specified pattern
  686. return filter(copy(s:MRU_files), 'v:val =~? a:ArgLead')
  687. endif
  688. endif
  689. endfunc
  690. " MRU_Cmd {{{1
  691. " Function to handle the MRU command
  692. " pat - File name pattern passed to the MRU command
  693. func! s:MRU_Cmd(pat, splitdir, winsz) abort
  694. if empty(a:pat)
  695. " No arguments specified. Open the MRU window
  696. call s:MRU_Open_Window('', a:splitdir, a:winsz)
  697. return
  698. endif
  699. " Load the latest MRU file
  700. call s:MRU_LoadList()
  701. " Empty MRU list
  702. if empty(s:MRU_files)
  703. call s:MRU_Warn_Msg('MRU file list is empty')
  704. return
  705. endif
  706. " If Vim supports fuzzy matching, then try fuzzy matching the pattern
  707. " against the file names. Otherwise, use the specified string as a literal
  708. " string and search for filenames containing the string. If only one
  709. " filename is found, then edit it (unless the user wants to open the MRU
  710. " window always)
  711. if g:MRU_FuzzyMatch
  712. let m = matchfuzzy(s:MRU_files, a:pat)
  713. else
  714. let m = filter(copy(s:MRU_files), 'stridx(v:val, a:pat) != -1')
  715. endif
  716. if len(m) > 0
  717. if len(m) == 1 && !g:MRU_Window_Open_Always
  718. call s:MRU_Edit_File(m[0], 0, a:splitdir)
  719. return
  720. endif
  721. " More than one file matches. Try to find an accurate match
  722. let new_m = filter(m, 'v:val ==# a:pat')
  723. if len(new_m) == 1 && !g:MRU_Window_Open_Always
  724. call s:MRU_Edit_File(new_m[0], 0, a:splitdir)
  725. return
  726. endif
  727. " Couldn't find an exact match, open the MRU window with all the
  728. " files matching the pattern.
  729. call s:MRU_Open_Window(a:pat, a:splitdir, a:winsz)
  730. return
  731. endif
  732. " Use the specified string as a regular expression pattern and search
  733. " for filenames matching the pattern
  734. let m = filter(copy(s:MRU_files), 'v:val =~? a:pat')
  735. if len(m) == 0
  736. " If an existing file (not present in the MRU list) is specified,
  737. " then open the file.
  738. if filereadable(a:pat)
  739. call s:MRU_Edit_File(a:pat, 0, a:splitdir)
  740. return
  741. endif
  742. " No filenames matching the specified pattern are found
  743. call s:MRU_Warn_Msg("MRU file list doesn't contain " .
  744. \ 'files matching ' . a:pat)
  745. return
  746. endif
  747. if len(m) == 1 && !g:MRU_Window_Open_Always
  748. call s:MRU_Edit_File(m[0], 0, a:splitdir)
  749. return
  750. endif
  751. call s:MRU_Open_Window(a:pat, a:splitdir, a:winsz)
  752. endfunc
  753. " MRU_Toggle {{{1
  754. " Toggle MRU
  755. " pat - File name pattern passed to the MRU command
  756. func! s:MRU_Toggle(pat, splitdir) abort
  757. " If the MRU window is open, close it
  758. let winnum = bufwinnr(s:MRU_buf_name)
  759. if winnum != -1
  760. exe winnum . 'wincmd w'
  761. if g:MRU_Use_Current_Window && !empty(expand('#'))
  762. silent! b #
  763. else
  764. silent! close
  765. endif
  766. else
  767. call s:MRU_Cmd(a:pat, a:splitdir, '')
  768. endif
  769. endfunction
  770. " MRU_add_files_to_menu {{{1
  771. " Adds a list of files to the "Recent Files" sub menu under the "File" menu.
  772. " prefix - Prefix to use for each of the menu entries
  773. " file_list - List of file names to add to the menu
  774. func! s:MRU_add_files_to_menu(prefix, file_list) abort
  775. for fname in a:file_list
  776. " Escape special characters in the filename
  777. let esc_fname = escape(fnamemodify(fname, ':t'), ".\\" .
  778. \ s:esc_filename_chars)
  779. let esc_fname = substitute(esc_fname, '&', '&&', 'g')
  780. " Truncate the directory name if it is long
  781. let dir_name = fnamemodify(fname, ':h')
  782. if v:version >= 800 || has('patch-7.4.1730')
  783. let len = strchars(dir_name)
  784. " Shorten long file names by adding only few characters from
  785. " the beginning and end.
  786. if len > 30
  787. let dir_name = strcharpart(dir_name, 0, 10) .
  788. \ '...' .
  789. \ strcharpart(dir_name, len - 20)
  790. endif
  791. else
  792. let len = strlen(dir_name)
  793. " Shorten long file names by adding only few characters from
  794. " the beginning and end.
  795. if len > 30
  796. let dir_name = strpart(dir_name, 0, 10) .
  797. \ '...' .
  798. \ strpart(dir_name, len - 20)
  799. endif
  800. endif
  801. let esc_dir_name = escape(dir_name, ".\\" . s:esc_filename_chars)
  802. let esc_dir_name = substitute(esc_dir_name, '&', '&&', 'g')
  803. let menu_path = '&File.&Recent\ Files.' . a:prefix . esc_fname .
  804. \ '\ (' . esc_dir_name . ')'
  805. let esc_mfname = s:MRU_escape_filename(fname)
  806. exe 'anoremenu <silent> ' . menu_path .
  807. \ " :call <SID>MRU_Edit_File('" . esc_mfname . "', 1, '')<CR>"
  808. exe 'tmenu ' . menu_path . ' Edit file ' . esc_mfname
  809. endfor
  810. endfunc
  811. " MRU_Refresh_Menu {{{1
  812. " Refresh the MRU menu
  813. func! s:MRU_Refresh_Menu() abort
  814. if !has('menu') || !g:MRU_Add_Menu
  815. " No support for menus
  816. return
  817. endif
  818. " Setup the cpoptions properly for the maps to work
  819. let old_cpoptions = &cpoptions
  820. set cpoptions&vim
  821. " Remove the MRU menu
  822. " To retain the teared-off MRU menu, we need to add a dummy entry
  823. silent! unmenu &File.&Recent\ Files
  824. " The menu priority of the File menu is 10. If the MRU plugin runs
  825. " first before menu.vim, the File menu order may not be correct.
  826. " So specify the priority of the File menu here.
  827. 10noremenu &File.&Recent\ Files.Dummy <Nop>
  828. silent! unmenu! &File.&Recent\ Files
  829. anoremenu <silent> &File.&Recent\ Files.Refresh\ list
  830. \ :call <SID>MRU_LoadList()<CR>
  831. exe 'tmenu File.&Recent\ Files.Refresh\ list Reload the MRU file list from '
  832. \ . s:MRU_escape_filename(s:MRU_File)
  833. anoremenu File.&Recent\ Files.-SEP1- :
  834. " Add the filenames in the MRU list to the menu
  835. let entry_cnt = len(s:MRU_files)
  836. if entry_cnt > g:MRU_Max_Menu_Entries
  837. " Show only MRU_Max_Menu_Entries file names in the menu
  838. let mru_list = s:MRU_files[0 : g:MRU_Max_Menu_Entries - 1]
  839. let entry_cnt = g:MRU_Max_Menu_Entries
  840. else
  841. let mru_list = s:MRU_files
  842. endif
  843. if entry_cnt > g:MRU_Max_Submenu_Entries
  844. " Split the MRU menu into sub-menus
  845. for start_idx in range(0, entry_cnt, g:MRU_Max_Submenu_Entries)
  846. let last_idx = start_idx + g:MRU_Max_Submenu_Entries - 1
  847. if last_idx >= entry_cnt
  848. let last_idx = entry_cnt - 1
  849. endif
  850. let prefix = 'Files\ (' . (start_idx + 1) . '\.\.\.' .
  851. \ (last_idx + 1) . ').'
  852. call s:MRU_add_files_to_menu(prefix,
  853. \ mru_list[start_idx : last_idx])
  854. endfor
  855. else
  856. call s:MRU_add_files_to_menu('', mru_list)
  857. endif
  858. " Remove the dummy menu entry
  859. unmenu &File.&Recent\ Files.Dummy
  860. " Restore the previous cpoptions settings
  861. let &cpoptions = old_cpoptions
  862. endfunc
  863. " MRU_Refresh {{{1
  864. " Remove non-existing files from the MRU list
  865. func s:MRU_Refresh()
  866. call filter(s:MRU_files, 'filereadable(v:val)')
  867. call s:MRU_SaveList()
  868. call s:MRU_Refresh_Menu()
  869. endfunc
  870. " MRU_Delete_From_List {{{1
  871. " remove the entry under cursor in the MRU window from the MRU list
  872. func s:MRU_Delete_From_List()
  873. call filter(s:MRU_files,
  874. \ 'v:val != matchstr(getline("."), g:MRU_Filename_Format.parser)')
  875. setlocal modifiable
  876. del _
  877. setlocal nomodifiable
  878. call s:MRU_SaveList()
  879. call s:MRU_Refresh_Menu()
  880. endfunc
  881. " Return the list of file names in the MRU list {{{1
  882. func MruGetFiles(...)
  883. " Load the latest MRU list
  884. call s:MRU_LoadList()
  885. if a:0 == 1
  886. if g:MRU_FuzzyMatch
  887. " Return only the files fuzzy matching the specified pattern
  888. return matchfuzzy(s:MRU_files, a:1)
  889. endif
  890. " Return only the files matching the specified pattern
  891. return filter(copy(s:MRU_files), 'v:val =~? a:1')
  892. endif
  893. return copy(s:MRU_files)
  894. endfunc
  895. " Load the MRU list on plugin startup
  896. call s:MRU_LoadList()
  897. " MRU autocommands {{{1
  898. " Autocommands to update the most recently used files
  899. augroup MRUAutoCmds
  900. au!
  901. autocmd BufRead * call s:MRU_AddFile(expand('<abuf>'))
  902. autocmd BufWritePost * call s:MRU_AddFile(expand('<abuf>'))
  903. autocmd BufEnter * call s:MRU_AddFile(expand('<abuf>'))
  904. " The ':vimgrep' command adds all the files searched to the buffer list.
  905. " This also modifies the MRU list, even though the user didn't edit the
  906. " files. Use the following autocmds to prevent this.
  907. autocmd QuickFixCmdPre *vimgrep* let s:mru_list_locked = 1
  908. autocmd QuickFixCmdPost *vimgrep* let s:mru_list_locked = 0
  909. augroup END
  910. " MRU custom commands {{{1
  911. if v:version >= 800
  912. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 MRU
  913. \ call s:MRU_Cmd(<q-args>, <q-mods>, <count>)
  914. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 Mru
  915. \ call s:MRU_Cmd(<q-args>, <q-mods>, <count>)
  916. command! -nargs=? -complete=customlist,s:MRU_Complete MRUToggle
  917. \ call s:MRU_Toggle(<q-args>, <q-mods>)
  918. else
  919. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 MRU
  920. \ call s:MRU_Cmd(<q-args>, '', <count>)
  921. command! -nargs=? -complete=customlist,s:MRU_Complete -count=0 Mru
  922. \ call s:MRU_Cmd(<q-args>, '', <count>)
  923. command! -nargs=? -complete=customlist,s:MRU_Complete MRUToggle
  924. \ call s:MRU_Toggle(<q-args>, '')
  925. endif
  926. command! -nargs=0 MruRefresh call s:MRU_Refresh()
  927. " FZF (fuzzy finder) integration {{{1
  928. func s:MRU_FZF_EditFile(fname) abort
  929. call s:MRU_Window_Edit_File(a:fname, 0, 'edit', 'useopen')
  930. endfunc
  931. func s:MRU_FZF_Run() abort
  932. if !exists('*fzf#run')
  933. call s:MRU_Warn_Msg('FZF plugin is not present')
  934. return
  935. endif
  936. call fzf#run(fzf#wrap({'source' : s:MRU_files,
  937. \ 'options' : '--no-sort',
  938. \ 'sink' : function('s:MRU_FZF_EditFile'),
  939. \ 'down' : g:MRU_Window_Height}, 0))
  940. endfunc
  941. command! -nargs=0 FZFMru call s:MRU_FZF_Run()
  942. " }}}
  943. " restore 'cpoptions'
  944. let &cpoptions = s:cpo_save
  945. unlet s:cpo_save
  946. " vim:set sw=2 sts=2 foldenable foldmethod=marker: