1 /* Simple built-in editing commands.
2 Copyright (C) 1985, 1993, 1994, 1995 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 Lisp_Object Qkill_forward_chars
, Qkill_backward_chars
, Vblink_paren_function
;
31 /* A possible value for a buffer's overwrite-mode variable. */
32 Lisp_Object Qoverwrite_mode_binary
;
34 /* Non-nil means put this face on the next self-inserting character. */
35 Lisp_Object Vself_insert_face
;
37 /* This is the command that set up Vself_insert_face. */
38 Lisp_Object Vself_insert_face_command
;
40 extern Lisp_Object Qface
;
42 DEFUN ("forward-char", Fforward_char
, Sforward_char
, 0, 1, "p",
43 "Move point right ARG characters (left if ARG negative).\n\
44 On reaching end of buffer, stop and signal error.")
51 CHECK_NUMBER (arg
, 0);
53 /* This used to just set point to point + XINT (arg), and then check
54 to see if it was within boundaries. But now that SET_PT can
55 potentially do a lot of stuff (calling entering and exiting
56 hooks, etcetera), that's not a good approach. So we validate the
57 proposed position, then set point. */
59 int new_point
= point
+ XINT (arg
);
64 Fsignal (Qbeginning_of_buffer
, Qnil
);
69 Fsignal (Qend_of_buffer
, Qnil
);
78 DEFUN ("backward-char", Fbackward_char
, Sbackward_char
, 0, 1, "p",
79 "Move point left ARG characters (right if ARG negative).\n\
80 On attempt to pass beginning or end of buffer, stop and signal error.")
87 CHECK_NUMBER (arg
, 0);
89 XSETINT (arg
, - XINT (arg
));
90 return Fforward_char (arg
);
93 DEFUN ("forward-line", Fforward_line
, Sforward_line
, 0, 1, "p",
94 "Move ARG lines forward (backward if ARG is negative).\n\
95 Precisely, if point is on line I, move to the start of line I + ARG.\n\
96 If there isn't room, go as far as possible (no error).\n\
97 Returns the count of lines left to move. If moving forward,\n\
98 that is ARG - number of lines moved; if backward, ARG + number moved.\n\
99 With positive ARG, a non-empty line at the end counts as one line\n\
100 successfully moved (for the return value).")
106 int count
, shortage
, negp
;
117 pos
= scan_buffer ('\n', pos2
, 0, count
- negp
, &shortage
, 1);
122 && FETCH_CHAR (pos
- 1) != '\n')))
125 return make_number (negp
? - shortage
: shortage
);
128 DEFUN ("beginning-of-line", Fbeginning_of_line
, Sbeginning_of_line
,
130 "Move point to beginning of current line.\n\
131 With argument ARG not nil or 1, move forward ARG - 1 lines first.\n\
132 If scan reaches end of buffer, stop there without error.")
141 Fforward_line (make_number (XINT (n
) - 1));
145 DEFUN ("end-of-line", Fend_of_line
, Send_of_line
,
147 "Move point to end of current line.\n\
148 With argument ARG not nil or 1, move forward ARG - 1 lines first.\n\
149 If scan reaches end of buffer, stop there without error.")
161 SET_PT (find_before_next_newline (PT
, 0, XINT (n
) - (XINT (n
) <= 0)));
166 DEFUN ("delete-char", Fdelete_char
, Sdelete_char
, 1, 2, "p\nP",
167 "Delete the following ARG characters (previous, with negative arg).\n\
168 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).\n\
169 Interactively, ARG is the prefix arg, and KILLFLAG is set if\n\
170 ARG was explicitly specified.")
172 Lisp_Object n
, killflag
;
180 if (point
+ XINT (n
) < BEGV
)
181 Fsignal (Qbeginning_of_buffer
, Qnil
);
183 del_range (point
+ XINT (n
), point
);
187 if (point
+ XINT (n
) > ZV
)
188 Fsignal (Qend_of_buffer
, Qnil
);
190 del_range (point
, point
+ XINT (n
));
195 call1 (Qkill_forward_chars
, n
);
200 DEFUN ("delete-backward-char", Fdelete_backward_char
, Sdelete_backward_char
,
202 "Delete the previous ARG characters (following, with negative ARG).\n\
203 Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).\n\
204 Interactively, ARG is the prefix arg, and KILLFLAG is set if\n\
205 ARG was explicitly specified.")
207 Lisp_Object n
, killflag
;
210 return Fdelete_char (make_number (-XINT (n
)), killflag
);
213 DEFUN ("self-insert-command", Fself_insert_command
, Sself_insert_command
, 1, 1, "p",
214 "Insert the character you type.\n\
215 Whichever character you type to run this command is inserted.")
219 CHECK_NUMBER (arg
, 0);
221 /* Barf if the key that invoked this was not a character. */
222 if (!INTEGERP (last_command_char
))
224 else if (XINT (arg
) >= 2 && NILP (current_buffer
->overwrite_mode
))
226 XSETFASTINT (arg
, XFASTINT (arg
) - 2);
227 /* The first one might want to expand an abbrev. */
228 internal_self_insert (XINT (last_command_char
), 1);
229 /* The bulk of the copies of this char can be inserted simply.
230 We don't have to handle a user-specified face specially
231 because it will get inherited from the first char inserted. */
232 Finsert_char (last_command_char
, arg
, Qt
);
233 /* The last one might want to auto-fill. */
234 internal_self_insert (XINT (last_command_char
), 0);
237 while (XINT (arg
) > 0)
239 /* Ok since old and new vals both nonneg */
240 XSETFASTINT (arg
, XFASTINT (arg
) - 1);
241 internal_self_insert (XINT (last_command_char
), XFASTINT (arg
) != 0);
247 /* Insert character C1. If NOAUTOFILL is nonzero, don't do autofill
248 even if it is enabled.
250 If this insertion is suitable for direct output (completely simple),
251 return 0. A value of 1 indicates this *might* not have been simple.
252 A value of 2 means this did things that call for an undo boundary. */
254 internal_self_insert (c1
, noautofill
)
255 /* This has to be unsigned char; when it is char,
256 some compilers sign-extend it in SYNTAX_ENTRY, despite
257 the casts to unsigned char there. */
261 extern Lisp_Object
Fexpand_abbrev ();
264 register enum syntaxcode synt
;
266 Lisp_Object overwrite
;
268 overwrite
= current_buffer
->overwrite_mode
;
269 if (!NILP (Vbefore_change_function
) || !NILP (Vafter_change_function
)
270 || !NILP (Vbefore_change_functions
) || !NILP (Vafter_change_functions
))
273 if (!NILP (overwrite
)
275 && (EQ (overwrite
, Qoverwrite_mode_binary
)
276 || (c
!= '\n' && FETCH_CHAR (point
) != '\n'))
277 && (EQ (overwrite
, Qoverwrite_mode_binary
)
278 || FETCH_CHAR (point
) != '\t'
279 || XINT (current_buffer
->tab_width
) <= 0
280 || XFASTINT (current_buffer
->tab_width
) > 20
281 || !((current_column () + 1) % XFASTINT (current_buffer
->tab_width
))))
283 del_range (point
, point
+ 1);
286 if (!NILP (current_buffer
->abbrev_mode
)
287 && SYNTAX (c
) != Sword
288 && NILP (current_buffer
->read_only
)
289 && point
> BEGV
&& SYNTAX (FETCH_CHAR (point
- 1)) == Sword
)
293 /* We can't trust the value of Fexpand_abbrev,
294 but if Fexpand_abbrev changed the buffer,
295 assume it expanded something. */
296 if (MODIFF
!= modiff
)
299 if ((c
== ' ' || c
== '\n')
301 && !NILP (current_buffer
->auto_fill_function
))
303 insert_and_inherit (&c1
, 1);
305 /* After inserting a newline, move to previous line and fill */
306 /* that. Must have the newline in place already so filling and */
307 /* justification, if any, know where the end is going to be. */
309 call0 (current_buffer
->auto_fill_function
);
315 insert_and_inherit (&c1
, 1);
318 /* If previous command specified a face to use, use it. */
319 if (!NILP (Vself_insert_face
)
320 && EQ (current_kboard
->Vlast_command
, Vself_insert_face_command
))
322 Lisp_Object before
, after
;
323 XSETINT (before
, PT
- 1);
325 Fput_text_property (before
, after
, Qface
, Vself_insert_face
, Qnil
);
326 Vself_insert_face
= Qnil
;
330 if ((synt
== Sclose
|| synt
== Smath
)
331 && !NILP (Vblink_paren_function
) && INTERACTIVE
334 call0 (Vblink_paren_function
);
340 /* module initialization */
344 Qkill_backward_chars
= intern ("kill-backward-chars");
345 staticpro (&Qkill_backward_chars
);
347 Qkill_forward_chars
= intern ("kill-forward-chars");
348 staticpro (&Qkill_forward_chars
);
350 Qoverwrite_mode_binary
= intern ("overwrite-mode-binary");
351 staticpro (&Qoverwrite_mode_binary
);
353 DEFVAR_LISP ("self-insert-face", &Vself_insert_face
,
354 "If non-nil, set the face of the next self-inserting character to this.\n\
355 See also `self-insert-face-command'.");
356 Vself_insert_face
= Qnil
;
358 DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command
,
359 "This is the command that set up `self-insert-face'.\n\
360 If `last-command' does not equal this value, we ignore `self-insert-face'.");
361 Vself_insert_face_command
= Qnil
;
363 DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function
,
364 "Function called, if non-nil, whenever a close parenthesis is inserted.\n\
365 More precisely, a char with closeparen syntax is self-inserted.");
366 Vblink_paren_function
= Qnil
;
368 defsubr (&Sforward_char
);
369 defsubr (&Sbackward_char
);
370 defsubr (&Sforward_line
);
371 defsubr (&Sbeginning_of_line
);
372 defsubr (&Send_of_line
);
374 defsubr (&Sdelete_char
);
375 defsubr (&Sdelete_backward_char
);
377 defsubr (&Sself_insert_command
);
384 initial_define_key (global_map
, Ctl ('I'), "self-insert-command");
385 for (n
= 040; n
< 0177; n
++)
386 initial_define_key (global_map
, n
, "self-insert-command");
388 for (n
= 0200; n
< 0240; n
++)
389 initial_define_key (global_map
, n
, "self-insert-command");
391 for (n
= 0240; n
< 0400; n
++)
392 initial_define_key (global_map
, n
, "self-insert-command");
394 initial_define_key (global_map
, Ctl ('A'), "beginning-of-line");
395 initial_define_key (global_map
, Ctl ('B'), "backward-char");
396 initial_define_key (global_map
, Ctl ('D'), "delete-char");
397 initial_define_key (global_map
, Ctl ('E'), "end-of-line");
398 initial_define_key (global_map
, Ctl ('F'), "forward-char");
399 initial_define_key (global_map
, 0177, "delete-backward-char");