Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Input from buffer instead of minibuffer

44 views
Skip to first unread message

Michael Haensel

unread,
Sep 30, 2012, 4:46:03 PM9/30/12
to
Hello everyone -

I'm writing a quiz program in Emacs Lisp. The program creates a new frame and buffer for the quiz questions. The quiz then runs something like this:

(insert "Please identify: [quiz item]")
(setq response
(read-from-minibuffer "Please identify: [quiz item]"))
... do stuff based on response

This displays the question in the buffer and the minibuffer. The response is read in from the minibuffer. This isn't deal-breakingly bad, but a better design would read the response from the buffer and skip the minibuffer entirely.

Is there an easy way to read a response from the buffer instead of the minibuffer? If it matters, a "response" is a string of 1-8 alphabetic characters terminated by a newline/return key.

Barry Margolin

unread,
Sep 30, 2012, 9:10:43 PM9/30/12
to
In article <58f60ac7-2407-411c...@googlegroups.com>,
Nothing built-in, but it should be pretty straightforward to write a
command that looks in the buffer to find the response, and bind the
Return key to this command.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Stephen Berman

unread,
Oct 3, 2012, 11:45:00 AM10/3/12
to help-gn...@gnu.org
On Sun, 30 Sep 2012 21:10:43 -0400 Barry Margolin <bar...@alum.mit.edu> wrote:

> In article <58f60ac7-2407-411c...@googlegroups.com>,
> Michael Haensel <mhaen...@gmail.com> wrote:
>
>> Hello everyone -
>>
>> I'm writing a quiz program in Emacs Lisp. The program creates a new frame and
>> buffer for the quiz questions. The quiz then runs something like this:
>>
>> (insert "Please identify: [quiz item]")
>> (setq response
>> (read-from-minibuffer "Please identify: [quiz item]"))
>> ... do stuff based on response
>>
>> This displays the question in the buffer and the minibuffer. The response is
>> read in from the minibuffer. This isn't deal-breakingly bad, but a better
>> design would read the response from the buffer and skip the minibuffer
>> entirely.
>>
>> Is there an easy way to read a response from the buffer instead of the
>> minibuffer? If it matters, a "response" is a string of 1-8 alphabetic
>> characters terminated by a newline/return key.
>
> Nothing built-in, but it should be pretty straightforward to write a
> command that looks in the buffer to find the response, and bind the
> Return key to this command.

How about widgets? See (info "widget") for examples and details.

Steve Berman


Stefan Monnier

unread,
Oct 4, 2012, 9:21:28 AM10/4/12
to
> Is there an easy way to read a response from the buffer instead of the
> minibuffer? If it matters, a "response" is a string of 1-8 alphabetic
> characters terminated by a newline/return key.

You might want to do it in the same way as M-x doctor.


Stefan

Pascal J. Bourguignon

unread,
Oct 4, 2012, 9:56:49 AM10/4/12
to
First, read can read lisp objects from a buffer, at the point (and
advancing it). But it won't be convenient for user input.

Next, there's read-char which just reads a character from the user, kind
of low-level. You can use it to make a blocking input function.

----------------------------------------------------------------------------------
(require 'cl)

(defun newline-char-p (ch) (= ch ?\n) (= ch ?\r))
(defun char-erase-char-p (ch) (or (= ch ? ) (= ch ? )))
(defun word-erase-char-p (ch) (= ch ? ))
(defun kill-line-char-p (ch) (= ch ? ))
(defun reprint-line-char-p (ch) (= ch ? ))
(defun interrupt-char-p (ch) (= ch ? ))
(defun whitespace-char-p (ch) (or (= ch 32) (= ch 9)))

(defun* read-line ()
"Reads a line from the user with read-char, echoing it in the current buffer.
RETURN: the line read from the user."
(let ((buffer '())
(start-pt (point)))
(loop
(let ((ch (read-char)))
(cond
((newline-char-p ch)
(insert "\n")
(return-from read-line (coerce (reverse buffer) 'string)))
((and (char-erase-char-p ch) buffer)
(pop buffer)
(delete-backward-char 1))
((and (word-erase-char-p ch) buffer)
(loop
named erase-whitespaces
while (and buffer (whitespace-char-p (first buffer)))
do (pop buffer) (delete-backward-char 1))
(loop
named erase-word
while (and buffer (not (whitespace-char-p (first buffer))))
do (pop buffer) (delete-backward-char 1)))
((and (kill-line-char-p ch) buffer)
(setf buffer '())
(delete-region start-pt (point)))
((reprint-line-char-p ch)
(delete-region start-pt (point))
(insert (coerce (reverse buffer) 'string)))
((interrupt-char-p ch)
(return-from read-line nil))
(t
(push ch buffer)
(insert (format "%c" ch))))))))

(progn (insert "\nEnter something: ")
(read-line))
----------------------------------------------------------------------------------

But as you can see, you take control over emacs usual event loop this
way, which may be what you want or not.

So the last option, is to just let the user edit the buffer as he wants,
and bind a special function on a key, such as RET, that will analyze the
buffer when the user press that, to infer some input.

------------------------------------------------------------------------
(defvar *input-start-point* (make-marker))
(defvar *input-callback* (lambda (string) (insert "\ngot: %S\n" string)))
(defvar *input-old-newline* nil)

(defun start-input ()
(set-marker *input-start-point* (point))
(setf *input-old-newline* (key-binding (kbd "RET"))))

(defun newline-and-return-input ()
(interactive)
(let ((end-pt (point)))
(if (<= *input-start-point* end-pt)
(let ((input (buffer-substring *input-start-point* end-pt)))
(set-marker *input-start-point* nil)
(local-set-key (kbd "RET") *input-old-newline*)
(funcall *input-callback* input))
(funcall *input-old-newline*))))

(progn
(insert "\nEnter something: ")
(start-input)
(local-set-key (kbd "RET") 'newline-and-return-input))
------------------------------------------------------------------------

The advantage here is that emacs still runs, so you can use whatever
editing command, switch to another buffer, come back, etc. You can edit
anything about the prompt point, but if you type RET after it, the
callback is called with the "input" text. Notice you can insert
newlines with C-q RET, so the "input" text can be multiline too. The
inconvenient of course is that it's asynchronous, and you need to go
thru the callback to do something with the "input" text.


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
0 new messages