mru.vim 37 KB

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