1 /* Simple built-in editing commands.
2 Copyright (C) 1985, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
31 Lisp_Object Qkill_forward_chars
, Qkill_backward_chars
, Vblink_paren_function
;
33 /* A possible value for a buffer's overwrite-mode variable. */
34 Lisp_Object Qoverwrite_mode_binary
;
36 /* Non-nil means put this face on the next self-inserting character. */
37 Lisp_Object Vself_insert_face
;
39 /* This is the command that set up Vself_insert_face. */
40 Lisp_Object Vself_insert_face_command
;
42 extern Lisp_Object Qface
;
44 /* Return buffer position which is N characters after `point'. */
51 if (!NILP (current_buffer
->enable_multibyte_characters
))
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
59 while (pos
< ZV
&& n
--) INC_POS (pos
);
64 while (pos
> BEGV
&& n
++) DEC_POS (pos
);
73 DEFUN ("forward-point", Fforward_point
, Sforward_point
, 1, 1, 0,
74 "Return buffer position N characters after (before if N negative) point.")
80 return make_number (forward_point (XINT (n
)));
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.")
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. */
100 int new_point
= forward_point (XINT (n
));
102 if (new_point
< BEGV
)
105 Fsignal (Qbeginning_of_buffer
, Qnil
);
110 Fsignal (Qend_of_buffer
, Qnil
);
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.")
130 XSETINT (n
, - XINT (n
));
131 return Fforward_char (n
);
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).")
147 int count
, shortage
, negp
;
158 pos
= scan_buffer ('\n', pos2
, 0, count
- negp
, &shortage
, 1);
163 && FETCH_BYTE (pos
- 1) != '\n')))
166 return make_number (negp
? - shortage
: shortage
);
169 DEFUN ("beginning-of-line", Fbeginning_of_line
, Sbeginning_of_line
,
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.")
182 SET_PT (XINT (Fline_beginning_position (n
)));
186 DEFUN ("end-of-line", Fend_of_line
, Send_of_line
,
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.")
202 SET_PT (XINT (Fline_end_position (n
)));
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.")
213 Lisp_Object n
, killflag
;
219 pos
= forward_point (XINT (n
));
225 Fsignal (Qbeginning_of_buffer
, Qnil
);
232 Fsignal (Qend_of_buffer
, Qnil
);
239 call1 (Qkill_forward_chars
, n
);
244 DEFUN ("delete-backward-char", Fdelete_backward_char
, Sdelete_backward_char
,
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.")
251 Lisp_Object n
, killflag
;
254 int deleted_special
= 0;
259 /* See if we are about to delete a tab or newline backwards. */
261 for (i
= 0; i
< XINT (n
) && pos
> BEGV
; i
++)
266 c
= FETCH_BYTE (pos
);
267 if (c
== '\t' || c
== '\n')
274 /* In overwrite mode, back over columns while clearing them out,
275 unless at end of line. */
277 && ! NILP (current_buffer
->overwrite_mode
)
279 && ! (PT
== ZV
|| FETCH_BYTE (PT
) == '\n'))
281 int column
= current_column ();
283 value
= Fdelete_char (make_number (-XINT (n
)), killflag
);
284 i
= column
- current_column ();
285 Finsert_char (make_number (' '), i
);
289 value
= Fdelete_char (make_number (-XINT (n
)), killflag
);
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.")
302 /* Barf if the key that invoked this was not a character. */
303 if (!INTEGERP (last_command_char
))
305 else if (XINT (n
) >= 2 && NILP (current_buffer
->overwrite_mode
))
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);
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);
328 /* Insert character C. If NOAUTOFILL is nonzero, don't do autofill
329 even if it is enabled.
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. */
335 internal_self_insert (c
, noautofill
)
339 extern Lisp_Object
Fexpand_abbrev ();
342 register enum syntaxcode synt
;
343 Lisp_Object overwrite
;
344 /* Length of multi-byte form of C. */
346 /* Working buffer and pointer for multi-byte form of C. */
347 unsigned char workbuf
[4], *str
;
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
))
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
);
358 workbuf
[0] = c
, str
= workbuf
, len
= 1;
360 if (!NILP (overwrite
)
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. */
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. */
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
386 if (EQ (overwrite
, Qoverwrite_mode_binary
)
389 && (target_clm
= current_column() + WIDTH_BY_CHAR_HEAD (str
[0]),
391 || XINT (current_buffer
->tab_width
) <= 0
392 || XFASTINT (current_buffer
->tab_width
) > 20
393 || !(target_clm
% XFASTINT (current_buffer
->tab_width
))))))
396 del_range (PT
, forward_point (1));
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
405 int actual_clm
= XFASTINT (Fmove_to_column (target_clm
));
408 if (actual_clm
> target_clm
)
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
);
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
)
428 sym
= Fexpand_abbrev ();
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
))
437 prop
= Fget (XSYMBOL (sym
)->function
, intern ("no-self-insert"));
442 if (MODIFF
!= modiff
)
445 if ((c
== ' ' || c
== '\n')
447 && !NILP (current_buffer
->auto_fill_function
))
451 insert_and_inherit (str
, len
);
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. */
457 tem
= call0 (current_buffer
->auto_fill_function
);
464 insert_and_inherit (str
, len
);
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
))
471 Lisp_Object before
, after
;
472 XSETINT (before
, PT
- len
);
474 Fput_text_property (before
, after
, Qface
, Vself_insert_face
, Qnil
);
475 Vself_insert_face
= Qnil
;
479 if ((synt
== Sclose
|| synt
== Smath
)
480 && !NILP (Vblink_paren_function
) && INTERACTIVE
483 call0 (Vblink_paren_function
);
489 /* module initialization */
493 Qkill_backward_chars
= intern ("kill-backward-chars");
494 staticpro (&Qkill_backward_chars
);
496 Qkill_forward_chars
= intern ("kill-forward-chars");
497 staticpro (&Qkill_forward_chars
);
499 Qoverwrite_mode_binary
= intern ("overwrite-mode-binary");
500 staticpro (&Qoverwrite_mode_binary
);
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
;
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
;
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
;
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
);
524 defsubr (&Sdelete_char
);
525 defsubr (&Sdelete_backward_char
);
527 defsubr (&Sself_insert_command
);
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");
538 for (n
= 0200; n
< 0240; n
++)
539 initial_define_key (global_map
, n
, "self-insert-command");
541 for (n
= 0240; n
< 0400; n
++)
542 initial_define_key (global_map
, n
, "self-insert-command");
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");