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 DEFUN ("forward-char", Fforward_char, 0, 2, "_p", /*
46 Move point right ARG characters (left if ARG negative).
47 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
48 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
49 On reaching end of buffer, stop and signal error.
53 struct buffer *buf = decode_buffer (buffer, 1);
60 /* This used to just set point to point + XINT (arg), and then check
61 to see if it was within boundaries. But now that SET_PT can
62 potentially do a lot of stuff (calling entering and exiting
63 hooks, etcetera), that's not a good approach. So we validate the
64 proposed position, then set point. */
66 Bufpos new_point = BUF_PT (buf) + XINT (arg);
68 if (new_point < BUF_BEGV (buf))
70 BUF_SET_PT (buf, BUF_BEGV (buf));
71 Fsignal (Qbeginning_of_buffer, Qnil);
74 if (new_point > BUF_ZV (buf))
76 BUF_SET_PT (buf, BUF_ZV (buf));
77 Fsignal (Qend_of_buffer, Qnil);
81 BUF_SET_PT (buf, new_point);
87 DEFUN ("backward-char", Fbackward_char, 0, 2, "_p", /*
88 Move point left ARG characters (right if ARG negative).
89 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
90 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
99 XSETINT (arg, - XINT (arg));
100 return Fforward_char (arg, buffer);
103 DEFUN ("forward-line", Fforward_line, 0, 2, "_p", /*
104 Move ARG lines forward (backward if ARG is negative).
105 Precisely, if point is on line I, move to the start of line I + ARG.
106 If there isn't room, go as far as possible (no error).
107 Returns the count of lines left to move. If moving forward,
108 that is ARG - number of lines moved; if backward, ARG + number moved.
109 With positive ARG, a non-empty line at the end counts as one line
110 successfully moved (for the return value).
111 If BUFFER is nil, the current buffer is assumed.
115 struct buffer *buf = decode_buffer (buffer, 1);
116 Bufpos pos2 = BUF_PT (buf);
118 EMACS_INT count, shortage, negp;
129 pos = scan_buffer (buf, '\n', pos2, 0, count - negp, &shortage, 1);
132 || (BUF_ZV (buf) > BUF_BEGV (buf)
134 && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
136 BUF_SET_PT (buf, pos);
137 return make_int (negp ? - shortage : shortage);
140 DEFUN ("point-at-bol", Fpoint_at_bol, 0, 2, 0, /*
141 Return the character position of the first character on the current line.
142 With argument N not nil or 1, move forward N - 1 lines first.
143 If scan reaches end of buffer, return that position.
144 This function does not move point.
148 struct buffer *b = decode_buffer (buffer, 1);
149 REGISTER int orig, end;
151 XSETBUFFER (buffer, b);
158 Fforward_line (make_int (XINT (arg) - 1), buffer);
162 return make_int (end);
165 DEFUN ("beginning-of-line", Fbeginning_of_line, 0, 2, "_p", /*
166 Move point to beginning of current line.
167 With argument ARG not nil or 1, move forward ARG - 1 lines first.
168 If scan reaches end of buffer, stop there without error.
169 If BUFFER is nil, the current buffer is assumed.
173 struct buffer *b = decode_buffer (buffer, 1);
175 BUF_SET_PT(b, XINT (Fpoint_at_bol(arg, buffer)));
179 DEFUN ("point-at-eol", Fpoint_at_eol, 0, 2, 0, /*
180 Return the character position of the last character on the current line.
181 With argument N not nil or 1, move forward N - 1 lines first.
182 If scan reaches end of buffer, return that position.
183 This function does not move point.
187 struct buffer *buf = decode_buffer (buffer, 1);
189 XSETBUFFER (buffer, buf);
196 return make_int (find_before_next_newline (buf, BUF_PT (buf), 0,
197 XINT (arg) - (XINT (arg) <= 0)));
200 DEFUN ("end-of-line", Fend_of_line, 0, 2, "_p", /*
201 Move point to end of current line.
202 With argument ARG not nil or 1, move forward ARG - 1 lines first.
203 If scan reaches end of buffer, stop there without error.
204 If BUFFER is nil, the current buffer is assumed.
208 struct buffer *b = decode_buffer (buffer, 1);
210 BUF_SET_PT(b, XINT (Fpoint_at_eol (arg, buffer)));
214 DEFUN ("delete-char", Fdelete_char, 1, 2, "*p\nP", /*
215 Delete the following ARG characters (previous, with negative arg).
216 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).
217 Interactively, ARG is the prefix arg, and KILLFLAG is set if
218 ARG was explicitly specified.
222 /* This function can GC */
224 struct buffer *buf = current_buffer;
228 pos = BUF_PT (buf) + XINT (arg);
233 if (pos < BUF_BEGV (buf))
234 signal_error (Qbeginning_of_buffer, Qnil);
236 buffer_delete_range (buf, pos, BUF_PT (buf), 0);
240 if (pos > BUF_ZV (buf))
241 signal_error (Qend_of_buffer, Qnil);
243 buffer_delete_range (buf, BUF_PT (buf), pos, 0);
248 call1 (Qkill_forward_chars, arg);
253 DEFUN ("delete-backward-char", Fdelete_backward_char, 1, 2, "*p\nP", /*
254 Delete the previous ARG characters (following, with negative ARG).
255 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).
256 Interactively, ARG is the prefix arg, and KILLFLAG is set if
257 ARG was explicitly specified.
261 /* This function can GC */
263 return Fdelete_char (make_int (-XINT (arg)), killflag);
266 static void internal_self_insert (Emchar ch, int noautofill);
268 DEFUN ("self-insert-command", Fself_insert_command, 1, 1, "*p", /*
269 Insert the character you type.
270 Whichever character you type to run this command is inserted.
274 /* This function can GC */
280 if (CHAR_OR_CHAR_INTP (Vlast_command_char))
281 c = Vlast_command_char;
283 c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
286 signal_simple_error ("last typed character has no ASCII equivalent",
287 Fcopy_event (Vlast_command_event, Qnil));
289 CHECK_CHAR_COERCE_INT (c);
294 /* #### This optimization won't work because of differences in
295 how the start-open and end-open properties default for text
296 properties. See internal_self_insert(). */
297 if (n >= 2 && NILP (current_buffer->overwrite_mode))
300 /* The first one might want to expand an abbrev. */
301 internal_self_insert (c, 1);
302 /* The bulk of the copies of this char can be inserted simply.
303 We don't have to handle a user-specified face specially
304 because it will get inherited from the first char inserted. */
305 Finsert_char (make_char (c), make_int (n), Qt, Qnil);
306 /* The last one might want to auto-fill. */
307 internal_self_insert (c, 0);
314 internal_self_insert (ch, (n != 0));
319 /* Insert character C1. If NOAUTOFILL is nonzero, don't do autofill
320 even if it is enabled.
324 If this insertion is suitable for direct output (completely simple),
325 return 0. A value of 1 indicates this *might* not have been simple.
326 A value of 2 means this did things that call for an undo boundary. */
329 internal_self_insert (Emchar c1, int noautofill)
331 /* This function can GC */
332 /* int hairy = 0; -- unused */
333 REGISTER enum syntaxcode synt;
335 Lisp_Object overwrite;
336 struct Lisp_Char_Table *syntax_table;
337 struct buffer *buf = current_buffer;
339 overwrite = buf->overwrite_mode;
340 syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
343 /* No, this is very bad, it makes undo *always* undo a character at a time
344 instead of grouping consecutive self-inserts together. Nasty nasty.
346 if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
347 || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
351 if (!NILP (overwrite)
352 && BUF_PT (buf) < BUF_ZV (buf)
353 && (EQ (overwrite, Qoverwrite_mode_binary)
354 || (c1 != '\n' && BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\n'))
355 && (EQ (overwrite, Qoverwrite_mode_binary)
356 || BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\t'
357 || XINT (buf->tab_width) <= 0
358 || XINT (buf->tab_width) > 20
359 || !((current_column (buf) + 1) % XINT (buf->tab_width))))
361 buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
365 if (!NILP (buf->abbrev_mode)
366 && !WORD_SYNTAX_P (syntax_table, c1)
367 && NILP (buf->read_only)
368 && BUF_PT (buf) > BUF_BEGV (buf))
370 c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
372 if (WORD_SYNTAX_P (syntax_table, c2))
377 Lisp_Object sym = Fexpand_abbrev ();
379 /* I think this is too bogus to add. The function should
380 have a way of examining the character to be inserted, so
381 it can decide whether to insert it or not. We should
382 design it better than that. */
384 /* Here FSFmacs remembers MODIFF, compares it after
385 Fexpand_abbrev() finishes, and updates HAIRY. */
387 /* NOTE: we cannot simply check for Vlast_abbrev, because
388 Fexpand_abbrev() can bail out before setting it to
389 anything meaningful, leaving us stuck with an old value.
390 Thus Fexpand_abbrev() was extended to return the actual
393 && !NILP (symbol_function (XSYMBOL (sym)))
394 && SYMBOLP (symbol_function (XSYMBOL (sym))))
396 Lisp_Object prop = Fget (symbol_function (XSYMBOL (sym)),
397 Qno_self_insert, Qnil);
404 if ((c1 == ' ' || c1 == '\n')
406 && !NILP (buf->auto_fill_function))
408 buffer_insert_emacs_char (buf, c1);
410 /* After inserting a newline, move to previous line and fill */
411 /* that. Must have the newline in place already so filling and */
412 /* justification, if any, know where the end is going to be. */
413 BUF_SET_PT (buf, BUF_PT (buf) - 1);
414 call0 (buf->auto_fill_function);
416 BUF_SET_PT (buf, BUF_PT (buf) + 1);
420 buffer_insert_emacs_char (buf, c1);
422 /* If previous command specified a face to use, use it. */
423 if (!NILP (Vself_insert_face)
424 && EQ (Vlast_command, Vself_insert_face_command))
426 Lisp_Object before = make_int (BUF_PT (buf) - 1);
427 Lisp_Object after = make_int (BUF_PT (buf));
428 Fput_text_property (before, after, Qface, Vself_insert_face, Qnil);
429 Fput_text_property (before, after, Qstart_open, Qt, Qnil);
430 Fput_text_property (before, after, Qend_open, Qnil, Qnil);
431 /* #### FSFmacs properties are normally closed ("sticky") on the
432 end but not the beginning. It's the opposite for us. */
433 Vself_insert_face = Qnil;
435 synt = SYNTAX (syntax_table, c1);
436 if ((synt == Sclose || synt == Smath)
437 && !NILP (Vblink_paren_function) && INTERACTIVE
440 call0 (Vblink_paren_function);
447 /* (this comes from Mule but is a generally good idea) */
449 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
450 Invoke `self-insert-command' as if CH is entered from keyboard.
454 /* This function can GC */
455 CHECK_CHAR_COERCE_INT (ch);
456 internal_self_insert (XCHAR (ch), 0);
460 /* module initialization */
465 defsymbol (&Qkill_forward_chars, "kill-forward-chars");
466 defsymbol (&Qself_insert_command, "self-insert-command");
467 defsymbol (&Qoverwrite_mode_binary, "overwrite-mode-binary");
468 defsymbol (&Qno_self_insert, "no-self-insert");
470 DEFSUBR (Fforward_char);
471 DEFSUBR (Fbackward_char);
472 DEFSUBR (Fforward_line);
473 DEFSUBR (Fbeginning_of_line);
474 DEFSUBR (Fend_of_line);
476 DEFSUBR (Fpoint_at_bol);
477 DEFSUBR (Fpoint_at_eol);
479 DEFSUBR (Fdelete_char);
480 DEFSUBR (Fdelete_backward_char);
482 DEFSUBR (Fself_insert_command);
483 DEFSUBR (Fself_insert_internal);
489 DEFVAR_LISP ("self-insert-face", &Vself_insert_face /*
490 If non-nil, set the face of the next self-inserting character to this.
491 See also `self-insert-face-command'.
493 Vself_insert_face = Qnil;
495 DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command /*
496 This is the command that set up `self-insert-face'.
497 If `last-command' does not equal this value, we ignore `self-insert-face'.
499 Vself_insert_face_command = Qnil;
501 DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function /*
502 Function called, if non-nil, whenever a close parenthesis is inserted.
503 More precisely, a char with closeparen syntax is self-inserted.
505 Vblink_paren_function = Qnil;