Mercurial > hg > xemacs-beta
annotate src/callint.c @ 4644:b0ae008bf1a0
Documentment placement restriction. <87d490jb7v.fsf@uwakimon.sk.tsukuba.ac.jp>
author | Stephen J. Turnbull <stephen@xemacs.org> |
---|---|
date | Sat, 20 Jun 2009 04:07:12 +0900 |
parents | ecf1ebac70d8 |
children | cdc51540fed7 |
rev | line source |
---|---|
428 | 1 /* Call a Lisp function interactively. |
2 Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc. | |
793 | 3 Copyright (C) 1995, 1996, 2001, 2002 Ben Wing. |
428 | 4 |
5 This file is part of XEmacs. | |
6 | |
7 XEmacs is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
10 later version. | |
11 | |
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with XEmacs; see the file COPYING. If not, write to | |
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 Boston, MA 02111-1307, USA. */ | |
21 | |
22 /* Synched up with: FSF 19.30, Mule 2.0. */ | |
23 | |
24 /* Authorship: | |
25 | |
26 FSF: long ago. | |
27 Mly or JWZ: various changes. | |
28 */ | |
29 | |
30 #include <config.h> | |
31 #include "lisp.h" | |
32 | |
33 #include "buffer.h" | |
34 #include "bytecode.h" | |
35 #include "commands.h" | |
36 #include "events.h" | |
37 #include "insdel.h" | |
872 | 38 #include "window-impl.h" /* WINDOW_MINI_P */ |
428 | 39 |
1204 | 40 extern Charcount num_input_chars; |
428 | 41 |
42 Lisp_Object Vcurrent_prefix_arg; | |
43 Lisp_Object Qcall_interactively; | |
44 Lisp_Object Vcommand_history; | |
45 | |
46 Lisp_Object Vcommand_debug_status, Qcommand_debug_status; | |
47 Lisp_Object Qenable_recursive_minibuffers; | |
48 | |
49 #if 0 /* FSFmacs */ | |
50 /* Non-nil means treat the mark as active | |
51 even if mark_active is 0. */ | |
52 Lisp_Object Vmark_even_if_inactive; | |
53 #endif | |
54 | |
55 #if 0 /* ill-conceived */ | |
444 | 56 /* FSF calls Qmouse_leave_buffer_hook at all sorts of random places, |
57 including a bunch of places in their mouse.el. If this is | |
58 implemented, it has to be done cleanly. */ | |
428 | 59 Lisp_Object Vmouse_leave_buffer_hook, Qmouse_leave_buffer_hook; |
60 #endif | |
61 | |
442 | 62 Lisp_Object QletX, Qsave_excursion; |
428 | 63 |
64 Lisp_Object Qread_from_minibuffer; | |
65 Lisp_Object Qread_file_name; | |
66 Lisp_Object Qread_directory_name; | |
67 Lisp_Object Qcompleting_read; | |
68 Lisp_Object Qread_buffer; | |
69 Lisp_Object Qread_function; | |
70 Lisp_Object Qread_variable; | |
71 Lisp_Object Qread_expression; | |
72 Lisp_Object Qread_command; | |
73 Lisp_Object Qread_number; | |
74 Lisp_Object Qread_string; | |
75 Lisp_Object Qevents_to_keys; | |
76 | |
77 Lisp_Object Qread_coding_system; | |
78 Lisp_Object Qread_non_nil_coding_system; | |
79 | |
80 /* ARGSUSED */ | |
81 DEFUN ("interactive", Finteractive, 0, UNEVALLED, 0, /* | |
82 Specify a way of parsing arguments for interactive use of a function. | |
83 For example, write | |
84 (defun foo (arg) "Doc string" (interactive "p") ...use arg...) | |
85 to make ARG be the prefix argument when `foo' is called as a command. | |
86 The "call" to `interactive' is actually a declaration rather than a function; | |
87 it tells `call-interactively' how to read arguments | |
88 to pass to the function. | |
4644
b0ae008bf1a0
Documentment placement restriction. <87d490jb7v.fsf@uwakimon.sk.tsukuba.ac.jp>
Stephen J. Turnbull <stephen@xemacs.org>
parents:
2367
diff
changeset
|
89 The interactive form must appear at the top level of the function body. If |
b0ae008bf1a0
Documentment placement restriction. <87d490jb7v.fsf@uwakimon.sk.tsukuba.ac.jp>
Stephen J. Turnbull <stephen@xemacs.org>
parents:
2367
diff
changeset
|
90 it is wrapped in a `let' or `progn' or similar, Lisp will not even realize |
b0ae008bf1a0
Documentment placement restriction. <87d490jb7v.fsf@uwakimon.sk.tsukuba.ac.jp>
Stephen J. Turnbull <stephen@xemacs.org>
parents:
2367
diff
changeset
|
91 the function is an interactive command! |
428 | 92 When actually called, `interactive' just returns nil. |
93 | |
94 The argument of `interactive' is usually a string containing a code letter | |
95 followed by a prompt. (Some code letters do not use I/O to get | |
96 the argument and do not need prompts.) To prompt for multiple arguments, | |
97 give a code letter, its prompt, a newline, and another code letter, etc. | |
98 Prompts are passed to format, and may use % escapes to print the | |
99 arguments that have already been read. | |
100 If the argument is not a string, it is evaluated to get a list of | |
101 arguments to pass to the function. | |
102 Just `(interactive)' means pass no args when calling interactively. | |
103 | |
104 Code letters available are: | |
105 a -- Function name: symbol with a function definition. | |
106 b -- Name of existing buffer. | |
107 B -- Name of buffer, possibly nonexistent. | |
108 c -- Character. | |
109 C -- Command name: symbol with interactive function definition. | |
110 d -- Value of point as number. Does not do I/O. | |
111 D -- Directory name. | |
112 e -- Last mouse-button or misc-user event that invoked this command. | |
113 If used more than once, the Nth `e' returns the Nth such event. | |
114 Does not do I/O. | |
115 f -- Existing file name. | |
116 F -- Possibly nonexistent file name. | |
117 i -- Always nil, ignore. Use to skip arguments when interactive. | |
118 k -- Key sequence (a vector of events). | |
119 K -- Key sequence to be redefined (do not automatically down-case). | |
120 m -- Value of mark as number. Does not do I/O. | |
121 n -- Number read using minibuffer. | |
122 N -- Prefix arg converted to number, or if none, do like code `n'. | |
123 p -- Prefix arg converted to number. Does not do I/O. | |
124 P -- Prefix arg in raw form. Does not do I/O. | |
125 r -- Region: point and mark as 2 numeric args, smallest first. Does no I/O. | |
126 s -- Any string. | |
127 S -- Any symbol. | |
128 v -- Variable name: symbol that is user-variable-p. | |
129 x -- Lisp expression read but not evaluated. | |
130 X -- Lisp expression read and evaluated. | |
131 z -- Coding system. (Always nil if no Mule support.) | |
132 Z -- Coding system, nil if no prefix arg. (Always nil if no Mule support.) | |
133 In addition, if the string begins with `*' | |
134 then an error is signaled if the buffer is read-only. | |
135 This happens before reading any arguments. | |
136 If the string begins with `@', then the window the mouse is over is selected | |
137 before anything else is done. | |
138 If the string begins with `_', then this command will not cause the region | |
139 to be deactivated when it completes; that is, `zmacs-region-stays' will be | |
140 set to t when the command exits successfully. | |
141 You may use any of `@', `*' and `_' at the beginning of the string; | |
142 they are processed in the order that they appear. | |
502 | 143 |
144 | |
145 When writing your own interactive spec, it can be useful to know the | |
146 equivalent Lisp expressions for the various code letters. They are: | |
147 | |
148 a -- (read-function "PROMPT") | |
149 b -- (let ((def (current-buffer))) | |
150 (if (eq (selected-window) (active-minibuffer-window)) | |
151 (setq def (other-buffer def)) | |
152 (read-buffer "PROMPT" def t))) | |
153 B -- (read-buffer "PROMPT" (other-buffer (current-buffer))) | |
154 c -- (prog1 | |
155 (let ((cursor-in-echo-area t)) | |
156 (message "%s" "PROMPT") | |
157 (read-char)) | |
158 (message nil)) | |
159 C -- (read-command "PROMPT") | |
160 d -- (point) | |
161 D -- (read-directory-name "PROMPT" nil default-directory t) | |
162 e -- current-mouse-event ;; #### not quite right. needs access to the KEYS | |
163 ;; argument of `call-interactively', but that's | |
164 ;; currently impossible. | |
165 f -- (read-file-name "PROMPT" nil nil 0) | |
166 F -- (read-file-name "PROMPT") | |
167 i -- nil | |
168 k -- (read-key-sequence "PROMPT") | |
169 K -- (read-key-sequence "PROMPT" nil t) | |
170 m -- (mark) | |
171 n -- (read-number "PROMPT") | |
172 N -- (if current-prefix-arg | |
173 (prefix-numeric-value current-prefix-arg) | |
174 (read-number "PROMPT")) | |
175 p -- (prefix-numeric-value current-prefix-arg) | |
176 P -- current-prefix-arg | |
177 r -- (if (and zmacs-regions (not zmacs-region-active-p)) | |
178 (error "The region is not active now")) | |
179 (let ((tem (marker-buffer (mark-marker t)))) | |
180 (unless (and tem (eq tem (current-buffer))) | |
181 (error "The mark is now set now"))) | |
182 (region-beginning) + | |
183 (region-end) | |
184 s -- (read-string "PROMPT") | |
185 S -- (let (tem prev-tem) | |
186 (while (not tem) | |
187 (setq tem (completing-read "PROMPT" obarray nil nil prev-tem)) | |
188 (setq prev-tem tem) | |
189 (setq tem (intern tem)) | |
190 (if (= (length tem) 0) | |
191 (setq tem nil)))) | |
192 v -- (read-variable "PROMPT") | |
193 x -- (read-expression "PROMPT") | |
194 X -- (eval (read-expression "PROMPT")) | |
195 z -- (and (fboundp 'read-coding-system) (read-coding-system "PROMPT")) | |
196 Z -- (and current-prefix-arg (fboundp 'read-coding-system) | |
197 (read-coding-system "PROMPT")) | |
198 | |
199 `*' (barf-if-buffer-read-only) | |
200 `@' (let ((event current-mouse-event)) ;; #### not quite right; needs the | |
201 (when event ;; value from the `e' spec above. | |
202 (let ((window event-window event)) | |
203 (when window | |
204 (if (and (window-minibuffer-p window) | |
205 (not (and (> (minibuffer-depth) 0) | |
206 (eq window (active-minibuffer-window))))) | |
207 (error "Attempt to select inactive minibuffer window")) | |
208 (select window))))) | |
209 `_' (setq zmacs-region-stays t) | |
210 | |
428 | 211 */ |
2286 | 212 (UNUSED (args))) |
428 | 213 { |
214 return Qnil; | |
215 } | |
216 | |
217 /* Originally, this was just a function -- but `custom' used a | |
218 garden-variety version, so why not make it a subr? */ | |
219 /* #### Move it to another file! */ | |
220 DEFUN ("quote-maybe", Fquote_maybe, 1, 1, 0, /* | |
221 Quote EXPR if it is not self quoting. | |
222 */ | |
223 (expr)) | |
224 { | |
225 return ((NILP (expr) | |
226 || EQ (expr, Qt) | |
227 || INTP (expr) | |
228 || FLOATP (expr) | |
229 || CHARP (expr) | |
230 || STRINGP (expr) | |
231 || VECTORP (expr) | |
232 || KEYWORDP (expr) | |
233 || BIT_VECTORP (expr) | |
234 || (CONSP (expr) && EQ (XCAR (expr), Qlambda))) | |
235 ? expr : list2 (Qquote, expr)); | |
236 } | |
237 | |
238 /* Modify EXPR by quotifying each element (except the first). */ | |
239 static Lisp_Object | |
240 quotify_args (Lisp_Object expr) | |
241 { | |
2367 | 242 EXTERNAL_LIST_LOOP_3 (elt, expr, tail) |
243 XSETCAR (tail, Fquote_maybe (elt)); | |
428 | 244 return expr; |
245 } | |
246 | |
665 | 247 static Charbpos |
428 | 248 check_mark (void) |
249 { | |
250 Lisp_Object tem; | |
251 | |
252 if (zmacs_regions && !zmacs_region_active_p) | |
563 | 253 invalid_operation ("The region is not active now", Qunbound); |
428 | 254 |
255 tem = Fmarker_buffer (current_buffer->mark); | |
256 if (NILP (tem) || (XBUFFER (tem) != current_buffer)) | |
563 | 257 invalid_operation ("The mark is not set now", Qunbound); |
428 | 258 |
259 return marker_position (current_buffer->mark); | |
260 } | |
261 | |
262 static Lisp_Object | |
867 | 263 callint_prompt (const Ibyte *prompt_start, Bytecount prompt_length, |
442 | 264 const Lisp_Object *args, int nargs) |
428 | 265 { |
266 Lisp_Object s = make_string (prompt_start, prompt_length); | |
267 struct gcpro gcpro1; | |
268 | |
269 /* Fformat no longer smashes its arg vector, so no need to copy it. */ | |
270 | |
271 if (!strchr ((char *) XSTRING_DATA (s), '%')) | |
272 return s; | |
273 GCPRO1 (s); | |
771 | 274 RETURN_UNGCPRO (emacs_vsprintf_string_lisp (0, s, nargs, args)); |
428 | 275 } |
276 | |
277 /* `lambda' for RECORD-FLAG is an XEmacs addition. */ | |
278 | |
279 DEFUN ("call-interactively", Fcall_interactively, 1, 3, 0, /* | |
280 Call FUNCTION, reading args according to its interactive calling specs. | |
281 Return the value FUNCTION returns. | |
282 The function contains a specification of how to do the argument reading. | |
283 In the case of user-defined functions, this is specified by placing a call | |
284 to the function `interactive' at the top level of the function body. | |
285 See `interactive'. | |
286 | |
287 If optional second arg RECORD-FLAG is the symbol `lambda', the interactive | |
288 calling arguments for FUNCTION are read and returned as a list, | |
289 but the function is not called on them. | |
290 | |
291 If RECORD-FLAG is `t' then unconditionally put this command in the | |
292 command-history. Otherwise, this is done only if an arg is read using | |
293 the minibuffer. | |
294 | |
295 The argument KEYS specifies the value to use instead of (this-command-keys) | |
296 when reading the arguments. | |
297 */ | |
298 (function, record_flag, keys)) | |
299 { | |
300 /* This function can GC */ | |
301 int speccount = specpdl_depth (); | |
302 Lisp_Object prefix; | |
303 | |
304 Lisp_Object fun; | |
305 Lisp_Object specs = Qnil; | |
306 #ifdef IT_SEEMS_THAT_MLY_DOESNT_LIKE_THIS | |
307 Lisp_Object enable; | |
308 #endif | |
793 | 309 /* If SPECS is a string, we reset prompt_data to XSTRING_DATA (specs) |
310 every time a GC might have occurred */ | |
442 | 311 const char *prompt_data = 0; |
428 | 312 int prompt_index = 0; |
313 int argcount; | |
314 int set_zmacs_region_stays = 0; | |
315 int mouse_event_count = 0; | |
316 | |
317 if (!NILP (keys)) | |
318 { | |
319 int i, len; | |
320 | |
321 CHECK_VECTOR (keys); | |
322 len = XVECTOR_LENGTH (keys); | |
323 for (i = 0; i < len; i++) | |
324 CHECK_LIVE_EVENT (XVECTOR_DATA (keys)[i]); | |
325 } | |
326 | |
327 /* Save this now, since use of minibuffer will clobber it. */ | |
328 prefix = Vcurrent_prefix_arg; | |
329 | |
330 retry: | |
331 | |
332 #ifdef IT_SEEMS_THAT_MLY_DOESNT_LIKE_THIS | |
333 /* Marginal kludge. Use an evaluated interactive spec instead of this! */ | |
334 if (SYMBOLP (function)) | |
335 enable = Fget (function, Qenable_recursive_minibuffers, Qnil); | |
336 #endif | |
337 | |
338 fun = indirect_function (function, 1); | |
339 | |
340 /* Decode the kind of function. Either handle it and return, | |
341 or go to `lose' if not interactive, or go to `retry' | |
342 to specify a different function, or set either PROMPT_DATA or SPECS. */ | |
343 | |
344 if (SUBRP (fun)) | |
345 { | |
346 prompt_data = XSUBR (fun)->prompt; | |
347 if (!prompt_data) | |
348 { | |
349 lose: | |
350 function = wrong_type_argument (Qcommandp, function); | |
351 goto retry; | |
352 } | |
353 #if 0 /* FSFmacs */ /* Huh? Where is this used? */ | |
354 if ((EMACS_INT) prompt_data == 1) | |
355 /* Let SPECS (which is nil) be used as the args. */ | |
356 prompt_data = 0; | |
357 #endif | |
358 } | |
359 else if (COMPILED_FUNCTIONP (fun)) | |
360 { | |
361 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun); | |
362 if (! f->flags.interactivep) | |
363 goto lose; | |
364 specs = compiled_function_interactive (f); | |
365 } | |
366 else if (!CONSP (fun)) | |
367 goto lose; | |
368 else | |
369 { | |
370 Lisp_Object funcar = Fcar (fun); | |
371 | |
372 if (EQ (funcar, Qautoload)) | |
373 { | |
970 | 374 struct gcpro gcpro1; |
375 GCPRO1 (prefix); | |
376 /* do_autoload GCPROs both arguments */ | |
428 | 377 do_autoload (fun, function); |
378 UNGCPRO; | |
379 goto retry; | |
380 } | |
381 else if (EQ (funcar, Qlambda)) | |
382 { | |
383 specs = Fassq (Qinteractive, Fcdr (Fcdr (fun))); | |
384 if (NILP (specs)) | |
385 goto lose; | |
386 specs = Fcar (Fcdr (specs)); | |
387 } | |
388 else | |
389 goto lose; | |
390 } | |
391 | |
2367 | 392 /* FSFmacs makes an ALLOCA() copy of prompt_data here. |
428 | 393 We're more intelligent about this and just reset prompt_data |
394 as necessary. */ | |
395 /* If either specs or prompt_data is set to a string, use it. */ | |
396 if (!STRINGP (specs) && prompt_data == 0) | |
397 { | |
398 struct gcpro gcpro1, gcpro2, gcpro3; | |
399 int i = num_input_chars; | |
400 Lisp_Object input = specs; | |
401 | |
402 GCPRO3 (function, specs, input); | |
403 /* Compute the arg values using the user's expression. */ | |
404 specs = Feval (specs); | |
405 if (EQ (record_flag, Qlambda)) /* XEmacs addition */ | |
406 { | |
407 UNGCPRO; | |
408 return specs; | |
409 } | |
410 if (!NILP (record_flag) || i != num_input_chars) | |
411 { | |
412 /* We should record this command on the command history. */ | |
413 /* #### The following is too specific; should have general | |
414 mechanism for doing this. */ | |
415 Lisp_Object values, car; | |
416 /* Make a copy of the list of values, for the command history, | |
417 and turn them into things we can eval. */ | |
418 values = quotify_args (Fcopy_sequence (specs)); | |
419 /* If the list of args was produced with an explicit call to `list', | |
420 look for elements that were computed with (region-beginning) | |
421 or (region-end), and put those expressions into VALUES | |
422 instead of the present values. */ | |
423 if (CONSP (input)) | |
424 { | |
425 car = XCAR (input); | |
426 /* Skip through certain special forms. */ | |
427 while (EQ (car, Qlet) || EQ (car, QletX) | |
428 || EQ (car, Qsave_excursion)) | |
429 { | |
430 while (CONSP (XCDR (input))) | |
431 input = XCDR (input); | |
432 input = XCAR (input); | |
433 if (!CONSP (input)) | |
434 break; | |
435 car = XCAR (input); | |
436 } | |
437 if (EQ (car, Qlist)) | |
438 { | |
439 Lisp_Object intail, valtail; | |
440 for (intail = Fcdr (input), valtail = values; | |
441 CONSP (valtail); | |
442 intail = Fcdr (intail), valtail = Fcdr (valtail)) | |
443 { | |
444 Lisp_Object elt; | |
445 elt = Fcar (intail); | |
446 if (CONSP (elt)) | |
447 { | |
448 Lisp_Object eltcar = Fcar (elt); | |
449 if (EQ (eltcar, Qpoint) || | |
450 EQ (eltcar, Qmark) || | |
451 EQ (eltcar, Qregion_beginning) || | |
452 EQ (eltcar, Qregion_end)) | |
453 Fsetcar (valtail, Fcar (intail)); | |
454 } | |
455 } | |
456 } | |
457 } | |
458 Vcommand_history | |
459 = Fcons (Fcons (function, values), Vcommand_history); | |
460 } | |
461 single_console_state (); | |
462 RETURN_UNGCPRO (apply1 (fun, specs)); | |
463 } | |
464 | |
465 /* Here if function specifies a string to control parsing the defaults */ | |
466 | |
467 #ifdef I18N3 | |
468 /* Translate interactive prompt. */ | |
469 if (STRINGP (specs)) | |
470 { | |
471 Lisp_Object domain = Qnil; | |
472 if (COMPILED_FUNCTIONP (fun)) | |
473 domain = compiled_function_domain (XCOMPILED_FUNCTION (fun)); | |
474 if (NILP (domain)) | |
475 specs = Fgettext (specs); | |
476 else | |
477 specs = Fdgettext (domain, specs); | |
478 } | |
479 else if (prompt_data) | |
480 /* We do not have to worry about domains in this case because | |
481 prompt_data is non-nil only for built-in functions, which | |
482 always use the default domain. */ | |
483 prompt_data = gettext (prompt_data); | |
484 #endif | |
485 | |
486 /* Handle special starting chars `*' and `@' and `_'. */ | |
487 /* Note that `+' is reserved for user extensions. */ | |
488 prompt_index = 0; | |
489 { | |
490 struct gcpro gcpro1, gcpro2; | |
491 GCPRO2 (function, specs); | |
492 | |
493 for (;;) | |
494 { | |
495 if (STRINGP (specs)) | |
442 | 496 prompt_data = (const char *) XSTRING_DATA (specs); |
428 | 497 |
498 if (prompt_data[prompt_index] == '+') | |
563 | 499 syntax_error ("`+' is not used in `interactive' for ordinary commands", Qunbound); |
428 | 500 else if (prompt_data[prompt_index] == '*') |
501 { | |
502 prompt_index++; | |
503 if (!NILP (current_buffer->read_only)) | |
504 barf_if_buffer_read_only (current_buffer, -1, -1); | |
505 } | |
506 else if (prompt_data[prompt_index] == '@') | |
507 { | |
508 Lisp_Object event; | |
509 prompt_index++; | |
510 | |
511 if (!NILP (keys)) | |
512 event = extract_vector_nth_mouse_event (keys, 0); | |
513 else | |
514 #if 0 | |
515 event = extract_this_command_keys_nth_mouse_event (0); | |
516 #else | |
517 /* Doesn't work; see below */ | |
518 event = Vcurrent_mouse_event; | |
519 #endif | |
520 if (! NILP (event)) | |
521 { | |
522 Lisp_Object window = Fevent_window (event); | |
523 if (!NILP (window)) | |
524 { | |
525 if (MINI_WINDOW_P (XWINDOW (window)) | |
526 && ! (minibuf_level > 0 && EQ (window, | |
527 minibuf_window))) | |
563 | 528 invalid_operation ("Attempt to select inactive minibuffer window", Qunbound); |
428 | 529 |
530 #if 0 /* unclean! see event-stream.c */ | |
531 /* If the current buffer wants to clean up, let it. */ | |
532 if (!NILP (Vmouse_leave_buffer_hook)) | |
533 run_hook (Qmouse_leave_buffer_hook); | |
534 #endif | |
535 | |
536 Fselect_window (window, Qnil); | |
537 } | |
538 } | |
539 } | |
540 else if (prompt_data[prompt_index] == '_') | |
541 { | |
542 prompt_index++; | |
543 set_zmacs_region_stays = 1; | |
544 } | |
545 else | |
546 { | |
547 UNGCPRO; | |
548 break; | |
549 } | |
550 } | |
551 } | |
552 | |
553 /* Count the number of arguments the interactive spec would have | |
554 us give to the function. */ | |
555 argcount = 0; | |
556 { | |
442 | 557 const char *tem; |
428 | 558 for (tem = prompt_data + prompt_index; *tem; ) |
559 { | |
560 /* 'r' specifications ("point and mark as 2 numeric args") | |
561 produce *two* arguments. */ | |
562 if (*tem == 'r') | |
563 argcount += 2; | |
564 else | |
565 argcount += 1; | |
442 | 566 tem = (const char *) strchr (tem + 1, '\n'); |
428 | 567 if (!tem) |
568 break; | |
569 tem++; | |
570 } | |
571 } | |
572 | |
573 #ifdef IT_SEEMS_THAT_MLY_DOESNT_LIKE_THIS | |
574 if (!NILP (enable)) | |
575 specbind (Qenable_recursive_minibuffers, Qt); | |
576 #endif | |
577 | |
578 if (argcount == 0) | |
579 { | |
580 /* Interactive function or no arguments; just call it */ | |
581 if (EQ (record_flag, Qlambda)) | |
582 return Qnil; | |
583 if (!NILP (record_flag)) | |
584 { | |
585 Vcommand_history = Fcons (list1 (function), Vcommand_history); | |
586 } | |
587 specbind (Qcommand_debug_status, Qnil); | |
588 /* XEmacs: was fun = call0 (fun), but that's backtraced wrong */ | |
589 { | |
590 struct gcpro gcpro1; | |
591 | |
592 GCPRO1 (fun); | |
593 fun = Ffuncall (1, &fun); | |
594 UNGCPRO; | |
595 } | |
596 if (set_zmacs_region_stays) | |
597 zmacs_region_stays = 1; | |
771 | 598 return unbind_to_1 (speccount, fun); |
428 | 599 } |
600 | |
601 /* Read interactive arguments */ | |
602 { | |
603 /* args[-1] is the function to call */ | |
604 /* args[n] is the n'th argument to the function */ | |
605 int alloca_size = (1 /* function to call */ | |
606 + argcount /* actual arguments */ | |
607 + argcount /* visargs */ | |
608 + argcount /* varies */ | |
609 ); | |
610 Lisp_Object *args = alloca_array (Lisp_Object, alloca_size) + 1; | |
611 /* visargs is an array of either Qnil or user-friendlier versions (often | |
612 * strings) of previous arguments, to use in prompts for successive | |
613 * arguments. ("Often strings" because emacs didn't used to have | |
614 * format %S and prin1-to-string.) */ | |
615 Lisp_Object *visargs = args + argcount; | |
616 /* If varies[i] is non-null, the i'th argument shouldn't just have | |
617 its value in this call quoted in the command history. It should be | |
618 recorded as a call to the function named varies[i]]. */ | |
619 Lisp_Object *varies = visargs + argcount; | |
620 int arg_from_tty = 0; | |
621 REGISTER int argnum; | |
622 struct gcpro gcpro1, gcpro2; | |
623 | |
624 args[-1] = function; | |
625 for (argnum = 0; argnum < alloca_size - 1; argnum++) | |
626 args[argnum] = Qnil; | |
627 | |
628 /* Must GC-protect args[-1] (ie function) because Ffuncall doesn't */ | |
629 /* `function' itself isn't GC-protected -- use args[-1] from here | |
630 (actually, doesn't matter since Emacs GC doesn't relocate, sigh) */ | |
631 GCPRO2 (prefix, args[-1]); | |
632 gcpro2.nvars = alloca_size; | |
633 | |
634 for (argnum = 0; ; argnum++) | |
635 { | |
442 | 636 const char *prompt_start = prompt_data + prompt_index + 1; |
637 const char *prompt_limit = (const char *) strchr (prompt_start, '\n'); | |
428 | 638 int prompt_length; |
639 prompt_length = ((prompt_limit) | |
640 ? (prompt_limit - prompt_start) | |
664 | 641 : (int) strlen (prompt_start)); |
428 | 642 if (prompt_limit && prompt_limit[1] == 0) |
643 { | |
644 prompt_limit = 0; /* "sfoo:\n" -- strip tailing return */ | |
645 prompt_length -= 1; | |
646 } | |
647 /* This uses `visargs' instead of `args' so that global-set-key | |
648 prompts with "Set key C-x C-f to command: "instead of printing | |
649 event objects in there. | |
650 */ | |
867 | 651 #define PROMPT() callint_prompt ((const Ibyte *) prompt_start, prompt_length, visargs, argnum) |
428 | 652 switch (prompt_data[prompt_index]) |
653 { | |
654 case 'a': /* Symbol defined as a function */ | |
655 { | |
656 Lisp_Object tem = call1 (Qread_function, PROMPT ()); | |
657 args[argnum] = tem; | |
658 arg_from_tty = 1; | |
659 break; | |
660 } | |
661 case 'b': /* Name of existing buffer */ | |
662 { | |
663 Lisp_Object def = Fcurrent_buffer (); | |
664 if (EQ (Fselected_window (Qnil), minibuf_window)) | |
665 def = Fother_buffer (def, Qnil, Qnil); | |
666 /* read-buffer returns a buffer name, not a buffer! */ | |
667 args[argnum] = call3 (Qread_buffer, PROMPT (), def, | |
668 Qt); | |
669 arg_from_tty = 1; | |
670 break; | |
671 } | |
672 case 'B': /* Name of buffer, possibly nonexistent */ | |
673 { | |
674 /* read-buffer returns a buffer name, not a buffer! */ | |
675 args[argnum] = call2 (Qread_buffer, PROMPT (), | |
676 Fother_buffer (Fcurrent_buffer (), Qnil, | |
677 Qnil)); | |
678 arg_from_tty = 1; | |
679 break; | |
680 } | |
681 case 'c': /* Character */ | |
682 { | |
683 Lisp_Object tem; | |
684 int shadowing_speccount = specpdl_depth (); | |
685 | |
686 specbind (Qcursor_in_echo_area, Qt); | |
687 message ("%s", XSTRING_DATA (PROMPT ())); | |
688 tem = (call0 (Qread_char)); | |
689 args[argnum] = tem; | |
690 /* visargs[argnum] = Fsingle_key_description (tem); */ | |
691 /* FSF has visargs[argnum] = Fchar_to_string (tem); */ | |
692 | |
771 | 693 unbind_to (shadowing_speccount); |
428 | 694 |
695 /* #### `C-x / a' should not leave the prompt in the minibuffer. | |
696 This isn't the right fix, because (message ...) (read-char) | |
697 shouldn't leave the message there either... */ | |
698 clear_message (); | |
699 | |
700 arg_from_tty = 1; | |
701 break; | |
702 } | |
703 case 'C': /* Command: symbol with interactive function */ | |
704 { | |
705 Lisp_Object tem = call1 (Qread_command, PROMPT ()); | |
706 args[argnum] = tem; | |
707 arg_from_tty = 1; | |
708 break; | |
709 } | |
710 case 'd': /* Value of point. Does not do I/O. */ | |
711 { | |
712 args[argnum] = Fcopy_marker (current_buffer->point_marker, Qt); | |
713 varies[argnum] = Qpoint; | |
714 break; | |
715 } | |
716 case 'e': | |
717 { | |
718 Lisp_Object event; | |
719 | |
720 if (!NILP (keys)) | |
721 event = extract_vector_nth_mouse_event (keys, | |
722 mouse_event_count); | |
723 else | |
724 #if 0 | |
725 /* This doesn't quite work because this-command-keys | |
726 behaves in utterly counterintuitive ways. Sometimes | |
727 it retrieves an event back in the future, e.g. when | |
728 one command invokes another command and both are | |
729 invoked with the mouse. */ | |
730 event = (extract_this_command_keys_nth_mouse_event | |
731 (mouse_event_count)); | |
732 #else | |
733 event = Vcurrent_mouse_event; | |
734 #endif | |
735 | |
736 if (NILP (event)) | |
563 | 737 signal_error (Qinvalid_operation, |
738 "function must be bound to a mouse or misc-user event", | |
739 function); | |
428 | 740 args[argnum] = event; |
741 mouse_event_count++; | |
742 break; | |
743 } | |
744 case 'D': /* Directory name. */ | |
745 { | |
746 args[argnum] = call4 (Qread_directory_name, PROMPT (), | |
747 Qnil, /* dir */ | |
748 current_buffer->directory, /* default */ | |
749 Qt /* must-match */ | |
750 ); | |
751 arg_from_tty = 1; | |
752 break; | |
753 } | |
754 case 'f': /* Existing file name. */ | |
755 { | |
756 Lisp_Object tem = call4 (Qread_file_name, PROMPT (), | |
757 Qnil, /* dir */ | |
758 Qnil, /* default */ | |
759 Qzero /* must-match */ | |
760 ); | |
761 args[argnum] = tem; | |
762 arg_from_tty = 1; | |
763 break; | |
764 } | |
765 case 'F': /* Possibly nonexistent file name. */ | |
766 { | |
767 args[argnum] = call4 (Qread_file_name, PROMPT (), | |
768 Qnil, /* dir */ | |
769 Qnil, /* default */ | |
770 Qnil /* must-match */ | |
771 ); | |
772 arg_from_tty = 1; | |
773 break; | |
774 } | |
775 case 'i': /* Ignore: always nil. Use to skip arguments. */ | |
776 { | |
777 args[argnum] = Qnil; | |
778 break; | |
779 } | |
780 case 'k': /* Key sequence (vector of events) */ | |
781 { | |
782 struct gcpro ngcpro1; | |
783 Lisp_Object tem; | |
784 Lisp_Object key_prompt = PROMPT (); | |
785 | |
786 NGCPRO1(key_prompt); | |
787 tem = Fread_key_sequence (key_prompt, Qnil, Qnil); | |
788 NUNGCPRO; | |
789 | |
790 visargs[argnum] = Fkey_description (tem); | |
791 /* The following makes `describe-key' not work with | |
792 extent-local keymaps and such; and anyway, it's | |
793 contrary to the documentation. */ | |
794 /* args[argnum] = call1 (Qevents_to_keys, tem); */ | |
795 args[argnum] = tem; | |
796 arg_from_tty = 1; | |
797 break; | |
798 } | |
799 case 'K': /* Key sequence (vector of events), | |
800 no automatic downcasing */ | |
801 { | |
802 struct gcpro ngcpro1; | |
803 Lisp_Object tem; | |
804 Lisp_Object key_prompt = PROMPT (); | |
805 | |
806 NGCPRO1(key_prompt); | |
807 tem = Fread_key_sequence (key_prompt, Qnil, Qt); | |
808 NUNGCPRO; | |
809 | |
810 visargs[argnum] = Fkey_description (tem); | |
811 /* The following makes `describe-key' not work with | |
812 extent-local keymaps and such; and anyway, it's | |
813 contrary to the documentation. */ | |
814 /* args[argnum] = call1 (Qevents_to_keys, tem); */ | |
815 args[argnum] = tem; | |
816 arg_from_tty = 1; | |
817 break; | |
818 } | |
819 | |
820 case 'm': /* Value of mark. Does not do I/O. */ | |
821 { | |
822 args[argnum] = current_buffer->mark; | |
823 varies[argnum] = Qmark; | |
824 break; | |
825 } | |
826 case 'n': /* Read number from minibuffer. */ | |
827 { | |
828 read_number: | |
829 args[argnum] = call2 (Qread_number, PROMPT (), Qnil); | |
830 /* numbers are too boring to go on command history */ | |
831 /* arg_from_tty = 1; */ | |
832 break; | |
833 } | |
834 case 'N': /* Prefix arg, else number from minibuffer */ | |
835 { | |
836 if (NILP (prefix)) | |
837 goto read_number; | |
838 else | |
839 goto prefix_value; | |
840 } | |
841 case 'P': /* Prefix arg in raw form. Does no I/O. */ | |
842 { | |
843 args[argnum] = prefix; | |
844 break; | |
845 } | |
846 case 'p': /* Prefix arg converted to number. No I/O. */ | |
847 { | |
848 prefix_value: | |
849 { | |
850 Lisp_Object tem = Fprefix_numeric_value (prefix); | |
851 args[argnum] = tem; | |
852 } | |
853 break; | |
854 } | |
855 case 'r': /* Region, point and mark as 2 args. */ | |
856 { | |
665 | 857 Charbpos tem = check_mark (); |
428 | 858 args[argnum] = (BUF_PT (current_buffer) < tem |
859 ? Fcopy_marker (current_buffer->point_marker, Qt) | |
860 : current_buffer->mark); | |
861 varies[argnum] = Qregion_beginning; | |
862 args[++argnum] = (BUF_PT (current_buffer) > tem | |
863 ? Fcopy_marker (current_buffer->point_marker, | |
864 Qt) | |
865 : current_buffer->mark); | |
866 varies[argnum] = Qregion_end; | |
867 break; | |
868 } | |
869 case 's': /* String read via minibuffer. */ | |
870 { | |
871 args[argnum] = call1 (Qread_string, PROMPT ()); | |
872 arg_from_tty = 1; | |
873 break; | |
874 } | |
875 case 'S': /* Any symbol. */ | |
876 { | |
877 visargs[argnum] = Qnil; | |
878 for (;;) | |
879 { | |
880 Lisp_Object tem = call5 (Qcompleting_read, | |
881 PROMPT (), | |
882 Vobarray, | |
883 Qnil, | |
884 Qnil, | |
885 /* nil, or prev attempt */ | |
886 visargs[argnum]); | |
887 visargs[argnum] = tem; | |
888 /* I could use condition-case with this loser, but why bother? | |
889 * tem = Fread (tem); check-symbol-p; | |
890 */ | |
891 tem = Fintern (tem, Qnil); | |
892 args[argnum] = tem; | |
793 | 893 if (XSTRING_LENGTH (XSYMBOL (tem)->name) > 0) |
428 | 894 /* Don't accept the empty-named symbol. If the loser |
895 really wants this s/he can call completing-read | |
896 directly */ | |
897 break; | |
898 } | |
899 arg_from_tty = 1; | |
900 break; | |
901 } | |
902 case 'v': /* Variable name: user-variable-p symbol */ | |
903 { | |
904 Lisp_Object tem = call1 (Qread_variable, PROMPT ()); | |
905 args[argnum] = tem; | |
906 arg_from_tty = 1; | |
907 break; | |
908 } | |
909 case 'x': /* Lisp expression read but not evaluated */ | |
910 { | |
911 args[argnum] = call1 (Qread_expression, PROMPT ()); | |
912 /* visargs[argnum] = Fprin1_to_string (args[argnum], Qnil); */ | |
913 arg_from_tty = 1; | |
914 break; | |
915 } | |
916 case 'X': /* Lisp expression read and evaluated */ | |
917 { | |
918 Lisp_Object tem = call1 (Qread_expression, PROMPT ()); | |
919 /* visargs[argnum] = Fprin1_to_string (tem, Qnil); */ | |
920 args[argnum] = Feval (tem); | |
921 arg_from_tty = 1; | |
922 break; | |
923 } | |
924 case 'Z': /* Coding-system symbol or nil if no prefix */ | |
925 { | |
926 if (NILP (prefix)) | |
927 { | |
928 args[argnum] = Qnil; | |
929 } | |
930 else | |
931 { | |
932 args[argnum] = | |
933 call1 (Qread_non_nil_coding_system, PROMPT ()); | |
934 arg_from_tty = 1; | |
935 } | |
936 break; | |
937 } | |
938 case 'z': /* Coding-system symbol */ | |
939 { | |
940 args[argnum] = call1 (Qread_coding_system, PROMPT ()); | |
941 arg_from_tty = 1; | |
942 break; | |
943 } | |
944 | |
945 /* We have a case for `+' so we get an error | |
946 if anyone tries to define one here. */ | |
947 case '+': | |
948 default: | |
949 { | |
826 | 950 signal_ferror |
951 (Qsyntax_error, | |
952 "Invalid `interactive' control letter \"%c\" (#o%03o).", | |
953 prompt_data[prompt_index], prompt_data[prompt_index]); | |
428 | 954 } |
955 } | |
956 #undef PROMPT | |
957 if (NILP (visargs[argnum])) | |
958 visargs[argnum] = args[argnum]; | |
959 | |
960 if (!prompt_limit) | |
961 break; | |
962 if (STRINGP (specs)) | |
442 | 963 prompt_data = (const char *) XSTRING_DATA (specs); |
428 | 964 prompt_index += prompt_length + 1 + 1; /* +1 to skip spec, +1 for \n */ |
965 } | |
771 | 966 unbind_to (speccount); |
428 | 967 |
968 QUIT; | |
969 | |
970 if (EQ (record_flag, Qlambda)) | |
971 { | |
972 RETURN_UNGCPRO (Flist (argcount, args)); | |
973 } | |
974 | |
975 if (arg_from_tty || !NILP (record_flag)) | |
976 { | |
977 /* Reuse visargs as a temporary for constructing the command history */ | |
978 for (argnum = 0; argnum < argcount; argnum++) | |
979 { | |
980 if (!NILP (varies[argnum])) | |
981 visargs[argnum] = list1 (varies[argnum]); | |
982 else | |
983 visargs[argnum] = Fquote_maybe (args[argnum]); | |
984 } | |
985 Vcommand_history = Fcons (Fcons (args[-1], Flist (argcount, visargs)), | |
986 Vcommand_history); | |
987 } | |
988 | |
989 /* If we used a marker to hold point, mark, or an end of the region, | |
990 temporarily, convert it to an integer now. */ | |
991 for (argnum = 0; argnum < argcount; argnum++) | |
992 if (!NILP (varies[argnum])) | |
793 | 993 args[argnum] = make_int (marker_position (args[argnum])); |
428 | 994 |
995 single_console_state (); | |
996 specbind (Qcommand_debug_status, Qnil); | |
997 fun = Ffuncall (argcount + 1, args - 1); | |
998 UNGCPRO; | |
999 if (set_zmacs_region_stays) | |
1000 zmacs_region_stays = 1; | |
771 | 1001 return unbind_to_1 (speccount, fun); |
428 | 1002 } |
1003 } | |
1004 | |
1005 DEFUN ("prefix-numeric-value", Fprefix_numeric_value, 1, 1, 0, /* | |
444 | 1006 Return numeric meaning of raw prefix argument RAW. |
428 | 1007 A raw prefix argument is what you get from `(interactive "P")'. |
1008 Its numeric meaning is what you would get from `(interactive "p")'. | |
1009 */ | |
1010 (raw)) | |
1011 { | |
1012 if (NILP (raw)) | |
1013 return make_int (1); | |
1014 if (EQ (raw, Qminus)) | |
1015 return make_int (-1); | |
1016 if (INTP (raw)) | |
1017 return raw; | |
1018 if (CONSP (raw) && INTP (XCAR (raw))) | |
1019 return XCAR (raw); | |
1020 | |
1021 return make_int (1); | |
1022 } | |
1023 | |
1024 void | |
1025 syms_of_callint (void) | |
1026 { | |
563 | 1027 DEFSYMBOL (Qcall_interactively); |
1028 DEFSYMBOL (Qread_from_minibuffer); | |
1029 DEFSYMBOL (Qcompleting_read); | |
1030 DEFSYMBOL (Qread_file_name); | |
1031 DEFSYMBOL (Qread_directory_name); | |
1032 DEFSYMBOL (Qread_string); | |
1033 DEFSYMBOL (Qread_buffer); | |
1034 DEFSYMBOL (Qread_variable); | |
1035 DEFSYMBOL (Qread_function); | |
1036 DEFSYMBOL (Qread_command); | |
1037 DEFSYMBOL (Qread_number); | |
1038 DEFSYMBOL (Qread_expression); | |
1039 DEFSYMBOL (Qread_coding_system); | |
1040 DEFSYMBOL (Qread_non_nil_coding_system); | |
1041 DEFSYMBOL (Qevents_to_keys); | |
1042 DEFSYMBOL (Qcommand_debug_status); | |
1043 DEFSYMBOL (Qenable_recursive_minibuffers); | |
428 | 1044 |
1045 defsymbol (&QletX, "let*"); | |
563 | 1046 DEFSYMBOL (Qsave_excursion); |
428 | 1047 #if 0 /* ill-conceived */ |
563 | 1048 DEFSYMBOL (Qmouse_leave_buffer_hook); |
428 | 1049 #endif |
1050 | |
1051 DEFSUBR (Finteractive); | |
1052 DEFSUBR (Fquote_maybe); | |
1053 DEFSUBR (Fcall_interactively); | |
1054 DEFSUBR (Fprefix_numeric_value); | |
1055 } | |
1056 | |
1057 void | |
1058 vars_of_callint (void) | |
1059 { | |
1060 DEFVAR_LISP ("current-prefix-arg", &Vcurrent_prefix_arg /* | |
1061 The value of the prefix argument for this editing command. | |
1062 It may be a number, or the symbol `-' for just a minus sign as arg, | |
1063 or a list whose car is a number for just one or more C-U's | |
1064 or nil if no argument has been specified. | |
1065 This is what `(interactive "P")' returns. | |
1066 */ ); | |
1067 Vcurrent_prefix_arg = Qnil; | |
1068 | |
1069 DEFVAR_LISP ("command-history", &Vcommand_history /* | |
1070 List of recent commands that read arguments from terminal. | |
1071 Each command is represented as a form to evaluate. | |
1072 */ ); | |
1073 Vcommand_history = Qnil; | |
1074 | |
1075 DEFVAR_LISP ("command-debug-status", &Vcommand_debug_status /* | |
1076 Debugging status of current interactive command. | |
1077 Bound each time `call-interactively' is called; | |
1078 may be set by the debugger as a reminder for itself. | |
1079 */ ); | |
1080 Vcommand_debug_status = Qnil; | |
1081 | |
1082 #if 0 /* FSFmacs */ | |
1083 xxDEFVAR_LISP ("mark-even-if-inactive", &Vmark_even_if_inactive /* | |
1084 *Non-nil means you can use the mark even when inactive. | |
1085 This option makes a difference in Transient Mark mode. | |
1086 When the option is non-nil, deactivation of the mark | |
1087 turns off region highlighting, but commands that use the mark | |
1088 behave as if the mark were still active. | |
1089 */ ); | |
1090 Vmark_even_if_inactive = Qnil; | |
1091 #endif | |
1092 | |
1093 #if 0 /* Doesn't work and is totally ill-conceived anyway. */ | |
1094 xxDEFVAR_LISP ("mouse-leave-buffer-hook", &Vmouse_leave_buffer_hook /* | |
1095 Hook to run when about to switch windows with a mouse command. | |
1096 Its purpose is to give temporary modes such as Isearch mode | |
1097 a way to turn themselves off when a mouse command switches windows. | |
1098 */ ); | |
1099 Vmouse_leave_buffer_hook = Qnil; | |
1100 #endif | |
1101 } |