mru.vim 37 KB

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