Browse Source

Add support for setting the alternate file to the first file in the MRU list

Yegappan Lakshmanan 4 years ago
parent
commit
f30ba874a9
3 changed files with 68 additions and 4 deletions
  1. 11 1
      doc/mru.txt
  2. 22 2
      plugin/mru.vim
  3. 35 1
      test/unit_tests.vim

+ 11 - 1
doc/mru.txt

@@ -2,7 +2,7 @@
 
 Author: Yegappan Lakshmanan  (yegappan AT yahoo DOT com)
 For Vim version 7.0 and above
-Last change: August 13, 2021
+Last change: January 13, 2022
 
 ==============================================================================
 						*mru-license*
@@ -272,6 +272,7 @@ file. A summary of these variables is below:
   |MRU_Add_Menu|		add MRU files to the "Recent Files" menu
   |MRU_Max_Menu_Entries|	maximum number of entries in the MRU menu
   |MRU_Max_Submenu_Entries|	maximum number of entries in the MRU submenu
+  |MRU_Set_Alternate_File|	set the alternate file on plugin startup
   |MRU_Filename_Format|		patterns to populate and parse file names in
 				the MRU window
 
@@ -398,6 +399,15 @@ The default setting for this is 10. You can change this to increase the
 number of file names displayed in a single sub-menu:
 >
 	let MRU_Max_Submenu_Entries = 15
+<
+						*MRU_Set_Alternate_File*
+When the MRU plugin starts up, if the MRU_Set_Alternate_File variable is set
+to 1, then it sets the alternate file (|alternate-file|) to the first file in
+the MRU list. You can edit this file using the ":e #" command. By default this
+variable is set to 0. You can enable this behavior, by setting the
+MRU_Set_Alternate_File variable to 1:
+>
+	let MRU_Set_Alternate_File = 1
 <
 						*MRU_Filename_Format*
 In the MRU window, the filenames are displayed in two parts. The first part

+ 22 - 2
plugin/mru.vim

@@ -117,6 +117,13 @@ if !exists('MRU_FuzzyMatch')
   endif
 endif
 
+" Controls whether the alternate file (:help alternate-file) is set when the
+" plugin is loaded to the first file in the MRU list. Default is to set the
+" alternate file.
+if !exists('MRU_Set_Alternate_File')
+  let MRU_Set_Alternate_File = 0
+endif
+
 " Format of the file names displayed in the MRU window.
 " The default is to display the filename followed by the complete path to the
 " file in parenthesis. This variable controls the expressions used to format
@@ -397,7 +404,7 @@ func! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type) abort
   let esc_fname = s:MRU_escape_filename(a:fname)
 
   if a:open_type ==# 'newwin_horiz'
-    " Edit the file in a new horizontally split window above the previous
+    " Edit the file in a new horizontally split window below the previous
     " window
     wincmd p
     if bufexists(esc_fname)
@@ -406,7 +413,7 @@ func! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type) abort
       exe 'belowright new ' . esc_fname
     endif
   elseif a:open_type ==# 'newwin_vert'
-    " Edit the file in a new vertically split window above the previous
+    " Edit the file in a new vertically split window right of the previous
     " window
     wincmd p
     if bufexists(esc_fname)
@@ -1004,6 +1011,19 @@ endfunc
 " Load the MRU list on plugin startup
 call s:MRU_LoadList()
 
+" Set the first entry in the MRU list as the alternate file
+" Credit to Martin Roa Villescas (https://github.com/mroavi) for the patch.
+" bufadd() is available starting from Vim 8.1.1610
+if g:MRU_Set_Alternate_File == 1 && (v:version >= 802 || has('patch-8.1.1610'))
+  if !empty(s:MRU_files)
+    let first_mru_file = s:MRU_files[0]
+    if filereadable(first_mru_file)
+      call bufadd(first_mru_file)
+      let @# = first_mru_file
+    endif
+  endif
+endif
+
 " MRU autocommands {{{1
 " Autocommands to update the most recently used files
 augroup MRUAutoCmds

+ 35 - 1
test/unit_tests.vim

@@ -1715,10 +1715,44 @@ func Test_58()
     return
   endif
   let g:MRU_Use_Current_Window = 0
-  %bw!
+  bw!
   call LogResult(test_name, 'pass')
 endfunc
 
+" ==========================================================================
+" Test59
+" When the MRU_Set_Alternate_File is set to 1, on plugin startup, the
+" alternate file should be set to the first file in the MRU list.
+" ==========================================================================
+func Test_59()
+  if v:version < 802
+    return
+  endif
+  let test_name = 'test59'
+  call writefile([], 'Xfirstfile')
+  edit Xfirstfile
+  call writefile([
+        \ "let MRU_File='vim_mru_file'",
+        \ "let MRU_Set_Alternate_File=1",
+        \ "source ../plugin/mru.vim",
+        \ "call writefile([@#], 'Xoutput')"
+        \ ], 'Xscript')
+  !vim -u NONE --noplugin i NONE -N -S Xscript -c "qa!"
+  if !filereadable('Xoutput')
+    call LogResult(test_name, 'FAIL (1)')
+  else
+    let lines = readfile('Xoutput')
+    if len(lines) == 1 && lines[0] == 'Xfirstfile'
+      call LogResult(test_name, 'pass')
+    else
+      call LogResult(test_name, 'FAIL (2)')
+    endif
+  endif
+  call delete('Xscript')
+  call delete('Xoutput')
+  call delete('Xfirstfile')
+endfunc
+
 " ==========================================================================
 
 " Create the files used by the tests