Mercurial > hg > xemacs-beta
annotate src/symbols.c @ 4502:8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
src/ChangeLog addition:
2008-08-23 Aidan Kehoe <kehoea@parhasard.net>
* eval.c (Fuser_variable_p): Moved to symbols.c
* symbols.c (Fcustom_variable_p): Moved here from custom.el.
(user_variable_alias_check_fun): Mapper function used in
`user-variable-p'.
(Fuser_variable_p): Moved here from eval.c, to allow it to examine
the variable alias chain. Expanded to check each entry in the
variable alias chain for signs of being a user variable;
documentation updated, noting the differences between GNU's
behaviour and ours (ours is a little more sensible)
(map_varalias_chain): New.
Given a C function, call it at least once for each symbol in a
symbol's varalias chain, signalling an error if there's a cycle,
and returning immediately if the function returns something other
than Qzero.
(Fdefvaralias): Correct the use of the word "alias" in the
docstring and in the argument name. Motivate this in a
comment. Add support for a DOCSTRING argument, something GNU has
too, and document this
* gc.c (vars_of_gc): Start the docstring of
`garbage-collection-messages' with an asterisk, to indicate that
it's a user variable.
lisp/ChangeLog addition:
2008-08-23 Aidan Kehoe <kehoea@parhasard.net>
* custom.el: Move #'custom-variable-p to C, since it's now called
from #'user-variable-p.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 23 Aug 2008 16:38:51 +0200 |
parents | a2af1ff1761f |
children | af95657e0bfd |
rev | line source |
---|---|
428 | 1 /* "intern" and friends -- moved here from lread.c and data.c |
2 Copyright (C) 1985-1989, 1992-1994 Free Software Foundation, Inc. | |
793 | 3 Copyright (C) 1995, 2000, 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. */ | |
23 | |
24 /* This file has been Mule-ized. */ | |
25 | |
26 /* NOTE: | |
27 | |
28 The value cell of a symbol can contain a simple value or one of | |
29 various symbol-value-magic objects. Some of these objects can | |
30 chain into other kinds of objects. Here is a table of possibilities: | |
31 | |
32 1a) simple value | |
33 1b) Qunbound | |
34 1c) symbol-value-forward, excluding Qunbound | |
35 2) symbol-value-buffer-local -> 1a or 1b or 1c | |
36 3) symbol-value-lisp-magic -> 1a or 1b or 1c | |
37 4) symbol-value-lisp-magic -> symbol-value-buffer-local -> 1a or 1b or 1c | |
38 5) symbol-value-varalias | |
39 6) symbol-value-lisp-magic -> symbol-value-varalias | |
40 | |
41 The "chain" of a symbol-value-buffer-local is its current_value slot. | |
42 | |
43 The "chain" of a symbol-value-lisp-magic is its shadowed slot, which | |
44 applies for handler types without associated handlers. | |
45 | |
46 All other fields in all the structures (including the "shadowed" slot | |
47 in a symbol-value-varalias) can *only* contain a simple value or Qunbound. | |
48 | |
49 */ | |
50 | |
51 /* #### Ugh, though, this file does awful things with symbol-value-magic | |
52 objects. This ought to be cleaned up. */ | |
53 | |
54 #include <config.h> | |
55 #include "lisp.h" | |
56 | |
57 #include "buffer.h" /* for Vbuffer_defaults */ | |
872 | 58 #include "console-impl.h" |
428 | 59 #include "elhash.h" |
60 | |
61 Lisp_Object Qad_advice_info, Qad_activate; | |
62 | |
63 Lisp_Object Qget_value, Qset_value, Qbound_predicate, Qmake_unbound; | |
64 Lisp_Object Qlocal_predicate, Qmake_local; | |
65 | |
66 Lisp_Object Qboundp, Qglobally_boundp, Qmakunbound; | |
67 Lisp_Object Qsymbol_value, Qset, Qdefault_boundp, Qdefault_value; | |
68 Lisp_Object Qset_default, Qsetq_default; | |
69 Lisp_Object Qmake_variable_buffer_local, Qmake_local_variable; | |
70 Lisp_Object Qkill_local_variable, Qkill_console_local_variable; | |
71 Lisp_Object Qsymbol_value_in_buffer, Qsymbol_value_in_console; | |
72 Lisp_Object Qlocal_variable_p; | |
73 | |
74 Lisp_Object Qconst_integer, Qconst_boolean, Qconst_object; | |
75 Lisp_Object Qconst_specifier; | |
76 Lisp_Object Qdefault_buffer, Qcurrent_buffer, Qconst_current_buffer; | |
77 Lisp_Object Qdefault_console, Qselected_console, Qconst_selected_console; | |
78 | |
79 static Lisp_Object maybe_call_magic_handler (Lisp_Object sym, | |
80 Lisp_Object funsym, | |
81 int nargs, ...); | |
82 static Lisp_Object fetch_value_maybe_past_magic (Lisp_Object sym, | |
83 Lisp_Object follow_past_lisp_magic); | |
84 static Lisp_Object *value_slot_past_magic (Lisp_Object sym); | |
85 static Lisp_Object follow_varalias_pointers (Lisp_Object symbol, | |
86 Lisp_Object follow_past_lisp_magic); | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
87 static Lisp_Object map_varalias_chain (Lisp_Object symbol, |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
88 Lisp_Object follow_past_lisp_magic, |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
89 Lisp_Object (*fn) (Lisp_Object arg)); |
428 | 90 |
91 | |
92 static Lisp_Object | |
93 mark_symbol (Lisp_Object obj) | |
94 { | |
440 | 95 Lisp_Symbol *sym = XSYMBOL (obj); |
428 | 96 |
97 mark_object (sym->value); | |
98 mark_object (sym->function); | |
793 | 99 mark_object (sym->name); |
428 | 100 if (!symbol_next (sym)) |
101 return sym->plist; | |
102 else | |
103 { | |
104 mark_object (sym->plist); | |
105 /* Mark the rest of the symbols in the obarray hash-chain */ | |
106 sym = symbol_next (sym); | |
793 | 107 return wrap_symbol (sym); |
428 | 108 } |
109 } | |
110 | |
1204 | 111 static const struct memory_description symbol_description[] = { |
440 | 112 { XD_LISP_OBJECT, offsetof (Lisp_Symbol, next) }, |
113 { XD_LISP_OBJECT, offsetof (Lisp_Symbol, name) }, | |
114 { XD_LISP_OBJECT, offsetof (Lisp_Symbol, value) }, | |
115 { XD_LISP_OBJECT, offsetof (Lisp_Symbol, function) }, | |
116 { XD_LISP_OBJECT, offsetof (Lisp_Symbol, plist) }, | |
428 | 117 { XD_END } |
118 }; | |
119 | |
442 | 120 /* Symbol plists are directly accessible, so we need to protect against |
121 invalid property list structure */ | |
122 | |
123 static Lisp_Object | |
124 symbol_getprop (Lisp_Object symbol, Lisp_Object property) | |
125 { | |
126 return external_plist_get (&XSYMBOL (symbol)->plist, property, 0, ERROR_ME); | |
127 } | |
128 | |
129 static int | |
130 symbol_putprop (Lisp_Object symbol, Lisp_Object property, Lisp_Object value) | |
131 { | |
132 external_plist_put (&XSYMBOL (symbol)->plist, property, value, 0, ERROR_ME); | |
133 return 1; | |
134 } | |
135 | |
136 static int | |
137 symbol_remprop (Lisp_Object symbol, Lisp_Object property) | |
138 { | |
139 return external_remprop (&XSYMBOL (symbol)->plist, property, 0, ERROR_ME); | |
140 } | |
141 | |
934 | 142 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS ("symbol", symbol, |
143 1, /*dumpable-flag*/ | |
144 mark_symbol, print_symbol, | |
145 0, 0, 0, symbol_description, | |
146 symbol_getprop, | |
147 symbol_putprop, | |
148 symbol_remprop, | |
149 Fsymbol_plist, | |
150 Lisp_Symbol); | |
428 | 151 |
152 /**********************************************************************/ | |
153 /* Intern */ | |
154 /**********************************************************************/ | |
155 | |
156 /* #### using a vector here is way bogus. Use a hash table instead. */ | |
157 | |
158 Lisp_Object Vobarray; | |
159 | |
160 static Lisp_Object initial_obarray; | |
161 | |
162 /* oblookup stores the bucket number here, for the sake of Funintern. */ | |
163 | |
164 static int oblookup_last_bucket_number; | |
165 | |
166 static Lisp_Object | |
167 check_obarray (Lisp_Object obarray) | |
168 { | |
169 while (!VECTORP (obarray) || XVECTOR_LENGTH (obarray) == 0) | |
170 { | |
171 /* If Vobarray is now invalid, force it to be valid. */ | |
172 if (EQ (Vobarray, obarray)) Vobarray = initial_obarray; | |
173 | |
174 obarray = wrong_type_argument (Qvectorp, obarray); | |
175 } | |
176 return obarray; | |
177 } | |
178 | |
179 Lisp_Object | |
867 | 180 intern_int (const Ibyte *str) |
428 | 181 { |
771 | 182 Bytecount len = qxestrlen (str); |
428 | 183 Lisp_Object obarray = Vobarray; |
184 | |
185 if (!VECTORP (obarray) || XVECTOR_LENGTH (obarray) == 0) | |
186 obarray = check_obarray (obarray); | |
187 | |
188 { | |
771 | 189 Lisp_Object tem = oblookup (obarray, str, len); |
428 | 190 if (SYMBOLP (tem)) |
191 return tem; | |
192 } | |
193 | |
771 | 194 return Fintern (make_string (str, len), obarray); |
195 } | |
196 | |
197 Lisp_Object | |
867 | 198 intern (const CIbyte *str) |
771 | 199 { |
867 | 200 return intern_int ((Ibyte *) str); |
428 | 201 } |
202 | |
814 | 203 Lisp_Object |
867 | 204 intern_converting_underscores_to_dashes (const CIbyte *str) |
814 | 205 { |
206 Bytecount len = strlen (str); | |
867 | 207 CIbyte *tmp = alloca_extbytes (len + 1); |
814 | 208 Bytecount i; |
209 strcpy (tmp, str); | |
210 for (i = 0; i < len; i++) | |
211 if (tmp[i] == '_') | |
212 tmp[i] = '-'; | |
867 | 213 return intern_int ((Ibyte *) tmp); |
814 | 214 } |
215 | |
428 | 216 DEFUN ("intern", Fintern, 1, 2, 0, /* |
217 Return the canonical symbol whose name is STRING. | |
218 If there is none, one is created by this function and returned. | |
444 | 219 Optional second argument OBARRAY specifies the obarray to use; |
220 it defaults to the value of the variable `obarray'. | |
428 | 221 */ |
222 (string, obarray)) | |
223 { | |
224 Lisp_Object object, *ptr; | |
793 | 225 Lisp_Object symbol; |
428 | 226 Bytecount len; |
227 | |
228 if (NILP (obarray)) obarray = Vobarray; | |
229 obarray = check_obarray (obarray); | |
230 | |
231 CHECK_STRING (string); | |
232 | |
233 len = XSTRING_LENGTH (string); | |
234 object = oblookup (obarray, XSTRING_DATA (string), len); | |
235 if (!INTP (object)) | |
236 /* Found it */ | |
237 return object; | |
238 | |
239 ptr = &XVECTOR_DATA (obarray)[XINT (object)]; | |
240 | |
241 object = Fmake_symbol (string); | |
793 | 242 symbol = object; |
428 | 243 |
244 if (SYMBOLP (*ptr)) | |
793 | 245 XSYMBOL_NEXT (symbol) = XSYMBOL (*ptr); |
428 | 246 else |
793 | 247 XSYMBOL_NEXT (symbol) = 0; |
428 | 248 *ptr = object; |
249 | |
826 | 250 if (string_byte (XSYMBOL_NAME (symbol), 0) == ':' && EQ (obarray, Vobarray)) |
428 | 251 { |
252 /* The LISP way is to put keywords in their own package, but we | |
253 don't have packages, so we do something simpler. Someday, | |
254 maybe we'll have packages and then this will be reworked. | |
255 --Stig. */ | |
793 | 256 XSYMBOL_VALUE (symbol) = object; |
428 | 257 } |
258 | |
259 return object; | |
260 } | |
261 | |
4355
a2af1ff1761f
Provide a DEFAULT argument in #'intern-soft.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4337
diff
changeset
|
262 DEFUN ("intern-soft", Fintern_soft, 1, 3, 0, /* |
428 | 263 Return the canonical symbol named NAME, or nil if none exists. |
264 NAME may be a string or a symbol. If it is a symbol, that exact | |
265 symbol is searched for. | |
444 | 266 Optional second argument OBARRAY specifies the obarray to use; |
267 it defaults to the value of the variable `obarray'. | |
4355
a2af1ff1761f
Provide a DEFAULT argument in #'intern-soft.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4337
diff
changeset
|
268 Optional third argument DEFAULT says what Lisp object to return if there is |
a2af1ff1761f
Provide a DEFAULT argument in #'intern-soft.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4337
diff
changeset
|
269 no canonical symbol named NAME, and defaults to nil. |
428 | 270 */ |
4355
a2af1ff1761f
Provide a DEFAULT argument in #'intern-soft.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4337
diff
changeset
|
271 (name, obarray, default_)) |
428 | 272 { |
273 Lisp_Object tem; | |
793 | 274 Lisp_Object string; |
428 | 275 |
276 if (NILP (obarray)) obarray = Vobarray; | |
277 obarray = check_obarray (obarray); | |
278 | |
279 if (!SYMBOLP (name)) | |
280 { | |
281 CHECK_STRING (name); | |
793 | 282 string = name; |
428 | 283 } |
284 else | |
285 string = symbol_name (XSYMBOL (name)); | |
286 | |
793 | 287 tem = oblookup (obarray, XSTRING_DATA (string), XSTRING_LENGTH (string)); |
428 | 288 if (INTP (tem) || (SYMBOLP (name) && !EQ (name, tem))) |
4355
a2af1ff1761f
Provide a DEFAULT argument in #'intern-soft.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4337
diff
changeset
|
289 return default_; |
428 | 290 else |
291 return tem; | |
292 } | |
293 | |
294 DEFUN ("unintern", Funintern, 1, 2, 0, /* | |
295 Delete the symbol named NAME, if any, from OBARRAY. | |
296 The value is t if a symbol was found and deleted, nil otherwise. | |
297 NAME may be a string or a symbol. If it is a symbol, that symbol | |
298 is deleted, if it belongs to OBARRAY--no other symbol is deleted. | |
444 | 299 OBARRAY defaults to the value of the variable `obarray'. |
428 | 300 */ |
301 (name, obarray)) | |
302 { | |
303 Lisp_Object tem; | |
793 | 304 Lisp_Object string; |
428 | 305 int hash; |
306 | |
307 if (NILP (obarray)) obarray = Vobarray; | |
308 obarray = check_obarray (obarray); | |
309 | |
310 if (SYMBOLP (name)) | |
311 string = symbol_name (XSYMBOL (name)); | |
312 else | |
313 { | |
314 CHECK_STRING (name); | |
793 | 315 string = name; |
428 | 316 } |
317 | |
793 | 318 tem = oblookup (obarray, XSTRING_DATA (string), XSTRING_LENGTH (string)); |
428 | 319 if (INTP (tem)) |
320 return Qnil; | |
321 /* If arg was a symbol, don't delete anything but that symbol itself. */ | |
322 if (SYMBOLP (name) && !EQ (name, tem)) | |
323 return Qnil; | |
324 | |
325 hash = oblookup_last_bucket_number; | |
326 | |
327 if (EQ (XVECTOR_DATA (obarray)[hash], tem)) | |
328 { | |
329 if (XSYMBOL (tem)->next) | |
793 | 330 XVECTOR_DATA (obarray)[hash] = wrap_symbol (XSYMBOL (tem)->next); |
428 | 331 else |
332 XVECTOR_DATA (obarray)[hash] = Qzero; | |
333 } | |
334 else | |
335 { | |
336 Lisp_Object tail, following; | |
337 | |
338 for (tail = XVECTOR_DATA (obarray)[hash]; | |
339 XSYMBOL (tail)->next; | |
340 tail = following) | |
341 { | |
793 | 342 following = wrap_symbol (XSYMBOL (tail)->next); |
428 | 343 if (EQ (following, tem)) |
344 { | |
345 XSYMBOL (tail)->next = XSYMBOL (following)->next; | |
346 break; | |
347 } | |
348 } | |
349 } | |
350 return Qt; | |
351 } | |
352 | |
353 /* Return the symbol in OBARRAY whose names matches the string | |
354 of SIZE characters at PTR. If there is no such symbol in OBARRAY, | |
355 return the index into OBARRAY that the string hashes to. | |
356 | |
357 Also store the bucket number in oblookup_last_bucket_number. */ | |
358 | |
359 Lisp_Object | |
867 | 360 oblookup (Lisp_Object obarray, const Ibyte *ptr, Bytecount size) |
428 | 361 { |
490 | 362 unsigned int hash, obsize; |
440 | 363 Lisp_Symbol *tail; |
428 | 364 Lisp_Object bucket; |
365 | |
366 if (!VECTORP (obarray) || | |
367 (obsize = XVECTOR_LENGTH (obarray)) == 0) | |
368 { | |
369 obarray = check_obarray (obarray); | |
370 obsize = XVECTOR_LENGTH (obarray); | |
371 } | |
372 hash = hash_string (ptr, size) % obsize; | |
373 oblookup_last_bucket_number = hash; | |
374 bucket = XVECTOR_DATA (obarray)[hash]; | |
375 if (ZEROP (bucket)) | |
376 ; | |
377 else if (!SYMBOLP (bucket)) | |
563 | 378 signal_error (Qinvalid_state, "Bad data in guts of obarray", Qunbound); /* Like CADR error message */ |
428 | 379 else |
380 for (tail = XSYMBOL (bucket); ;) | |
381 { | |
793 | 382 if (XSTRING_LENGTH (tail->name) == size && |
383 !memcmp (XSTRING_DATA (tail->name), ptr, size)) | |
428 | 384 { |
793 | 385 return wrap_symbol (tail); |
428 | 386 } |
387 tail = symbol_next (tail); | |
388 if (!tail) | |
389 break; | |
390 } | |
391 return make_int (hash); | |
392 } | |
393 | |
490 | 394 /* An excellent string hashing function. |
395 Adapted from glib's g_str_hash(). | |
396 Investigation by Karl Nelson <kenelson@ece.ucdavis.edu>. | |
397 Do a web search for "g_str_hash X31_HASH" if you want to know more. */ | |
398 unsigned int | |
867 | 399 hash_string (const Ibyte *ptr, Bytecount len) |
428 | 400 { |
490 | 401 unsigned int hash; |
402 | |
403 for (hash = 0; len; len--, ptr++) | |
404 /* (31 * hash) will probably be optimized to ((hash << 5) - hash). */ | |
405 hash = 31 * hash + *ptr; | |
406 | |
407 return hash; | |
428 | 408 } |
409 | |
410 /* Map FN over OBARRAY. The mapping is stopped when FN returns a | |
411 non-zero value. */ | |
412 void | |
413 map_obarray (Lisp_Object obarray, | |
414 int (*fn) (Lisp_Object, void *), void *arg) | |
415 { | |
416 REGISTER int i; | |
417 | |
418 CHECK_VECTOR (obarray); | |
419 for (i = XVECTOR_LENGTH (obarray) - 1; i >= 0; i--) | |
420 { | |
421 Lisp_Object tail = XVECTOR_DATA (obarray)[i]; | |
422 if (SYMBOLP (tail)) | |
423 while (1) | |
424 { | |
440 | 425 Lisp_Symbol *next; |
428 | 426 if ((*fn) (tail, arg)) |
427 return; | |
428 next = symbol_next (XSYMBOL (tail)); | |
429 if (!next) | |
430 break; | |
793 | 431 tail = wrap_symbol (next); |
428 | 432 } |
433 } | |
434 } | |
435 | |
436 static int | |
437 mapatoms_1 (Lisp_Object sym, void *arg) | |
438 { | |
439 call1 (*(Lisp_Object *)arg, sym); | |
440 return 0; | |
441 } | |
442 | |
443 DEFUN ("mapatoms", Fmapatoms, 1, 2, 0, /* | |
444 Call FUNCTION on every symbol in OBARRAY. | |
445 OBARRAY defaults to the value of `obarray'. | |
446 */ | |
447 (function, obarray)) | |
448 { | |
442 | 449 struct gcpro gcpro1; |
450 | |
428 | 451 if (NILP (obarray)) |
452 obarray = Vobarray; | |
453 obarray = check_obarray (obarray); | |
454 | |
442 | 455 GCPRO1 (obarray); |
428 | 456 map_obarray (obarray, mapatoms_1, &function); |
442 | 457 UNGCPRO; |
428 | 458 return Qnil; |
459 } | |
460 | |
461 | |
462 /**********************************************************************/ | |
463 /* Apropos */ | |
464 /**********************************************************************/ | |
465 | |
466 struct appropos_mapper_closure | |
467 { | |
468 Lisp_Object regexp; | |
469 Lisp_Object predicate; | |
470 Lisp_Object accumulation; | |
471 }; | |
472 | |
473 static int | |
474 apropos_mapper (Lisp_Object symbol, void *arg) | |
475 { | |
476 struct appropos_mapper_closure *closure = | |
477 (struct appropos_mapper_closure *) arg; | |
478 Bytecount match = fast_lisp_string_match (closure->regexp, | |
479 Fsymbol_name (symbol)); | |
480 | |
481 if (match >= 0 && | |
482 (NILP (closure->predicate) || | |
483 !NILP (call1 (closure->predicate, symbol)))) | |
484 closure->accumulation = Fcons (symbol, closure->accumulation); | |
485 | |
486 return 0; | |
487 } | |
488 | |
489 DEFUN ("apropos-internal", Fapropos_internal, 1, 2, 0, /* | |
444 | 490 Return a list of all symbols whose names contain match for REGEXP. |
491 If optional 2nd arg PREDICATE is non-nil, only symbols for which | |
492 \(funcall PREDICATE SYMBOL) returns non-nil are returned. | |
428 | 493 */ |
494 (regexp, predicate)) | |
495 { | |
496 struct appropos_mapper_closure closure; | |
442 | 497 struct gcpro gcpro1; |
428 | 498 |
499 CHECK_STRING (regexp); | |
500 | |
501 closure.regexp = regexp; | |
502 closure.predicate = predicate; | |
503 closure.accumulation = Qnil; | |
442 | 504 GCPRO1 (closure.accumulation); |
428 | 505 map_obarray (Vobarray, apropos_mapper, &closure); |
506 closure.accumulation = Fsort (closure.accumulation, Qstring_lessp); | |
442 | 507 UNGCPRO; |
428 | 508 return closure.accumulation; |
509 } | |
510 | |
511 | |
512 /* Extract and set components of symbols */ | |
513 | |
514 static void set_up_buffer_local_cache (Lisp_Object sym, | |
515 struct symbol_value_buffer_local *bfwd, | |
516 struct buffer *buf, | |
517 Lisp_Object new_alist_el, | |
518 int set_it_p); | |
519 | |
520 DEFUN ("boundp", Fboundp, 1, 1, 0, /* | |
521 Return t if SYMBOL's value is not void. | |
522 */ | |
523 (symbol)) | |
524 { | |
525 CHECK_SYMBOL (symbol); | |
526 return UNBOUNDP (find_symbol_value (symbol)) ? Qnil : Qt; | |
527 } | |
528 | |
529 DEFUN ("globally-boundp", Fglobally_boundp, 1, 1, 0, /* | |
530 Return t if SYMBOL has a global (non-bound) value. | |
531 This is for the byte-compiler; you really shouldn't be using this. | |
532 */ | |
533 (symbol)) | |
534 { | |
535 CHECK_SYMBOL (symbol); | |
536 return UNBOUNDP (top_level_value (symbol)) ? Qnil : Qt; | |
537 } | |
538 | |
539 DEFUN ("fboundp", Ffboundp, 1, 1, 0, /* | |
540 Return t if SYMBOL's function definition is not void. | |
541 */ | |
542 (symbol)) | |
543 { | |
544 CHECK_SYMBOL (symbol); | |
545 return UNBOUNDP (XSYMBOL (symbol)->function) ? Qnil : Qt; | |
546 } | |
547 | |
548 /* Return non-zero if SYM's value or function (the current contents of | |
549 which should be passed in as VAL) is constant, i.e. unsettable. */ | |
550 | |
551 static int | |
552 symbol_is_constant (Lisp_Object sym, Lisp_Object val) | |
553 { | |
554 /* #### - I wonder if it would be better to just have a new magic value | |
555 type and make nil, t, and all keywords have that same magic | |
556 constant_symbol value. This test is awfully specific about what is | |
557 constant and what isn't. --Stig */ | |
558 if (EQ (sym, Qnil) || | |
559 EQ (sym, Qt)) | |
560 return 1; | |
561 | |
562 if (SYMBOL_VALUE_MAGIC_P (val)) | |
563 switch (XSYMBOL_VALUE_MAGIC_TYPE (val)) | |
564 { | |
565 case SYMVAL_CONST_OBJECT_FORWARD: | |
566 case SYMVAL_CONST_SPECIFIER_FORWARD: | |
567 case SYMVAL_CONST_FIXNUM_FORWARD: | |
568 case SYMVAL_CONST_BOOLEAN_FORWARD: | |
569 case SYMVAL_CONST_CURRENT_BUFFER_FORWARD: | |
570 case SYMVAL_CONST_SELECTED_CONSOLE_FORWARD: | |
571 return 1; | |
572 default: break; /* Warning suppression */ | |
573 } | |
574 | |
575 /* We don't return true for keywords here because they are handled | |
576 specially by reject_constant_symbols(). */ | |
577 return 0; | |
578 } | |
579 | |
580 /* We are setting SYM's value slot (or function slot, if FUNCTION_P is | |
581 non-zero) to NEWVAL. Make sure this is allowed. | |
582 FOLLOW_PAST_LISP_MAGIC specifies whether we delve past | |
583 symbol-value-lisp-magic objects. */ | |
584 | |
585 void | |
586 reject_constant_symbols (Lisp_Object sym, Lisp_Object newval, int function_p, | |
587 Lisp_Object follow_past_lisp_magic) | |
588 { | |
589 Lisp_Object val = | |
590 (function_p ? XSYMBOL (sym)->function | |
591 : fetch_value_maybe_past_magic (sym, follow_past_lisp_magic)); | |
592 | |
593 if (SYMBOL_VALUE_MAGIC_P (val) && | |
594 XSYMBOL_VALUE_MAGIC_TYPE (val) == SYMVAL_CONST_SPECIFIER_FORWARD) | |
563 | 595 invalid_change ("Use `set-specifier' to change a specifier's value", |
596 sym); | |
428 | 597 |
996 | 598 if ( |
599 #ifdef HAVE_SHLIB | |
600 !(unloading_module && UNBOUNDP(newval)) && | |
601 #endif | |
602 (symbol_is_constant (sym, val) | |
603 || (SYMBOL_IS_KEYWORD (sym) && !EQ (newval, sym)))) | |
563 | 604 signal_error_1 (Qsetting_constant, |
605 UNBOUNDP (newval) ? list1 (sym) : list2 (sym, newval)); | |
428 | 606 } |
607 | |
608 /* Verify that it's ok to make SYM buffer-local. This rejects | |
609 constants and default-buffer-local variables. FOLLOW_PAST_LISP_MAGIC | |
610 specifies whether we delve into symbol-value-lisp-magic objects. | |
611 (Should be a symbol indicating what action is being taken; that way, | |
612 we don't delve if there's a handler for that action, but do otherwise.) */ | |
613 | |
614 static void | |
615 verify_ok_for_buffer_local (Lisp_Object sym, | |
616 Lisp_Object follow_past_lisp_magic) | |
617 { | |
618 Lisp_Object val = fetch_value_maybe_past_magic (sym, follow_past_lisp_magic); | |
619 | |
620 if (symbol_is_constant (sym, val)) | |
621 goto not_ok; | |
622 if (SYMBOL_VALUE_MAGIC_P (val)) | |
623 switch (XSYMBOL_VALUE_MAGIC_TYPE (val)) | |
624 { | |
625 case SYMVAL_DEFAULT_BUFFER_FORWARD: | |
626 case SYMVAL_DEFAULT_CONSOLE_FORWARD: | |
627 /* #### It's theoretically possible for it to be reasonable | |
628 to have both console-local and buffer-local variables, | |
629 but I don't want to consider that right now. */ | |
630 case SYMVAL_SELECTED_CONSOLE_FORWARD: | |
631 goto not_ok; | |
632 default: break; /* Warning suppression */ | |
633 } | |
634 | |
635 return; | |
636 | |
637 not_ok: | |
563 | 638 invalid_change ("Symbol may not be buffer-local", sym); |
428 | 639 } |
640 | |
641 DEFUN ("makunbound", Fmakunbound, 1, 1, 0, /* | |
642 Make SYMBOL's value be void. | |
643 */ | |
644 (symbol)) | |
645 { | |
646 Fset (symbol, Qunbound); | |
647 return symbol; | |
648 } | |
649 | |
650 DEFUN ("fmakunbound", Ffmakunbound, 1, 1, 0, /* | |
651 Make SYMBOL's function definition be void. | |
652 */ | |
653 (symbol)) | |
654 { | |
655 CHECK_SYMBOL (symbol); | |
656 reject_constant_symbols (symbol, Qunbound, 1, Qt); | |
657 XSYMBOL (symbol)->function = Qunbound; | |
658 return symbol; | |
659 } | |
660 | |
661 DEFUN ("symbol-function", Fsymbol_function, 1, 1, 0, /* | |
662 Return SYMBOL's function definition. Error if that is void. | |
663 */ | |
664 (symbol)) | |
665 { | |
666 CHECK_SYMBOL (symbol); | |
667 if (UNBOUNDP (XSYMBOL (symbol)->function)) | |
668 signal_void_function_error (symbol); | |
669 return XSYMBOL (symbol)->function; | |
670 } | |
671 | |
672 DEFUN ("symbol-plist", Fsymbol_plist, 1, 1, 0, /* | |
673 Return SYMBOL's property list. | |
674 */ | |
675 (symbol)) | |
676 { | |
677 CHECK_SYMBOL (symbol); | |
678 return XSYMBOL (symbol)->plist; | |
679 } | |
680 | |
681 DEFUN ("symbol-name", Fsymbol_name, 1, 1, 0, /* | |
682 Return SYMBOL's name, a string. | |
683 */ | |
684 (symbol)) | |
685 { | |
686 CHECK_SYMBOL (symbol); | |
793 | 687 return XSYMBOL (symbol)->name; |
428 | 688 } |
689 | |
690 DEFUN ("fset", Ffset, 2, 2, 0, /* | |
691 Set SYMBOL's function definition to NEWDEF, and return NEWDEF. | |
692 */ | |
693 (symbol, newdef)) | |
694 { | |
695 /* This function can GC */ | |
696 CHECK_SYMBOL (symbol); | |
697 reject_constant_symbols (symbol, newdef, 1, Qt); | |
698 if (!NILP (Vautoload_queue) && !UNBOUNDP (XSYMBOL (symbol)->function)) | |
699 Vautoload_queue = Fcons (Fcons (symbol, XSYMBOL (symbol)->function), | |
700 Vautoload_queue); | |
701 XSYMBOL (symbol)->function = newdef; | |
702 /* Handle automatic advice activation */ | |
703 if (CONSP (XSYMBOL (symbol)->plist) && | |
704 !NILP (Fget (symbol, Qad_advice_info, Qnil))) | |
705 { | |
706 call2 (Qad_activate, symbol, Qnil); | |
707 newdef = XSYMBOL (symbol)->function; | |
708 } | |
709 return newdef; | |
710 } | |
711 | |
712 /* FSFmacs */ | |
713 DEFUN ("define-function", Fdefine_function, 2, 2, 0, /* | |
714 Set SYMBOL's function definition to NEWDEF, and return NEWDEF. | |
715 Associates the function with the current load file, if any. | |
716 */ | |
717 (symbol, newdef)) | |
718 { | |
719 /* This function can GC */ | |
720 Ffset (symbol, newdef); | |
721 LOADHIST_ATTACH (symbol); | |
722 return newdef; | |
723 } | |
724 | |
3368 | 725 DEFUN ("subr-name", Fsubr_name, 1, 1, 0, /* |
726 Return name of function SUBR. | |
727 SUBR must be a built-in function. | |
728 */ | |
729 (subr)) | |
730 { | |
731 const char *name; | |
3497 | 732 CHECK_SUBR (subr); |
733 | |
3368 | 734 name = XSUBR (subr)->name; |
3379 | 735 return make_string ((const Ibyte *)name, strlen (name)); |
3368 | 736 } |
428 | 737 |
4336
cdc2f70d4319
Provide #'special-form-p, for the use of advice.el, perhaps other files.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3497
diff
changeset
|
738 DEFUN ("special-form-p", Fspecial_form_p, 1, 1, 0, /* |
4337
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
739 Return whether SUBR is a special form. |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
740 |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
741 A special form is a built-in function (a subr, that is a function |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
742 implemented in C, not Lisp) which does not necessarily evaluate all its |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
743 arguments. Much of the basic XEmacs Lisp syntax is implemented by means of |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
744 special forms; examples are `let', `condition-case', `defun', `setq' and so |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
745 on. |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
746 |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
747 If you intend to write a Lisp function that does not necessarily evaluate |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
748 all its arguments, the portable (across emacs variants, and across Lisp |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
749 implementations) way to go about it is to write a macro instead. See |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
750 `defmacro' and `backquote'. |
4336
cdc2f70d4319
Provide #'special-form-p, for the use of advice.el, perhaps other files.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3497
diff
changeset
|
751 */ |
cdc2f70d4319
Provide #'special-form-p, for the use of advice.el, perhaps other files.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3497
diff
changeset
|
752 (subr)) |
cdc2f70d4319
Provide #'special-form-p, for the use of advice.el, perhaps other files.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3497
diff
changeset
|
753 { |
4337
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
754 subr = indirect_function (subr, 0); |
c32e4dca0296
#'special-form-p; don't error (thank you Jerry James); flesh out docstring.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4336
diff
changeset
|
755 return (SUBRP (subr) && XSUBR (subr)->max_args == UNEVALLED) ? Qt : Qnil; |
4336
cdc2f70d4319
Provide #'special-form-p, for the use of advice.el, perhaps other files.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3497
diff
changeset
|
756 } |
cdc2f70d4319
Provide #'special-form-p, for the use of advice.el, perhaps other files.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3497
diff
changeset
|
757 |
428 | 758 DEFUN ("setplist", Fsetplist, 2, 2, 0, /* |
759 Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. | |
760 */ | |
761 (symbol, newplist)) | |
762 { | |
763 CHECK_SYMBOL (symbol); | |
764 | |
765 XSYMBOL (symbol)->plist = newplist; | |
766 return newplist; | |
767 } | |
768 | |
769 | |
770 /**********************************************************************/ | |
771 /* symbol-value */ | |
772 /**********************************************************************/ | |
773 | |
774 /* If the contents of the value cell of a symbol is one of the following | |
775 three types of objects, then the symbol is "magic" in that setting | |
776 and retrieving its value doesn't just set or retrieve the raw | |
777 contents of the value cell. None of these objects can escape to | |
778 the user level, so there is no loss of generality. | |
779 | |
780 If a symbol is "unbound", then the contents of its value cell is | |
781 Qunbound. Despite appearances, this is *not* a symbol, but is a | |
782 symbol-value-forward object. This is so that printing it results | |
783 in "INTERNAL OBJECT (XEmacs bug?)", in case it leaks to Lisp, somehow. | |
784 | |
785 Logically all of the following objects are "symbol-value-magic" | |
786 objects, and there are some games played w.r.t. this (#### this | |
787 should be cleaned up). SYMBOL_VALUE_MAGIC_P is true for all of | |
788 the object types. XSYMBOL_VALUE_MAGIC_TYPE returns the type of | |
789 symbol-value-magic object. There are more than three types | |
790 returned by this macro: in particular, symbol-value-forward | |
791 has eight subtypes, and symbol-value-buffer-local has two. See | |
792 symeval.h. | |
793 | |
794 1. symbol-value-forward | |
795 | |
796 symbol-value-forward is used for variables whose actual contents | |
797 are stored in a C variable of some sort, and for Qunbound. The | |
798 lcheader.next field (which is only used to chain together free | |
799 lcrecords) holds a pointer to the actual C variable. Included | |
800 in this type are "buffer-local" variables that are actually | |
801 stored in the buffer object itself; in this case, the "pointer" | |
802 is an offset into the struct buffer structure. | |
803 | |
804 The subtypes are as follows: | |
805 | |
806 SYMVAL_OBJECT_FORWARD: | |
807 (declare with DEFVAR_LISP) | |
808 The value of this variable is stored in a C variable of type | |
809 "Lisp_Object". Setting this variable sets the C variable. | |
810 Accessing this variable retrieves a value from the C variable. | |
811 These variables can be buffer-local -- in this case, the | |
812 raw symbol-value field gets converted into a | |
813 symbol-value-buffer-local, whose "current_value" slot contains | |
814 the symbol-value-forward. (See below.) | |
815 | |
816 SYMVAL_FIXNUM_FORWARD: | |
458 | 817 (declare with DEFVAR_INT) |
818 Similar to SYMVAL_OBJECT_FORWARD except that the C variable | |
819 is of type "Fixnum", a typedef for "EMACS_INT", and the corresponding | |
820 lisp variable is always the corresponding integer. | |
821 | |
428 | 822 SYMVAL_BOOLEAN_FORWARD: |
458 | 823 (declare with DEFVAR_BOOL) |
428 | 824 Similar to SYMVAL_OBJECT_FORWARD except that the C variable |
458 | 825 is of type "int" and is a boolean. |
428 | 826 |
827 SYMVAL_CONST_OBJECT_FORWARD: | |
828 SYMVAL_CONST_FIXNUM_FORWARD: | |
829 SYMVAL_CONST_BOOLEAN_FORWARD: | |
830 (declare with DEFVAR_CONST_LISP, DEFVAR_CONST_INT, or | |
831 DEFVAR_CONST_BOOL) | |
832 Similar to SYMVAL_OBJECT_FORWARD, SYMVAL_FIXNUM_FORWARD, or | |
833 SYMVAL_BOOLEAN_FORWARD, respectively, except that the value cannot | |
834 be changed. | |
835 | |
836 SYMVAL_CONST_SPECIFIER_FORWARD: | |
837 (declare with DEFVAR_SPECIFIER) | |
440 | 838 Exactly like SYMVAL_CONST_OBJECT_FORWARD except that the error |
839 message you get when attempting to set the value says to use | |
428 | 840 `set-specifier' instead. |
841 | |
842 SYMVAL_CURRENT_BUFFER_FORWARD: | |
843 (declare with DEFVAR_BUFFER_LOCAL) | |
844 This is used for built-in buffer-local variables -- i.e. | |
845 Lisp variables whose value is stored in the "struct buffer". | |
846 Variables of this sort always forward into C "Lisp_Object" | |
847 fields (although there's no reason in principle that other | |
848 types for ints and booleans couldn't be added). Note that | |
849 some of these variables are automatically local in each | |
850 buffer, while some are only local when they become set | |
851 (similar to `make-variable-buffer-local'). In these latter | |
852 cases, of course, the default value shows through in all | |
853 buffers in which the variable doesn't have a local value. | |
854 This is implemented by making sure the "struct buffer" field | |
855 always contains the correct value (whether it's local or | |
856 a default) and maintaining a mask in the "struct buffer" | |
857 indicating which fields are local. When `set-default' is | |
858 called on a variable that's not always local to all buffers, | |
859 it loops through each buffer and sets the corresponding | |
860 field in each buffer without a local value for the field, | |
861 according to the mask. | |
862 | |
863 Calling `make-local-variable' on a variable of this sort | |
864 only has the effect of maybe changing the current buffer's mask. | |
865 Calling `make-variable-buffer-local' on a variable of this | |
866 sort has no effect at all. | |
867 | |
868 SYMVAL_CONST_CURRENT_BUFFER_FORWARD: | |
869 (declare with DEFVAR_CONST_BUFFER_LOCAL) | |
870 Same as SYMVAL_CURRENT_BUFFER_FORWARD except that the | |
871 value cannot be set. | |
872 | |
873 SYMVAL_DEFAULT_BUFFER_FORWARD: | |
874 (declare with DEFVAR_BUFFER_DEFAULTS) | |
875 This is used for the Lisp variables that contain the | |
876 default values of built-in buffer-local variables. Setting | |
877 or referencing one of these variables forwards into a slot | |
878 in the special struct buffer Vbuffer_defaults. | |
879 | |
880 SYMVAL_UNBOUND_MARKER: | |
881 This is used for only one object, Qunbound. | |
882 | |
883 SYMVAL_SELECTED_CONSOLE_FORWARD: | |
884 (declare with DEFVAR_CONSOLE_LOCAL) | |
885 This is used for built-in console-local variables -- i.e. | |
886 Lisp variables whose value is stored in the "struct console". | |
887 These work just like built-in buffer-local variables. | |
888 However, calling `make-local-variable' or | |
889 `make-variable-buffer-local' on one of these variables | |
890 is currently disallowed because that would entail having | |
891 both console-local and buffer-local variables, which is | |
892 trickier to implement. | |
893 | |
894 SYMVAL_CONST_SELECTED_CONSOLE_FORWARD: | |
895 (declare with DEFVAR_CONST_CONSOLE_LOCAL) | |
896 Same as SYMVAL_SELECTED_CONSOLE_FORWARD except that the | |
897 value cannot be set. | |
898 | |
899 SYMVAL_DEFAULT_CONSOLE_FORWARD: | |
900 (declare with DEFVAR_CONSOLE_DEFAULTS) | |
901 This is used for the Lisp variables that contain the | |
902 default values of built-in console-local variables. Setting | |
903 or referencing one of these variables forwards into a slot | |
904 in the special struct console Vconsole_defaults. | |
905 | |
906 | |
907 2. symbol-value-buffer-local | |
908 | |
909 symbol-value-buffer-local is used for variables that have had | |
910 `make-local-variable' or `make-variable-buffer-local' applied | |
911 to them. This object contains an alist mapping buffers to | |
912 values. In addition, the object contains a "current value", | |
913 which is the value in some buffer. Whenever you access the | |
914 variable with `symbol-value' or set it with `set' or `setq', | |
915 things are switched around so that the "current value" | |
916 refers to the current buffer, if it wasn't already. This | |
917 way, repeated references to a variable in the same buffer | |
918 are almost as efficient as if the variable weren't buffer | |
919 local. Note that the alist may not be up-to-date w.r.t. | |
920 the buffer whose value is current, as the "current value" | |
921 cache is normally only flushed into the alist when the | |
922 buffer it refers to changes. | |
923 | |
924 Note also that it is possible for `make-local-variable' | |
925 or `make-variable-buffer-local' to be called on a variable | |
926 that forwards into a C variable (i.e. a variable whose | |
927 value cell is a symbol-value-forward). In this case, | |
928 the value cell becomes a symbol-value-buffer-local (as | |
929 always), and the symbol-value-forward moves into | |
930 the "current value" cell in this object. Also, in | |
931 this case the "current value" *always* refers to the | |
932 current buffer, so that the values of the C variable | |
933 always is the correct value for the current buffer. | |
934 set_buffer_internal() automatically updates the current-value | |
935 cells of all buffer-local variables that forward into C | |
936 variables. (There is a list of all buffer-local variables | |
937 that is maintained for this and other purposes.) | |
938 | |
939 Note that only certain types of `symbol-value-forward' objects | |
940 can find their way into the "current value" cell of a | |
941 `symbol-value-buffer-local' object: SYMVAL_OBJECT_FORWARD, | |
942 SYMVAL_FIXNUM_FORWARD, SYMVAL_BOOLEAN_FORWARD, and | |
943 SYMVAL_UNBOUND_MARKER. The SYMVAL_CONST_*_FORWARD cannot | |
944 be buffer-local because they are unsettable; | |
945 SYMVAL_DEFAULT_*_FORWARD cannot be buffer-local because that | |
946 makes no sense; making SYMVAL_CURRENT_BUFFER_FORWARD buffer-local | |
947 does not have much of an effect (it's already buffer-local); and | |
948 SYMVAL_SELECTED_CONSOLE_FORWARD cannot be buffer-local because | |
949 that's not currently implemented. | |
950 | |
951 | |
952 3. symbol-value-varalias | |
953 | |
954 A symbol-value-varalias object is used for variables that | |
955 are aliases for other variables. This object contains | |
956 the symbol that this variable is aliased to. | |
957 symbol-value-varalias objects cannot occur anywhere within | |
958 a symbol-value-buffer-local object, and most of the | |
959 low-level functions below do not accept them; you need | |
960 to call follow_varalias_pointers to get the actual | |
961 symbol to operate on. */ | |
962 | |
1204 | 963 static const struct memory_description symbol_value_buffer_local_description[] = { |
964 { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, default_value) }, | |
965 { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, current_value) }, | |
966 { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, current_buffer) }, | |
967 { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, current_alist_element) }, | |
968 { XD_END } | |
969 }; | |
970 | |
428 | 971 static Lisp_Object |
972 mark_symbol_value_buffer_local (Lisp_Object obj) | |
973 { | |
974 struct symbol_value_buffer_local *bfwd; | |
975 | |
800 | 976 #ifdef ERROR_CHECK_TYPES |
428 | 977 assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_BUFFER_LOCAL || |
978 XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_SOME_BUFFER_LOCAL); | |
979 #endif | |
980 | |
981 bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (obj); | |
982 mark_object (bfwd->default_value); | |
983 mark_object (bfwd->current_value); | |
984 mark_object (bfwd->current_buffer); | |
985 return bfwd->current_alist_element; | |
986 } | |
987 | |
1204 | 988 |
989 static const struct memory_description symbol_value_lisp_magic_description[] = { | |
990 { XD_LISP_OBJECT_ARRAY, offsetof (struct symbol_value_lisp_magic, handler), MAGIC_HANDLER_MAX }, | |
991 { XD_LISP_OBJECT_ARRAY, offsetof (struct symbol_value_lisp_magic, harg), MAGIC_HANDLER_MAX }, | |
992 { XD_LISP_OBJECT, offsetof (struct symbol_value_lisp_magic, shadowed) }, | |
993 { XD_END } | |
994 }; | |
995 | |
428 | 996 static Lisp_Object |
997 mark_symbol_value_lisp_magic (Lisp_Object obj) | |
998 { | |
999 struct symbol_value_lisp_magic *bfwd; | |
1000 int i; | |
1001 | |
1002 assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_LISP_MAGIC); | |
1003 | |
1004 bfwd = XSYMBOL_VALUE_LISP_MAGIC (obj); | |
1005 for (i = 0; i < MAGIC_HANDLER_MAX; i++) | |
1006 { | |
1007 mark_object (bfwd->handler[i]); | |
1008 mark_object (bfwd->harg[i]); | |
1009 } | |
1010 return bfwd->shadowed; | |
1011 } | |
1012 | |
1204 | 1013 static const struct memory_description symbol_value_varalias_description[] = { |
1014 { XD_LISP_OBJECT, offsetof (struct symbol_value_varalias, aliasee) }, | |
1015 { XD_LISP_OBJECT, offsetof (struct symbol_value_varalias, shadowed) }, | |
1016 { XD_END } | |
1017 }; | |
1018 | |
428 | 1019 static Lisp_Object |
1020 mark_symbol_value_varalias (Lisp_Object obj) | |
1021 { | |
1022 struct symbol_value_varalias *bfwd; | |
1023 | |
1024 assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_VARALIAS); | |
1025 | |
1026 bfwd = XSYMBOL_VALUE_VARALIAS (obj); | |
1027 mark_object (bfwd->shadowed); | |
1028 return bfwd->aliasee; | |
1029 } | |
1030 | |
1031 /* Should never, ever be called. (except by an external debugger) */ | |
1032 void | |
2286 | 1033 print_symbol_value_magic (Lisp_Object obj, Lisp_Object printcharfun, |
1034 int UNUSED (escapeflag)) | |
428 | 1035 { |
800 | 1036 write_fmt_string (printcharfun, |
1037 "#<INTERNAL OBJECT (XEmacs bug?) (%s type %d) 0x%lx>", | |
1038 XRECORD_LHEADER_IMPLEMENTATION (obj)->name, | |
1039 XSYMBOL_VALUE_MAGIC_TYPE (obj), | |
1040 (long) XPNTR (obj)); | |
428 | 1041 } |
1042 | |
1204 | 1043 static const struct memory_description symbol_value_forward_description[] = { |
428 | 1044 { XD_END } |
1045 }; | |
1046 | |
934 | 1047 DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-forward", |
1048 symbol_value_forward, | |
1049 1, /*dumpable-flag*/ | |
1050 0, | |
1051 print_symbol_value_magic, 0, 0, 0, | |
1052 symbol_value_forward_description, | |
1053 struct symbol_value_forward); | |
1054 | |
1055 DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-buffer-local", | |
1056 symbol_value_buffer_local, | |
1057 1, /*dumpable-flag*/ | |
1058 mark_symbol_value_buffer_local, | |
1059 print_symbol_value_magic, 0, 0, 0, | |
1060 symbol_value_buffer_local_description, | |
1061 struct symbol_value_buffer_local); | |
1062 | |
1063 DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-lisp-magic", | |
1064 symbol_value_lisp_magic, | |
1065 1, /*dumpable-flag*/ | |
1066 mark_symbol_value_lisp_magic, | |
1067 print_symbol_value_magic, 0, 0, 0, | |
1068 symbol_value_lisp_magic_description, | |
1069 struct symbol_value_lisp_magic); | |
1070 | |
1071 DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-varalias", | |
1072 symbol_value_varalias, | |
1073 1, /*dumpable-flag*/ | |
1074 mark_symbol_value_varalias, | |
1075 print_symbol_value_magic, 0, 0, 0, | |
1076 symbol_value_varalias_description, | |
1077 struct symbol_value_varalias); | |
1078 | |
428 | 1079 |
1080 /* Getting and setting values of symbols */ | |
1081 | |
1082 /* Given the raw contents of a symbol value cell, return the Lisp value of | |
1083 the symbol. However, VALCONTENTS cannot be a symbol-value-buffer-local, | |
1084 symbol-value-lisp-magic, or symbol-value-varalias. | |
1085 | |
1086 BUFFER specifies a buffer, and is used for built-in buffer-local | |
1087 variables, which are of type SYMVAL_CURRENT_BUFFER_FORWARD. | |
1088 Note that such variables are never encapsulated in a | |
1089 symbol-value-buffer-local structure. | |
1090 | |
1091 CONSOLE specifies a console, and is used for built-in console-local | |
1092 variables, which are of type SYMVAL_SELECTED_CONSOLE_FORWARD. | |
1093 Note that such variables are (currently) never encapsulated in a | |
1094 symbol-value-buffer-local structure. | |
1095 */ | |
1096 | |
1097 static Lisp_Object | |
1098 do_symval_forwarding (Lisp_Object valcontents, struct buffer *buffer, | |
1099 struct console *console) | |
1100 { | |
442 | 1101 const struct symbol_value_forward *fwd; |
428 | 1102 |
1103 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
1104 return valcontents; | |
1105 | |
1106 fwd = XSYMBOL_VALUE_FORWARD (valcontents); | |
1107 switch (fwd->magic.type) | |
1108 { | |
1109 case SYMVAL_FIXNUM_FORWARD: | |
1110 case SYMVAL_CONST_FIXNUM_FORWARD: | |
458 | 1111 return make_int (*((Fixnum *)symbol_value_forward_forward (fwd))); |
428 | 1112 |
1113 case SYMVAL_BOOLEAN_FORWARD: | |
1114 case SYMVAL_CONST_BOOLEAN_FORWARD: | |
1115 return *((int *)symbol_value_forward_forward (fwd)) ? Qt : Qnil; | |
1116 | |
1117 case SYMVAL_OBJECT_FORWARD: | |
1118 case SYMVAL_CONST_OBJECT_FORWARD: | |
1119 case SYMVAL_CONST_SPECIFIER_FORWARD: | |
1120 return *((Lisp_Object *)symbol_value_forward_forward (fwd)); | |
1121 | |
1122 case SYMVAL_DEFAULT_BUFFER_FORWARD: | |
1123 return (*((Lisp_Object *)((char *) XBUFFER (Vbuffer_defaults) | |
1124 + ((char *)symbol_value_forward_forward (fwd) | |
1125 - (char *)&buffer_local_flags)))); | |
1126 | |
1127 | |
1128 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
1129 case SYMVAL_CONST_CURRENT_BUFFER_FORWARD: | |
1130 assert (buffer); | |
1131 return (*((Lisp_Object *)((char *)buffer | |
1132 + ((char *)symbol_value_forward_forward (fwd) | |
1133 - (char *)&buffer_local_flags)))); | |
1134 | |
1135 case SYMVAL_DEFAULT_CONSOLE_FORWARD: | |
1136 return (*((Lisp_Object *)((char *) XCONSOLE (Vconsole_defaults) | |
1137 + ((char *)symbol_value_forward_forward (fwd) | |
1138 - (char *)&console_local_flags)))); | |
1139 | |
1140 case SYMVAL_SELECTED_CONSOLE_FORWARD: | |
1141 case SYMVAL_CONST_SELECTED_CONSOLE_FORWARD: | |
1142 assert (console); | |
1143 return (*((Lisp_Object *)((char *)console | |
1144 + ((char *)symbol_value_forward_forward (fwd) | |
1145 - (char *)&console_local_flags)))); | |
1146 | |
1147 case SYMVAL_UNBOUND_MARKER: | |
1148 return valcontents; | |
1149 | |
1150 default: | |
2500 | 1151 ABORT (); |
428 | 1152 } |
1153 return Qnil; /* suppress compiler warning */ | |
1154 } | |
1155 | |
1156 /* Set the value of default-buffer-local variable SYM to VALUE. */ | |
1157 | |
1158 static void | |
1159 set_default_buffer_slot_variable (Lisp_Object sym, | |
1160 Lisp_Object value) | |
1161 { | |
1162 /* Handle variables like case-fold-search that have special slots in | |
1163 the buffer. Make them work apparently like buffer_local variables. | |
1164 */ | |
1165 /* At this point, the value cell may not contain a symbol-value-varalias | |
1166 or symbol-value-buffer-local, and if there's a handler, we should | |
1167 have already called it. */ | |
1168 Lisp_Object valcontents = fetch_value_maybe_past_magic (sym, Qt); | |
442 | 1169 const struct symbol_value_forward *fwd |
428 | 1170 = XSYMBOL_VALUE_FORWARD (valcontents); |
1171 int offset = ((char *) symbol_value_forward_forward (fwd) | |
1172 - (char *) &buffer_local_flags); | |
1173 int mask = XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd))); | |
1174 int (*magicfun) (Lisp_Object simm, Lisp_Object *val, Lisp_Object in_object, | |
1175 int flags) = symbol_value_forward_magicfun (fwd); | |
1176 | |
1177 *((Lisp_Object *) (offset + (char *) XBUFFER (Vbuffer_defaults))) | |
1178 = value; | |
1179 | |
1180 if (mask > 0) /* Not always per-buffer */ | |
1181 { | |
1182 /* Set value in each buffer which hasn't shadowed the default */ | |
1183 LIST_LOOP_2 (elt, Vbuffer_alist) | |
1184 { | |
1185 struct buffer *b = XBUFFER (XCDR (elt)); | |
1186 if (!(b->local_var_flags & mask)) | |
1187 { | |
1188 if (magicfun) | |
771 | 1189 magicfun (sym, &value, wrap_buffer (b), 0); |
428 | 1190 *((Lisp_Object *) (offset + (char *) b)) = value; |
1191 } | |
1192 } | |
1193 } | |
1194 } | |
1195 | |
1196 /* Set the value of default-console-local variable SYM to VALUE. */ | |
1197 | |
1198 static void | |
1199 set_default_console_slot_variable (Lisp_Object sym, | |
1200 Lisp_Object value) | |
1201 { | |
1202 /* Handle variables like case-fold-search that have special slots in | |
1203 the console. Make them work apparently like console_local variables. | |
1204 */ | |
1205 /* At this point, the value cell may not contain a symbol-value-varalias | |
1206 or symbol-value-buffer-local, and if there's a handler, we should | |
1207 have already called it. */ | |
1208 Lisp_Object valcontents = fetch_value_maybe_past_magic (sym, Qt); | |
442 | 1209 const struct symbol_value_forward *fwd |
428 | 1210 = XSYMBOL_VALUE_FORWARD (valcontents); |
1211 int offset = ((char *) symbol_value_forward_forward (fwd) | |
1212 - (char *) &console_local_flags); | |
1213 int mask = XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd))); | |
1214 int (*magicfun) (Lisp_Object simm, Lisp_Object *val, Lisp_Object in_object, | |
1215 int flags) = symbol_value_forward_magicfun (fwd); | |
1216 | |
1217 *((Lisp_Object *) (offset + (char *) XCONSOLE (Vconsole_defaults))) | |
1218 = value; | |
1219 | |
1220 if (mask > 0) /* Not always per-console */ | |
1221 { | |
1222 /* Set value in each console which hasn't shadowed the default */ | |
1223 LIST_LOOP_2 (console, Vconsole_list) | |
1224 { | |
1225 struct console *d = XCONSOLE (console); | |
1226 if (!(d->local_var_flags & mask)) | |
1227 { | |
1228 if (magicfun) | |
1229 magicfun (sym, &value, console, 0); | |
1230 *((Lisp_Object *) (offset + (char *) d)) = value; | |
1231 } | |
1232 } | |
1233 } | |
1234 } | |
1235 | |
1236 /* Store NEWVAL into SYM. | |
1237 | |
1238 SYM's value slot may *not* be types (5) or (6) above, | |
1239 i.e. no symbol-value-varalias objects. (You should have | |
1240 forwarded past all of these.) | |
1241 | |
1242 SYM should not be an unsettable symbol or a symbol with | |
1243 a magic `set-value' handler (unless you want to explicitly | |
1244 ignore this handler). | |
1245 | |
1246 OVALUE is the current value of SYM, but forwarded past any | |
1247 symbol-value-buffer-local and symbol-value-lisp-magic objects. | |
1248 (i.e. if SYM is a symbol-value-buffer-local, OVALUE should be | |
1249 the contents of its current-value cell.) NEWVAL may only be | |
1250 a simple value or Qunbound. If SYM is a symbol-value-buffer-local, | |
1251 this function will only modify its current-value cell, which should | |
1252 already be set up to point to the current buffer. | |
1253 */ | |
1254 | |
1255 static void | |
1256 store_symval_forwarding (Lisp_Object sym, Lisp_Object ovalue, | |
1257 Lisp_Object newval) | |
1258 { | |
1259 if (!SYMBOL_VALUE_MAGIC_P (ovalue) || UNBOUNDP (ovalue)) | |
1260 { | |
1261 Lisp_Object *store_pointer = value_slot_past_magic (sym); | |
1262 | |
1263 if (SYMBOL_VALUE_BUFFER_LOCAL_P (*store_pointer)) | |
1264 store_pointer = | |
1265 &XSYMBOL_VALUE_BUFFER_LOCAL (*store_pointer)->current_value; | |
1266 | |
1267 assert (UNBOUNDP (*store_pointer) | |
1268 || !SYMBOL_VALUE_MAGIC_P (*store_pointer)); | |
1269 *store_pointer = newval; | |
1270 } | |
1271 else | |
1272 { | |
442 | 1273 const struct symbol_value_forward *fwd = XSYMBOL_VALUE_FORWARD (ovalue); |
428 | 1274 int (*magicfun) (Lisp_Object simm, Lisp_Object *val, |
1275 Lisp_Object in_object, int flags) | |
1276 = symbol_value_forward_magicfun (fwd); | |
1277 | |
1278 switch (XSYMBOL_VALUE_MAGIC_TYPE (ovalue)) | |
1279 { | |
1280 case SYMVAL_FIXNUM_FORWARD: | |
1281 CHECK_INT (newval); | |
1282 if (magicfun) | |
1283 magicfun (sym, &newval, Qnil, 0); | |
458 | 1284 *((Fixnum *) symbol_value_forward_forward (fwd)) = XINT (newval); |
428 | 1285 return; |
1286 | |
1287 case SYMVAL_BOOLEAN_FORWARD: | |
1288 if (magicfun) | |
1289 magicfun (sym, &newval, Qnil, 0); | |
1290 *((int *) symbol_value_forward_forward (fwd)) | |
1291 = !NILP (newval); | |
1292 return; | |
1293 | |
1294 case SYMVAL_OBJECT_FORWARD: | |
1295 if (magicfun) | |
1296 magicfun (sym, &newval, Qnil, 0); | |
1297 *((Lisp_Object *) symbol_value_forward_forward (fwd)) = newval; | |
1298 return; | |
1299 | |
1300 case SYMVAL_DEFAULT_BUFFER_FORWARD: | |
1301 set_default_buffer_slot_variable (sym, newval); | |
1302 return; | |
1303 | |
1304 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
1305 if (magicfun) | |
771 | 1306 magicfun (sym, &newval, wrap_buffer (current_buffer), 0); |
428 | 1307 *((Lisp_Object *) ((char *) current_buffer |
1308 + ((char *) symbol_value_forward_forward (fwd) | |
1309 - (char *) &buffer_local_flags))) | |
1310 = newval; | |
1311 return; | |
1312 | |
1313 case SYMVAL_DEFAULT_CONSOLE_FORWARD: | |
1314 set_default_console_slot_variable (sym, newval); | |
1315 return; | |
1316 | |
1317 case SYMVAL_SELECTED_CONSOLE_FORWARD: | |
1318 if (magicfun) | |
1319 magicfun (sym, &newval, Vselected_console, 0); | |
1320 *((Lisp_Object *) ((char *) XCONSOLE (Vselected_console) | |
1321 + ((char *) symbol_value_forward_forward (fwd) | |
1322 - (char *) &console_local_flags))) | |
1323 = newval; | |
1324 return; | |
1325 | |
1326 default: | |
2500 | 1327 ABORT (); |
428 | 1328 } |
1329 } | |
1330 } | |
1331 | |
1332 /* Given a per-buffer variable SYMBOL and its raw value-cell contents | |
1333 BFWD, locate and return a pointer to the element in BUFFER's | |
1334 local_var_alist for SYMBOL. The return value will be Qnil if | |
1335 BUFFER does not have its own value for SYMBOL (i.e. the default | |
1336 value is seen in that buffer). | |
1337 */ | |
1338 | |
1339 static Lisp_Object | |
1340 buffer_local_alist_element (struct buffer *buffer, Lisp_Object symbol, | |
1341 struct symbol_value_buffer_local *bfwd) | |
1342 { | |
1343 if (!NILP (bfwd->current_buffer) && | |
1344 XBUFFER (bfwd->current_buffer) == buffer) | |
1345 /* This is just an optimization of the below. */ | |
1346 return bfwd->current_alist_element; | |
1347 else | |
1348 return assq_no_quit (symbol, buffer->local_var_alist); | |
1349 } | |
1350 | |
1351 /* [Remember that the slot that mirrors CURRENT-VALUE in the | |
1352 symbol-value-buffer-local of a per-buffer variable -- i.e. the | |
1353 slot in CURRENT-BUFFER's local_var_alist, or the DEFAULT-VALUE | |
1354 slot -- may be out of date.] | |
1355 | |
1356 Write out any cached value in buffer-local variable SYMBOL's | |
1357 buffer-local structure, which is passed in as BFWD. | |
1358 */ | |
1359 | |
1360 static void | |
2286 | 1361 write_out_buffer_local_cache (Lisp_Object UNUSED (symbol), |
428 | 1362 struct symbol_value_buffer_local *bfwd) |
1363 { | |
1364 if (!NILP (bfwd->current_buffer)) | |
1365 { | |
1366 /* We pass 0 for BUFFER because only SYMVAL_CURRENT_BUFFER_FORWARD | |
1367 uses it, and that type cannot be inside a symbol-value-buffer-local */ | |
1368 Lisp_Object cval = do_symval_forwarding (bfwd->current_value, 0, 0); | |
1369 if (NILP (bfwd->current_alist_element)) | |
1370 /* current_value may be updated more recently than default_value */ | |
1371 bfwd->default_value = cval; | |
1372 else | |
1373 Fsetcdr (bfwd->current_alist_element, cval); | |
1374 } | |
1375 } | |
1376 | |
1377 /* SYM is a buffer-local variable, and BFWD is its buffer-local structure. | |
1378 Set up BFWD's cache for validity in buffer BUF. This assumes that | |
1379 the cache is currently in a consistent state (this can include | |
1380 not having any value cached, if BFWD->CURRENT_BUFFER is nil). | |
1381 | |
1382 If the cache is already set up for BUF, this function does nothing | |
1383 at all. | |
1384 | |
1385 Otherwise, if SYM forwards out to a C variable, this also forwards | |
1386 SYM's value in BUF out to the variable. Therefore, you generally | |
1387 only want to call this when BUF is, or is about to become, the | |
1388 current buffer. | |
1389 | |
1390 (Otherwise, you can just retrieve the value without changing the | |
1391 cache, at the expense of slower retrieval.) | |
1392 */ | |
1393 | |
1394 static void | |
1395 set_up_buffer_local_cache (Lisp_Object sym, | |
1396 struct symbol_value_buffer_local *bfwd, | |
1397 struct buffer *buf, | |
1398 Lisp_Object new_alist_el, | |
1399 int set_it_p) | |
1400 { | |
1401 Lisp_Object new_val; | |
1402 | |
1403 if (!NILP (bfwd->current_buffer) | |
1404 && buf == XBUFFER (bfwd->current_buffer)) | |
1405 /* Cache is already set up. */ | |
1406 return; | |
1407 | |
1408 /* Flush out the old cache. */ | |
1409 write_out_buffer_local_cache (sym, bfwd); | |
1410 | |
1411 /* Retrieve the new alist element and new value. */ | |
1412 if (NILP (new_alist_el) | |
1413 && set_it_p) | |
1414 new_alist_el = buffer_local_alist_element (buf, sym, bfwd); | |
1415 | |
1416 if (NILP (new_alist_el)) | |
1417 new_val = bfwd->default_value; | |
1418 else | |
1419 new_val = Fcdr (new_alist_el); | |
1420 | |
1421 bfwd->current_alist_element = new_alist_el; | |
793 | 1422 bfwd->current_buffer = wrap_buffer (buf); |
428 | 1423 |
1424 /* Now store the value into the current-value slot. | |
1425 We don't simply write it there, because the current-value | |
1426 slot might be a forwarding pointer, in which case we need | |
1427 to instead write the value into the C variable. | |
1428 | |
1429 We might also want to call a magic function. | |
1430 | |
1431 So instead, we call this function. */ | |
1432 store_symval_forwarding (sym, bfwd->current_value, new_val); | |
1433 } | |
1434 | |
446 | 1435 |
1436 /* SYM is a buffer-local variable, and BFWD is its buffer-local structure. | |
1437 Flush the cache. BFWD->CURRENT_BUFFER will be nil after this operation. | |
1438 */ | |
1439 | |
1440 static void | |
1441 flush_buffer_local_cache (Lisp_Object sym, | |
1442 struct symbol_value_buffer_local *bfwd) | |
1443 { | |
1444 if (NILP (bfwd->current_buffer)) | |
1445 /* Cache is already flushed. */ | |
1446 return; | |
1447 | |
1448 /* Flush out the old cache. */ | |
1449 write_out_buffer_local_cache (sym, bfwd); | |
1450 | |
1451 bfwd->current_alist_element = Qnil; | |
1452 bfwd->current_buffer = Qnil; | |
1453 | |
1454 /* Now store default the value into the current-value slot. | |
1455 We don't simply write it there, because the current-value | |
1456 slot might be a forwarding pointer, in which case we need | |
1457 to instead write the value into the C variable. | |
1458 | |
1459 We might also want to call a magic function. | |
1460 | |
1461 So instead, we call this function. */ | |
1462 store_symval_forwarding (sym, bfwd->current_value, bfwd->default_value); | |
1463 } | |
1464 | |
1465 /* Flush all the buffer-local variable caches. Whoever has a | |
1466 non-interned buffer-local variable will be spanked. Whoever has a | |
1467 magic variable that interns or uninterns symbols... I don't even | |
1468 want to think about it. | |
1469 */ | |
1470 | |
1471 void | |
1472 flush_all_buffer_local_cache (void) | |
1473 { | |
1474 Lisp_Object *syms = XVECTOR_DATA (Vobarray); | |
1475 long count = XVECTOR_LENGTH (Vobarray); | |
1476 long i; | |
1477 | |
1478 for (i=0; i<count; i++) | |
1479 { | |
1480 Lisp_Object sym = syms[i]; | |
1481 Lisp_Object value; | |
1482 | |
1483 if (!ZEROP (sym)) | |
1484 for(;;) | |
1485 { | |
1486 Lisp_Symbol *next; | |
1487 assert (SYMBOLP (sym)); | |
1488 value = fetch_value_maybe_past_magic (sym, Qt); | |
1489 if (SYMBOL_VALUE_BUFFER_LOCAL_P (value)) | |
1490 flush_buffer_local_cache (sym, XSYMBOL_VALUE_BUFFER_LOCAL (value)); | |
1491 | |
1492 next = symbol_next (XSYMBOL (sym)); | |
1493 if (!next) | |
1494 break; | |
793 | 1495 sym = wrap_symbol (next); |
446 | 1496 } |
1497 } | |
1498 } | |
1499 | |
428 | 1500 |
1501 void | |
1502 kill_buffer_local_variables (struct buffer *buf) | |
1503 { | |
1504 Lisp_Object prev = Qnil; | |
1505 Lisp_Object alist; | |
1506 | |
1507 /* Any which are supposed to be permanent, | |
1508 make local again, with the same values they had. */ | |
1509 | |
1510 for (alist = buf->local_var_alist; !NILP (alist); alist = XCDR (alist)) | |
1511 { | |
1512 Lisp_Object sym = XCAR (XCAR (alist)); | |
1513 struct symbol_value_buffer_local *bfwd; | |
1514 /* Variables with a symbol-value-varalias should not be here | |
1515 (we should have forwarded past them) and there must be a | |
1516 symbol-value-buffer-local. If there's a symbol-value-lisp-magic, | |
1517 just forward past it; if the variable has a handler, it was | |
1518 already called. */ | |
1519 Lisp_Object value = fetch_value_maybe_past_magic (sym, Qt); | |
1520 | |
1521 assert (SYMBOL_VALUE_BUFFER_LOCAL_P (value)); | |
1522 bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (value); | |
1523 | |
1524 if (!NILP (Fget (sym, Qpermanent_local, Qnil))) | |
1525 /* prev points to the last alist element that is still | |
1526 staying around, so *only* update it now. This didn't | |
1527 used to be the case; this bug has been around since | |
1528 mly's rewrite two years ago! */ | |
1529 prev = alist; | |
1530 else | |
1531 { | |
1532 /* Really truly kill it. */ | |
1533 if (!NILP (prev)) | |
1534 XCDR (prev) = XCDR (alist); | |
1535 else | |
1536 buf->local_var_alist = XCDR (alist); | |
1537 | |
1538 /* We just effectively changed the value for this variable | |
1539 in BUF. So: */ | |
1540 | |
1541 /* (1) If the cache is caching BUF, invalidate the cache. */ | |
1542 if (!NILP (bfwd->current_buffer) && | |
1543 buf == XBUFFER (bfwd->current_buffer)) | |
1544 bfwd->current_buffer = Qnil; | |
1545 | |
1546 /* (2) If we changed the value in current_buffer and this | |
1547 variable forwards to a C variable, we need to change the | |
1548 value of the C variable. set_up_buffer_local_cache() | |
1549 will do this. It doesn't hurt to do it whenever | |
1550 BUF == current_buffer, so just go ahead and do that. */ | |
1551 if (buf == current_buffer) | |
1552 set_up_buffer_local_cache (sym, bfwd, buf, Qnil, 0); | |
1553 } | |
1554 } | |
1555 } | |
1556 | |
1557 static Lisp_Object | |
1558 find_symbol_value_1 (Lisp_Object sym, struct buffer *buf, | |
1559 struct console *con, int swap_it_in, | |
1560 Lisp_Object symcons, int set_it_p) | |
1561 { | |
1562 Lisp_Object valcontents; | |
1563 | |
1564 retry: | |
1565 valcontents = XSYMBOL (sym)->value; | |
1566 | |
1567 retry_2: | |
1568 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
1569 return valcontents; | |
1570 | |
1571 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
1572 { | |
1573 case SYMVAL_LISP_MAGIC: | |
1574 /* #### kludge */ | |
1575 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
1576 /* semi-change-o */ | |
1577 goto retry_2; | |
1578 | |
1579 case SYMVAL_VARALIAS: | |
1580 sym = follow_varalias_pointers (sym, Qt /* #### kludge */); | |
1581 symcons = Qnil; | |
1582 /* presto change-o! */ | |
1583 goto retry; | |
1584 | |
1585 case SYMVAL_BUFFER_LOCAL: | |
1586 case SYMVAL_SOME_BUFFER_LOCAL: | |
1587 { | |
1588 struct symbol_value_buffer_local *bfwd | |
1589 = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
1590 | |
1591 if (swap_it_in) | |
1592 { | |
1593 set_up_buffer_local_cache (sym, bfwd, buf, symcons, set_it_p); | |
1594 valcontents = bfwd->current_value; | |
1595 } | |
1596 else | |
1597 { | |
1598 if (!NILP (bfwd->current_buffer) && | |
1599 buf == XBUFFER (bfwd->current_buffer)) | |
1600 valcontents = bfwd->current_value; | |
1601 else if (NILP (symcons)) | |
1602 { | |
1603 if (set_it_p) | |
1604 valcontents = assq_no_quit (sym, buf->local_var_alist); | |
1605 if (NILP (valcontents)) | |
1606 valcontents = bfwd->default_value; | |
1607 else | |
1608 valcontents = XCDR (valcontents); | |
1609 } | |
1610 else | |
1611 valcontents = XCDR (symcons); | |
1612 } | |
1613 break; | |
1614 } | |
1615 | |
1616 default: | |
1617 break; | |
1618 } | |
1619 return do_symval_forwarding (valcontents, buf, con); | |
1620 } | |
1621 | |
1622 | |
1623 /* Find the value of a symbol in BUFFER, returning Qunbound if it's not | |
1624 bound. Note that it must not be possible to QUIT within this | |
1625 function. */ | |
1626 | |
1627 Lisp_Object | |
1628 symbol_value_in_buffer (Lisp_Object sym, Lisp_Object buffer) | |
1629 { | |
1630 struct buffer *buf; | |
1631 | |
1632 CHECK_SYMBOL (sym); | |
1633 | |
1634 if (NILP (buffer)) | |
1635 buf = current_buffer; | |
1636 else | |
1637 { | |
1638 CHECK_BUFFER (buffer); | |
1639 buf = XBUFFER (buffer); | |
1640 } | |
1641 | |
1642 return find_symbol_value_1 (sym, buf, | |
1643 /* If it bombs out at startup due to a | |
1644 Lisp error, this may be nil. */ | |
1645 CONSOLEP (Vselected_console) | |
1646 ? XCONSOLE (Vselected_console) : 0, 0, Qnil, 1); | |
1647 } | |
1648 | |
1649 static Lisp_Object | |
1650 symbol_value_in_console (Lisp_Object sym, Lisp_Object console) | |
1651 { | |
1652 CHECK_SYMBOL (sym); | |
1653 | |
1654 if (NILP (console)) | |
1655 console = Vselected_console; | |
1656 else | |
1657 CHECK_CONSOLE (console); | |
1658 | |
1659 return find_symbol_value_1 (sym, current_buffer, XCONSOLE (console), 0, | |
1660 Qnil, 1); | |
1661 } | |
1662 | |
1663 /* Return the current value of SYM. The difference between this function | |
1664 and calling symbol_value_in_buffer with a BUFFER of Qnil is that | |
1665 this updates the CURRENT_VALUE slot of buffer-local variables to | |
1666 point to the current buffer, while symbol_value_in_buffer doesn't. */ | |
1667 | |
1668 Lisp_Object | |
1669 find_symbol_value (Lisp_Object sym) | |
1670 { | |
1671 /* WARNING: This function can be called when current_buffer is 0 | |
1672 and Vselected_console is Qnil, early in initialization. */ | |
1673 struct console *con; | |
1674 Lisp_Object valcontents; | |
1675 | |
1676 CHECK_SYMBOL (sym); | |
1677 | |
1678 valcontents = XSYMBOL (sym)->value; | |
1679 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
1680 return valcontents; | |
1681 | |
1682 if (CONSOLEP (Vselected_console)) | |
1683 con = XCONSOLE (Vselected_console); | |
1684 else | |
1685 { | |
1686 /* This can also get called while we're preparing to shutdown. | |
1687 #### What should really happen in that case? Should we | |
1688 actually fix things so we can't get here in that case? */ | |
1689 #ifndef PDUMP | |
1690 assert (!initialized || preparing_for_armageddon); | |
1691 #endif | |
1692 con = 0; | |
1693 } | |
1694 | |
1695 return find_symbol_value_1 (sym, current_buffer, con, 1, Qnil, 1); | |
1696 } | |
1697 | |
1698 /* This is an optimized function for quick lookup of buffer local symbols | |
1699 by avoiding O(n) search. This will work when either: | |
1700 a) We have already found the symbol e.g. by traversing local_var_alist. | |
1701 or | |
1702 b) We know that the symbol will not be found in the current buffer's | |
1703 list of local variables. | |
1704 In the former case, find_it_p is 1 and symbol_cons is the element from | |
1705 local_var_alist. In the latter case, find_it_p is 0 and symbol_cons | |
1706 is the symbol. | |
1707 | |
1708 This function is called from set_buffer_internal which does both of these | |
1709 things. */ | |
1710 | |
1711 Lisp_Object | |
1712 find_symbol_value_quickly (Lisp_Object symbol_cons, int find_it_p) | |
1713 { | |
1714 /* WARNING: This function can be called when current_buffer is 0 | |
1715 and Vselected_console is Qnil, early in initialization. */ | |
1716 struct console *con; | |
1717 Lisp_Object sym = find_it_p ? XCAR (symbol_cons) : symbol_cons; | |
1718 | |
1719 CHECK_SYMBOL (sym); | |
1720 if (CONSOLEP (Vselected_console)) | |
1721 con = XCONSOLE (Vselected_console); | |
1722 else | |
1723 { | |
1724 /* This can also get called while we're preparing to shutdown. | |
1725 #### What should really happen in that case? Should we | |
1726 actually fix things so we can't get here in that case? */ | |
1727 #ifndef PDUMP | |
1728 assert (!initialized || preparing_for_armageddon); | |
1729 #endif | |
1730 con = 0; | |
1731 } | |
1732 | |
1733 return find_symbol_value_1 (sym, current_buffer, con, 1, | |
1734 find_it_p ? symbol_cons : Qnil, | |
1735 find_it_p); | |
1736 } | |
1737 | |
1738 DEFUN ("symbol-value", Fsymbol_value, 1, 1, 0, /* | |
1739 Return SYMBOL's value. Error if that is void. | |
1740 */ | |
1741 (symbol)) | |
1742 { | |
1743 Lisp_Object val = find_symbol_value (symbol); | |
1744 | |
1745 if (UNBOUNDP (val)) | |
1746 return Fsignal (Qvoid_variable, list1 (symbol)); | |
1747 else | |
1748 return val; | |
1749 } | |
1750 | |
1751 DEFUN ("set", Fset, 2, 2, 0, /* | |
1752 Set SYMBOL's value to NEWVAL, and return NEWVAL. | |
1753 */ | |
1754 (symbol, newval)) | |
1755 { | |
1756 REGISTER Lisp_Object valcontents; | |
440 | 1757 Lisp_Symbol *sym; |
428 | 1758 /* remember, we're called by Fmakunbound() as well */ |
1759 | |
1760 CHECK_SYMBOL (symbol); | |
1761 | |
1762 retry: | |
1763 sym = XSYMBOL (symbol); | |
1764 valcontents = sym->value; | |
1765 | |
1766 if (EQ (symbol, Qnil) || | |
1767 EQ (symbol, Qt) || | |
1768 SYMBOL_IS_KEYWORD (symbol)) | |
1769 reject_constant_symbols (symbol, newval, 0, | |
1770 UNBOUNDP (newval) ? Qmakunbound : Qset); | |
1771 | |
1772 if (!SYMBOL_VALUE_MAGIC_P (valcontents) || UNBOUNDP (valcontents)) | |
1773 { | |
1774 sym->value = newval; | |
1775 return newval; | |
1776 } | |
1777 | |
1778 reject_constant_symbols (symbol, newval, 0, | |
1779 UNBOUNDP (newval) ? Qmakunbound : Qset); | |
1780 | |
1781 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
1782 { | |
1783 case SYMVAL_LISP_MAGIC: | |
1784 { | |
1785 if (UNBOUNDP (newval)) | |
440 | 1786 { |
1787 maybe_call_magic_handler (symbol, Qmakunbound, 0); | |
1788 return XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed = Qunbound; | |
1789 } | |
428 | 1790 else |
440 | 1791 { |
1792 maybe_call_magic_handler (symbol, Qset, 1, newval); | |
1793 return XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed = newval; | |
1794 } | |
428 | 1795 } |
1796 | |
1797 case SYMVAL_VARALIAS: | |
1798 symbol = follow_varalias_pointers (symbol, | |
1799 UNBOUNDP (newval) | |
1800 ? Qmakunbound : Qset); | |
1801 /* presto change-o! */ | |
1802 goto retry; | |
1803 | |
1804 case SYMVAL_FIXNUM_FORWARD: | |
996 | 1805 case SYMVAL_CONST_FIXNUM_FORWARD: |
428 | 1806 case SYMVAL_BOOLEAN_FORWARD: |
996 | 1807 case SYMVAL_CONST_BOOLEAN_FORWARD: |
428 | 1808 case SYMVAL_DEFAULT_BUFFER_FORWARD: |
1809 case SYMVAL_DEFAULT_CONSOLE_FORWARD: | |
1810 if (UNBOUNDP (newval)) | |
996 | 1811 { |
1812 #ifdef HAVE_SHLIB | |
1813 if (unloading_module) | |
1814 { | |
1815 sym->value = newval; | |
1816 return newval; | |
1817 } | |
1818 else | |
1819 #endif | |
1820 invalid_change ("Cannot makunbound", symbol); | |
1821 } | |
1822 break; | |
1823 | |
1824 case SYMVAL_OBJECT_FORWARD: | |
1825 case SYMVAL_CONST_OBJECT_FORWARD: | |
1826 if (UNBOUNDP (newval)) | |
1827 { | |
1828 #ifdef HAVE_SHLIB | |
1829 if (unloading_module) | |
1830 { | |
1111 | 1831 unstaticpro_nodump ((Lisp_Object *) |
1832 symbol_value_forward_forward | |
996 | 1833 (XSYMBOL_VALUE_FORWARD (valcontents))); |
1834 sym->value = newval; | |
1835 return newval; | |
1836 } | |
1837 else | |
1838 #endif | |
1839 invalid_change ("Cannot makunbound", symbol); | |
1840 } | |
428 | 1841 break; |
1842 | |
1843 /* case SYMVAL_UNBOUND_MARKER: break; */ | |
1844 | |
1845 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
1846 { | |
442 | 1847 const struct symbol_value_forward *fwd |
428 | 1848 = XSYMBOL_VALUE_FORWARD (valcontents); |
1849 int mask = XINT (*((Lisp_Object *) | |
1850 symbol_value_forward_forward (fwd))); | |
1851 if (mask > 0) | |
1852 /* Setting this variable makes it buffer-local */ | |
1853 current_buffer->local_var_flags |= mask; | |
1854 break; | |
1855 } | |
1856 | |
1857 case SYMVAL_SELECTED_CONSOLE_FORWARD: | |
1858 { | |
442 | 1859 const struct symbol_value_forward *fwd |
428 | 1860 = XSYMBOL_VALUE_FORWARD (valcontents); |
1861 int mask = XINT (*((Lisp_Object *) | |
1862 symbol_value_forward_forward (fwd))); | |
1863 if (mask > 0) | |
1864 /* Setting this variable makes it console-local */ | |
1865 XCONSOLE (Vselected_console)->local_var_flags |= mask; | |
1866 break; | |
1867 } | |
1868 | |
1869 case SYMVAL_BUFFER_LOCAL: | |
1870 case SYMVAL_SOME_BUFFER_LOCAL: | |
1871 { | |
1872 /* If we want to examine or set the value and | |
1873 CURRENT-BUFFER is current, we just examine or set | |
1874 CURRENT-VALUE. If CURRENT-BUFFER is not current, we | |
1875 store the current CURRENT-VALUE value into | |
1876 CURRENT-ALIST- ELEMENT, then find the appropriate alist | |
1877 element for the buffer now current and set up | |
1878 CURRENT-ALIST-ELEMENT. Then we set CURRENT-VALUE out | |
1879 of that element, and store into CURRENT-BUFFER. | |
1880 | |
1881 If we are setting the variable and the current buffer does | |
1882 not have an alist entry for this variable, an alist entry is | |
1883 created. | |
1884 | |
1885 Note that CURRENT-VALUE can be a forwarding pointer. | |
1886 Each time it is examined or set, forwarding must be | |
1887 done. */ | |
1888 struct symbol_value_buffer_local *bfwd | |
1889 = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
1890 int some_buffer_local_p = | |
1891 (bfwd->magic.type == SYMVAL_SOME_BUFFER_LOCAL); | |
1892 /* What value are we caching right now? */ | |
1893 Lisp_Object aelt = bfwd->current_alist_element; | |
1894 | |
1895 if (!NILP (bfwd->current_buffer) && | |
1896 current_buffer == XBUFFER (bfwd->current_buffer) | |
1897 && ((some_buffer_local_p) | |
1898 ? 1 /* doesn't automatically become local */ | |
1899 : !NILP (aelt) /* already local */ | |
1900 )) | |
1901 { | |
1902 /* Cache is valid */ | |
1903 valcontents = bfwd->current_value; | |
1904 } | |
1905 else | |
1906 { | |
1907 /* If the current buffer is not the buffer whose binding is | |
1908 currently cached, or if it's a SYMVAL_BUFFER_LOCAL and | |
1909 we're looking at the default value, the cache is invalid; we | |
1910 need to write it out, and find the new CURRENT-ALIST-ELEMENT | |
1911 */ | |
1912 | |
1913 /* Write out the cached value for the old buffer; copy it | |
1914 back to its alist element. This works if the current | |
1915 buffer only sees the default value, too. */ | |
1916 write_out_buffer_local_cache (symbol, bfwd); | |
1917 | |
1918 /* Find the new value for CURRENT-ALIST-ELEMENT. */ | |
1919 aelt = buffer_local_alist_element (current_buffer, symbol, bfwd); | |
1920 if (NILP (aelt)) | |
1921 { | |
1922 /* This buffer is still seeing the default value. */ | |
1923 if (!some_buffer_local_p) | |
1924 { | |
1925 /* If it's a SYMVAL_BUFFER_LOCAL, give this buffer a | |
1926 new assoc for a local value and set | |
1927 CURRENT-ALIST-ELEMENT to point to that. */ | |
1928 aelt = | |
1929 do_symval_forwarding (bfwd->current_value, | |
1930 current_buffer, | |
1931 XCONSOLE (Vselected_console)); | |
1932 aelt = Fcons (symbol, aelt); | |
1933 current_buffer->local_var_alist | |
1934 = Fcons (aelt, current_buffer->local_var_alist); | |
1935 } | |
1936 else | |
1937 { | |
1938 /* If the variable is a SYMVAL_SOME_BUFFER_LOCAL, | |
1939 we're currently seeing the default value. */ | |
1940 ; | |
1941 } | |
1942 } | |
1943 /* Cache the new buffer's assoc in CURRENT-ALIST-ELEMENT. */ | |
1944 bfwd->current_alist_element = aelt; | |
1945 /* Set BUFFER, now that CURRENT-ALIST-ELEMENT is accurate. */ | |
793 | 1946 bfwd->current_buffer = wrap_buffer (current_buffer); |
428 | 1947 valcontents = bfwd->current_value; |
1948 } | |
1949 break; | |
1950 } | |
1951 default: | |
2500 | 1952 ABORT (); |
428 | 1953 } |
1954 store_symval_forwarding (symbol, valcontents, newval); | |
1955 | |
1956 return newval; | |
1957 } | |
1958 | |
1959 | |
1960 /* Access or set a buffer-local symbol's default value. */ | |
1961 | |
1962 /* Return the default value of SYM, but don't check for voidness. | |
1963 Return Qunbound if it is void. */ | |
1964 | |
1965 static Lisp_Object | |
1966 default_value (Lisp_Object sym) | |
1967 { | |
1968 Lisp_Object valcontents; | |
1969 | |
1970 CHECK_SYMBOL (sym); | |
1971 | |
1972 retry: | |
1973 valcontents = XSYMBOL (sym)->value; | |
1974 | |
1975 retry_2: | |
1976 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
1977 return valcontents; | |
1978 | |
1979 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
1980 { | |
1981 case SYMVAL_LISP_MAGIC: | |
1982 /* #### kludge */ | |
1983 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
1984 /* semi-change-o */ | |
1985 goto retry_2; | |
1986 | |
1987 case SYMVAL_VARALIAS: | |
1988 sym = follow_varalias_pointers (sym, Qt /* #### kludge */); | |
1989 /* presto change-o! */ | |
1990 goto retry; | |
1991 | |
1992 case SYMVAL_UNBOUND_MARKER: | |
1993 return valcontents; | |
1994 | |
1995 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
1996 { | |
442 | 1997 const struct symbol_value_forward *fwd |
428 | 1998 = XSYMBOL_VALUE_FORWARD (valcontents); |
1999 return (*((Lisp_Object *)((char *) XBUFFER (Vbuffer_defaults) | |
2000 + ((char *)symbol_value_forward_forward (fwd) | |
2001 - (char *)&buffer_local_flags)))); | |
2002 } | |
2003 | |
2004 case SYMVAL_SELECTED_CONSOLE_FORWARD: | |
2005 { | |
442 | 2006 const struct symbol_value_forward *fwd |
428 | 2007 = XSYMBOL_VALUE_FORWARD (valcontents); |
2008 return (*((Lisp_Object *)((char *) XCONSOLE (Vconsole_defaults) | |
2009 + ((char *)symbol_value_forward_forward (fwd) | |
2010 - (char *)&console_local_flags)))); | |
2011 } | |
2012 | |
2013 case SYMVAL_BUFFER_LOCAL: | |
2014 case SYMVAL_SOME_BUFFER_LOCAL: | |
2015 { | |
2016 struct symbol_value_buffer_local *bfwd = | |
2017 XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
2018 | |
2019 /* Handle user-created local variables. */ | |
2020 /* If var is set up for a buffer that lacks a local value for it, | |
2021 the current value is nominally the default value. | |
2022 But the current value slot may be more up to date, since | |
2023 ordinary setq stores just that slot. So use that. */ | |
2024 if (NILP (bfwd->current_alist_element)) | |
2025 return do_symval_forwarding (bfwd->current_value, current_buffer, | |
2026 XCONSOLE (Vselected_console)); | |
2027 else | |
2028 return bfwd->default_value; | |
2029 } | |
2030 default: | |
2031 /* For other variables, get the current value. */ | |
2032 return do_symval_forwarding (valcontents, current_buffer, | |
2033 XCONSOLE (Vselected_console)); | |
2034 } | |
2035 | |
1204 | 2036 RETURN_NOT_REACHED (Qnil); /* suppress compiler warning */ |
428 | 2037 } |
2038 | |
2039 DEFUN ("default-boundp", Fdefault_boundp, 1, 1, 0, /* | |
2040 Return t if SYMBOL has a non-void default value. | |
2041 This is the value that is seen in buffers that do not have their own values | |
2042 for this variable. | |
2043 */ | |
2044 (symbol)) | |
2045 { | |
2046 return UNBOUNDP (default_value (symbol)) ? Qnil : Qt; | |
2047 } | |
2048 | |
2049 DEFUN ("default-value", Fdefault_value, 1, 1, 0, /* | |
2050 Return SYMBOL's default value. | |
2051 This is the value that is seen in buffers that do not have their own values | |
2052 for this variable. The default value is meaningful for variables with | |
2053 local bindings in certain buffers. | |
2054 */ | |
2055 (symbol)) | |
2056 { | |
2057 Lisp_Object value = default_value (symbol); | |
2058 | |
2059 return UNBOUNDP (value) ? Fsignal (Qvoid_variable, list1 (symbol)) : value; | |
2060 } | |
2061 | |
2062 DEFUN ("set-default", Fset_default, 2, 2, 0, /* | |
444 | 2063 Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated. |
428 | 2064 The default value is seen in buffers that do not have their own values |
2065 for this variable. | |
2066 */ | |
2067 (symbol, value)) | |
2068 { | |
2069 Lisp_Object valcontents; | |
2070 | |
2071 CHECK_SYMBOL (symbol); | |
2072 | |
2073 retry: | |
2074 valcontents = XSYMBOL (symbol)->value; | |
2075 | |
2076 retry_2: | |
2077 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
2078 return Fset (symbol, value); | |
2079 | |
2080 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
2081 { | |
2082 case SYMVAL_LISP_MAGIC: | |
2083 RETURN_IF_NOT_UNBOUND (maybe_call_magic_handler (symbol, Qset_default, 1, | |
2084 value)); | |
2085 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
2086 /* semi-change-o */ | |
2087 goto retry_2; | |
2088 | |
2089 case SYMVAL_VARALIAS: | |
2090 symbol = follow_varalias_pointers (symbol, Qset_default); | |
2091 /* presto change-o! */ | |
2092 goto retry; | |
2093 | |
2094 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
2095 set_default_buffer_slot_variable (symbol, value); | |
2096 return value; | |
2097 | |
2098 case SYMVAL_SELECTED_CONSOLE_FORWARD: | |
2099 set_default_console_slot_variable (symbol, value); | |
2100 return value; | |
2101 | |
2102 case SYMVAL_BUFFER_LOCAL: | |
2103 case SYMVAL_SOME_BUFFER_LOCAL: | |
2104 { | |
2105 /* Store new value into the DEFAULT-VALUE slot */ | |
2106 struct symbol_value_buffer_local *bfwd | |
2107 = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
2108 | |
2109 bfwd->default_value = value; | |
2110 /* If current-buffer doesn't shadow default_value, | |
2111 * we must set the CURRENT-VALUE slot too */ | |
2112 if (NILP (bfwd->current_alist_element)) | |
2113 store_symval_forwarding (symbol, bfwd->current_value, value); | |
2114 return value; | |
2115 } | |
2116 | |
2117 default: | |
2118 return Fset (symbol, value); | |
2119 } | |
2120 } | |
2121 | |
2122 DEFUN ("setq-default", Fsetq_default, 0, UNEVALLED, 0, /* | |
2123 Set the default value of variable SYMBOL to VALUE. | |
2124 SYMBOL, the variable name, is literal (not evaluated); | |
2125 VALUE is an expression and it is evaluated. | |
2126 The default value of a variable is seen in buffers | |
2127 that do not have their own values for the variable. | |
2128 | |
2129 More generally, you can use multiple variables and values, as in | |
2130 (setq-default SYMBOL VALUE SYMBOL VALUE...) | |
2131 This sets each SYMBOL's default value to the corresponding VALUE. | |
2132 The VALUE for the Nth SYMBOL can refer to the new default values | |
2133 of previous SYMBOLs. | |
2134 */ | |
2135 (args)) | |
2136 { | |
2137 /* This function can GC */ | |
2138 int nargs; | |
2421 | 2139 Lisp_Object retval = Qnil; |
428 | 2140 |
2141 GET_LIST_LENGTH (args, nargs); | |
2142 | |
2143 if (nargs & 1) /* Odd number of arguments? */ | |
2144 Fsignal (Qwrong_number_of_arguments, | |
2145 list2 (Qsetq_default, make_int (nargs))); | |
2146 | |
2421 | 2147 GC_PROPERTY_LIST_LOOP_3 (symbol, val, args) |
428 | 2148 { |
2149 val = Feval (val); | |
2150 Fset_default (symbol, val); | |
2421 | 2151 retval = val; |
428 | 2152 } |
2153 | |
2421 | 2154 END_GC_PROPERTY_LIST_LOOP (symbol); |
2155 return retval; | |
428 | 2156 } |
2157 | |
2158 /* Lisp functions for creating and removing buffer-local variables. */ | |
2159 | |
2160 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, 1, 1, | |
2161 "vMake Variable Buffer Local: ", /* | |
2162 Make VARIABLE have a separate value for each buffer. | |
2163 At any time, the value for the current buffer is in effect. | |
2164 There is also a default value which is seen in any buffer which has not yet | |
2165 set its own value. | |
2166 Using `set' or `setq' to set the variable causes it to have a separate value | |
2167 for the current buffer if it was previously using the default value. | |
2168 The function `default-value' gets the default value and `set-default' | |
2169 sets it. | |
2170 */ | |
2171 (variable)) | |
2172 { | |
2173 Lisp_Object valcontents; | |
2174 | |
2175 CHECK_SYMBOL (variable); | |
2176 | |
2177 retry: | |
2178 verify_ok_for_buffer_local (variable, Qmake_variable_buffer_local); | |
2179 | |
2180 valcontents = XSYMBOL (variable)->value; | |
2181 | |
2182 retry_2: | |
2183 if (SYMBOL_VALUE_MAGIC_P (valcontents)) | |
2184 { | |
2185 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
2186 { | |
2187 case SYMVAL_LISP_MAGIC: | |
2188 if (!UNBOUNDP (maybe_call_magic_handler | |
2189 (variable, Qmake_variable_buffer_local, 0))) | |
2190 return variable; | |
2191 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
2192 /* semi-change-o */ | |
2193 goto retry_2; | |
2194 | |
2195 case SYMVAL_VARALIAS: | |
2196 variable = follow_varalias_pointers (variable, | |
2197 Qmake_variable_buffer_local); | |
2198 /* presto change-o! */ | |
2199 goto retry; | |
2200 | |
2201 case SYMVAL_FIXNUM_FORWARD: | |
2202 case SYMVAL_BOOLEAN_FORWARD: | |
2203 case SYMVAL_OBJECT_FORWARD: | |
2204 case SYMVAL_UNBOUND_MARKER: | |
2205 break; | |
2206 | |
2207 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
2208 case SYMVAL_BUFFER_LOCAL: | |
2209 /* Already per-each-buffer */ | |
2210 return variable; | |
2211 | |
2212 case SYMVAL_SOME_BUFFER_LOCAL: | |
2213 /* Transmogrify */ | |
2214 XSYMBOL_VALUE_BUFFER_LOCAL (valcontents)->magic.type = | |
2215 SYMVAL_BUFFER_LOCAL; | |
2216 return variable; | |
2217 | |
2218 default: | |
2500 | 2219 ABORT (); |
428 | 2220 } |
2221 } | |
2222 | |
2223 { | |
2224 struct symbol_value_buffer_local *bfwd | |
3017 | 2225 = ALLOC_LCRECORD_TYPE (struct symbol_value_buffer_local, |
428 | 2226 &lrecord_symbol_value_buffer_local); |
2227 Lisp_Object foo; | |
2228 bfwd->magic.type = SYMVAL_BUFFER_LOCAL; | |
2229 | |
2230 bfwd->default_value = find_symbol_value (variable); | |
2231 bfwd->current_value = valcontents; | |
2232 bfwd->current_alist_element = Qnil; | |
2233 bfwd->current_buffer = Fcurrent_buffer (); | |
793 | 2234 foo = wrap_symbol_value_magic (bfwd); |
428 | 2235 *value_slot_past_magic (variable) = foo; |
2236 #if 1 /* #### Yuck! FSFmacs bug-compatibility*/ | |
2237 /* This sets the default-value of any make-variable-buffer-local to nil. | |
2238 That just sucks. User can just use setq-default to effect that, | |
2239 but there's no way to do makunbound-default to undo this lossage. */ | |
2240 if (UNBOUNDP (valcontents)) | |
2241 bfwd->default_value = Qnil; | |
2242 #endif | |
2243 #if 0 /* #### Yuck! */ | |
2244 /* This sets the value to nil in this buffer. | |
2245 User could use (setq variable nil) to do this. | |
2246 It isn't as egregious to do this automatically | |
2247 as it is to do so to the default-value, but it's | |
2248 still really dubious. */ | |
2249 if (UNBOUNDP (valcontents)) | |
2250 Fset (variable, Qnil); | |
2251 #endif | |
2252 return variable; | |
2253 } | |
2254 } | |
2255 | |
2256 DEFUN ("make-local-variable", Fmake_local_variable, 1, 1, | |
2257 "vMake Local Variable: ", /* | |
2258 Make VARIABLE have a separate value in the current buffer. | |
2259 Other buffers will continue to share a common default value. | |
2260 \(The buffer-local value of VARIABLE starts out as the same value | |
2261 VARIABLE previously had. If VARIABLE was void, it remains void.) | |
2262 See also `make-variable-buffer-local'. | |
2263 | |
2264 If the variable is already arranged to become local when set, | |
2265 this function causes a local value to exist for this buffer, | |
2266 just as setting the variable would do. | |
2267 | |
2268 Do not use `make-local-variable' to make a hook variable buffer-local. | |
2269 Use `make-local-hook' instead. | |
2270 */ | |
2271 (variable)) | |
2272 { | |
2273 Lisp_Object valcontents; | |
2274 struct symbol_value_buffer_local *bfwd; | |
2275 | |
2276 CHECK_SYMBOL (variable); | |
2277 | |
2278 retry: | |
2279 verify_ok_for_buffer_local (variable, Qmake_local_variable); | |
2280 | |
2281 valcontents = XSYMBOL (variable)->value; | |
2282 | |
2283 retry_2: | |
2284 if (SYMBOL_VALUE_MAGIC_P (valcontents)) | |
2285 { | |
2286 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
2287 { | |
2288 case SYMVAL_LISP_MAGIC: | |
2289 if (!UNBOUNDP (maybe_call_magic_handler | |
2290 (variable, Qmake_local_variable, 0))) | |
2291 return variable; | |
2292 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
2293 /* semi-change-o */ | |
2294 goto retry_2; | |
2295 | |
2296 case SYMVAL_VARALIAS: | |
2297 variable = follow_varalias_pointers (variable, Qmake_local_variable); | |
2298 /* presto change-o! */ | |
2299 goto retry; | |
2300 | |
2301 case SYMVAL_FIXNUM_FORWARD: | |
2302 case SYMVAL_BOOLEAN_FORWARD: | |
2303 case SYMVAL_OBJECT_FORWARD: | |
2304 case SYMVAL_UNBOUND_MARKER: | |
2305 break; | |
2306 | |
2307 case SYMVAL_BUFFER_LOCAL: | |
2308 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
2309 { | |
2310 /* Make sure the symbol has a local value in this particular | |
2311 buffer, by setting it to the same value it already has. */ | |
2312 Fset (variable, find_symbol_value (variable)); | |
2313 return variable; | |
2314 } | |
2315 | |
2316 case SYMVAL_SOME_BUFFER_LOCAL: | |
2317 { | |
2318 if (!NILP (buffer_local_alist_element (current_buffer, | |
2319 variable, | |
2320 (XSYMBOL_VALUE_BUFFER_LOCAL | |
2321 (valcontents))))) | |
2322 goto already_local_to_current_buffer; | |
2323 else | |
2324 goto already_local_to_some_other_buffer; | |
2325 } | |
2326 | |
2327 default: | |
2500 | 2328 ABORT (); |
428 | 2329 } |
2330 } | |
2331 | |
2332 /* Make sure variable is set up to hold per-buffer values */ | |
3017 | 2333 bfwd = ALLOC_LCRECORD_TYPE (struct symbol_value_buffer_local, |
428 | 2334 &lrecord_symbol_value_buffer_local); |
2335 bfwd->magic.type = SYMVAL_SOME_BUFFER_LOCAL; | |
2336 | |
2337 bfwd->current_buffer = Qnil; | |
2338 bfwd->current_alist_element = Qnil; | |
2339 bfwd->current_value = valcontents; | |
2340 /* passing 0 is OK because this should never be a | |
2341 SYMVAL_CURRENT_BUFFER_FORWARD or SYMVAL_SELECTED_CONSOLE_FORWARD | |
2342 variable. */ | |
2343 bfwd->default_value = do_symval_forwarding (valcontents, 0, 0); | |
2344 | |
2345 #if 0 | |
2346 if (UNBOUNDP (bfwd->default_value)) | |
2347 bfwd->default_value = Qnil; /* Yuck! */ | |
2348 #endif | |
2349 | |
793 | 2350 valcontents = wrap_symbol_value_magic (bfwd); |
428 | 2351 *value_slot_past_magic (variable) = valcontents; |
2352 | |
2353 already_local_to_some_other_buffer: | |
2354 | |
2355 /* Make sure this buffer has its own value of variable */ | |
2356 bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
2357 | |
2358 if (UNBOUNDP (bfwd->default_value)) | |
2359 { | |
2360 /* If default value is unbound, set local value to nil. */ | |
793 | 2361 bfwd->current_buffer = wrap_buffer (current_buffer); |
428 | 2362 bfwd->current_alist_element = Fcons (variable, Qnil); |
2363 current_buffer->local_var_alist = | |
2364 Fcons (bfwd->current_alist_element, current_buffer->local_var_alist); | |
2365 store_symval_forwarding (variable, bfwd->current_value, Qnil); | |
2366 return variable; | |
2367 } | |
2368 | |
2369 current_buffer->local_var_alist | |
2370 = Fcons (Fcons (variable, bfwd->default_value), | |
2371 current_buffer->local_var_alist); | |
2372 | |
2373 /* Make sure symbol does not think it is set up for this buffer; | |
2374 force it to look once again for this buffer's value */ | |
2375 if (!NILP (bfwd->current_buffer) && | |
2376 current_buffer == XBUFFER (bfwd->current_buffer)) | |
2377 bfwd->current_buffer = Qnil; | |
2378 | |
2379 already_local_to_current_buffer: | |
2380 | |
2381 /* If the symbol forwards into a C variable, then swap in the | |
2382 variable for this buffer immediately. If C code modifies the | |
2383 variable before we swap in, then that new value will clobber the | |
2384 default value the next time we swap. */ | |
2385 bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
2386 if (SYMBOL_VALUE_MAGIC_P (bfwd->current_value)) | |
2387 { | |
2388 switch (XSYMBOL_VALUE_MAGIC_TYPE (bfwd->current_value)) | |
2389 { | |
2390 case SYMVAL_FIXNUM_FORWARD: | |
2391 case SYMVAL_BOOLEAN_FORWARD: | |
2392 case SYMVAL_OBJECT_FORWARD: | |
2393 case SYMVAL_DEFAULT_BUFFER_FORWARD: | |
2394 set_up_buffer_local_cache (variable, bfwd, current_buffer, Qnil, 1); | |
2395 break; | |
2396 | |
2397 case SYMVAL_UNBOUND_MARKER: | |
2398 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
2399 break; | |
2400 | |
2401 default: | |
2500 | 2402 ABORT (); |
428 | 2403 } |
2404 } | |
2405 | |
2406 return variable; | |
2407 } | |
2408 | |
2409 DEFUN ("kill-local-variable", Fkill_local_variable, 1, 1, | |
2410 "vKill Local Variable: ", /* | |
2411 Make VARIABLE no longer have a separate value in the current buffer. | |
2412 From now on the default value will apply in this buffer. | |
2413 */ | |
2414 (variable)) | |
2415 { | |
2416 Lisp_Object valcontents; | |
2417 | |
2418 CHECK_SYMBOL (variable); | |
2419 | |
2420 retry: | |
2421 valcontents = XSYMBOL (variable)->value; | |
2422 | |
2423 retry_2: | |
2424 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
2425 return variable; | |
2426 | |
2427 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
2428 { | |
2429 case SYMVAL_LISP_MAGIC: | |
2430 if (!UNBOUNDP (maybe_call_magic_handler | |
2431 (variable, Qkill_local_variable, 0))) | |
2432 return variable; | |
2433 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
2434 /* semi-change-o */ | |
2435 goto retry_2; | |
2436 | |
2437 case SYMVAL_VARALIAS: | |
2438 variable = follow_varalias_pointers (variable, Qkill_local_variable); | |
2439 /* presto change-o! */ | |
2440 goto retry; | |
2441 | |
2442 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
2443 { | |
442 | 2444 const struct symbol_value_forward *fwd |
428 | 2445 = XSYMBOL_VALUE_FORWARD (valcontents); |
2446 int offset = ((char *) symbol_value_forward_forward (fwd) | |
2447 - (char *) &buffer_local_flags); | |
2448 int mask = | |
2449 XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd))); | |
2450 | |
2451 if (mask > 0) | |
2452 { | |
2453 int (*magicfun) (Lisp_Object sym, Lisp_Object *val, | |
2454 Lisp_Object in_object, int flags) = | |
2455 symbol_value_forward_magicfun (fwd); | |
2456 Lisp_Object oldval = * (Lisp_Object *) | |
2457 (offset + (char *) XBUFFER (Vbuffer_defaults)); | |
2458 if (magicfun) | |
771 | 2459 (magicfun) (variable, &oldval, wrap_buffer (current_buffer), 0); |
428 | 2460 *(Lisp_Object *) (offset + (char *) current_buffer) |
2461 = oldval; | |
2462 current_buffer->local_var_flags &= ~mask; | |
2463 } | |
2464 return variable; | |
2465 } | |
2466 | |
2467 case SYMVAL_BUFFER_LOCAL: | |
2468 case SYMVAL_SOME_BUFFER_LOCAL: | |
2469 { | |
2470 /* Get rid of this buffer's alist element, if any */ | |
2471 struct symbol_value_buffer_local *bfwd | |
2472 = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
2473 Lisp_Object alist = current_buffer->local_var_alist; | |
2474 Lisp_Object alist_element | |
2475 = buffer_local_alist_element (current_buffer, variable, bfwd); | |
2476 | |
2477 if (!NILP (alist_element)) | |
2478 current_buffer->local_var_alist = Fdelq (alist_element, alist); | |
2479 | |
2480 /* Make sure symbol does not think it is set up for this buffer; | |
2481 force it to look once again for this buffer's value */ | |
2482 if (!NILP (bfwd->current_buffer) && | |
2483 current_buffer == XBUFFER (bfwd->current_buffer)) | |
2484 bfwd->current_buffer = Qnil; | |
2485 | |
2486 /* We just changed the value in the current_buffer. If this | |
2487 variable forwards to a C variable, we need to change the | |
2488 value of the C variable. set_up_buffer_local_cache() | |
2489 will do this. It doesn't hurt to do it always, | |
2490 so just go ahead and do that. */ | |
2491 set_up_buffer_local_cache (variable, bfwd, current_buffer, Qnil, 1); | |
2492 } | |
2493 return variable; | |
2494 | |
2495 default: | |
2496 return variable; | |
2497 } | |
1204 | 2498 RETURN_NOT_REACHED(Qnil); /* suppress compiler warning */ |
428 | 2499 } |
2500 | |
2501 | |
2502 DEFUN ("kill-console-local-variable", Fkill_console_local_variable, 1, 1, | |
2503 "vKill Console Local Variable: ", /* | |
2504 Make VARIABLE no longer have a separate value in the selected console. | |
2505 From now on the default value will apply in this console. | |
2506 */ | |
2507 (variable)) | |
2508 { | |
2509 Lisp_Object valcontents; | |
2510 | |
2511 CHECK_SYMBOL (variable); | |
2512 | |
2513 retry: | |
2514 valcontents = XSYMBOL (variable)->value; | |
2515 | |
2516 retry_2: | |
2517 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
2518 return variable; | |
2519 | |
2520 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
2521 { | |
2522 case SYMVAL_LISP_MAGIC: | |
2523 if (!UNBOUNDP (maybe_call_magic_handler | |
2524 (variable, Qkill_console_local_variable, 0))) | |
2525 return variable; | |
2526 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
2527 /* semi-change-o */ | |
2528 goto retry_2; | |
2529 | |
2530 case SYMVAL_VARALIAS: | |
2531 variable = follow_varalias_pointers (variable, | |
2532 Qkill_console_local_variable); | |
2533 /* presto change-o! */ | |
2534 goto retry; | |
2535 | |
2536 case SYMVAL_SELECTED_CONSOLE_FORWARD: | |
2537 { | |
442 | 2538 const struct symbol_value_forward *fwd |
428 | 2539 = XSYMBOL_VALUE_FORWARD (valcontents); |
2540 int offset = ((char *) symbol_value_forward_forward (fwd) | |
2541 - (char *) &console_local_flags); | |
2542 int mask = | |
2543 XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd))); | |
2544 | |
2545 if (mask > 0) | |
2546 { | |
2547 int (*magicfun) (Lisp_Object sym, Lisp_Object *val, | |
2548 Lisp_Object in_object, int flags) = | |
2549 symbol_value_forward_magicfun (fwd); | |
2550 Lisp_Object oldval = * (Lisp_Object *) | |
2551 (offset + (char *) XCONSOLE (Vconsole_defaults)); | |
2552 if (magicfun) | |
2553 magicfun (variable, &oldval, Vselected_console, 0); | |
2554 *(Lisp_Object *) (offset + (char *) XCONSOLE (Vselected_console)) | |
2555 = oldval; | |
2556 XCONSOLE (Vselected_console)->local_var_flags &= ~mask; | |
2557 } | |
2558 return variable; | |
2559 } | |
2560 | |
2561 default: | |
2562 return variable; | |
2563 } | |
2564 } | |
2565 | |
2566 /* Used by specbind to determine what effects it might have. Returns: | |
2567 * 0 if symbol isn't buffer-local, and wouldn't be after it is set | |
2568 * <0 if symbol isn't presently buffer-local, but set would make it so | |
2569 * >0 if symbol is presently buffer-local | |
2570 */ | |
2571 int | |
2572 symbol_value_buffer_local_info (Lisp_Object symbol, struct buffer *buffer) | |
2573 { | |
2574 Lisp_Object valcontents; | |
2575 | |
2576 retry: | |
2577 valcontents = XSYMBOL (symbol)->value; | |
2578 | |
2579 retry_2: | |
2580 if (SYMBOL_VALUE_MAGIC_P (valcontents)) | |
2581 { | |
2582 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
2583 { | |
2584 case SYMVAL_LISP_MAGIC: | |
2585 /* #### kludge */ | |
2586 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
2587 /* semi-change-o */ | |
2588 goto retry_2; | |
2589 | |
2590 case SYMVAL_VARALIAS: | |
2591 symbol = follow_varalias_pointers (symbol, Qt /* #### kludge */); | |
2592 /* presto change-o! */ | |
2593 goto retry; | |
2594 | |
2595 case SYMVAL_CURRENT_BUFFER_FORWARD: | |
2596 { | |
442 | 2597 const struct symbol_value_forward *fwd |
428 | 2598 = XSYMBOL_VALUE_FORWARD (valcontents); |
2599 int mask = XINT (*((Lisp_Object *) | |
2600 symbol_value_forward_forward (fwd))); | |
2601 if ((mask <= 0) || (buffer && (buffer->local_var_flags & mask))) | |
2602 /* Already buffer-local */ | |
2603 return 1; | |
2604 else | |
2605 /* Would be buffer-local after set */ | |
2606 return -1; | |
2607 } | |
2608 case SYMVAL_BUFFER_LOCAL: | |
2609 case SYMVAL_SOME_BUFFER_LOCAL: | |
2610 { | |
2611 struct symbol_value_buffer_local *bfwd | |
2612 = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents); | |
2613 if (buffer | |
2614 && !NILP (buffer_local_alist_element (buffer, symbol, bfwd))) | |
2615 return 1; | |
2616 else | |
2617 /* Automatically becomes local when set */ | |
2618 return bfwd->magic.type == SYMVAL_BUFFER_LOCAL ? -1 : 0; | |
2619 } | |
2620 default: | |
2621 return 0; | |
2622 } | |
2623 } | |
2624 return 0; | |
2625 } | |
2626 | |
2627 | |
2628 DEFUN ("symbol-value-in-buffer", Fsymbol_value_in_buffer, 2, 3, 0, /* | |
2629 Return the value of SYMBOL in BUFFER, or UNBOUND-VALUE if it is unbound. | |
2630 */ | |
2631 (symbol, buffer, unbound_value)) | |
2632 { | |
2633 Lisp_Object value; | |
2634 CHECK_SYMBOL (symbol); | |
2635 CHECK_BUFFER (buffer); | |
2636 value = symbol_value_in_buffer (symbol, buffer); | |
2637 return UNBOUNDP (value) ? unbound_value : value; | |
2638 } | |
2639 | |
2640 DEFUN ("symbol-value-in-console", Fsymbol_value_in_console, 2, 3, 0, /* | |
2641 Return the value of SYMBOL in CONSOLE, or UNBOUND-VALUE if it is unbound. | |
2642 */ | |
2643 (symbol, console, unbound_value)) | |
2644 { | |
2645 Lisp_Object value; | |
2646 CHECK_SYMBOL (symbol); | |
2647 CHECK_CONSOLE (console); | |
2648 value = symbol_value_in_console (symbol, console); | |
2649 return UNBOUNDP (value) ? unbound_value : value; | |
2650 } | |
2651 | |
2652 DEFUN ("built-in-variable-type", Fbuilt_in_variable_type, 1, 1, 0, /* | |
2653 If SYMBOL is a built-in variable, return info about this; else return nil. | |
2654 The returned info will be a symbol, one of | |
2655 | |
2656 `object' A simple built-in variable. | |
2657 `const-object' Same, but cannot be set. | |
2658 `integer' A built-in integer variable. | |
2659 `const-integer' Same, but cannot be set. | |
2660 `boolean' A built-in boolean variable. | |
2661 `const-boolean' Same, but cannot be set. | |
2662 `const-specifier' Always contains a specifier; e.g. `has-modeline-p'. | |
2663 `current-buffer' A built-in buffer-local variable. | |
2664 `const-current-buffer' Same, but cannot be set. | |
2665 `default-buffer' Forwards to the default value of a built-in | |
2666 buffer-local variable. | |
2667 `selected-console' A built-in console-local variable. | |
2668 `const-selected-console' Same, but cannot be set. | |
2669 `default-console' Forwards to the default value of a built-in | |
2670 console-local variable. | |
2671 */ | |
2672 (symbol)) | |
2673 { | |
2674 REGISTER Lisp_Object valcontents; | |
2675 | |
2676 CHECK_SYMBOL (symbol); | |
2677 | |
2678 retry: | |
2679 valcontents = XSYMBOL (symbol)->value; | |
2680 | |
2681 retry_2: | |
2682 if (!SYMBOL_VALUE_MAGIC_P (valcontents)) | |
2683 return Qnil; | |
2684 | |
2685 switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents)) | |
2686 { | |
2687 case SYMVAL_LISP_MAGIC: | |
2688 valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed; | |
2689 /* semi-change-o */ | |
2690 goto retry_2; | |
2691 | |
2692 case SYMVAL_VARALIAS: | |
2693 symbol = follow_varalias_pointers (symbol, Qt); | |
2694 /* presto change-o! */ | |
2695 goto retry; | |
2696 | |
2697 case SYMVAL_BUFFER_LOCAL: | |
2698 case SYMVAL_SOME_BUFFER_LOCAL: | |
2699 valcontents = | |
2700 XSYMBOL_VALUE_BUFFER_LOCAL (valcontents)->current_value; | |
2701 /* semi-change-o */ | |
2702 goto retry_2; | |
2703 | |
2704 case SYMVAL_FIXNUM_FORWARD: return Qinteger; | |
2705 case SYMVAL_CONST_FIXNUM_FORWARD: return Qconst_integer; | |
2706 case SYMVAL_BOOLEAN_FORWARD: return Qboolean; | |
2707 case SYMVAL_CONST_BOOLEAN_FORWARD: return Qconst_boolean; | |
2708 case SYMVAL_OBJECT_FORWARD: return Qobject; | |
2709 case SYMVAL_CONST_OBJECT_FORWARD: return Qconst_object; | |
2710 case SYMVAL_CONST_SPECIFIER_FORWARD: return Qconst_specifier; | |
2711 case SYMVAL_DEFAULT_BUFFER_FORWARD: return Qdefault_buffer; | |
2712 case SYMVAL_CURRENT_BUFFER_FORWARD: return Qcurrent_buffer; | |
2713 case SYMVAL_CONST_CURRENT_BUFFER_FORWARD: return Qconst_current_buffer; | |
2714 case SYMVAL_DEFAULT_CONSOLE_FORWARD: return Qdefault_console; | |
2715 case SYMVAL_SELECTED_CONSOLE_FORWARD: return Qselected_console; | |
2716 case SYMVAL_CONST_SELECTED_CONSOLE_FORWARD: return Qconst_selected_console; | |
2717 case SYMVAL_UNBOUND_MARKER: return Qnil; | |
2718 | |
2719 default: | |
2500 | 2720 ABORT (); return Qnil; |
428 | 2721 } |
2722 } | |
2723 | |
2724 | |
2725 DEFUN ("local-variable-p", Flocal_variable_p, 2, 3, 0, /* | |
2726 Return t if SYMBOL's value is local to BUFFER. | |
444 | 2727 If optional third arg AFTER-SET is non-nil, return t if SYMBOL would be |
428 | 2728 buffer-local after it is set, regardless of whether it is so presently. |
2729 A nil value for BUFFER is *not* the same as (current-buffer), but means | |
2730 "no buffer". Specifically: | |
2731 | |
2732 -- If BUFFER is nil and AFTER-SET is nil, a return value of t indicates that | |
2733 the variable is one of the special built-in variables that is always | |
2734 buffer-local. (This includes `buffer-file-name', `buffer-read-only', | |
2735 `buffer-undo-list', and others.) | |
2736 | |
2737 -- If BUFFER is nil and AFTER-SET is t, a return value of t indicates that | |
2738 the variable has had `make-variable-buffer-local' applied to it. | |
2739 */ | |
2740 (symbol, buffer, after_set)) | |
2741 { | |
2742 int local_info; | |
2743 | |
2744 CHECK_SYMBOL (symbol); | |
2745 if (!NILP (buffer)) | |
2746 { | |
2747 buffer = get_buffer (buffer, 1); | |
2748 local_info = symbol_value_buffer_local_info (symbol, XBUFFER (buffer)); | |
2749 } | |
2750 else | |
2751 { | |
2752 local_info = symbol_value_buffer_local_info (symbol, 0); | |
2753 } | |
2754 | |
2755 if (NILP (after_set)) | |
2756 return local_info > 0 ? Qt : Qnil; | |
2757 else | |
2758 return local_info != 0 ? Qt : Qnil; | |
2759 } | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2760 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2761 DEFUN ("custom-variable-p", Fcustom_variable_p, 1, 1, 0, /* |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2762 Return non-nil if SYMBOL names a custom variable. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2763 Does not follow the variable alias chain. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2764 */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2765 (symbol)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2766 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2767 return (!(NILP (Fget(symbol, intern ("standard-value"), Qnil))) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2768 || !(NILP (Fget(symbol, intern ("custom-autoload"), Qnil)))) ? |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2769 Qt: Qnil; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2770 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2771 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2772 static Lisp_Object |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2773 user_variable_alias_check_fun (Lisp_Object symbol) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2774 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2775 Lisp_Object documentation = Fget (symbol, Qvariable_documentation, Qnil); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2776 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2777 if ((INTP (documentation) && XINT (documentation) < 0) || |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2778 (STRINGP (documentation) && |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2779 (string_byte (documentation, 0) == '*')) || |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2780 /* If (STRING . INTEGER), a negative integer means a user variable. */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2781 (CONSP (documentation) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2782 && STRINGP (XCAR (documentation)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2783 && INTP (XCDR (documentation)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2784 && XINT (XCDR (documentation)) < 0) || |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2785 !NILP (Fcustom_variable_p (symbol))) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2786 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2787 return make_int(1); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2788 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2789 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2790 return Qzero; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2791 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2792 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2793 DEFUN ("user-variable-p", Fuser_variable_p, 1, 1, 0, /* |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2794 Return t if SYMBOL names a variable intended to be set and modified by users. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2795 \(The alternative is a variable used internally in a Lisp program.) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2796 A symbol names a user variable if |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2797 \(1) the first character of its documentation is `*', or |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2798 \(2) it is customizable (`custom-variable-p' gives t), or |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2799 \(3) it names a variable alias that eventually resolves to another user variable. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2800 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2801 The GNU Emacs implementation of `user-variable-p' returns nil if there is a |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2802 loop in the chain of symbols. Since this is indistinguishable from the case |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2803 where a symbol names a non-user variable, XEmacs signals a |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2804 `cyclic-variable-indirection' error instead; use `condition-case' to catch |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2805 this error if you really want to avoid this. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2806 */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2807 (symbol)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2808 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2809 Lisp_Object mapped; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2810 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2811 if (!SYMBOLP (symbol)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2812 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2813 return Qnil; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2814 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2815 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2816 /* Called for its side-effects, we want it to signal if there's a loop. */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2817 follow_varalias_pointers (symbol, Qt); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2818 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2819 /* Look through the various aliases. */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2820 mapped = map_varalias_chain (symbol, Qt, user_variable_alias_check_fun); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2821 if (EQ (Qzero, mapped)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2822 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2823 return Qnil; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2824 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2825 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2826 assert (make_int (1) == mapped); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2827 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2828 return Qt; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2829 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2830 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
2831 |
428 | 2832 |
2833 | |
2834 /* | |
2835 I've gone ahead and partially implemented this because it's | |
2836 super-useful for dealing with the compatibility problems in supporting | |
2837 the old pointer-shape variables, and preventing people from `setq'ing | |
2838 the new variables. Any other way of handling this problem is way | |
2839 ugly, likely to be slow, and generally not something I want to waste | |
2840 my time worrying about. | |
2841 | |
2842 The interface and/or function name is sure to change before this | |
2843 gets into its final form. I currently like the way everything is | |
2844 set up and it has all the features I want it to have, except for | |
2845 one: I really want to be able to have multiple nested handlers, | |
2846 to implement an `advice'-like capability. This would allow, | |
2847 for example, a clean way of implementing `debug-if-set' or | |
2848 `debug-if-referenced' and such. | |
2849 | |
2850 NOTE NOTE NOTE NOTE NOTE NOTE NOTE: | |
2851 ************************************************************ | |
2852 **Only** the `set-value', `make-unbound', and `make-local' | |
2853 handler types are currently implemented. Implementing the | |
2854 get-value and bound-predicate handlers is somewhat tricky | |
2855 because there are lots of subfunctions (e.g. find_symbol_value()). | |
2856 find_symbol_value(), in fact, is called from outside of | |
2857 this module. You'd have to have it do this: | |
2858 | |
2859 -- check for a `bound-predicate' handler, call that if so; | |
2860 if it returns nil, return Qunbound | |
2861 -- check for a `get-value' handler and call it and return | |
2862 that value | |
2863 | |
2864 It gets even trickier when you have to deal with | |
2865 sub-subfunctions like find_symbol_value_1(), and esp. | |
2866 when you have to properly handle variable aliases, which | |
2867 can lead to lots of tricky situations. So I've just | |
2868 punted on this, since the interface isn't officially | |
2869 exported and we can get by with just a `set-value' | |
2870 handler. | |
2871 | |
2872 Actions in unimplemented handler types will correctly | |
2873 ignore any handlers, and will not fuck anything up or | |
2874 go awry. | |
2875 | |
2876 WARNING WARNING: If you do go and implement another | |
2877 type of handler, make *sure* to change | |
2878 would_be_magic_handled() so it knows about this, | |
2879 or dire things could result. | |
2880 ************************************************************ | |
2881 NOTE NOTE NOTE NOTE NOTE NOTE NOTE | |
2882 | |
2883 Real documentation is as follows. | |
2884 | |
2885 Set a magic handler for VARIABLE. | |
2886 This allows you to specify arbitrary behavior that results from | |
2887 accessing or setting a variable. For example, retrieving the | |
2888 variable's value might actually retrieve the first element off of | |
2889 a list stored in another variable, and setting the variable's value | |
2890 might add an element to the front of that list. (This is how the | |
2891 obsolete variable `unread-command-event' is implemented.) | |
2892 | |
2893 In general it is NOT good programming practice to use magic variables | |
2894 in a new package that you are designing. If you feel the need to | |
2895 do this, it's almost certainly a sign that you should be using a | |
2896 function instead of a variable. This facility is provided to allow | |
2897 a package to support obsolete variables and provide compatibility | |
2898 with similar packages with different variable names and semantics. | |
2899 By using magic handlers, you can cleanly provide obsoleteness and | |
2900 compatibility support and separate this support from the core | |
2901 routines in a package. | |
2902 | |
2903 VARIABLE should be a symbol naming the variable for which the | |
2904 magic behavior is provided. HANDLER-TYPE is a symbol specifying | |
2905 which behavior is being controlled, and HANDLER is the function | |
2906 that will be called to control this behavior. HARG is a | |
2907 value that will be passed to HANDLER but is otherwise | |
2908 uninterpreted. KEEP-EXISTING specifies what to do with existing | |
2909 handlers of the same type; nil means "erase them all", t means | |
2910 "keep them but insert at the beginning", the list (t) means | |
2911 "keep them but insert at the end", a function means "keep | |
2912 them but insert before the specified function", a list containing | |
2913 a function means "keep them but insert after the specified | |
2914 function". | |
2915 | |
2916 You can specify magic behavior for any type of variable at all, | |
2917 and for any handler types that are unspecified, the standard | |
2918 behavior applies. This allows you, for example, to use | |
2919 `defvaralias' in conjunction with this function. (For that | |
2920 matter, `defvaralias' could be implemented using this function.) | |
2921 | |
2922 The behaviors that can be specified in HANDLER-TYPE are | |
2923 | |
2924 get-value (SYM ARGS FUN HARG HANDLERS) | |
2925 This means that one of the functions `symbol-value', | |
2926 `default-value', `symbol-value-in-buffer', or | |
2927 `symbol-value-in-console' was called on SYM. | |
2928 | |
2929 set-value (SYM ARGS FUN HARG HANDLERS) | |
2930 This means that one of the functions `set' or `set-default' | |
2931 was called on SYM. | |
2932 | |
2933 bound-predicate (SYM ARGS FUN HARG HANDLERS) | |
2934 This means that one of the functions `boundp', `globally-boundp', | |
2935 or `default-boundp' was called on SYM. | |
2936 | |
2937 make-unbound (SYM ARGS FUN HARG HANDLERS) | |
2938 This means that the function `makunbound' was called on SYM. | |
2939 | |
2940 local-predicate (SYM ARGS FUN HARG HANDLERS) | |
2941 This means that the function `local-variable-p' was called | |
2942 on SYM. | |
2943 | |
2944 make-local (SYM ARGS FUN HARG HANDLERS) | |
2945 This means that one of the functions `make-local-variable', | |
2946 `make-variable-buffer-local', `kill-local-variable', | |
2947 or `kill-console-local-variable' was called on SYM. | |
2948 | |
2949 The meanings of the arguments are as follows: | |
2950 | |
2951 SYM is the symbol on which the function was called, and is always | |
2952 the first argument to the function. | |
2953 | |
2954 ARGS are the remaining arguments in the original call (i.e. all | |
2955 but the first). In the case of `set-value' in particular, | |
2956 the first element of ARGS is the value to which the variable | |
2957 is being set. In some cases, ARGS is sanitized from what was | |
2958 actually given. For example, whenever `nil' is passed to an | |
2959 argument and it means `current-buffer', the current buffer is | |
2960 substituted instead. | |
2961 | |
2962 FUN is a symbol indicating which function is being called. | |
2963 For many of the functions, you can determine the corresponding | |
2964 function of a different class using | |
2965 `symbol-function-corresponding-function'. | |
2966 | |
2967 HARG is the argument that was given in the call | |
2968 to `set-symbol-value-handler' for SYM and HANDLER-TYPE. | |
2969 | |
2970 HANDLERS is a structure containing the remaining handlers | |
2971 for the variable; to call one of them, use | |
2972 `chain-to-symbol-value-handler'. | |
2973 | |
2974 NOTE: You may *not* modify the list in ARGS, and if you want to | |
2975 keep it around after the handler function exits, you must make | |
2976 a copy using `copy-sequence'. (Same caveats for HANDLERS also.) | |
2977 */ | |
2978 | |
2979 static enum lisp_magic_handler | |
2980 decode_magic_handler_type (Lisp_Object symbol) | |
2981 { | |
2982 if (EQ (symbol, Qget_value)) return MAGIC_HANDLER_GET_VALUE; | |
2983 if (EQ (symbol, Qset_value)) return MAGIC_HANDLER_SET_VALUE; | |
2984 if (EQ (symbol, Qbound_predicate)) return MAGIC_HANDLER_BOUND_PREDICATE; | |
2985 if (EQ (symbol, Qmake_unbound)) return MAGIC_HANDLER_MAKE_UNBOUND; | |
2986 if (EQ (symbol, Qlocal_predicate)) return MAGIC_HANDLER_LOCAL_PREDICATE; | |
2987 if (EQ (symbol, Qmake_local)) return MAGIC_HANDLER_MAKE_LOCAL; | |
2988 | |
563 | 2989 invalid_constant ("Unrecognized symbol value handler type", symbol); |
1204 | 2990 RETURN_NOT_REACHED (MAGIC_HANDLER_MAX); |
428 | 2991 } |
2992 | |
2993 static enum lisp_magic_handler | |
2994 handler_type_from_function_symbol (Lisp_Object funsym, int abort_if_not_found) | |
2995 { | |
2996 if (EQ (funsym, Qsymbol_value) | |
2997 || EQ (funsym, Qdefault_value) | |
2998 || EQ (funsym, Qsymbol_value_in_buffer) | |
2999 || EQ (funsym, Qsymbol_value_in_console)) | |
3000 return MAGIC_HANDLER_GET_VALUE; | |
3001 | |
3002 if (EQ (funsym, Qset) | |
3003 || EQ (funsym, Qset_default)) | |
3004 return MAGIC_HANDLER_SET_VALUE; | |
3005 | |
3006 if (EQ (funsym, Qboundp) | |
3007 || EQ (funsym, Qglobally_boundp) | |
3008 || EQ (funsym, Qdefault_boundp)) | |
3009 return MAGIC_HANDLER_BOUND_PREDICATE; | |
3010 | |
3011 if (EQ (funsym, Qmakunbound)) | |
3012 return MAGIC_HANDLER_MAKE_UNBOUND; | |
3013 | |
3014 if (EQ (funsym, Qlocal_variable_p)) | |
3015 return MAGIC_HANDLER_LOCAL_PREDICATE; | |
3016 | |
3017 if (EQ (funsym, Qmake_variable_buffer_local) | |
3018 || EQ (funsym, Qmake_local_variable)) | |
3019 return MAGIC_HANDLER_MAKE_LOCAL; | |
3020 | |
3021 if (abort_if_not_found) | |
2500 | 3022 ABORT (); |
563 | 3023 invalid_argument ("Unrecognized symbol-value function", funsym); |
1204 | 3024 RETURN_NOT_REACHED (MAGIC_HANDLER_MAX); |
428 | 3025 } |
3026 | |
3027 static int | |
3028 would_be_magic_handled (Lisp_Object sym, Lisp_Object funsym) | |
3029 { | |
3030 /* does not take into account variable aliasing. */ | |
3031 Lisp_Object valcontents = XSYMBOL (sym)->value; | |
3032 enum lisp_magic_handler slot; | |
3033 | |
3034 if (!SYMBOL_VALUE_LISP_MAGIC_P (valcontents)) | |
3035 return 0; | |
3036 slot = handler_type_from_function_symbol (funsym, 1); | |
3037 if (slot != MAGIC_HANDLER_SET_VALUE && slot != MAGIC_HANDLER_MAKE_UNBOUND | |
3038 && slot != MAGIC_HANDLER_MAKE_LOCAL) | |
3039 /* #### temporary kludge because we haven't implemented | |
3040 lisp-magic variables completely */ | |
3041 return 0; | |
3042 return !NILP (XSYMBOL_VALUE_LISP_MAGIC (valcontents)->handler[slot]); | |
3043 } | |
3044 | |
3045 static Lisp_Object | |
3046 fetch_value_maybe_past_magic (Lisp_Object sym, | |
3047 Lisp_Object follow_past_lisp_magic) | |
3048 { | |
3049 Lisp_Object value = XSYMBOL (sym)->value; | |
3050 if (SYMBOL_VALUE_LISP_MAGIC_P (value) | |
3051 && (EQ (follow_past_lisp_magic, Qt) | |
3052 || (!NILP (follow_past_lisp_magic) | |
3053 && !would_be_magic_handled (sym, follow_past_lisp_magic)))) | |
3054 value = XSYMBOL_VALUE_LISP_MAGIC (value)->shadowed; | |
3055 return value; | |
3056 } | |
3057 | |
3058 static Lisp_Object * | |
3059 value_slot_past_magic (Lisp_Object sym) | |
3060 { | |
3061 Lisp_Object *store_pointer = &XSYMBOL (sym)->value; | |
3062 | |
3063 if (SYMBOL_VALUE_LISP_MAGIC_P (*store_pointer)) | |
3064 store_pointer = &XSYMBOL_VALUE_LISP_MAGIC (sym)->shadowed; | |
3065 return store_pointer; | |
3066 } | |
3067 | |
3068 static Lisp_Object | |
3069 maybe_call_magic_handler (Lisp_Object sym, Lisp_Object funsym, int nargs, ...) | |
3070 { | |
3071 va_list vargs; | |
3072 Lisp_Object args[20]; /* should be enough ... */ | |
3073 int i; | |
3074 enum lisp_magic_handler htype; | |
3075 Lisp_Object legerdemain; | |
3076 struct symbol_value_lisp_magic *bfwd; | |
3077 | |
440 | 3078 assert (nargs >= 0 && nargs < countof (args)); |
428 | 3079 legerdemain = XSYMBOL (sym)->value; |
3080 assert (SYMBOL_VALUE_LISP_MAGIC_P (legerdemain)); | |
3081 bfwd = XSYMBOL_VALUE_LISP_MAGIC (legerdemain); | |
3082 | |
3083 va_start (vargs, nargs); | |
3084 for (i = 0; i < nargs; i++) | |
3085 args[i] = va_arg (vargs, Lisp_Object); | |
3086 va_end (vargs); | |
3087 | |
3088 htype = handler_type_from_function_symbol (funsym, 1); | |
3089 if (NILP (bfwd->handler[htype])) | |
3090 return Qunbound; | |
3091 /* #### should be reusing the arglist, not always consing anew. | |
3092 Repeated handler invocations should not cause repeated consing. | |
3093 Doesn't matter for now, because this is just a quick implementation | |
3094 for obsolescence support. */ | |
3095 return call5 (bfwd->handler[htype], sym, Flist (nargs, args), funsym, | |
3096 bfwd->harg[htype], Qnil); | |
3097 } | |
3098 | |
3099 DEFUN ("dontusethis-set-symbol-value-handler", Fdontusethis_set_symbol_value_handler, | |
3100 3, 5, 0, /* | |
3101 Don't you dare use this. | |
3102 If you do, suffer the wrath of Ben, who is likely to rename | |
3103 this function (or change the semantics of its arguments) without | |
3104 pity, thereby invalidating your code. | |
3105 */ | |
2286 | 3106 (variable, handler_type, handler, harg, |
3107 UNUSED (keep_existing))) | |
428 | 3108 { |
3109 Lisp_Object valcontents; | |
3110 struct symbol_value_lisp_magic *bfwd; | |
3111 enum lisp_magic_handler htype; | |
3112 int i; | |
3113 | |
3114 /* #### WARNING, only some handler types are implemented. See above. | |
3115 Actions of other types will ignore a handler if it's there. | |
3116 | |
3117 #### Also, `chain-to-symbol-value-handler' and | |
3118 `symbol-function-corresponding-function' are not implemented. */ | |
3119 CHECK_SYMBOL (variable); | |
3120 CHECK_SYMBOL (handler_type); | |
3121 htype = decode_magic_handler_type (handler_type); | |
3122 valcontents = XSYMBOL (variable)->value; | |
3123 if (!SYMBOL_VALUE_LISP_MAGIC_P (valcontents)) | |
3124 { | |
3017 | 3125 bfwd = ALLOC_LCRECORD_TYPE (struct symbol_value_lisp_magic, |
428 | 3126 &lrecord_symbol_value_lisp_magic); |
3127 bfwd->magic.type = SYMVAL_LISP_MAGIC; | |
3128 for (i = 0; i < MAGIC_HANDLER_MAX; i++) | |
3129 { | |
3130 bfwd->handler[i] = Qnil; | |
3131 bfwd->harg[i] = Qnil; | |
3132 } | |
3133 bfwd->shadowed = valcontents; | |
793 | 3134 XSYMBOL (variable)->value = wrap_symbol_value_magic (bfwd); |
428 | 3135 } |
3136 else | |
3137 bfwd = XSYMBOL_VALUE_LISP_MAGIC (valcontents); | |
3138 bfwd->handler[htype] = handler; | |
3139 bfwd->harg[htype] = harg; | |
3140 | |
3141 for (i = 0; i < MAGIC_HANDLER_MAX; i++) | |
3142 if (!NILP (bfwd->handler[i])) | |
3143 break; | |
3144 | |
3145 if (i == MAGIC_HANDLER_MAX) | |
3146 /* there are no remaining handlers, so remove the structure. */ | |
3147 XSYMBOL (variable)->value = bfwd->shadowed; | |
3148 | |
3149 return Qnil; | |
3150 } | |
3151 | |
3152 | |
3153 /* functions for working with variable aliases. */ | |
3154 | |
3155 /* Follow the chain of variable aliases for SYMBOL. Return the | |
3156 resulting symbol, whose value cell is guaranteed not to be a | |
3157 symbol-value-varalias. | |
3158 | |
3159 Also maybe follow past symbol-value-lisp-magic -> symbol-value-varalias. | |
3160 If FUNSYM is t, always follow in such a case. If FUNSYM is nil, | |
3161 never follow; stop right there. Otherwise FUNSYM should be a | |
3162 recognized symbol-value function symbol; this means, follow | |
3163 unless there is a special handler for the named function. | |
3164 | |
3165 OK, there is at least one reason why it's necessary for | |
3166 FOLLOW-PAST-LISP-MAGIC to be specified correctly: So that we | |
3167 can always be sure to catch cyclic variable aliasing. If we never | |
3168 follow past Lisp magic, then if the following is done: | |
3169 | |
3170 (defvaralias 'a 'b) | |
3171 add some magic behavior to a, but not a "get-value" handler | |
3172 (defvaralias 'b 'a) | |
3173 | |
3174 then an attempt to retrieve a's or b's value would cause infinite | |
3175 looping in `symbol-value'. | |
3176 | |
3177 We (of course) can't always follow past Lisp magic, because then | |
3178 we make any variable that is lisp-magic -> varalias behave as if | |
3179 the lisp-magic is not present at all. | |
3180 */ | |
3181 | |
3182 static Lisp_Object | |
3183 follow_varalias_pointers (Lisp_Object symbol, | |
3184 Lisp_Object follow_past_lisp_magic) | |
3185 { | |
3186 #define VARALIAS_INDIRECTION_SUSPICION_LENGTH 16 | |
3187 Lisp_Object tortoise, hare, val; | |
3188 int count; | |
3189 | |
3190 /* quick out just in case */ | |
3191 if (!SYMBOL_VALUE_MAGIC_P (XSYMBOL (symbol)->value)) | |
3192 return symbol; | |
3193 | |
3194 /* Compare implementation of indirect_function(). */ | |
3195 for (hare = tortoise = symbol, count = 0; | |
3196 val = fetch_value_maybe_past_magic (hare, follow_past_lisp_magic), | |
3197 SYMBOL_VALUE_VARALIAS_P (val); | |
3198 hare = symbol_value_varalias_aliasee (XSYMBOL_VALUE_VARALIAS (val)), | |
3199 count++) | |
3200 { | |
3201 if (count < VARALIAS_INDIRECTION_SUSPICION_LENGTH) continue; | |
3202 | |
3203 if (count & 1) | |
3204 tortoise = symbol_value_varalias_aliasee | |
3205 (XSYMBOL_VALUE_VARALIAS (fetch_value_maybe_past_magic | |
3206 (tortoise, follow_past_lisp_magic))); | |
3207 if (EQ (hare, tortoise)) | |
3208 return Fsignal (Qcyclic_variable_indirection, list1 (symbol)); | |
3209 } | |
3210 | |
3211 return hare; | |
3212 } | |
3213 | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3214 /* Map FN over the chain of variable aliases for SYMBOL. If FN returns |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3215 something other than Qzero for some link in the chain, return that |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3216 immediately. Otherwise return Qzero (which is not a symbol). |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3217 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3218 FN may be called twice on the same symbol if the varalias chain is |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3219 cyclic. Prevent this by calling follow_varalias_pointers first for its |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3220 side-effects. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3221 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3222 Signals a cyclic-variable-indirection error if a cyclic structure is |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3223 detected. */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3224 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3225 static Lisp_Object |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3226 map_varalias_chain (Lisp_Object symbol, |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3227 Lisp_Object follow_past_lisp_magic, |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3228 Lisp_Object (*fn) (Lisp_Object arg)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3229 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3230 #define VARALIAS_INDIRECTION_SUSPICION_LENGTH 16 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3231 Lisp_Object tortoise, hare, val, res; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3232 int count; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3233 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3234 assert (fn); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3235 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3236 /* quick out just in case */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3237 if (!SYMBOL_VALUE_MAGIC_P (XSYMBOL (symbol)->value)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3238 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3239 return (fn)(symbol); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3240 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3241 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3242 /* Compare implementation of indirect_function(). */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3243 for (hare = tortoise = symbol, count = 0; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3244 val = fetch_value_maybe_past_magic (hare, follow_past_lisp_magic), |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3245 SYMBOL_VALUE_VARALIAS_P (val); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3246 hare = symbol_value_varalias_aliasee (XSYMBOL_VALUE_VARALIAS (val)), |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3247 count++) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3248 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3249 res = (fn) (hare); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3250 if (Qzero != res) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3251 { |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3252 return res; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3253 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3254 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3255 if (count < VARALIAS_INDIRECTION_SUSPICION_LENGTH) continue; |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3256 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3257 if (count & 1) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3258 tortoise = symbol_value_varalias_aliasee |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3259 (XSYMBOL_VALUE_VARALIAS (fetch_value_maybe_past_magic |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3260 (tortoise, follow_past_lisp_magic))); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3261 if (EQ (hare, tortoise)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3262 return Fsignal (Qcyclic_variable_indirection, list1 (symbol)); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3263 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3264 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3265 return (fn) (hare); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3266 } |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3267 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3268 /* |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3269 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3270 OED entry, 2nd edition, IPA transliterated using Kirshenbaum: |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3271 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3272 alias ('eIlI@s, '&lI@s), adv. and n. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3273 [...] |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3274 B. n. (with pl. aliases.) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3275 1. Another name, an assumed name. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3276 1605 Camden Rem. (1614) 147 An Alias or double name cannot preiudice the honest. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3277 1831 Edin. Rev. LIII. 364 He has been assuming various aliases. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3278 1861 Macaulay Hist. Eng. V. 92 The monk who was sometimes called Harrison |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3279 and sometimes went by the alias of Johnson. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3280 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3281 The alias is the fake name. Let's try to follow that usage in our |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3282 documentation. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3283 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3284 */ |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3285 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3286 DEFUN ("defvaralias", Fdefvaralias, 2, 3, 0, /* |
428 | 3287 Define a variable as an alias for another variable. |
3288 Thenceforth, any operations performed on VARIABLE will actually be | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3289 performed on ALIASED. Both VARIABLE and ALIASED should be symbols. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3290 If ALIASED is nil and VARIABLE is an existing alias, remove that alias. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3291 ALIASED can itself be an alias, and the chain of variable aliases |
428 | 3292 will be followed appropriately. |
3293 If VARIABLE already has a value, this value will be shadowed | |
3294 until the alias is removed, at which point it will be restored. | |
3295 Currently VARIABLE cannot be a built-in variable, a variable that | |
3296 has a buffer-local value in any buffer, or the symbols nil or t. | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3297 \(ALIASED, however, can be any type of variable.) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3298 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3299 Optional argument DOCSTRING is documentation for VARIABLE in its use as an |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3300 alias for ALIASED. The XEmacs help code ignores this documentation, using |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3301 the documentation of ALIASED instead, and the docstring, if specified, is |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3302 not shadowed in the same way that the value is. Only use it if you know |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3303 what you're doing. |
428 | 3304 */ |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3305 (variable, aliased, docstring)) |
428 | 3306 { |
3307 struct symbol_value_varalias *bfwd; | |
3308 Lisp_Object valcontents; | |
3309 | |
3310 CHECK_SYMBOL (variable); | |
3311 reject_constant_symbols (variable, Qunbound, 0, Qt); | |
3312 | |
3313 valcontents = XSYMBOL (variable)->value; | |
3314 | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3315 if (NILP (aliased)) |
428 | 3316 { |
3317 if (SYMBOL_VALUE_VARALIAS_P (valcontents)) | |
3318 { | |
3319 XSYMBOL (variable)->value = | |
3320 symbol_value_varalias_shadowed | |
3321 (XSYMBOL_VALUE_VARALIAS (valcontents)); | |
3322 } | |
3323 return Qnil; | |
3324 } | |
3325 | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3326 CHECK_SYMBOL (aliased); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3327 |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3328 if (!NILP (docstring)) |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3329 Fput (variable, Qvariable_documentation, docstring); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3330 |
428 | 3331 if (SYMBOL_VALUE_VARALIAS_P (valcontents)) |
3332 { | |
3333 /* transmogrify */ | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3334 XSYMBOL_VALUE_VARALIAS (valcontents)->aliasee = aliased; |
428 | 3335 return Qnil; |
3336 } | |
3337 | |
3338 if (SYMBOL_VALUE_MAGIC_P (valcontents) | |
3339 && !UNBOUNDP (valcontents)) | |
563 | 3340 invalid_change ("Variable is magic and cannot be aliased", variable); |
428 | 3341 reject_constant_symbols (variable, Qunbound, 0, Qt); |
3342 | |
3017 | 3343 bfwd = ALLOC_LCRECORD_TYPE (struct symbol_value_varalias, |
428 | 3344 &lrecord_symbol_value_varalias); |
3345 bfwd->magic.type = SYMVAL_VARALIAS; | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3346 bfwd->aliasee = aliased; |
428 | 3347 bfwd->shadowed = valcontents; |
3348 | |
793 | 3349 valcontents = wrap_symbol_value_magic (bfwd); |
428 | 3350 XSYMBOL (variable)->value = valcontents; |
3351 return Qnil; | |
3352 } | |
3353 | |
3354 DEFUN ("variable-alias", Fvariable_alias, 1, 2, 0, /* | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3355 If VARIABLE is an alias of another variable, return that variable. |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3356 VARIABLE should be a symbol. If VARIABLE is not an alias, return nil. |
428 | 3357 Variable aliases are created with `defvaralias'. See also |
3358 `indirect-variable'. | |
3359 */ | |
3360 (variable, follow_past_lisp_magic)) | |
3361 { | |
3362 Lisp_Object valcontents; | |
3363 | |
3364 CHECK_SYMBOL (variable); | |
3365 if (!NILP (follow_past_lisp_magic) && !EQ (follow_past_lisp_magic, Qt)) | |
3366 { | |
3367 CHECK_SYMBOL (follow_past_lisp_magic); | |
3368 handler_type_from_function_symbol (follow_past_lisp_magic, 0); | |
3369 } | |
3370 | |
3371 valcontents = fetch_value_maybe_past_magic (variable, | |
3372 follow_past_lisp_magic); | |
3373 | |
3374 if (SYMBOL_VALUE_VARALIAS_P (valcontents)) | |
3375 return symbol_value_varalias_aliasee | |
3376 (XSYMBOL_VALUE_VARALIAS (valcontents)); | |
3377 else | |
3378 return Qnil; | |
3379 } | |
3380 | |
3381 DEFUN ("indirect-variable", Findirect_variable, 1, 2, 0, /* | |
3382 Return the variable at the end of OBJECT's variable-alias chain. | |
3383 If OBJECT is a symbol, follow all variable aliases and return | |
3384 the final (non-aliased) symbol. Variable aliases are created with | |
3385 the function `defvaralias'. | |
3386 If OBJECT is not a symbol, just return it. | |
3387 Signal a cyclic-variable-indirection error if there is a loop in the | |
3388 variable chain of symbols. | |
3389 */ | |
3390 (object, follow_past_lisp_magic)) | |
3391 { | |
3392 if (!SYMBOLP (object)) | |
3393 return object; | |
3394 if (!NILP (follow_past_lisp_magic) && !EQ (follow_past_lisp_magic, Qt)) | |
3395 { | |
3396 CHECK_SYMBOL (follow_past_lisp_magic); | |
3397 handler_type_from_function_symbol (follow_past_lisp_magic, 0); | |
3398 } | |
3399 return follow_varalias_pointers (object, follow_past_lisp_magic); | |
3400 } | |
3401 | |
1674 | 3402 DEFUN ("variable-binding-locus", Fvariable_binding_locus, 1, 1, 0, /* |
3403 Return a value indicating where VARIABLE's current binding comes from. | |
3404 If the current binding is buffer-local, the value is the current buffer. | |
3405 If the current binding is global (the default), the value is nil. | |
3406 */ | |
3407 (variable)) | |
3408 { | |
3409 Lisp_Object valcontents; | |
3410 | |
3411 CHECK_SYMBOL (variable); | |
3412 variable = Findirect_variable (variable, Qnil); | |
3413 | |
3414 /* Make sure the current binding is actually swapped in. */ | |
3415 find_symbol_value (variable); | |
3416 | |
3417 valcontents = XSYMBOL (variable)->value; | |
3418 | |
3419 if (SYMBOL_VALUE_MAGIC_P (valcontents) | |
3420 && ((XSYMBOL_VALUE_MAGIC_TYPE (valcontents) == SYMVAL_BUFFER_LOCAL) | |
3421 || (XSYMBOL_VALUE_MAGIC_TYPE (valcontents) == SYMVAL_SOME_BUFFER_LOCAL)) | |
3422 && (!NILP (Flocal_variable_p (variable, Fcurrent_buffer (), Qnil)))) | |
3423 return Fcurrent_buffer (); | |
3424 else | |
3425 return Qnil; | |
3426 } | |
428 | 3427 |
3428 /************************************************************************/ | |
3429 /* initialization */ | |
3430 /************************************************************************/ | |
3431 | |
3432 /* A dumped XEmacs image has a lot more than 1511 symbols. Last | |
3433 estimate was that there were actually around 6300. So let's try | |
3434 making this bigger and see if we get better hashing behavior. */ | |
3435 #define OBARRAY_SIZE 16411 | |
3436 | |
3437 #ifndef Qzero | |
3438 Lisp_Object Qzero; | |
3439 #endif | |
3440 #ifndef Qnull_pointer | |
3441 Lisp_Object Qnull_pointer; | |
3442 #endif | |
3443 | |
3263 | 3444 #ifndef NEW_GC |
428 | 3445 /* some losing systems can't have static vars at function scope... */ |
442 | 3446 static const struct symbol_value_magic guts_of_unbound_marker = |
3447 { /* struct symbol_value_magic */ | |
3024 | 3448 { /* struct old_lcrecord_header */ |
442 | 3449 { /* struct lrecord_header */ |
3450 lrecord_type_symbol_value_forward, /* lrecord_type_index */ | |
3451 1, /* mark bit */ | |
3452 1, /* c_readonly bit */ | |
3453 1, /* lisp_readonly bit */ | |
3454 }, | |
3455 0, /* next */ | |
3456 0, /* uid */ | |
3457 0, /* free */ | |
3458 }, | |
3459 0, /* value */ | |
3460 SYMVAL_UNBOUND_MARKER | |
3461 }; | |
3263 | 3462 #endif /* not NEW_GC */ |
428 | 3463 |
3464 void | |
3465 init_symbols_once_early (void) | |
3466 { | |
442 | 3467 INIT_LRECORD_IMPLEMENTATION (symbol); |
3468 INIT_LRECORD_IMPLEMENTATION (symbol_value_forward); | |
3469 INIT_LRECORD_IMPLEMENTATION (symbol_value_buffer_local); | |
3470 INIT_LRECORD_IMPLEMENTATION (symbol_value_lisp_magic); | |
3471 INIT_LRECORD_IMPLEMENTATION (symbol_value_varalias); | |
3472 | |
1204 | 3473 reinit_symbols_early (); |
428 | 3474 |
3475 /* Bootstrapping problem: Qnil isn't set when make_string_nocopy is | |
3476 called the first time. */ | |
867 | 3477 Qnil = Fmake_symbol (make_string_nocopy ((const Ibyte *) "nil", 3)); |
793 | 3478 XSTRING_PLIST (XSYMBOL (Qnil)->name) = Qnil; |
428 | 3479 XSYMBOL (Qnil)->value = Qnil; /* Nihil ex nihil */ |
3480 XSYMBOL (Qnil)->plist = Qnil; | |
3481 | |
3482 Vobarray = make_vector (OBARRAY_SIZE, Qzero); | |
3483 initial_obarray = Vobarray; | |
3484 staticpro (&initial_obarray); | |
3485 /* Intern nil in the obarray */ | |
3486 { | |
793 | 3487 unsigned int hash = hash_string (XSTRING_DATA (XSYMBOL (Qnil)->name), 3); |
428 | 3488 XVECTOR_DATA (Vobarray)[hash % OBARRAY_SIZE] = Qnil; |
3489 } | |
3490 | |
3491 { | |
3492 /* Required to get around a GCC syntax error on certain | |
3493 architectures */ | |
3263 | 3494 #ifdef NEW_GC |
2720 | 3495 struct symbol_value_magic *tem = (struct symbol_value_magic *) |
3496 mc_alloc (sizeof (struct symbol_value_magic)); | |
3497 MARK_LRECORD_AS_LISP_READONLY (tem); | |
3498 MARK_LRECORD_AS_NOT_FREE (tem); | |
3499 tem->header.type = lrecord_type_symbol_value_forward; | |
3500 mcpro (wrap_pointer_1 (tem)); | |
3501 tem->value = 0; | |
3502 tem->type = SYMVAL_UNBOUND_MARKER; | |
2994 | 3503 #ifdef ALLOC_TYPE_STATS |
2775 | 3504 inc_lrecord_stats (sizeof (struct symbol_value_magic), |
3505 (const struct lrecord_header *) tem); | |
2994 | 3506 #endif /* ALLOC_TYPE_STATS */ |
3263 | 3507 #else /* not NEW_GC */ |
442 | 3508 const struct symbol_value_magic *tem = &guts_of_unbound_marker; |
3263 | 3509 #endif /* not NEW_GC */ |
428 | 3510 |
793 | 3511 Qunbound = wrap_symbol_value_magic (tem); |
428 | 3512 } |
3513 | |
3514 XSYMBOL (Qnil)->function = Qunbound; | |
3515 | |
563 | 3516 DEFSYMBOL (Qt); |
444 | 3517 XSYMBOL (Qt)->value = Qt; /* Veritas aeterna */ |
428 | 3518 Vquit_flag = Qnil; |
3519 | |
1204 | 3520 dump_add_root_lisp_object (&Qnil); |
3521 dump_add_root_lisp_object (&Qunbound); | |
3522 dump_add_root_lisp_object (&Vquit_flag); | |
428 | 3523 } |
3524 | |
3525 void | |
1204 | 3526 reinit_symbols_early (void) |
440 | 3527 { |
3528 } | |
3529 | |
442 | 3530 static void |
3531 defsymbol_massage_name_1 (Lisp_Object *location, const char *name, int dump_p, | |
3532 int multiword_predicate_p) | |
3533 { | |
3534 char temp[500]; | |
3535 int len = strlen (name) - 1; | |
3536 int i; | |
3537 | |
3538 if (multiword_predicate_p) | |
647 | 3539 assert (len + 1 < (int) sizeof (temp)); |
442 | 3540 else |
647 | 3541 assert (len < (int) sizeof (temp)); |
442 | 3542 strcpy (temp, name + 1); /* Remove initial Q */ |
3543 if (multiword_predicate_p) | |
3544 { | |
3545 strcpy (temp + len - 1, "_p"); | |
3546 len++; | |
3547 } | |
3548 for (i = 0; i < len; i++) | |
3549 if (temp[i] == '_') | |
3550 temp[i] = '-'; | |
867 | 3551 *location = Fintern (make_string ((const Ibyte *) temp, len), Qnil); |
442 | 3552 if (dump_p) |
3553 staticpro (location); | |
3554 else | |
3555 staticpro_nodump (location); | |
3556 } | |
3557 | |
440 | 3558 void |
442 | 3559 defsymbol_massage_name_nodump (Lisp_Object *location, const char *name) |
3560 { | |
3561 defsymbol_massage_name_1 (location, name, 0, 0); | |
3562 } | |
3563 | |
3564 void | |
3565 defsymbol_massage_name (Lisp_Object *location, const char *name) | |
428 | 3566 { |
442 | 3567 defsymbol_massage_name_1 (location, name, 1, 0); |
3568 } | |
3569 | |
3570 void | |
3571 defsymbol_massage_multiword_predicate_nodump (Lisp_Object *location, | |
3572 const char *name) | |
3573 { | |
3574 defsymbol_massage_name_1 (location, name, 0, 1); | |
3575 } | |
3576 | |
3577 void | |
3578 defsymbol_massage_multiword_predicate (Lisp_Object *location, const char *name) | |
3579 { | |
3580 defsymbol_massage_name_1 (location, name, 1, 1); | |
3581 } | |
3582 | |
3583 void | |
3584 defsymbol_nodump (Lisp_Object *location, const char *name) | |
3585 { | |
867 | 3586 *location = Fintern (make_string_nocopy ((const Ibyte *) name, |
428 | 3587 strlen (name)), |
3588 Qnil); | |
3589 staticpro_nodump (location); | |
3590 } | |
3591 | |
3592 void | |
442 | 3593 defsymbol (Lisp_Object *location, const char *name) |
428 | 3594 { |
867 | 3595 *location = Fintern (make_string_nocopy ((const Ibyte *) name, |
428 | 3596 strlen (name)), |
3597 Qnil); | |
3598 staticpro (location); | |
3599 } | |
3600 | |
3601 void | |
442 | 3602 defkeyword (Lisp_Object *location, const char *name) |
428 | 3603 { |
3604 defsymbol (location, name); | |
3605 Fset (*location, *location); | |
3606 } | |
3607 | |
442 | 3608 void |
3609 defkeyword_massage_name (Lisp_Object *location, const char *name) | |
3610 { | |
3611 char temp[500]; | |
3612 int len = strlen (name); | |
3613 | |
647 | 3614 assert (len < (int) sizeof (temp)); |
442 | 3615 strcpy (temp, name); |
3616 temp[1] = ':'; /* it's an underscore in the C variable */ | |
3617 | |
3618 defsymbol_massage_name (location, temp); | |
3619 Fset (*location, *location); | |
3620 } | |
3621 | |
428 | 3622 #ifdef DEBUG_XEMACS |
930 | 3623 /* Check that nobody spazzed writing a builtin (non-module) DEFUN. */ |
428 | 3624 static void |
3625 check_sane_subr (Lisp_Subr *subr, Lisp_Object sym) | |
3626 { | |
930 | 3627 if (!initialized) { |
3628 assert (subr->min_args >= 0); | |
3629 assert (subr->min_args <= SUBR_MAX_ARGS); | |
3630 | |
3631 if (subr->max_args != MANY && | |
3632 subr->max_args != UNEVALLED) | |
3633 { | |
3634 /* Need to fix lisp.h and eval.c if SUBR_MAX_ARGS too small */ | |
3635 assert (subr->max_args <= SUBR_MAX_ARGS); | |
3636 assert (subr->min_args <= subr->max_args); | |
3637 } | |
3638 assert (UNBOUNDP (XSYMBOL (sym)->function)); | |
3639 } | |
428 | 3640 } |
3641 #else | |
3642 #define check_sane_subr(subr, sym) /* nothing */ | |
3643 #endif | |
3644 | |
3645 #ifdef HAVE_SHLIB | |
3263 | 3646 #ifndef NEW_GC |
428 | 3647 /* |
3648 * If we are not in a pure undumped Emacs, we need to make a duplicate of | |
3649 * the subr. This is because the only time this function will be called | |
3650 * in a running Emacs is when a dynamically loaded module is adding a | |
3651 * subr, and we need to make sure that the subr is in allocated, Lisp- | |
3652 * accessible memory. The address assigned to the static subr struct | |
3653 * in the shared object will be a trampoline address, so we need to create | |
3654 * a copy here to ensure that a real address is used. | |
3655 * | |
3656 * Once we have copied everything across, we re-use the original static | |
3657 * structure to store a pointer to the newly allocated one. This will be | |
3658 * used in emodules.c by emodules_doc_subr() to find a pointer to the | |
442 | 3659 * allocated object so that we can set its doc string properly. |
428 | 3660 * |
442 | 3661 * NOTE: We don't actually use the DOC pointer here any more, but we did |
428 | 3662 * in an earlier implementation of module support. There is no harm in |
3663 * setting it here in case we ever need it in future implementations. | |
3664 * subr->doc will point to the new subr structure that was allocated. | |
442 | 3665 * Code can then get this value from the static subr structure and use |
428 | 3666 * it if required. |
3667 * | |
442 | 3668 * FIXME: Should newsubr be staticpro()'ed? I don't think so but I need |
428 | 3669 * a guru to check. |
3670 */ | |
930 | 3671 #define check_module_subr(subr) \ |
3672 do { \ | |
3673 if (initialized) { \ | |
3674 Lisp_Subr *newsubr; \ | |
3675 Lisp_Object f; \ | |
3676 \ | |
3677 if (subr->min_args < 0) \ | |
3678 signal_ferror (Qdll_error, "%s min_args (%hd) too small", \ | |
3679 subr_name (subr), subr->min_args); \ | |
3680 if (subr->min_args > SUBR_MAX_ARGS) \ | |
3681 signal_ferror (Qdll_error, "%s min_args (%hd) too big (max = %d)", \ | |
3682 subr_name (subr), subr->min_args, SUBR_MAX_ARGS); \ | |
3683 \ | |
3684 if (subr->max_args != MANY && \ | |
3685 subr->max_args != UNEVALLED) \ | |
3686 { \ | |
3687 /* Need to fix lisp.h and eval.c if SUBR_MAX_ARGS too small */ \ | |
3688 if (subr->max_args > SUBR_MAX_ARGS) \ | |
3689 signal_ferror (Qdll_error, "%s max_args (%hd) too big (max = %d)", \ | |
3690 subr_name (subr), subr->max_args, SUBR_MAX_ARGS); \ | |
3691 if (subr->min_args > subr->max_args) \ | |
3692 signal_ferror (Qdll_error, "%s min_args (%hd) > max_args (%hd)", \ | |
3693 subr_name (subr), subr->min_args, subr->max_args); \ | |
3694 } \ | |
3695 \ | |
3696 f = XSYMBOL (sym)->function; \ | |
3697 if (!UNBOUNDP (f) && (!CONSP (f) || !EQ (XCAR (f), Qautoload))) \ | |
3698 signal_ferror (Qdll_error, "Attempt to redefine %s", subr_name (subr)); \ | |
3699 \ | |
2367 | 3700 newsubr = xnew (Lisp_Subr); \ |
930 | 3701 memcpy (newsubr, subr, sizeof (Lisp_Subr)); \ |
3702 subr->doc = (const char *)newsubr; \ | |
3703 subr = newsubr; \ | |
3704 } \ | |
428 | 3705 } while (0) |
3263 | 3706 #else /* NEW_GC */ |
2963 | 3707 /* |
3708 * If we have the new allocator enabled, we do not need to make a | |
3709 * duplicate of the subr. The new allocator already does allocate all | |
3710 * subrs in Lisp-accessible memory rather than have it in the static | |
3711 * subr struct. | |
3712 * | |
3713 * NOTE: The DOC pointer is not set here as described above. | |
3714 */ | |
3715 #define check_module_subr(subr) \ | |
3716 do { \ | |
3717 if (initialized) { \ | |
3718 Lisp_Object f; \ | |
3719 \ | |
3720 if (subr->min_args < 0) \ | |
3721 signal_ferror (Qdll_error, "%s min_args (%hd) too small", \ | |
3722 subr_name (subr), subr->min_args); \ | |
3723 if (subr->min_args > SUBR_MAX_ARGS) \ | |
3724 signal_ferror (Qdll_error, "%s min_args (%hd) too big (max = %d)", \ | |
3725 subr_name (subr), subr->min_args, SUBR_MAX_ARGS); \ | |
3726 \ | |
3727 if (subr->max_args != MANY && \ | |
3728 subr->max_args != UNEVALLED) \ | |
3729 { \ | |
3730 /* Need to fix lisp.h and eval.c if SUBR_MAX_ARGS too small */ \ | |
3731 if (subr->max_args > SUBR_MAX_ARGS) \ | |
3732 signal_ferror (Qdll_error, "%s max_args (%hd) too big (max = %d)", \ | |
3733 subr_name (subr), subr->max_args, SUBR_MAX_ARGS); \ | |
3734 if (subr->min_args > subr->max_args) \ | |
3735 signal_ferror (Qdll_error, "%s min_args (%hd) > max_args (%hd)", \ | |
3736 subr_name (subr), subr->min_args, subr->max_args); \ | |
3737 } \ | |
3738 \ | |
3739 f = XSYMBOL (sym)->function; \ | |
3740 if (!UNBOUNDP (f) && (!CONSP (f) || !EQ (XCAR (f), Qautoload))) \ | |
3741 signal_ferror (Qdll_error, "Attempt to redefine %s", subr_name (subr)); \ | |
3742 } \ | |
3743 } while (0) | |
3263 | 3744 #endif /* NEW_GC */ |
428 | 3745 #else /* ! HAVE_SHLIB */ |
930 | 3746 #define check_module_subr(subr) |
428 | 3747 #endif |
3748 | |
3749 void | |
3750 defsubr (Lisp_Subr *subr) | |
3751 { | |
3752 Lisp_Object sym = intern (subr_name (subr)); | |
3753 Lisp_Object fun; | |
3754 | |
3755 check_sane_subr (subr, sym); | |
930 | 3756 check_module_subr (subr); |
428 | 3757 |
793 | 3758 fun = wrap_subr (subr); |
428 | 3759 XSYMBOL (sym)->function = fun; |
996 | 3760 |
3761 #ifdef HAVE_SHLIB | |
3762 /* If it is declared in a module, update the load history */ | |
3763 if (initialized) | |
3764 LOADHIST_ATTACH (sym); | |
3765 #endif | |
428 | 3766 } |
3767 | |
3768 /* Define a lisp macro using a Lisp_Subr. */ | |
3769 void | |
3770 defsubr_macro (Lisp_Subr *subr) | |
3771 { | |
3772 Lisp_Object sym = intern (subr_name (subr)); | |
3773 Lisp_Object fun; | |
3774 | |
3775 check_sane_subr (subr, sym); | |
930 | 3776 check_module_subr (subr); |
428 | 3777 |
793 | 3778 fun = wrap_subr (subr); |
428 | 3779 XSYMBOL (sym)->function = Fcons (Qmacro, fun); |
996 | 3780 |
3781 #ifdef HAVE_SHLIB | |
3782 /* If it is declared in a module, update the load history */ | |
3783 if (initialized) | |
3784 LOADHIST_ATTACH (sym); | |
3785 #endif | |
428 | 3786 } |
3787 | |
442 | 3788 static void |
3789 deferror_1 (Lisp_Object *symbol, const char *name, const char *messuhhj, | |
3790 Lisp_Object inherits_from, int massage_p) | |
428 | 3791 { |
3792 Lisp_Object conds; | |
442 | 3793 if (massage_p) |
3794 defsymbol_massage_name (symbol, name); | |
3795 else | |
3796 defsymbol (symbol, name); | |
428 | 3797 |
3798 assert (SYMBOLP (inherits_from)); | |
3799 conds = Fget (inherits_from, Qerror_conditions, Qnil); | |
3800 Fput (*symbol, Qerror_conditions, Fcons (*symbol, conds)); | |
771 | 3801 /* NOT build_msg_string (). This function is called at load time |
428 | 3802 and the string needs to get translated at run time. (This happens |
3803 in the function (display-error) in cmdloop.el.) */ | |
771 | 3804 Fput (*symbol, Qerror_message, build_msg_string (messuhhj)); |
428 | 3805 } |
3806 | |
3807 void | |
442 | 3808 deferror (Lisp_Object *symbol, const char *name, const char *messuhhj, |
3809 Lisp_Object inherits_from) | |
3810 { | |
3811 deferror_1 (symbol, name, messuhhj, inherits_from, 0); | |
3812 } | |
3813 | |
3814 void | |
3815 deferror_massage_name (Lisp_Object *symbol, const char *name, | |
3816 const char *messuhhj, Lisp_Object inherits_from) | |
3817 { | |
3818 deferror_1 (symbol, name, messuhhj, inherits_from, 1); | |
3819 } | |
3820 | |
3821 void | |
3822 deferror_massage_name_and_message (Lisp_Object *symbol, const char *name, | |
3823 Lisp_Object inherits_from) | |
3824 { | |
3825 char temp[500]; | |
3826 int i; | |
3827 int len = strlen (name) - 1; | |
3828 | |
647 | 3829 assert (len < (int) sizeof (temp)); |
442 | 3830 strcpy (temp, name + 1); /* Remove initial Q */ |
3831 temp[0] = toupper (temp[0]); | |
3832 for (i = 0; i < len; i++) | |
3833 if (temp[i] == '_') | |
3834 temp[i] = ' '; | |
3835 | |
3836 deferror_1 (symbol, name, temp, inherits_from, 1); | |
3837 } | |
3838 | |
3839 void | |
428 | 3840 syms_of_symbols (void) |
3841 { | |
442 | 3842 DEFSYMBOL (Qvariable_documentation); |
3843 DEFSYMBOL (Qvariable_domain); /* I18N3 */ | |
3844 DEFSYMBOL (Qad_advice_info); | |
3845 DEFSYMBOL (Qad_activate); | |
3846 | |
3847 DEFSYMBOL (Qget_value); | |
3848 DEFSYMBOL (Qset_value); | |
3849 DEFSYMBOL (Qbound_predicate); | |
3850 DEFSYMBOL (Qmake_unbound); | |
3851 DEFSYMBOL (Qlocal_predicate); | |
3852 DEFSYMBOL (Qmake_local); | |
3853 | |
3854 DEFSYMBOL (Qboundp); | |
3855 DEFSYMBOL (Qglobally_boundp); | |
3856 DEFSYMBOL (Qmakunbound); | |
3857 DEFSYMBOL (Qsymbol_value); | |
3858 DEFSYMBOL (Qset); | |
3859 DEFSYMBOL (Qsetq_default); | |
3860 DEFSYMBOL (Qdefault_boundp); | |
3861 DEFSYMBOL (Qdefault_value); | |
3862 DEFSYMBOL (Qset_default); | |
3863 DEFSYMBOL (Qmake_variable_buffer_local); | |
3864 DEFSYMBOL (Qmake_local_variable); | |
3865 DEFSYMBOL (Qkill_local_variable); | |
3866 DEFSYMBOL (Qkill_console_local_variable); | |
3867 DEFSYMBOL (Qsymbol_value_in_buffer); | |
3868 DEFSYMBOL (Qsymbol_value_in_console); | |
3869 DEFSYMBOL (Qlocal_variable_p); | |
3870 DEFSYMBOL (Qconst_integer); | |
3871 DEFSYMBOL (Qconst_boolean); | |
3872 DEFSYMBOL (Qconst_object); | |
3873 DEFSYMBOL (Qconst_specifier); | |
3874 DEFSYMBOL (Qdefault_buffer); | |
3875 DEFSYMBOL (Qcurrent_buffer); | |
3876 DEFSYMBOL (Qconst_current_buffer); | |
3877 DEFSYMBOL (Qdefault_console); | |
3878 DEFSYMBOL (Qselected_console); | |
3879 DEFSYMBOL (Qconst_selected_console); | |
428 | 3880 |
3881 DEFSUBR (Fintern); | |
3882 DEFSUBR (Fintern_soft); | |
3883 DEFSUBR (Funintern); | |
3884 DEFSUBR (Fmapatoms); | |
3885 DEFSUBR (Fapropos_internal); | |
3886 | |
3887 DEFSUBR (Fsymbol_function); | |
3888 DEFSUBR (Fsymbol_plist); | |
3889 DEFSUBR (Fsymbol_name); | |
3890 DEFSUBR (Fmakunbound); | |
3891 DEFSUBR (Ffmakunbound); | |
3892 DEFSUBR (Fboundp); | |
3893 DEFSUBR (Fglobally_boundp); | |
3894 DEFSUBR (Ffboundp); | |
3895 DEFSUBR (Ffset); | |
3896 DEFSUBR (Fdefine_function); | |
3897 Ffset (intern ("defalias"), intern ("define-function")); | |
3368 | 3898 DEFSUBR (Fsubr_name); |
4336
cdc2f70d4319
Provide #'special-form-p, for the use of advice.el, perhaps other files.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3497
diff
changeset
|
3899 DEFSUBR (Fspecial_form_p); |
428 | 3900 DEFSUBR (Fsetplist); |
3901 DEFSUBR (Fsymbol_value_in_buffer); | |
3902 DEFSUBR (Fsymbol_value_in_console); | |
3903 DEFSUBR (Fbuilt_in_variable_type); | |
3904 DEFSUBR (Fsymbol_value); | |
3905 DEFSUBR (Fset); | |
3906 DEFSUBR (Fdefault_boundp); | |
3907 DEFSUBR (Fdefault_value); | |
3908 DEFSUBR (Fset_default); | |
3909 DEFSUBR (Fsetq_default); | |
3910 DEFSUBR (Fmake_variable_buffer_local); | |
3911 DEFSUBR (Fmake_local_variable); | |
3912 DEFSUBR (Fkill_local_variable); | |
3913 DEFSUBR (Fkill_console_local_variable); | |
3914 DEFSUBR (Flocal_variable_p); | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3915 DEFSUBR (Fcustom_variable_p); |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4355
diff
changeset
|
3916 DEFSUBR (Fuser_variable_p); |
428 | 3917 DEFSUBR (Fdefvaralias); |
3918 DEFSUBR (Fvariable_alias); | |
3919 DEFSUBR (Findirect_variable); | |
1674 | 3920 DEFSUBR (Fvariable_binding_locus); |
428 | 3921 DEFSUBR (Fdontusethis_set_symbol_value_handler); |
3922 } | |
3923 | |
3924 /* Create and initialize a Lisp variable whose value is forwarded to C data */ | |
3925 void | |
442 | 3926 defvar_magic (const char *symbol_name, const struct symbol_value_forward *magic) |
428 | 3927 { |
442 | 3928 Lisp_Object sym; |
428 | 3929 |
996 | 3930 #ifdef HAVE_SHLIB |
428 | 3931 /* |
3932 * As with defsubr(), this will only be called in a dumped Emacs when | |
3933 * we are adding variables from a dynamically loaded module. That means | |
3934 * we can't use purespace. Take that into account. | |
3935 */ | |
3936 if (initialized) | |
996 | 3937 { |
3938 sym = Fintern (build_string (symbol_name), Qnil); | |
3939 LOADHIST_ATTACH (sym); | |
3940 } | |
428 | 3941 else |
3942 #endif | |
867 | 3943 sym = Fintern (make_string_nocopy ((const Ibyte *) symbol_name, |
428 | 3944 strlen (symbol_name)), Qnil); |
3945 | |
793 | 3946 XSYMBOL (sym)->value = wrap_pointer_1 (magic); |
428 | 3947 } |
3948 | |
3949 void | |
3950 vars_of_symbols (void) | |
3951 { | |
3952 DEFVAR_LISP ("obarray", &Vobarray /* | |
3953 Symbol table for use by `intern' and `read'. | |
3954 It is a vector whose length ought to be prime for best results. | |
3955 The vector's contents don't make sense if examined from Lisp programs; | |
3956 to find all the symbols in an obarray, use `mapatoms'. | |
3957 */ ); | |
3958 /* obarray has been initialized long before */ | |
3959 } |