mru.vim 31 KB

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