Generalize INTERNAL_FIELD between buffers, keyboards and frames.
[bpt/emacs.git] / src / minibuf.c
index 9a4df33..5ece9d5 100644 (file)
@@ -1,6 +1,6 @@
 /* Minibuffer input and completion.
 
-Copyright (C) 1985-1986, 1993-2011  Free Software Foundation, Inc.
+Copyright (C) 1985-1986, 1993-2012  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -25,6 +25,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "lisp.h"
 #include "commands.h"
+#include "character.h"
 #include "buffer.h"
 #include "dispextern.h"
 #include "keyboard.h"
@@ -72,7 +73,7 @@ Lisp_Object Qcompletion_ignore_case;
 static Lisp_Object Qminibuffer_completion_table;
 static Lisp_Object Qminibuffer_completion_predicate;
 static Lisp_Object Qminibuffer_completion_confirm;
-static Lisp_Object Quser_variable_p;
+static Lisp_Object Qcustom_variable_p;
 
 static Lisp_Object Qminibuffer_default;
 
@@ -101,14 +102,14 @@ choose_minibuf_frame (void)
 {
   if (FRAMEP (selected_frame)
       && FRAME_LIVE_P (XFRAME (selected_frame))
-      && !EQ (minibuf_window, XFRAME (selected_frame)->minibuffer_window))
+      && !EQ (minibuf_window, FVAR (XFRAME (selected_frame), minibuffer_window)))
     {
       struct frame *sf = XFRAME (selected_frame);
       Lisp_Object buffer;
 
       /* I don't think that any frames may validly have a null minibuffer
         window anymore.  */
-      if (NILP (sf->minibuffer_window))
+      if (NILP (FVAR (sf, minibuffer_window)))
        abort ();
 
       /* Under X, we come here with minibuf_window being the
@@ -116,8 +117,8 @@ choose_minibuf_frame (void)
         init_window_once.  That window doesn't have a buffer.  */
       buffer = XWINDOW (minibuf_window)->buffer;
       if (BUFFERP (buffer))
-       Fset_window_buffer (sf->minibuffer_window, buffer, Qnil);
-      minibuf_window = sf->minibuffer_window;
+       Fset_window_buffer (FVAR (sf, minibuffer_window), buffer, Qnil);
+      minibuf_window = FVAR (sf, minibuffer_window);
     }
 
   /* Make sure no other frame has a minibuffer as its selected window,
@@ -247,7 +248,7 @@ read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
   val = Qnil;
   size = 100;
   len = 0;
-  line = (char *) xmalloc (size);
+  line = xmalloc (size);
 
   while ((c = getchar ()) != '\n')
     {
@@ -423,8 +424,8 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
     {
       if (CONSP (initial))
        {
-         Lisp_Object backup_n = Fcdr (initial);
-         initial = Fcar (initial);
+         Lisp_Object backup_n = XCDR (initial);
+         initial = XCAR (initial);
          CHECK_STRING (initial);
          if (!NILP (backup_n))
            {
@@ -621,7 +622,7 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
   /* Display this minibuffer in the proper window.  */
   Fset_window_buffer (minibuf_window, Fcurrent_buffer (), Qnil);
   Fselect_window (minibuf_window, Qnil);
-  XSETFASTINT (XWINDOW (minibuf_window)->hscroll, 0);
+  XWINDOW (minibuf_window)->hscroll = 0;
 
   Fmake_local_variable (Qprint_escape_newlines);
   print_escape_newlines = 1;
@@ -791,8 +792,8 @@ get_minibuffer (EMACS_INT depth)
   buf = Fcar (tail);
   if (NILP (buf) || NILP (BVAR (XBUFFER (buf), name)))
     {
-      sprintf (name, " *Minibuf-%"pI"d*", depth);
-      buf = Fget_buffer_create (build_string (name));
+      buf = Fget_buffer_create
+       (make_formatted_string (name, " *Minibuf-%"pI"d*", depth));
 
       /* Although the buffer's name starts with a space, undo should be
         enabled in it.  */
@@ -803,10 +804,9 @@ get_minibuffer (EMACS_INT depth)
   else
     {
       ptrdiff_t count = SPECPDL_INDEX ();
-      /* `reset_buffer' blindly sets the list of overlays to NULL, so we
-        have to empty the list, otherwise we end up with overlays that
-        think they belong to this buffer while the buffer doesn't know about
-        them any more.  */
+      /* We have to empty both overlay lists.  Otherwise we end
+        up with overlays that think they belong to this buffer
+        while the buffer doesn't know about them any more.  */
       delete_all_overlays (XBUFFER (buf));
       reset_buffer (XBUFFER (buf));
       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
@@ -887,8 +887,8 @@ read_minibuf_unwind (Lisp_Object data)
 
   /* Make sure minibuffer window is erased, not ignored.  */
   windows_or_buffers_changed++;
-  XSETFASTINT (XWINDOW (window)->last_modified, 0);
-  XSETFASTINT (XWINDOW (window)->last_overlay_modified, 0);
+  XWINDOW (window)->last_modified = 0;
+  XWINDOW (window)->last_overlay_modified = 0;
 
   /* In case the previous minibuffer displayed in this miniwindow is
      dead, we may keep displaying this buffer (tho it's inactive), so reset it,
@@ -1034,12 +1034,20 @@ Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
   (Lisp_Object prompt, Lisp_Object initial_input, Lisp_Object history, Lisp_Object default_value, Lisp_Object inherit_input_method)
 {
   Lisp_Object val;
+  ptrdiff_t count = SPECPDL_INDEX ();
+
+  /* Just in case we're in a recursive minibuffer, make it clear that the
+     previous minibuffer's completion table does not apply to the new
+     minibuffer.
+     FIXME: `minibuffer-completion-table' should be buffer-local instead.  */
+  specbind (Qminibuffer_completion_table, Qnil);
+
   val = Fread_from_minibuffer (prompt, initial_input, Qnil,
                               Qnil, history, default_value,
                               inherit_input_method);
   if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (default_value))
     val = CONSP (default_value) ? XCAR (default_value) : default_value;
-  return val;
+  return unbind_to (count, val);
 }
 
 DEFUN ("read-no-blanks-input", Fread_no_blanks_input, Sread_no_blanks_input, 1, 3, 0,
@@ -1094,10 +1102,11 @@ Prompt with PROMPT.  */)
 #endif /* NOTDEF */
 
 DEFUN ("read-variable", Fread_variable, Sread_variable, 1, 2, 0,
-       doc: /* Read the name of a user variable and return it as a symbol.
+       doc: /* Read the name of a user option and return it as a symbol.
 Prompt with PROMPT.  By default, return DEFAULT-VALUE or its first element
 if it is a list.
-A user variable is one for which `user-variable-p' returns non-nil.  */)
+A user option, or customizable variable, is one for which
+`custom-variable-p' returns non-nil.  */)
   (Lisp_Object prompt, Lisp_Object default_value)
 {
   Lisp_Object name, default_string;
@@ -1110,7 +1119,7 @@ A user variable is one for which `user-variable-p' returns non-nil.  */)
     default_string = default_value;
 
   name = Fcompleting_read (prompt, Vobarray,
-                          Quser_variable_p, Qt,
+                          Qcustom_variable_p, Qt,
                           Qnil, Qnil, default_string, Qnil);
   if (NILP (name))
     return name;
@@ -1264,7 +1273,7 @@ is used to further constrain the set of candidates.  */)
     {
       collection = check_obarray (collection);
       obsize = ASIZE (collection);
-      bucket = XVECTOR (collection)->contents[idx];
+      bucket = AREF (collection, idx);
     }
 
   while (1)
@@ -1299,7 +1308,7 @@ is used to further constrain the set of candidates.  */)
            break;
          else
            {
-             bucket = XVECTOR (collection)->contents[idx];
+             bucket = AREF (collection, idx);
              continue;
            }
        }
@@ -1527,7 +1536,7 @@ with a space are ignored unless STRING itself starts with a space.  */)
     {
       collection = check_obarray (collection);
       obsize = ASIZE (collection);
-      bucket = XVECTOR (collection)->contents[idx];
+      bucket = AREF (collection, idx);
     }
 
   while (1)
@@ -1562,7 +1571,7 @@ with a space are ignored unless STRING itself starts with a space.  */)
            break;
          else
            {
-             bucket = XVECTOR (collection)->contents[idx];
+             bucket = AREF (collection, idx);
              continue;
            }
        }
@@ -1725,8 +1734,6 @@ See also `completing-read-function'.  */)
   return Ffuncall (9, args);
 }
 \f
-Lisp_Object Fassoc_string (register Lisp_Object key, Lisp_Object list, Lisp_Object case_fold);
-
 /* Test whether TXT is an exact completion.  */
 DEFUN ("test-completion", Ftest_completion, Stest_completion, 2, 3, 0,
        doc: /* Return non-nil if STRING is a valid completion.
@@ -1772,7 +1779,7 @@ the values STRING, PREDICATE and `lambda'.  */)
        {
          for (i = ASIZE (collection) - 1; i >= 0; i--)
            {
-             tail = XVECTOR (collection)->contents[i];
+             tail = AREF (collection, i);
              if (SYMBOLP (tail))
                while (1)
                  {
@@ -1975,11 +1982,11 @@ syms_of_minibuf (void)
   staticpro (&last_minibuf_string);
   last_minibuf_string = Qnil;
 
-  DEFSYM (Quser_variable_p, "user-variable-p");
   DEFSYM (Qminibuffer_history, "minibuffer-history");
   DEFSYM (Qbuffer_name_history, "buffer-name-history");
   Fset (Qbuffer_name_history, Qnil);
 
+  DEFSYM (Qcustom_variable_p, "custom-variable-p");
   DEFSYM (Qminibuffer_setup_hook, "minibuffer-setup-hook");
   DEFSYM (Qminibuffer_exit_hook, "minibuffer-exit-hook");
   DEFSYM (Qhistory_length, "history-length");
@@ -2002,7 +2009,7 @@ The function is called with the arguments passed to `read-buffer'.  */);
 
   DEFVAR_BOOL ("read-buffer-completion-ignore-case",
               read_buffer_completion_ignore_case,
-              doc: /* *Non-nil means completion ignores case when reading a buffer name.  */);
+              doc: /* Non-nil means completion ignores case when reading a buffer name.  */);
   read_buffer_completion_ignore_case = 0;
 
   DEFVAR_LISP ("minibuffer-setup-hook", Vminibuffer_setup_hook,
@@ -2014,20 +2021,24 @@ The function is called with the arguments passed to `read-buffer'.  */);
   Vminibuffer_exit_hook = Qnil;
 
   DEFVAR_LISP ("history-length", Vhistory_length,
-              doc: /* *Maximum length for history lists before truncation takes place.
-A number means that length; t means infinite.  Truncation takes place
-just after a new element is inserted.  Setting the `history-length'
-property of a history variable overrides this default.  */);
+              doc: /* Maximum length of history lists before truncation takes place.
+A number means truncate to that length; truncation deletes old
+elements, and is done just after inserting a new element.
+A value of t means no truncation.
+
+This variable only affects history lists that don't specify their own
+maximum lengths.  Setting the `history-length' property of a history
+variable overrides this default.  */);
   XSETFASTINT (Vhistory_length, 30);
 
   DEFVAR_BOOL ("history-delete-duplicates", history_delete_duplicates,
-              doc: /* *Non-nil means to delete duplicates in history.
+              doc: /* Non-nil means to delete duplicates in history.
 If set to t when adding a new history element, all previous identical
 elements are deleted from the history list.  */);
   history_delete_duplicates = 0;
 
   DEFVAR_LISP ("history-add-new-input", Vhistory_add_new_input,
-              doc: /* *Non-nil means to add new elements in history.
+              doc: /* Non-nil means to add new elements in history.
 If set to nil, minibuffer reading functions don't add new elements to the
 history list, so it is possible to do this afterwards by calling
 `add-to-history' explicitly.  */);
@@ -2042,7 +2053,7 @@ controls the behavior, rather than this variable.  */);
   completion_ignore_case = 0;
 
   DEFVAR_BOOL ("enable-recursive-minibuffers", enable_recursive_minibuffers,
-              doc: /* *Non-nil means to allow minibuffer commands while in the minibuffer.
+              doc: /* Non-nil means to allow minibuffer commands while in the minibuffer.
 This variable makes a difference whenever the minibuffer window is active. */);
   enable_recursive_minibuffers = 0;
 
@@ -2098,7 +2109,7 @@ is added with
   Vminibuffer_history_position = Qnil;
 
   DEFVAR_BOOL ("minibuffer-auto-raise", minibuffer_auto_raise,
-              doc: /* *Non-nil means entering the minibuffer raises the minibuffer's frame.
+              doc: /* Non-nil means entering the minibuffer raises the minibuffer's frame.
 Some uses of the echo area also raise that frame (since they use it too).  */);
   minibuffer_auto_raise = 0;