kakoune

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

menu.kak (2899B)


      1 provide-module menu %§§
      2 
      3 define-command menu -params 1.. -docstring %{
      4     menu [<switches>] <name1> <commands1> <name2> <commands2>...: display a 
      5     menu and execute commands for the selected item
      6 
      7     -auto-single instantly validate if only one item is available
      8     -select-cmds each item specify an additional command to run when selected
      9 } %{
     10     evaluate-commands %sh{
     11         auto_single=false
     12         select_cmds=false
     13         stride=2
     14         on_abort=
     15         while true
     16         do
     17             case "$1" in
     18                 (-auto-single) auto_single=true ;;
     19                 (-select-cmds) select_cmds=true; stride=3 ;;
     20                 (-on-abort) on_abort="$2"; shift ;;
     21                 (-markup) ;; # no longer supported
     22                 (*) break ;;
     23             esac
     24             shift
     25         done
     26         if [ $(( $# % $stride )) -ne 0 ]; then
     27             echo fail "wrong argument count"
     28             exit
     29         fi
     30         if $auto_single && [ $# -eq $stride ]; then
     31             printf %s "$2"
     32             exit
     33         fi
     34         shellquote() {
     35             printf "'%s'" "$(printf %s "$1" | sed "s/'/'\\\\''/g; s/§/§§/g; $2")"
     36         }
     37         cases=
     38         select_cases=
     39         completion=
     40         nl=$(printf '\n.'); nl=${nl%.}
     41         while [ $# -gt 0 ]; do
     42             title=$1
     43             command=$2
     44             completion="${completion}${title}${nl}"
     45             cases="${cases}
     46                 ($(shellquote "$title" s/¶/¶¶/g))
     47                     printf '%s\\n' $(shellquote "$command" s/¶/¶¶/g)
     48                     ;;"
     49             if $select_cmds; then
     50                 select_command=$3
     51                 select_cases="${select_cases}
     52                     ($(shellquote "$title" s/¶/¶¶/g))
     53                         printf '%s\\n' $(shellquote "$select_command" s/¶/¶¶/g)
     54                         ;;"
     55             fi
     56             shift $stride
     57         done
     58         printf "\
     59             prompt '' %%§
     60                     evaluate-commands %%sh¶
     61                         case \"\$kak_text\" in \
     62                         %s
     63                         (*) echo fail -- no such item: \"'\$(printf %%s \"\$kak_text\" | sed \"s/'/''/g\")'\" ;;
     64                         esac
     65     66                 §" "$cases"
     67         if $select_cmds; then
     68             printf " \
     69                     -on-change %%§
     70                         evaluate-commands %%sh¶
     71                             case \"\$kak_text\" in \
     72                             %s
     73                             (*) : ;;
     74                             esac
     75     76                     §" "$select_cases"
     77         fi
     78         if [ -n "$on_abort" ]; then
     79             printf " -on-abort '%s'" "$(printf %s "$on_abort" | sed "s/'/''/g")"
     80         fi
     81         printf ' -menu -shell-script-candidates %%§
     82                     printf %%s %s
     83                 §\n' "$(shellquote "$completion")"
     84     }
     85 }