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