* How to handle default value in read-string?
@ 2015-08-03 21:00 Marcin Borkowski
2015-08-03 21:17 ` John Mastro
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Marcin Borkowski @ 2015-08-03 21:00 UTC (permalink / raw)
To: Help Gnu Emacs mailing list
Hi everybody,
I'd like read-string to somehow indicate the default value. A natural
idea is to include it in the prompt, for example having a prompt like
(format "Foo (%s): " default)
However, Icicles' version of read-string already does exactly that, so
for Icicles users this would be superfluous.
So here's the question: is there a better way than just have a prompt of
(if icicle-mode (don't-include-default) (do-include-default))?
TIA,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to handle default value in read-string?
2015-08-03 21:00 How to handle default value in read-string? Marcin Borkowski
@ 2015-08-03 21:17 ` John Mastro
2015-08-03 23:40 ` Marcin Borkowski
2015-08-03 23:51 ` Drew Adams
2015-08-05 20:02 ` Emanuel Berg
2 siblings, 1 reply; 8+ messages in thread
From: John Mastro @ 2015-08-03 21:17 UTC (permalink / raw)
To: Help Gnu Emacs mailing list
> I'd like read-string to somehow indicate the default value. A natural
> idea is to include it in the prompt, for example having a prompt like
>
> (format "Foo (%s): " default)
>
> However, Icicles' version of read-string already does exactly that, so
> for Icicles users this would be superfluous.
>
> So here's the question: is there a better way than just have a prompt of
>
> (if icicle-mode (don't-include-default) (do-include-default))?
There's no getting around that you'll need a condition somewhere.
However, you can of course wrap it up in a helper function, so you're
not repeating the condition every time you use `read-string'.
(defun my-read-string (prompt &optional ...)
(unless (bound-and-true-p icicle-mode)
(setq prompt (concat prompt " (%s)" default)))
(read-string prompt ...))
You could use advice to do the same thing but I don't think it would be
an improvement in this case.
--
john
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to handle default value in read-string?
2015-08-03 21:17 ` John Mastro
@ 2015-08-03 23:40 ` Marcin Borkowski
0 siblings, 0 replies; 8+ messages in thread
From: Marcin Borkowski @ 2015-08-03 23:40 UTC (permalink / raw)
To: Help Gnu Emacs mailing list
On 2015-08-03, at 23:17, John Mastro <john.b.mastro@gmail.com> wrote:
>> I'd like read-string to somehow indicate the default value. A natural
>> idea is to include it in the prompt, for example having a prompt like
>>
>> (format "Foo (%s): " default)
>>
>> However, Icicles' version of read-string already does exactly that, so
>> for Icicles users this would be superfluous.
>>
>> So here's the question: is there a better way than just have a prompt of
>>
>> (if icicle-mode (don't-include-default) (do-include-default))?
>
> There's no getting around that you'll need a condition somewhere.
Well, that was more or less obvious... However, your suggestion of
bound-and-true-p (below) is (obviously) a significant improvement for
anyone without Icicles loaded:-). Thanks!
> However, you can of course wrap it up in a helper function, so you're
> not repeating the condition every time you use `read-string'.
>
> (defun my-read-string (prompt &optional ...)
> (unless (bound-and-true-p icicle-mode)
> (setq prompt (concat prompt " (%s)" default)))
> (read-string prompt ...))
>
> You could use advice to do the same thing but I don't think it would be
> an improvement in this case.
Thanks a lot!
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: How to handle default value in read-string?
2015-08-03 21:00 How to handle default value in read-string? Marcin Borkowski
2015-08-03 21:17 ` John Mastro
@ 2015-08-03 23:51 ` Drew Adams
2015-08-04 18:44 ` Marcin Borkowski
2015-08-19 21:15 ` Marcin Borkowski
2015-08-05 20:02 ` Emanuel Berg
2 siblings, 2 replies; 8+ messages in thread
From: Drew Adams @ 2015-08-03 23:51 UTC (permalink / raw)
To: Marcin Borkowski, Help Gnu Emacs mailing list
> I'd like read-string to somehow indicate the default value. A natural
> idea is to include it in the prompt, for example having a prompt like
>
> (format "Foo (%s): " default)
>
> However, Icicles' version of read-string already does exactly that, so
> for Icicles users this would be superfluous.
>
> So here's the question: is there a better way than just have a prompt of
>
> (if icicle-mode (don't-include-default) (do-include-default))?
That's OK. Or wrap the `read-string' call in:
(let ((icicle-default-value nil)) ...)
A nil value of `icicle-default-value' tells Icicles not to put the
default value in the prompt. Then you can add it to the prompt
explicitly, so it will be there with and without Icicle mode:
(defun foo (strg)
(interactive
(let ((icicle-default-value nil))
(list (read-string "String (default my-default): "
nil nil "my-default")))))
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to handle default value in read-string?
2015-08-03 23:51 ` Drew Adams
@ 2015-08-04 18:44 ` Marcin Borkowski
2015-08-19 21:15 ` Marcin Borkowski
1 sibling, 0 replies; 8+ messages in thread
From: Marcin Borkowski @ 2015-08-04 18:44 UTC (permalink / raw)
To: Help Gnu Emacs mailing list
On 2015-08-04, at 01:51, Drew Adams <drew.adams@oracle.com> wrote:
>> I'd like read-string to somehow indicate the default value. A natural
>> idea is to include it in the prompt, for example having a prompt like
>>
>> (format "Foo (%s): " default)
>>
>> However, Icicles' version of read-string already does exactly that, so
>> for Icicles users this would be superfluous.
>>
>> So here's the question: is there a better way than just have a prompt of
>>
>> (if icicle-mode (don't-include-default) (do-include-default))?
>
> That's OK. Or wrap the `read-string' call in:
>
> (let ((icicle-default-value nil)) ...)
>
> A nil value of `icicle-default-value' tells Icicles not to put the
> default value in the prompt. Then you can add it to the prompt
> explicitly, so it will be there with and without Icicle mode:
>
> (defun foo (strg)
> (interactive
> (let ((icicle-default-value nil))
> (list (read-string "String (default my-default): "
> nil nil "my-default")))))
Thanks, Drew, that's much better!
Fun fact: I thought that putting the default into the prompt was the
default Emacs behavior, until I ran my code with Icicles off (I
occasionally turn Icicles off to restore some keybindings used by
Org-mode. Yes, yes, I should check how to customize Icicles. I'll do
it some day;-).). Then I spent a few minutes looking for a bug in my
code ("Did I change anything recently that could influence the
read-string prompt?"). Then it dawned upon me that Icicles (again?)
does what (maybe) Emacs should do by default...
Drew, thanks again for Icicles. It is so cool. (The only problem
I have with it is speed. But I'll move to a faster computer within
a few weeks, and I hope this won't be an issue then.)
Best,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to handle default value in read-string?
2015-08-03 21:00 How to handle default value in read-string? Marcin Borkowski
2015-08-03 21:17 ` John Mastro
2015-08-03 23:51 ` Drew Adams
@ 2015-08-05 20:02 ` Emanuel Berg
2 siblings, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2015-08-05 20:02 UTC (permalink / raw)
To: help-gnu-emacs
Marcin Borkowski <mbork@mbork.pl> writes:
> I'd like read-string to somehow indicate the default
> value. A natural idea is to include it in the
> prompt, for example having a prompt like
>
> (format "Foo (%s): " default)
>
> However, Icicles' version of read-string already does
> exactly that, so for Icicles users this would
> be superfluous.
>
> So here's the question: is there a better way than
> just have a prompt of
>
> (if icicle-mode (don't-include-default)
> (do-include-default))?
You want it to be there for everyone that uses your
thing, not just those using icicle-mode as well.
You shouldn't require it just to get such a small
thing, so either way you must include your own
version. And if you include your own version, you may
as well just use it for everyone and don't bother with
Icicles at all. That way is more consistent as well as
more logical.
The best solution for everyone involved is of course
for vanilla Emacs to have a thing for this and then
everyone that needs it can use it from there.
But since it is so simple why not just put it there
and move on?
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to handle default value in read-string?
2015-08-03 23:51 ` Drew Adams
2015-08-04 18:44 ` Marcin Borkowski
@ 2015-08-19 21:15 ` Marcin Borkowski
2015-08-21 7:46 ` Drew Adams
1 sibling, 1 reply; 8+ messages in thread
From: Marcin Borkowski @ 2015-08-19 21:15 UTC (permalink / raw)
To: Help Gnu Emacs mailing list
On 2015-08-04, at 01:51, Drew Adams <drew.adams@oracle.com> wrote:
>> I'd like read-string to somehow indicate the default value. A natural
>> idea is to include it in the prompt, for example having a prompt like
>>
>> (format "Foo (%s): " default)
>>
>> However, Icicles' version of read-string already does exactly that, so
>> for Icicles users this would be superfluous.
>>
>> So here's the question: is there a better way than just have a prompt of
>>
>> (if icicle-mode (don't-include-default) (do-include-default))?
>
> That's OK. Or wrap the `read-string' call in:
>
> (let ((icicle-default-value nil)) ...)
>
> A nil value of `icicle-default-value' tells Icicles not to put the
> default value in the prompt. Then you can add it to the prompt
> explicitly, so it will be there with and without Icicle mode:
>
> (defun foo (strg)
> (interactive
> (let ((icicle-default-value nil))
> (list (read-string "String (default my-default): "
> nil nil "my-default")))))
OK, so I sat to this today, and didn't manage to get it to work. Here's
my function:
--8<---------------cut here---------------start------------->8---
(defun my-read-string (prompt &optional initial-input history default-value inherit-input-method)
"A replacement for `read-string', displaying the default.
Also, not displaying it twice should the user use Icicles."
(let ((icicle-default-value nil)
(prompt-with-default
(progn
(string-match "\\(: \\)?$" prompt)
(replace-match (format " (%s)\\1" default-value) t nil prompt))))
(read-string prompt-with-default initial-input history default-value inherit-input-method)))
--8<---------------cut here---------------end--------------->8---
If I do M-: (my-read-string "foo: " nil nil "bar") with Icicles off,
everything is fine. If Icicles are on, however, this is what I get:
foo (bar) (bar): -!-
Setting icicle-default-value to nil globally doesn't help, either.
My Icicles version is
--8<---------------cut here---------------start------------->8---
;; Version: 2015.04.03
;; Package-Requires: ()
;; Last-Updated: Fri Apr 3 09:12:40 2015 (-0700)
;; By: dradams
;; Update #: 23650
--8<---------------cut here---------------end--------------->8---
What am I doing wrong?
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: How to handle default value in read-string?
2015-08-19 21:15 ` Marcin Borkowski
@ 2015-08-21 7:46 ` Drew Adams
0 siblings, 0 replies; 8+ messages in thread
From: Drew Adams @ 2015-08-21 7:46 UTC (permalink / raw)
To: Marcin Borkowski, Help Gnu Emacs mailing list
> > (let ((icicle-default-value nil)) ...)
> >
> > A nil value of `icicle-default-value' tells Icicles not to put the
> > default value in the prompt. Then you can add it to the prompt
> > explicitly, so it will be there with and without Icicle mode:
> >
> > (defun foo (strg)
> > (interactive
> > (let ((icicle-default-value nil))
> > (list (read-string "String (default my-default): "
> > nil nil "my-default")))))
>
> OK, so I sat to this today, and didn't manage to get it to work.
> Here's my function:
>
> (defun my-read-string (prompt &optional initial-input history
> default-value inherit-input-method)
> (let ((icicle-default-value nil)
> (prompt-with-default
> (progn
> (string-match "\\(: \\)?$" prompt)
> (replace-match (format " (%s)\\1" default-value)
> t nil prompt))))
> (read-string prompt-with-default initial-input history
> default-value inherit-input-method)))
>
> If I do M-: (my-read-string "foo: " nil nil "bar") with Icicles
> off, everything is fine. If Icicles are on, however, this is
> what I get: foo (bar) (bar):
> What am I doing wrong?
1. Let me say first that you need not be hobbled here by
Icicles. ;-) You can easily take Icicles out of the mix
altogether if you like, by simply customizing option
`icicle-functions-to-redefine', removing `read-string'
from the list of functions to replace in Icicle mode.
2. Just binding `icicle-default-value' to nil should have
done the trick. You can see that the example I gave works.
But in fact there was a bug: `icicle-read-string' (unlike
`icicle-completing-read', which has similar treatment of the
prompt) hard-coded inserting the default value into the prompt.
I've fixed that now, so all you should need to do, to inhibit
default insertion, is bind `icicle-default-value' to nil -
nothing else. I thought that was the case already, but it was not.
3. The reason my example worked, even though automatic
inclusion of the default value was hardcoded, is this:
When the default value _is_ to be included, Icicles tries to
DTRT with prompts that might already hard-code the default
value in various ways, as well as with prompts that do not
show the default value. It recognizes certain patterns of
default-value inclusion, and handles them properly.
4. To understand how it does this, and how you can adapt
it to other prompt patterns you might encounter, see function
`icicle-handle-default-for-prompt', option
`icicle-default-in-prompt-format-function', and variable
`minibuffer-default-in-prompt-regexps' in standard library
`minibuf-eldef.el'.
From the doc string of `icicle-handle-default-for-prompt':
In the existing PROMPT before modification, recognizes
inclusion of a default value according to these possible
patterns:
`minibuffer-default-in-prompt-regexps'
"(default ___):"
"(default is ___):"
" [___] "
That is the default behavior for recognizing defaults in
prompts. My example fit that (it passed "(default DEF):"),
and yours did not, hence the difference in behavior.
To modify the default recognition of default-in-prompt patterns,
you can customize `minibuffer-default-in-prompt-regexps' if you
use library `minibuf-eldef.el' (good).
A prompt matching one of the recognized patterns gets its
default removed, first. And then, if argument INCLUDE to
`icicle-handle-default-for-prompt' is non-nil (which it was
in the bugged `icicle-read-string'), the default value is
added back to the prompt using format "(___): ". So regardless
of the input format of a prompt (provided it is recognized),
the resulting prompt has the same, common format. Icicles
always adds the default value in the same way (format).
You can control this resulting format by customizing option
`icicle-default-in-prompt-format-function', whose default
value is (lambda (default) (format " (%s)" default)). The
function, whatever it is, places the formatted default
value just before the `:' in the prompt.
Again, the default value is inserted (using this function)
only if `icicle-default-value' is non-nil. (That was the
bug - `icicle-default-value' was being ignored for
`icicle-read-string'.)
Note that even with the bug, if you had known about this
option you could have made your example work by customizing
the option to make it a no-op (no insertion):
(lambda (def) "").
Dunno whether all of this explanation makes things clearer,
but in any case the `icicle-read-string' bug that hardcoded
adding the default to the prompt should be fixed now, which
will make your example work. And all you should need to do,
to inhibit insertion of the default, is to bind (or customize)
`icicle-default-value' to nil.
Sorry for the trouble.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-08-21 7:46 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-03 21:00 How to handle default value in read-string? Marcin Borkowski
2015-08-03 21:17 ` John Mastro
2015-08-03 23:40 ` Marcin Borkowski
2015-08-03 23:51 ` Drew Adams
2015-08-04 18:44 ` Marcin Borkowski
2015-08-19 21:15 ` Marcin Borkowski
2015-08-21 7:46 ` Drew Adams
2015-08-05 20:02 ` Emanuel Berg
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).