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

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

Re: Line editor with dynamic prompt


From: Pascal J. Bourguignon
Subject: Re: Line editor with dynamic prompt
Date: Wed, 29 Oct 2014 08:46:05 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

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




reply via email to

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