XEmacs 21.4.15
[chise/xemacs-chise.git.1] / src / symbols.c
index 2667fce..58cf76f 100644 (file)
@@ -1,6 +1,6 @@
 /* "intern" and friends -- moved here from lread.c and data.c
    Copyright (C) 1985-1989, 1992-1994 Free Software Foundation, Inc.
-   Copyright (C) 1995 Ben Wing.
+   Copyright (C) 1995, 2000 Ben Wing.
 
 This file is part of XEmacs.
 
@@ -198,8 +198,8 @@ intern (const char *str)
 DEFUN ("intern", Fintern, 1, 2, 0, /*
 Return the canonical symbol whose name is STRING.
 If there is none, one is created by this function and returned.
-A second optional argument specifies the obarray to use;
-it defaults to the value of `obarray'.
+Optional second argument OBARRAY specifies the obarray to use;
+it defaults to the value of the variable `obarray'.
 */
        (string, obarray))
 {
@@ -245,8 +245,8 @@ DEFUN ("intern-soft", Fintern_soft, 1, 2, 0, /*
 Return the canonical symbol named NAME, or nil if none exists.
 NAME may be a string or a symbol.  If it is a symbol, that exact
 symbol is searched for.
-A second optional argument specifies the obarray to use;
-it defaults to the value of `obarray'.
+Optional second argument OBARRAY specifies the obarray to use;
+it defaults to the value of the variable `obarray'.
 */
        (name, obarray))
 {
@@ -278,7 +278,7 @@ Delete the symbol named NAME, if any, from OBARRAY.
 The value is t if a symbol was found and deleted, nil otherwise.
 NAME may be a string or a symbol.  If it is a symbol, that symbol
 is deleted, if it belongs to OBARRAY--no other symbol is deleted.
-OBARRAY defaults to the value of the variable `obarray'
+OBARRAY defaults to the value of the variable `obarray'.
 */
        (name, obarray))
 {
@@ -449,11 +449,15 @@ OBARRAY defaults to the value of `obarray'.
 */
        (function, obarray))
 {
+  struct gcpro gcpro1;
+
   if (NILP (obarray))
     obarray = Vobarray;
   obarray = check_obarray (obarray);
 
+  GCPRO1 (obarray);
   map_obarray (obarray, mapatoms_1, &function);
+  UNGCPRO;
   return Qnil;
 }
 
@@ -486,23 +490,24 @@ apropos_mapper (Lisp_Object symbol, void *arg)
 }
 
 DEFUN ("apropos-internal", Fapropos_internal, 1, 2, 0, /*
-Show all symbols whose names contain match for REGEXP.
-If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL)
- is done for each symbol and a symbol is mentioned only if that
- returns non-nil.
-Return list of symbols found.
+Return a list of all symbols whose names contain match for REGEXP.
+If optional 2nd arg PREDICATE is non-nil, only symbols for which
+\(funcall PREDICATE SYMBOL) returns non-nil are returned.
 */
        (regexp, predicate))
 {
   struct appropos_mapper_closure closure;
+  struct gcpro gcpro1;
 
   CHECK_STRING (regexp);
 
   closure.regexp = regexp;
   closure.predicate = predicate;
   closure.accumulation = Qnil;
+  GCPRO1 (closure.accumulation);
   map_obarray (Vobarray, apropos_mapper, &closure);
   closure.accumulation = Fsort (closure.accumulation, Qstring_lessp);
+  UNGCPRO;
   return closure.accumulation;
 }
 
@@ -785,10 +790,15 @@ Set SYMBOL's property list to NEWPLIST, and return NEWPLIST.
       the symbol-value-forward. (See below.)
 
    SYMVAL_FIXNUM_FORWARD:
+      (declare with DEFVAR_INT)
+      Similar to SYMVAL_OBJECT_FORWARD except that the C variable
+      is of type "Fixnum", a typedef for "EMACS_INT", and the corresponding
+      lisp variable is always the corresponding integer.
+
    SYMVAL_BOOLEAN_FORWARD:
-      (declare with DEFVAR_INT or DEFVAR_BOOL)
+      (declare with DEFVAR_BOOL)
       Similar to SYMVAL_OBJECT_FORWARD except that the C variable
-      is of type "int" and is an integer or boolean, respectively.
+      is of type "int" and is a boolean.
 
    SYMVAL_CONST_OBJECT_FORWARD:
    SYMVAL_CONST_FIXNUM_FORWARD:
@@ -990,8 +1000,10 @@ static const struct lrecord_description symbol_value_forward_description[] = {
 };
 
 static const struct lrecord_description symbol_value_buffer_local_description[] = {
-  { XD_LISP_OBJECT,  offsetof (struct symbol_value_buffer_local, default_value) },
-  { XD_LO_RESET_NIL, offsetof (struct symbol_value_buffer_local, current_value), 3 },
+  { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, default_value) },
+  { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, current_value) },
+  { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, current_buffer) },
+  { XD_LISP_OBJECT, offsetof (struct symbol_value_buffer_local, current_alist_element) },
   { XD_END }
 };
 
@@ -1066,7 +1078,7 @@ do_symval_forwarding (Lisp_Object valcontents, struct buffer *buffer,
     {
     case SYMVAL_FIXNUM_FORWARD:
     case SYMVAL_CONST_FIXNUM_FORWARD:
-      return make_int (*((int *)symbol_value_forward_forward (fwd)));
+      return make_int (*((Fixnum *)symbol_value_forward_forward (fwd)));
 
     case SYMVAL_BOOLEAN_FORWARD:
     case SYMVAL_CONST_BOOLEAN_FORWARD:
@@ -1137,8 +1149,6 @@ set_default_buffer_slot_variable (Lisp_Object sym,
 
   if (mask > 0)                /* Not always per-buffer */
     {
-      Lisp_Object elt;
-
       /* Set value in each buffer which hasn't shadowed the default */
       LIST_LOOP_2 (elt, Vbuffer_alist)
        {
@@ -1179,8 +1189,6 @@ set_default_console_slot_variable (Lisp_Object sym,
 
   if (mask > 0)                /* Not always per-console */
     {
-      Lisp_Object console;
-
       /* Set value in each console which hasn't shadowed the default */
       LIST_LOOP_2 (console, Vconsole_list)
        {
@@ -1243,7 +1251,7 @@ store_symval_forwarding (Lisp_Object sym, Lisp_Object ovalue,
          CHECK_INT (newval);
          if (magicfun)
            magicfun (sym, &newval, Qnil, 0);
-         *((int *) symbol_value_forward_forward (fwd)) = XINT (newval);
+         *((Fixnum *) symbol_value_forward_forward (fwd)) = XINT (newval);
          return;
 
        case SYMVAL_BOOLEAN_FORWARD:
@@ -1394,6 +1402,71 @@ set_up_buffer_local_cache (Lisp_Object sym,
   store_symval_forwarding (sym, bfwd->current_value, new_val);
 }
 
+
+/* SYM is a buffer-local variable, and BFWD is its buffer-local structure.
+   Flush the cache.  BFWD->CURRENT_BUFFER will be nil after this operation.
+*/
+
+static void
+flush_buffer_local_cache (Lisp_Object sym,
+                         struct symbol_value_buffer_local *bfwd)
+{
+  if (NILP (bfwd->current_buffer))
+    /* Cache is already flushed. */
+    return;
+
+  /* Flush out the old cache. */
+  write_out_buffer_local_cache (sym, bfwd);
+
+  bfwd->current_alist_element = Qnil;
+  bfwd->current_buffer = Qnil;
+
+  /* Now store default the value into the current-value slot.
+     We don't simply write it there, because the current-value
+     slot might be a forwarding pointer, in which case we need
+     to instead write the value into the C variable.
+
+     We might also want to call a magic function.
+
+     So instead, we call this function. */
+  store_symval_forwarding (sym, bfwd->current_value, bfwd->default_value);
+}
+
+/* Flush all the buffer-local variable caches.  Whoever has a
+   non-interned buffer-local variable will be spanked.  Whoever has a
+   magic variable that interns or uninterns symbols... I don't even
+   want to think about it.
+*/
+
+void
+flush_all_buffer_local_cache (void)
+{
+  Lisp_Object *syms = XVECTOR_DATA (Vobarray);
+  long count = XVECTOR_LENGTH (Vobarray);
+  long i;
+
+  for (i=0; i<count; i++)
+    {
+      Lisp_Object sym = syms[i];
+      Lisp_Object value;
+
+      if (!ZEROP (sym))
+       for(;;)
+         {
+           Lisp_Symbol *next;
+           assert (SYMBOLP (sym));
+           value = fetch_value_maybe_past_magic (sym, Qt);
+           if (SYMBOL_VALUE_BUFFER_LOCAL_P (value))
+             flush_buffer_local_cache (sym, XSYMBOL_VALUE_BUFFER_LOCAL (value));
+
+           next = symbol_next (XSYMBOL (sym));
+           if (!next)
+             break;
+           XSETSYMBOL (sym, next);
+         }
+    }
+}
+
 \f
 void
 kill_buffer_local_variables (struct buffer *buf)
@@ -1928,7 +2001,7 @@ local bindings in certain buffers.
 }
 
 DEFUN ("set-default", Fset_default, 2, 2, 0, /*
-Set SYMBOL's default value to VAL.  SYMBOL and VAL are evaluated.
+Set SYMBOL's default value to VALUE.  SYMBOL and VALUE are evaluated.
 The default value is seen in buffers that do not have their own values
 for this variable.
 */
@@ -2095,6 +2168,7 @@ sets it.
       = alloc_lcrecord_type (struct symbol_value_buffer_local,
                             &lrecord_symbol_value_buffer_local);
     Lisp_Object foo;
+    zero_lcrecord (&bfwd->magic);
     bfwd->magic.type = SYMVAL_BUFFER_LOCAL;
 
     bfwd->default_value = find_symbol_value (variable);
@@ -2202,6 +2276,7 @@ Use `make-local-hook' instead.
   /* Make sure variable is set up to hold per-buffer values */
   bfwd = alloc_lcrecord_type (struct symbol_value_buffer_local,
                              &lrecord_symbol_value_buffer_local);
+  zero_lcrecord (&bfwd->magic);
   bfwd->magic.type = SYMVAL_SOME_BUFFER_LOCAL;
 
   bfwd->current_buffer = Qnil;
@@ -2594,7 +2669,7 @@ The returned info will be a symbol, one of
 
 DEFUN ("local-variable-p", Flocal_variable_p, 2, 3, 0, /*
 Return t if SYMBOL's value is local to BUFFER.
-If optional third arg AFTER-SET is true, return t if SYMBOL would be
+If optional third arg AFTER-SET is non-nil, return t if SYMBOL would be
 buffer-local after it is set, regardless of whether it is so presently.
 A nil value for BUFFER is *not* the same as (current-buffer), but means
 "no buffer".  Specifically:
@@ -2922,6 +2997,7 @@ pity, thereby invalidating your code.
     {
       bfwd = alloc_lcrecord_type (struct symbol_value_lisp_magic,
                                  &lrecord_symbol_value_lisp_magic);
+      zero_lcrecord (&bfwd->magic);
       bfwd->magic.type = SYMVAL_LISP_MAGIC;
       for (i = 0; i < MAGIC_HANDLER_MAX; i++)
        {
@@ -3058,6 +3134,7 @@ has a buffer-local value in any buffer, or the symbols nil or t.
 
   bfwd = alloc_lcrecord_type (struct symbol_value_varalias,
                              &lrecord_symbol_value_varalias);
+  zero_lcrecord (&bfwd->magic);
   bfwd->magic.type = SYMVAL_VARALIAS;
   bfwd->aliasee = alias;
   bfwd->shadowed = valcontents;
@@ -3115,6 +3192,31 @@ variable chain of symbols.
   return follow_varalias_pointers (object, follow_past_lisp_magic);
 }
 
+DEFUN ("variable-binding-locus", Fvariable_binding_locus, 1, 1, 0, /*
+Return a value indicating where VARIABLE's current binding comes from.
+If the current binding is buffer-local, the value is the current buffer.
+If the current binding is global (the default), the value is nil. 
+*/
+       (variable))
+{
+  Lisp_Object valcontents;
+
+  CHECK_SYMBOL (variable);
+  variable = Findirect_variable (variable, Qnil);
+
+  /* Make sure the current binding is actually swapped in.  */
+  find_symbol_value (variable);
+
+  valcontents = XSYMBOL (variable)->value;
+
+  if (SYMBOL_VALUE_MAGIC_P (valcontents)
+      && ((XSYMBOL_VALUE_MAGIC_TYPE (valcontents) == SYMVAL_BUFFER_LOCAL)
+         || (XSYMBOL_VALUE_MAGIC_TYPE (valcontents) == SYMVAL_SOME_BUFFER_LOCAL))
+      && (!NILP (Flocal_variable_p (variable, Fcurrent_buffer (), Qnil))))
+    return Fcurrent_buffer ();
+  else
+    return Qnil;
+}
 \f
 /************************************************************************/
 /*                            initialization                            */
@@ -3188,12 +3290,12 @@ init_symbols_once_early (void)
   XSYMBOL (Qnil)->function = Qunbound;
 
   defsymbol (&Qt, "t");
-  XSYMBOL (Qt)->value = Qt;    /* Veritas aetera */
+  XSYMBOL (Qt)->value = Qt;    /* Veritas aeterna */
   Vquit_flag = Qnil;
 
-  pdump_wire (&Qnil);
-  pdump_wire (&Qunbound);
-  pdump_wire (&Vquit_flag);
+  dump_add_root_object (&Qnil);
+  dump_add_root_object (&Qunbound);
+  dump_add_root_object (&Vquit_flag);
 }
 
 void
@@ -3206,10 +3308,63 @@ reinit_symbols_once_early (void)
 #ifndef Qnull_pointer
   /* C guarantees that Qnull_pointer will be initialized to all 0 bits,
      so the following is actually a no-op.  */
-  XSETOBJ (Qnull_pointer, (enum Lisp_Type) 0, 0);
+  XSETOBJ (Qnull_pointer, 0);
 #endif
 }
 
+static void
+defsymbol_massage_name_1 (Lisp_Object *location, const char *name, int dump_p,
+                         int multiword_predicate_p)
+{
+  char temp[500];
+  size_t len = strlen (name) - 1;
+  size_t i;
+
+  if (multiword_predicate_p)
+    assert (len + 1 < sizeof (temp));
+  else
+    assert (len < sizeof (temp));
+  strcpy (temp, name + 1); /* Remove initial Q */
+  if (multiword_predicate_p)
+    {
+      strcpy (temp + len - 1, "_p");
+      len++;
+    }
+  for (i = 0; i < len; i++)
+    if (temp[i] == '_')
+      temp[i] = '-';
+  *location = Fintern (make_string ((const Bufbyte *) temp, len), Qnil);
+  if (dump_p)
+    staticpro (location);
+  else
+    staticpro_nodump (location);
+}
+
+void
+defsymbol_massage_name_nodump (Lisp_Object *location, const char *name)
+{
+  defsymbol_massage_name_1 (location, name, 0, 0);
+}
+
+void
+defsymbol_massage_name (Lisp_Object *location, const char *name)
+{
+  defsymbol_massage_name_1 (location, name, 1, 0);
+}
+
+void
+defsymbol_massage_multiword_predicate_nodump (Lisp_Object *location,
+                                             const char *name)
+{
+  defsymbol_massage_name_1 (location, name, 0, 1);
+}
+
+void
+defsymbol_massage_multiword_predicate (Lisp_Object *location, const char *name)
+{
+  defsymbol_massage_name_1 (location, name, 1, 1);
+}
+
 void
 defsymbol_nodump (Lisp_Object *location, const char *name)
 {
@@ -3235,6 +3390,20 @@ defkeyword (Lisp_Object *location, const char *name)
   Fset (*location, *location);
 }
 
+void
+defkeyword_massage_name (Lisp_Object *location, const char *name)
+{
+  char temp[500];
+  size_t len = strlen (name);
+
+  assert (len < sizeof (temp));
+  strcpy (temp, name);
+  temp[1] = ':'; /* it's an underscore in the C variable */
+
+  defsymbol_massage_name (location, temp);
+  Fset (*location, *location);
+}
+
 #ifdef DEBUG_XEMACS
 /* Check that nobody spazzed writing a DEFUN. */
 static void
@@ -3270,16 +3439,16 @@ check_sane_subr (Lisp_Subr *subr, Lisp_Object sym)
  * Once we have copied everything across, we re-use the original static
  * structure to store a pointer to the newly allocated one. This will be
  * used in emodules.c by emodules_doc_subr() to find a pointer to the
- * allocated object so that we can set its doc string propperly.
+ * allocated object so that we can set its doc string properly.
  *
- * NOTE: We dont actually use the DOC pointer here any more, but we did
+ * NOTE: We don't actually use the DOC pointer here any more, but we did
  * in an earlier implementation of module support. There is no harm in
  * setting it here in case we ever need it in future implementations.
  * subr->doc will point to the new subr structure that was allocated.
- * Code can then get this value from the statis subr structure and use
+ * Code can then get this value from the static subr structure and use
  * it if required.
  *
- * FIXME: Should newsubr be staticpro()'ed? I dont think so but I need
+ * FIXME: Should newsubr be staticpro()'ed? I don't think so but I need
  * a guru to check.
  */
 #define check_module_subr()                                            \
@@ -3322,12 +3491,15 @@ defsubr_macro (Lisp_Subr *subr)
   XSYMBOL (sym)->function = Fcons (Qmacro, fun);
 }
 
-void
-deferror (Lisp_Object *symbol, const char *name, const char *messuhhj,
-         Lisp_Object inherits_from)
+static void
+deferror_1 (Lisp_Object *symbol, const char *name, const char *messuhhj,
+           Lisp_Object inherits_from, int massage_p)
 {
   Lisp_Object conds;
-  defsymbol (symbol, name);
+  if (massage_p)
+    defsymbol_massage_name (symbol, name);
+  else
+    defsymbol (symbol, name);
 
   assert (SYMBOLP (inherits_from));
   conds = Fget (inherits_from, Qerror_conditions, Qnil);
@@ -3339,47 +3511,78 @@ deferror (Lisp_Object *symbol, const char *name, const char *messuhhj,
 }
 
 void
+deferror (Lisp_Object *symbol, const char *name, const char *messuhhj,
+         Lisp_Object inherits_from)
+{
+  deferror_1 (symbol, name, messuhhj, inherits_from, 0);
+}
+
+void
+deferror_massage_name (Lisp_Object *symbol, const char *name,
+                      const char *messuhhj, Lisp_Object inherits_from)
+{
+  deferror_1 (symbol, name, messuhhj, inherits_from, 1);
+}
+
+void
+deferror_massage_name_and_message (Lisp_Object *symbol, const char *name,
+                                  Lisp_Object inherits_from)
+{
+  char temp[500];
+  size_t i;
+  size_t len = strlen (name) - 1;
+
+  assert (len < sizeof (temp));
+  strcpy (temp, name + 1); /* Remove initial Q */
+  temp[0] = toupper (temp[0]);
+  for (i = 0; i < len; i++)
+    if (temp[i] == '_')
+      temp[i] = ' ';
+
+  deferror_1 (symbol, name, temp, inherits_from, 1);
+}
+
+void
 syms_of_symbols (void)
 {
-  defsymbol (&Qvariable_documentation, "variable-documentation");
-  defsymbol (&Qvariable_domain, "variable-domain");    /* I18N3 */
-  defsymbol (&Qad_advice_info, "ad-advice-info");
-  defsymbol (&Qad_activate, "ad-activate");
-
-  defsymbol (&Qget_value, "get-value");
-  defsymbol (&Qset_value, "set-value");
-  defsymbol (&Qbound_predicate, "bound-predicate");
-  defsymbol (&Qmake_unbound, "make-unbound");
-  defsymbol (&Qlocal_predicate, "local-predicate");
-  defsymbol (&Qmake_local, "make-local");
-
-  defsymbol (&Qboundp, "boundp");
-  defsymbol (&Qglobally_boundp, "globally-boundp");
-  defsymbol (&Qmakunbound, "makunbound");
-  defsymbol (&Qsymbol_value, "symbol-value");
-  defsymbol (&Qset, "set");
-  defsymbol (&Qsetq_default, "setq-default");
-  defsymbol (&Qdefault_boundp, "default-boundp");
-  defsymbol (&Qdefault_value, "default-value");
-  defsymbol (&Qset_default, "set-default");
-  defsymbol (&Qmake_variable_buffer_local, "make-variable-buffer-local");
-  defsymbol (&Qmake_local_variable, "make-local-variable");
-  defsymbol (&Qkill_local_variable, "kill-local-variable");
-  defsymbol (&Qkill_console_local_variable, "kill-console-local-variable");
-  defsymbol (&Qsymbol_value_in_buffer, "symbol-value-in-buffer");
-  defsymbol (&Qsymbol_value_in_console, "symbol-value-in-console");
-  defsymbol (&Qlocal_variable_p, "local-variable-p");
-
-  defsymbol (&Qconst_integer, "const-integer");
-  defsymbol (&Qconst_boolean, "const-boolean");
-  defsymbol (&Qconst_object, "const-object");
-  defsymbol (&Qconst_specifier, "const-specifier");
-  defsymbol (&Qdefault_buffer, "default-buffer");
-  defsymbol (&Qcurrent_buffer, "current-buffer");
-  defsymbol (&Qconst_current_buffer, "const-current-buffer");
-  defsymbol (&Qdefault_console, "default-console");
-  defsymbol (&Qselected_console, "selected-console");
-  defsymbol (&Qconst_selected_console, "const-selected-console");
+  DEFSYMBOL (Qvariable_documentation);
+  DEFSYMBOL (Qvariable_domain);        /* I18N3 */
+  DEFSYMBOL (Qad_advice_info);
+  DEFSYMBOL (Qad_activate);
+
+  DEFSYMBOL (Qget_value);
+  DEFSYMBOL (Qset_value);
+  DEFSYMBOL (Qbound_predicate);
+  DEFSYMBOL (Qmake_unbound);
+  DEFSYMBOL (Qlocal_predicate);
+  DEFSYMBOL (Qmake_local);
+
+  DEFSYMBOL (Qboundp);
+  DEFSYMBOL (Qglobally_boundp);
+  DEFSYMBOL (Qmakunbound);
+  DEFSYMBOL (Qsymbol_value);
+  DEFSYMBOL (Qset);
+  DEFSYMBOL (Qsetq_default);
+  DEFSYMBOL (Qdefault_boundp);
+  DEFSYMBOL (Qdefault_value);
+  DEFSYMBOL (Qset_default);
+  DEFSYMBOL (Qmake_variable_buffer_local);
+  DEFSYMBOL (Qmake_local_variable);
+  DEFSYMBOL (Qkill_local_variable);
+  DEFSYMBOL (Qkill_console_local_variable);
+  DEFSYMBOL (Qsymbol_value_in_buffer);
+  DEFSYMBOL (Qsymbol_value_in_console);
+  DEFSYMBOL (Qlocal_variable_p);
+  DEFSYMBOL (Qconst_integer);
+  DEFSYMBOL (Qconst_boolean);
+  DEFSYMBOL (Qconst_object);
+  DEFSYMBOL (Qconst_specifier);
+  DEFSYMBOL (Qdefault_buffer);
+  DEFSYMBOL (Qcurrent_buffer);
+  DEFSYMBOL (Qconst_current_buffer);
+  DEFSYMBOL (Qdefault_console);
+  DEFSYMBOL (Qselected_console);
+  DEFSYMBOL (Qconst_selected_console);
 
   DEFSUBR (Fintern);
   DEFSUBR (Fintern_soft);
@@ -3416,6 +3619,7 @@ syms_of_symbols (void)
   DEFSUBR (Fdefvaralias);
   DEFSUBR (Fvariable_alias);
   DEFSUBR (Findirect_variable);
+  DEFSUBR (Fvariable_binding_locus);
   DEFSUBR (Fdontusethis_set_symbol_value_handler);
 }
 
@@ -3438,7 +3642,7 @@ defvar_magic (const char *symbol_name, const struct symbol_value_forward *magic)
     sym = Fintern (make_string_nocopy ((const Bufbyte *) symbol_name,
                                       strlen (symbol_name)), Qnil);
 
-  XSETOBJ (XSYMBOL (sym)->value, Lisp_Type_Record, magic);
+  XSETOBJ (XSYMBOL (sym)->value, magic);
 }
 
 void