all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Getting last-command with arguments
@ 2013-09-13 10:06 Suvayu Ali
  2013-09-13 11:34 ` Nicolas Richard
  0 siblings, 1 reply; 8+ messages in thread
From: Suvayu Ali @ 2013-09-13 10:06 UTC (permalink / raw)
  To: Emacs help

Hi,

I am conditionally repeating the last command from inside an elisp
function.  So I look at `last-command' and decide.  But now I want to
see the original arguments: say the command was, M-2 M-x my-command RET,
then I want to know 2 was passed to `my-command'.  I am not sure how I
can do that.

I looked in (info "(elisp) Command Loop Info"), unless I missed
something I didn't find anything about retrieving arguments.

Any ideas?

-- 
Suvayu

Open source is the future. It sets us free.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Getting last-command with arguments
  2013-09-13 10:06 Getting last-command with arguments Suvayu Ali
@ 2013-09-13 11:34 ` Nicolas Richard
  2013-09-13 12:42   ` Stefan Monnier
  2013-09-13 13:10   ` Suvayu Ali
  0 siblings, 2 replies; 8+ messages in thread
From: Nicolas Richard @ 2013-09-13 11:34 UTC (permalink / raw)
  To: Suvayu Ali; +Cc: Emacs help

Suvayu Ali <fatkasuvayu+linux@gmail.com> writes:
> I am conditionally repeating the last command from inside an elisp
> function.  So I look at `last-command' and decide.  But now I want to
> see the original arguments: say the command was, M-2 M-x my-command RET,
> then I want to know 2 was passed to `my-command'.  I am not sure how I
> can do that.

The variable current-prefix-arg has that information.

hth,

-- 
Nicolas.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Getting last-command with arguments
  2013-09-13 11:34 ` Nicolas Richard
@ 2013-09-13 12:42   ` Stefan Monnier
  2013-09-13 13:33     ` Suvayu Ali
  2013-09-13 13:10   ` Suvayu Ali
  1 sibling, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2013-09-13 12:42 UTC (permalink / raw)
  To: help-gnu-emacs

>> I am conditionally repeating the last command from inside an elisp
>> function.  So I look at `last-command' and decide.  But now I want to
>> see the original arguments: say the command was, M-2 M-x my-command RET,
>> then I want to know 2 was passed to `my-command'.  I am not sure how I
>> can do that.
> The variable current-prefix-arg has that information.

Since we're talking about my-command rather than some random command,
you can simply change my-command to stash its argument in, say,
my-command--last-args.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Getting last-command with arguments
  2013-09-13 11:34 ` Nicolas Richard
  2013-09-13 12:42   ` Stefan Monnier
@ 2013-09-13 13:10   ` Suvayu Ali
  2013-09-13 13:58     ` Nicolas Richard
  1 sibling, 1 reply; 8+ messages in thread
From: Suvayu Ali @ 2013-09-13 13:10 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: Emacs help

Hi Nicolas,

On Fri, Sep 13, 2013 at 01:34:04PM +0200, Nicolas Richard wrote:
> Suvayu Ali <fatkasuvayu+linux@gmail.com> writes:
> > I am conditionally repeating the last command from inside an elisp
> > function.  So I look at `last-command' and decide.  But now I want to
> > see the original arguments: say the command was, M-2 M-x my-command RET,
> > then I want to know 2 was passed to `my-command'.  I am not sure how I
> > can do that.
> 
> The variable current-prefix-arg has that information.

Thanks a lot.  But did you mean last-prefix-arg instead?  I want the
prefix arg that was passed to last-command.  After your hint, I read
(info "(elisp) Prefix Command Arguments").

This is pseudo-code for what I'm trying to do:

(defun my-cmd-1 (&optional args) nil)

(defun my-cmd-2 (&optional args) 
  (if (eq last-command 'my-cmd-1)
      (progn
	(setq this-command 'my-cmd-1)
	(my-cmd-1 last-prefix-arg)
	(setq prefix-arg last-prefix-arg))  ;; <-- this bit doesn't seem to work
    (some-command arg)))

Then I call my-cmd-1, now if I call my-cmd-2 right after, it repeats
my-cmd-1.  If I do something else in between, the my-cmd-2 just calls
some-command.

I hope my intentions are clearer now.

Cheers,

-- 
Suvayu

Open source is the future. It sets us free.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Getting last-command with arguments
  2013-09-13 12:42   ` Stefan Monnier
@ 2013-09-13 13:33     ` Suvayu Ali
  0 siblings, 0 replies; 8+ messages in thread
From: Suvayu Ali @ 2013-09-13 13:33 UTC (permalink / raw)
  To: help-gnu-emacs

On Fri, Sep 13, 2013 at 08:42:37AM -0400, Stefan Monnier wrote:
> >> I am conditionally repeating the last command from inside an elisp
> >> function.  So I look at `last-command' and decide.  But now I want to
> >> see the original arguments: say the command was, M-2 M-x my-command RET,
> >> then I want to know 2 was passed to `my-command'.  I am not sure how I
> >> can do that.
> > The variable current-prefix-arg has that information.
> 
> Since we're talking about my-command rather than some random command,
> you can simply change my-command to stash its argument in, say,
> my-command--last-args.

Ah!  Did not think about that.  Thank you.

-- 
Suvayu

Open source is the future. It sets us free.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Getting last-command with arguments
  2013-09-13 13:10   ` Suvayu Ali
@ 2013-09-13 13:58     ` Nicolas Richard
  2013-09-14  0:12       ` Xue Fuqiao
  0 siblings, 1 reply; 8+ messages in thread
From: Nicolas Richard @ 2013-09-13 13:58 UTC (permalink / raw)
  To: Suvayu Ali; +Cc: Emacs help

Suvayu Ali <fatkasuvayu+linux@gmail.com> writes:

> Hi Nicolas,
>
> On Fri, Sep 13, 2013 at 01:34:04PM +0200, Nicolas Richard wrote:
>> Suvayu Ali <fatkasuvayu+linux@gmail.com> writes:
>> > I am conditionally repeating the last command from inside an elisp
>> > function.  So I look at `last-command' and decide.  But now I want to
>> > see the original arguments: say the command was, M-2 M-x my-command RET,
>> > then I want to know 2 was passed to `my-command'.  I am not sure how I
>> > can do that.
>> 
>> The variable current-prefix-arg has that information.
>
> Thanks a lot.  But did you mean last-prefix-arg instead?

No, but I had misread last-command for this-command. Sorry about that.
last-prefix-arg sounds like a good candidate from its docstring, but I
don't understand how it works:

(defun yf/foo (arg)
  (interactive "i")
  (message "Last: %s : %s\nThis: %s : %s"
           last-command last-prefix-arg 
           this-command current-prefix-arg))
(local-set-key (kbd "<f5>") #'yf/foo)

Hitting RET RET C-u <f5> in a buffer shows me:
Last: newline : (4)
This: yf/foo : (4)

I expected

Last: newline : nil
This: yf/foo : (4)

would someone have an explanation ?

-- 
Nico.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Getting last-command with arguments
  2013-09-13 13:58     ` Nicolas Richard
@ 2013-09-14  0:12       ` Xue Fuqiao
  2013-09-16 13:34         ` Nicolas Richard
  0 siblings, 1 reply; 8+ messages in thread
From: Xue Fuqiao @ 2013-09-14  0:12 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: Emacs help

On Fri, Sep 13, 2013 at 9:58 PM, Nicolas Richard
<theonewiththeevillook@yahoo.fr> wrote:
> last-prefix-arg sounds like a good candidate from its docstring, but I
> don't understand how it works:
>
> (defun yf/foo (arg)
>   (interactive "i")
>   (message "Last: %s : %s\nThis: %s : %s"
>            last-command last-prefix-arg
>            this-command current-prefix-arg))
> (local-set-key (kbd "<f5>") #'yf/foo)
>
> Hitting RET RET C-u <f5> in a buffer shows me:
> Last: newline : (4)
> This: yf/foo : (4)
>
> I expected
>
> Last: newline : nil
> This: yf/foo : (4)

I just tried it with r114269 and it worked as you expected.

-- 
Best regards, Xue Fuqiao.
http://www.gnu.org/software/emacs/



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Getting last-command with arguments
  2013-09-14  0:12       ` Xue Fuqiao
@ 2013-09-16 13:34         ` Nicolas Richard
  0 siblings, 0 replies; 8+ messages in thread
From: Nicolas Richard @ 2013-09-16 13:34 UTC (permalink / raw)
  To: Xue Fuqiao; +Cc: Emacs help

Xue Fuqiao <xfq.free@gmail.com> writes:
> On Fri, Sep 13, 2013 at 9:58 PM, Nicolas Richard
>> Hitting RET RET C-u <f5> in a buffer shows me:
>> Last: newline : (4)
>> This: yf/foo : (4)
>>
>> I expected
>>
>> Last: newline : nil
>> This: yf/foo : (4)
>
> I just tried it with r114269 and it worked as you expected.

Thanks for your input. I tried from the tip of the emacs-24 branch and
the result is the unexpected one, but trying from trunk indeed gives the
expected result.

-- 
Nicolas.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-09-16 13:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-13 10:06 Getting last-command with arguments Suvayu Ali
2013-09-13 11:34 ` Nicolas Richard
2013-09-13 12:42   ` Stefan Monnier
2013-09-13 13:33     ` Suvayu Ali
2013-09-13 13:10   ` Suvayu Ali
2013-09-13 13:58     ` Nicolas Richard
2013-09-14  0:12       ` Xue Fuqiao
2013-09-16 13:34         ` Nicolas Richard

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.