+ int, int, int));
+static Lisp_Object read_minibuf_noninteractive P_ ((Lisp_Object, Lisp_Object,
+ Lisp_Object, Lisp_Object,
+ int, Lisp_Object,
+ Lisp_Object, Lisp_Object,
+ int, int));
+static Lisp_Object string_to_object P_ ((Lisp_Object, Lisp_Object));
+
+
+/* Read a Lisp object from VAL and return it. If VAL is an empty
+ string, and DEFALT is a string, read from DEFALT instead of VAL. */
+
+static Lisp_Object
+string_to_object (val, defalt)
+ Lisp_Object val, defalt;
+{
+ struct gcpro gcpro1, gcpro2;
+ Lisp_Object expr_and_pos;
+ int pos;
+
+ GCPRO2 (val, defalt);
+
+ if (STRINGP (val) && SCHARS (val) == 0
+ && STRINGP (defalt))
+ val = defalt;
+
+ expr_and_pos = Fread_from_string (val, Qnil, Qnil);
+ pos = XINT (Fcdr (expr_and_pos));
+ if (pos != SCHARS (val))
+ {
+ /* Ignore trailing whitespace; any other trailing junk
+ is an error. */
+ int i;
+ pos = string_char_to_byte (val, pos);
+ for (i = pos; i < SBYTES (val); i++)
+ {
+ int c = SREF (val, i);
+ if (c != ' ' && c != '\t' && c != '\n')
+ error ("Trailing garbage following expression");
+ }
+ }
+
+ val = Fcar (expr_and_pos);
+ RETURN_UNGCPRO (val);
+}
+
+
+/* Like read_minibuf but reading from stdin. This function is called
+ from read_minibuf to do the job if noninteractive. */
+
+static Lisp_Object
+read_minibuf_noninteractive (map, initial, prompt, backup_n, expflag,
+ histvar, histpos, defalt, allow_props,
+ inherit_input_method)
+ Lisp_Object map;
+ Lisp_Object initial;
+ Lisp_Object prompt;
+ Lisp_Object backup_n;
+ int expflag;
+ Lisp_Object histvar;
+ Lisp_Object histpos;
+ Lisp_Object defalt;
+ int allow_props;
+ int inherit_input_method;
+{
+ int size, len;
+ char *line, *s;
+ Lisp_Object val;
+
+ fprintf (stdout, "%s", SDATA (prompt));
+ fflush (stdout);
+
+ val = Qnil;
+ size = 100;
+ len = 0;
+ line = (char *) xmalloc (size * sizeof *line);
+ while ((s = fgets (line + len, size - len, stdin)) != NULL
+ && (len = strlen (line),
+ len == size - 1 && line[len - 1] != '\n'))
+ {
+ size *= 2;
+ line = (char *) xrealloc (line, size);
+ }
+
+ if (s)
+ {
+ len = strlen (line);
+
+ if (len > 0 && line[len - 1] == '\n')
+ line[--len] = '\0';
+
+ val = build_string (line);
+ xfree (line);
+ }
+ else
+ {
+ xfree (line);
+ error ("Error reading from stdin");
+ }
+
+ /* If Lisp form desired instead of string, parse it. */
+ if (expflag)
+ val = string_to_object (val, defalt);
+
+ return val;
+}
+\f
+DEFUN ("minibufferp", Fminibufferp,
+ Sminibufferp, 0, 1, 0,
+ doc: /* Return t if BUFFER is a minibuffer.
+No argument or nil as argument means use current buffer as BUFFER.
+BUFFER can be a buffer or a buffer name. */)
+ (buffer)
+ Lisp_Object buffer;
+{
+ Lisp_Object tem;
+
+ if (NILP (buffer))
+ buffer = Fcurrent_buffer ();
+ else if (STRINGP (buffer))
+ buffer = Fget_buffer (buffer);
+ else
+ CHECK_BUFFER (buffer);
+
+ tem = Fmemq (buffer, Vminibuffer_list);
+ return ! NILP (tem) ? Qt : Qnil;
+}
+
+DEFUN ("minibuffer-prompt-end", Fminibuffer_prompt_end,
+ Sminibuffer_prompt_end, 0, 0, 0,
+ doc: /* Return the buffer position of the end of the minibuffer prompt.
+Return (point-min) if current buffer is not a minibuffer. */)
+ ()
+{
+ /* This function is written to be most efficient when there's a prompt. */
+ Lisp_Object beg, end, tem;
+ beg = make_number (BEGV);
+
+ tem = Fmemq (Fcurrent_buffer (), Vminibuffer_list);
+ if (NILP (tem))
+ return beg;
+
+ end = Ffield_end (beg, Qnil, Qnil);
+
+ if (XINT (end) == ZV && NILP (Fget_char_property (beg, Qfield, Qnil)))
+ return beg;
+ else
+ return end;
+}
+
+DEFUN ("minibuffer-contents", Fminibuffer_contents,
+ Sminibuffer_contents, 0, 0, 0,
+ doc: /* Return the user input in a minibuffer as a string.
+The current buffer must be a minibuffer. */)
+ ()
+{
+ int prompt_end = XINT (Fminibuffer_prompt_end ());
+ return make_buffer_string (prompt_end, ZV, 1);
+}