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 N characters (left if N 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);
64 /* This used to just set point to point + XINT (n), 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. */
70 Bufpos new_point = BUF_PT (buf) + count;
72 if (new_point < BUF_BEGV (buf))
74 BUF_SET_PT (buf, BUF_BEGV (buf));
75 Fsignal (Qbeginning_of_buffer, Qnil);
78 if (new_point > BUF_ZV (buf))
80 BUF_SET_PT (buf, BUF_ZV (buf));
81 Fsignal (Qend_of_buffer, Qnil);
85 BUF_SET_PT (buf, new_point);
91 DEFUN ("backward-char", Fbackward_char, 0, 2, "_p", /*
92 Move point left N characters (right if N negative).
93 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
94 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
103 XSETINT (n, - XINT (n));
105 return Fforward_char (n, buffer);
108 DEFUN ("forward-line", Fforward_line, 0, 2, "_p", /*
109 Move N lines forward (backward if N is negative).
110 Precisely, if point is on line I, move to the start of line I + N.
111 If there isn't room, go as far as possible (no error).
112 Returns the count of lines left to move. If moving forward,
113 that is N - number of lines moved; if backward, N + number moved.
114 With positive N, a non-empty line at the end counts as one line
115 successfully moved (for the return value).
116 If BUFFER is nil, the current buffer is assumed.
120 struct buffer *buf = decode_buffer (buffer, 1);
121 Bufpos pos2 = BUF_PT (buf);
123 EMACS_INT count, shortage, negp;
134 pos = scan_buffer (buf, '\n', pos2, 0, count - negp, &shortage, 1);
137 || (BUF_ZV (buf) > BUF_BEGV (buf)
139 && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
141 BUF_SET_PT (buf, pos);
142 return make_int (negp ? - shortage : shortage);
145 DEFUN ("point-at-bol", Fpoint_at_bol, 0, 2, 0, /*
146 Return the character position of the first character on the current line.
147 With argument N not nil or 1, move forward N - 1 lines first.
148 If scan reaches end of buffer, return that position.
149 This function does not move point.
153 struct buffer *b = decode_buffer (buffer, 1);
154 REGISTER int orig, end;
156 XSETBUFFER (buffer, b);
162 n = make_int (XINT (n) - 1);
166 Fforward_line (n, buffer);
168 BUF_SET_PT (b, orig);
170 return make_int (end);
173 DEFUN ("beginning-of-line", Fbeginning_of_line, 0, 2, "_p", /*
174 Move point to beginning of current line.
175 With argument N not nil or 1, move forward N - 1 lines first.
176 If scan reaches end of buffer, stop there without error.
177 If BUFFER is nil, the current buffer is assumed.
181 struct buffer *b = decode_buffer (buffer, 1);
183 BUF_SET_PT (b, XINT (Fpoint_at_bol (n, buffer)));
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.
195 struct buffer *buf = decode_buffer (buffer, 1);
206 return make_int (find_before_next_newline (buf, BUF_PT (buf), 0,
207 count - (count <= 0)));
210 DEFUN ("end-of-line", Fend_of_line, 0, 2, "_p", /*
211 Move point to end of current line.
212 With argument N not nil or 1, move forward N - 1 lines first.
213 If scan reaches end of buffer, stop there without error.
214 If BUFFER is nil, the current buffer is assumed.
218 struct buffer *b = decode_buffer (buffer, 1);
220 BUF_SET_PT (b, XINT (Fpoint_at_eol (n, buffer)));
224 DEFUN ("delete-char", Fdelete_char, 1, 2, "*p\nP", /*
225 Delete the following N characters (previous, with negative N).
226 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).
227 Interactively, N is the prefix arg, and KILLFLAG is set if
228 N was explicitly specified.
232 /* This function can GC */
234 struct buffer *buf = current_buffer;
240 pos = BUF_PT (buf) + count;
245 if (pos < BUF_BEGV (buf))
246 signal_error (Qbeginning_of_buffer, Qnil);
248 buffer_delete_range (buf, pos, BUF_PT (buf), 0);
252 if (pos > BUF_ZV (buf))
253 signal_error (Qend_of_buffer, Qnil);
255 buffer_delete_range (buf, BUF_PT (buf), pos, 0);
260 call1 (Qkill_forward_chars, n);
265 DEFUN ("delete-backward-char", Fdelete_backward_char, 1, 2, "*p\nP", /*
266 Delete the previous N characters (following, with negative N).
267 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).
268 Interactively, N is the prefix arg, and KILLFLAG is set if
269 N was explicitly specified.
273 /* This function can GC */
275 return Fdelete_char (make_int (- XINT (n)), killflag);
278 static void internal_self_insert (Emchar ch, int noautofill);
280 DEFUN ("self-insert-command", Fself_insert_command, 1, 1, "*p", /*
281 Insert the character you type.
282 Whichever character you type to run this command is inserted.
286 /* This function can GC */
294 if (CHAR_OR_CHAR_INTP (Vlast_command_char))
295 c = Vlast_command_char;
297 c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
300 signal_simple_error ("Last typed character has no ASCII equivalent",
301 Fcopy_event (Vlast_command_event, Qnil));
303 CHECK_CHAR_COERCE_INT (c);
308 internal_self_insert (ch, (count != 0));
313 /* Insert character C1. If NOAUTOFILL is nonzero, don't do autofill
314 even if it is enabled.
318 If this insertion is suitable for direct output (completely simple),
319 return 0. A value of 1 indicates this *might* not have been simple.
320 A value of 2 means this did things that call for an undo boundary. */
323 internal_self_insert (Emchar c1, int noautofill)
325 /* This function can GC */
326 /* int hairy = 0; -- unused */
327 REGISTER enum syntaxcode synt;
329 Lisp_Object overwrite;
330 struct Lisp_Char_Table *syntax_table;
331 struct buffer *buf = current_buffer;
334 overwrite = buf->overwrite_mode;
335 syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
338 /* No, this is very bad, it makes undo *always* undo a character at a time
339 instead of grouping consecutive self-inserts together. Nasty nasty.
341 if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
342 || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
346 if (!NILP (overwrite)
347 && BUF_PT (buf) < BUF_ZV (buf)
348 && (EQ (overwrite, Qoverwrite_mode_binary)
349 || (c1 != '\n' && BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\n'))
350 && (EQ (overwrite, Qoverwrite_mode_binary)
351 || BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\t'
352 || ((tab_width = XINT (buf->tab_width), tab_width <= 0)
354 || !((current_column (buf) + 1) % tab_width))))
356 buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
360 if (!NILP (buf->abbrev_mode)
361 && !WORD_SYNTAX_P (syntax_table, c1)
362 && NILP (buf->read_only)
363 && BUF_PT (buf) > BUF_BEGV (buf))
365 c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
367 if (WORD_SYNTAX_P (syntax_table, c2))
372 Lisp_Object sym = Fexpand_abbrev ();
374 /* I think this is too bogus to add. The function should
375 have a way of examining the character to be inserted, so
376 it can decide whether to insert it or not. We should
377 design it better than that. */
379 /* Here FSFmacs remembers MODIFF, compares it after
380 Fexpand_abbrev() finishes, and updates HAIRY. */
382 /* NOTE: we cannot simply check for Vlast_abbrev, because
383 Fexpand_abbrev() can bail out before setting it to
384 anything meaningful, leaving us stuck with an old value.
385 Thus Fexpand_abbrev() was extended to return the actual
388 && !NILP (symbol_function (XSYMBOL (sym)))
389 && SYMBOLP (symbol_function (XSYMBOL (sym))))
391 Lisp_Object prop = Fget (symbol_function (XSYMBOL (sym)),
392 Qno_self_insert, Qnil);
399 if ((c1 == ' ' || c1 == '\n')
401 && !NILP (buf->auto_fill_function))
403 buffer_insert_emacs_char (buf, c1);
405 /* After inserting a newline, move to previous line and fill */
406 /* that. Must have the newline in place already so filling and */
407 /* justification, if any, know where the end is going to be. */
408 BUF_SET_PT (buf, BUF_PT (buf) - 1);
409 call0 (buf->auto_fill_function);
411 BUF_SET_PT (buf, BUF_PT (buf) + 1);
415 buffer_insert_emacs_char (buf, c1);
417 /* If previous command specified a face to use, use it. */
418 if (!NILP (Vself_insert_face)
419 && EQ (Vlast_command, Vself_insert_face_command))
421 Lisp_Object before = make_int (BUF_PT (buf) - 1);
422 Lisp_Object after = make_int (BUF_PT (buf));
423 Fput_text_property (before, after, Qface, Vself_insert_face, Qnil);
424 Fput_text_property (before, after, Qstart_open, Qt, Qnil);
425 Fput_text_property (before, after, Qend_open, Qnil, Qnil);
426 /* #### FSFmacs properties are normally closed ("sticky") on the
427 end but not the beginning. It's the opposite for us. */
428 Vself_insert_face = Qnil;
430 synt = SYNTAX (syntax_table, c1);
431 if ((synt == Sclose || synt == Smath)
432 && !NILP (Vblink_paren_function) && INTERACTIVE
435 call0 (Vblink_paren_function);
442 /* (this comes from Mule but is a generally good idea) */
444 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
445 Invoke `self-insert-command' as if CH is entered from keyboard.
449 /* This function can GC */
450 CHECK_CHAR_COERCE_INT (ch);
451 internal_self_insert (XCHAR (ch), 0);
455 /* module initialization */
460 defsymbol (&Qkill_forward_chars, "kill-forward-chars");
461 defsymbol (&Qself_insert_command, "self-insert-command");
462 defsymbol (&Qoverwrite_mode_binary, "overwrite-mode-binary");
463 defsymbol (&Qno_self_insert, "no-self-insert");
465 DEFSUBR (Fforward_char);
466 DEFSUBR (Fbackward_char);
467 DEFSUBR (Fforward_line);
468 DEFSUBR (Fbeginning_of_line);
469 DEFSUBR (Fend_of_line);
471 DEFSUBR (Fpoint_at_bol);
472 DEFSUBR (Fpoint_at_eol);
474 DEFSUBR (Fdelete_char);
475 DEFSUBR (Fdelete_backward_char);
477 DEFSUBR (Fself_insert_command);
478 DEFSUBR (Fself_insert_internal);
484 DEFVAR_LISP ("self-insert-face", &Vself_insert_face /*
485 If non-nil, set the face of the next self-inserting character to this.
486 See also `self-insert-face-command'.
488 Vself_insert_face = Qnil;
490 DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command /*
491 This is the command that set up `self-insert-face'.
492 If `last-command' does not equal this value, we ignore `self-insert-face'.
494 Vself_insert_face_command = Qnil;
496 DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function /*
497 Function called, if non-nil, whenever a close parenthesis is inserted.
498 More precisely, a char with closeparen syntax is self-inserted.
500 Vblink_paren_function = Qnil;