mru.vim 31 KB

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