import xemacs-21.2.37
[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
45 /* A char-table for characters which may invoke auto-filling.  */
46 Lisp_Object Vauto_fill_chars;
47 \f
48 DEFUN ("forward-char", Fforward_char, 0, 2, "_p", /*
49 Move point right COUNT characters (left if COUNT is negative).
50 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
51 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
52 On reaching end of buffer, stop and signal error.
53 */
54        (count, buffer))
55 {
56   struct buffer *buf = decode_buffer (buffer, 1);
57   EMACS_INT n;
58
59   if (NILP (count))
60     n = 1;
61   else
62     {
63       CHECK_INT (count);
64       n = XINT (count);
65     }
66
67   /* This used to just set point to point + XINT (count), and then check
68      to see if it was within boundaries.  But now that SET_PT can
69      potentially do a lot of stuff (calling entering and exiting
70      hooks, etcetera), that's not a good approach.  So we validate the
71      proposed position, then set point.  */
72   {
73     Bufpos new_point = BUF_PT (buf) + n;
74
75     if (new_point < BUF_BEGV (buf))
76       {
77         BUF_SET_PT (buf, BUF_BEGV (buf));
78         Fsignal (Qbeginning_of_buffer, Qnil);
79         return Qnil;
80       }
81     if (new_point > BUF_ZV (buf))
82       {
83         BUF_SET_PT (buf, BUF_ZV (buf));
84         Fsignal (Qend_of_buffer, Qnil);
85         return Qnil;
86       }
87
88     BUF_SET_PT (buf, new_point);
89   }
90
91   return Qnil;
92 }
93
94 DEFUN ("backward-char", Fbackward_char, 0, 2, "_p", /*
95 Move point left COUNT characters (right if COUNT is negative).
96 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
97 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
98 */
99        (count, buffer))
100 {
101   if (NILP (count))
102     count = make_int (-1);
103   else
104     {
105       CHECK_INT (count);
106       count = make_int (- XINT (count));
107     }
108   return Fforward_char (count, buffer);
109 }
110
111 DEFUN ("forward-line", Fforward_line, 0, 2, "_p", /*
112 Move COUNT lines forward (backward if COUNT is negative).
113 Precisely, if point is on line I, move to the start of line I + COUNT.
114 If there isn't room, go as far as possible (no error).
115 Returns the count of lines left to move.  If moving forward,
116 that is COUNT - number of lines moved; if backward, COUNT + number moved.
117 With positive COUNT, a non-empty line at the end counts as one line
118   successfully moved (for the return value).
119 If BUFFER is nil, the current buffer is assumed.
120 */
121        (count, buffer))
122 {
123   struct buffer *buf = decode_buffer (buffer, 1);
124   Bufpos pos2 = BUF_PT (buf);
125   Bufpos pos;
126   EMACS_INT n, shortage, negp;
127
128   if (NILP (count))
129     n = 1;
130   else
131     {
132       CHECK_INT (count);
133       n = XINT (count);
134     }
135
136   negp = n <= 0;
137   pos = scan_buffer (buf, '\n', pos2, 0, n - negp, &shortage, 1);
138   if (shortage > 0
139       && (negp
140           || (BUF_ZV (buf) > BUF_BEGV (buf)
141               && pos != pos2
142               && BUF_FETCH_CHAR (buf, pos - 1) != '\n')))
143     shortage--;
144   BUF_SET_PT (buf, pos);
145   return make_int (negp ? - shortage : shortage);
146 }
147
148 DEFUN ("point-at-bol", Fpoint_at_bol, 0, 2, 0, /*
149 Return the character position of the first character on the current line.
150 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
151 If scan reaches end of buffer, return that position.
152 This function does not move point.
153 */
154        (count, buffer))
155 {
156   struct buffer *b = decode_buffer (buffer, 1);
157   REGISTER int orig, end;
158
159   XSETBUFFER (buffer, b);
160   if (NILP (count))
161     count = make_int (0);
162   else
163     {
164       CHECK_INT (count);
165       count = make_int (XINT (count) - 1);
166     }
167
168   orig = BUF_PT (b);
169   Fforward_line (count, buffer);
170   end = BUF_PT (b);
171   BUF_SET_PT (b, orig);
172
173   return make_int (end);
174 }
175
176 DEFUN ("beginning-of-line", Fbeginning_of_line, 0, 2, "_p", /*
177 Move point to beginning of current line.
178 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
179 If scan reaches end of buffer, stop there without error.
180 If BUFFER is nil, the current buffer is assumed.
181 */
182        (count, buffer))
183 {
184   struct buffer *b = decode_buffer (buffer, 1);
185
186   BUF_SET_PT (b, XINT (Fpoint_at_bol (count, buffer)));
187   return Qnil;
188 }
189
190 DEFUN ("point-at-eol", Fpoint_at_eol, 0, 2, 0, /*
191 Return the character position of the last character on the current line.
192 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
193 If scan reaches end of buffer, return that position.
194 This function does not move point.
195 */
196        (count, buffer))
197 {
198   struct buffer *buf = decode_buffer (buffer, 1);
199   int n;
200
201   if (NILP (count))
202     n = 1;
203   else
204     {
205       CHECK_INT (count);
206       n = XINT (count);
207     }
208
209   return make_int (find_before_next_newline (buf, BUF_PT (buf), 0,
210                                              n - (n <= 0)));
211 }
212
213 DEFUN ("end-of-line", Fend_of_line, 0, 2, "_p", /*
214 Move point to end of current line.
215 With argument COUNT not nil or 1, move forward COUNT - 1 lines first.
216 If scan reaches end of buffer, stop there without error.
217 If BUFFER is nil, the current buffer is assumed.
218 */
219        (count, buffer))
220 {
221   struct buffer *b = decode_buffer (buffer, 1);
222
223   BUF_SET_PT (b, XINT (Fpoint_at_eol (count, buffer)));
224   return Qnil;
225 }
226
227 DEFUN ("delete-char", Fdelete_char, 1, 2, "*p\nP", /*
228 Delete the following COUNT characters (previous, with negative COUNT).
229 Optional second arg KILLP non-nil means kill instead (save in kill ring).
230 Interactively, COUNT is the prefix arg, and KILLP is set if
231 COUNT was explicitly specified.
232 */
233        (count, killp))
234 {
235   /* This function can GC */
236   Bufpos pos;
237   struct buffer *buf = current_buffer;
238   int n;
239
240   CHECK_INT (count);
241   n = XINT (count);
242
243   pos = BUF_PT (buf) + n;
244   if (NILP (killp))
245     {
246       if (n < 0)
247         {
248           if (pos < BUF_BEGV (buf))
249             signal_error (Qbeginning_of_buffer, Qnil);
250           else
251             buffer_delete_range (buf, pos, BUF_PT (buf), 0);
252         }
253       else
254         {
255           if (pos > BUF_ZV (buf))
256             signal_error (Qend_of_buffer, Qnil);
257           else
258             buffer_delete_range (buf, BUF_PT (buf), pos, 0);
259         }
260     }
261   else
262     {
263       call1 (Qkill_forward_chars, count);
264     }
265   return Qnil;
266 }
267
268 DEFUN ("delete-backward-char", Fdelete_backward_char, 1, 2, "*p\nP", /*
269 Delete the previous COUNT characters (following, with negative COUNT).
270 Optional second arg KILLP non-nil means kill instead (save in kill ring).
271 Interactively, COUNT is the prefix arg, and KILLP is set if
272 COUNT was explicitly specified.
273 */
274        (count, killp))
275 {
276   /* This function can GC */
277   CHECK_INT (count);
278   return Fdelete_char (make_int (- XINT (count)), killp);
279 }
280
281 static void internal_self_insert (Emchar ch, int noautofill);
282
283 DEFUN ("self-insert-command", Fself_insert_command, 1, 1, "*p", /*
284 Insert the character you type.
285 Whichever character you type to run this command is inserted.
286 If a prefix arg COUNT is specified, the character is inserted COUNT times.
287 */
288        (count))
289 {
290   /* This function can GC */
291   Emchar ch;
292   Lisp_Object c;
293   int n;
294
295   CHECK_NATNUM (count);
296   n = XINT (count);
297
298   if (CHAR_OR_CHAR_INTP (Vlast_command_char))
299     c = Vlast_command_char;
300   else
301     c = Fevent_to_character (Vlast_command_event, Qnil, Qnil, Qt);
302
303   if (NILP (c))
304     signal_simple_error ("Last typed character has no ASCII equivalent",
305                          Fcopy_event (Vlast_command_event, Qnil));
306
307   CHECK_CHAR_COERCE_INT (c);
308
309   ch = XCHAR (c);
310
311   while (n--)
312     internal_self_insert (ch, (n != 0));
313
314   return Qnil;
315 }
316
317 /* Insert character C1.  If NOAUTOFILL is nonzero, don't do autofill
318    even if it is enabled.
319
320    FSF:
321
322    If this insertion is suitable for direct output (completely simple),
323    return 0.  A value of 1 indicates this *might* not have been simple.
324    A value of 2 means this did things that call for an undo boundary.  */
325
326 static void
327 internal_self_insert (Emchar c1, int noautofill)
328 {
329   /* This function can GC */
330   /* int hairy = 0; -- unused */
331   REGISTER enum syntaxcode synt;
332   REGISTER Emchar c2;
333   Lisp_Object overwrite;
334   Lisp_Char_Table *syntax_table;
335   struct buffer *buf = current_buffer;
336   int tab_width;
337
338   overwrite = buf->overwrite_mode;
339   syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
340
341 #if 0
342   /* No, this is very bad, it makes undo *always* undo a character at a time
343      instead of grouping consecutive self-inserts together.  Nasty nasty.
344    */
345   if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions)
346       || !NILP (Vbefore_change_function) || !NILP (Vafter_change_function))
347     hairy = 1;
348 #endif
349
350   if (!NILP (overwrite)
351       && BUF_PT (buf) < BUF_ZV (buf)
352       && (EQ (overwrite, Qoverwrite_mode_binary)
353           || (c1 != '\n' && BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\n'))
354       && (EQ (overwrite, Qoverwrite_mode_binary)
355           || BUF_FETCH_CHAR (buf, BUF_PT (buf)) != '\t'
356           || ((tab_width = XINT (buf->tab_width), tab_width <= 0)
357           || tab_width > 20
358           || !((current_column (buf) + 1) % tab_width))))
359     {
360       buffer_delete_range (buf, BUF_PT (buf), BUF_PT (buf) + 1, 0);
361       /* hairy = 2; */
362     }
363
364   if (!NILP (buf->abbrev_mode)
365       && !WORD_SYNTAX_P (syntax_table, c1)
366       && NILP (buf->read_only)
367       && BUF_PT (buf) > BUF_BEGV (buf))
368     {
369       c2 = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
370
371       if (WORD_SYNTAX_P (syntax_table, c2))
372         {
373 #if 1
374           Fexpand_abbrev ();
375 #else  /* FSFmacs */
376           Lisp_Object sym = Fexpand_abbrev ();
377
378           /* I think this is too bogus to add.  The function should
379              have a way of examining the character to be inserted, so
380              it can decide whether to insert it or not.  We should
381              design it better than that.  */
382
383           /* Here FSFmacs remembers MODIFF, compares it after
384              Fexpand_abbrev() finishes, and updates HAIRY.  */
385
386           /* NOTE: we cannot simply check for Vlast_abbrev, because
387              Fexpand_abbrev() can bail out before setting it to
388              anything meaningful, leaving us stuck with an old value.
389              Thus Fexpand_abbrev() was extended to return the actual
390              abbrev symbol.  */
391           if (!NILP (sym)
392               && !NILP (symbol_function (XSYMBOL (sym)))
393               && SYMBOLP (symbol_function (XSYMBOL (sym))))
394             {
395               Lisp_Object prop = Fget (symbol_function (XSYMBOL (sym)),
396                                        Qno_self_insert, Qnil);
397               if (!NILP (prop))
398                 return;
399             }
400 #endif /* FSFmacs */
401         }
402     }
403   if ((CHAR_TABLEP (Vauto_fill_chars)
404        ? !NILP (XCHAR_TABLE_VALUE_UNSAFE (Vauto_fill_chars, c1))
405        : (c1 == ' ' || c1 == '\n'))
406       && !noautofill
407       && !NILP (buf->auto_fill_function))
408     {
409       buffer_insert_emacs_char (buf, c1);
410       if (c1 == '\n')
411         /* After inserting a newline, move to previous line and fill */
412         /* that.  Must have the newline in place already so filling and */
413         /* justification, if any, know where the end is going to be. */
414         BUF_SET_PT (buf, BUF_PT (buf) - 1);
415       call0 (buf->auto_fill_function);
416       if (c1 == '\n')
417         BUF_SET_PT (buf, BUF_PT (buf) + 1);
418       /* hairy = 2; */
419     }
420   else
421     buffer_insert_emacs_char (buf, c1);
422
423   /* If previous command specified a face to use, use it.  */
424   if (!NILP (Vself_insert_face)
425       && EQ (Vlast_command, Vself_insert_face_command))
426     {
427       Lisp_Object before = make_int (BUF_PT (buf) - 1);
428       Lisp_Object after  = make_int (BUF_PT (buf));
429       Fput_text_property (before, after, Qface, Vself_insert_face, Qnil);
430       Fput_text_property (before, after, Qstart_open, Qt, Qnil);
431       Fput_text_property (before, after, Qend_open, Qnil, Qnil);
432       /* #### FSFmacs properties are normally closed ("sticky") on the
433          end but not the beginning.  It's the opposite for us. */
434       Vself_insert_face = Qnil;
435     }
436   synt = SYNTAX (syntax_table, c1);
437   if ((synt == Sclose || synt == Smath)
438       && !NILP (Vblink_paren_function) && INTERACTIVE
439       && !noautofill)
440     {
441       call0 (Vblink_paren_function);
442       /* hairy = 2; */
443     }
444
445   /* return hairy; */
446 }
447
448 /* (this comes from Mule but is a generally good idea) */
449
450 DEFUN ("self-insert-internal", Fself_insert_internal, 1, 1, 0, /*
451 Invoke `self-insert-command' as if CHARACTER is entered from keyboard.
452 */
453        (character))
454 {
455   /* This function can GC */
456   CHECK_CHAR_COERCE_INT (character);
457   internal_self_insert (XCHAR (character), 0);
458   return Qnil;
459 }
460 \f
461 /* module initialization */
462
463 void
464 syms_of_cmds (void)
465 {
466   defsymbol (&Qkill_forward_chars, "kill-forward-chars");
467   defsymbol (&Qself_insert_command, "self-insert-command");
468   defsymbol (&Qoverwrite_mode_binary, "overwrite-mode-binary");
469   defsymbol (&Qno_self_insert, "no-self-insert");
470
471   DEFSUBR (Fforward_char);
472   DEFSUBR (Fbackward_char);
473   DEFSUBR (Fforward_line);
474   DEFSUBR (Fbeginning_of_line);
475   DEFSUBR (Fend_of_line);
476
477   DEFSUBR (Fpoint_at_bol);
478   DEFSUBR (Fpoint_at_eol);
479
480   DEFSUBR (Fdelete_char);
481   DEFSUBR (Fdelete_backward_char);
482
483   DEFSUBR (Fself_insert_command);
484   DEFSUBR (Fself_insert_internal);
485 }
486
487 void
488 vars_of_cmds (void)
489 {
490   DEFVAR_LISP ("self-insert-face", &Vself_insert_face /*
491 If non-nil, set the face of the next self-inserting character to this.
492 See also `self-insert-face-command'.
493 */ );
494   Vself_insert_face = Qnil;
495
496   DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command /*
497 This is the command that set up `self-insert-face'.
498 If `last-command' does not equal this value, we ignore `self-insert-face'.
499 */ );
500   Vself_insert_face_command = Qnil;
501
502   DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function /*
503 Function called, if non-nil, whenever a close parenthesis is inserted.
504 More precisely, a char with closeparen syntax is self-inserted.
505 */ );
506   Vblink_paren_function = Qnil;
507
508   DEFVAR_LISP ("auto-fill-chars", &Vauto_fill_chars /*
509 A char-table for characters which invoke auto-filling.
510 Such characters have value t in this table.
511 */);
512   Vauto_fill_chars = Fmake_char_table (Qgeneric);
513   XCHAR_TABLE (Vauto_fill_chars)->ascii[' '] = Qt;
514   XCHAR_TABLE (Vauto_fill_chars)->ascii['\n'] = Qt;
515 }