~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to src/Edit/Interface/edit_keyboard.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************************************
 
3
* MODULE     : edit_keyboard.cpp
 
4
* DESCRIPTION: Keyboard handling
 
5
* COPYRIGHT  : (C) 1999  Joris van der Hoeven
 
6
*******************************************************************************
 
7
* This software falls under the GNU general public license and comes WITHOUT
 
8
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
 
9
* If you don't have this file, write to the Free Software Foundation, Inc.,
 
10
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
11
******************************************************************************/
 
12
 
 
13
#include "edit_interface.hpp"
 
14
#include "analyze.hpp"
 
15
#include "tm_buffer.hpp"
 
16
 
 
17
/******************************************************************************
 
18
* Keyboard
 
19
******************************************************************************/
 
20
 
 
21
int
 
22
edit_interface_rep::get_input_mode () {
 
23
  return input_mode;
 
24
}
 
25
 
 
26
void
 
27
edit_interface_rep::set_input_mode (int mode) {
 
28
  sh_s  = string ("");    // avoids keyboard shorthands when
 
29
  sh_len= 0;              // using the menu between two keystrokes
 
30
 
 
31
  if ((mode == INPUT_NORMAL) && (input_mode != INPUT_NORMAL)) {
 
32
    selection_cancel ();
 
33
    completions= array<string> ();
 
34
  }
 
35
  input_mode= mode;
 
36
}
 
37
 
 
38
void
 
39
edit_interface_rep::set_input_normal () {
 
40
  set_input_mode (INPUT_NORMAL);
 
41
}
 
42
 
 
43
bool
 
44
edit_interface_rep::in_normal_mode () {
 
45
  return input_mode == INPUT_NORMAL;
 
46
}
 
47
 
 
48
bool
 
49
edit_interface_rep::in_search_mode () {
 
50
  return input_mode == INPUT_SEARCH;
 
51
}
 
52
 
 
53
bool
 
54
edit_interface_rep::in_replace_mode () {
 
55
  return input_mode == INPUT_REPLACE;
 
56
}
 
57
 
 
58
bool
 
59
edit_interface_rep::in_spell_mode () {
 
60
  return input_mode == INPUT_SPELL;
 
61
}
 
62
 
 
63
/******************************************************************************
 
64
* Keyboard
 
65
******************************************************************************/
 
66
 
 
67
bool
 
68
edit_interface_rep::try_shortcut (string comb) {
 
69
  int     status;
 
70
  command cmd;
 
71
  string  shorth;
 
72
  string  help;
 
73
 
 
74
  sv->get_keycomb (comb, status, cmd, shorth, help);
 
75
  if ((status & 1) == 1) {
 
76
    if (sh_len>0) {
 
77
      tp= path_add (tp, -sh_len);
 
78
      remove (tp, sh_len);
 
79
    }
 
80
    cmd ();
 
81
    sh_s  = string ("");
 
82
    sh_len= 0;
 
83
    return true;
 
84
  }
 
85
  if ((status & 2) == 2) {
 
86
    sh_s= comb;
 
87
    if (sh_len>0) {
 
88
      tp= path_add (tp, -sh_len);
 
89
      if (sh_len>0) remove (tp, sh_len);
 
90
    }
 
91
    if (N(shorth)>0) insert_tree (shorth);
 
92
    sh_len= N (shorth);
 
93
    string rew= sv->kbd_post_rewrite (sh_s);
 
94
    if (N(help)>0) set_message (help, rew);
 
95
    else set_message ("keyboard shorthand: " * rew, shorth);
 
96
    return true;
 
97
  }
 
98
  return false;
 
99
}
 
100
 
 
101
static string
 
102
simplify_key_press (string key) {
 
103
  while ((N(key) >= 5) && (key(0,3) == "Mod") && (key[4] == '-') &&
 
104
         (key[3] >= '1') && (key[3] <= '5')) key= key (5, N(key));
 
105
  if (key == "space") key= " ";
 
106
  return key;
 
107
}
 
108
 
 
109
void
 
110
edit_interface_rep::key_press (string key) {
 
111
  if (input_mode != INPUT_NORMAL)
 
112
    key= simplify_key_press (key);
 
113
  switch (input_mode) {
 
114
  case INPUT_NORMAL:
 
115
    break;
 
116
  case INPUT_SEARCH:
 
117
    if (search_keypress (key)) return;
 
118
    break;
 
119
  case INPUT_REPLACE:
 
120
    if (replace_keypress (key)) return;
 
121
    break;
 
122
  case INPUT_SPELL:
 
123
    if (spell_keypress (key)) return;
 
124
    break;
 
125
  case INPUT_COMPLETE:
 
126
    if (complete_keypress (key)) return;
 
127
    set_input_normal ();
 
128
    break;
 
129
  }
 
130
 
 
131
  string new_sh= N(sh_s)==0? key: sh_s * " " * key;
 
132
  if (try_shortcut (new_sh)) return;
 
133
  if (new_sh != key) {
 
134
    sh_s  = string ("");
 
135
    sh_len= 0;
 
136
    if (try_shortcut (key)) return;
 
137
  }
 
138
 
 
139
  string rew= sv->kbd_post_rewrite (key);
 
140
  if (N(rew)==1) {
 
141
    int i ((unsigned char) rew[0]);
 
142
    if (((i>=32) && (i<=127)) ||
 
143
        ((i>=128) && (i <=255))) insert_tree (rew);
 
144
    sh_s  = string ("");
 
145
    sh_len= 0;
 
146
  }
 
147
}
 
148
 
 
149
void
 
150
edit_interface_rep::emulate_keyboard (string keys, string action) {
 
151
  string s= keys;
 
152
  while (s != "") {
 
153
    int i;
 
154
    for (i=1; i<N(s); i++)
 
155
      if (s[i]==' ') break;
 
156
    key_press (s (0, i));
 
157
    if (i<N(s)) i++;
 
158
    s= s (i, N(s));
 
159
  }
 
160
  if (N(action) != 0)
 
161
    set_message ("You can also obtain#" * action *
 
162
                 "#by typing#" * keys, action);
 
163
}
 
164
 
 
165
/******************************************************************************
 
166
* Event handlers
 
167
******************************************************************************/
 
168
 
 
169
void
 
170
edit_interface_rep::show_keymaps () {
 
171
  fatal_error ("no longer supported", "edit_interface_rep::show_keymaps");
 
172
}
 
173
 
 
174
void
 
175
edit_interface_rep::handle_keypress (keypress_event ev) {
 
176
  call ("lazy-in-mode-force");
 
177
  buf->mark_undo_block ();
 
178
  key_press (ev->key);
 
179
  notify_change (THE_DECORATIONS);
 
180
}
 
181
 
 
182
void
 
183
edit_interface_rep::handle_keyboard_focus (keyboard_focus_event ev) {
 
184
  got_focus= ev->flag;
 
185
  notify_change (THE_FOCUS);
 
186
  if (got_focus) {
 
187
    focus_on_this_editor ();
 
188
    notify_change (THE_DECORATIONS);
 
189
  }
 
190
}