(Fx_list_fonts): If names is 0, just return nil.
[bpt/emacs.git] / src / data.c
index 20a28be..d9c905a 100644 (file)
@@ -1,5 +1,5 @@
 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
-   Copyright (C) 1985, 1986, 1988, 1992 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1988, 1993 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -31,6 +31,9 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "syssignal.h"
 
 #ifdef LISP_FLOAT_TYPE
+#ifdef STDC_HEADERS
+#include <stdlib.h>
+#endif
 #include <math.h>
 #endif /* LISP_FLOAT_TYPE */
 
@@ -49,6 +52,9 @@ Lisp_Object Qbuffer_or_string_p;
 Lisp_Object Qboundp, Qfboundp;
 Lisp_Object Qcdr;
 
+Lisp_Object Qrange_error, Qdomain_error, Qsingularity_error;
+Lisp_Object Qoverflow_error, Qunderflow_error;
+
 #ifdef LISP_FLOAT_TYPE
 Lisp_Object Qfloatp;
 Lisp_Object Qnumberp, Qnumber_or_marker_p;
@@ -69,7 +75,7 @@ wrong_type_argument (predicate, value)
             (EQ (predicate, Qintegerp) || EQ (predicate, Qinteger_or_marker_p)))
           return Fstring_to_number (value);
         if (XTYPE (value) == Lisp_Int && EQ (predicate, Qstringp))
-          return Fint_to_string (value);
+          return Fnumber_to_string (value);
        }
       value = Fsignal (Qwrong_type_argument, Fcons (predicate, Fcons (value, Qnil)));
       tem = call1 (predicate, value);
@@ -515,10 +521,43 @@ DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
      register Lisp_Object sym, newdef;
 {
   CHECK_SYMBOL (sym, 0);
+
+  if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
+    Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
+                            Vautoload_queue);
+  XSYMBOL (sym)->function = newdef;
+  return newdef;
+}
+
+/* This name should be removed once it is eliminated from elsewhere.  */
+
+DEFUN ("defalias", Fdefalias, Sdefalias, 2, 2, 0,
+  "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.\n\
+Associates the function with the current load file, if any.")
+  (sym, newdef)
+     register Lisp_Object sym, newdef;
+{
+  CHECK_SYMBOL (sym, 0);
+  if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
+    Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
+                            Vautoload_queue);
+  XSYMBOL (sym)->function = newdef;
+  LOADHIST_ATTACH (sym);
+  return newdef;
+}
+
+DEFUN ("define-function", Fdefine_function, Sdefine_function, 2, 2, 0,
+  "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.\n\
+Associates the function with the current load file, if any.")
+  (sym, newdef)
+     register Lisp_Object sym, newdef;
+{
+  CHECK_SYMBOL (sym, 0);
   if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
     Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
                             Vautoload_queue);
   XSYMBOL (sym)->function = newdef;
+  LOADHIST_ATTACH (sym);
   return newdef;
 }
 
@@ -1176,9 +1215,9 @@ Lisp_Object
 indirect_function (object)
   register Lisp_Object object;
 {
-  Lisp_Object tortise, hare;
+  Lisp_Object tortoise, hare;
 
-  hare = tortise = object;
+  hare = tortoise = object;
 
   for (;;)
     {
@@ -1189,9 +1228,9 @@ indirect_function (object)
        break;
       hare = XSYMBOL (hare)->function;
 
-      tortise = XSYMBOL (tortise)->function;
+      tortoise = XSYMBOL (tortoise)->function;
 
-      if (EQ (hare, tortise))
+      if (EQ (hare, tortoise))
        Fsignal (Qcyclic_function_indirection, Fcons (object, Qnil));
     }
 
@@ -1422,7 +1461,36 @@ DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0, "T if NUMBER is zero.")
   return Qnil;
 }
 \f
-DEFUN ("int-to-string", Fint_to_string, Sint_to_string, 1, 1, 0,
+/* Convert between 32-bit values and pairs of lispy 24-bit values.  */
+
+Lisp_Object
+long_to_cons (i)
+     unsigned long i;
+{
+  unsigned int top = i >> 16;
+  unsigned int bot = i & 0xFFFF;
+  if (top == 0)
+    return make_number (bot);
+  if (top == 0xFFFF)
+    return Fcons (make_number (-1), make_number (bot));
+  return Fcons (make_number (top), make_number (bot));
+}
+
+unsigned long
+cons_to_long (c)
+     Lisp_Object c;
+{
+  int top, bot;
+  if (INTEGERP (c))
+    return XINT (c);
+  top = XCONS (c)->car;
+  bot = XCONS (c)->cdr;
+  if (CONSP (bot))
+    bot = XCONS (bot)->car;
+  return ((XINT (top) << 16) | XINT (bot));
+}
+\f
+DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
   "Convert NUM to a string by printing it in decimal.\n\
 Uses a minus sign if negative.\n\
 NUM may be an integer or a floating point number.")
@@ -1480,8 +1548,7 @@ enum arithop
 extern Lisp_Object float_arith_driver ();
 
 Lisp_Object
-arith_driver
-  (code, nargs, args)
+arith_driver (code, nargs, args)
      enum arithop code;
      int nargs;
      register Lisp_Object *args;
@@ -1537,7 +1604,12 @@ arith_driver
        case Amult: accum *= next; break;
        case Adiv:
          if (!argnum) accum = next;
-         else accum /= next;
+         else
+           {
+             if (next == 0)
+               Fsignal (Qarith_error, Qnil);
+             accum /= next;
+           }
          break;
        case Alogand: accum &= next; break;
        case Alogior: accum |= next; break;
@@ -1598,7 +1670,11 @@ float_arith_driver (accum, argnum, code, nargs, args)
          if (!argnum)
            accum = next;
          else
-           accum /= next;
+           {
+             if (next == 0)
+               Fsignal (Qarith_error, Qnil);
+             accum /= next;
+           }
          break;
        case Alogand:
        case Alogior:
@@ -1676,12 +1752,16 @@ Both must be numbers or markers.")
 
       f1 = XTYPE (num1) == Lisp_Float ? XFLOAT (num1)->data : XINT (num1);
       f2 = XTYPE (num2) == Lisp_Float ? XFLOAT (num2)->data : XINT (num2);
+      if (f2 == 0)
+       Fsignal (Qarith_error, Qnil);
+
 #if defined (USG) || defined (sun) || defined (ultrix) || defined (hpux)
       f1 = fmod (f1, f2);
 #else
       f1 = drem (f1, f2);
 #endif
-      if (f1 < 0)
+      /* If the "remainder" comes out with the wrong sign, fix it.  */
+      if ((f1 < 0) != (f2 < 0))
        f1 += f2;
       return (make_float (f1));
     }
@@ -1690,6 +1770,9 @@ Both must be numbers or markers.")
   CHECK_NUMBER_COERCE_MARKER (num2, 1);
 #endif /* not LISP_FLOAT_TYPE */
 
+  if (XFASTINT (num2) == 0)
+    Fsignal (Qarith_error, Qnil);
+
   XSET (val, Lisp_Int, XINT (num1) % XINT (num2));
   return val;
 }
@@ -1833,6 +1916,8 @@ DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
 void
 syms_of_data ()
 {
+  Lisp_Object error_tail, arith_tail;
+
   Qquote = intern ("quote");
   Qlambda = intern ("lambda");
   Qsubr = intern ("subr");
@@ -1884,10 +1969,12 @@ syms_of_data ()
 
   Qcdr = intern ("cdr");
 
+  error_tail = Fcons (Qerror, Qnil);
+
   /* ERROR is used as a signaler for random errors for which nothing else is right */
 
   Fput (Qerror, Qerror_conditions,
-       Fcons (Qerror, Qnil));
+       error_tail);
   Fput (Qerror, Qerror_message,
        build_string ("error"));
 
@@ -1897,80 +1984,120 @@ syms_of_data ()
        build_string ("Quit"));
 
   Fput (Qwrong_type_argument, Qerror_conditions,
-       Fcons (Qwrong_type_argument, Fcons (Qerror, Qnil)));
+       Fcons (Qwrong_type_argument, error_tail));
   Fput (Qwrong_type_argument, Qerror_message,
        build_string ("Wrong type argument"));
 
   Fput (Qargs_out_of_range, Qerror_conditions,
-       Fcons (Qargs_out_of_range, Fcons (Qerror, Qnil)));
+       Fcons (Qargs_out_of_range, error_tail));
   Fput (Qargs_out_of_range, Qerror_message,
        build_string ("Args out of range"));
 
   Fput (Qvoid_function, Qerror_conditions,
-       Fcons (Qvoid_function, Fcons (Qerror, Qnil)));
+       Fcons (Qvoid_function, error_tail));
   Fput (Qvoid_function, Qerror_message,
        build_string ("Symbol's function definition is void"));
 
   Fput (Qcyclic_function_indirection, Qerror_conditions,
-       Fcons (Qcyclic_function_indirection, Fcons (Qerror, Qnil)));
+       Fcons (Qcyclic_function_indirection, error_tail));
   Fput (Qcyclic_function_indirection, Qerror_message,
        build_string ("Symbol's chain of function indirections contains a loop"));
 
   Fput (Qvoid_variable, Qerror_conditions,
-       Fcons (Qvoid_variable, Fcons (Qerror, Qnil)));
+       Fcons (Qvoid_variable, error_tail));
   Fput (Qvoid_variable, Qerror_message,
        build_string ("Symbol's value as variable is void"));
 
   Fput (Qsetting_constant, Qerror_conditions,
-       Fcons (Qsetting_constant, Fcons (Qerror, Qnil)));
+       Fcons (Qsetting_constant, error_tail));
   Fput (Qsetting_constant, Qerror_message,
        build_string ("Attempt to set a constant symbol"));
 
   Fput (Qinvalid_read_syntax, Qerror_conditions,
-       Fcons (Qinvalid_read_syntax, Fcons (Qerror, Qnil)));
+       Fcons (Qinvalid_read_syntax, error_tail));
   Fput (Qinvalid_read_syntax, Qerror_message,
        build_string ("Invalid read syntax"));
 
   Fput (Qinvalid_function, Qerror_conditions,
-       Fcons (Qinvalid_function, Fcons (Qerror, Qnil)));
+       Fcons (Qinvalid_function, error_tail));
   Fput (Qinvalid_function, Qerror_message,
        build_string ("Invalid function"));
 
   Fput (Qwrong_number_of_arguments, Qerror_conditions,
-       Fcons (Qwrong_number_of_arguments, Fcons (Qerror, Qnil)));
+       Fcons (Qwrong_number_of_arguments, error_tail));
   Fput (Qwrong_number_of_arguments, Qerror_message,
        build_string ("Wrong number of arguments"));
 
   Fput (Qno_catch, Qerror_conditions,
-       Fcons (Qno_catch, Fcons (Qerror, Qnil)));
+       Fcons (Qno_catch, error_tail));
   Fput (Qno_catch, Qerror_message,
        build_string ("No catch for tag"));
 
   Fput (Qend_of_file, Qerror_conditions,
-       Fcons (Qend_of_file, Fcons (Qerror, Qnil)));
+       Fcons (Qend_of_file, error_tail));
   Fput (Qend_of_file, Qerror_message,
        build_string ("End of file during parsing"));
 
+  arith_tail = Fcons (Qarith_error, error_tail);
   Fput (Qarith_error, Qerror_conditions,
-       Fcons (Qarith_error, Fcons (Qerror, Qnil)));
+       arith_tail);
   Fput (Qarith_error, Qerror_message,
        build_string ("Arithmetic error"));
 
   Fput (Qbeginning_of_buffer, Qerror_conditions,
-       Fcons (Qbeginning_of_buffer, Fcons (Qerror, Qnil)));
+       Fcons (Qbeginning_of_buffer, error_tail));
   Fput (Qbeginning_of_buffer, Qerror_message,
        build_string ("Beginning of buffer"));
 
   Fput (Qend_of_buffer, Qerror_conditions,
-       Fcons (Qend_of_buffer, Fcons (Qerror, Qnil)));
+       Fcons (Qend_of_buffer, error_tail));
   Fput (Qend_of_buffer, Qerror_message,
        build_string ("End of buffer"));
 
   Fput (Qbuffer_read_only, Qerror_conditions,
-       Fcons (Qbuffer_read_only, Fcons (Qerror, Qnil)));
+       Fcons (Qbuffer_read_only, error_tail));
   Fput (Qbuffer_read_only, Qerror_message,
        build_string ("Buffer is read-only"));
 
+#ifdef LISP_FLOAT_TYPE
+  Qrange_error = intern ("range-error");
+  Qdomain_error = intern ("domain-error");
+  Qsingularity_error = intern ("singularity-error");
+  Qoverflow_error = intern ("overflow-error");
+  Qunderflow_error = intern ("underflow-error");
+
+  Fput (Qdomain_error, Qerror_conditions,
+       Fcons (Qdomain_error, arith_tail));
+  Fput (Qdomain_error, Qerror_message,
+       build_string ("Arithmetic domain error"));
+
+  Fput (Qrange_error, Qerror_conditions,
+       Fcons (Qrange_error, arith_tail));
+  Fput (Qrange_error, Qerror_message,
+       build_string ("Arithmetic range error"));
+
+  Fput (Qsingularity_error, Qerror_conditions,
+       Fcons (Qsingularity_error, Fcons (Qdomain_error, arith_tail)));
+  Fput (Qsingularity_error, Qerror_message,
+       build_string ("Arithmetic singularity error"));
+
+  Fput (Qoverflow_error, Qerror_conditions,
+       Fcons (Qoverflow_error, Fcons (Qdomain_error, arith_tail)));
+  Fput (Qoverflow_error, Qerror_message,
+       build_string ("Arithmetic overflow error"));
+
+  Fput (Qunderflow_error, Qerror_conditions,
+       Fcons (Qunderflow_error, Fcons (Qdomain_error, arith_tail)));
+  Fput (Qunderflow_error, Qerror_message,
+       build_string ("Arithmetic underflow error"));
+
+  staticpro (&Qrange_error);
+  staticpro (&Qdomain_error);
+  staticpro (&Qsingularity_error);
+  staticpro (&Qoverflow_error);
+  staticpro (&Qunderflow_error);
+#endif /* LISP_FLOAT_TYPE */
+
   staticpro (&Qnil);
   staticpro (&Qt);
   staticpro (&Qquote);
@@ -2062,6 +2189,8 @@ syms_of_data ()
   defsubr (&Sboundp);
   defsubr (&Sfboundp);
   defsubr (&Sfset);
+  defsubr (&Sdefalias);
+  defsubr (&Sdefine_function);
   defsubr (&Ssetplist);
   defsubr (&Ssymbol_value);
   defsubr (&Sset);
@@ -2074,7 +2203,7 @@ syms_of_data ()
   defsubr (&Skill_local_variable);
   defsubr (&Saref);
   defsubr (&Saset);
-  defsubr (&Sint_to_string);
+  defsubr (&Snumber_to_string);
   defsubr (&Sstring_to_number);
   defsubr (&Seqlsign);
   defsubr (&Slss);