Coverity fix CI:203 REVERSE_INULL
[sxemacs] / src / callint.c
1 /* Call a Lisp function interactively.
2    Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
3    Copyright (C) 1995, 1996 Ben Wing.
4
5 This file is part of SXEmacs
6
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20
21 /* Synched up with: FSF 19.30, Mule 2.0. */
22
23 /* Authorship:
24
25    FSF: long ago.
26    Mly or JWZ: various changes.
27  */
28
29 #include <config.h>
30 #include "lisp.h"
31
32 #include "buffer.h"
33 #include "bytecode.h"
34 #include "commands.h"
35 #define INCLUDE_EVENTS_H_PRIVATE_SPHERE
36 #include "events/events.h"
37 #include "ui/insdel.h"
38 #include "ui/window.h"
39
40 extern Charcount num_input_chars;
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 */
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. */
59 Lisp_Object Vmouse_leave_buffer_hook, Qmouse_leave_buffer_hook;
60 #endif
61
62 Lisp_Object QletX, Qsave_excursion;
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 #if defined(MULE) || defined(FILE_CODING)
78 Lisp_Object Qread_coding_system;
79 Lisp_Object Qread_non_nil_coding_system;
80 #endif
81
82 /* ARGSUSED */
83 DEFUN("interactive", Finteractive, 0, UNEVALLED, 0,     /*
84 Specify a way of parsing arguments for interactive use of a function.
85 For example, write
86 (defun foo (arg) "Doc string" (interactive "p") ...use arg...)
87 to make ARG be the prefix argument when `foo' is called as a command.
88 The "call" to `interactive' is actually a declaration rather than a function;
89 it tells `call-interactively' how to read arguments
90 to pass to the function.
91 When actually called, `interactive' just returns nil.
92
93 The argument of `interactive' is usually a string containing a code letter
94 followed by a prompt.  (Some code letters do not use I/O to get
95 the argument and do not need prompts.)  To prompt for multiple arguments,
96 give a code letter, its prompt, a newline, and another code letter, etc.
97 Prompts are passed to format, and may use % escapes to print the
98 arguments that have already been read.
99 If the argument is not a string, it is evaluated to get a list of
100 arguments to pass to the function.
101 Just `(interactive)' means pass no args when calling interactively.
102
103 Code letters available are:
104 a -- Function name: symbol with a function definition.
105 b -- Name of existing buffer.
106 B -- Name of buffer, possibly nonexistent.
107 c -- Character.
108 C -- Command name: symbol with interactive function definition.
109 d -- Value of point as number.  Does not do I/O.
110 D -- Directory name.
111 e -- Last mouse-button or misc-user event that invoked this command.
112 If used more than once, the Nth `e' returns the Nth such event.
113 Does not do I/O.
114 f -- Existing file name.
115 F -- Possibly nonexistent file name.
116 i -- Always nil, ignore.  Use to skip arguments when interactive.
117 k -- Key sequence (a vector of events).
118 K -- Key sequence to be redefined (do not automatically down-case).
119 m -- Value of mark as number.  Does not do I/O.
120 n -- Number read using minibuffer.
121 N -- Prefix arg converted to number, or if none, do like code `n'.
122 p -- Prefix arg converted to number.  Does not do I/O.
123 P -- Prefix arg in raw form.  Does not do I/O.
124 r -- Region: point and mark as 2 numeric args, smallest first.  Does no I/O.
125 s -- Any string.
126 S -- Any symbol.
127 v -- Variable name: symbol that is user-variable-p.
128 x -- Lisp expression read but not evaluated.
129 X -- Lisp expression read and evaluated.
130 z -- Coding system. (Always nil if no Mule support.)
131 Z -- Coding system, nil if no prefix arg. (Always nil if no Mule support.)
132 In addition, if the string begins with `*'
133 then an error is signaled if the buffer is read-only.
134 This happens before reading any arguments.
135 If the string begins with `@', then the window the mouse is over is selected
136 before anything else is done.
137 If the string begins with `_', then this command will not cause the region
138 to be deactivated when it completes; that is, `zmacs-region-stays' will be
139 set to t when the command exits successfully.
140 You may use any of `@', `*' and `_' at the beginning of the string;
141 they are processed in the order that they appear.
142 */
143       (args))
144 {
145         return Qnil;
146 }
147
148 /* Originally, this was just a function -- but `custom' used a
149    garden-variety version, so why not make it a subr?  */
150 /* #### Move it to another file! */
151 DEFUN("quote-maybe", Fquote_maybe, 1, 1, 0,     /*
152 Quote EXPR if it is not self quoting.
153 */
154       (expr))
155 {
156         return ((NILP(expr)
157                  || EQ(expr, Qt)
158                  || INTP(expr)
159                  || FLOATP(expr)
160                  || CHARP(expr)
161                  || STRINGP(expr)
162                  || VECTORP(expr)
163                  || KEYWORDP(expr)
164                  || BIT_VECTORP(expr)
165                  || (CONSP(expr) && EQ(XCAR(expr), Qlambda)))
166                 ? expr : list2(Qquote, expr));
167 }
168
169 /* Modify EXPR by quotifying each element (except the first).  */
170 static Lisp_Object quotify_args(Lisp_Object expr)
171 {
172         Lisp_Object tail;
173         Lisp_Cons *ptr;
174         for (tail = expr; CONSP(tail); tail = ptr->cdr) {
175                 ptr = XCONS(tail);
176                 ptr->car = Fquote_maybe(ptr->car);
177         }
178         return expr;
179 }
180
181 static Bufpos check_mark(void)
182 {
183         Lisp_Object tem;
184
185         if (zmacs_regions && !zmacs_region_active_p)
186                 error("The region is not active now");
187
188         tem = Fmarker_buffer(current_buffer->mark);
189         if (NILP(tem) || (XBUFFER(tem) != current_buffer))
190                 error("The mark is not set now");
191
192         return marker_position(current_buffer->mark);
193 }
194
195 static Lisp_Object
196 callint_prompt(const Bufbyte * prompt_start, Bytecount prompt_length,
197                const Lisp_Object *args, int nargs)
198 {
199         Lisp_Object s = make_string(prompt_start, prompt_length);
200         struct gcpro gcpro1;
201
202         /* Fformat no longer smashes its arg vector, so no need to copy it. */
203
204         if (!strchr((char*)XSTRING_DATA(s), '%')) {
205                 return s;
206         }
207         GCPRO1(s);
208         RETURN_UNGCPRO(emacs_doprnt_string_lisp(0, s, 0, nargs, args));
209 }
210
211 /* `lambda' for RECORD-FLAG is an XEmacs addition. */
212
213 DEFUN("call-interactively", Fcall_interactively, 1, 3, 0,       /*
214 Call FUNCTION, reading args according to its interactive calling specs.