2020-04-07 23:42:49 +08:00
|
|
|
function __fish_preview_current_file --description "Open the file at the cursor in a pager"
|
2024-01-26 18:06:17 +08:00
|
|
|
if __fish_edit_command_if_at_cursor
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2023-10-28 16:31:24 +08:00
|
|
|
set -l pager (__fish_anypager)
|
|
|
|
or set pager cat
|
2020-04-07 23:42:49 +08:00
|
|
|
|
|
|
|
# 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)
|
2024-01-06 15:45:33 +08:00
|
|
|
set -l prefix eval set
|
2020-04-07 23:42:49 +08:00
|
|
|
|
|
|
|
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
|
2024-01-22 14:42:45 +08:00
|
|
|
set file (string replace -ra -- '([ ;#^<>&|()"\'])' "$backslash\$1" (commandline -xc)[-1])
|
2024-01-06 15:45:33 +08:00
|
|
|
set prefix set
|
2020-04-07 23:42:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
set -q file[1] || return
|
|
|
|
|
|
|
|
# strip -option= from token if present
|
|
|
|
set file (string replace -r -- '^-[^\s=]*=' '' $file | string collect)
|
|
|
|
|
2024-01-06 15:45:33 +08:00
|
|
|
$prefix -l files $file || return # Bail if $file does not tokenize.
|
2020-04-07 23:42:49 +08:00
|
|
|
|
|
|
|
if set -q files[1] && test -f $files[1]
|
|
|
|
$pager $files
|
|
|
|
commandline -f repaint
|
|
|
|
end
|
|
|
|
end
|