summaryrefslogtreecommitdiff
path: root/examples/scripts.noah/remap_keys.bash
blob: aa7c463d158ab1e08c131721817ea6352f3544b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# remap_keybindings.bash
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1992-01-11
# Last modified: 1993-02-03
# Public domain

# Conversion to bash v2 syntax done by Chet Ramey

# Commentary:
# Code:

#:docstring remap_keybindings:
# Usage: remap_keybindings old_function new_function
#
# Clear all readline keybindings associated with OLD_FUNCTION (a Readline
# function) rebinding them to NEW_FUNCTION (`self-insert' by default)
#
# This requires bash version 1.10 or newer, since previous versions did not
# implement the `bind' builtin.
#:end docstring:

###;;;autoload
function remap_keybindings ()
{
    local unbind_function="$1"
    local bind_function="${2:-'self-insert'}"
    local bind_output
    local arg

    # If they're the same thing, the work has already been done.  :-)
    if [ "${unbind_function}" = "${bind_function}" ]; then
       return 0
    fi

    while : ; do
       bind_output="$(bind -q ${unbind_function} 2> /dev/null)"

       case "${bind_output}" in
          "${unbind_function} can be invoked via"* ) ;;
          "" ) return 1 ;;         # probably bad argument to bind
          *) return 0 ;;           # unbound
       esac

       # Format of bind_output is like:
       # 'quoted-insert can be invoked via "\C-q", "\C-v".'
       # 'self-insert can be invoked via " ", "!", """, "$", "%", ...'
       set -- ${bind_output}
       shift 5
       
       for arg in "$@" ; do
          # strip off trailing `.' or `,'
          arg=${arg%.};
          arg=${arg%,};
       
          case ${arg} in
             ..)
                # bind -q didn't provide whole list of key bindings; jump
                # to top loop to get more
                continue 2 ; 
               ;;
             *)
                bind "${arg}: ${bind_function}"
               ;;
          esac
       done
    done
}

provide remap_keybindings

# remap_keybindings.bash ends here