mru.vim 35 KB

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