sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/

lib/sup/keymap.rb (3513B) - raw

      1 require 'sup/util/ncurses'
      2 
      3 module Redwood
      4 
      5 class Keymap
      6 
      7   HookManager.register "keybindings", <<EOS
      8 Add custom keybindings.
      9 Methods:
     10   modes: Hash from mode names to mode classes.
     11   global_keymap: The top-level keymap.
     12 EOS
     13 
     14   def initialize
     15     @map = {}
     16     @order = []
     17     yield self if block_given?
     18   end
     19 
     20   def self.keysym_to_keycode k
     21     case k
     22     when :down then Ncurses::KEY_DOWN
     23     when :up then Ncurses::KEY_UP
     24     when :left then Ncurses::KEY_LEFT
     25     when :right then Ncurses::KEY_RIGHT
     26     when :page_down then Ncurses::KEY_NPAGE
     27     when :page_up then Ncurses::KEY_PPAGE
     28     when :backspace then Ncurses::KEY_BACKSPACE
     29     when :home then Ncurses::KEY_HOME
     30     when :end then Ncurses::KEY_END
     31     when :ctrl_l then "\f".ord
     32     when :ctrl_g then "\a".ord
     33     when :tab then "\t".ord
     34     when :enter, :return then 10 #Ncurses::KEY_ENTER
     35     else
     36       if k.is_a?(String) && k.length == 1
     37         k.ord
     38       else
     39         raise ArgumentError, "unknown key name '#{k}'"
     40       end
     41     end
     42   end
     43 
     44   def self.keysym_to_string k
     45     case k
     46     when :down then "<down arrow>"
     47     when :up then "<up arrow>"
     48     when :left then "<left arrow>"
     49     when :right then "<right arrow>"
     50     when :page_down then "<page down>"
     51     when :page_up then "<page up>"
     52     when :backspace then "<backspace>"
     53     when :home then "<home>"
     54     when :end then "<end>"
     55     when :enter, :return then "<enter>"
     56     when :tab then "tab"
     57     when " " then "<space>"
     58     else
     59       Ncurses::keyname(keysym_to_keycode(k))
     60     end
     61   end
     62 
     63   def add action, help, *keys
     64     entry = [action, help, keys]
     65     @order << entry
     66     keys.each do |k|
     67       kc = Keymap.keysym_to_keycode k
     68       raise ArgumentError, "key '#{k}' already defined (as #{@map[kc].first})" if @map.include? kc
     69       @map[kc] = entry
     70     end
     71   end
     72 
     73   def delete k
     74     kc = Keymap.keysym_to_keycode(k)
     75     return unless @map.member? kc
     76     entry = @map.delete kc
     77     keys = entry[2]
     78     keys.delete k
     79     @order.delete entry if keys.empty?
     80   end
     81 
     82   def add! action, help, *keys
     83     keys.each { |k| delete k }
     84     add action, help, *keys
     85   end
     86 
     87   def add_multi prompt, key
     88     kc = Keymap.keysym_to_keycode(key)
     89     if @map.member? kc
     90       action = @map[kc].first
     91       raise "existing action is not a keymap" unless action.is_a?(Keymap)
     92       yield action
     93     else
     94       submap = Keymap.new
     95       add submap, prompt, key
     96       yield submap
     97     end
     98   end
     99 
    100   def action_for kc
    101     action, help, _keys = @map[kc.code]
    102     [action, help]
    103   end
    104 
    105   def has_key? k; @map[k.code] end
    106 
    107   def keysyms; @map.values.map { |action, help, keys| keys }.flatten; end
    108 
    109   def help_lines except_for={}, prefix=""
    110     lines = [] # :(
    111     @order.each do |action, help, keys|
    112       valid_keys = keys.select { |k| !except_for[k] }
    113       next if valid_keys.empty?
    114       case action
    115       when Symbol
    116         lines << [valid_keys.map { |k| prefix + Keymap.keysym_to_string(k) }.join(", "), help]
    117       when Keymap
    118         lines += action.help_lines({}, prefix + Keymap.keysym_to_string(keys.first))
    119       end
    120     end.compact
    121     lines
    122   end
    123 
    124   def help_text except_for={}
    125     lines = help_lines except_for
    126     llen = lines.max_of { |a, b| a.length }
    127     lines.map { |a, b| sprintf " %#{llen}s : %s", a, b }.join("\n")
    128   end
    129 
    130   def self.run_hook global_keymap
    131     modes = Hash[Mode.keymaps.map { |klass,keymap| [Mode.make_name(klass.name),klass] }]
    132     locals = {
    133       :modes => modes,
    134       :global_keymap => global_keymap,
    135     }
    136     HookManager.run 'keybindings', locals
    137   end
    138 end
    139 
    140 end