From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Line editor with dynamic prompt
Date: Wed, 29 Oct 2014 08:46:05 +0100 [thread overview]
Message-ID: <87zjcf48ua.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: CAF+O-CVi4RLK+2hTZL3D=QxJ94oori+xpS4Yt=TPps653fHBJQ@mail.gmail.com
Whitfield Diffie <whitfield.diffie@gmail.com> writes:
> The attached program --- whose calling sequence is
>
> (lineedit prompt-base starting-string position-in-string)
>
> displays the length of the string and the position of point in the
> string as part of the prompt; it changes as point moves or the string
> is edited.
>
> Does anyone know how to do this without having to write the line
> editor by hand?
Yes, somebody knows how to do it without having to write the line editor
by hand.
The question is why you need to input the string from the minibuffer.
This is an operation that is modal. Emacs is a mostly modless editor
and user interface framework.
The idea is to let the user choose what he is editing at any time.
When the user types some key chord that correspond to commands that
require more information, there may be some modal interaction and
editing occuring in the minibuffer. For example, C-x C-f will require
the user to edit the path of the file to be visited in the minibuffer.
For those small modal editing operations, it has not been deemed useful
to provide the length of input and cursor position.
If you let the user edit this string in a normal buffer, then you can
use the minor modes line-number-mode and column-number-mode to see the
current line and column displayed in the mode line. I suppose you could
write a similar minor mode that would also display the line length.
To process the input you would bind a command to a key, perhaps RET or
C-RET or something else, and this command could collect the string from
the buffer
Otherwise, you may perhaps do something. Those modal operations
(read-from-minibufffer, read-string, read-file-name etc) are implemented
in C, you wouldn't want to modify them (you'd have to recompile emacs).
You can use them with the minibuffer-with-setup-hook to configure the
minibuffer however you want. For example:
(defun minibuffer-meat ()
(interactive)
(local-set-key (kbd "!") (lambda () (interactive) (insert "!!!"))))
(minibuffer-with-setup-hook (function minibuffer-meat)
(read-string "TRY> ")) C-u C-e C-x Hello world! RET
returns:
"Hello world!!!"
You can also use directly minibuffer-setup-hook and
minibuffer-exit-hook.
In those hooks, you could set things up so that the minibuffer contents
(probably not the prompt) is edited to contain the position and length
of the string, or you could display it in the mode line.
With:
(defun my-minibuffer-postcommand ()
(save-excursion
(message "length = %d"
(- (progn (end-of-line) (point))
(progn (beginning-of-line) (point))))))
(defun my-minibuffer-setup-meat ()
(add-hook 'post-command-hook 'my-minibuffer-postcommand))
(defun my-minibuffer-exit-meat ()
(remove-hook 'post-command-hook 'my-minibuffer-postcommand))
(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-meat)
(add-hook 'minibuffer-exit-hook 'my-minibuffer-exit-meat)
(minibuffer-with-setup-hook (function minibuffer-meat)
(read-string "TRY> ")) C-u C-x C-e hello SPC RET
will display in *Message* (and also in the minibuffer, oops):
length = 0
length = 1
length = 2
length = 3
length = 4
length = 5
length = 6
and will return:
"hello "
(remove-hook 'minibuffer-setup-hook 'my-minibuffer-setup-meat)
(remove-hook 'minibuffer-exit-hook 'my-minibuffer-exit-meat)
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
prev parent reply other threads:[~2014-10-29 7:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-28 16:15 Line editor with dynamic prompt Whitfield Diffie
2014-10-29 3:44 ` Michael Heerdegen
2014-10-29 7:46 ` Pascal J. Bourguignon [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87zjcf48ua.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).