mru.vim 31 KB

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