kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

man.kak (4985B)


      1 declare-option -docstring "name of the client in which documentation is to be displayed" \
      2     str docsclient
      3 
      4 declare-option -hidden str-list manpage
      5 
      6 hook -group man-highlight global WinSetOption filetype=man %{
      7     add-highlighter window/man-highlight group
      8     # Sections
      9     add-highlighter window/man-highlight/ regex ^\S.*?$ 0:title
     10     # Subsections
     11     add-highlighter window/man-highlight/ regex '^ {3}\S.*?$' 0:default+b
     12     # Command line options
     13     add-highlighter window/man-highlight/ regex '^ {7}-[^\s,]+(,\s+-[^\s,]+)*' 0:list
     14     # References to other manpages
     15     add-highlighter window/man-highlight/ regex [-a-zA-Z0-9_.]+\([a-z0-9]+\) 0:header
     16 
     17     map window normal <ret> :man-jump<ret>
     18 
     19     hook -once -always window WinSetOption filetype=.* %{
     20       remove-highlighter window/man-highlight
     21       unmap window normal <ret>
     22     }
     23 }
     24 
     25 hook global WinSetOption filetype=man %{
     26     hook -group man-hooks window WinResize .* %{ man-impl %opt{manpage} }
     27     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window man-hooks }
     28 }
     29 
     30 define-command -hidden -params ..3 man-impl %{ evaluate-commands %sh{
     31     buffer_name="$1"
     32     if [ -z "${buffer_name}" ]; then
     33         exit
     34     fi
     35     shift
     36     manout=$(mktemp "${TMPDIR:-/tmp}"/kak-man.XXXXXX)
     37     manerr=$(mktemp "${TMPDIR:-/tmp}"/kak-man.XXXXXX)
     38     colout=$(mktemp "${TMPDIR:-/tmp}"/kak-man.XXXXXX)
     39     env MANWIDTH=${kak_window_range##* } man "$@" > "$manout" 2> "$manerr"
     40     retval=$?
     41     if command -v col >/dev/null; then
     42         col -b -x > ${colout} < ${manout}
     43     else
     44         sed 's/.//g' > ${colout} < ${manout}
     45     fi
     46     rm ${manout}
     47 
     48     if [ "${retval}" -eq 0 ]; then
     49         printf %s\\n "
     50                 edit -scratch %{*$buffer_name ${*}*}
     51                 execute-keys '%|cat<space>${colout}<ret>gk'
     52                 nop %sh{ rm ${colout}; rm ${manerr} }
     53                 set-option buffer filetype man
     54                 set-option window manpage $buffer_name $*
     55         "
     56     else
     57         printf '
     58             fail %%{%s}
     59             nop %%sh{ rm "%s"; rm "%s" }
     60         ' "$(cat "$manerr")" "${colout}" "${manerr}"
     61     fi
     62 } }
     63 
     64 define-command -params ..1 \
     65     -shell-script-candidates %{
     66         find /usr/share/man/ $(printf %s "${MANPATH}" |
     67             sed 's/:/ /') -name '*.[1-8]*' |
     68             sed 's,^.*/\(.*\)\.\([1-8][a-zA-Z]*\).*$,\1(\2),'
     69     } \
     70     -docstring %{
     71         man [<page>]: manpage viewer wrapper
     72         If no argument is passed to the command, the selection will be used as page
     73         The page can be a word, or a word directly followed by a section number between parenthesis, e.g. kak(1)
     74     } man %{ evaluate-commands %sh{
     75     subject=${1-$kak_selection}
     76 
     77     ## The completion suggestions display the page number, strip them if present
     78     case "${subject}" in
     79         *\([1-8]*\))
     80             pagenum="${subject##*\(}"
     81             pagenum="${pagenum%\)}"
     82             subject="${subject%%\(*}"
     83             ;;
     84         *)
     85             pagenum=""
     86             ;;
     87     esac
     88 
     89     printf %s\\n "evaluate-commands -try-client %opt{docsclient} man-impl man $pagenum $subject"
     90 } }
     91 
     92 
     93 
     94 # The following section of code enables a user
     95 # to go to next or previous man page links and to follow man page links,
     96 # for example, apropos(1), that would normally appear in SEE ALSO sections.
     97 # The user would position the cursor on any character of the link
     98 # and then press <ret> to change to a buffer showing the man page.
     99 
    100 # Regex pattern defining a man page link.
    101 # Used for determining if a selection, which may just be a link, is a link.
    102 declare-option -hidden regex man_link1 \
    103   [\w_.:-]+\(\d[a-z]*\)
    104 
    105 # Same as above but with lookbehind and lookahead patterns.
    106 # Used for searching for a man page link.
    107 declare-option -hidden regex man_link2 \
    108   "(?:^|(?<=\W))%opt{man_link1}(?=\W)"
    109 
    110 # Define a useful command sequence for searching a given regex
    111 # and a given sequence of search keys.
    112 define-command -hidden man-search -params 2 %{
    113     set-register / %arg[1]
    114     try %{
    115         execute-keys %arg[2]
    116     } catch %{
    117         fail "Could not find man page link"
    118     }
    119 }
    120 
    121 define-command -docstring 'Go to next man page link' \
    122 man-link-next %{ man-search %opt[man_link2] n }
    123 
    124 define-command -docstring 'Go to previous man page link' \
    125 man-link-prev %{ man-search %opt[man_link2] <a-n> }
    126 
    127 define-command -docstring 'Try to jump to a man page' \
    128 man-jump %{
    129   try %{ execute-keys <a-a><a-w> s %opt[man_link1] <ret> } catch %{ fail 'Not a valid man page link' }
    130   try %{ man } catch %{ fail 'No man page link to follow' }
    131 }
    132 
    133 # Suggested keymaps for a user mode
    134 declare-user-mode man
    135 
    136 map global man 'g' -docstring 'Jump to a man page using selected man page link' :man-jump<ret>
    137 map global man 'j' -docstring 'Go to next man page link'                        :man-link-next<ret>
    138 map global man 'k' -docstring 'Go to previous man page link'                    :man-link-prev<ret>
    139 map global man 'm' -docstring 'Look up a man page'                              :man<space>