mru.vim 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. " File: mru.vim
  2. " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
  3. " Version: 3.3
  4. " Last Modified: December 18, 2009
  5. " Copyright: Copyright (C) 2003-2009 Yegappan Lakshmanan
  6. " 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 damamges resulting from the use of this
  12. " software.
  13. "
  14. " Overview
  15. " --------
  16. " The Most Recently Used (MRU) plugin provides an easy access to a list of
  17. " recently opened/edited files in Vim. This plugin automatically stores the
  18. " file names as you open/edit them in Vim.
  19. "
  20. " This plugin will work on all the platforms where Vim is supported. This
  21. " plugin will work in both console and GUI Vim. This version of the MRU
  22. " plugin needs Vim 7.0 and above. If you are using an earlier version of
  23. " Vim, then you should use an older version of the MRU plugin.
  24. "
  25. " The recently used filenames are stored in a file specified by the Vim
  26. " MRU_File variable.
  27. "
  28. " Installation
  29. " ------------
  30. " 1. Copy the mru.vim file to one of the following directories:
  31. "
  32. " $HOME/.vim/plugin - Unix like systems
  33. " $HOME/vimfiles/plugin - MS-Windows
  34. " $VIM:vimfiles:plugin - Macintosh
  35. " $VIM/vimfiles/plugin - All
  36. "
  37. " Refer to the following Vim help topics for more information about Vim
  38. " plugins:
  39. "
  40. " :help add-plugin
  41. " :help add-global-plugin
  42. " :help runtimepath
  43. "
  44. " 2. Set the MRU_File Vim variable in the .vimrc file to the location of a
  45. " file to store the most recently edited file names. This step is needed
  46. " only if you want to change the default MRU filename.
  47. " 3. Restart Vim.
  48. " 4. You can use the ":MRU" command to list and edit the recently used files.
  49. " In GUI Vim, you can use the 'File->Recent Files' menu to access the
  50. " recently used files.
  51. "
  52. " To uninstall this plugin, remove this file (mru.vim) from the
  53. " $HOME/.vim/plugin or $HOME/vimfiles/plugin or the $VIM/vimfile/plugin
  54. " directory.
  55. "
  56. " Usage
  57. " -----
  58. " To list and edit files from the MRU list, you can use the ":MRU" command.
  59. " The ":MRU" command displays the MRU file list in a temporary Vim window. If
  60. " the MRU window is already opened, then the MRU list displayed in the window
  61. " is refreshed.
  62. "
  63. " If you are using GUI Vim, then the names of the recently edited files are
  64. " added to the "File->Recent Files" menu. You can select the name of a file
  65. " from this sub-menu to edit the file.
  66. "
  67. " You can use the normal Vim commands to move around in the MRU window. You
  68. " cannot make changes in the MRU window.
  69. "
  70. " You can select a file name to edit by pressing the <Enter> key or by double
  71. " clicking the left mouse button on a file name. The selected file will be
  72. " opened. If the file is already opened in a window, the cursor will be moved
  73. " to that window. Otherwise, the file is opened in the previous window. If the
  74. " previous window has a modified buffer or is the preview window or is used by
  75. " some other plugin, then the file is opened in a new window.
  76. "
  77. " You can press the 'o' key to open the file name under the cursor in the
  78. " MRU window in a new window.
  79. "
  80. " To open a file from the MRU window in read-only mode (view), press the 'v'
  81. " key.
  82. "
  83. " To open a file from the MRU window in a new tab, press the 't' key. If the
  84. " file is already opened in a window in the current or in another tab, then
  85. " the cursor is moved to that tab. Otherwise, a new tab is opened.
  86. "
  87. " You can open multiple files from the MRU window by specifying a count before
  88. " pressing '<Enter>' or 'v' or 'o' or 't'. You can also visually select
  89. " multiple filenames and invoke the commands to open the files. Each selected
  90. " file will be opened in a separate window or tab.
  91. "
  92. " You can press the 'u' key in the MRU window to update the file list. This is
  93. " useful if you keep the MRU window open always.
  94. "
  95. " You can close the MRU window by pressing the 'q' key or using one of the Vim
  96. " window commands.
  97. "
  98. " To display only files matching a pattern from the MRU list in the MRU
  99. " window, you can specify a pattern to the ":MRU" command. For example, to
  100. " display only file names containing "vim" in them, you can use the following
  101. " command ":MRU vim". When you specify a partial file name and only one
  102. " matching filename is found, then the ":MRU" command will edit that file.
  103. "
  104. " The ":MRU" command supports command-line completion of file names from
  105. " the MRU list. You can enter a partial file name and then press <Tab>
  106. " or <Ctrl-D> to complete or list all the matching file names. Note that
  107. " after typing the ":MRU" command, you have to enter a space before completing
  108. " the file names with <Tab>.
  109. "
  110. " When a file supplied to the ":MRU" command is not present in the MRU list,
  111. " but it is a readable file, then the file will be opened (even though it is
  112. " not present in the MRU list). This is useful if you want to open a file
  113. " present in the same directory as a file in the MRU list. You can use the
  114. " command-line completion of the ":MRU" command to complete the full path of a
  115. " file and then modify the path to open another file present in the same path.
  116. "
  117. " Whenever the MRU list changes, the MRU file is updated with the latest MRU
  118. " list. When you have multiple instances of Vim running at the same time, the
  119. " latest MRU list will show up in all the instances of Vim.
  120. "
  121. " Configuration
  122. " -------------
  123. " By changing the following variables you can configure the behavior of this
  124. " plugin. Set the following variables in your .vimrc file using the 'let'
  125. " command.
  126. "
  127. " The list of recently edited file names is stored in the file specified by the
  128. " MRU_File variable. The default setting for this variable is
  129. " $HOME/.vim_mru_files for Unix-like systems and $USERPROFILE/_vim_mru_files
  130. " for MS-Windows systems. You can change this variable to point to a file by
  131. " adding the following line to the .vimrc file:
  132. "
  133. " let MRU_File = 'd:\myhome\_vim_mru_files'
  134. "
  135. " By default, the plugin will remember the names of the last 100 used files.
  136. " As you edit more files, old file names will be removed from the MRU list.
  137. " You can set the 'MRU_Max_Entries' variable to remember more file names. For
  138. " example, to remember 1000 most recently used file names, you can use
  139. "
  140. " let MRU_Max_Entries = 1000
  141. "
  142. " By default, all the edited file names will be added to the MRU list. If you
  143. " want to exclude file names matching a list of patterns, you can set the
  144. " MRU_Exclude_Files variable to a list of Vim regular expressions. By default,
  145. " this variable is set to an empty string. For example, to not include files
  146. " in the temporary (/tmp, /var/tmp and d:\temp) directories, you can set the
  147. " MRU_Exclude_Files variable to
  148. "
  149. " let MRU_Exclude_Files = '^/tmp/.*\|^/var/tmp/.*' " For Unix
  150. " let MRU_Exclude_Files = '^c:\\temp\\.*' " For MS-Windows
  151. "
  152. " The specified pattern should be a Vim regular expression pattern.
  153. "
  154. " If you want to add only file names matching a set of patterns to the MRU
  155. " list, then you can set the MRU_Include_Files variable. This variable should
  156. " be set to a Vim regular expression pattern. For example, to add only .c and
  157. " .h files to the MRU list, you can set this variable as below:
  158. "
  159. " let MRU_Include_Files = '\.c$\|\.h$'
  160. "
  161. " By default, MRU_Include_Files is set to an empty string and all the edited
  162. " filenames are added to the MRU list.
  163. "
  164. " The default height of the MRU window is 8. You can set the MRU_Window_Height
  165. " variable to change the window height.
  166. "
  167. " let MRU_Window_Height = 15
  168. "
  169. " By default, when the :MRU command is invoked, the MRU list will be displayed
  170. " in a new window. Instead, if you want the MRU plugin to reuse the current
  171. " window, then you can set the 'MRU_Use_Current_Window' variable to one.
  172. "
  173. " let MRU_Use_Current_Window = 1
  174. "
  175. " The MRU plugin will reuse the current window. When a file name is selected,
  176. " the file is also opened in the current window.
  177. "
  178. " When you select a file from the MRU window, the MRU window will be
  179. " automatically closed and the selected file will be opened in the previous
  180. " window. You can set the 'MRU_Auto_Close' variable to zero to keep the MRU
  181. " window open.
  182. "
  183. " let MRU_Auto_Close = 0
  184. "
  185. " If you don't use the "File->Recent Files" menu and want to disable it,
  186. " then you can set the 'MRU_Add_Menu' variable to zero. By default, the
  187. " menu is enabled.
  188. "
  189. " let MRU_Add_Menu = 0
  190. "
  191. " If too many file names are present in the MRU list, then updating the MRU
  192. " menu to list all the file names makes Vim slow. To avoid this, the
  193. " MRU_Max_Menu_Entries variable controls the number of file names to show in
  194. " the MRU menu. By default, this is set to 10. You can change this to show
  195. " more entries in the menu.
  196. "
  197. " let MRU_Max_Menu_Entries = 20
  198. "
  199. " If many file names are present in the MRU list, then the MRU menu is split
  200. " into sub-menus. Each sub-menu contains MRU_Max_Submenu_Entries file names.
  201. " The default setting for this is 10. You can change this to increase the
  202. " number of file names displayed in a single sub-menu:
  203. "
  204. " let MRU_Max_Submenu_Entries = 15
  205. "
  206. " ****************** Do not modify after this line ************************
  207. if exists('loaded_mru')
  208. finish
  209. endif
  210. let loaded_mru=1
  211. if v:version < 700
  212. finish
  213. endif
  214. " Line continuation used here
  215. let s:cpo_save = &cpo
  216. set cpo&vim
  217. " MRU configuration variables {{{1
  218. " Maximum number of entries allowed in the MRU list
  219. if !exists('MRU_Max_Entries')
  220. let MRU_Max_Entries = 100
  221. endif
  222. " Files to exclude from the MRU list
  223. if !exists('MRU_Exclude_Files')
  224. let MRU_Exclude_Files = ''
  225. endif
  226. " Files to include in the MRU list
  227. if !exists('MRU_Include_Files')
  228. let MRU_Include_Files = ''
  229. endif
  230. " Height of the MRU window
  231. " Default height is 8
  232. if !exists('MRU_Window_Height')
  233. let MRU_Window_Height = 8
  234. endif
  235. if !exists('MRU_Use_Current_Window')
  236. let MRU_Use_Current_Window = 0
  237. endif
  238. if !exists('MRU_Auto_Close')
  239. let MRU_Auto_Close = 1
  240. endif
  241. if !exists('MRU_File')
  242. if has('unix') || has('macunix')
  243. let MRU_File = $HOME . '/.vim_mru_files'
  244. else
  245. let MRU_File = $VIM . '/_vim_mru_files'
  246. if has('win32')
  247. " MS-Windows
  248. if $USERPROFILE != ''
  249. let MRU_File = $USERPROFILE . '\_vim_mru_files'
  250. endif
  251. endif
  252. endif
  253. endif
  254. " Option for enabling or disabling the MRU menu
  255. if !exists('MRU_Add_Menu')
  256. let MRU_Add_Menu = 1
  257. endif
  258. " Maximum number of file names to show in the MRU menu. If too many files are
  259. " listed in the menu, then Vim becomes slow when updating the menu. So set
  260. " this to a low value.
  261. if !exists('MRU_Max_Menu_Entries')
  262. let MRU_Max_Menu_Entries = 10
  263. endif
  264. " Maximum number of file names to show in a MRU sub-menu. If the MRU list
  265. " contains more file names than this setting, then the MRU menu is split into
  266. " one or more sub-menus.
  267. if !exists('MRU_Max_Submenu_Entries')
  268. let MRU_Max_Submenu_Entries = 10
  269. endif
  270. " Control to temporarily lock the MRU list. Used to prevent files from
  271. " getting added to the MRU list when the ':vimgrep' command is executed.
  272. let s:mru_list_locked = 0
  273. " MRU_LoadList {{{1
  274. " Loads the latest list of file names from the MRU file
  275. function! s:MRU_LoadList()
  276. " If the MRU file is present, then load the list of filenames. Otherwise
  277. " start with an empty list.
  278. if filereadable(g:MRU_File)
  279. let s:MRU_files = readfile(g:MRU_File)
  280. if s:MRU_files[0] =~# '^\s*" Most recently edited files in Vim'
  281. " Generated by the previous version of the MRU plugin.
  282. " Discard the list.
  283. let s:MRU_files = []
  284. elseif s:MRU_files[0] =~# '^#'
  285. " Remove the comment line
  286. call remove(s:MRU_files, 0)
  287. else
  288. " Unsupported format
  289. let s:MRU_files = []
  290. endif
  291. else
  292. let s:MRU_files = []
  293. endif
  294. " Refresh the MRU menu with the latest list of filenames
  295. call s:MRU_Refresh_Menu()
  296. endfunction
  297. " MRU_SaveList {{{1
  298. " Saves the MRU file names to the MRU file
  299. function! s:MRU_SaveList()
  300. let l = []
  301. call add(l, '# Most recently edited files in Vim (version 3.0)')
  302. call extend(l, s:MRU_files)
  303. call writefile(l, g:MRU_File)
  304. endfunction
  305. " MRU_AddFile {{{1
  306. " Adds a file to the MRU file list
  307. " acmd_bufnr - Buffer number of the file to add
  308. function! s:MRU_AddFile(acmd_bufnr)
  309. if s:mru_list_locked
  310. " MRU list is currently locked
  311. return
  312. endif
  313. " Get the full path to the filename
  314. let fname = fnamemodify(bufname(a:acmd_bufnr + 0), ':p')
  315. if fname == ''
  316. return
  317. endif
  318. " Skip temporary buffers with buftype set. The buftype is set for buffers
  319. " used by plugins.
  320. if &buftype != ''
  321. return
  322. endif
  323. if g:MRU_Include_Files != ''
  324. " If MRU_Include_Files is set, include only files matching the
  325. " specified pattern
  326. if fname !~# g:MRU_Include_Files
  327. return
  328. endif
  329. endif
  330. if g:MRU_Exclude_Files != ''
  331. " Do not add files matching the pattern specified in the
  332. " MRU_Exclude_Files to the MRU list
  333. if fname =~# g:MRU_Exclude_Files
  334. return
  335. endif
  336. endif
  337. " If the filename is not already present in the MRU list and is not
  338. " readable then ignore it
  339. let idx = index(s:MRU_files, fname)
  340. if idx == -1
  341. if !filereadable(fname)
  342. " File is not readable and is not in the MRU list
  343. return
  344. endif
  345. endif
  346. " Load the latest MRU file list
  347. call s:MRU_LoadList()
  348. " Remove the new file name from the existing MRU list (if already present)
  349. call filter(s:MRU_files, 'v:val !=# fname')
  350. " Add the new file list to the beginning of the updated old file list
  351. call insert(s:MRU_files, fname, 0)
  352. " Trim the list
  353. if len(s:MRU_files) > g:MRU_Max_Entries
  354. call remove(s:MRU_files, g:MRU_Max_Entries, -1)
  355. endif
  356. " Save the updated MRU list
  357. call s:MRU_SaveList()
  358. " Refresh the MRU menu
  359. call s:MRU_Refresh_Menu()
  360. " If the MRU window is open, update the displayed MRU list
  361. let bname = '__MRU_Files__'
  362. let winnum = bufwinnr(bname)
  363. if winnum != -1
  364. let cur_winnr = winnr()
  365. call s:MRU_Open_Window()
  366. if winnr() != cur_winnr
  367. exe cur_winnr . 'wincmd w'
  368. endif
  369. endif
  370. endfunction
  371. " MRU_escape_filename {{{1
  372. " Escape special characters in a filename. Special characters in file names
  373. " that should be escaped (for security reasons)
  374. let s:esc_filename_chars = ' *?[{`$%#"|!<>();&' . "'\t\n"
  375. function! s:MRU_escape_filename(fname)
  376. return escape(a:fname, s:esc_filename_chars)
  377. endfunction
  378. " MRU_Edit_File {{{1
  379. " Edit the specified file
  380. " filename - Name of the file to edit
  381. " sanitized - Specifies whether the filename is already escape for special
  382. " characters or not.
  383. function! s:MRU_Edit_File(filename, sanitized)
  384. if !a:sanitized
  385. let esc_fname = s:MRU_escape_filename(a:filename)
  386. else
  387. let esc_fname = a:filename
  388. endif
  389. " If the file is already open in one of the windows, jump to it
  390. let winnum = bufwinnr('^' . a:filename . '$')
  391. if winnum != -1
  392. if winnum != winnr()
  393. exe winnum . 'wincmd w'
  394. endif
  395. else
  396. if &modified || &buftype != '' || &previewwindow
  397. " Current buffer has unsaved changes or is a special buffer or is
  398. " the preview window. So open the file in a new window
  399. exe 'split ' . esc_fname
  400. else
  401. exe 'edit ' . esc_fname
  402. endif
  403. endif
  404. endfunction
  405. " MRU_Window_Edit_File {{{1
  406. " fname : Name of the file to edit. May specify single or multiple
  407. " files.
  408. " edit_type : Specifies how to edit the file. Can be one of 'edit' or 'view'.
  409. " 'view' - Open the file as a read-only file
  410. " 'edit' - Edit the file as a regular file
  411. " multi : Specifies whether a single file or multiple files need to be
  412. " opened.
  413. " open_type : Specifies where to open the file. Can be one of 'useopen' or
  414. " 'newwin' or 'newtab'.
  415. " useopen - If the file is already present in a window, then
  416. " jump to that window. Otherwise, open the file in
  417. " the previous window.
  418. " newwin - Open the file in a new window.
  419. " newtab - Open the file in a new tab. If the file is already
  420. " opened in a tab, then jump to that tab.
  421. function! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type)
  422. let esc_fname = s:MRU_escape_filename(a:fname)
  423. if a:open_type == 'newwin'
  424. " Edit the file in a new window
  425. exe 'leftabove new ' . esc_fname
  426. elseif a:open_type == 'newtab'
  427. " If the selected file is already open in the current tab or in
  428. " another tab, jump to it. Otherwise open it in a new tab
  429. if bufwinnr('^' . a:fname . '$') == -1
  430. let tabnum = -1
  431. let i = 1
  432. let bnum = bufnr('^' . a:fname . '$')
  433. while i <= tabpagenr('$')
  434. if index(tabpagebuflist(i), bnum) != -1
  435. let tabnum = i
  436. break
  437. endif
  438. let i += 1
  439. endwhile
  440. if tabnum != -1
  441. " Goto the tab containing the file
  442. exe 'tabnext ' . i
  443. else
  444. " Open a new tab as the last tab page
  445. exe '999tabnew ' . esc_fname
  446. endif
  447. endif
  448. " Jump to the window containing the file
  449. let winnum = bufwinnr('^' . a:fname . '$')
  450. if winnum != winnr()
  451. exe winnum . 'wincmd w'
  452. endif
  453. else
  454. " If the selected file is already open in one of the windows,
  455. " jump to it
  456. let winnum = bufwinnr('^' . a:fname . '$')
  457. if winnum != -1
  458. exe winnum . 'wincmd w'
  459. else
  460. if g:MRU_Auto_Close == 1 && g:MRU_Use_Current_Window == 0
  461. " Jump to the window from which the MRU window was opened
  462. if exists('s:MRU_last_buffer')
  463. let last_winnr = bufwinnr(s:MRU_last_buffer)
  464. if last_winnr != -1 && last_winnr != winnr()
  465. exe last_winnr . 'wincmd w'
  466. endif
  467. endif
  468. else
  469. if g:MRU_Use_Current_Window == 0
  470. " Goto the previous window
  471. " If MRU_Use_Current_Window is set to one, then the
  472. " current window is used to open the file
  473. wincmd p
  474. endif
  475. endif
  476. let split_window = 0
  477. if &modified || &previewwindow || a:multi
  478. " Current buffer has unsaved changes or is the preview window
  479. " or the user is opening multiple files
  480. " So open the file in a new window
  481. let split_window = 1
  482. endif
  483. if &buftype != ''
  484. " Current buffer is a special buffer (maybe used by a plugin)
  485. if g:MRU_Use_Current_Window == 0 ||
  486. \ bufnr('%') != bufnr('__MRU_Files__')
  487. let split_window = 1
  488. endif
  489. endif
  490. " Edit the file
  491. if split_window
  492. " Current buffer has unsaved changes or is a special buffer or
  493. " is the preview window. So open the file in a new window
  494. if a:edit_type == 'edit'
  495. exe 'split ' . esc_fname
  496. else
  497. exe 'sview ' . esc_fname
  498. endif
  499. else
  500. if a:edit_type == 'edit'
  501. exe 'edit ' . esc_fname
  502. else
  503. exe 'view ' . esc_fname
  504. endif
  505. endif
  506. endif
  507. endif
  508. endfunction
  509. " MRU_Select_File_Cmd {{{1
  510. " Open a file selected from the MRU window
  511. "
  512. " 'opt' has two values separated by comma. The first value specifies how to
  513. " edit the file and can be either 'edit' or 'view'. The second value
  514. " specifies where to open the file. It can be 'useopen' to open file in the
  515. " previous window. It can be 'newwin' to open the file in a new window. It
  516. " can be 'newtab' to open the file in a new tab.
  517. "
  518. " If multiple file names are selected using visual mode, then open multiple
  519. " files (either in split windows or tabs)
  520. function! s:MRU_Select_File_Cmd(opt) range
  521. let [edit_type, open_type] = split(a:opt, ',')
  522. let fnames = getline(a:firstline, a:lastline)
  523. if g:MRU_Auto_Close == 1 && g:MRU_Use_Current_Window == 0
  524. " Automatically close the window if the file window is
  525. " not used to display the MRU list.
  526. silent! close
  527. endif
  528. let multi = 0
  529. for f in fnames
  530. if f == ''
  531. continue
  532. endif
  533. call s:MRU_Window_Edit_File(f, multi, edit_type, open_type)
  534. if a:firstline != a:lastline
  535. " Opening multiple files
  536. let multi = 1
  537. endif
  538. endfor
  539. endfunction
  540. " MRU_Warn_Msg {{{1
  541. " Display a warning message
  542. function! s:MRU_Warn_Msg(msg)
  543. echohl WarningMsg
  544. echo a:msg
  545. echohl None
  546. endfunction
  547. " MRU_Open_Window {{{1
  548. " Display the Most Recently Used file list in a temporary window.
  549. " If the optional argument is supplied, then it specifies the pattern of files
  550. " to selectively display in the MRU window.
  551. function! s:MRU_Open_Window(...)
  552. " Load the latest MRU file list
  553. call s:MRU_LoadList()
  554. " Check for empty MRU list
  555. if empty(s:MRU_files)
  556. call s:MRU_Warn_Msg('MRU file list is empty')
  557. return
  558. endif
  559. " Save the current buffer number. This is used later to open a file when a
  560. " entry is selected from the MRU window. The window number is not saved,
  561. " as the window number will change when new windows are opened.
  562. let s:MRU_last_buffer = bufnr('%')
  563. let bname = '__MRU_Files__'
  564. " If the window is already open, jump to it
  565. let winnum = bufwinnr(bname)
  566. if winnum != -1
  567. if winnr() != winnum
  568. " If not already in the window, jump to it
  569. exe winnum . 'wincmd w'
  570. endif
  571. setlocal modifiable
  572. " Delete the contents of the buffer to the black-hole register
  573. silent! %delete _
  574. else
  575. if g:MRU_Use_Current_Window
  576. " Reuse the current window
  577. "
  578. " If the __MRU_Files__ buffer exists, then reuse it. Otherwise open
  579. " a new buffer
  580. let bufnum = bufnr(bname)
  581. if bufnum == -1
  582. let cmd = 'edit ' . bname
  583. else
  584. let cmd = 'buffer ' . bufnum
  585. endif
  586. exe cmd
  587. if bufnr('%') != bufnr(bname)
  588. " Failed to edit the MRU buffer
  589. return
  590. endif
  591. else
  592. " Open a new window at the bottom
  593. " If the __MRU_Files__ buffer exists, then reuse it. Otherwise open
  594. " a new buffer
  595. let bufnum = bufnr(bname)
  596. if bufnum == -1
  597. let wcmd = bname
  598. else
  599. let wcmd = '+buffer' . bufnum
  600. endif
  601. exe 'silent! botright ' . g:MRU_Window_Height . 'split ' . wcmd
  602. endif
  603. endif
  604. " Mark the buffer as scratch
  605. setlocal buftype=nofile
  606. setlocal bufhidden=delete
  607. setlocal noswapfile
  608. setlocal nowrap
  609. setlocal nobuflisted
  610. " Use fixed height for the MRU window
  611. setlocal winfixheight
  612. " Setup the cpoptions properly for the maps to work
  613. let old_cpoptions = &cpoptions
  614. set cpoptions&vim
  615. " Create mappings to select and edit a file from the MRU list
  616. nnoremap <buffer> <silent> <CR>
  617. \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
  618. vnoremap <buffer> <silent> <CR>
  619. \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
  620. nnoremap <buffer> <silent> o
  621. \ :call <SID>MRU_Select_File_Cmd('edit,newwin')<CR>
  622. vnoremap <buffer> <silent> o
  623. \ :call <SID>MRU_Select_File_Cmd('edit,newwin')<CR>
  624. nnoremap <buffer> <silent> t
  625. \ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR>
  626. vnoremap <buffer> <silent> t
  627. \ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR>
  628. nnoremap <buffer> <silent> v
  629. \ :call <SID>MRU_Select_File_Cmd('view,useopen')<CR>
  630. nnoremap <buffer> <silent> u :MRU<CR>
  631. nnoremap <buffer> <silent> <2-LeftMouse>
  632. \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
  633. nnoremap <buffer> <silent> q :close<CR>
  634. " Restore the previous cpoptions settings
  635. let &cpoptions = old_cpoptions
  636. " Display the MRU list
  637. if a:0 == 0
  638. " No search pattern specified. Display the complete list
  639. silent! 0put =s:MRU_files
  640. else
  641. " Display only the entries matching the specified pattern
  642. " First try using it as a literal pattern
  643. let m = filter(copy(s:MRU_files), 'stridx(v:val, a:1) != -1')
  644. if len(m) == 0
  645. " No match. Try using it as a regular expression
  646. let m = filter(copy(s:MRU_files), 'v:val =~# a:1')
  647. endif
  648. silent! 0put =m
  649. endif
  650. " Move the cursor to the beginning of the file
  651. normal! gg
  652. setlocal nomodifiable
  653. endfunction
  654. " MRU_Complete {{{1
  655. " Command-line completion function used by :MRU command
  656. function! s:MRU_Complete(ArgLead, CmdLine, CursorPos)
  657. if a:ArgLead == ''
  658. " Return the complete list of MRU files
  659. return s:MRU_files
  660. else
  661. " Return only the files matching the specified pattern
  662. return filter(copy(s:MRU_files), 'v:val =~? a:ArgLead')
  663. endif
  664. endfunction
  665. " MRU_Cmd {{{1
  666. " Function to handle the MRU command
  667. " pat - File name pattern passed to the MRU command
  668. function! s:MRU_Cmd(pat)
  669. if a:pat == ''
  670. " No arguments specified. Open the MRU window
  671. call s:MRU_Open_Window()
  672. return
  673. endif
  674. " Load the latest MRU file
  675. call s:MRU_LoadList()
  676. " Empty MRU list
  677. if empty(s:MRU_files)
  678. call s:MRU_Warn_Msg('MRU file list is empty')
  679. return
  680. endif
  681. " First use the specified string as a literal string and search for
  682. " filenames containing the string. If only one filename is found,
  683. " then edit it.
  684. let m = filter(copy(s:MRU_files), 'stridx(v:val, a:pat) != -1')
  685. if len(m) > 0
  686. if len(m) == 1
  687. call s:MRU_Edit_File(m[0], 0)
  688. return
  689. endif
  690. " More than one file matches. Try find an accurate match
  691. let new_m = filter(m, 'v:val ==# a:pat')
  692. if len(new_m) == 1
  693. call s:MRU_Edit_File(new_m[0], 0)
  694. return
  695. endif
  696. " Couldn't find an exact match, open the MRU window with all the
  697. " files matching the pattern.
  698. call s:MRU_Open_Window(a:pat)
  699. return
  700. endif
  701. " Use the specified string as a regular expression pattern and search
  702. " for filenames matching the pattern
  703. let m = filter(copy(s:MRU_files), 'v:val =~? a:pat')
  704. if len(m) == 0
  705. " If an existing file (not present in the MRU list) is specified,
  706. " then open the file.
  707. if filereadable(a:pat)
  708. call s:MRU_Edit_File(a:pat, 0)
  709. return
  710. endif
  711. " No filenames matching the specified pattern are found
  712. call s:MRU_Warn_Msg("MRU file list doesn't contain " .
  713. \ "files containing " . a:pat)
  714. return
  715. endif
  716. if len(m) == 1
  717. call s:MRU_Edit_File(m[0], 0)
  718. return
  719. endif
  720. call s:MRU_Open_Window(a:pat)
  721. endfunction
  722. " MRU_add_files_to_menu {{{1
  723. " Adds a list of files to the "Recent Files" sub menu under the "File" menu.
  724. " prefix - Prefix to use for each of the menu entries
  725. " file_list - List of file names to add to the menu
  726. function! s:MRU_add_files_to_menu(prefix, file_list)
  727. for fname in a:file_list
  728. " Escape special characters in the filename
  729. let esc_fname = escape(fnamemodify(fname, ':t'), ".\\" .
  730. \ s:esc_filename_chars)
  731. let esc_fname = substitute(esc_fname, '&', '&&', 'g')
  732. " Truncate the directory name if it is long
  733. let dir_name = fnamemodify(fname, ':h')
  734. let len = strlen(dir_name)
  735. " Shorten long file names by adding only few characters from
  736. " the beginning and end.
  737. if len > 30
  738. let dir_name = strpart(dir_name, 0, 10) .
  739. \ '...' .
  740. \ strpart(dir_name, len - 20)
  741. endif
  742. let esc_dir_name = escape(dir_name, ".\\" . s:esc_filename_chars)
  743. let esc_dir_name = substitute(esc_dir_name, '&', '&&', 'g')
  744. let menu_path = '&File.&Recent\ Files.' . a:prefix . esc_fname .
  745. \ '\ (' . esc_dir_name . ')'
  746. let esc_mfname = s:MRU_escape_filename(fname)
  747. exe 'anoremenu <silent> ' . menu_path .
  748. \ " :call <SID>MRU_Edit_File('" . esc_mfname . "', 1)<CR>"
  749. exe 'tmenu ' . menu_path . ' Edit file ' . esc_mfname
  750. endfor
  751. endfunction
  752. " MRU_Refresh_Menu {{{1
  753. " Refresh the MRU menu
  754. function! s:MRU_Refresh_Menu()
  755. if !has('menu') || !g:MRU_Add_Menu
  756. " No support for menus
  757. return
  758. endif
  759. " Setup the cpoptions properly for the maps to work
  760. let old_cpoptions = &cpoptions
  761. set cpoptions&vim
  762. " Remove the MRU menu
  763. " To retain the teared-off MRU menu, we need to add a dummy entry
  764. silent! unmenu &File.&Recent\ Files
  765. " The menu priority of the File menu is 10. If the MRU plugin runs
  766. " first before menu.vim, the File menu order may not be correct.
  767. " So specify the priority of the File menu here.
  768. 10noremenu &File.&Recent\ Files.Dummy <Nop>
  769. silent! unmenu! &File.&Recent\ Files
  770. anoremenu <silent> &File.&Recent\ Files.Refresh\ list
  771. \ :call <SID>MRU_LoadList()<CR>
  772. exe 'tmenu File.&Recent\ Files.Refresh\ list Reload the MRU file list from '
  773. \ . s:MRU_escape_filename(g:MRU_File)
  774. anoremenu File.&Recent\ Files.-SEP1- :
  775. " Add the filenames in the MRU list to the menu
  776. let entry_cnt = len(s:MRU_files)
  777. if entry_cnt > g:MRU_Max_Menu_Entries
  778. " Show only MRU_Max_Menu_Entries file names in the menu
  779. let mru_list = s:MRU_files[1 : g:MRU_Max_Menu_Entries]
  780. let entry_cnt = g:MRU_Max_Menu_Entries
  781. else
  782. let mru_list = s:MRU_files
  783. endif
  784. if entry_cnt > g:MRU_Max_Submenu_Entries
  785. " Split the MRU menu into sub-menus
  786. for start_idx in range(0, entry_cnt, g:MRU_Max_Submenu_Entries)
  787. let last_idx = start_idx + g:MRU_Max_Submenu_Entries - 1
  788. if last_idx >= entry_cnt
  789. let last_idx = entry_cnt - 1
  790. endif
  791. let prefix = 'Files\ (' . (start_idx + 1) . '\.\.\.' .
  792. \ (last_idx + 1) . ').'
  793. call s:MRU_add_files_to_menu(prefix,
  794. \ mru_list[start_idx : last_idx])
  795. endfor
  796. else
  797. call s:MRU_add_files_to_menu('', mru_list)
  798. endif
  799. " Remove the dummy menu entry
  800. unmenu &File.&Recent\ Files.Dummy
  801. " Restore the previous cpoptions settings
  802. let &cpoptions = old_cpoptions
  803. endfunction
  804. " Load the MRU list on plugin startup
  805. call s:MRU_LoadList()
  806. " MRU autocommands {{{1
  807. " Autocommands to detect the most recently used files
  808. autocmd BufRead * call s:MRU_AddFile(expand('<abuf>'))
  809. autocmd BufNewFile * call s:MRU_AddFile(expand('<abuf>'))
  810. autocmd BufWritePost * call s:MRU_AddFile(expand('<abuf>'))
  811. " The ':vimgrep' command adds all the files searched to the buffer list.
  812. " This also modifies the MRU list, even though the user didn't edit the
  813. " files. Use the following autocmds to prevent this.
  814. autocmd QuickFixCmdPre *vimgrep* let s:mru_list_locked = 1
  815. autocmd QuickFixCmdPost *vimgrep* let s:mru_list_locked = 0
  816. " Command to open the MRU window
  817. command! -nargs=? -complete=customlist,s:MRU_Complete MRU
  818. \ call s:MRU_Cmd(<q-args>)
  819. command! -nargs=? -complete=customlist,s:MRU_Complete Mru
  820. \ call s:MRU_Cmd(<q-args>)
  821. " }}}
  822. " restore 'cpo'
  823. let &cpo = s:cpo_save
  824. unlet s:cpo_save
  825. " vim:set foldenable foldmethod=marker: