* default value with interactive
@ 2017-11-19 19:08 Emanuel Berg
2017-11-19 21:49 ` Michael Heerdegen
0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2017-11-19 19:08 UTC (permalink / raw
To: help-gnu-emacs
I'm preparing one of my packs for MELPA so I go
thru the list of formal style
suggestions [1] ...
One I don't know how to do is when there is
a default value. The info node that is
referenced in the suggestion list, namely
(info "(elisp) Programming Tips")
says the prompt string should look like this
Enter the answer (default 42):
However then it says it should be implemented
like this
(defun foo (pos)
(interactive
(list (if SPECIFIED SPECIFIED-POS)))
(unless pos (setq pos DEFAULT-POS))
...)
Note that DEFAULT-POS in this example does not
appear in the `interactive' form! So how does
it appear in the prompt string at all?
And even if it did, how do you then get around
it without having to compute/fetch DEFAULT-POS
two times? One time for the prompt string, and
then again to set pos (in the abstract example
above)?
Is there a real-code example somewhere when
this is done the right way?
[1] https://github.com/melpa/melpa/blob/master/CONTRIBUTING.md
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-19 19:08 default value with interactive Emanuel Berg
@ 2017-11-19 21:49 ` Michael Heerdegen
2017-11-19 22:54 ` Emanuel Berg
0 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2017-11-19 21:49 UTC (permalink / raw
To: help-gnu-emacs
Emanuel Berg <moasen@zoho.com> writes:
> I'm preparing one of my packs for MELPA so I go
> thru the list of formal style
> suggestions [1] ...
>
> One I don't know how to do is when there is
> a default value. The info node that is
> referenced in the suggestion list, namely
>
> (info "(elisp) Programming Tips")
>
> says the prompt string should look like this
>
> Enter the answer (default 42):
>
> However then it says it should be implemented
> like this
>
> (defun foo (pos)
> (interactive
> (list (if SPECIFIED SPECIFIED-POS)))
> (unless pos (setq pos DEFAULT-POS))
> ...)
>
> Note that DEFAULT-POS in this example does not
> appear in the `interactive' form! So how does
> it appear in the prompt string at all?
>
> And even if it did, how do you then get around
> it without having to compute/fetch DEFAULT-POS
> two times? One time for the prompt string, and
> then again to set pos (in the abstract example
> above)?
>
> Is there a real-code example somewhere when
> this is done the right way?
It depends on you case. How does it look like? I think there is no
general answer.
In most situations, the prompt will rather look like
"Position (default: point)"
instead of
"Position (default 15327)".
In other situations, it is not really a problem to let `interactive'
return the default value, because when you repeat the command, you want
to specify the argument anyway, and a reasonable "default" is not
well-defined when repeating.
In some cases, maybe your case is such a case, it is better to read the
arguments in the body of the function and test with
`called-interactively-p' whether that is necessary.
Michael.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-19 21:49 ` Michael Heerdegen
@ 2017-11-19 22:54 ` Emanuel Berg
2017-11-20 0:05 ` Michael Heerdegen
0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2017-11-19 22:54 UTC (permalink / raw
To: help-gnu-emacs
Michael Heerdegen wrote:
> In most situations, the prompt will rather
> look like
>
> "Position (default: point)"
>
> instead of
>
> "Position (default 15327)".
OK, so that is OK?
Problem "solved" :)
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-19 22:54 ` Emanuel Berg
@ 2017-11-20 0:05 ` Michael Heerdegen
2017-11-20 1:10 ` Emanuel Berg
0 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2017-11-20 0:05 UTC (permalink / raw
To: help-gnu-emacs
Emanuel Berg <moasen@zoho.com> writes:
> Michael Heerdegen wrote:
>
> > In most situations, the prompt will rather
> > look like
> >
> > "Position (default: point)"
> >
> > instead of
> >
> > "Position (default 15327)".
>
> OK, so that is OK?
Sure - most of the time you are not aware of the value of point as a
number, so the first version is much clearer.
Michael.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-20 0:05 ` Michael Heerdegen
@ 2017-11-20 1:10 ` Emanuel Berg
2017-11-20 2:18 ` Michael Heerdegen
0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2017-11-20 1:10 UTC (permalink / raw
To: help-gnu-emacs
Michael Heerdegen wrote:
>> OK, so that is OK?
>
> Sure - most of the time you are not aware of
> the value of point as a number, so the first
> version is much clearer.
Yeah, in this case, I meant in principle.
What I have is two functions, one that
searches, and one that "searches again" with
the previous search string as input.
When the function that searches is invoked, it
asks for a search string, but if it isn't
provided, the default (i.e., previous) string
is used even tho the better way to do that is
to invoke the function that searches
again directly.
I can extract into a function the code that
fetches the previous search *but* how do I use
that in the interactive form as well as outside
it without computing twice? As was instructed
here - (info "(elisp) Programming Tips") - only
even in the provided example, it really didn't
happen that way at all.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-20 1:10 ` Emanuel Berg
@ 2017-11-20 2:18 ` Michael Heerdegen
2017-11-20 3:10 ` Emanuel Berg
0 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2017-11-20 2:18 UTC (permalink / raw
To: help-gnu-emacs
Emanuel Berg <moasen@zoho.com> writes:
> What I have is two functions, one that
> searches, and one that "searches again" with
> the previous search string as input.
>
> When the function that searches is invoked, it
> asks for a search string, but if it isn't
> provided, the default (i.e., previous) string
> is used even tho the better way to do that is
> to invoke the function that searches
> again directly.
>
> I can extract into a function the code that
> fetches the previous search *but* how do I use
> that in the interactive form as well as outside
> it without computing twice? As was instructed
> here - (info "(elisp) Programming Tips") - only
> even in the provided example, it really didn't
> happen that way at all.
You are trying to solve a problem that doesn't exist. The convention
does only speak about "region or position arguments" - right? Maybe,
more generally, about arguments that the user specifies implicitly, like
having marked a region, having a certain position of point, positioning
the cursor over a certain url, or so. Implicitly, because you don't
really enter the bounds of the region etc. when using a command that
operates on the region.
The rule you want to fulfill exists to make `repeat-complex-command'
behave convenient. When you e.g. have a command `foo' that operates on
the current region (let's say, it query-replaces something in the
current region), `foo' will have arguments BEG, END, and a REGEXP etc.
The interactive form should return BEG and END as nil so that when I
have invoked the command, but want to redo it with a different region
(and just reuse the given input), I can just change the region and
invoke `repeat-complex-command' - without having to paste the actual
region bounds as numbers into the minibuffer. I just can confirm the
default, because the command function had been invoked with BEG and END
nil, and that means "use the current region".
In your case, it makes no sense to use the "search again" command with
`repeat-complex-command' - when I want a new search, I should
`repeat-complex-command' the original search command (or just call it
itself), and the other one is already a redo command.
But even if you unite the two commands, it is still _useful_ to have the
last search pattern in the minibuffer to be able to reuse or edit it
(it's easy enough to replace it with something else) when using
`repeat-complex-command'.
It takes a bit of routine to learn how to design commands so that they
work well with `repeat-complex-command', but it's only sanity and reason
at the end.
Regards,
Michael.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-20 2:18 ` Michael Heerdegen
@ 2017-11-20 3:10 ` Emanuel Berg
2017-11-21 0:31 ` Robert Thorpe
0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2017-11-20 3:10 UTC (permalink / raw
To: help-gnu-emacs
Michael Heerdegen wrote:
> You are trying to solve a problem that
> doesn't exist. The convention does only speak
> about "region or position arguments" - right?
Here is what it says
When you mention a default value in
a minibuffer prompt, put it and the word
‘default’ inside parentheses. It should look
like this:
Enter the answer (default 42):
Reading it very carefully, it doesn't say the
default value *has* to be mentioned at all - it
says "when" it is mentioned, it should be put
that way. So I think I won't mention it at all.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-20 3:10 ` Emanuel Berg
@ 2017-11-21 0:31 ` Robert Thorpe
2017-11-21 2:00 ` Emanuel Berg
0 siblings, 1 reply; 9+ messages in thread
From: Robert Thorpe @ 2017-11-21 0:31 UTC (permalink / raw
To: Emanuel Berg; +Cc: help-gnu-emacs
Emanuel Berg <moasen@zoho.com> writes:
> Reading it very carefully, it doesn't say the
> default value *has* to be mentioned at all - it
> says "when" it is mentioned, it should be put
> that way. So I think I won't mention it at all.
I think you're right to do that.
If you ever want to put in the concrete value, then you can. This is
done by the help functions such as 'describe-function'. If you put
point on the words describe-function then press C-h f it will prompt you
with that function as the default. If you look at the source you can
see how it's done.
BR,
Robert Thorpe
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: default value with interactive
2017-11-21 0:31 ` Robert Thorpe
@ 2017-11-21 2:00 ` Emanuel Berg
0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2017-11-21 2:00 UTC (permalink / raw
To: help-gnu-emacs
Robert Thorpe wrote:
> If you ever want to put in the concrete
> value, then you can. This is done by the help
> functions such as 'describe-function'. If you
> put point on the words describe-function then
> press C-h f it will prompt you with that
> function as the default. If you look at the
> source you can see how it's done.
Not really what I can see (?) -
`describe-function' does it with the obarray
stunt and the default value is computed with
`function-called-at-point' well inside the
`interactive' form - so if you want that again
in the function body (i.e., outside of
`interactive') the only way at least that I'm
aware of is to compute it again.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2017-11-21 2:00 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-19 19:08 default value with interactive Emanuel Berg
2017-11-19 21:49 ` Michael Heerdegen
2017-11-19 22:54 ` Emanuel Berg
2017-11-20 0:05 ` Michael Heerdegen
2017-11-20 1:10 ` Emanuel Berg
2017-11-20 2:18 ` Michael Heerdegen
2017-11-20 3:10 ` Emanuel Berg
2017-11-21 0:31 ` Robert Thorpe
2017-11-21 2:00 ` Emanuel Berg
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.