help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Input from buffer instead of minibuffer


From: Pascal J. Bourguignon
Subject: Re: Input from buffer instead of minibuffer
Date: Thu, 04 Oct 2012 15:56:49 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)

Michael Haensel <mhaensel73@gmail.com> writes:

> 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.

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 {}.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]