mru.vim 31 KB

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