0
|
1 /* Simple built-in editing commands.
|
|
2 Copyright (C) 1985, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Mule 2.0, FSF 19.30. */
|
|
22
|
|
23 #include <config.h>
|
|
24 #include "lisp.h"
|
|
25 #include "commands.h"
|
|
26 #include "buffer.h"
|
|
27 #include "syntax.h"
|
|
28 #include "insdel.h"
|
|
29
|
|
30 Lisp_Object Qkill_forward_chars;
|
|
31 Lisp_Object Qself_insert_command;
|
|
32
|
|
33 Lisp_Object Vblink_paren_function;
|
|
34
|
|
35 /* A possible value for a buffer's overwrite-mode variable. */
|
|
36 Lisp_Object Qoverwrite_mode_binary;
|
|
37
|
|
38 /* Non-nil means put this face on the next self-inserting character. */
|
|
39 Lisp_Object Vself_insert_face;
|
|
40
|
|
41 /* This is the command that set up Vself_insert_face. */
|
|
42 Lisp_Object Vself_insert_face_command;
|
|
43
|
|
44
|
20
|
45 DEFUN ("forward-char", Fforward_char, 0, 2, "_p", /*
|
0
|
46 Move point right ARG characters (left if ARG negative).
|
|
47 On reaching end of buffer, stop and signal error.
|
70
|
48 If BUFFER is nil, the current buffer is assumed.
|
20
|
49 */
|
|
50 (arg, buffer))
|
0
|
51 {
|
|
52 struct buffer *buf = decode_buffer (buffer, 1);
|
|
53
|
|
54 if (NILP (arg))
|
|
55 arg = make_int (1);
|
|
56 else
|
|
57 CHECK_INT (arg);
|
|
58
|
|
59 /* This used to just set point to point + XINT (arg), and then check
|
|
60 to see if it was within boundaries. But now that SET_PT can
|
|
61 potentially do a lot of stuff (calling entering and exiting
|
|
62 hooks, etcetera), that's not a good approach. So we validate the
|
|
63 proposed position, then set point. */
|
|
64 {
|
|
65 Bufpos new_point = BUF_PT (buf) + XINT (arg);
|
|
66
|
|
67 if (new_point < BUF_BEGV (buf))
|
|
68 {
|
|
69 BUF_SET_PT (buf, BUF_BEGV (buf));
|
70
|
70 Fsignal (Qbeginning_of_buffer, Qnil);
|
0
|
71 }
|
|
72 if (new_point > BUF_ZV (buf))
|
|
73 {
|
|
74 BUF_SET_PT (buf, BUF_ZV (buf));
|
70
|
75 Fsignal (Qend_of_buffer, Qnil);
|
0
|
76 }
|
|
77
|
|
78 BUF_SET_PT (buf, new_point);
|
|
79 }
|
|
80
|
|
81 return Qnil;
|
|
82 }
|
|
83
|
20
|
84 DEFUN ("backward-char", Fbackward_char, 0, 2, "_p", /*
|
0
|
85 Move point left ARG characters (right if ARG negative).
|
|
86 On attempt to pass beginning or end of buffer, stop and signal error.
|
70
|
87 If BUFFER is nil, the current buffer is assumed.
|
20
|
88 */
|
|
89 (arg, buffer))
|
0
|
90 {
|
|
91 if (NILP (arg))
|
|
92 arg = make_int (1);
|
|
93 else
|
|
94 CHECK_INT (arg);
|
|
95
|
|
96 XSETINT (arg, - XINT (arg));
|
|
97 return Fforward_char (arg, buffer);
|
|
98 }
|
|
99
|
20
|
100 DEFUN ("forward-line", Fforward_line, 0, 2, "_p", /*
|
0
|
101 Move ARG lines forward (backward if ARG is negative).
|
|
102 Precisely, if point is on line I, move to the start of line I + ARG.
|
|
103 If there isn't room, go as far as possible (no error).
|
|
104 Returns the count of lines left to move. If moving forward,
|
|
105 that is ARG - number of lines moved; if backward, ARG + number moved.
|
|
106 With positive ARG, a non-empty line at the end counts as one line
|
|
107 successfully moved (for the return value).
|
|
108 If BUFFER is nil, the current buffer is assumed.
|
20
|
109 */
|
|
110 (arg, buffer))
|
0
|
111 {
|
|
112 struct buffer *buf = decode_buffer (buffer, 1);
|
|
113 Bufpos pos2 = BUF_PT (buf);
|
|
114 Bufpos pos;
|
|
115 int count, shortage, negp;
|
|
116
|
|
117 if (NILP (arg))
|
|
118 count = 1;
|
|
119 else
|
|
120 {
|
|
121 CHECK_INT (arg);
|
|
122 count = XINT (arg);
|
|
123 }
|
|
124
|
|
125 negp = count <= 0;
|
|
126 pos = scan_buffer (buf, '\n', pos2, 0, count - negp, &shortage, 1);
|
|
127 if (shortage > 0
|
|
128 && (negp
|
|
129 || (BUF_ZV (buf) > BUF_BEGV (buf)
|
|
130 && pos != pos2
|
|
131 && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
|
|
132 shortage--;
|
|
133 BUF_SET_PT (buf, pos);
|
|
134 return make_int (negp ? - shortage : shortage);
|
|
135 }
|
|
136
|
20
|
137 DEFUN ("beginning-of-line", Fbeginning_of_line, 0, 2, "_p", /*
|
8
|
138 Move point to beginning of current line.
|
|
139 With argument ARG not nil or 1, move forward ARG - 1 lines first.
|
|
140 If scan reaches end of buffer, stop there without error.
|
|
141 If BUFFER is nil, the current buffer is assumed.
|
20
|
142 */
|
|
143 (arg, buffer))
|
8
|
144 {
|
|
145 struct buffer *b = decode_buffer (buffer, 1);
|
|
146
|
70
|
147 XSETBUFFER (buffer, b);
|
|
148 if (NILP (arg))
|
|
149 arg = make_int (1);
|
|
150 else
|
|
151 CHECK_INT (arg);
|
|
152
|
|
153 Fforward_line (make_int (XINT (arg) - 1), buffer);
|
8
|
154 return Qnil;
|
|
155 }
|
|
156
|
70
|
157 DEFUN ("end-of-line", Fend_of_line, 0, 2, "_p", /*
|
|
158 Move point to end of current line.
|
|
159 With argument ARG not nil or 1, move forward ARG - 1 lines first.
|
|
160 If scan reaches end of buffer, stop there without error.
|
|
161 If BUFFER is nil, the current buffer is assumed.
|
20
|
162 */
|
|
163 (arg, buffer))
|
8
|
164 {
|
|
165 struct buffer *buf = decode_buffer (buffer, 1);
|
|
166
|
|
167 XSETBUFFER (buffer, buf);
|
|
168
|
|
169 if (NILP (arg))
|
|
170 arg = make_int (1);
|
|
171 else
|
|
172 CHECK_INT (arg);
|
|
173
|
70
|
174 BUF_SET_PT (buf, find_before_next_newline (buf, BUF_PT (buf), 0,
|
8
|
175 XINT (arg) - (XINT (arg) <= 0)));
|
6
|
176 return Qnil;
|
|
177 }
|
|
178
|
20
|
179 DEFUN ("delete-char", Fdelete_char, 1, 2, "*p\nP", /*
|
0
|
180 Delete the following ARG characters (previous, with negative arg).
|
|
181 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).
|
|
182 Interactively, ARG is the prefix arg, and KILLFLAG is set if
|
|
183 ARG was explicitly specified.
|
20
|
184 */
|
|
185 (arg, killflag))
|
0
|
186 {
|
|
187 /* This function can GC */
|
|
188 Bufpos pos;
|
|
189 struct buffer *buf = current_buffer;
|
|
190
|
|
191 CHECK_INT (arg);
|
|
192
|
|
193 pos = BUF_PT (buf) + XINT (arg);
|
|
194 if (NILP (killflag))
|
|
195 {
|
|
196 if (XINT (arg) < 0)
|
|
197 {
|
|
198 if (pos < BUF_BEGV (buf))
|
|
199 signal_error (Qbeginning_of_buffer, Qnil);
|
|
200 else
|
|
201 buffer_delete_range (buf, pos, BUF_PT (buf), 0);
|
|
202 }
|
|
203 else
|
|
204 {
|
|
205 if (pos > BUF_ZV (buf))
|
|
206 signal_error (Qend_of_buffer, Qnil);
|
|
207 else
|
|
208 buffer_delete_range (buf, BUF_PT (buf), pos, 0);
|
|
209 }
|
|
210 }
|
|
211 else
|
|
212 {
|
|
213 call1 (Qkill_forward_chars, arg);
|
|
214 }
|
|
215 return Qnil;
|
|
216 }
|
|
217
|
20
|
218 DEFUN ("delete-backward-char", Fdelete_backward_char, 1, 2, "*p\nP", /*
|
0
|
219 Delete the previous ARG characters (following, with negative ARG).
|
|
220 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).
|
|
221 Interactively, ARG is the prefix arg, and KILLFLAG is set if
|
|
222 ARG was explicitly specified.
|
20
|
223 */
|
|
224 (arg, killflag))
|
0
|
225 {
|
|
226 /* This function can GC */
|
|
227 CHECK_INT (arg);
|
|
228 return Fdelete_char (make_int (-XINT (arg)), killflag);
|
|
229 }
|
|
230
|
|
231 static void internal_self_insert (Emchar ch, int noautofill);
|
|
232
|
20
|
233 DEFUN ("self-insert-command", Fself_insert_command, 1, 1, "*p", /*
|
0
|
234 Insert the character you type.
|
|
235 Whichever character you type to run this command is inserted.
|
20
|
236 */
|
|
237 (arg))
|
0
|
238 {
|
|
239 /* This function can GC */
|
|
240 int n;
|
|
241 Emchar ch;
|
|
242 Lisp_Object c;
|
|
243 CHECK_INT (arg);
|
|
244
|
|
245 if (CHAR_OR_CHAR_INTP (Vlast_command_char))
|
|
246 c = Vlast_command_char;
|
|
247 else
|
|
248 c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
|
|
249
|
|
250 if (NILP (c))
|
|
251 signal_simple_error ("last typed character has no ASCII equivalent",
|
|
252 Fcopy_event (Vlast_command_event, Qnil));
|
|
253
|
|
254 CHECK_CHAR_COERCE_INT (c);
|
|
255
|
|
256 n = XINT (arg);
|
|
257 ch = XCHAR (c);
|
|
258 #if 0 /* FSFmacs */
|
|
259 /* #### This optimization won't work because of differences in
|
|
260 how the start-open and end-open properties default for text
|
|
261 properties. See internal_self_insert(). */
|
|
262 if (n >= 2 && NILP (current_buffer->overwrite_mode))
|
|
263 {
|
|
264 n -= 2;
|
|
265 /* The first one might want to expand an abbrev. */
|
|
266 internal_self_insert (c, 1);
|
|
267 /* The bulk of the copies of this char can be inserted simply.
|
|
268 We don't have to handle a user-specified face specially
|
|
269 because it will get inherited from the first char inserted. */
|
|
270 Finsert_char (make_char (c), make_int (n), Qt, Qnil);
|
|
271 /* The last one might want to auto-fill. */
|
|
272 internal_self_insert (c, 0);
|
|
273 }
|
|
274 else
|
|
275 #endif /* 0 */
|
|
276 while (n > 0)
|
|
277 {
|
|
278 n--;
|
|
279 internal_self_insert (ch, (n != 0));
|
|
280 }
|
|
281 return Qnil;
|
|
282 }
|
|
283
|
|
284 /* Insert character C1. If NOAUTOFILL is nonzero, don't do autofill
|
|
285 even if it is enabled.
|
|
286
|
|
287 FSF:
|
|
288
|
|
289 If this insertion is suitable for direct output (completely simple),
|
|
290 return 0. A value of 1 indicates this *might* not have been simple.
|
|
291 A value of 2 means this did things that call for an undo boundary. */
|
|
292
|
|
293 static void
|
|
294 internal_self_insert (Emchar c1, int noautofill)
|
|
295 {
|
|
296 /* This function can GC */
|
|
297 /* int hairy = 0; -- unused */
|
|
298 REGISTER enum syntaxcode synt;
|
|
299 REGISTER Emchar c2;
|
|
300 Lisp_Object overwrite;
|
70
|
301 struct Lisp_Char_Table *syntax_table;
|
0
|
302 struct buffer *buf = current_buffer;
|
|
303
|
|
304 overwrite = buf->overwrite_mode;
|
70
|
305 syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
|
0
|
306
|
|
307 #if 0
|
|
308 /* No, this is very bad, it makes undo *always* undo a character at a time
|
|
309 instead of grouping consecutive self-inserts together. Nasty nasty.
|
|
310 */
|
|
311 if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
|
|
312 || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
|
|
313 hairy = 1;
|
|
314 #endif
|
|
315
|
|
316 if (!NILP (overwrite)
|
|
317 && BUF_PT (buf) < BUF_ZV (buf)
|
|
318 && (EQ (overwrite, Qoverwrite_mode_binary)
|
|
319 || (c1 != '\n' && BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\n'))
|
|
320 && (EQ (overwrite, Qoverwrite_mode_binary)
|
|
321 || BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\t'
|
|
322 || XINT (buf->tab_width) <= 0
|
|
323 || XINT (buf->tab_width) > 20
|
|
324 || !((current_column (buf) + 1) % XINT (buf->tab_width))))
|
|
325 {
|
|
326 buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
|
|
327 /* hairy = 2; */
|
|
328 }
|
|
329
|
|
330 if (!NILP (buf->abbrev_mode)
|
|
331 && !WORD_SYNTAX_P (syntax_table, c1)
|
|
332 && NILP (buf->read_only)
|
|
333 && BUF_PT (buf) > BUF_BEGV (buf))
|
|
334 {
|
|
335 c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
|
|
336
|
|
337 if (WORD_SYNTAX_P (syntax_table, c2))
|
|
338 {
|
|
339 /* int modiff = BUF_MODIFF (current_buffer); */
|
|
340 Fexpand_abbrev ();
|
|
341 /* We can't trust the value of Fexpand_abbrev,
|
|
342 but if Fexpand_abbrev changed the buffer,
|
|
343 assume it expanded something. */
|
|
344 /* if (BUF_MODIFF (buf) != modiff)
|
|
345 hairy = 2; */
|
|
346 }
|
|
347 }
|
|
348 if ((c1 == ' ' || c1 == '\n')
|
|
349 && !noautofill
|
|
350 && !NILP (buf->auto_fill_function))
|
|
351 {
|
|
352 buffer_insert_emacs_char (buf, c1);
|
|
353 if (c1 == '\n')
|
|
354 /* After inserting a newline, move to previous line and fill */
|
|
355 /* that. Must have the newline in place already so filling and */
|
|
356 /* justification, if any, know where the end is going to be. */
|
|
357 BUF_SET_PT (buf, BUF_PT (buf) - 1);
|
|
358 call0 (buf->auto_fill_function);
|
|
359 if (c1 == '\n')
|
|
360 BUF_SET_PT (buf, BUF_PT (buf) + 1);
|
|
361 /* hairy = 2; */
|
|
362 }
|
|
363 else
|
|
364 buffer_insert_emacs_char (buf, c1);
|
|
365
|
|
366 /* If previous command specified a face to use, use it. */
|
|
367 if (!NILP (Vself_insert_face)
|
|
368 && EQ (Vlast_command, Vself_insert_face_command))
|
|
369 {
|
|
370 Lisp_Object before, after;
|
|
371 XSETINT (before, BUF_PT (buf) - 1);
|
|
372 XSETINT (after, BUF_PT (buf));
|
|
373 Fput_text_property (before, after, Qface, Vself_insert_face, Qnil);
|
|
374 Fput_text_property (before, after, Qstart_open, Qt, Qnil);
|
|
375 Fput_text_property (before, after, Qend_open, Qnil, Qnil);
|
|
376 /* #### FSFmacs properties are normally closed ("sticky") on the
|
|
377 end but not the beginning. It's the opposite for us. */
|
|
378 Vself_insert_face = Qnil;
|
|
379 }
|
|
380 synt = SYNTAX (syntax_table, c1);
|
|
381 if ((synt == Sclose || synt == Smath)
|
|
382 && !NILP (Vblink_paren_function) && INTERACTIVE
|
|
383 && !noautofill)
|
|
384 {
|
|
385 call0 (Vblink_paren_function);
|
|
386 /* hairy = 2; */
|
|
387 }
|
|
388
|
|
389 /* return hairy; */
|
|
390 }
|
|
391
|
|
392 /* (this comes from Mule but is a generally good idea) */
|
|
393
|
20
|
394 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
|
0
|
395 Invoke `self-insert-command' as if CH is entered from keyboard.
|
20
|
396 */
|
|
397 (ch))
|
0
|
398 {
|
|
399 /* This function can GC */
|
|
400 CHECK_CHAR_COERCE_INT (ch);
|
|
401 internal_self_insert (XCHAR (ch), 0);
|
|
402 return Qnil;
|
|
403 }
|
|
404
|
|
405 /* module initialization */
|
|
406
|
|
407 void
|
|
408 syms_of_cmds (void)
|
|
409 {
|
|
410 defsymbol (&Qkill_forward_chars, "kill-forward-chars");
|
|
411 defsymbol (&Qself_insert_command, "self-insert-command");
|
|
412 defsymbol (&Qoverwrite_mode_binary, "overwrite-mode-binary");
|
|
413
|
20
|
414 DEFSUBR (Fforward_char);
|
|
415 DEFSUBR (Fbackward_char);
|
|
416 DEFSUBR (Fforward_line);
|
|
417 DEFSUBR (Fbeginning_of_line);
|
|
418 DEFSUBR (Fend_of_line);
|
0
|
419
|
20
|
420 DEFSUBR (Fdelete_char);
|
|
421 DEFSUBR (Fdelete_backward_char);
|
0
|
422
|
20
|
423 DEFSUBR (Fself_insert_command);
|
|
424 DEFSUBR (Fself_insert_internal);
|
0
|
425 }
|
|
426
|
|
427 void
|
|
428 vars_of_cmds (void)
|
|
429 {
|
|
430 DEFVAR_LISP ("self-insert-face", &Vself_insert_face /*
|
|
431 If non-nil, set the face of the next self-inserting character to this.
|
|
432 See also `self-insert-face-command'.
|
|
433 */ );
|
|
434 Vself_insert_face = Qnil;
|
|
435
|
|
436 DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command /*
|
|
437 This is the command that set up `self-insert-face'.
|
|
438 If `last-command' does not equal this value, we ignore `self-insert-face'.
|
|
439 */ );
|
|
440 Vself_insert_face_command = Qnil;
|
|
441
|
|
442 DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function /*
|
|
443 Function called, if non-nil, whenever a close parenthesis is inserted.
|
|
444 More precisely, a char with closeparen syntax is self-inserted.
|
|
445 */ );
|
|
446 Vblink_paren_function = Qnil;
|
|
447 }
|