1# remap_keybindings.bash
2# Author: Noah Friedman <friedman@prep.ai.mit.edu>
3# Created: 1992-01-11
4# Last modified: 1993-02-03
5# Public domain
6
7# Conversion to bash v2 syntax done by Chet Ramey
8
9# Commentary:
10# Code:
11
12#:docstring remap_keybindings:
13# Usage: remap_keybindings old_function new_function
14#
15# Clear all readline keybindings associated with OLD_FUNCTION (a Readline
16# function) rebinding them to NEW_FUNCTION (`self-insert' by default)
17#
18# This requires bash version 1.10 or newer, since previous versions did not
19# implement the `bind' builtin.
20#:end docstring:
21
22###;;;autoload
23function remap_keybindings ()
24{
25    local unbind_function="$1"
26    local bind_function="${2:-'self-insert'}"
27    local bind_output
28    local arg
29
30    # If they're the same thing, the work has already been done.  :-)
31    if [ "${unbind_function}" = "${bind_function}" ]; then
32       return 0
33    fi
34
35    while : ; do
36       bind_output="$(bind -q ${unbind_function} 2> /dev/null)"
37
38       case "${bind_output}" in
39          "${unbind_function} can be invoked via"* ) ;;
40          "" ) return 1 ;;         # probably bad argument to bind
41          *) return 0 ;;           # unbound
42       esac
43
44       # Format of bind_output is like:
45       # 'quoted-insert can be invoked via "\C-q", "\C-v".'
46       # 'self-insert can be invoked via " ", "!", """, "$", "%", ...'
47       set -- ${bind_output}
48       shift 5
49       
50       for arg in "$@" ; do
51          # strip off trailing `.' or `,'
52          arg=${arg%.};
53          arg=${arg%,};
54       
55          case ${arg} in
56             ..)
57                # bind -q didn't provide whole list of key bindings; jump
58                # to top loop to get more
59                continue 2 ; 
60               ;;
61             *)
62                bind "${arg}: ${bind_function}"
63               ;;
64          esac
65       done
66    done
67}
68
69provide remap_keybindings
70
71# remap_keybindings.bash ends here
72