sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

init-cmap.sh (13454B)


      1 #!/bin/bash
      2 
      3 #
      4 # 以下は ble-decode.sh にて既定で定義される特殊キー
      5 #
      6 # 制御文字
      7 #
      8 #   TAB  RET
      9 #
     10 #   NUL  SOH  STX  ETX  EOT  ENQ  ACK  BEL
     11 #   BS   HT   LF   VT   FF   CR   SO   SI
     12 #   DLE  DC1  DC2  DC3  DC4  NAK  SYN  ETB
     13 #   CAN  EM   SUB  ESC  FS   GS   RS   US
     14 #
     15 #   SP   DEL
     16 #
     17 #   PAD  HOP  BPH  NBH  IND  NEL  SSA  ESA
     18 #   HTS  HTJ  VTS  PLD  PLU  RI   SS2  SS3
     19 #   DCS  PU1  PU2  STS  CCH  MW   SPA  EPA
     20 #   SOS  SGCI SCI  CSI  ST   OSC  PM   APC
     21 #
     22 # 特殊文字 (内部使用)
     23 #
     24 #   @ESC @NUL
     25 #
     26 # 特殊キーバインディング
     27 #
     28 #   __batch_char__
     29 #   __defchar__
     30 #   __default__
     31 #   __before_widget__
     32 #   __after_widget__
     33 #   __attach__
     34 #   __detach__
     35 #
     36 # 修飾キー
     37 #
     38 #   shift alter control meta super hyper
     39 #
     40 # 端末の応答を処理した時
     41 #
     42 #   __ignore__
     43 #
     44 # Note: ble-decode.sh における特殊キーの変更に際して、
     45 # この一覧を更新することでキャッシュの更新が起こるようにしている。
     46 #
     47 # 2019-04-15 __error__ を追加したので keycode の再生成が必要。
     48 # 2019-05-04 実験的に mouse, mouse_move を追加した。
     49 # 2019-05-06 ble-update 関連でバグがあったのを潰したので更新。
     50 # 2020-01-31 @ESC, @NUL を追加した。
     51 # 2020-03-12 __line_limit__ を追加
     52 # 2020-04-13 cmap キャッシュ生成バグ修正に伴う更新。
     53 # 2020-04-29 cmap キャッシュ生成バグ修正に伴う更新 (2)
     54 # 2021-07-12 __detach__ 追加
     55 
     56 function ble/init:cmap/bind-single-csi {
     57   ble-bind -k "ESC [ $1" "$2"
     58   ble-bind -k "CSI $1" "$2"
     59 }
     60 function ble/init:cmap/bind-single-ss3 {
     61   ble-bind -k "ESC O $1" "$2"
     62   ble-bind -k "SS3 $1" "$2"
     63 }
     64 function ble/init:cmap/bind-keypad-key {
     65   local Ft=$1 name=$2
     66   (($3&4)) && ble-bind --csi "$Ft" "$name"
     67   (($3&1)) && ble/init:cmap/bind-single-ss3 "$Ft" "$name"
     68   (($3&2)) && ble-bind -k "ESC ? $Ft" "$name"
     69 }
     70 
     71 function ble/init:cmap/initialize {
     72   # Synonyms
     73   #   paste = S-insert [rxvt]
     74   #   scroll_up = S-prior [rxvt]
     75   #   scroll_down = S-next [rxvt]
     76   #   help = f15 [rxvt]
     77   #   menu = f16 [rxvt]
     78   #   print = f16 [xterm]
     79   #   deleteline = A-delete
     80 
     81   ble/edit/info/immediate-show text "ble/lib/init-cmap.sh: updating key sequences..."
     82 
     83   # pc-style keys
     84   # # vt52, xterm, rxvt
     85   # ble-bind --csi '1~' find
     86   # ble-bind --csi '2~' insert
     87   # ble-bind --csi '3~' delete # execute
     88   # ble-bind --csi '4~' select
     89   # ble-bind --csi '5~' prior
     90   # ble-bind --csi '6~' next
     91   # ble-bind --csi '7~' home
     92   # ble-bind --csi '8~' end
     93 
     94   # # cygwin, screen, rosaterm
     95   # ble-bind --csi '1~' home
     96   # ble-bind --csi '2~' insert
     97   # ble-bind --csi '3~' delete
     98   # ble-bind --csi '4~' end
     99   # ble-bind --csi '5~' prior
    100   # ble-bind --csi '6~' next
    101 
    102   # # vt100 (seems minority)
    103   # ble-bind --csi '1~' insert
    104   # ble-bind --csi '2~' home
    105   # ble-bind --csi '3~' prior
    106   # ble-bind --csi '4~' delete
    107   # ble-bind --csi '5~' end
    108   # ble-bind --csi '6~' next
    109 
    110   # 順番を固定
    111   local ret
    112   ble-decode-kbd/generate-keycode insert
    113   ble-decode-kbd/generate-keycode home
    114   ble-decode-kbd/generate-keycode prior
    115   ble-decode-kbd/generate-keycode delete
    116   ble-decode-kbd/generate-keycode end
    117   ble-decode-kbd/generate-keycode next
    118   ble-decode-kbd/generate-keycode find
    119   ble-decode-kbd/generate-keycode select
    120 
    121   local kend; ble/util/assign kend 'tput @7 2>/dev/null || tput kend 2>/dev/null'
    122   if [[ $kend == $'\e[5~' ]]; then
    123     # vt100
    124     ble-bind --csi '1~' insert
    125     ble-bind --csi '2~' home
    126     ble-bind --csi '3~' prior
    127     ble-bind --csi '4~' delete
    128     ble-bind --csi '5~' end
    129     ble-bind --csi '6~' next
    130   else
    131     # Note: openSUSE の /etc/inputrc.keys が home/end と find/select
    132     #   を別のキーと見做して誰も使わない keybinding を設定しているので、
    133     #   home/end が上書きされてしまう。仕方がないので TERM=xterm の時
    134     #   のみ find/select を独立したキーとして取り扱う事にする。これで
    135     #   動かなくなる設定も存在するかもしれないが、取り敢えず openSUSE
    136     #   inputrc を優先させてみる事にする。
    137     #
    138 
    139     # 調べると DEC keyboard では home/end の位置に find/select と印字
    140     # されている。幾つかの端末で 1~/4~ が home/end になっているのはこ
    141     # れが由来だろう。
    142     if [[ $kend == $'\e[F' && ( $TERM == xterm || $TERM == xterm-* || $TERM == kvt ) ]]; then
    143       ble-bind --csi '1~' find
    144       ble-bind --csi '4~' select
    145     else
    146       ble-bind --csi '1~' home
    147       ble-bind --csi '4~' end
    148     fi
    149     ble-bind --csi '2~' insert
    150     ble-bind --csi '3~' delete
    151     ble-bind --csi '5~' prior
    152     ble-bind --csi '6~' next
    153   fi
    154   ble-bind --csi '7~' home
    155   ble-bind --csi '8~' end
    156   local kdch1; ble/util/assign kdch1 'tput kD 2>/dev/null || tput kdch1 2>/dev/null'
    157   [[ $kdch1 == $'\x7F' || $TERM == sun* ]] && ble-bind -k 'DEL' delete
    158 
    159   # vt220, xterm, rxvt
    160   ble-bind --csi '11~' f1
    161   ble-bind --csi '12~' f2
    162   ble-bind --csi '13~' f3
    163   ble-bind --csi '14~' f4
    164   ble-bind --csi '15~' f5
    165   ble-bind --csi '17~' f6
    166   ble-bind --csi '18~' f7
    167   ble-bind --csi '19~' f8
    168   ble-bind --csi '20~' f9
    169   ble-bind --csi '21~' f10
    170   ble-bind --csi '23~' f11
    171   ble-bind --csi '24~' f12
    172   ble-bind --csi '25~' f13
    173   ble-bind --csi '26~' f14
    174   ble-bind --csi '28~' f15
    175   ble-bind --csi '29~' f16
    176   ble-bind --csi '31~' f17
    177   ble-bind --csi '32~' f18
    178   ble-bind --csi '33~' f19
    179   ble-bind --csi '34~' f20
    180 
    181   ble-bind --csi '200~' paste_begin
    182   ble-bind --csi '201~' paste_end
    183 
    184   # keypad
    185   #   vt100, xterm, application mode
    186   #   ESC ? は vt52 由来
    187   #
    188   #   Note: kp~ と通常のキーを区別しても binding が大変なだけで
    189   #   余り利点もないので取り敢えずこの設定では区別しない。
    190   #
    191   # Note: ble/init:cmap/bind-keypad-key 第3引数は
    192   #   1: SS3 X, 2: ESC ? X, 4: CSI X の和。
    193   ble/init:cmap/bind-keypad-key 'SP' SP   3 # kpspace
    194   ble/init:cmap/bind-keypad-key 'A' up    5
    195   ble/init:cmap/bind-keypad-key 'B' down  5
    196   ble/init:cmap/bind-keypad-key 'C' right 5
    197   ble/init:cmap/bind-keypad-key 'D' left  5
    198   ble/init:cmap/bind-keypad-key 'E' begin 5
    199   ble/init:cmap/bind-keypad-key 'F' end   5
    200   ble/init:cmap/bind-keypad-key 'H' home  5
    201   ble/init:cmap/bind-keypad-key 'I' TAB   3 # kptab (Note: CSI I は xterm SM(?1004) focus と重複)
    202   ble/init:cmap/bind-keypad-key 'M' RET   7 # kpent
    203   ble/init:cmap/bind-keypad-key 'P' f1    5 # kpf1 # Note: 普通の f1-f4
    204   ble/init:cmap/bind-keypad-key 'Q' f2    5 # kpf2 #   に対してこれらの
    205   ble/init:cmap/bind-keypad-key 'R' f3    5 # kpf3 #   シーケンスを送る
    206   ble/init:cmap/bind-keypad-key 'S' f4    5 # kpf4 #   端末もある。
    207   ble/init:cmap/bind-keypad-key 'j' '*'   7 # kpmul
    208   ble/init:cmap/bind-keypad-key 'k' '+'   7 # kpadd
    209   ble/init:cmap/bind-keypad-key 'l' ','   7 # kpsep
    210   ble/init:cmap/bind-keypad-key 'm' '-'   7 # kpsub
    211   ble/init:cmap/bind-keypad-key 'n' '.'   7 # kpdec
    212   ble/init:cmap/bind-keypad-key 'o' '/'   7 # kpdiv
    213   ble/init:cmap/bind-keypad-key 'p' '0'   7 # kp0
    214   ble/init:cmap/bind-keypad-key 'q' '1'   7 # kp1
    215   ble/init:cmap/bind-keypad-key 'r' '2'   7 # kp2
    216   ble/init:cmap/bind-keypad-key 's' '3'   7 # kp3
    217   ble/init:cmap/bind-keypad-key 't' '4'   7 # kp4
    218   ble/init:cmap/bind-keypad-key 'u' '5'   7 # kp5
    219   ble/init:cmap/bind-keypad-key 'v' '6'   7 # kp6
    220   ble/init:cmap/bind-keypad-key 'w' '7'   7 # kp7
    221   ble/init:cmap/bind-keypad-key 'x' '8'   7 # kp8
    222   ble/init:cmap/bind-keypad-key 'y' '9'   7 # kp9
    223   ble/init:cmap/bind-keypad-key 'X' '='   7 # kpeq
    224 
    225   # xterm SM(?1004) Focus In/Out の通知
    226   ble/init:cmap/bind-keypad-key 'I' focus 4 # Note: 1 (= SS3) は TAB と重複するので設定しない
    227   ble/init:cmap/bind-keypad-key 'O' blur  5
    228 
    229   # rxvt
    230   #   Note: "CSI code @", "CSI code ^" は本体側で特別に処理している。
    231   ble/init:cmap/bind-single-csi 'Z'     S-TAB
    232   ble/init:cmap/bind-single-ss3 'a'     C-up
    233   ble/init:cmap/bind-single-csi 'a'     S-up
    234   ble/init:cmap/bind-single-ss3 'b'     C-down
    235   ble/init:cmap/bind-single-csi 'b'     S-down
    236   ble/init:cmap/bind-single-ss3 'c'     C-right
    237   ble/init:cmap/bind-single-csi 'c'     S-right
    238   ble/init:cmap/bind-single-ss3 'd'     C-left
    239   ble/init:cmap/bind-single-csi 'd'     S-left
    240   ble/init:cmap/bind-single-csi '2 $'   S-insert # ECMA-48 違反
    241   ble/init:cmap/bind-single-csi '3 $'   S-delete # ECMA-48 違反
    242   ble/init:cmap/bind-single-csi '5 $'   S-prior  # ECMA-48 違反
    243   ble/init:cmap/bind-single-csi '6 $'   S-next   # ECMA-48 違反
    244   ble/init:cmap/bind-single-csi '7 $'   S-home   # ECMA-48 違反
    245   ble/init:cmap/bind-single-csi '8 $'   S-end    # ECMA-48 違反
    246   ble/init:cmap/bind-single-csi '2 3 $' S-f11    # ECMA-48 違反
    247   ble/init:cmap/bind-single-csi '2 4 $' S-f12    # ECMA-48 違反
    248   ble/init:cmap/bind-single-csi '2 5 $' S-f13    # ECMA-48 違反
    249   ble/init:cmap/bind-single-csi '2 6 $' S-f14    # ECMA-48 違反
    250   ble/init:cmap/bind-single-csi '2 8 $' S-f15    # ECMA-48 違反
    251   ble/init:cmap/bind-single-csi '2 9 $' S-f16    # ECMA-48 違反
    252   ble/init:cmap/bind-single-csi '3 1 $' S-f17    # ECMA-48 違反
    253   ble/init:cmap/bind-single-csi '3 2 $' S-f18    # ECMA-48 違反
    254   ble/init:cmap/bind-single-csi '3 3 $' S-f19    # ECMA-48 違反
    255   ble/init:cmap/bind-single-csi '3 4 $' S-f20    # ECMA-48 違反
    256 
    257   # cygwin specific
    258   ble/init:cmap/bind-single-csi '[ A' f1
    259   ble/init:cmap/bind-single-csi '[ B' f2
    260   ble/init:cmap/bind-single-csi '[ C' f3
    261   ble/init:cmap/bind-single-csi '[ D' f4
    262   ble/init:cmap/bind-single-csi '[ E' f5
    263 
    264   # sun specific (Solaris)
    265   ble/init:cmap/bind-single-csi '2 4 7 z' insert
    266   ble/init:cmap/bind-single-csi '2 1 4 z' home
    267   ble/init:cmap/bind-single-csi '2 2 0 z' end
    268   ble/init:cmap/bind-single-csi '2 2 2 z' prior
    269   ble/init:cmap/bind-single-csi '2 1 6 z' next
    270   ble/init:cmap/bind-single-csi '2 2 4 z' f1
    271   ble/init:cmap/bind-single-csi '2 2 5 z' f2
    272   ble/init:cmap/bind-single-csi '2 2 6 z' f3
    273   ble/init:cmap/bind-single-csi '2 2 7 z' f4
    274   ble/init:cmap/bind-single-csi '2 2 8 z' f5
    275   ble/init:cmap/bind-single-csi '2 2 9 z' f6
    276   ble/init:cmap/bind-single-csi '2 3 0 z' f7
    277   ble/init:cmap/bind-single-csi '2 3 1 z' f8
    278   ble/init:cmap/bind-single-csi '2 3 2 z' f9
    279   ble/init:cmap/bind-single-csi '2 3 3 z' f10
    280   ble/init:cmap/bind-single-csi '2 3 4 z' f11
    281   ble/init:cmap/bind-single-csi '2 3 5 z' f12
    282   # ble/init:cmap/bind-single-csi '2 z'     insert # terminfo
    283   # ble/init:cmap/bind-single-csi '3 z'     delete # terminfo
    284   # ble/init:cmap/bind-single-csi '1 9 2 z' f11
    285   # ble/init:cmap/bind-single-csi '1 9 3 z' f12
    286   ble/init:cmap/bind-single-csi '1 z' find   # from xterm ctlseqs
    287   ble/init:cmap/bind-single-csi '4 z' select # from xterm ctlseqs
    288 
    289   # 修飾キー 'CAN @ ?'
    290   #
    291   #   取り敢えず CAN で始まる修飾キーは無効にしておく。何故なら、
    292   #   CAN (C-x) で始まるシーケンスをキーに当てはめてしまうと、
    293   #   C-x で終わるコマンド (exchange-point-and-mark) が曖昧になってしまう。
    294   #   結果として、次に非 @ の文字が来るまで確定しないので実行が遅れる。
    295   #   また C-x C-x の後で @h 等を入力したい場合に別の解釈になってしまう。
    296   #
    297   # ble-bind -k "CAN @ S" shift
    298   # ble-bind -k "CAN @ a" alter
    299   # ble-bind -k "CAN @ c" control
    300   # ble-bind -k "CAN @ h" hyper
    301   # ble-bind -k "CAN @ m" meta
    302   # ble-bind -k "CAN @ s" super
    303 
    304   # st specific
    305   ble/init:cmap/bind-single-csi '2 J' S-home
    306   ble/init:cmap/bind-single-csi 'J' C-end
    307   ble/init:cmap/bind-single-csi 'K' S-end
    308   ble/init:cmap/bind-single-csi '4 l' S-insert
    309   ble/init:cmap/bind-single-csi 'L'   C-insert
    310   ble/init:cmap/bind-single-csi '4 h' insert
    311   # ble/init:cmap/bind-single-csi 'M'   C-delete # conflicts with kpent
    312   ble/init:cmap/bind-single-csi '2 K' S-delete
    313   ble/init:cmap/bind-single-csi 'P'   delete
    314 
    315   # kitty specific "CSI ... u" sequences
    316   _ble_decode_csimap_kitty_u=(
    317     [57358]=capslock [57359]=scrolllock [57360]=numlock [57361]=print [57362]=pause [57363]=menu
    318 
    319     [57376]=f13 [57377]=f14 [57378]=f15 [57379]=f16 [57380]=f17 [57381]=f18 [57382]=f19 [57383]=f20
    320     [57384]=f21 [57385]=f22 [57386]=f23 [57387]=f24 [57388]=f25 [57389]=f26 [57390]=f27 [57391]=f28
    321     [57392]=f29 [57393]=f30 [57394]=f31 [57395]=f32 [57396]=f33 [57397]=f34 [57398]=f35
    322 
    323     [57399]=0 [57400]=1 [57401]=2 [57402]=3 [57403]=4 [57404]=5 [57405]=6 [57406]=7 [57407]=8 [57408]=9
    324     [57409]='.' [57410]='/' [57411]='*' [57412]='-' [57413]='+' [57414]=RET [57415]='=' [57416]=','
    325     [57417]=left [57418]=right [57419]=up [57420]=down
    326     [57421]=prior [57422]=next [57423]=home [57424]=end [57425]=insert [57426]=delete [57427]=begin
    327 
    328     [57428]=media_play [57429]=media_pause [57430]=media_play_pause [57431]=media_reverse
    329     [57432]=media_stop [57433]=media_fast_forward [57434]=media_rewind [57435]=media_track_next
    330     [57436]=media_track_prev [57437]=media_record [57438]=lower_volume [57439]=raise_volume
    331     [57440]=mute_volume
    332 
    333     [57441]=lshift [57442]=lcontrol [57443]=lalter [57444]=lsuper [57445]=lhyper [57446]=lmeta
    334     [57447]=rshift [57448]=rcontrol [57449]=ralter [57450]=rsuper [57451]=rhyper [57452]=rmeta
    335     [57453]=iso_shift3 [57454]=iso_shift5
    336   )
    337   local keyname
    338   for keyname in "${_ble_decode_csimap_kitty_u[@]}"; do
    339     ble-decode-kbd/generate-keycode "$keyname"
    340   done
    341 
    342   ble/edit/info/immediate-show text "ble/lib/init-cmap.sh: updating key sequences... done"
    343 }
    344 
    345 ble/init:cmap/initialize