mru.vim 28 KB

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