Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Input from buffer instead of minibuffer
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michael Haensel  
View profile  
 More options Sep 30 2012, 4:46 pm
Newsgroups: gnu.emacs.help
From: Michael Haensel <mhaense...@gmail.com>
Date: Sun, 30 Sep 2012 13:46:03 -0700 (PDT)
Local: Sun, Sep 30 2012 4:46 pm
Subject: Input from buffer instead of minibuffer
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Sep 30 2012, 9:10 pm
Newsgroups: gnu.emacs.help
From: Barry Margolin <bar...@alum.mit.edu>
Date: Sun, 30 Sep 2012 21:10:43 -0400
Local: Sun, Sep 30 2012 9:10 pm
Subject: Re: Input from buffer instead of minibuffer
In article <58f60ac7-2407-411c-92ba-13a4550f682a@googlegroups.com>,
 Michael Haensel <mhaense...@gmail.com> wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephen Berman  
View profile  
 More options Oct 3 2012, 11:46 am
Newsgroups: gnu.emacs.help
From: Stephen Berman <stephen.ber...@gmx.net>
Date: Wed, 03 Oct 2012 17:45:00 +0200
Subject: Re: Input from buffer instead of minibuffer
On Sun, 30 Sep 2012 21:10:43 -0400 Barry Margolin <bar...@alum.mit.edu> wrote:

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

Steve Berman


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan Monnier  
View profile  
 More options Oct 4 2012, 9:21 am
Newsgroups: gnu.emacs.help
From: Stefan Monnier <monn...@iro.umontreal.ca>
Date: Thu, 04 Oct 2012 09:21:28 -0400
Local: Thurs, Oct 4 2012 9:21 am
Subject: Re: Input from buffer instead of minibuffer

> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal J. Bourguignon  
View profile  
 More options Oct 4 2012, 9:56 am
Newsgroups: gnu.emacs.help
From: "Pascal J. Bourguignon" <p...@informatimago.com>
Date: Thu, 04 Oct 2012 15:56:49 +0200
Local: Thurs, Oct 4 2012 9:56 am
Subject: Re: Input from buffer instead of minibuffer

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »