mru.vim 35 KB

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