github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/bundled/narrow.elv.go (about)

     1  package bundled
     2  
     3  const narrowElv = `
     4  # Implementation of location, history and lastcmd mode using the new
     5  # -narrow-read mode. One advantage of this is that it allows the
     6  # execution of arbitrary hooks before or after each mode.
     7  #
     8  # Usage:
     9  #   use narrow
    10  #   narrow:bind-trigger-keys
    11  #
    12  # narrow:bind-trigger-keys binds keys for location, history and lastcmd
    13  # modes. Without options, it uses the default bindings (same as the
    14  # default bindings for edit:location, edit:history and edit:lastcmd),
    15  # but different keys can be specified with the options. To disable a
    16  # binding, specify its key as "".
    17  # Example:
    18  #   narrow:bind-trigger-keys &location=Alt-l &lastcmd=""
    19  
    20  # Hooks
    21  # Each hook variable is an array which must contain lambdas, all of
    22  # which will be executed in sequence before and after the
    23  # corresponding mode.
    24  # Example (list the new directory after switching to it in location mode):
    25  #    narrow:after-location = [ $@narrow:after-location { edit:insert-at-dot "ls"; edit:smart-enter } ]
    26  before-location = []
    27  after-location = []
    28  before-history = []
    29  after-history = []
    30  before-lastcmd = []
    31  after-lastcmd = []
    32  
    33  fn location {
    34    for hook $before-location { $hook }
    35    candidates = [(dir-history | each [arg]{
    36  	      score = (splits . $arg[score] | take 1)
    37          put [
    38            &content=$arg[path]
    39            &display=$score" "(tilde-abbr $arg[path])
    40  	      ]
    41    })]
    42  
    43    edit:-narrow-read {
    44      put $@candidates
    45    } [arg]{
    46      cd $arg[content]
    47      for hook $after-location { $hook }
    48    } &modeline="[narrow] Location " &ignore-case=$true
    49  }
    50  
    51  fn history {
    52    for hook $before-history { $hook }
    53    candidates = [(edit:command-history | each [arg]{
    54          put [
    55  	        &content=$arg[cmd]
    56  	        &display=$arg[id]" "$arg[cmd]
    57          ]
    58    })]
    59  
    60    edit:-narrow-read {
    61      put $@candidates
    62    } [arg]{
    63      edit:replace-input $arg[content]
    64      for hook $after-history { $hook }
    65    } &modeline="[narrow] History " &keep-bottom=$true &ignore-case=$true
    66  }
    67  
    68  fn lastcmd {
    69    for hook $before-lastcmd { $hook }
    70    last = (edit:command-history -1)
    71    cmd = [
    72      &content=$last[cmd]
    73      &display="M-1 "$last[cmd]
    74  	  &filter-text=""
    75    ]
    76    index = 0
    77    candidates = [$cmd ( edit:wordify $last[cmd] | each [arg]{
    78  	      put [
    79            &content=$arg
    80            &display=$index" "$arg
    81            &filter-text=$index
    82  	      ]
    83  	      index = (+ $index 1)
    84    })]
    85    edit:-narrow-read {
    86      put $@candidates
    87    } [arg]{
    88      edit:insert-at-dot $arg[content]
    89      for hook $after-lastcmd { $hook }
    90    } &modeline="[narrow] Lastcmd " &auto-commit=$true &bindings=[&M-1={ edit:narrow:accept-close }] &ignore-case=$true
    91  }
    92  
    93  
    94  fn -bind-insert [k f]{
    95    edit:insert:binding[$k] = $f
    96  }
    97  
    98  fn -bind [k f]{
    99    edit:narrow:binding[$k] = $f
   100  }
   101  
   102  # Bind keys for location, history and lastcmd modes. Without
   103  # options, it uses the default bindings, but different keys
   104  # can be specified with the options. To disable a binding,
   105  # specify its key as "".
   106  # Example:
   107  #   narrow:bind-trigger-keys &location=Alt-l &lastcmd=""
   108  fn bind-trigger-keys [&location=C-l &history=C-r &lastcmd=M-1]{
   109    if (not-eq $location "") { -bind-insert $location $location~ }
   110    if (not-eq $history "")  { -bind-insert $history  $history~ }
   111    if (not-eq $lastcmd "")  { -bind-insert $lastcmd  $lastcmd~ }
   112  }
   113  
   114  # Set up some default useful bindings for narrow mode
   115  -bind Up        $edit:narrow:up~
   116  -bind PageUp    $edit:narrow:page-up~
   117  -bind Down      $edit:narrow:down~
   118  -bind PageDown  $edit:narrow:page-down~
   119  -bind Tab       $edit:narrow:down-cycle~
   120  -bind S-Tab     $edit:narrow:up-cycle~
   121  -bind Backspace $edit:narrow:backspace~
   122  -bind Enter     $edit:narrow:accept-close~
   123  -bind M-Enter   $edit:narrow:accept~
   124  -bind Default   $edit:narrow:default~
   125  -bind "C-["     $edit:insert:start~
   126  -bind C-G       $edit:narrow:toggle-ignore-case~
   127  -bind C-D       $edit:narrow:toggle-ignore-duplication~
   128  `