XEmacs 21.2.28 "Hermes".
[chise/xemacs-chise.git.1] / src / cmds.c
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;
32 Lisp_Object Qno_self_insert;
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 \f
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.
50 */
51        (n, buffer))
52 {
53   struct buffer *buf = decode_buffer (buffer, 1);
54   EMACS_INT count;
55
56   if (NILP (n))
57     count = 1;
58   else
59     {
60       CHECK_INT (n);
61       count = XINT (n);
62     }
63
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.  */
69   {
70     Bufpos new_point = BUF_PT (buf) + count;
71
72     if (new_point < BUF_BEGV (buf))
73       {
74         BUF_SET_PT (buf, BUF_BEGV (buf));
75         Fsignal (Qbeginning_of_buffer, Qnil);
76         return Qnil;
77       }
78     if (new_point > BUF_ZV (buf))
79       {
80         BUF_SET_PT (buf, BUF_ZV (buf));
81         Fsignal (Qend_of_buffer, Qnil);
82         return Qnil;
83       }
84
85     BUF_SET_PT (buf, new_point);
86   }
87
88   return Qnil;
89 }
90
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'.
95 */
96        (n, buffer))
97 {
98   if (NILP (n))
99     n = make_int (-1);
100   else
101     {
102       CHECK_INT (n);
103       XSETINT (n, - XINT (n));
104     }
105   return Fforward_char (n, buffer);
106 }
107
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.
117 */
118        (n, buffer))
119 {
120   struct buffer *buf = decode_buffer (buffer, 1);
121   Bufpos pos2 = BUF_PT (buf);
122   Bufpos pos;
123   EMACS_INT count, shortage, negp;
124
125   if (NILP (n))
126     count = 1;
127   else
128     {
129       CHECK_INT (n);
130       count = XINT (n);
131     }
132
133   negp = count <= 0;
134   pos = scan_buffer (buf, '\n', pos2, 0, count - negp, &shortage, 1);
135   if (shortage > 0
136       && (negp
137           || (BUF_ZV (buf) > BUF_BEGV (buf)
138               && pos != pos2
139               && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
140     shortage--;
141   BUF_SET_PT (buf, pos);
142   return make_int (negp ? - shortage : shortage);
143 }
144
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.
150 */
151        (n, buffer))
152 {
153   struct buffer *b = decode_buffer (buffer, 1);
154   REGISTER int orig, end;
155
156   XSETBUFFER (buffer, b);
157   if (NILP (n))
158     n = make_int (0);
159   else
160     {
161       CHECK_INT (n);
162       n = make_int (XINT (n) - 1);
163     }
164
165   orig = BUF_PT (b);
166   Fforward_line (n, buffer);
167   end = BUF_PT (b);
168   BUF_SET_PT (b, orig);
169
170   return make_int (end);
171 }
172
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.
178 */
179        (n, buffer))
180 {
181   struct buffer *b = decode_buffer (buffer, 1);
182
183   BUF_SET_PT (b, XINT (Fpoint_at_bol (n, buffer)));
184   return Qnil;
185 }
186
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.
192 */
193        (n, buffer))
194 {
195   struct buffer *buf = decode_buffer (buffer, 1);
196   int count;
197
198   if (NILP (n))
199     count = 1;
200   else
201     {
202       CHECK_INT (n);
203       count = XINT (n);
204     }
205
206   return make_int (find_before_next_newline (buf, BUF_PT (buf), 0,
207                                              count - (count <= 0)));
208 }
209
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.
215 */
216        (n, buffer))
217 {
218   struct buffer *b = decode_buffer (buffer, 1);
219
220   BUF_SET_PT (b, XINT (Fpoint_at_eol (n, buffer)));
221   return Qnil;
222 }
223
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.
229 */
230        (n, killflag))
231 {
232   /* This function can GC */
233   Bufpos pos;
234   struct buffer *buf = current_buffer;
235   int count;
236
237   CHECK_INT (n);
238   count = XINT (n);
239
240   pos = BUF_PT (buf) + count;
241   if (NILP (killflag))
242     {
243       if (count < 0)
244         {
245           if (pos < BUF_BEGV (buf))
246             signal_error (Qbeginning_of_buffer, Qnil);
247           else
248             buffer_delete_range (buf, pos, BUF_PT (buf), 0);
249         }
250       else
251         {
252           if (pos > BUF_ZV (buf))
253             signal_error (Qend_of_buffer, Qnil);
254           else
255             buffer_delete_range (buf, BUF_PT (buf), pos, 0);
256         }
257     }
258   else
259     {
260       call1 (Qkill_forward_chars, n);
261     }
262   return Qnil;
263 }
264
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.
270 */
271        (n, killflag))
272 {
273   /* This function can GC */
274   CHECK_INT (n);
275   return Fdelete_char (make_int (- XINT (n)), killflag);
276 }
277
278 static void internal_self_insert (Emchar ch, int noautofill);
279
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.
283 */
284        (n))
285 {
286   /* This function can GC */
287   Emchar ch;
288   Lisp_Object c;
289   int count;
290
291   CHECK_NATNUM (n);
292   count = XINT (n);
293
294   if (CHAR_OR_CHAR_INTP (Vlast_command_char))
295     c = Vlast_command_char;
296   else
297     c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
298
299   if (NILP (c))
300     signal_simple_error ("Last typed character has no ASCII equivalent",
301                          Fcopy_event (Vlast_command_event, Qnil));
302
303   CHECK_CHAR_COERCE_INT (c);
304
305   ch = XCHAR (c);
306
307   while (count--)
308     internal_self_insert (ch, (count != 0));
309
310   return Qnil;
311 }
312
313 /* Insert character C1.  If NOAUTOFILL is nonzero, don't do autofill
314    even if it is enabled.
315
316    FSF:
317
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.  */
321
322 static void
323 internal_self_insert (Emchar c1, int noautofill)
324 {
325   /* This function can GC */
326   /* int hairy = 0; -- unused */
327   REGISTER enum syntaxcode synt;
328   REGISTER Emchar c2;
329   Lisp_Object overwrite;
330   Lisp_Char_Table *syntax_table;
331   struct buffer *buf = current_buffer;
332   int tab_width;
333
334   overwrite = buf->overwrite_mode;
335   syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
336
337 #if 0
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.
340    */
341   if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
342       || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
343     hairy = 1;
344 #endif
345
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)
353           || tab_width > 20
354           || !((current_column (buf) + 1) % tab_width))))
355     {
356       buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
357       /* hairy = 2; */
358     }
359
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))
364     {
365       c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
366
367       if (WORD_SYNTAX_P (syntax_table, c2))
368         {
369 #if 1
370           Fexpand_abbrev ();
371 #else  /* FSFmacs */
372           Lisp_Object sym = Fexpand_abbrev ();
373
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.  */
378
379           /* Here FSFmacs remembers MODIFF, compares it after
380              Fexpand_abbrev() finishes, and updates HAIRY.  */
381
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
386              abbrev symbol.  */
387           if (!NILP (sym)
388               && !NILP (symbol_function (XSYMBOL (sym)))
389               && SYMBOLP (symbol_function (XSYMBOL (sym))))
390             {
391               Lisp_Object prop = Fget (symbol_function (XSYMBOL (sym)),
392                                        Qno_self_insert, Qnil);
393               if (!NILP (prop))
394                 return;
395             }
396 #endif /* FSFmacs */
397         }
398     }
399   if ((c1 == ' ' || c1 == '\n')
400       && !noautofill
401       && !NILP (buf->auto_fill_function))
402     {
403       buffer_insert_emacs_char (buf, c1);
404       if (c1 == '\n')
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);
410       if (c1 == '\n')
411         BUF_SET_PT (buf, BUF_PT (buf) + 1);
412       /* hairy = 2; */
413     }
414   else
415     buffer_insert_emacs_char (buf, c1);
416
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))
420     {
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;
429     }
430   synt = SYNTAX (syntax_table, c1);
431   if ((synt == Sclose || synt == Smath)
432       && !NILP (Vblink_paren_function) && INTERACTIVE
433       && !noautofill)
434     {
435       call0 (Vblink_paren_function);
436       /* hairy = 2; */
437     }
438
439   /* return hairy; */
440 }
441
442 /* (this comes from Mule but is a generally good idea) */
443
444 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
445 Invoke `self-insert-command' as if CH is entered from keyboard.
446 */
447        (ch))
448 {
449   /* This function can GC */
450   CHECK_CHAR_COERCE_INT (ch);
451   internal_self_insert (XCHAR (ch), 0);
452   return Qnil;
453 }
454 \f
455 /* module initialization */
456
457 void
458 syms_of_cmds (void)
459 {
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");
464
465   DEFSUBR (Fforward_char);
466   DEFSUBR (Fbackward_char);
467   DEFSUBR (Fforward_line);
468   DEFSUBR (Fbeginning_of_line);
469   DEFSUBR (Fend_of_line);
470
471   DEFSUBR (Fpoint_at_bol);
472   DEFSUBR (Fpoint_at_eol);
473
474   DEFSUBR (Fdelete_char);
475   DEFSUBR (Fdelete_backward_char);
476
477   DEFSUBR (Fself_insert_command);
478   DEFSUBR (Fself_insert_internal);
479 }
480
481 void
482 vars_of_cmds (void)
483 {
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'.
487 */ );
488   Vself_insert_face = Qnil;
489
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'.
493 */ );
494   Vself_insert_face_command = Qnil;
495
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.
499 */ );
500   Vblink_paren_function = Qnil;
501 }