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