mru.vim 31 KB

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