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