1 /* Simple built-in editing commands.
2 Copyright (C) 1985, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Copyright (C) 2001 MORIOKA Tomohiko
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Mule 2.0, FSF 19.30. */
31 Lisp_Object Qkill_forward_chars;
32 Lisp_Object Qself_insert_command;
33 Lisp_Object Qno_self_insert;
35 Lisp_Object Vblink_paren_function;
37 /* A possible value for a buffer's overwrite-mode variable. */
38 Lisp_Object Qoverwrite_mode_binary;
40 /* Non-nil means put this face on the next self-inserting character. */
41 Lisp_Object Vself_insert_face;
43 /* This is the command that set up Vself_insert_face. */
44 Lisp_Object Vself_insert_face_command;
46 /* A char-table for characters which may invoke auto-filling. */
47 Lisp_Object Vauto_fill_chars;
49 DEFUN ("forward-char", Fforward_char, 0, 2, "_p", /*
50 Move point right COUNT characters (left if COUNT is negative).
51 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
52 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
53 On reaching end of buffer, stop and signal error.
57 struct buffer *buf = decode_buffer (buffer, 1);
68 /* This used to just set point to point + XINT (count), and then check
69 to see if it was within boundaries. But now that SET_PT can
70 potentially do a lot of stuff (calling entering and exiting
71 hooks, etcetera), that's not a good approach. So we validate the
72 proposed position, then set point. */
74 Bufpos new_point = BUF_PT (buf) + n;
76 if (new_point < BUF_BEGV (buf))
78 BUF_SET_PT (buf, BUF_BEGV (buf));
79 Fsignal (Qbeginning_of_buffer, Qnil);
82 if (new_point > BUF_ZV (buf))
84 BUF_SET_PT (buf, BUF_ZV (buf));
85 Fsignal (Qend_of_buffer, Qnil);
89 BUF_SET_PT (buf, new_point);
95 DEFUN ("backward-char", Fbackward_char, 0, 2, "_p", /*
96 Move point left COUNT characters (right if COUNT is negative).
97 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
98 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
103 count = make_int (-1);
107 count = make_int (- XINT (count));
109 return Fforward_char (count, buffer);
112 DEFUN ("forward-line", Fforward_line, 0, 2, "_p", /*
113 Move COUNT lines forward (backward if COUNT is negative).
114 Precisely, if point is on line I, move to the start of line I + COUNT.
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 COUNT - number of lines moved; if backward, COUNT + number moved.
118 With positive COUNT, 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.
124 struct buffer *buf = decode_buffer (buffer, 1);
125 Bufpos pos2 = BUF_PT (buf);
127 EMACS_INT n, shortage, negp;
138 pos = scan_buffer (buf, '\n', pos2, 0, n - negp, &shortage, 1);
141 || (BUF_ZV (buf) > BUF_BEGV (buf)
143 && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
145 BUF_SET_PT (buf, pos);
146 return make_int (negp ? - shortage : shortage);
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 COUNT not nil or 1, move forward COUNT - 1 lines first.
152 If scan reaches end of buffer, return that position.
153 This function does not move point.
157 struct buffer *b = decode_buffer (buffer, 1);
158 REGISTER int orig, end;
160 XSETBUFFER (buffer, b);
162 count = make_int (0);
166 count = make_int (XINT (count) - 1);
170 Fforward_line (count, buffer);
172 BUF_SET_PT (b, orig);
174 return make_int (end);
177 DEFUN ("beginning-of-line", Fbeginning_of_line, 0, 2, "_p", /*
178 Move point to beginning of current line.
179 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
180 If scan reaches end of buffer, stop there without error.
181 If BUFFER is nil, the current buffer is assumed.
185 struct buffer *b = decode_buffer (buffer, 1);
187 BUF_SET_PT (b, XINT (Fpoint_at_bol (count, buffer)));
191 DEFUN ("point-at-eol", Fpoint_at_eol, 0, 2, 0, /*
192 Return the character position of the last character on the current line.
193 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
194 If scan reaches end of buffer, return that position.
195 This function does not move point.
199 struct buffer *buf = decode_buffer (buffer, 1);
210 return make_int (find_before_next_newline (buf, BUF_PT (buf), 0,
214 DEFUN ("end-of-line", Fend_of_line, 0, 2, "_p", /*
215 Move point to end of current line.
216 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
217 If scan reaches end of buffer, stop there without error.
218 If BUFFER is nil, the current buffer is assumed.
222 struct buffer *b = decode_buffer (buffer, 1);
224 BUF_SET_PT (b, XINT (Fpoint_at_eol (count, buffer)));
228 DEFUN ("delete-char", Fdelete_char, 0, 2, "*p\nP", /*
229 Delete the following COUNT characters (previous, with negative COUNT).
230 Optional second arg KILLP non-nil means kill instead (save in kill ring).
231 Interactively, COUNT is the prefix arg, and KILLP is set if
232 COUNT was explicitly specified.
236 /* This function can GC */
238 struct buffer *buf = current_buffer;
249 pos = BUF_PT (buf) + n;
254 if (pos < BUF_BEGV (buf))
255 signal_error (Qbeginning_of_buffer, Qnil);
257 buffer_delete_range (buf, pos, BUF_PT (buf), 0);
261 if (pos > BUF_ZV (buf))
262 signal_error (Qend_of_buffer, Qnil);
264 buffer_delete_range (buf, BUF_PT (buf), pos, 0);
269 call1 (Qkill_forward_chars, count);
274 DEFUN ("delete-backward-char", Fdelete_backward_char, 0, 2, "*p\nP", /*
275 Delete the previous COUNT characters (following, with negative COUNT).
276 Optional second arg KILLP non-nil means kill instead (save in kill ring).
277 Interactively, COUNT is the prefix arg, and KILLP is set if
278 COUNT was explicitly specified.
282 /* This function can GC */
293 return Fdelete_char (make_int (- n), killp);
296 static void internal_self_insert (Emchar ch, int noautofill);
298 DEFUN ("self-insert-command", Fself_insert_command, 1, 1, "*p", /*
299 Insert the character you type.
300 Whichever character you type to run this command is inserted.
301 If a prefix arg COUNT is specified, the character is inserted COUNT times.
305 /* This function can GC */
310 CHECK_NATNUM (count);
313 if (CHAR_OR_CHAR_INTP (Vlast_command_char))
314 c = Vlast_command_char;
316 c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
319 signal_simple_error ("Last typed character has no ASCII equivalent",
320 Fcopy_event (Vlast_command_event, Qnil));
322 CHECK_CHAR_COERCE_INT (c);
327 internal_self_insert (ch, (n != 0));
332 /* Insert character C1. If NOAUTOFILL is nonzero, don't do autofill
333 even if it is enabled.
337 If this insertion is suitable for direct output (completely simple),
338 return 0. A value of 1 indicates this *might* not have been simple.
339 A value of 2 means this did things that call for an undo boundary. */
342 internal_self_insert (Emchar c1, int noautofill)
344 /* This function can GC */
345 /* int hairy = 0; -- unused */
346 REGISTER enum syntaxcode synt;
348 Lisp_Object overwrite;
349 Lisp_Char_Table *syntax_table;
350 struct buffer *buf = current_buffer;
353 overwrite = buf->overwrite_mode;
355 syntax_table = XCHAR_TABLE (buf->syntax_table);
357 syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
361 /* No, this is very bad, it makes undo *always* undo a character at a time
362 instead of grouping consecutive self-inserts together. Nasty nasty.
364 if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
365 || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
369 if (!NILP (overwrite)
370 && BUF_PT (buf) < BUF_ZV (buf)
371 && (EQ (overwrite, Qoverwrite_mode_binary)
372 || (c1 != '\n' && BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\n'))
373 && (EQ (overwrite, Qoverwrite_mode_binary)
374 || BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\t'
375 || ((tab_width = XINT (buf->tab_width), tab_width <= 0)
377 || !((current_column (buf) + 1) % tab_width))))
379 buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
383 if (!NILP (buf->abbrev_mode)
384 && !WORD_SYNTAX_P (syntax_table, c1)
385 && NILP (buf->read_only)
386 && BUF_PT (buf) > BUF_BEGV (buf))
388 c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
390 if (WORD_SYNTAX_P (syntax_table, c2))
395 Lisp_Object sym = Fexpand_abbrev ();
397 /* I think this is too bogus to add. The function should
398 have a way of examining the character to be inserted, so
399 it can decide whether to insert it or not. We should
400 design it better than that. */
402 /* Here FSFmacs remembers MODIFF, compares it after
403 Fexpand_abbrev() finishes, and updates HAIRY. */
405 /* NOTE: we cannot simply check for Vlast_abbrev, because
406 Fexpand_abbrev() can bail out before setting it to
407 anything meaningful, leaving us stuck with an old value.
408 Thus Fexpand_abbrev() was extended to return the actual
411 && !NILP (symbol_function (XSYMBOL (sym)))
412 && SYMBOLP (symbol_function (XSYMBOL (sym))))
414 Lisp_Object prop = Fget (symbol_function (XSYMBOL (sym)),
415 Qno_self_insert, Qnil);
422 if ((CHAR_TABLEP (Vauto_fill_chars)
423 ? !NILP (XCHAR_TABLE_VALUE_UNSAFE (Vauto_fill_chars, c1))
424 : (c1 == ' ' || c1 == '\n'))
426 && !NILP (buf->auto_fill_function))
428 buffer_insert_emacs_char (buf, c1);
430 /* After inserting a newline, move to previous line and fill */
431 /* that. Must have the newline in place already so filling and */
432 /* justification, if any, know where the end is going to be. */
433 BUF_SET_PT (buf, BUF_PT (buf) - 1);
434 call0 (buf->auto_fill_function);
436 BUF_SET_PT (buf, BUF_PT (buf) + 1);
440 buffer_insert_emacs_char (buf, c1);
442 /* If previous command specified a face to use, use it. */
443 if (!NILP (Vself_insert_face)
444 && EQ (Vlast_command, Vself_insert_face_command))
446 Lisp_Object before = make_int (BUF_PT (buf) - 1);
447 Lisp_Object after = make_int (BUF_PT (buf));
448 Fput_text_property (before, after, Qface, Vself_insert_face, Qnil);
449 Fput_text_property (before, after, Qstart_open, Qt, Qnil);
450 Fput_text_property (before, after, Qend_open, Qnil, Qnil);
451 /* #### FSFmacs properties are normally closed ("sticky") on the
452 end but not the beginning. It's the opposite for us. */
453 Vself_insert_face = Qnil;
455 synt = SYNTAX (syntax_table, c1);
456 if ((synt == Sclose || synt == Smath)
457 && !NILP (Vblink_paren_function) && INTERACTIVE
460 call0 (Vblink_paren_function);
467 /* (this comes from Mule but is a generally good idea) */
469 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
470 Invoke `self-insert-command' as if CHARACTER is entered from keyboard.
474 /* This function can GC */
475 CHECK_CHAR_COERCE_INT (character);
476 internal_self_insert (XCHAR (character), 0);
480 /* module initialization */
485 defsymbol (&Qkill_forward_chars, "kill-forward-chars");
486 defsymbol (&Qself_insert_command, "self-insert-command");
487 defsymbol (&Qoverwrite_mode_binary, "overwrite-mode-binary");
488 defsymbol (&Qno_self_insert, "no-self-insert");
490 DEFSUBR (Fforward_char);
491 DEFSUBR (Fbackward_char);
492 DEFSUBR (Fforward_line);
493 DEFSUBR (Fbeginning_of_line);
494 DEFSUBR (Fend_of_line);
496 DEFSUBR (Fpoint_at_bol);
497 DEFSUBR (Fpoint_at_eol);
499 DEFSUBR (Fdelete_char);
500 DEFSUBR (Fdelete_backward_char);
502 DEFSUBR (Fself_insert_command);
503 DEFSUBR (Fself_insert_internal);
509 DEFVAR_LISP ("self-insert-face", &Vself_insert_face /*
510 If non-nil, set the face of the next self-inserting character to this.
511 See also `self-insert-face-command'.
513 Vself_insert_face = Qnil;
515 DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command /*
516 This is the command that set up `self-insert-face'.
517 If `last-command' does not equal this value, we ignore `self-insert-face'.
519 Vself_insert_face_command = Qnil;
521 DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function /*
522 Function called, if non-nil, whenever a close parenthesis is inserted.
523 More precisely, a char with closeparen syntax is self-inserted.
525 Vblink_paren_function = Qnil;
527 DEFVAR_LISP ("auto-fill-chars", &Vauto_fill_chars /*
528 A char-table for characters which invoke auto-filling.
529 Such characters have value t in this table.
531 Vauto_fill_chars = Fmake_char_table (Qgeneric);
533 put_char_id_table_0 (XCHAR_TABLE (Vauto_fill_chars), ' ', Qt);
534 put_char_id_table_0 (XCHAR_TABLE (Vauto_fill_chars), '\n', Qt);
536 XCHAR_TABLE (Vauto_fill_chars)->ascii[' '] = Qt;
537 XCHAR_TABLE (Vauto_fill_chars)->ascii['\n'] = Qt;