use dynwind_begin and dynwind_end
[bpt/emacs.git] / src / minibuf.c
index 2c33b83..c53f91a 100644 (file)
@@ -1,6 +1,6 @@
 /* Minibuffer input and completion.
 
-Copyright (C) 1985-1986, 1993-2013 Free Software Foundation, Inc.
+Copyright (C) 1985-1986, 1993-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -231,7 +231,7 @@ read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
   val = Qnil;
   size = 100;
   len = 0;
-  line = xmalloc (size);
+  line = xmalloc_atomic (size);
 
   while ((c = getchar ()) != '\n')
     {
@@ -384,6 +384,7 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
   EMACS_INT pos = 0;
   /* String to add to the history.  */
   Lisp_Object histstring;
+  Lisp_Object histval;
 
   Lisp_Object empty_minibuf;
   Lisp_Object dummy, frame;
@@ -395,7 +396,8 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
      in previous recursive minibuffer, but was not set explicitly
      to t for this invocation, so set it to nil in this minibuffer.
      Save the old value now, before we change it.  */
-  specbind (intern ("minibuffer-completing-file-name"), Vminibuffer_completing_file_name);
+  specbind (intern ("minibuffer-completing-file-name"),
+           Vminibuffer_completing_file_name);
   if (EQ (Vminibuffer_completing_file_name, Qlambda))
     Vminibuffer_completing_file_name = Qnil;
 
@@ -535,6 +537,14 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
   if (!NILP (Vminibuffer_completing_file_name))
     Vminibuffer_completing_file_name = Qlambda;
 
+  /* If variable is unbound, make it nil.  */
+  histval = find_symbol_value (Vminibuffer_history_variable);
+  if (EQ (histval, Qunbound))
+    {
+      Fset (Vminibuffer_history_variable, Qnil);
+      histval = Qnil;
+    }
+
   if (inherit_input_method)
     {
       /* `current-input-method' is buffer local.  So, remember it in
@@ -568,22 +578,15 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
     bset_directory (current_buffer, ambient_dir);
   else
     {
-      Lisp_Object buf_list;
+      Lisp_Object tail, buf;
 
-      for (buf_list = Vbuffer_alist;
-          CONSP (buf_list);
-          buf_list = XCDR (buf_list))
-       {
-         Lisp_Object other_buf;
-
-         other_buf = XCDR (XCAR (buf_list));
-         if (STRINGP (BVAR (XBUFFER (other_buf), directory)))
-           {
-             bset_directory (current_buffer,
-                             BVAR (XBUFFER (other_buf), directory));
-             break;
-           }
-       }
+      FOR_EACH_LIVE_BUFFER (tail, buf)
+       if (STRINGP (BVAR (XBUFFER (buf), directory)))
+         {
+           bset_directory (current_buffer,
+                           BVAR (XBUFFER (buf), directory));
+           break;
+         }
     }
 
   if (!EQ (mini_frame, selected_frame))
@@ -679,12 +682,7 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
       XWINDOW (minibuf_window)->cursor.x = 0;
       XWINDOW (minibuf_window)->must_be_updated_p = 1;
       update_frame (XFRAME (selected_frame), 1, 1);
-      {
-        struct frame *f = XFRAME (XWINDOW (minibuf_window)->frame);
-        struct redisplay_interface *rif = FRAME_RIF (f);
-        if (rif && rif->flush_display)
-          rif->flush_display (f);
-      }
+      flush_frame (XFRAME (XWINDOW (minibuf_window)->frame));
     }
 
   /* Make minibuffer contents into a string.  */
@@ -715,13 +713,6 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
     {
       /* If the caller wanted to save the value read on a history list,
         then do so if the value is not already the front of the list.  */
-      Lisp_Object histval;
-
-      /* If variable is unbound, make it nil.  */
-
-      histval = find_symbol_value (Vminibuffer_history_variable);
-      if (EQ (histval, Qunbound))
-       Fset (Vminibuffer_history_variable, Qnil);
 
       /* The value of the history variable must be a cons or nil.  Other
         values are unacceptable.  We silently ignore these values.  */
@@ -877,11 +868,6 @@ read_minibuf_unwind (void)
   if (minibuf_level == 0)
     resize_mini_window (XWINDOW (window), 0);
 
-  /* Make sure minibuffer window is erased, not ignored.  */
-  windows_or_buffers_changed++;
-  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,
      to make sure we don't leave around bindings and stuff which only
@@ -982,9 +968,9 @@ and some related functions, which use zero-indexing for POSITION.  */)
 DEFUN ("read-string", Fread_string, Sread_string, 1, 5, 0,
        doc: /* Read a string from the minibuffer, prompting with string PROMPT.
 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
-  This argument has been superseded by DEFAULT-VALUE and should normally
-  be nil in new code.  It behaves as in `read-from-minibuffer'.  See the
-  documentation string of that function for details.
+  This argument has been superseded by DEFAULT-VALUE and should normally be nil
+  in new code.  It behaves as INITIAL-CONTENTS in `read-from-minibuffer' (which
+  see).
 The third arg HISTORY, if non-nil, specifies a history list
   and optionally the initial position in the list.
 See `read-from-minibuffer' for details of HISTORY argument.
@@ -1213,13 +1199,10 @@ is used to further constrain the set of candidates.  */)
     type = (HASH_TABLE_P (collection) ? hash_table
            : VECTORP (collection) ? obarray_table
            : ((NILP (collection)
-               || (CONSP (collection)
-                   && (!SYMBOLP (XCAR (collection))
-                       || NILP (XCAR (collection)))))
+               || (CONSP (collection) && !FUNCTIONP (collection)))
               ? list_table : function_table));
   ptrdiff_t idx = 0, obsize = 0;
   int matchcount = 0;
-  ptrdiff_t bindcount = -1;
   Lisp_Object bucket, zero, end, tem;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
 
@@ -1304,18 +1287,17 @@ is used to further constrain the set of candidates.  */)
 
          /* Ignore this element if it fails to match all the regexps.  */
          {
+            dynwind_begin ();
+            specbind (Qcase_fold_search,
+                      completion_ignore_case ? Qt : Qnil);
            for (regexps = Vcompletion_regexp_list; CONSP (regexps);
                 regexps = XCDR (regexps))
              {
-               if (bindcount < 0) {
-                 bindcount = SPECPDL_INDEX ();
-                 specbind (Qcase_fold_search,
-                           completion_ignore_case ? Qt : Qnil);
-               }
                tem = Fstring_match (XCAR (regexps), eltstring, zero);
                if (NILP (tem))
                  break;
              }
+            dynwind_end ();
            if (CONSP (regexps))
              continue;
          }
@@ -1329,11 +1311,6 @@ is used to further constrain the set of candidates.  */)
                tem = Fcommandp (elt, Qnil);
              else
                {
-                 if (bindcount >= 0)
-                   {
-                     unbind_to (bindcount, Qnil);
-                     bindcount = -1;
-                   }
                  GCPRO4 (tail, string, eltstring, bestmatch);
                  tem = (type == hash_table
                         ? call2 (predicate, elt,
@@ -1412,11 +1389,6 @@ is used to further constrain the set of candidates.  */)
        }
     }
 
-  if (bindcount >= 0) {
-    unbind_to (bindcount, Qnil);
-    bindcount = -1;
-  }
-
   if (NILP (bestmatch))
     return Qnil;               /* No completions found.  */
   /* If we are ignoring case, and there is no exact match,
@@ -1474,11 +1446,8 @@ with a space are ignored unless STRING itself starts with a space.  */)
   Lisp_Object allmatches;
   int type = HASH_TABLE_P (collection) ? 3
     : VECTORP (collection) ? 2
-    : NILP (collection) || (CONSP (collection)
-                           && (!SYMBOLP (XCAR (collection))
-                               || NILP (XCAR (collection))));
+    : NILP (collection) || (CONSP (collection) && !FUNCTIONP (collection));
   ptrdiff_t idx = 0, obsize = 0;
-  ptrdiff_t bindcount = -1;
   Lisp_Object bucket, tem, zero;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
 
@@ -1569,18 +1538,17 @@ with a space are ignored unless STRING itself starts with a space.  */)
 
          /* Ignore this element if it fails to match all the regexps.  */
          {
+            dynwind_begin ();
+            specbind (Qcase_fold_search,
+                      completion_ignore_case ? Qt : Qnil);
            for (regexps = Vcompletion_regexp_list; CONSP (regexps);
                 regexps = XCDR (regexps))
              {
-               if (bindcount < 0) {
-                 bindcount = SPECPDL_INDEX ();
-                 specbind (Qcase_fold_search,
-                           completion_ignore_case ? Qt : Qnil);
-               }
                tem = Fstring_match (XCAR (regexps), eltstring, zero);
                if (NILP (tem))
                  break;
              }
+            dynwind_end ();
            if (CONSP (regexps))
              continue;
          }
@@ -1594,10 +1562,6 @@ with a space are ignored unless STRING itself starts with a space.  */)
                tem = Fcommandp (elt, Qnil);
              else
                {
-                 if (bindcount >= 0) {
-                   unbind_to (bindcount, Qnil);
-                   bindcount = -1;
-                 }
                  GCPRO4 (tail, eltstring, allmatches, string);
                  tem = type == 3
                    ? call2 (predicate, elt,
@@ -1612,11 +1576,6 @@ with a space are ignored unless STRING itself starts with a space.  */)
        }
     }
 
-  if (bindcount >= 0) {
-    unbind_to (bindcount, Qnil);
-    bindcount = -1;
-  }
-
   return Fnreverse (allmatches);
 }
 \f
@@ -1705,9 +1664,7 @@ the values STRING, PREDICATE and `lambda'.  */)
 
   CHECK_STRING (string);
 
-  if ((CONSP (collection)
-       && (!SYMBOLP (XCAR (collection)) || NILP (XCAR (collection))))
-      || NILP (collection))
+  if (NILP (collection) || (CONSP (collection) && !FUNCTIONP (collection)))
     {
       tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
       if (NILP (tem))
@@ -1924,6 +1881,8 @@ init_minibuf_once (void)
 void
 syms_of_minibuf (void)
 {
+#include "minibuf.x"
+
   minibuf_level = 0;
   minibuf_prompt = Qnil;
   staticpro (&minibuf_prompt);
@@ -1990,7 +1949,7 @@ 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);
+  XSETFASTINT (Vhistory_length, 100);
 
   DEFVAR_BOOL ("history-delete-duplicates", history_delete_duplicates,
               doc: /* Non-nil means to delete duplicates in history.
@@ -2098,28 +2057,4 @@ properties.  */);
   /* We use `intern' here instead of Qread_only to avoid
      initialization-order problems.  */
   Vminibuffer_prompt_properties = list2 (intern_c_string ("read-only"), Qt);
-
-  defsubr (&Sactive_minibuffer_window);
-  defsubr (&Sset_minibuffer_window);
-  defsubr (&Sread_from_minibuffer);
-  defsubr (&Sread_string);
-  defsubr (&Sread_command);
-  defsubr (&Sread_variable);
-  defsubr (&Sinternal_complete_buffer);
-  defsubr (&Sread_buffer);
-  defsubr (&Sread_no_blanks_input);
-  defsubr (&Sminibuffer_depth);
-  defsubr (&Sminibuffer_prompt);
-
-  defsubr (&Sminibufferp);
-  defsubr (&Sminibuffer_prompt_end);
-  defsubr (&Sminibuffer_contents);
-  defsubr (&Sminibuffer_contents_no_properties);
-  defsubr (&Sminibuffer_completion_contents);
-
-  defsubr (&Stry_completion);
-  defsubr (&Sall_completions);
-  defsubr (&Stest_completion);
-  defsubr (&Sassoc_string);
-  defsubr (&Scompleting_read);
 }