]> code.delx.au - gnu-emacs/blobdiff - src/lread.c
Merge from emacs-24; up to 2014-07-27T09:41:59Z!ttn@gnu.org
[gnu-emacs] / src / lread.c
index 7e4f5d38d099962a885266ddfbc5a1a3326aec21..171a51acb3f40abee55b34ebfeebc4338a555f8f 100644 (file)
@@ -1,6 +1,6 @@
 /* Lisp parsing and input streams.
 
-Copyright (C) 1985-1989, 1993-1995, 1997-2013 Free Software Foundation,
+Copyright (C) 1985-1989, 1993-1995, 1997-2014 Free Software Foundation,
 Inc.
 
 This file is part of GNU Emacs.
@@ -213,7 +213,7 @@ readchar (Lisp_Object readcharfun, bool *multibyte)
       else
        {
          c = BUF_FETCH_BYTE (inbuffer, pt_byte);
-         if (! ASCII_BYTE_P (c))
+         if (! ASCII_CHAR_P (c))
            c = BYTE8_TO_CHAR (c);
          pt_byte++;
        }
@@ -242,7 +242,7 @@ readchar (Lisp_Object readcharfun, bool *multibyte)
       else
        {
          c = BUF_FETCH_BYTE (inbuffer, bytepos);
-         if (! ASCII_BYTE_P (c))
+         if (! ASCII_CHAR_P (c))
            c = BYTE8_TO_CHAR (c);
          bytepos++;
        }
@@ -324,7 +324,7 @@ readchar (Lisp_Object readcharfun, bool *multibyte)
     return c;
   if (multibyte)
     *multibyte = 1;
-  if (ASCII_BYTE_P (c))
+  if (ASCII_CHAR_P (c))
     return c;
   if (emacs_mule_encoding)
     return read_emacs_mule_char (c, readbyte, readcharfun);
@@ -970,10 +970,8 @@ load_warn_old_style_backquotes (Lisp_Object file)
 {
   if (!NILP (Vold_style_backquotes))
     {
-      Lisp_Object args[2];
-      args[0] = build_string ("Loading `%s': old-style backquotes detected!");
-      args[1] = file;
-      Fmessage (2, args);
+      AUTO_STRING (format, "Loading `%s': old-style backquotes detected!");
+      Fmessage (2, (Lisp_Object []) {format, file});
     }
 }
 
@@ -1030,6 +1028,10 @@ in `load-file-rep-suffixes'.  If MUST-SUFFIX is non-nil, only the
 return value of `get-load-suffixes' is used, i.e. the file name is
 required to have a non-empty suffix.
 
+When searching suffixes, this function normally stops at the first
+one that exists.  If the option `load-prefer-newer' is non-nil,
+however, it tries all suffixes, and uses whichever file is the newest.
+
 Loading a file records its definitions, and its `provide' and
 `require' calls, in an element of `load-history' whose
 car is the file name loaded.  See `load-history'.
@@ -1130,7 +1132,7 @@ Return t if the file exists and loads successfully.  */)
            }
        }
 
-      fd = openp (Vload_path, file, suffixes, &found, Qnil);
+      fd = openp (Vload_path, file, suffixes, &found, Qnil, load_prefer_newer);
       UNGCPRO;
     }
 
@@ -1253,29 +1255,36 @@ Return t if the file exists and loads successfully.  */)
 #ifdef DOS_NT
          fmode = "rb";
 #endif /* DOS_NT */
-         result = stat (SSDATA (efound), &s1);
-         if (result == 0)
-           {
-             SSET (efound, SBYTES (efound) - 1, 0);
-             result = stat (SSDATA (efound), &s2);
-             SSET (efound, SBYTES (efound) - 1, 'c');
-           }
 
-         if (result == 0
-             && timespec_cmp (get_stat_mtime (&s1), get_stat_mtime (&s2)) < 0)
-           {
-             /* Make the progress messages mention that source is newer.  */
-             newer = 1;
+          /* openp already checked for newness, no point doing it again.
+             FIXME would be nice to get a message when openp
+             ignores suffix order due to load_prefer_newer.  */
+          if (!load_prefer_newer)
+            {
+              result = stat (SSDATA (efound), &s1);
+              if (result == 0)
+                {
+                  SSET (efound, SBYTES (efound) - 1, 0);
+                  result = stat (SSDATA (efound), &s2);
+                  SSET (efound, SBYTES (efound) - 1, 'c');
+                }
 
-             /* If we won't print another message, mention this anyway.  */
-             if (!NILP (nomessage) && !force_load_messages)
-               {
-                 Lisp_Object msg_file;
-                 msg_file = Fsubstring (found, make_number (0), make_number (-1));
-                 message_with_string ("Source file `%s' newer than byte-compiled file",
-                                      msg_file, 1);
-               }
-           }
+              if (result == 0
+                  && timespec_cmp (get_stat_mtime (&s1), get_stat_mtime (&s2)) < 0)
+                {
+                  /* Make the progress messages mention that source is newer.  */
+                  newer = 1;
+
+                  /* If we won't print another message, mention this anyway.  */
+                  if (!NILP (nomessage) && !force_load_messages)
+                    {
+                      Lisp_Object msg_file;
+                      msg_file = Fsubstring (found, make_number (0), make_number (-1));
+                      message_with_string ("Source file `%s' newer than byte-compiled file",
+                                           msg_file, 1);
+                    }
+                }
+            } /* !load_prefer_newer */
          UNGCPRO;
        }
     }
@@ -1414,7 +1423,7 @@ directories, make sure the PREDICATE function returns `dir-ok' for them.  */)
   (Lisp_Object filename, Lisp_Object path, Lisp_Object suffixes, Lisp_Object predicate)
 {
   Lisp_Object file;
-  int fd = openp (path, filename, suffixes, &file, predicate);
+  int fd = openp (path, filename, suffixes, &file, predicate, false);
   if (NILP (predicate) && fd >= 0)
     emacs_close (fd);
   return file;
@@ -1441,22 +1450,32 @@ static Lisp_Object Qdir_ok;
    nil is stored there on failure.
 
    If the file we find is remote, return -2
-   but store the found remote file name in *STOREPTR.  */
+   but store the found remote file name in *STOREPTR.
+
+   If NEWER is true, try all SUFFIXes and return the result for the
+   newest file that exists.  Does not apply to remote files,
+   or if PREDICATE is specified.  */
 
 int
 openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
-       Lisp_Object *storeptr, Lisp_Object predicate)
+       Lisp_Object *storeptr, Lisp_Object predicate, bool newer)
 {
   ptrdiff_t fn_size = 100;
   char buf[100];
   char *fn = buf;
-  bool absolute = 0;
+  bool absolute;
   ptrdiff_t want_length;
   Lisp_Object filename;
-  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
-  Lisp_Object string, tail, encoded_fn;
+  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6, gcpro7;
+  Lisp_Object string, tail, encoded_fn, save_string;
   ptrdiff_t max_suffix_len = 0;
   int last_errno = ENOENT;
+  int save_fd = -1;
+  USE_SAFE_ALLOCA;
+
+  /* The last-modified time of the newest matching file found.
+     Initialize it to something less than all valid timestamps.  */
+  struct timespec save_mtime = make_timespec (TYPE_MINIMUM (time_t), -1);
 
   CHECK_STRING (str);
 
@@ -1467,14 +1486,13 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
                            SBYTES (XCAR (tail)));
     }
 
-  string = filename = encoded_fn = Qnil;
-  GCPRO6 (str, string, filename, path, suffixes, encoded_fn);
+  string = filename = encoded_fn = save_string = Qnil;
+  GCPRO7 (str, string, save_string, filename, path, suffixes, encoded_fn);
 
   if (storeptr)
     *storeptr = Qnil;
 
-  if (complete_filename_p (str))
-    absolute = 1;
+  absolute = complete_filename_p (str);
 
   for (; CONSP (path); path = XCDR (path))
     {
@@ -1494,7 +1512,10 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
         this path element/specified file name and any possible suffix.  */
       want_length = max_suffix_len + SBYTES (filename);
       if (fn_size <= want_length)
-       fn = alloca (fn_size = 100 + want_length);
+       {
+         fn_size = 100 + want_length;
+         fn = SAFE_ALLOCA (fn_size);
+       }
 
       /* Loop over suffixes.  */
       for (tail = NILP (suffixes) ? list1 (empty_unibyte_string) : suffixes;
@@ -1544,30 +1565,32 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
                {
                  Lisp_Object tmp = call1 (predicate, string);
                  if (NILP (tmp))
-                   exists = 0;
+                   exists = false;
                  else if (EQ (tmp, Qdir_ok)
                           || NILP (Ffile_directory_p (string)))
-                   exists = 1;
+                   exists = true;
                  else
                    {
-                     exists = 0;
+                     exists = false;
                      last_errno = EISDIR;
                    }
                }
 
              if (exists)
                {
-                 /* We succeeded; return this descriptor and filename.  */
-                 if (storeptr)
-                   *storeptr = string;
-                 UNGCPRO;
-                 return -2;
+                  /* We succeeded; return this descriptor and filename.  */
+                  if (storeptr)
+                    *storeptr = string;
+                 SAFE_FREE ();
+                  UNGCPRO;
+                  return -2;
                }
            }
          else
            {
              int fd;
              const char *pfn;
+             struct stat st;
 
              encoded_fn = ENCODE_FILE (string);
              pfn = SSDATA (encoded_fn);
@@ -1598,7 +1621,6 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
                    }
                  else
                    {
-                     struct stat st;
                      int err = (fstat (fd, &st) != 0 ? errno
                                 : S_ISDIR (st.st_mode) ? EISDIR : 0);
                      if (err)
@@ -1612,18 +1634,48 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
 
              if (fd >= 0)
                {
-                 /* We succeeded; return this descriptor and filename.  */
-                 if (storeptr)
-                   *storeptr = string;
-                 UNGCPRO;
-                 return fd;
+                  if (newer && !NATNUMP (predicate))
+                    {
+                      struct timespec mtime = get_stat_mtime (&st);
+
+                     if (timespec_cmp (mtime, save_mtime) <= 0)
+                       emacs_close (fd);
+                     else
+                        {
+                         if (0 <= save_fd)
+                           emacs_close (save_fd);
+                          save_fd = fd;
+                          save_mtime = mtime;
+                          save_string = string;
+                        }
+                    }
+                  else
+                    {
+                      /* We succeeded; return this descriptor and filename.  */
+                      if (storeptr)
+                        *storeptr = string;
+                     SAFE_FREE ();
+                      UNGCPRO;
+                      return fd;
+                    }
                }
+
+              /* No more suffixes.  Return the newest.  */
+             if (0 <= save_fd && ! CONSP (XCDR (tail)))
+                {
+                  if (storeptr)
+                    *storeptr = save_string;
+                 SAFE_FREE ();
+                  UNGCPRO;
+                  return save_fd;
+                }
            }
        }
       if (absolute)
        break;
     }
 
+  SAFE_FREE ();
   UNGCPRO;
   errno = last_errno;
   return -1;
@@ -1717,6 +1769,31 @@ end_of_file_error (void)
   xsignal0 (Qend_of_file);
 }
 
+static Lisp_Object
+readevalloop_eager_expand_eval (Lisp_Object val, Lisp_Object macroexpand)
+{
+  /* If we macroexpand the toplevel form non-recursively and it ends
+     up being a `progn' (or if it was a progn to start), treat each
+     form in the progn as a top-level form.  This way, if one form in
+     the progn defines a macro, that macro is in effect when we expand
+     the remaining forms.  See similar code in bytecomp.el.  */
+  val = call2 (macroexpand, val, Qnil);
+  if (EQ (CAR_SAFE (val), Qprogn))
+    {
+      struct gcpro gcpro1;
+      Lisp_Object subforms = XCDR (val);
+
+      GCPRO1 (subforms);
+      for (val = Qnil; CONSP (subforms); subforms = XCDR (subforms))
+          val = readevalloop_eager_expand_eval (XCAR (subforms),
+                                                macroexpand);
+      UNGCPRO;
+    }
+  else
+      val = eval_sub (call2 (macroexpand, val, Qt));
+  return val;
+}
+
 /* UNIBYTE specifies how to set load_convert_to_unibyte
    for this invocation.
    READFUN, if non-nil, is used instead of `read'.
@@ -1884,8 +1961,9 @@ readevalloop (Lisp_Object readcharfun,
 
       /* Now eval what we just read.  */
       if (!NILP (macroexpand))
-       val = call1 (macroexpand, val);
-      val = eval_sub (val);
+        val = readevalloop_eager_expand_eval (val, macroexpand);
+      else
+        val = eval_sub (val);
 
       if (printflag)
        {
@@ -2007,7 +2085,7 @@ STREAM or the value of `standard-input' may be:
   if (EQ (stream, Qt))
     stream = Qread_char;
   if (EQ (stream, Qread_char))
-    /* FIXME: ¿¡ When is this used !?  */
+    /* FIXME: ?! When is this used !?  */
     return call1 (intern ("read-minibuffer"),
                  build_string ("Lisp expression: "));
 
@@ -2018,9 +2096,10 @@ DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
        doc: /* Read one Lisp expression which is represented as text by STRING.
 Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
 FINAL-STRING-INDEX is an integer giving the position of the next
- remaining character in STRING.
-START and END optionally delimit a substring of STRING from which to read;
- they default to 0 and (length STRING) respectively.  */)
+remaining character in STRING.  START and END optionally delimit
+a substring of STRING from which to read;  they default to 0 and
+(length STRING) respectively.  Negative values are counted from
+the end of STRING.  */)
   (Lisp_Object string, Lisp_Object start, Lisp_Object end)
 {
   Lisp_Object ret;
@@ -2031,10 +2110,9 @@ START and END optionally delimit a substring of STRING from which to read;
 }
 
 /* Function to set up the global context we need in toplevel read
-   calls.  */
+   calls.  START and END only used when STREAM is a string.  */
 static Lisp_Object
 read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
-/* `start', `end' only used when stream is a string.  */
 {
   Lisp_Object retval;
 
@@ -2056,25 +2134,9 @@ read_internal_start (Lisp_Object stream, Lisp_Object start, Lisp_Object end)
       else
        string = XCAR (stream);
 
-      if (NILP (end))
-       endval = SCHARS (string);
-      else
-       {
-         CHECK_NUMBER (end);
-         if (! (0 <= XINT (end) && XINT (end) <= SCHARS (string)))
-           args_out_of_range (string, end);
-         endval = XINT (end);
-       }
+      validate_subarray (string, start, end, SCHARS (string),
+                        &startval, &endval);
 
-      if (NILP (start))
-       startval = 0;
-      else
-       {
-         CHECK_NUMBER (start);
-         if (! (0 <= XINT (start) && XINT (start) <= endval))
-           args_out_of_range (string, start);
-         startval = XINT (start);
-       }
       read_from_string_index = startval;
       read_from_string_index_byte = string_char_to_byte (string, startval);
       read_from_string_limit = endval;
@@ -2549,21 +2611,38 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
              c = READCHAR;
              if (c == '[')
                {
-                 Lisp_Object tmp;
-                 int depth;
-                 ptrdiff_t size;
+                 /* Sub char-table can't be read as a regular
+                    vector because of a two C integer fields.  */
+                 Lisp_Object tbl, tmp = read_list (1, readcharfun);
+                 ptrdiff_t size = XINT (Flength (tmp));
+                 int i, depth, min_char;
+                 struct Lisp_Cons *cell;
 
-                 tmp = read_vector (readcharfun, 0);
-                 size = ASIZE (tmp);
                  if (size == 0)
-                   error ("Invalid size char-table");
-                 if (! RANGED_INTEGERP (1, AREF (tmp, 0), 3))
-                   error ("Invalid depth in char-table");
-                 depth = XINT (AREF (tmp, 0));
+                   error ("Zero-sized sub char-table");
+
+                 if (! RANGED_INTEGERP (1, XCAR (tmp), 3))
+                   error ("Invalid depth in sub char-table");
+                 depth = XINT (XCAR (tmp));
                  if (chartab_size[depth] != size - 2)
-                   error ("Invalid size char-table");
-                 XSETPVECTYPE (XVECTOR (tmp), PVEC_SUB_CHAR_TABLE);
-                 return tmp;
+                   error ("Invalid size in sub char-table");
+                 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
+                 free_cons (cell);
+
+                 if (! RANGED_INTEGERP (0, XCAR (tmp), MAX_CHAR))
+                   error ("Invalid minimum character in sub-char-table");
+                 min_char = XINT (XCAR (tmp));
+                 cell = XCONS (tmp), tmp = XCDR (tmp), size--;
+                 free_cons (cell);
+
+                 tbl = make_uninit_sub_char_table (depth, min_char);
+                 for (i = 0; i < size; i++)
+                   {
+                     XSUB_CHAR_TABLE (tbl)->contents[i] = XCAR (tmp);
+                     cell = XCONS (tmp), tmp = XCDR (tmp);
+                     free_cons (cell);
+                   }
+                 return tbl;
                }
              invalid_syntax ("#^^");
            }
@@ -2577,9 +2656,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
          if (c == '"')
            {
              Lisp_Object tmp, val;
-             EMACS_INT size_in_chars
-               = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
-                  / BOOL_VECTOR_BITS_PER_CHAR);
+             EMACS_INT size_in_chars = bool_vector_bytes (XFASTINT (length));
              unsigned char *data;
 
              UNREAD (c);
@@ -2594,7 +2671,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
                            == (SCHARS (tmp) - 1) * BOOL_VECTOR_BITS_PER_CHAR)))
                invalid_syntax ("#&...");
 
-             val = Fmake_bool_vector (length, Qnil);
+             val = make_uninit_bool_vector (XFASTINT (length));
              data = bool_vector_uchar_data (val);
              memcpy (data, SDATA (tmp), size_in_chars);
              /* Clear the extraneous bits in the last byte.  */
@@ -2610,9 +2687,10 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
          /* Accept compiled functions at read-time so that we don't have to
             build them using function calls.  */
          Lisp_Object tmp;
+         struct Lisp_Vector *vec;
          tmp = read_vector (readcharfun, 1);
-         struct Lisp_Vector* vec = XVECTOR (tmp);
-         if (vec->header.size==0)
+         vec = XVECTOR (tmp);
+         if (vec->header.size == 0)
            invalid_syntax ("Empty byte-code object");
          make_byte_code (vec);
          return tmp;
@@ -2795,11 +2873,8 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
                  if (c == '=')
                    {
                      /* Make a placeholder for #n# to use temporarily.  */
-                     Lisp_Object placeholder;
-                     Lisp_Object cell;
-
-                     placeholder = Fcons (Qnil, Qnil);
-                     cell = Fcons (make_number (n), placeholder);
+                     AUTO_CONS (placeholder, Qnil, Qnil);
+                     Lisp_Object cell = Fcons (make_number (n), placeholder);
                      read_objects = Fcons (cell, read_objects);
 
                      /* Read the object itself.  */
@@ -3278,7 +3353,7 @@ substitute_object_recurse (Lisp_Object object, Lisp_Object placeholder, Lisp_Obj
           substitute_in_interval contains part of the logic.  */
 
        INTERVAL root_interval = string_intervals (subtree);
-       Lisp_Object arg = Fcons (object, placeholder);
+       AUTO_CONS (arg, object, placeholder);
 
        traverse_intervals_noorder (root_interval,
                                    &substitute_in_interval, arg);
@@ -3540,7 +3615,7 @@ read_vector (Lisp_Object readcharfun, bool bytecodeflag)
   return vector;
 }
 
-/* FLAG means check for ] to terminate rather than ) and .  */
+/* FLAG means check for ']' to terminate rather than ')' and '.'.  */
 
 static Lisp_Object
 read_list (bool flag, Lisp_Object readcharfun)
@@ -3585,8 +3660,10 @@ read_list (bool flag, Lisp_Object readcharfun)
               in the installed Lisp directory.
               We don't use Fexpand_file_name because that would make
               the directory absolute now.  */
-           elt = concat2 (build_string ("../lisp/"),
-                        Ffile_name_nondirectory (elt));
+           {
+             AUTO_STRING (dot_dot_lisp, "../lisp/");
+             elt = concat2 (dot_dot_lisp, Ffile_name_nondirectory (elt));
+           }
        }
       else if (EQ (elt, Vload_file_name)
               && ! NILP (elt)
@@ -3714,6 +3791,30 @@ check_obarray (Lisp_Object obarray)
   return obarray;
 }
 
+/* Intern a symbol with name STRING in OBARRAY using bucket INDEX.  */
+
+Lisp_Object
+intern_driver (Lisp_Object string, Lisp_Object obarray, ptrdiff_t index)
+{
+  Lisp_Object *ptr, sym = Fmake_symbol (string);
+
+  XSYMBOL (sym)->interned = (EQ (obarray, initial_obarray)
+                            ? SYMBOL_INTERNED_IN_INITIAL_OBARRAY
+                            : SYMBOL_INTERNED);
+
+  if ((SREF (string, 0) == ':') && EQ (obarray, initial_obarray))
+    {
+      XSYMBOL (sym)->constant = 1;
+      XSYMBOL (sym)->redirect = SYMBOL_PLAINVAL;
+      SET_SYMBOL_VAL (XSYMBOL (sym), sym);
+    }
+
+  ptr = aref_addr (obarray, index);
+  set_symbol_next (sym, SYMBOLP (*ptr) ? XSYMBOL (*ptr) : NULL);
+  *ptr = sym;
+  return sym;
+}
+
 /* Intern the C string STR: return a symbol with that name,
    interned in the current obarray.  */
 
@@ -3723,7 +3824,8 @@ intern_1 (const char *str, ptrdiff_t len)
   Lisp_Object obarray = check_obarray (Vobarray);
   Lisp_Object tem = oblookup (obarray, str, len, len);
 
-  return SYMBOLP (tem) ? tem : Fintern (make_string (str, len), obarray);
+  return SYMBOLP (tem) ? tem : intern_driver (make_string (str, len),
+                                             obarray, XINT (tem));
 }
 
 Lisp_Object
@@ -3732,16 +3834,14 @@ intern_c_string_1 (const char *str, ptrdiff_t len)
   Lisp_Object obarray = check_obarray (Vobarray);
   Lisp_Object tem = oblookup (obarray, str, len, len);
 
-  if (SYMBOLP (tem))
-    return tem;
-
-  if (NILP (Vpurify_flag))
-    /* Creating a non-pure string from a string literal not
-       implemented yet.  We could just use make_string here and live
-       with the extra copy.  */
-    emacs_abort ();
-
-  return Fintern (make_pure_c_string (str, len), obarray);
+  if (!SYMBOLP (tem))
+    {
+      /* Creating a non-pure string from a string literal not implemented yet.
+        We could just use make_string here and live with the extra copy.  */
+      eassert (!NILP (Vpurify_flag));
+      tem = intern_driver (make_pure_c_string (str, len), obarray, XINT (tem));
+    }
+  return tem;
 }
 \f
 DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
@@ -3751,43 +3851,16 @@ A second optional argument specifies the obarray to use;
 it defaults to the value of `obarray'.  */)
   (Lisp_Object string, Lisp_Object obarray)
 {
-  register Lisp_Object tem, sym, *ptr;
-
-  if (NILP (obarray)) obarray = Vobarray;
-  obarray = check_obarray (obarray);
+  Lisp_Object tem;
 
+  obarray = check_obarray (NILP (obarray) ? Vobarray : obarray);
   CHECK_STRING (string);
 
-  tem = oblookup (obarray, SSDATA (string),
-                 SCHARS (string),
-                 SBYTES (string));
-  if (!INTEGERP (tem))
-    return tem;
-
-  if (!NILP (Vpurify_flag))
-    string = Fpurecopy (string);
-  sym = Fmake_symbol (string);
-
-  if (EQ (obarray, initial_obarray))
-    XSYMBOL (sym)->interned = SYMBOL_INTERNED_IN_INITIAL_OBARRAY;
-  else
-    XSYMBOL (sym)->interned = SYMBOL_INTERNED;
-
-  if ((SREF (string, 0) == ':')
-      && EQ (obarray, initial_obarray))
-    {
-      XSYMBOL (sym)->constant = 1;
-      XSYMBOL (sym)->redirect = SYMBOL_PLAINVAL;
-      SET_SYMBOL_VAL (XSYMBOL (sym), sym);
-    }
-
-  ptr = aref_addr (obarray, XINT(tem));
-  if (SYMBOLP (*ptr))
-    set_symbol_next (sym, XSYMBOL (*ptr));
-  else
-    set_symbol_next (sym, NULL);
-  *ptr = sym;
-  return sym;
+  tem = oblookup (obarray, SSDATA (string), SCHARS (string), SBYTES (string));
+  if (!SYMBOLP (tem))
+    tem = intern_driver (NILP (Vpurify_flag) ? string
+                        : Fpurecopy (string), obarray, XINT (tem));
+  return tem;
 }
 
 DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
@@ -3823,7 +3896,8 @@ DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0,
 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, if nil, defaults to the value of the variable `obarray'.
+usage: (unintern NAME OBARRAY)  */)
   (Lisp_Object name, Lisp_Object obarray)
 {
   register Lisp_Object string, tem;
@@ -3893,7 +3967,8 @@ OBARRAY defaults to the value of the variable `obarray'.  */)
 \f
 /* Return the symbol in OBARRAY whose names matches the string
    of SIZE characters (SIZE_BYTE bytes) at PTR.
-   If there is no such symbol in OBARRAY, return nil.
+   If there is no such symbol, return the integer bucket number of
+   where the symbol would be if it were present.
 
    Also store the bucket number in oblookup_last_bucket_number.  */
 
@@ -4108,68 +4183,63 @@ defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd,
   SET_SYMBOL_FWD (XSYMBOL (sym), (union Lisp_Fwd *)ko_fwd);
 }
 \f
-/* Check that the elements of Vload_path exist.  */
+/* Check that the elements of lpath exist.  */
 
 static void
-load_path_check (void)
+load_path_check (Lisp_Object lpath)
 {
   Lisp_Object path_tail;
 
   /* The only elements that might not exist are those from
      PATH_LOADSEARCH, EMACSLOADPATH.  Anything else is only added if
      it exists.  */
-  for (path_tail = Vload_path; !NILP (path_tail); path_tail = XCDR (path_tail))
+  for (path_tail = lpath; !NILP (path_tail); path_tail = XCDR (path_tail))
     {
       Lisp_Object dirfile;
       dirfile = Fcar (path_tail);
       if (STRINGP (dirfile))
         {
           dirfile = Fdirectory_file_name (dirfile);
-          if (! file_accessible_directory_p (SSDATA (dirfile)))
+          if (! file_accessible_directory_p (dirfile))
             dir_warning ("Lisp directory", XCAR (path_tail));
         }
     }
 }
 
-/* Record the value of load-path used at the start of dumping
-   so we can see if the site changed it later during dumping.  */
-static Lisp_Object dump_path;
+/* Return the default load-path, to be used if EMACSLOADPATH is unset.
+   This does not include the standard site-lisp directories
+   under the installation prefix (i.e., PATH_SITELOADSEARCH),
+   but it does (unless no_site_lisp is set) include site-lisp
+   directories in the source/build directories if those exist and we
+   are running uninstalled.
 
-/* Compute the default Vload_path, with the following logic:
-   If CANNOT_DUMP:
-   use EMACSLOADPATH env-var if set; otherwise use PATH_LOADSEARCH,
-   prepending PATH_SITELOADSEARCH unless --no-site-lisp.
+   Uses the following logic:
+   If CANNOT_DUMP: Use PATH_LOADSEARCH.
    The remainder is what happens when dumping works:
    If purify-flag (ie dumping) just use PATH_DUMPLOADSEARCH.
-   Otherwise use EMACSLOADPATH if set, else PATH_LOADSEARCH.
+   Otherwise use PATH_LOADSEARCH.
 
-   If !initialized, then just set both Vload_path and dump_path.
-   If initialized, then if Vload_path != dump_path, do nothing.
-   (Presumably the load-path has already been changed by something.
-   This can only be from a site-load file during dumping,
-   or because EMACSLOADPATH is set.)
+   If !initialized, then just return PATH_DUMPLOADSEARCH.
+   If initialized:
    If Vinstallation_directory is not nil (ie, running uninstalled):
    If installation-dir/lisp exists and not already a member,
    we must be running uninstalled.  Reset the load-path
    to just installation-dir/lisp.  (The default PATH_LOADSEARCH
    refers to the eventual installation directories.  Since we
    are not yet installed, we should not use them, even if they exist.)
-   If installation-dir/lisp does not exist, just add dump_path at the
-   end instead.
-   Add installation-dir/leim (if exists and not already a member) at the front.
+   If installation-dir/lisp does not exist, just add
+   PATH_DUMPLOADSEARCH at the end instead.
    Add installation-dir/site-lisp (if !no_site_lisp, and exists
    and not already a member) at the front.
    If installation-dir != source-dir (ie running an uninstalled,
    out-of-tree build) AND install-dir/src/Makefile exists BUT
    install-dir/src/Makefile.in does NOT exist (this is a sanity
-   check), then repeat the above steps for source-dir/lisp,
-   leim and site-lisp.
-   Finally, add the site-lisp directories at the front (if !no_site_lisp).
-*/
+   check), then repeat the above steps for source-dir/lisp, site-lisp.  */
 
-void
-init_lread (void)
+static Lisp_Object
+load_path_default (void)
 {
+  Lisp_Object lpath = Qnil;
   const char *normal;
 
 #ifdef CANNOT_DUMP
@@ -4179,190 +4249,193 @@ init_lread (void)
 
   normal = PATH_LOADSEARCH;
 #ifdef HAVE_NS
-  Vload_path = decode_env_path ("EMACSLOADPATH", loadpath ? loadpath : normal);
+  lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
 #else
-  Vload_path = decode_env_path ("EMACSLOADPATH", normal);
+  lpath = decode_env_path (0, normal, 0);
 #endif
 
-  load_path_check ();
-
-  /* FIXME CANNOT_DUMP platforms should get source-dir/lisp etc added
-   to their load-path too, AFAICS.  I don't think we can tell the
-   difference between initialized and !initialized in this case,
-   so we'll have to do it unconditionally when Vinstallation_directory
-   is non-nil.  */
-  if (!no_site_lisp && !egetenv ("EMACSLOADPATH"))
-    {
-      Lisp_Object sitelisp;
-      sitelisp = decode_env_path (0, PATH_SITELOADSEARCH);
-      if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
-    }
 #else  /* !CANNOT_DUMP */
-  if (NILP (Vpurify_flag))
-    {
-      normal = PATH_LOADSEARCH;
-      /* If the EMACSLOADPATH environment variable is set, use its value.
-         This doesn't apply if we're dumping.  */
-      if (egetenv ("EMACSLOADPATH"))
-        Vload_path = decode_env_path ("EMACSLOADPATH", normal);
-    }
-  else
-    normal = PATH_DUMPLOADSEARCH;
-
-  /* In a dumped Emacs, we normally reset the value of Vload_path using
-     PATH_LOADSEARCH, since the value that was dumped uses lisp/ in
-     the source directory, instead of the path of the installed elisp
-     libraries.  However, if it appears that Vload_path has already been
-     changed from the default that was saved before dumping, don't
-     change it further.  Changes can only be due to EMACSLOADPATH, or
-     site-lisp files that were processed during dumping.  */
+
+  normal = NILP (Vpurify_flag) ? PATH_LOADSEARCH : PATH_DUMPLOADSEARCH;
+
   if (initialized)
     {
-      if (NILP (Fequal (dump_path, Vload_path)))
-        {
-          /* Do not make any changes, just check the elements exist.  */
-          /* Note: --no-site-lisp is ignored.
-             I don't know what to do about this.  */
-          load_path_check ();
-        }
-      else
-       {
 #ifdef HAVE_NS
-         const char *loadpath = ns_load_path ();
-         Vload_path = decode_env_path (0, loadpath ? loadpath : normal);
+      const char *loadpath = ns_load_path ();
+      lpath = decode_env_path (0, loadpath ? loadpath : normal, 0);
 #else
-         Vload_path = decode_env_path (0, normal);
+      lpath = decode_env_path (0, normal, 0);
 #endif
-         if (!NILP (Vinstallation_directory))
-           {
-             Lisp_Object tem, tem1;
-
-              /* Add to the path the lisp subdir of the installation
-                 dir, if it is accessible.  Note: in out-of-tree builds,
-                 this directory is empty save for Makefile.  */
-              tem = Fexpand_file_name (build_string ("lisp"),
-                                       Vinstallation_directory);
-              tem1 = Ffile_accessible_directory_p (tem);
-              if (!NILP (tem1))
+      if (!NILP (Vinstallation_directory))
+        {
+          Lisp_Object tem, tem1;
+
+          /* Add to the path the lisp subdir of the installation
+             dir, if it is accessible.  Note: in out-of-tree builds,
+             this directory is empty save for Makefile.  */
+          tem = Fexpand_file_name (build_string ("lisp"),
+                                   Vinstallation_directory);
+          tem1 = Ffile_accessible_directory_p (tem);
+          if (!NILP (tem1))
+            {
+              if (NILP (Fmember (tem, lpath)))
                 {
-                  if (NILP (Fmember (tem, Vload_path)))
-                    {
-                      /* We are running uninstalled.  The default load-path
-                         points to the eventual installed lisp, leim
-                         directories.  We should not use those now, even
-                         if they exist, so start over from a clean slate.  */
-                      Vload_path = list1 (tem);
-                    }
+                  /* We are running uninstalled.  The default load-path
+                     points to the eventual installed lisp directories.
+                     We should not use those now, even if they exist,
+                     so start over from a clean slate.  */
+                  lpath = list1 (tem);
                 }
-              else
-                /* That dir doesn't exist, so add the build-time
-                   Lisp dirs instead.  */
-                Vload_path = nconc2 (Vload_path, dump_path);
+            }
+          else
+            /* That dir doesn't exist, so add the build-time
+               Lisp dirs instead.  */
+            {
+              Lisp_Object dump_path =
+                decode_env_path (0, PATH_DUMPLOADSEARCH, 0);
+              lpath = nconc2 (lpath, dump_path);
+            }
 
-              /* Add leim under the installation dir, if it is accessible. */
-              tem = Fexpand_file_name (build_string ("leim"),
+          /* Add site-lisp under the installation dir, if it exists.  */
+          if (!no_site_lisp)
+            {
+              tem = Fexpand_file_name (build_string ("site-lisp"),
                                        Vinstallation_directory);
               tem1 = Ffile_accessible_directory_p (tem);
               if (!NILP (tem1))
                 {
-                  if (NILP (Fmember (tem, Vload_path)))
-                    Vload_path = Fcons (tem, Vload_path);
+                  if (NILP (Fmember (tem, lpath)))
+                    lpath = Fcons (tem, lpath);
                 }
+            }
+
+          /* If Emacs was not built in the source directory,
+             and it is run from where it was built, add to load-path
+             the lisp and site-lisp dirs under that directory.  */
+
+          if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
+            {
+              Lisp_Object tem2;
+
+              tem = Fexpand_file_name (build_string ("src/Makefile"),
+                                       Vinstallation_directory);
+              tem1 = Ffile_exists_p (tem);
 
-              /* Add site-lisp under the installation dir, if it exists.  */
-              if (!no_site_lisp)
+              /* Don't be fooled if they moved the entire source tree
+                 AFTER dumping Emacs.  If the build directory is indeed
+                 different from the source dir, src/Makefile.in and
+                 src/Makefile will not be found together.  */
+              tem = Fexpand_file_name (build_string ("src/Makefile.in"),
+                                       Vinstallation_directory);
+              tem2 = Ffile_exists_p (tem);
+              if (!NILP (tem1) && NILP (tem2))
                 {
-                  tem = Fexpand_file_name (build_string ("site-lisp"),
-                                           Vinstallation_directory);
-                  tem1 = Ffile_accessible_directory_p (tem);
-                  if (!NILP (tem1))
+                  tem = Fexpand_file_name (build_string ("lisp"),
+                                           Vsource_directory);
+
+                  if (NILP (Fmember (tem, lpath)))
+                    lpath = Fcons (tem, lpath);
+
+                  if (!no_site_lisp)
                     {
-                      if (NILP (Fmember (tem, Vload_path)))
-                        Vload_path = Fcons (tem, Vload_path);
+                      tem = Fexpand_file_name (build_string ("site-lisp"),
+                                               Vsource_directory);
+                      tem1 = Ffile_accessible_directory_p (tem);
+                      if (!NILP (tem1))
+                        {
+                          if (NILP (Fmember (tem, lpath)))
+                            lpath = Fcons (tem, lpath);
+                        }
                     }
                 }
+            } /* Vinstallation_directory != Vsource_directory */
 
-              /* If Emacs was not built in the source directory,
-                 and it is run from where it was built, add to load-path
-                 the lisp, leim and site-lisp dirs under that directory.  */
+        } /* if Vinstallation_directory */
+    }
+  else                          /* !initialized */
+    {
+      /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
+         source directory.  We used to add ../lisp (ie the lisp dir in
+         the build directory) at the front here, but that should not
+         be necessary, since in out of tree builds lisp/ is empty, save
+         for Makefile.  */
+      lpath = decode_env_path (0, normal, 0);
+    }
+#endif /* !CANNOT_DUMP */
 
-              if (NILP (Fequal (Vinstallation_directory, Vsource_directory)))
-                {
-                  Lisp_Object tem2;
-
-                  tem = Fexpand_file_name (build_string ("src/Makefile"),
-                                           Vinstallation_directory);
-                  tem1 = Ffile_exists_p (tem);
-
-                  /* Don't be fooled if they moved the entire source tree
-                     AFTER dumping Emacs.  If the build directory is indeed
-                     different from the source dir, src/Makefile.in and
-                     src/Makefile will not be found together.  */
-                  tem = Fexpand_file_name (build_string ("src/Makefile.in"),
-                                           Vinstallation_directory);
-                  tem2 = Ffile_exists_p (tem);
-                  if (!NILP (tem1) && NILP (tem2))
-                    {
-                      tem = Fexpand_file_name (build_string ("lisp"),
-                                               Vsource_directory);
+  return lpath;
+}
 
-                      if (NILP (Fmember (tem, Vload_path)))
-                        Vload_path = Fcons (tem, Vload_path);
+void
+init_lread (void)
+{
+  /* First, set Vload_path.  */
 
-                      tem = Fexpand_file_name (build_string ("leim"),
-                                               Vsource_directory);
+  /* Ignore EMACSLOADPATH when dumping.  */
+#ifdef CANNOT_DUMP
+  bool use_loadpath = true;
+#else
+  bool use_loadpath = NILP (Vpurify_flag);
+#endif
 
-                      if (NILP (Fmember (tem, Vload_path)))
-                        Vload_path = Fcons (tem, Vload_path);
+  if (use_loadpath && egetenv ("EMACSLOADPATH"))
+    {
+      Vload_path = decode_env_path ("EMACSLOADPATH", 0, 1);
 
-                      if (!no_site_lisp)
-                        {
-                          tem = Fexpand_file_name (build_string ("site-lisp"),
-                                                   Vsource_directory);
-                          tem1 = Ffile_accessible_directory_p (tem);
-                          if (!NILP (tem1))
-                            {
-                              if (NILP (Fmember (tem, Vload_path)))
-                                Vload_path = Fcons (tem, Vload_path);
-                            }
-                        }
-                    }
-                } /* Vinstallation_directory != Vsource_directory */
+      /* Check (non-nil) user-supplied elements.  */
+      load_path_check (Vload_path);
 
-           } /* if Vinstallation_directory */
+      /* If no nils in the environment variable, use as-is.
+         Otherwise, replace any nils with the default.  */
+      if (! NILP (Fmemq (Qnil, Vload_path)))
+        {
+          Lisp_Object elem, elpath = Vload_path;
+          Lisp_Object default_lpath = load_path_default ();
 
-          /* Check before adding the site-lisp directories.
-             The install should have created them, but they are not
-             required, so no need to warn if they are absent.
-             Or we might be running before installation.  */
-          load_path_check ();
+          /* Check defaults, before adding site-lisp.  */
+          load_path_check (default_lpath);
 
-          /* Add the site-lisp directories at the front.  */
+          /* Add the site-lisp directories to the front of the default.  */
           if (!no_site_lisp)
             {
               Lisp_Object sitelisp;
-              sitelisp = decode_env_path (0, PATH_SITELOADSEARCH);
-              if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
+              sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
+              if (! NILP (sitelisp))
+                default_lpath = nconc2 (sitelisp, default_lpath);
+            }
+
+          Vload_path = Qnil;
+
+          /* Replace nils from EMACSLOADPATH by default.  */
+          while (CONSP (elpath))
+            {
+              Lisp_Object arg[2];
+              elem = XCAR (elpath);
+              elpath = XCDR (elpath);
+              arg[0] = Vload_path;
+              arg[1] = NILP (elem) ? default_lpath : Fcons (elem, Qnil);
+              Vload_path = Fappend (2, arg);
             }
-       } /* if dump_path == Vload_path */
+        }                       /* Fmemq (Qnil, Vload_path) */
     }
-  else                          /* !initialized */
+  else
     {
-      /* NORMAL refers to PATH_DUMPLOADSEARCH, ie the lisp dir in the
-         source directory.  We used to add ../lisp (ie the lisp dir in
-         the build directory) at the front here, but that caused trouble
-        because it was copied from dump_path into Vload_path, above,
-        when Vinstallation_directory was non-nil.  It should not be
-         necessary, since in out of tree builds lisp/ is empty, save
-         for Makefile.  */
-      Vload_path = decode_env_path (0, normal);
-      dump_path = Vload_path;
-      /* No point calling load_path_check; load-path only contains essential
-         elements from the source directory at this point.  They cannot
-         be missing unless something went extremely (and improbably)
-         wrong, in which case the build will fail in obvious ways.  */
+      Vload_path = load_path_default ();
+
+      /* Check before adding site-lisp directories.
+         The install should have created them, but they are not
+         required, so no need to warn if they are absent.
+         Or we might be running before installation.  */
+      load_path_check (Vload_path);
+
+      /* Add the site-lisp directories at the front.  */
+      if (initialized && !no_site_lisp)
+        {
+          Lisp_Object sitelisp;
+          sitelisp = decode_env_path (0, PATH_SITELOADSEARCH, 0);
+          if (! NILP (sitelisp)) Vload_path = nconc2 (sitelisp, Vload_path);
+        }
     }
-#endif  /* !CANNOT_DUMP */
 
   Vvalues = Qnil;
 
@@ -4469,9 +4542,8 @@ were read in.  */);
 
   DEFVAR_LISP ("load-path", Vload_path,
               doc: /* List of directories to search for files to load.
-Each element is a string (directory name) or nil (try default directory).
-Initialized based on EMACSLOADPATH environment variable, if any,
-otherwise to default specified by file `epaths.h' when Emacs was built.  */);
+Each element is a string (directory name) or nil (meaning `default-directory').
+Initialized during startup as described in Info node `(elisp)Library Search'.  */);
 
   DEFVAR_LISP ("load-suffixes", Vload_suffixes,
               doc: /* List of suffixes for (compiled or source) Emacs Lisp files.
@@ -4587,7 +4659,7 @@ and is not meant for users to change.  */);
 You cannot count on them to still be there!  */);
   Vsource_directory
     = Fexpand_file_name (build_string ("../"),
-                        Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH)));
+                        Fcar (decode_env_path (0, PATH_DUMPLOADSEARCH, 0)));
 
   DEFVAR_LISP ("preloaded-file-list", Vpreloaded_file_list,
               doc: /* List of files that were preloaded (when dumping Emacs).  */);
@@ -4638,6 +4710,18 @@ variables, this must be set in the first line of a file.  */);
   Vold_style_backquotes = Qnil;
   DEFSYM (Qold_style_backquotes, "old-style-backquotes");
 
+  DEFVAR_BOOL ("load-prefer-newer", load_prefer_newer,
+               doc: /* Non-nil means `load' prefers the newest version of a file.
+This applies when a filename suffix is not explicitly specified and
+`load' is trying various possible suffixes (see `load-suffixes' and
+`load-file-rep-suffixes').  Normally, it stops at the first file
+that exists unless you explicitly specify one or the other.  If this
+option is non-nil, it checks all suffixes and uses whichever file is
+newest.
+Note that if you customize this, obviously it will not affect files
+that are loaded before your customizations are read!  */);
+  load_prefer_newer = 0;
+
   /* Vsource_directory was initialized in init_lread.  */
 
   DEFSYM (Qcurrent_load_list, "current-load-list");
@@ -4662,8 +4746,6 @@ variables, this must be set in the first line of a file.  */);
   DEFSYM (Qdir_ok, "dir-ok");
   DEFSYM (Qdo_after_load_evaluation, "do-after-load-evaluation");
 
-  staticpro (&dump_path);
-
   staticpro (&read_objects);
   read_objects = Qnil;
   staticpro (&seen_list);