Skip to content

Commit 35b767a

Browse files
jpseibtchrisbra
authored andcommitted
patch 9.2.0502: runtime(netrw): bookmark handling can be improved
Problem: To goto or delete a bookmark, one needs to prefix a count for the bookmark number (e.g., "2gb" to open bookmark#2). As the bookmark list gets or deletes entries, the numbers keep changing, requiring listing the bookmarks with qb to discover the desired bookmark number. Typing gb or mB without a count targets g:netrw_bookmarklist[-1]. Solution: If no count is given to gb or mB, list all bookmarks and prompt for a number using inputlist(), similar to tag jump with g]. closes: #20211 Signed-off-by: J. Paulo Seibt <jpseibt@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 07dc940 commit 35b767a

5 files changed

Lines changed: 126 additions & 26 deletions

File tree

runtime/doc/pi_netrw.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ QUICK REFERENCE: MAPS *netrw-browse-maps* {{{2
10471047
C Setting the editing window |netrw-C|
10481048
d Make a directory |netrw-d|
10491049
D Attempt to remove the file(s)/directory(ies) |netrw-D|
1050-
gb Go to previous bookmarked directory |netrw-gb|
1050+
gb Go to bookmark |netrw-gb|
10511051
gd Force treatment as directory |netrw-gd|
10521052
gf Force treatment as file |netrw-gf|
10531053
gh Quick hide/unhide of dot-files |netrw-gh|
@@ -1056,11 +1056,12 @@ QUICK REFERENCE: MAPS *netrw-browse-maps* {{{2
10561056
i Cycle between thin, long, wide, and tree listings |netrw-i|
10571057
I Toggle the displaying of the banner |netrw-I|
10581058
mb Bookmark current directory |netrw-mb|
1059+
mB Delete bookmark |netrw-mB|
10591060
mc Copy marked files to marked-file target directory |netrw-mc|
10601061
md Apply diff to marked files (up to 3) |netrw-md|
10611062
me Place marked files on arg list and edit them |netrw-me|
10621063
mf Mark a file |netrw-mf|
1063-
mF Unmark files |netrw-mF|
1064+
mF Unmark buffer-local files |netrw-mF|
10641065
mg Apply vimgrep to marked files |netrw-mg|
10651066
mh Toggle marked file suffices' presence on hiding list |netrw-mh|
10661067
mm Move marked files to marked-file target directory |netrw-mm|
@@ -1365,13 +1366,16 @@ Currently, this only works for local files.
13651366
Associated setting variables: |g:netrw_chgperm|
13661367

13671368

1368-
CHANGING TO A BOOKMARKED DIRECTORY *netrw-gb* {{{2
1369+
CHANGING TO A BOOKMARKED PATH *netrw-gb* {{{2
13691370

1370-
To change directory back to a bookmarked directory, use
1371+
To change to a bookmarked path, use
13711372

1372-
{cnt}gb
1373+
[{cnt}]gb
13731374

13741375
Any count may be used to reference any of the bookmarks.
1376+
If {cnt} is omitted, it shows a list of the current bookmarks
1377+
and prompts for a bookmark number to go to.
1378+
13751379
Note that |netrw-qb| shows both bookmarks and history; to go
13761380
to a location stored in the history see |netrw-u| and |netrw-U|.
13771381

@@ -1434,10 +1438,13 @@ DELETING BOOKMARKS *netrw-mB* {{{2
14341438

14351439
To delete a bookmark, use >
14361440
1437-
{cnt}mB
1441+
[{cnt}]mB
14381442
14391443
If there are marked files, then mB will remove them from the
14401444
bookmark list.
1445+
If no files are marked and {cnt} is omitted, it shows a list
1446+
of the current bookmarks and prompts for a bookmark number
1447+
to delete.
14411448

14421449
Alternatively, one may use :NetrwMB! (see |netrw-:NetrwMB|). >
14431450

runtime/pack/dist/opt/netrw/autoload/netrw.vim

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,7 +2707,7 @@ endfunction
27072707

27082708
" s:NetrwBookHistHandler: {{{2
27092709
" 0: (user: <mb>) bookmark current directory
2710-
" 1: (user: <gb>) change to the bookmarked directory
2710+
" 1: (user: <gb>) change to the bookmarked path
27112711
" 2: (user: <qb>) list bookmarks
27122712
" 3: (browsing) records current directory history
27132713
" 4: (user: <u>) go up (previous) directory, using history
@@ -2737,11 +2737,33 @@ function s:NetrwBookHistHandler(chg,curdir)
27372737
endtry
27382738

27392739
elseif a:chg == 1
2740-
" change to the bookmarked directory
2741-
if exists("g:netrw_bookmarklist[v:count-1]")
2742-
exe "NetrwKeepj e ".fnameescape(g:netrw_bookmarklist[v:count-1])
2740+
" change to bookmarked path
2741+
if exists("g:netrw_bookmarklist") && !empty(g:netrw_bookmarklist)
2742+
let len_bookmarklist = len(g:netrw_bookmarklist)
2743+
let bookmark_num = v:count
2744+
2745+
" v:count value is set to zero if no count (prefix) is given to the `gb` map
2746+
if bookmark_num == 0
2747+
" list bookmarks and prompt for a bookmark number
2748+
let goto_list = [" # | Goto Bookmark:"]
2749+
let i = 0
2750+
while i < len_bookmarklist
2751+
call add(goto_list, printf("%3d| %s", i + 1, g:netrw_bookmarklist[i]))
2752+
let i += 1
2753+
endwhile
2754+
let bookmark_num = inputlist(goto_list)
2755+
endif
2756+
2757+
if bookmark_num > 0
2758+
if bookmark_num <= len_bookmarklist
2759+
exe "NetrwKeepj e " . fnameescape(g:netrw_bookmarklist[bookmark_num - 1])
2760+
else
2761+
echomsg "Sorry, bookmark#" . bookmark_num . " doesn't exist!"
2762+
endif
2763+
endif
2764+
" Exit silently if user cancels with `q` or empty after inputlist()
27432765
else
2744-
echomsg "Sorry, bookmark#".v:count." doesn't exist!"
2766+
echo "Bookmark list is empty."
27452767
endif
27462768

27472769
elseif a:chg == 2
@@ -2842,16 +2864,38 @@ function s:NetrwBookHistHandler(chg,curdir)
28422864
endif
28432865

28442866
elseif a:chg == 6
2845-
if exists("s:netrwmarkfilelist_{curbufnr}")
2867+
if exists("s:netrwmarkfilelist_{curbufnr}") && !empty(s:netrwmarkfilelist_{curbufnr})
28462868
call s:NetrwBookmark(1)
28472869
echo "removed marked files from bookmarks"
2870+
elseif exists("g:netrw_bookmarklist") && !empty(g:netrw_bookmarklist)
2871+
let len_bookmarklist = len(g:netrw_bookmarklist)
2872+
let bookmark_num = v:count
2873+
2874+
" v:count value is set to zero if no count (prefix) is given to the `gb` map
2875+
if bookmark_num == 0
2876+
" list bookmarks and prompt for a bookmark number
2877+
let goto_list = [" # | Delete Bookmark:"]
2878+
let i = 0
2879+
while i < len_bookmarklist
2880+
call add(goto_list, printf("%3d| %s", i + 1, g:netrw_bookmarklist[i]))
2881+
let i += 1
2882+
endwhile
2883+
let bookmark_num = inputlist(goto_list)
2884+
endif
2885+
2886+
if bookmark_num > 0
2887+
if bookmark_num <= len_bookmarklist
2888+
let bookmark_path = g:netrw_bookmarklist[bookmark_num - 1]
2889+
call s:MergeBookmarks()
2890+
NetrwKeepj call remove(g:netrw_bookmarklist, bookmark_num - 1)
2891+
echo "removed " . bookmark_path . " from g:netrw_bookmarklist."
2892+
else
2893+
echomsg "Sorry, bookmark#" . bookmark_num . " doesn't exist!"
2894+
endif
2895+
endif
2896+
" Exit silently if user cancels with `q` or empty after inputlist()
28482897
else
2849-
" delete the v:count'th bookmark
2850-
let iremove = v:count
2851-
let dremove = g:netrw_bookmarklist[iremove - 1]
2852-
call s:MergeBookmarks()
2853-
NetrwKeepj call remove(g:netrw_bookmarklist,iremove-1)
2854-
echo "removed ".dremove." from g:netrw_bookmarklist"
2898+
echo "Bookmark list is empty."
28552899
endif
28562900

28572901
try

runtime/pack/dist/opt/netrw/doc/netrw.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ QUICK REFERENCE: MAPS *netrw-browse-maps* {{{2
10471047
C Setting the editing window |netrw-C|
10481048
d Make a directory |netrw-d|
10491049
D Attempt to remove the file(s)/directory(ies) |netrw-D|
1050-
gb Go to previous bookmarked directory |netrw-gb|
1050+
gb Go to bookmark |netrw-gb|
10511051
gd Force treatment as directory |netrw-gd|
10521052
gf Force treatment as file |netrw-gf|
10531053
gh Quick hide/unhide of dot-files |netrw-gh|
@@ -1056,11 +1056,12 @@ QUICK REFERENCE: MAPS *netrw-browse-maps* {{{2
10561056
i Cycle between thin, long, wide, and tree listings |netrw-i|
10571057
I Toggle the displaying of the banner |netrw-I|
10581058
mb Bookmark current directory |netrw-mb|
1059+
mB Delete bookmark |netrw-mB|
10591060
mc Copy marked files to marked-file target directory |netrw-mc|
10601061
md Apply diff to marked files (up to 3) |netrw-md|
10611062
me Place marked files on arg list and edit them |netrw-me|
10621063
mf Mark a file |netrw-mf|
1063-
mF Unmark files |netrw-mF|
1064+
mF Unmark buffer-local files |netrw-mF|
10641065
mg Apply vimgrep to marked files |netrw-mg|
10651066
mh Toggle marked file suffices' presence on hiding list |netrw-mh|
10661067
mm Move marked files to marked-file target directory |netrw-mm|
@@ -1365,13 +1366,16 @@ Currently, this only works for local files.
13651366
Associated setting variables: |g:netrw_chgperm|
13661367

13671368

1368-
CHANGING TO A BOOKMARKED DIRECTORY *netrw-gb* {{{2
1369+
CHANGING TO A BOOKMARKED PATH *netrw-gb* {{{2
13691370

1370-
To change directory back to a bookmarked directory, use
1371+
To change to a bookmarked path, use
13711372

1372-
{cnt}gb
1373+
[{cnt}]gb
13731374

13741375
Any count may be used to reference any of the bookmarks.
1376+
If {cnt} is omitted, it shows a list of the current bookmarks
1377+
and prompts for a bookmark number to go to.
1378+
13751379
Note that |netrw-qb| shows both bookmarks and history; to go
13761380
to a location stored in the history see |netrw-u| and |netrw-U|.
13771381

@@ -1434,10 +1438,13 @@ DELETING BOOKMARKS *netrw-mB* {{{2
14341438

14351439
To delete a bookmark, use >
14361440
1437-
{cnt}mB
1441+
[{cnt}]mB
14381442
14391443
If there are marked files, then mB will remove them from the
14401444
bookmark list.
1445+
If no files are marked and {cnt} is omitted, it shows a list
1446+
of the current bookmarks and prompts for a bookmark number
1447+
to delete.
14411448

14421449
Alternatively, one may use :NetrwMB! (see |netrw-:NetrwMB|). >
14431450

src/testdir/test_plugin_netrw.vim

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ func Test_netrw_parse_special_char_user()
330330
call assert_equal(result.path, 'test.txt')
331331
endfunction
332332

333-
func Test_netrw_empty_buffer_fastpath_wipe()
333+
" Note: Test_netrw_a_empty_buffer_fastpath_wipe() should run before
334+
" any other tests that open a netrw buffer (e.g, :Explore).
335+
func Test_netrw_a_empty_buffer_fastpath_wipe()
334336
" SetUp() may have opened some buffers
335337
let previous = bufnr('$')
336338
let g:netrw_fastbrowse=0
@@ -717,7 +719,44 @@ func Test_netrw_bookmark_marked_file()
717719

718720
let g:netrw_keepdir = save_keepdir
719721
if save_bookmarklist is v:null
720-
unlet g:netrw_bookmarklist
722+
unlet! g:netrw_bookmarklist
723+
else
724+
let g:netrw_bookmarklist = save_bookmarklist
725+
endif
726+
727+
bw!
728+
endfunc
729+
730+
" Typing gb or mB without a count should prompt
731+
" for a bookmark number through an inputlist().
732+
" Expected dialog output (gb): # | Goto Bookmark:
733+
" 1| /foo/bar/baz
734+
"
735+
" Expected dialog output (mB): # | Delete Bookmark:
736+
" 1| /foo/bar/baz
737+
func Test_netrw_bookmark_goto_delete_prompt()
738+
let save_home = g:netrw_home
739+
let save_bookmarklist = exists('g:netrw_bookmarklist') ? g:netrw_bookmarklist : v:null
740+
741+
let g:netrw_home = getcwd()
742+
let g:netrw_bookmarklist = ['/foo/bar/baz']
743+
Explore .
744+
745+
" Inject 'q' to cancel the inputlist() prompt
746+
call feedkeys('q', 't')
747+
let dialog_out = execute('normal gb')
748+
call assert_match('Goto Bookmark:', dialog_out)
749+
750+
call feedkeys('q', 't')
751+
let dialog_out = execute('normal mB')
752+
call assert_match('Delete Bookmark:', dialog_out)
753+
754+
" Tear down
755+
call delete(g:netrw_home . '/.netrwbook')
756+
call delete(g:netrw_home . '/.netrwhist')
757+
let g:netrw_home = save_home
758+
if save_bookmarklist is v:null
759+
unlet! g:netrw_bookmarklist
721760
else
722761
let g:netrw_bookmarklist = save_bookmarklist
723762
endif
@@ -780,4 +819,5 @@ func Test_netrw_injection()
780819
unlet! g:netrw_home g:netrw_dirhistmax g:netrw_dirhistcnt g:netrw_dirhist_1 g:injected
781820
endtry
782821
endfunc
822+
783823
" vim:ts=8 sts=2 sw=2 et

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ static char *(features[]) =
729729

730730
static int included_patches[] =
731731
{ /* Add new patch number below this line */
732+
/**/
733+
502,
732734
/**/
733735
501,
734736
/**/

0 commit comments

Comments
 (0)