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