mru.vim 37 KB

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