This commit was generated by cvs2svn to compensate for changes in r5670,
[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 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.
50 */
51        (arg, buffer))
52 {
53   struct buffer *buf = decode_buffer (buffer, 1);
54
55   if (NILP (arg))
56     arg = make_int (1);
57   else
58     CHECK_INT (arg);
59
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.  */
65   {
66     Bufpos new_point = BUF_PT (buf) + XINT (arg);
67
68     if (new_point < BUF_BEGV (buf))
69       {
70         BUF_SET_PT (buf, BUF_BEGV (buf));
71         Fsignal (Qbeginning_of_buffer, Qnil);
72         return Qnil;
73       }
74     if (new_point > BUF_ZV (buf))
75       {
76         BUF_SET_PT (buf, BUF_ZV (buf));
77         Fsignal (Qend_of_buffer, Qnil);
78         return Qnil;
79       }
80
81     BUF_SET_PT (buf, new_point);
82   }
83
84   return Qnil;
85 }
86
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'.
91 */
92        (arg, buffer))
93 {
94   if (NILP (arg))
95     arg = make_int (1);
96   else
97     CHECK_INT (arg);
98
99   XSETINT (arg, - XINT (arg));
100   return Fforward_char (arg, buffer);
101 }
102
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.
112 */
113        (arg, buffer))
114 {
115   struct buffer *buf = decode_buffer (buffer, 1);
116   Bufpos pos2 = BUF_PT (buf);
117   Bufpos pos;
118   EMACS_INT count, shortage, negp;
119
120   if (NILP (arg))
121     count = 1;
122   else
123     {
124       CHECK_INT (arg);
125       count = XINT (arg);
126     }
127
128   negp = count <= 0;
129   pos = scan_buffer (buf, '\n', pos2, 0, count - negp, &shortage, 1);
130   if (shortage > 0
131       && (negp
132           || (BUF_ZV (buf) > BUF_BEGV (buf)
133               && pos != pos2
134               && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
135     shortage--;
136   BUF_SET_PT (buf, pos);
137   return make_int (negp ? - shortage : shortage);
138 }
139
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.
145 */
146        (arg, buffer))
147 {
148   struct buffer *b = decode_buffer (buffer, 1);
149   REGISTER int orig, end;
150
151   XSETBUFFER (buffer, b);
152   if (NILP (arg))
153     arg = make_int (1);
154   else
155     CHECK_INT (arg);
156
157   orig = BUF_PT(b);
158   Fforward_line (make_int (XINT (arg) - 1), buffer);
159   end = BUF_PT(b);
160   BUF_SET_PT(b, orig);
161
162   return make_int (end);
163 }
164
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.
170 */
171        (arg, buffer))
172 {
173   struct buffer *b = decode_buffer (buffer, 1);
174
175   BUF_SET_PT(b, XINT (Fpoint_at_bol(arg, buffer)));
176   return Qnil;
177 }
178
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.
184 */
185        (arg, buffer))
186 {
187   struct buffer *buf = decode_buffer (buffer, 1);
188
189   XSETBUFFER (buffer, buf);
190
191   if (NILP (arg))
192     arg = make_int (1);
193   else
194     CHECK_INT (arg);
195
196   return make_int (find_before_next_newline (buf, BUF_PT (buf), 0,
197                                              XINT (arg) - (XINT (arg) <= 0)));
198 }
199
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.
205 */
206        (arg, buffer))
207 {
208   struct buffer *b = decode_buffer (buffer, 1);
209
210   BUF_SET_PT(b, XINT (Fpoint_at_eol (arg, buffer)));
211   return Qnil;
212 }
213
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.
219 */
220        (arg, killflag))
221 {
222   /* This function can GC */
223   Bufpos pos;
224   struct buffer *buf = current_buffer;
225
226   CHECK_INT (arg);
227
228   pos = BUF_PT (buf) + XINT (arg);
229   if (NILP (killflag))
230     {
231       if (XINT (arg) < 0)
232         {
233           if (pos < BUF_BEGV (buf))
234             signal_error (Qbeginning_of_buffer, Qnil);
235           else
236             buffer_delete_range (buf, pos, BUF_PT (buf), 0);
237         }
238       else
239         {
240           if (pos > BUF_ZV (buf))
241             signal_error (Qend_of_buffer, Qnil);
242           else
243             buffer_delete_range (buf, BUF_PT (buf), pos, 0);
244         }
245     }
246   else
247     {
248       call1 (Qkill_forward_chars, arg);
249     }
250   return Qnil;
251 }
252
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.
258 */
259        (arg, killflag))
260 {
261   /* This function can GC */
262   CHECK_INT (arg);
263   return Fdelete_char (make_int (-XINT (arg)), killflag);
264 }
265
266 static void internal_self_insert (Emchar ch, int noautofill);
267
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.
271 */
272        (arg))
273 {
274   /* This function can GC */
275   int n;
276   Emchar ch;
277   Lisp_Object c;
278   CHECK_INT (arg);
279
280   if (CHAR_OR_CHAR_INTP (Vlast_command_char))
281     c = Vlast_command_char;
282   else
283     c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
284
285   if (NILP (c))
286     signal_simple_error ("last typed character has no ASCII equivalent",
287                          Fcopy_event (Vlast_command_event, Qnil));
288
289   CHECK_CHAR_COERCE_INT (c);
290
291   n = XINT (arg);
292   ch = XCHAR (c);
293 #if 0 /* FSFmacs */
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))
298     {
299       n -= 2;
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);
308     }
309   else
310 #endif /* 0 */
311     while (n > 0)
312       {
313         n--;
314         internal_self_insert (ch, (n != 0));
315       }
316   return Qnil;
317 }
318
319 /* Insert character C1.  If NOAUTOFILL is nonzero, don't do autofill
320    even if it is enabled.
321
322    FSF:
323
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.  */
327
328 static void
329 internal_self_insert (Emchar c1, int noautofill)
330 {
331   /* This function can GC */
332   /* int hairy = 0; -- unused */
333   REGISTER enum syntaxcode synt;
334   REGISTER Emchar c2;
335   Lisp_Object overwrite;
336   struct Lisp_Char_Table *syntax_table;
337   struct buffer *buf = current_buffer;
338
339   overwrite = buf->overwrite_mode;
340   syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
341
342 #if 0
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.
345    */
346   if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
347       || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
348     hairy = 1;
349 #endif
350
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))))
360     {
361       buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
362       /* hairy = 2; */
363     }
364
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))
369     {
370       c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
371
372       if (WORD_SYNTAX_P (syntax_table, c2))
373         {
374 #if 1
375           Fexpand_abbrev ();
376 #else  /* FSFmacs */
377           Lisp_Object sym = Fexpand_abbrev ();
378
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.  */
383
384           /* Here FSFmacs remembers MODIFF, compares it after
385              Fexpand_abbrev() finishes, and updates HAIRY.  */
386
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
391              abbrev symbol.  */
392           if (!NILP (sym)
393               && !NILP (symbol_function (XSYMBOL (sym)))
394               && SYMBOLP (symbol_function (XSYMBOL (sym))))
395             {
396               Lisp_Object prop = Fget (symbol_function (XSYMBOL (sym)),
397                                        Qno_self_insert, Qnil);
398               if (!NILP (prop))
399                 return;
400             }
401 #endif /* FSFmacs */
402         }
403     }
404   if ((c1 == ' ' || c1 == '\n')
405       && !noautofill
406       && !NILP (buf->auto_fill_function))
407     {
408       buffer_insert_emacs_char (buf, c1);
409       if (c1 == '\n')
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);
415       if (c1 == '\n')
416         BUF_SET_PT (buf, BUF_PT (buf) + 1);
417       /* hairy = 2; */
418     }
419   else
420     buffer_insert_emacs_char (buf, c1);
421
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))
425     {
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;
434     }
435   synt = SYNTAX (syntax_table, c1);
436   if ((synt == Sclose || synt == Smath)
437       && !NILP (Vblink_paren_function) && INTERACTIVE
438       && !noautofill)
439     {
440       call0 (Vblink_paren_function);
441       /* hairy = 2; */
442     }
443
444   /* return hairy; */
445 }
446
447 /* (this comes from Mule but is a generally good idea) */
448
449 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
450 Invoke `self-insert-command' as if CH is entered from keyboard.
451 */
452        (ch))
453 {
454   /* This function can GC */
455   CHECK_CHAR_COERCE_INT (ch);
456   internal_self_insert (XCHAR (ch), 0);
457   return Qnil;
458 }
459 \f
460 /* module initialization */
461
462 void
463 syms_of_cmds (void)
464 {
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");
469
470   DEFSUBR (Fforward_char);
471   DEFSUBR (Fbackward_char);
472   DEFSUBR (Fforward_line);
473   DEFSUBR (Fbeginning_of_line);
474   DEFSUBR (Fend_of_line);
475
476   DEFSUBR (Fpoint_at_bol);
477   DEFSUBR (Fpoint_at_eol);
478
479   DEFSUBR (Fdelete_char);
480   DEFSUBR (Fdelete_backward_char);
481
482   DEFSUBR (Fself_insert_command);
483   DEFSUBR (Fself_insert_internal);
484 }
485
486 void
487 vars_of_cmds (void)
488 {
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'.
492 */ );
493   Vself_insert_face = Qnil;
494
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'.
498 */ );
499   Vself_insert_face_command = Qnil;
500
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.
504 */ );
505   Vblink_paren_function = Qnil;
506 }