all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument
@ 2018-10-21  4:18 Van L
  2018-10-21 12:26 ` Eli Zaretskii
  2018-10-21 20:33 ` Michael Heerdegen
  0 siblings, 2 replies; 6+ messages in thread
From: Van L @ 2018-10-21  4:18 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I am unable to have the following work.

The elisp documentation has for me the problem of tl;dr and after experimenting with a few paragraphs and failing I want to ask how to get this done. I wonder sometimes if the documentation is obtuse by design to steal time.

┌────
│ (defun x-setface-height-i (number)
│   "Face height is set to NUMBER."
│   (interactive "nInsert number: ")
│   (set-face-attribute 'default (selected-frame) :height number))
│ 
│ ;; (info "(elisp) Interactive Call")
│ (global-set-key (kbd "C-c o") (funcall-interactively x-setface-height-i 202))
│ (global-set-key (kbd "C-c O") (funcall-interactively x-setface-height-i 256))
└────




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

* Re: 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument
  2018-10-21  4:18 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument Van L
@ 2018-10-21 12:26 ` Eli Zaretskii
  2018-10-21 20:33 ` Michael Heerdegen
  1 sibling, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2018-10-21 12:26 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Van L <van@scratch.space>
> Date: Sun, 21 Oct 2018 15:18:39 +1100
> 
> I am unable to have the following work.

The problem is your global-set-key forms: the general form is

  (global-set-key KEY COMMAND)

where COMMAND is a _symbol_, like this:

  (global-set-key (kbd "C-c o") 'foo)

So you need to define an interactive function named SOMETHING:

  (defun SOMETHING (...)
    (interactive)
    ...)

then bind it to a key:

  (global-set-key (kbd "C-c o") 'SOMETHING)

> The elisp documentation has for me the problem of tl;dr and after experimenting with a few paragraphs and failing I want to ask how to get this done. I wonder sometimes if the documentation is obtuse by design to steal time.

Please tell which part of the documentation confused you.  The doc
string of global-set-key says:

  COMMAND is the command definition to use; usually it is
  a symbol naming an interactively-callable function.

IOW, it usually should be a symbol, whereas in your example it is a
list that calls a function.



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

* Re: 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument
  2018-10-21  4:18 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument Van L
  2018-10-21 12:26 ` Eli Zaretskii
@ 2018-10-21 20:33 ` Michael Heerdegen
  2018-10-21 23:00   ` Van L
  1 sibling, 1 reply; 6+ messages in thread
From: Michael Heerdegen @ 2018-10-21 20:33 UTC (permalink / raw)
  To: Van L; +Cc: help-gnu-emacs

Van L <van@scratch.space> writes:

> │ (global-set-key (kbd "C-c o") (funcall-interactively
> x-setface-height-i 202))
> │ (global-set-key (kbd "C-c O") (funcall-interactively
> x-setface-height-i 256))
> └────

In addition to what Eli said: How could that even work?
`global-set-key' is a function, so your `funcall-interactively' is
evaluated already when the `global-set-key' expression is evaluated.

Besides specifying a quoted function name at the COMMAND position in the
`global-set-key' call, you can write there any expression that evaluates
to an (fbound) symbol, which includes a `defun'.  That means you can
write

  (global-set-key KEY (defun name ...))

instead of using two expressions

  (defun name ...)
  (global-set-key KEY 'name)

It's also possible to specify a function object (an anonymous function)
as COMMAND argument:

  (global-set-key KEY (lambda () (interactive) CODE...)

This has downsides, however: if you ask Emacs for help about KEY, like
C-h k KEY, the information you get is less useful, in particular, you
get no link to the source code, so it's harder to localize the
definition in your files if you ever want to change KEY's binding.

If you want to specify an anonymous command, you can define a helper
macro that wraps the (lambda () (interactive) ...) around CODE.  If your
macro would be named "command", you could then write shorter

  (global-set-key KEY (command CODE...))

if this is your goal.


Michael.



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

* Re: 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument
  2018-10-21 20:33 ` Michael Heerdegen
@ 2018-10-21 23:00   ` Van L
  2018-10-22  6:25     ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: Van L @ 2018-10-21 23:00 UTC (permalink / raw)
  To: help-gnu-emacs

Thank you so much Eli, Michael.

┌────
│ the general form is
│ 
│  (global-set-key KEY COMMAND)
│ 
│ where COMMAND is a _symbol_, like this:
│ 
│  (global-set-key (kbd "C-c o") 'foo)
│ 
│ So you need to define an interactive function named SOMETHING:
│ 
│  (defun SOMETHING (...)
│    (interactive)
│    ...)
│ 
│ then bind it to a key:
│ 
│  (global-set-key (kbd "C-c o") 'SOMETHING)
└────

┌────
│  1  (defun set-face-height (number)
│  2    "Face height is set to NUMBER."
│  3    (interactive "nInsert number: ")
│  4    (set-face-attribute 'default (selected-frame) :height number))
│  5  
│  6  (defun set-face-height-202 ()
│  7    "Set height of face to 202"
│  8    (interactive)
│  9    (set-face-attribute 'default (selected-frame) :height 202))
│ 10  (global-set-key (kbd "C-c o") 'set-face-height-202)
│ 11  
│ 12  (defun set-face-height-256 ()
│ 13    "Set height of face to 256"
│ 14    (interactive)
│ 15    (set-face-attribute 'default (selected-frame) :height 256))
│ 16  (global-set-key (kbd "C-c O") 'set-face-height-256)
└────

At the outset my goal was to get rid of lines 6 to 9, 12 to 15.

To do that is to call a function with an argument in the place of
'set-face-height-XXX.

I hit the documentation as follows:

  1) (info "(elisp) Calling Functions")
     - experiment with funcall and fail
  2) (info "(elisp) Interactive Call")
     - experiment with funcall-interactively and fail

Along the way I came across two mentions of what a COMMAND is. 

The errors I got mentioned “commandp”.

I didn't lookup global-set-key (lazily) believing I needed a list decorated the right-way to slot in the
context given by 'SOMETHING, which I understand to be a quote or
'(quoted list).

  COMMAND is the command definition to use; usually it is
  a symbol naming an interactively-callable function.

  [ I am going to read that as follows: In use the COMMAND is usually
    a symbol naming an interactive function which is called. ]

>> IOW, it usually should be a symbol,
>> whereas in your example it is a
>> list that calls a function.

The guiding lamp I take to Lisp is it is a Russian Doll of lists.  

If I plug a list to that-list-context-right it will work. 

Obviously it is more complicated than that. 

The COMMAND has to be an "expression that evals to (fbound) symbol” and I’d like a function with an argument to be that.

That will by explained going from

(info "(elisp) Symbols”)

Thank you.




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

* Re: 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument
  2018-10-21 23:00   ` Van L
@ 2018-10-22  6:25     ` Eli Zaretskii
  2018-10-22 21:55       ` Van L
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2018-10-22  6:25 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Van L <van@scratch.space>
> Date: Mon, 22 Oct 2018 10:00:27 +1100
> 
> ┌────
> │  1  (defun set-face-height (number)
> │  2    "Face height is set to NUMBER."
> │  3    (interactive "nInsert number: ")
> │  4    (set-face-attribute 'default (selected-frame) :height number))
> │  5  
> │  6  (defun set-face-height-202 ()
> │  7    "Set height of face to 202"
> │  8    (interactive)
> │  9    (set-face-attribute 'default (selected-frame) :height 202))
> │ 10  (global-set-key (kbd "C-c o") 'set-face-height-202)
> │ 11  
> │ 12  (defun set-face-height-256 ()
> │ 13    "Set height of face to 256"
> │ 14    (interactive)
> │ 15    (set-face-attribute 'default (selected-frame) :height 256))
> │ 16  (global-set-key (kbd "C-c O") 'set-face-height-256)
> └────
> 
> At the outset my goal was to get rid of lines 6 to 9, 12 to 15.

Why would you want that?  Copy/paste is cheap, much cheaper than your
time (assuming you need to do something other than play with Lisp
forms).  It's possible, but it's advanced stuff, so beginners are well
advised to steer away from that.

The "advanced" way of doing what you wanted was explained by Michael:
use a lambda-function that calls set-face-height with the appropriate
argument.  (Actually, I'd do away with set-face-height entiely, and
call set-face-attribute directly.)

>   1) (info "(elisp) Calling Functions")
>      - experiment with funcall and fail

funcall needs an argument that is a symbol or one whose value is a
symbol.  There's a telltale example in the doc string.

>   2) (info "(elisp) Interactive Call")
>      - experiment with funcall-interactively and fail

Ditto.

> The guiding lamp I take to Lisp is it is a Russian Doll of lists.  

Yes, but there's a difference between a normal list and a quoted one:
the latter doesn't get evaluated right away.



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

* Re: 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument
  2018-10-22  6:25     ` Eli Zaretskii
@ 2018-10-22 21:55       ` Van L
  0 siblings, 0 replies; 6+ messages in thread
From: Van L @ 2018-10-22 21:55 UTC (permalink / raw)
  To: help-gnu-emacs


>> At the outset my goal was to get rid of lines 6 to 9, 12 to 15.
> 
> Why would you want that?

I guess in the jargon here 
I was trying to refactor 
just a very little bit.

>  Copy/paste is cheap, much cheaper than your
> time (assuming you need to do something other than play with Lisp
> forms).  It's possible, but it's advanced stuff, so beginners are well
> advised to steer away from that.
> 
> The "advanced" way of doing what you wanted was explained by Michael:
> use a lambda-function

I will that figure out one day. Probably not today or tomorrow.
The word lambda isn’t an everyday, day-to-day word in my voabc.

My ultimate goal is to figure out how to write a lisp program, 
to write a lisp program, which writes a lisp program. 
On its own.


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

end of thread, other threads:[~2018-10-22 21:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-21  4:18 26.1 emacs-mac 7.2; map key to interactive lisp function (command) with an argument Van L
2018-10-21 12:26 ` Eli Zaretskii
2018-10-21 20:33 ` Michael Heerdegen
2018-10-21 23:00   ` Van L
2018-10-22  6:25     ` Eli Zaretskii
2018-10-22 21:55       ` Van L

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.