mru.vim 37 KB

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