Do not use backslash-b to select backspaces.
[mp-5.x.git] / mp_keyseq.mpsl
blob24fcfa60651abaca1077545779ce7e2e8a21fb8e
1 /*
3     Minimum Profit 5.x
4     A Programmer's Text Editor
6     Key sequences support (mainly for ANSI strings).
8     Copyright (C) 1991-2007 Angel Ortega <angel@triptico.com>
10     This program is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License
12     as published by the Free Software Foundation; either version 2
13     of the License, or (at your option) any later version.
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24     http://www.triptico.com
28 mp.keyseq = {
29         'store'         => {},
30         'pending'       => [],
31         'tmp'           => [],
32         'ptr'           => NULL
35 sub mp.pending_key() { shift(mp.keyseq.pending); }
37 sub mp.process_keyseq(key)
39         local k;
41         if (mp.keyseq.ptr == NULL)
42                 mp.keyseq.ptr = mp.keyseq.store;
44         /* store the key in the temporal queue */
45         push(mp.keyseq.tmp, key);
47         /* get the key in the current pointer */
48         k = mp.keyseq.ptr[key];
50         if (k == NULL) {
51                 /* no hash for key? string not found;
52                    return back all the stored keys
53                    and restart over */
54                 mp.keyseq.pending = mp.keyseq.tmp;
55                 mp.keyseq.tmp = [];
56                 key = mp.pending_key();
57                 mp.keyseq.ptr = NULL;
58         }
59         else
60         if (is_hash(k)) {
61                 /* is the value itself a hash? keep searching */
62                 mp.keyseq.ptr = k;
63                 key = NULL;
64         }
65         else {
66                 /* value is scalar; it's the keycode, so
67                    return it and restart over */
68                 key = k;
69                 mp.keyseq.pending = [];
70                 mp.keyseq.tmp = [];
71                 mp.keyseq.ptr = NULL;
72         }
74         return key;
78 sub mp.add_keyseq(seq, keycode)
80         local p = mp.keyseq.store;
81         local l = split(seq);
82         local v = pop(l);
84         /* split by character and store as a chain of hashes
85            with the keycode as the final, scalar value */
86         foreach (k, l) {
87                 if (! p[k])
88                         p[k] = {};
89                 p = p[k];
90         }
92         p[v] = keycode;