new function __fish_preview_current_file to open file at the cursor in pager

bound to Alt+O by shared key bindings
created with help from @krobelus
fixes #6838
This commit is contained in:
exploide 2020-04-07 17:42:49 +02:00 committed by Johannes Altmanninger
parent 77d33a8eb9
commit 8025e80fdb
4 changed files with 31 additions and 0 deletions

View file

@ -17,6 +17,7 @@
#### New or improved bindings
- New readline commands `undo` (Ctrl+_) and `redo` (Alt-/) can be used to revert changes to the command line or the pager search field (#6570).
- New function `__fish_preview_current_file` (Alt+O) opens the current file at the cursor in a pager (#6838).
#### Improved prompts
- The default and example prompts print the correct exit status for commands prefixed with `not` (#6566).

View file

@ -1324,6 +1324,8 @@ Some bindings are shared between emacs- and vi-mode because they aren't text edi
- :kbd:`Alt`\ +\ :kbd:`L` lists the contents of the current directory, unless the cursor is over a directory argument, in which case the contents of that directory will be listed.
- :kbd:`Alt`\ +\ :kbd:`O` opens the file at the cursor in a pager.
- :kbd:`Alt`\ +\ :kbd:`P` adds the string '``| less;``' to the end of the job under the cursor. The result is that the output of the command will be paged.
- :kbd:`Alt`\ +\ :kbd:`W` prints a short description of the command under the cursor.

View file

@ -0,0 +1,27 @@
function __fish_preview_current_file --description "Open the file at the cursor in a pager"
set -l pager less --
set -q PAGER && echo $PAGER | read -at pager
# commandline -t will never return an empty list. However, the token
# could comprise multiple lines, so join them into a single string.
set -l file (commandline -t | string collect)
if test -z $file
# $backslash will parsed as regex which may need additional escaping.
set -l backslash '\\\\'
not status test-feature regex-easyesc && set backslash $backslash$backslash
set file (string replace -ra -- '([ ;#^<>&|()"\'])' "$backslash\$1" (commandline -oc)[-1])
end
set -q file[1] || return
# strip -option= from token if present
set file (string replace -r -- '^-[^\s=]*=' '' $file | string collect)
eval set -l files $file || return # Bail if $file does not tokenize.
if set -q files[1] && test -f $files[1]
$pager $files
commandline -f repaint
end
end

View file

@ -84,6 +84,7 @@ function __fish_shared_key_bindings -d "Bindings shared between emacs and vi mod
bind --preset $argv \e. history-token-search-backward
bind --preset $argv \el __fish_list_current_token
bind --preset $argv \eo __fish_preview_current_file
bind --preset $argv \ew __fish_whatis_current_token
# ncurses > 6.0 sends a "delete scrollback" sequence along with clear.
# This string replace removes it.