1 /* Simple built-in editing commands.
2 Copyright (C) 1985, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4 This file is part of XEmacs.
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
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
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. */
21 /* Synched up with: Mule 2.0, FSF 19.30. */
30 Lisp_Object Qkill_forward_chars;
31 Lisp_Object Qself_insert_command;
32 Lisp_Object Qno_self_insert;
34 Lisp_Object Vblink_paren_function;
36 /* A possible value for a buffer's overwrite-mode variable. */
37 Lisp_Object Qoverwrite_mode_binary;
39 /* Non-nil means put this face on the next self-inserting character. */
40 Lisp_Object Vself_insert_face;
42 /* This is the command that set up Vself_insert_face. */
43 Lisp_Object Vself_insert_face_command;
45 /* A char-table for characters which may invoke auto-filling. */
46 Lisp_Object Vauto_fill_chars;
48 DEFUN ("forward-char", Fforward_char, 0, 2, "_p", /*
49 Move point right COUNT characters (left if COUNT is negative).
50 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
51 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
52 On reaching end of buffer, stop and signal error.
56 struct buffer *buf = decode_buffer (buffer, 1);
67 /* This used to just set point to point + XINT (count), and then check
68 to see if it was within boundaries. But now that SET_PT can
69 potentially do a lot of stuff (calling entering and exiting
70 hooks, etcetera), that's not a good approach. So we validate the
71 proposed position, then set point. */
73 Bufpos new_point = BUF_PT (buf) + n;
75 if (new_point < BUF_BEGV (buf))
77 BUF_SET_PT (buf, BUF_BEGV (buf));
78 Fsignal (Qbeginning_of_buffer, Qnil);
81 if (new_point > BUF_ZV (buf))
83 BUF_SET_PT (buf, BUF_ZV (buf));
84 Fsignal (Qend_of_buffer, Qnil);
88 BUF_SET_PT (buf, new_point);
94 DEFUN ("backward-char", Fbackward_char, 0, 2, "_p", /*
95 Move point left COUNT characters (right if COUNT is negative).
96 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
97 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
102 count = make_int (-1);
106 count = make_int (- XINT (count));
108 return Fforward_char (count, buffer);
111 DEFUN ("forward-line", Fforward_line, 0, 2, "_p", /*
112 Move COUNT lines forward (backward if COUNT is negative).
113 Precisely, if point is on line I, move to the start of line I + COUNT.
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 COUNT - number of lines moved; if backward, COUNT + number moved.
117 With positive COUNT, 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.
123 struct buffer *buf = decode_buffer (buffer, 1);
124 Bufpos pos2 = BUF_PT (buf);
126 EMACS_INT n, shortage, negp;
137 pos = scan_buffer (buf, '\n', pos2, 0, n - negp, &shortage, 1);
140 || (BUF_ZV (buf) > BUF_BEGV (buf)
142 && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
144 BUF_SET_PT (buf, pos);
145 return make_int (negp ? - shortage : shortage);
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 COUNT not nil or 1, move forward COUNT - 1 lines first.
151 If scan reaches end of buffer, return that position.
152 This function does not move point.
156 struct buffer *b = decode_buffer (buffer, 1);
157 REGISTER int orig, end;
159 XSETBUFFER (buffer, b);
161 count = make_int (0);
165 count = make_int (XINT (count) - 1);
169 Fforward_line (count, buffer);
171 BUF_SET_PT (b, orig);
173 return make_int (end);
176 DEFUN ("beginning-of-line", Fbeginning_of_line, 0, 2, "_p", /*
177 Move point to beginning of current line.
178 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
179 If scan reaches end of buffer, stop there without error.
180 If BUFFER is nil, the current buffer is assumed.
184 struct buffer *b = decode_buffer (buffer, 1);
186 BUF_SET_PT (b, XINT (Fpoint_at_bol (count, buffer)));
190 DEFUN ("point-at-eol", Fpoint_at_eol, 0, 2, 0, /*
191 Return the character position of the last character on the current line.
192 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
193 If scan reaches end of buffer, return that position.
194 This function does not move point.
198 struct buffer *buf = decode_buffer (buffer, 1);
209 return make_int (find_before_next_newline (buf, BUF_PT (buf), 0,
213 DEFUN ("end-of-line", Fend_of_line, 0, 2, "_p", /*
214 Move point to end of current line.
215 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
216 If scan reaches end of buffer, stop there without error.
217 If BUFFER is nil, the current buffer is assumed.
221 struct buffer *b = decode_buffer (buffer, 1);
223 BUF_SET_PT (b, XINT (Fpoint_at_eol (count, buffer)));
227 DEFUN ("delete-char", Fdelete_char, 0, 2, "*p\nP", /*
228 Delete the following COUNT characters (previous, with negative COUNT).
229 Optional second arg KILLP non-nil means kill instead (save in kill ring).
230 Interactively, COUNT is the prefix arg, and KILLP is set if
231 COUNT was explicitly specified.
235 /* This function can GC */
237 struct buffer *buf = current_buffer;
248 pos = BUF_PT (buf) + n;
253 if (pos < BUF_BEGV (buf))
254 signal_error (Qbeginning_of_buffer, Qnil);
256 buffer_delete_range (buf, pos, BUF_PT (buf), 0);
260 if (pos > BUF_ZV (buf))
261 signal_error (Qend_of_buffer, Qnil);
263 buffer_delete_range (buf, BUF_PT (buf), pos, 0);
268 call1 (Qkill_forward_chars, count);
273 DEFUN ("delete-backward-char", Fdelete_backward_char, 0, 2, "*p\nP", /*
274 Delete the previous COUNT characters (following, with negative COUNT).
275 Optional second arg KILLP non-nil means kill instead (save in kill ring).
276 Interactively, COUNT is the prefix arg, and KILLP is set if
277 COUNT was explicitly specified.
281 /* This function can GC */
292 return Fdelete_char (make_int (- n), killp);
295 static void internal_self_insert (Emchar ch, int noautofill);
297 DEFUN ("self-insert-command", Fself_insert_command, 1, 1, "*p", /*
298 Insert the character you type.
299 Whichever character you type to run this command is inserted.
300 If a prefix arg COUNT is specified, the character is inserted COUNT times.
304 /* This function can GC */
309 CHECK_NATNUM (count);
312 if (CHAR_OR_CHAR_INTP (Vlast_command_char))
313 c = Vlast_command_char;
315 c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
318 signal_simple_error ("Last typed character has no ASCII equivalent",
319 Fcopy_event (Vlast_command_event, Qnil));
321 CHECK_CHAR_COERCE_INT (c);
326 internal_self_insert (ch, (n != 0));
331 /* Insert character C1. If NOAUTOFILL is nonzero, don't do autofill
332 even if it is enabled.
336 If this insertion is suitable for direct output (completely simple),
337 return 0. A value of 1 indicates this *might* not have been simple.
338 A value of 2 means this did things that call for an undo boundary. */
341 internal_self_insert (Emchar c1, int noautofill)
343 /* This function can GC */
344 /* int hairy = 0; -- unused */
345 REGISTER enum syntaxcode synt;
347 Lisp_Object overwrite;
348 Lisp_Char_Table *syntax_table;
349 struct buffer *buf = current_buffer;
352 overwrite = buf->overwrite_mode;
353 syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
356 /* No, this is very bad, it makes undo *always* undo a character at a time
357 instead of grouping consecutive self-inserts together. Nasty nasty.
359 if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
360 || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
364 if (!NILP (overwrite)
365 && BUF_PT (buf) < BUF_ZV (buf)
366 && (EQ (overwrite, Qoverwrite_mode_binary)
367 || (c1 != '\n' && BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\n'))
368 && (EQ (overwrite, Qoverwrite_mode_binary)
369 || BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\t'
370 || ((tab_width = XINT (buf->tab_width), tab_width <= 0)
372 || !((current_column (buf) + 1) % tab_width))))
374 buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
378 if (!NILP (buf->abbrev_mode)
379 && !WORD_SYNTAX_P (syntax_table, c1)
380 && NILP (buf->read_only)
381 && BUF_PT (buf) > BUF_BEGV (buf))
383 c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
385 if (WORD_SYNTAX_P (syntax_table, c2))
390 Lisp_Object sym = Fexpand_abbrev ();
392 /* I think this is too bogus to add. The function should
393 have a way of examining the character to be inserted, so
394 it can decide whether to insert it or not. We should
395 design it better than that. */
397 /* Here FSFmacs remembers MODIFF, compares it after
398 Fexpand_abbrev() finishes, and updates HAIRY. */
400 /* NOTE: we cannot simply check for Vlast_abbrev, because
401 Fexpand_abbrev() can bail out before setting it to
402 anything meaningful, leaving us stuck with an old value.
403 Thus Fexpand_abbrev() was extended to return the actual
406 && !NILP (symbol_function (XSYMBOL (sym)))
407 && SYMBOLP (symbol_function (XSYMBOL (sym))))
409 Lisp_Object prop = Fget (symbol_function (XSYMBOL (sym)),
410 Qno_self_insert, Qnil);
417 if ((CHAR_TABLEP (Vauto_fill_chars)
418 ? !NILP (XCHAR_TABLE_VALUE_UNSAFE (Vauto_fill_chars, c1))
419 : (c1 == ' ' || c1 == '\n'))
421 && !NILP (buf->auto_fill_function))
423 buffer_insert_emacs_char (buf, c1);
425 /* After inserting a newline, move to previous line and fill */
426 /* that. Must have the newline in place already so filling and */
427 /* justification, if any, know where the end is going to be. */
428 BUF_SET_PT (buf, BUF_PT (buf) - 1);
429 call0 (buf->auto_fill_function);
431 BUF_SET_PT (buf, BUF_PT (buf) + 1);
435 buffer_insert_emacs_char (buf, c1);
437 /* If previous command specified a face to use, use it. */
438 if (!NILP (Vself_insert_face)
439 && EQ (Vlast_command, Vself_insert_face_command))
441 Lisp_Object before = make_int (BUF_PT (buf) - 1);
442 Lisp_Object after = make_int (BUF_PT (buf));
443 Fput_text_property (before, after, Qface, Vself_insert_face, Qnil);
444 Fput_text_property (before, after, Qstart_open, Qt, Qnil);
445 Fput_text_property (before, after, Qend_open, Qnil, Qnil);
446 /* #### FSFmacs properties are normally closed ("sticky") on the
447 end but not the beginning. It's the opposite for us. */
448 Vself_insert_face = Qnil;
450 synt = SYNTAX (syntax_table, c1);
451 if ((synt == Sclose || synt == Smath)
452 && !NILP (Vblink_paren_function) && INTERACTIVE
455 call0 (Vblink_paren_function);
462 /* (this comes from Mule but is a generally good idea) */
464 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
465 Invoke `self-insert-command' as if CHARACTER is entered from keyboard.
469 /* This function can GC */
470 CHECK_CHAR_COERCE_INT (character);
471 internal_self_insert (XCHAR (character), 0);
475 /* module initialization */
480 defsymbol (&Qkill_forward_chars, "kill-forward-chars");
481 defsymbol (&Qself_insert_command, "self-insert-command");
482 defsymbol (&Qoverwrite_mode_binary, "overwrite-mode-binary");
483 defsymbol (&Qno_self_insert, "no-self-insert");
485 DEFSUBR (Fforward_char);
486 DEFSUBR (Fbackward_char);
487 DEFSUBR (Fforward_line);
488 DEFSUBR (Fbeginning_of_line);
489 DEFSUBR (Fend_of_line);
491 DEFSUBR (Fpoint_at_bol);
492 DEFSUBR (Fpoint_at_eol);
494 DEFSUBR (Fdelete_char);
495 DEFSUBR (Fdelete_backward_char);
497 DEFSUBR (Fself_insert_command);
498 DEFSUBR (Fself_insert_internal);
504 DEFVAR_LISP ("self-insert-face", &Vself_insert_face /*
505 If non-nil, set the face of the next self-inserting character to this.
506 See also `self-insert-face-command'.
508 Vself_insert_face = Qnil;
510 DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command /*
511 This is the command that set up `self-insert-face'.
512 If `last-command' does not equal this value, we ignore `self-insert-face'.
514 Vself_insert_face_command = Qnil;
516 DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function /*
517 Function called, if non-nil, whenever a close parenthesis is inserted.
518 More precisely, a char with closeparen syntax is self-inserted.
520 Vblink_paren_function = Qnil;
522 DEFVAR_LISP ("auto-fill-chars", &Vauto_fill_chars /*
523 A char-table for characters which invoke auto-filling.
524 Such characters have value t in this table.
526 Vauto_fill_chars = Fmake_char_table (Qgeneric);
527 XCHAR_TABLE (Vauto_fill_chars)->ascii[' '] = Qt;
528 XCHAR_TABLE (Vauto_fill_chars)->ascii['\n'] = Qt;