]> code.delx.au - gnu-emacs/blob - src/cmds.c
(lisp, shortlisp): Add lao.elc.
[gnu-emacs] / src / cmds.c
1 /* Simple built-in editing commands.
2 Copyright (C) 1985, 93, 94, 95, 1996 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; 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
22 #include <config.h>
23 #include "lisp.h"
24 #include "commands.h"
25 #include "buffer.h"
26 #include "charset.h"
27 #include "syntax.h"
28 #include "window.h"
29 #include "keyboard.h"
30
31 Lisp_Object Qkill_forward_chars, Qkill_backward_chars, Vblink_paren_function;
32
33 /* A possible value for a buffer's overwrite-mode variable. */
34 Lisp_Object Qoverwrite_mode_binary;
35
36 /* Non-nil means put this face on the next self-inserting character. */
37 Lisp_Object Vself_insert_face;
38
39 /* This is the command that set up Vself_insert_face. */
40 Lisp_Object Vself_insert_face_command;
41
42 extern Lisp_Object Qface;
43 \f
44 /* Return buffer position which is N characters after `point'. */
45 int
46 forward_point (n)
47 int n;
48 {
49 int pos = PT, c;
50
51 if (!NILP (current_buffer->enable_multibyte_characters))
52 {
53 /* Simply adding N to `point' doesn't work because of multi-byte
54 form. We had better not use INC_POS and DEC_POS because they
55 check the gap position every time. But, for the moment, we
56 need working code. */
57 if (n > 0)
58 {
59 while (pos < ZV && n--) INC_POS (pos);
60 if (pos < ZV) n++;
61 }
62 else
63 {
64 while (pos > BEGV && n++) DEC_POS (pos);
65 if (pos > BEGV) n--;
66 }
67 }
68 pos += n;
69
70 return pos;
71 }
72
73 DEFUN ("forward-point", Fforward_point, Sforward_point, 1, 1, 0,
74 "Return buffer position N characters after (before if N negative) point.")
75 (n)
76 Lisp_Object n;
77 {
78 CHECK_NUMBER (n, 0);
79
80 return make_number (forward_point (XINT (n)));
81 }
82
83 DEFUN ("forward-char", Fforward_char, Sforward_char, 0, 1, "p",
84 "Move point right N characters (left if N is negative).\n\
85 On reaching end of buffer, stop and signal error.")
86 (n)
87 Lisp_Object n;
88 {
89 if (NILP (n))
90 XSETFASTINT (n, 1);
91 else
92 CHECK_NUMBER (n, 0);
93
94 /* This used to just set point to point + XINT (n), and then check
95 to see if it was within boundaries. But now that SET_PT can
96 potentially do a lot of stuff (calling entering and exiting
97 hooks, etcetera), that's not a good approach. So we validate the
98 proposed position, then set point. */
99 {
100 int new_point = forward_point (XINT (n));
101
102 if (new_point < BEGV)
103 {
104 SET_PT (BEGV);
105 Fsignal (Qbeginning_of_buffer, Qnil);
106 }
107 if (new_point > ZV)
108 {
109 SET_PT (ZV);
110 Fsignal (Qend_of_buffer, Qnil);
111 }
112
113 SET_PT (new_point);
114 }
115
116 return Qnil;
117 }
118
119 DEFUN ("backward-char", Fbackward_char, Sbackward_char, 0, 1, "p",
120 "Move point left N characters (right if N is negative).\n\
121 On attempt to pass beginning or end of buffer, stop and signal error.")
122 (n)
123 Lisp_Object n;
124 {
125 if (NILP (n))
126 XSETFASTINT (n, 1);
127 else
128 CHECK_NUMBER (n, 0);
129
130 XSETINT (n, - XINT (n));
131 return Fforward_char (n);
132 }
133
134 DEFUN ("forward-line", Fforward_line, Sforward_line, 0, 1, "p",
135 "Move N lines forward (backward if N is negative).\n\
136 Precisely, if point is on line I, move to the start of line I + N.\n\
137 If there isn't room, go as far as possible (no error).\n\
138 Returns the count of lines left to move. If moving forward,\n\
139 that is N - number of lines moved; if backward, N + number moved.\n\
140 With positive N, a non-empty line at the end counts as one line\n\
141 successfully moved (for the return value).")
142 (n)
143 Lisp_Object n;
144 {
145 int pos2 = PT;
146 int pos;
147 int count, shortage, negp;
148
149 if (NILP (n))
150 count = 1;
151 else
152 {
153 CHECK_NUMBER (n, 0);
154 count = XINT (n);
155 }
156
157 negp = count <= 0;
158 pos = scan_buffer ('\n', pos2, 0, count - negp, &shortage, 1);
159 if (shortage > 0
160 && (negp
161 || (ZV > BEGV
162 && pos != pos2
163 && FETCH_BYTE (pos - 1) != '\n')))
164 shortage--;
165 SET_PT (pos);
166 return make_number (negp ? - shortage : shortage);
167 }
168
169 DEFUN ("beginning-of-line", Fbeginning_of_line, Sbeginning_of_line,
170 0, 1, "p",
171 "Move point to beginning of current line.\n\
172 With argument N not nil or 1, move forward N - 1 lines first.\n\
173 If scan reaches end of buffer, stop there without error.")
174 (n)
175 Lisp_Object n;
176 {
177 if (NILP (n))
178 XSETFASTINT (n, 1);
179 else
180 CHECK_NUMBER (n, 0);
181
182 SET_PT (XINT (Fline_beginning_position (n)));
183 return Qnil;
184 }
185
186 DEFUN ("end-of-line", Fend_of_line, Send_of_line,
187 0, 1, "p",
188 "Move point to end of current line.\n\
189 With argument N not nil or 1, move forward N - 1 lines first.\n\
190 If scan reaches end of buffer, stop there without error.")
191 (n)
192 Lisp_Object n;
193 {
194 register int pos;
195 register int stop;
196
197 if (NILP (n))
198 XSETFASTINT (n, 1);
199 else
200 CHECK_NUMBER (n, 0);
201
202 SET_PT (XINT (Fline_end_position (n)));
203
204 return Qnil;
205 }
206
207 DEFUN ("delete-char", Fdelete_char, Sdelete_char, 1, 2, "p\nP",
208 "Delete the following N characters (previous if N is negative).\n\
209 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).\n\
210 Interactively, N is the prefix arg, and KILLFLAG is set if\n\
211 N was explicitly specified.")
212 (n, killflag)
213 Lisp_Object n, killflag;
214 {
215 int pos;
216
217 CHECK_NUMBER (n, 0);
218
219 pos = forward_point (XINT (n));
220 if (NILP (killflag))
221 {
222 if (XINT (n) < 0)
223 {
224 if (pos < BEGV)
225 Fsignal (Qbeginning_of_buffer, Qnil);
226 else
227 del_range (pos, PT);
228 }
229 else
230 {
231 if (pos > ZV)
232 Fsignal (Qend_of_buffer, Qnil);
233 else
234 del_range (PT, pos);
235 }
236 }
237 else
238 {
239 call1 (Qkill_forward_chars, n);
240 }
241 return Qnil;
242 }
243
244 DEFUN ("delete-backward-char", Fdelete_backward_char, Sdelete_backward_char,
245 1, 2, "p\nP",
246 "Delete the previous N characters (following if N is negative).\n\
247 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).\n\
248 Interactively, N is the prefix arg, and KILLFLAG is set if\n\
249 N was explicitly specified.")
250 (n, killflag)
251 Lisp_Object n, killflag;
252 {
253 Lisp_Object value;
254 int deleted_special = 0;
255 int pos, i;
256
257 CHECK_NUMBER (n, 0);
258
259 /* See if we are about to delete a tab or newline backwards. */
260 pos = PT;
261 for (i = 0; i < XINT (n) && pos > BEGV; i++)
262 {
263 int c;
264
265 DEC_POS (pos);
266 c = FETCH_BYTE (pos);
267 if (c == '\t' || c == '\n')
268 {
269 deleted_special = 1;
270 break;
271 }
272 }
273
274 /* In overwrite mode, back over columns while clearing them out,
275 unless at end of line. */
276 if (XINT (n) > 0
277 && ! NILP (current_buffer->overwrite_mode)
278 && ! deleted_special
279 && ! (PT == ZV || FETCH_BYTE (PT) == '\n'))
280 {
281 int column = current_column ();
282
283 value = Fdelete_char (make_number (-XINT (n)), killflag);
284 i = column - current_column ();
285 Finsert_char (make_number (' '), i);
286 SET_PT (PT - i);
287 }
288 else
289 value = Fdelete_char (make_number (-XINT (n)), killflag);
290
291 return value;
292 }
293
294 DEFUN ("self-insert-command", Fself_insert_command, Sself_insert_command, 1, 1, "p",
295 "Insert the character you type.\n\
296 Whichever character you type to run this command is inserted.")
297 (n)
298 Lisp_Object n;
299 {
300 CHECK_NUMBER (n, 0);
301
302 /* Barf if the key that invoked this was not a character. */
303 if (!INTEGERP (last_command_char))
304 bitch_at_user ();
305 else if (XINT (n) >= 2 && NILP (current_buffer->overwrite_mode))
306 {
307 XSETFASTINT (n, XFASTINT (n) - 2);
308 /* The first one might want to expand an abbrev. */
309 internal_self_insert (XINT (last_command_char), 1);
310 /* The bulk of the copies of this char can be inserted simply.
311 We don't have to handle a user-specified face specially
312 because it will get inherited from the first char inserted. */
313 Finsert_char (last_command_char, n, Qt);
314 /* The last one might want to auto-fill. */
315 internal_self_insert (XINT (last_command_char), 0);
316 }
317 else
318 while (XINT (n) > 0)
319 {
320 /* Ok since old and new vals both nonneg */
321 XSETFASTINT (n, XFASTINT (n) - 1);
322 internal_self_insert (XINT (last_command_char), XFASTINT (n) != 0);
323 }
324
325 return Qnil;
326 }
327
328 /* Insert character C. If NOAUTOFILL is nonzero, don't do autofill
329 even if it is enabled.
330
331 If this insertion is suitable for direct output (completely simple),
332 return 0. A value of 1 indicates this *might* not have been simple.
333 A value of 2 means this did things that call for an undo boundary. */
334
335 internal_self_insert (c, noautofill)
336 int c;
337 int noautofill;
338 {
339 extern Lisp_Object Fexpand_abbrev ();
340 int hairy = 0;
341 Lisp_Object tem;
342 register enum syntaxcode synt;
343 Lisp_Object overwrite;
344 /* Length of multi-byte form of C. */
345 int len;
346 /* Working buffer and pointer for multi-byte form of C. */
347 unsigned char workbuf[4], *str;
348
349 overwrite = current_buffer->overwrite_mode;
350 if (!NILP (Vbefore_change_function) || !NILP (Vafter_change_function)
351 || !NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions))
352 hairy = 1;
353
354 /* At first, get multi-byte form of C in STR. */
355 if (!NILP (current_buffer->enable_multibyte_characters))
356 len = CHAR_STRING (c, workbuf, str);
357 else
358 workbuf[0] = c, str = workbuf, len = 1;
359
360 if (!NILP (overwrite)
361 && PT < ZV)
362 {
363 /* In overwrite-mode, we substitute a character at point (C2,
364 hereafter) by C. For that, we delete C2 in advance. But,
365 just substituting C2 by C may move a remaining text in the
366 line to the right or to the left, which is not preferable.
367 So we insert more spaces or delete more characters in the
368 following cases: if C is narrower than C2, after deleting C2,
369 we fill columns with spaces, if C is wider than C2, we delete
370 C2 and several characters following C2. */
371
372 /* A code at `point'. Since this is checked only against
373 NEWLINE and TAB, we don't need a character code but only the
374 first byte of multi-byte form. */
375 unsigned char c2 = FETCH_BYTE (PT);
376 /* A column the cursor should be placed at after this insertion.
377 The correct value should be calculated only when necessary. */
378 int target_clm = 0;
379
380 /* Overwriting in binary-mode always substitute C2 by C. But,
381 overwriting in textual-mode does this substitution in the
382 case that C is not NEWLINE and C2 is not NEWLINE nor TAB. If
383 C2 is TAB, the substitution is done only when C2 is currently
384 expanded to 0 column, or more than 20 columns, or more than
385 the width of C. */
386 if (EQ (overwrite, Qoverwrite_mode_binary)
387 || (c != '\n'
388 && c2 != '\n'
389 && (target_clm = current_column() + WIDTH_BY_CHAR_HEAD (str[0]),
390 (c2 != '\t'
391 || XINT (current_buffer->tab_width) <= 0
392 || XFASTINT (current_buffer->tab_width) > 20
393 || !(target_clm % XFASTINT (current_buffer->tab_width))))))
394 {
395 if (target_clm == 0)
396 del_range (PT, forward_point (1));
397 else
398 {
399 int pos = point;
400 /* The actual cursor position after the trial of moving
401 to column TARGET_CLM. It is greater than TARGET_CLM
402 if the TARGET_CLM is middle of multi-column
403 character. In that case, the new point is set after
404 that character. */
405 int actual_clm = XFASTINT (Fmove_to_column (target_clm));
406
407 del_range (pos, PT);
408 if (actual_clm > target_clm)
409 {
410 /* We deleted too many columns. Let's fill columns
411 by spaces so that the remaining text won't move. */
412 insert(" ", actual_clm - target_clm);
413 SET_PT (pos);
414 }
415 }
416 hairy = 2;
417 }
418 hairy = 2;
419 }
420 if (!NILP (current_buffer->abbrev_mode)
421 && SYNTAX (c) != Sword
422 && NILP (current_buffer->read_only)
423 && PT > BEGV && SYNTAX (XFASTINT (Fprevious_char ())) == Sword)
424 {
425 int modiff = MODIFF;
426 Lisp_Object sym;
427
428 sym = Fexpand_abbrev ();
429
430 /* If we expanded an abbrev which has only a hook,
431 and the hook has a non-nil `no-self-insert' property,
432 return right away--don't really self-insert. */
433 if (! NILP (sym) && ! NILP (XSYMBOL (sym)->function)
434 && SYMBOLP (XSYMBOL (sym)->function))
435 {
436 Lisp_Object prop;
437 prop = Fget (XSYMBOL (sym)->function, intern ("no-self-insert"));
438 if (! NILP (prop))
439 return Qnil;
440 }
441
442 if (MODIFF != modiff)
443 hairy = 2;
444 }
445 if ((c == ' ' || c == '\n')
446 && !noautofill
447 && !NILP (current_buffer->auto_fill_function))
448 {
449 Lisp_Object tem;
450
451 insert_and_inherit (str, len);
452 if (c == '\n')
453 /* After inserting a newline, move to previous line and fill */
454 /* that. Must have the newline in place already so filling and */
455 /* justification, if any, know where the end is going to be. */
456 SET_PT (PT - 1);
457 tem = call0 (current_buffer->auto_fill_function);
458 if (c == '\n')
459 SET_PT (PT + 1);
460 if (!NILP (tem))
461 hairy = 2;
462 }
463 else
464 insert_and_inherit (str, len);
465
466 #ifdef HAVE_FACES
467 /* If previous command specified a face to use, use it. */
468 if (!NILP (Vself_insert_face)
469 && EQ (current_kboard->Vlast_command, Vself_insert_face_command))
470 {
471 Lisp_Object before, after;
472 XSETINT (before, PT - len);
473 XSETINT (after, PT);
474 Fput_text_property (before, after, Qface, Vself_insert_face, Qnil);
475 Vself_insert_face = Qnil;
476 }
477 #endif
478 synt = SYNTAX (c);
479 if ((synt == Sclose || synt == Smath)
480 && !NILP (Vblink_paren_function) && INTERACTIVE
481 && !noautofill)
482 {
483 call0 (Vblink_paren_function);
484 hairy = 2;
485 }
486 return hairy;
487 }
488 \f
489 /* module initialization */
490
491 syms_of_cmds ()
492 {
493 Qkill_backward_chars = intern ("kill-backward-chars");
494 staticpro (&Qkill_backward_chars);
495
496 Qkill_forward_chars = intern ("kill-forward-chars");
497 staticpro (&Qkill_forward_chars);
498
499 Qoverwrite_mode_binary = intern ("overwrite-mode-binary");
500 staticpro (&Qoverwrite_mode_binary);
501
502 DEFVAR_LISP ("self-insert-face", &Vself_insert_face,
503 "If non-nil, set the face of the next self-inserting character to this.\n\
504 See also `self-insert-face-command'.");
505 Vself_insert_face = Qnil;
506
507 DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command,
508 "This is the command that set up `self-insert-face'.\n\
509 If `last-command' does not equal this value, we ignore `self-insert-face'.");
510 Vself_insert_face_command = Qnil;
511
512 DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function,
513 "Function called, if non-nil, whenever a close parenthesis is inserted.\n\
514 More precisely, a char with closeparen syntax is self-inserted.");
515 Vblink_paren_function = Qnil;
516
517 defsubr (&Sforward_point);
518 defsubr (&Sforward_char);
519 defsubr (&Sbackward_char);
520 defsubr (&Sforward_line);
521 defsubr (&Sbeginning_of_line);
522 defsubr (&Send_of_line);
523
524 defsubr (&Sdelete_char);
525 defsubr (&Sdelete_backward_char);
526
527 defsubr (&Sself_insert_command);
528 }
529
530 keys_of_cmds ()
531 {
532 int n;
533
534 initial_define_key (global_map, Ctl ('I'), "self-insert-command");
535 for (n = 040; n < 0177; n++)
536 initial_define_key (global_map, n, "self-insert-command");
537 #ifdef MSDOS
538 for (n = 0200; n < 0240; n++)
539 initial_define_key (global_map, n, "self-insert-command");
540 #endif
541 for (n = 0240; n < 0400; n++)
542 initial_define_key (global_map, n, "self-insert-command");
543
544 initial_define_key (global_map, Ctl ('A'), "beginning-of-line");
545 initial_define_key (global_map, Ctl ('B'), "backward-char");
546 initial_define_key (global_map, Ctl ('D'), "delete-char");
547 initial_define_key (global_map, Ctl ('E'), "end-of-line");
548 initial_define_key (global_map, Ctl ('F'), "forward-char");
549 initial_define_key (global_map, 0177, "delete-backward-char");
550 }