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