GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Version 3.1 · vim-scripts/mru.vim@17e7597

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • plugin
    • mru.vim
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎plugin/mru.vim‎

Copy file name to clipboardExpand all lines: plugin/mru.vim
+130-87Lines changed: 130 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" File: mru.vim
22
" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
3-
" Version: 3.0
4-
" Last Modified: January 12, 2008
3+
" Version: 3.1
4+
" Last Modified: February 17, 2008
55
"
66
" Overview
77
" --------
@@ -19,10 +19,20 @@
1919
"
2020
" Installation
2121
" ------------
22-
" 1. Copy the mru.vim script to the $HOME/.vim/plugin or the
23-
" $HOME/vimfiles/plugin or the $VIM/vimfiles directory. Refer to the
24-
" ':help add-plugin', ':help add-global-plugin' and ':help runtimepath'
25-
" topics for more details about Vim plugins.
22+
" 1. Copy the mru.vim file to one of the following directories:
23+
"
24+
" $HOME/.vim/plugin - Unix like systems
25+
" $HOME/vimfiles/plugin - MS-Windows
26+
" $VIM:vimfiles:plugin - Macintosh
27+
" $VIM/vimfiles/plugin - All
28+
"
29+
" Refer to the following Vim help topics for more information about Vim
30+
" plugins:
31+
"
32+
" :help add-plugin
33+
" :help add-global-plugin
34+
" :help runtimepath
35+
"
2636
" 2. Set the MRU_File Vim variable in the .vimrc file to the location of a
2737
" file to store the most recently edited file names. This step is needed
2838
" only if you want to change the default MRU filename.
@@ -31,6 +41,10 @@
3141
" In GUI Vim, you can use the 'File->Recent Files' menu to access the
3242
" recently used files.
3343
"
44+
" To uninstall this plugin, remove this file (mru.vim) from the
45+
" $HOME/.vim/plugin or $HOME/vimfiles/plugin or the $VIM/vimfile/plugin
46+
" directory.
47+
"
3448
" Usage
3549
" -----
3650
" You can use the ":MRU" command to list all the most recently edited file
@@ -57,7 +71,7 @@
5771
"
5872
" To open a file from the MRU window in a new tab, press the 't' key. If the
5973
" file is already opened in a window in the current or in another tab, then
60-
" the cursor is moved to that tab. Othewrise, a new tab is opened.
74+
" the cursor is moved to that tab. Otherwise, a new tab is opened.
6175
"
6276
" You can press the 'u' key in the MRU window to update the file list. This is
6377
" useful if you keep the MRU window open always.
@@ -68,11 +82,12 @@
6882
" To display only files matching a pattern from the MRU list in the MRU
6983
" window, you can specify a pattern to the ":MRU" command. For example, to
7084
" display only file names containing "vim" in them, you can use the following
71-
" command ":MRU vim"
85+
" command ":MRU vim". When you specify a partial file name and only one
86+
" matching filename is found, then the ":MRU" command will edit that file.
7287
"
73-
" You can use the ":MRUedit" command to edit files from the MRU list. This
74-
" command supports completion of file names from the MRU list. You can specify
75-
" a file either by the name or by the location in the MRU list.
88+
" The ":MRU" command supports command-line completion of file names from
89+
" the MRU list. You can enter a partial file name and then press <Tab>
90+
" or <Ctrl-D> to complete or list all the matching file names.
7691
"
7792
" Whenever the MRU list changes, the MRU file is updated with the latest MRU
7893
" list. When you have multiple instances of Vim running at the same time, the
@@ -132,11 +147,17 @@
132147
"
133148
" let MRU_Auto_Close = 0
134149
"
150+
" If you don't use the "File->Recent Files" menu and want to disable it,
151+
" then you can set the 'MRU_Add_Menu' variable to zero. By default, the
152+
" menu is enabled.
153+
"
154+
" let MRU_Add_Menu = 0
155+
"
135156
" ****************** Do not modify after this line ************************
136-
if exists('loaded_mru_autoload')
157+
if exists('loaded_mru')
137158
finish
138159
endif
139-
let loaded_mru_autoload=1
160+
let loaded_mru=1
140161

141162
if v:version < 700
142163
finish
@@ -178,6 +199,11 @@ if !exists('MRU_File')
178199
endif
179200
endif
180201

202+
" Option for enabling or disabling the MRU menu
203+
if !exists('MRU_Add_Menu')
204+
let MRU_Add_Menu = 1
205+
endif
206+
181207
" MRU_LoadList
182208
" Load the latest MRU file list from the MRU file
183209
function! s:MRU_LoadList()
@@ -276,7 +302,7 @@ endfunction
276302
" MRU_Edit_File
277303
" Edit the specified file
278304
function! s:MRU_Edit_File(filename)
279-
let fname = escape(a:filename, ' %#')
305+
let fname = escape(a:filename, ' %#"')
280306
" If the file is already open in one of the windows, jump to it
281307
let winnum = bufwinnr('^' . fname . '$')
282308
if winnum != -1
@@ -306,7 +332,7 @@ function! s:MRU_Window_Edit_File(win_opt)
306332
return
307333
endif
308334

309-
let fname = escape(fname, ' %#')
335+
let fname = escape(fname, ' %#"')
310336

311337
if a:win_opt == 'newwin'
312338
" Edit the file in a new window
@@ -349,7 +375,7 @@ function! s:MRU_Window_Edit_File(win_opt)
349375
exe 'tabnext ' . i
350376
else
351377
" Open a new tab as the last tab page
352-
exe '999tabnew ' . escape(fname, ' ')
378+
exe '999tabnew ' . fname
353379
endif
354380
endif
355381

@@ -409,6 +435,14 @@ function! s:MRU_Window_Edit_File(win_opt)
409435
endif
410436
endfunction
411437

438+
" MRU_Warn_Msg
439+
" Display a warning message
440+
function! s:MRU_Warn_Msg(msg)
441+
echohl WarningMsg
442+
echo a:msg
443+
echohl None
444+
endfunction
445+
412446
" MRU_Open_Window
413447
" Display the Most Recently Used file list in a temporary window.
414448
function! s:MRU_Open_Window(...)
@@ -418,7 +452,7 @@ function! s:MRU_Open_Window(...)
418452

419453
" Empty MRU list
420454
if empty(s:MRU_files)
421-
echohl WarningMsg | echo 'MRU file list is empty' | echohl None
455+
call s:MRU_Warn_Msg('MRU file list is empty')
422456
return
423457
endif
424458

@@ -521,9 +555,9 @@ function! s:MRU_Open_Window(...)
521555
setlocal nomodifiable
522556
endfunction
523557

524-
" MRU_Edit_Complete
525-
" Command-line completion function used by :MRUedit command
526-
function! s:MRU_Edit_Complete(ArgLead, CmdLine, CursorPos)
558+
" MRU_Complete
559+
" Command-line completion function used by :MRU command
560+
function! s:MRU_Complete(ArgLead, CmdLine, CursorPos)
527561
if a:ArgLead == ''
528562
" Return the list of MRU files
529563
return s:MRU_files
@@ -532,67 +566,79 @@ function! s:MRU_Edit_Complete(ArgLead, CmdLine, CursorPos)
532566
endif
533567
endfunction
534568

535-
" MRU_Edit_File_Cmd
536-
" Function to handle the MRUedit command
537-
function! s:MRU_Edit_File_Cmd(fspec)
538-
if a:fspec == ''
569+
" MRU_Cmd
570+
" Function to handle the MRU command
571+
function! s:MRU_Cmd(pat)
572+
if a:pat == ''
573+
" No arguments specified. Open the MRU window
574+
call s:MRU_Open_Window()
539575
return
540576
endif
541577

542578
" Load the latest MRU file
543579
call s:MRU_LoadList()
544580

545-
" User can specify either a number or a partial file name to
546-
" edit a file from the MRU list
547-
if a:fspec =~ '^\d\+$'
548-
" A number is specified
549-
let fnum = a:fspec
550-
if fnum <= 0 || fnum > len(s:MRU_files)
551-
echohl WarningMsg
552-
echo 'Error: valid range of values is 1 - ' . len(s:MRU_files)
553-
echohl None
554-
return
555-
endif
581+
" Empty MRU list
582+
if empty(s:MRU_files)
583+
call s:MRU_Warn_Msg('MRU file list is empty')
584+
return
585+
endif
556586

557-
" User index is 1 based, but the internal index is 0 based
558-
let fnum -= 1
587+
" First use the specified string as a literal string and search for
588+
" filenames containing the string. If only one filename is found,
589+
" then edit it.
590+
let m = filter(copy(s:MRU_files), 'stridx(v:val, a:pat) != -1')
591+
if len(m) == 1
592+
call s:MRU_Edit_File(m[0])
593+
return
594+
endif
559595

560-
let fname = s:MRU_files[fnum]
561-
if fname != ''
562-
call s:MRU_Edit_File(fname)
563-
endif
564-
else
565-
" Locate the file name matching the supplied partial name
596+
" Use the specified string as a regular expression pattern and search
597+
" for filenames matching the pattern
598+
let m = filter(copy(s:MRU_files), 'v:val =~? a:pat')
566599

567-
" Escape backslash in the partial filename, otherwise regexp
568-
" comparison will fail
569-
let fpat = escape(a:fspec, '\')
600+
if len(m) == 0
601+
" No filenames matching the specified pattern are found
602+
call s:MRU_Warn_Msg("MRU file list doesn't contain " .
603+
\ "files containing " . a:pat)
604+
return
605+
endif
570606

571-
let l = filter(copy(s:MRU_files), 'v:val =~ fpat')
572-
if len(l) == 0
573-
echohl WarningMsg
574-
echo 'Error: No matching file for ' . a:fspec
575-
echohl None
576-
return
577-
endif
607+
if len(m) == 1
608+
call s:MRU_Edit_File(m[0])
609+
return
610+
endif
578611

579-
if len(l) > 1
580-
echohl WarningMsg
581-
echo 'Error: More than one match for ' . a:fspec
582-
echohl None
583-
return
584-
endif
612+
call s:MRU_Open_Window(a:pat)
613+
endfunction
585614

586-
let fname = l[0]
615+
function! s:MRU_add_files_to_menu(prefix, file_list)
616+
for fname in a:file_list
617+
" Escape special characters in the filename
618+
let esc_fname = escape(fnamemodify(fname, ':t'), ". \\|\t%#")
587619

588-
call s:MRU_Edit_File(fname)
589-
endif
620+
" Truncate the directory name if it is long
621+
let dir_name = fnamemodify(fname, ':h')
622+
let len = strlen(dir_name)
623+
" Shorten long file names by adding only few characters from
624+
" the beginning and end.
625+
if len > 30
626+
let dir_name = strpart(dir_name, 0, 10) .
627+
\ '...' .
628+
\ strpart(dir_name, len - 20)
629+
endif
630+
let esc_dir_name = escape(dir_name, ". \\|\t")
631+
632+
exe 'anoremenu <silent> &File.Recent\ Files.' . a:prefix . esc_fname .
633+
\ '\ (' . esc_dir_name . ')' .
634+
\ " :call <SID>MRU_Edit_File('" . fname . "')<CR>"
635+
endfor
590636
endfunction
591637

592638
" MRU_Refresh_Menu()
593639
" Refresh the MRU menu
594640
function! s:MRU_Refresh_Menu()
595-
if !has('menu')
641+
if !has('menu') || !g:MRU_Add_Menu
596642
" No support for menus
597643
return
598644
endif
@@ -604,34 +650,32 @@ function! s:MRU_Refresh_Menu()
604650
" Remove the MRU menu
605651
" To retain the teared-off MRU menu, we need to add a dummy entry
606652
silent! unmenu &File.Recent\ Files
607-
noremenu &File.Recent\ Files.Dummy <Nop>
653+
" The menu priority of the File menu is 10. If the MRU plugin runs
654+
" first before menu.vim, the File menu order may not be correct.
655+
" So specify the priority of the File menu here.
656+
10noremenu &File.Recent\ Files.Dummy <Nop>
608657
silent! unmenu! &File.Recent\ Files
609658

610659
anoremenu <silent> &File.Recent\ Files.Refresh\ list
611660
\ :call <SID>MRU_LoadList()<CR>
612661
anoremenu File.Recent\ Files.-SEP1- :
613662

614663
" Add the filenames in the MRU list to the menu
615-
for fname in s:MRU_files
616-
" Escape special characters in the filename
617-
let esc_fname = escape(fnamemodify(fname, ':t'), ". \\|\t%#")
618-
619-
" Truncate the directory name if it is long
620-
let dir_name = fnamemodify(fname, ':h')
621-
let len = strlen(dir_name)
622-
" Shorten long file names by adding only few characters from
623-
" the beginning and end.
624-
if len > 30
625-
let dir_name = strpart(dir_name, 0, 10) .
626-
\ '...' .
627-
\ strpart(dir_name, len - 20)
628-
endif
629-
let esc_dir_name = escape(dir_name, ". \\|\t")
630-
631-
exe 'anoremenu <silent> &File.Recent\ Files.' . esc_fname .
632-
\ '\ (' . esc_dir_name . ')' .
633-
\ " :call <SID>MRU_Edit_File('" . fname . "')<CR>"
634-
endfor
664+
let entry_cnt = len(s:MRU_files)
665+
if entry_cnt > 10
666+
for start_idx in range(0, entry_cnt, 10)
667+
let last_idx = start_idx + 9
668+
if last_idx >= entry_cnt
669+
let last_idx = entry_cnt - 1
670+
endif
671+
let prefix = 'Files\ (' . (start_idx + 1) . '\.\.\.' .
672+
\ (last_idx + 1) . ').'
673+
call s:MRU_add_files_to_menu(prefix,
674+
\ s:MRU_files[start_idx : last_idx])
675+
endfor
676+
else
677+
call s:MRU_add_files_to_menu('', s:MRU_files)
678+
endif
635679

636680
" Remove the dummy menu entry
637681
unmenu &File.Recent\ Files.Dummy
@@ -649,9 +693,8 @@ autocmd BufNewFile * call s:MRU_AddFile(expand('<abuf>'))
649693
autocmd BufWritePost * call s:MRU_AddFile(expand('<abuf>'))
650694

651695
" Command to open the MRU window
652-
command! -nargs=? MRU call s:MRU_Open_Window(<q-args>)
653-
command! -nargs=1 -complete=customlist,s:MRU_Edit_Complete MRUedit
654-
\ call s:MRU_Edit_File_Cmd(<q-args>)
696+
command! -nargs=? -complete=customlist,s:MRU_Complete MRU
697+
\ call s:MRU_Cmd(<q-args>)
655698

656699
" restore 'cpo'
657700
let &cpo = s:cpo_save

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.