all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* changing a variable with a keystroke
@ 2009-01-14 11:32 Joff
  2009-01-14 12:14 ` Juanma Barranquero
  0 siblings, 1 reply; 6+ messages in thread
From: Joff @ 2009-01-14 11:32 UTC (permalink / raw
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1677 bytes --]

Dear all,

 I'm having trouble trying to create a key combination to set a global
variable. I have tried putting the following in my .emacs file:

(global-set-key (kbd "C-l") nil)
(global-set-key (kbd "C-l C-r") '(setq dired-listing-switches "-lR")

and various permutions of the '(setq .. "-lR") part (with a single quote in
front of the -lR, without the intial single quote etc. This got me various
errors:

(global-set-key (kbd "C-l C-r") (setq dired-listing-switches -lR))   -->
Symbol's value as variable is void: -lR

(global-set-key (kbd "C-l C-r") '(setq dired-listing-switches -lR))  -->
Wrong type argument: commandp, (setq dired-listing-switches -lR) on pressing
C-l C-r

(global-set-key (kbd "C-l C-r") (setq dired-listing-switches "-lR"))  -->
Printed -lR into my buffer when I pressed C-l C-r

and so on...

so then I tried

(defun set_recursive_dired () "Set dired mode to recursive view"
  (interactive "p")
  (setq dired-listing-switches "-lR"))

(global-set-key (kbd "C-l") nil)
(global-set-key (kbd "C-l C-r") 'set_recursive_dired)
because I thought setq might not be a command (?) which got me:

call-interactively: Wrong number of arguments: (lambda nil "Set dired mode
to recursive view" (interactive "p") (setq dired-listing-switches "-lR")), 1

as you can probably tell, I'm pretty new to lisp/elisp... I have tried
'reading the error messages' and have done a lot of googling, which has got
me this far (and which suggested the above '(defun...set_recursive_dired) )

so could anyone kindly shed some light on why the above don't work, and
perhaps suggest what I should be trying? Is it a syntax thing or am I
missing the point completely?

best,
Joff

[-- Attachment #2: Type: text/html, Size: 2313 bytes --]

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

* Re: changing a variable with a keystroke
  2009-01-14 11:32 changing a variable with a keystroke Joff
@ 2009-01-14 12:14 ` Juanma Barranquero
  0 siblings, 0 replies; 6+ messages in thread
From: Juanma Barranquero @ 2009-01-14 12:14 UTC (permalink / raw
  To: Joff; +Cc: help-gnu-emacs

On Wed, Jan 14, 2009 at 12:32, Joff <jack.joff@gmail.com> wrote:

> (defun set_recursive_dired () "Set dired mode to recursive view"
>   (interactive "p")
>   (setq dired-listing-switches "-lR"))
>
> (global-set-key (kbd "C-l") nil)
> (global-set-key (kbd "C-l C-r") 'set_recursive_dired)
>
> because I thought setq might not be a command (?)

`setq' is an elisp function (more properly, a "special form"; see the
Emacs Lisp Intro), but it is not an interactive command.  Functions
bound to keys must be interactive commands.

> which got me:
>
> call-interactively: Wrong number of arguments: (lambda nil "Set dired mode
> to recursive view" (interactive "p") (setq dired-listing-switches "-lR")), 1

You are using (interactive "p"), which says that the function has an
argument, but it has none. If you use just (interactive) it will work.
See the documentation for function `interactive'.

However, it's a bit weird that you need a keybinding just to set
switches for dired. Doesn't it work if you just add

  (setq dired-listing-switches "-lR")

to your .emacs file? Unless you don't always want these switches, of course.

    Juanma




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

* Re: changing a variable with a keystroke
       [not found] <mailman.4829.1231934186.26697.help-gnu-emacs@gnu.org>
@ 2009-01-14 22:05 ` Xah Lee
  2009-01-15  8:48   ` Joff
  0 siblings, 1 reply; 6+ messages in thread
From: Xah Lee @ 2009-01-14 22:05 UTC (permalink / raw
  To: help-gnu-emacs



On Jan 14, 3:32 am, Joff <jack.j...@gmail.com> wrote:
> Dear all,
>
>  I'm having trouble trying to create a key combination to set a global
> variable. I have tried putting the following in my .emacs file:
>
> (global-set-key (kbd "C-l") nil)
> (global-set-key (kbd "C-l C-r") '(setq dired-listing-switches "-lR")
>
> and various permutions of the '(setq .. "-lR") part (with a single quote in
> front of the -lR, without the intial single quote etc. This got me various
> errors:
>
> (global-set-key (kbd "C-l C-r") (setq dired-listing-switches -lR))   -->
> Symbol's value as variable is void: -lR
>
> (global-set-key (kbd "C-l C-r") '(setq dired-listing-switches -lR))  -->
> Wrong type argument: commandp, (setq dired-listing-switches -lR) on pressing
> C-l C-r
>
> (global-set-key (kbd "C-l C-r") (setq dired-listing-switches "-lR"))  -->
> Printed -lR into my buffer when I pressed C-l C-r
>
> and so on...

Here's what's wrong with your code. Using pseudo C-like code to
illustrate, what you want is:

setkey(keyCode, functionName)

but what you are doing is:

setkey(keyCode, diredswich = "lr")

So, your second argument is supposed to be a function, but you give it
a expression of what the function is supposed to do.

To fix, you can define your function, then put the function name as
the second arg to setkey. But since elisp has a function construct
(aka lambda), so you don't need to define it separately.

Here's the code:

(global-set-key (kbd "C-l C-r")
 (lambda () (setq dired-listing-switches "-lR"))
) ;; code not tested

-----------------

Note that what you are doing is strange. Depending what you want to
achieve, there are probably better ways.


---------------------


> so then I tried
>
> (defun set_recursive_dired () "Set dired mode to recursive view"
>   (interactive "p")
>   (setq dired-listing-switches "-lR"))
>
> (global-set-key (kbd "C-l") nil)
> (global-set-key (kbd "C-l C-r") 'set_recursive_dired)
> because I thought setq might not be a command (?) which got me:

if you want to define it separately, you can do it like this:

(defun set_recursive_dired ()
  "Set dired mode to recursive view"
  (setq dired-listing-switches "-lR"))

> as you can probably tell, I'm pretty new to lisp/elisp... I have tried
> 'reading the error messages' and have done a lot of googling, which has got
> me this far (and which suggested the above '(defun...set_recursive_dired) )
>
> so could anyone kindly shed some light on why the above don't work, and
> perhaps suggest what I should be trying? Is it a syntax thing or am I
> missing the point completely?

lisp syntax gets a bit used to, but partly also because it is
irregular, and inconsistant in its eval/not-eval expectation in its
various functions ...

my website has a elisp tutorial you might be interested.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: changing a variable with a keystroke
  2009-01-14 22:05 ` Xah Lee
@ 2009-01-15  8:48   ` Joff
  2009-01-15 18:51     ` Tassilo Horn
       [not found]     ` <mailman.4960.1232045493.26697.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 6+ messages in thread
From: Joff @ 2009-01-15  8:48 UTC (permalink / raw
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 6704 bytes --]

 On 1/14/09, Juanma Barranquero <lekktu@gmail.com> wrote:
>You are using (interactive "p"), which says that the function has an
>argument, but it has none. If you use just (interactive) it will work.
>See the documentation for function `interactive'.

This has solved it, thank you

 >However, it's a bit weird that you need a keybinding just to set
>switches for dired. Doesn't it work if you just add
>(setq dired-listing-switches "-lR")
>to your .emacs file? Unless you don't always want these switches, of
course.

I want to be able to turn it on and off at will. And also wanted to see if I
could make it work!

On Jan 14, 2009 10:05pm, Xah Lee <xahlee@gmail.com> wrote:
>
 On Jan 14, 3:32 am, Joff jack.j...@gmail.com> wrote:
> > (global-set-key (kbd "C-l C-r") '(setq dired-listing-switches -lR))  -->
> > Wrong type argument: commandp, (setq dired-listing-switches -lR) on
pressing
> > C-l C-r
>
>
> Here's what's wrong with your code. Using pseudo C-like code to
>
>
> illustrate, what you want is:
> setkey(keyCode, functionName)
>
> but what you are doing is:
> setkey(keyCode, diredswich = "lr")
>
> So, your second argument is supposed to be a function, but you give it
> a expression of what the function is supposed to do.
>
> To fix, you can define your function, then put the function name as
> the second arg to setkey. But since elisp has a function construct
> (aka lambda), so you don't need to define it separately.
> Here's the code:
> (global-set-key (kbd "C-l C-r")
>  (lambda () (setq dired-listing-switches "-lR"))
> ) ;; code not tested

This doesn't seem to work: it gives me:
Wrong type argument: commandp, (lambda nil (setq dired-listing-switches
"-lR"))

But I think I get what you are saying: I needed to define a function. Which
is why the "defun..." bit (almost) worked.


> Note that what you are doing is strange. Depending what you want to
> achieve, there are probably better ways.
>
What I want to be able to acheive is being able to switch to and from dired
recursive list 'mode' with a single key combination (rather than going
through M-x set-variable... etc or C-u s switch. Is there a better way to do
this? Secondly, my intention was to introduce myself to customising emacs
using elisp.

>
> > so then I tried
> > (defun set_recursive_dired () "Set dired mode to recursive view"
> >   (interactive "p")
> >   (setq dired-listing-switches "-lR"))
> > (global-set-key (kbd "C-l") nil)
> > (global-set-key (kbd "C-l C-r") 'set_recursive_dired)
> > because I thought setq might not be a command (?) which got me:
>
> if you want to define it separately, you can do it like this:
>
> (defun set_recursive_dired ()
>   "Set dired mode to recursive view"
>   (setq dired-listing-switches "-lR"))
>
without the 'interactive' line, I get this:
Wrong type argument: commandp, set_recursive_dired
(which is why I put it in in the first place)

>
>
> lisp syntax gets a bit used to, but partly also because it is
> irregular, and inconsistant in its eval/not-eval expectation in its
> various functions ...

> my website has a elisp tutorial you might be interested.

Thankyou, that looks useful.

For the record, what I eventually did was take the "p" out of (interactive
"p") which I did not realise meant that interactive takes an argument.
The following works for me (hooray!):

(defun set_recursive_dired ()
  "Set dired mode to recursive view"
  (interactive)
  (setq dired-listing-switches "-lR")
  (message "Switched to recursive dired view"))

(global-set-key (kbd "C-l") nil)
(global-set-key (kbd "C-l C-r") 'set_recursive_dired)

So thanks all of you who replied for your help! Elisp here I come....
Joff


On 1/14/09, Xah Lee <xahlee@gmail.com> wrote:
>
>
>
> On Jan 14, 3:32 am, Joff <jack.j...@gmail.com> wrote:
> > Dear all,
> >
> >  I'm having trouble trying to create a key combination to set a global
> > variable. I have tried putting the following in my .emacs file:
> >
> > (global-set-key (kbd "C-l") nil)
> > (global-set-key (kbd "C-l C-r") '(setq dired-listing-switches "-lR")
> >
> > and various permutions of the '(setq .. "-lR") part (with a single quote
> in
> > front of the -lR, without the intial single quote etc. This got me
> various
> > errors:
> >
> > (global-set-key (kbd "C-l C-r") (setq dired-listing-switches -lR))   -->
> > Symbol's value as variable is void: -lR
> >
> > (global-set-key (kbd "C-l C-r") '(setq dired-listing-switches -lR))  -->
> > Wrong type argument: commandp, (setq dired-listing-switches -lR) on
> pressing
> > C-l C-r
> >
> > (global-set-key (kbd "C-l C-r") (setq dired-listing-switches "-lR"))  -->
> > Printed -lR into my buffer when I pressed C-l C-r
> >
> > and so on...
>
> Here's what's wrong with your code. Using pseudo C-like code to
> illustrate, what you want is:
>
> setkey(keyCode, functionName)
>
> but what you are doing is:
>
> setkey(keyCode, diredswich = "lr")
>
> So, your second argument is supposed to be a function, but you give it
> a expression of what the function is supposed to do.
>
> To fix, you can define your function, then put the function name as
> the second arg to setkey. But since elisp has a function construct
> (aka lambda), so you don't need to define it separately.
>
> Here's the code:
>
> (global-set-key (kbd "C-l C-r")
> (lambda () (setq dired-listing-switches "-lR"))
> ) ;; code not tested
>
> -----------------
>
> Note that what you are doing is strange. Depending what you want to
> achieve, there are probably better ways.
>
>
> ---------------------
>
>
> > so then I tried
> >
> > (defun set_recursive_dired () "Set dired mode to recursive view"
> >   (interactive "p")
> >   (setq dired-listing-switches "-lR"))
> >
> > (global-set-key (kbd "C-l") nil)
> > (global-set-key (kbd "C-l C-r") 'set_recursive_dired)
> > because I thought setq might not be a command (?) which got me:
>
> if you want to define it separately, you can do it like this:
>
> (defun set_recursive_dired ()
> "Set dired mode to recursive view"
> (setq dired-listing-switches "-lR"))
>
> > as you can probably tell, I'm pretty new to lisp/elisp... I have tried
> > 'reading the error messages' and have done a lot of googling, which has
> got
> > me this far (and which suggested the above '(defun...set_recursive_dired)
> )
> >
> > so could anyone kindly shed some light on why the above don't work, and
> > perhaps suggest what I should be trying? Is it a syntax thing or am I
> > missing the point completely?
>
> lisp syntax gets a bit used to, but partly also because it is
> irregular, and inconsistant in its eval/not-eval expectation in its
> various functions ...
>
> my website has a elisp tutorial you might be interested.
>
> Xah
> ∑ http://xahlee.org/
>
> ☄
>
>

[-- Attachment #2: Type: text/html, Size: 8929 bytes --]

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

* Re: changing a variable with a keystroke
  2009-01-15  8:48   ` Joff
@ 2009-01-15 18:51     ` Tassilo Horn
       [not found]     ` <mailman.4960.1232045493.26697.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 6+ messages in thread
From: Tassilo Horn @ 2009-01-15 18:51 UTC (permalink / raw
  To: help-gnu-emacs

Joff <jack.joff@gmail.com> writes:

Hi!

>> To fix, you can define your function, then put the function name as
>> the second arg to setkey. But since elisp has a function construct
>> (aka lambda), so you don't need to define it separately.
>> Here's the code:
>> (global-set-key (kbd "C-l C-r")
>>  (lambda () (setq dired-listing-switches "-lR"))
>> ) ;; code not tested
>
> This doesn't seem to work: it gives me:
> Wrong type argument: commandp, (lambda nil (setq dired-listing-switches
> "-lR"))

Only commands (functions with `interactive' spec) can be bound to keys,
so simply putting (interactive) afted "lambda ()" should do the trick.

>> Note that what you are doing is strange. Depending what you want to
>> achieve, there are probably better ways.
>>
> What I want to be able to acheive is being able to switch to and from
> dired recursive list 'mode' with a single key combination (rather than
> going through M-x set-variable... etc or C-u s switch. Is there a
> better way to do this?

Dired can do that automatically.  See

      (info "(emacs)Subdirectories in Dired") <== C-x C-e here!

in the emacs manual.

Bye,
Tassilo
-- 
It is better to give than to receive. This is especially true of a Chuck
Norris roundhouse kick.





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

* Re: changing a variable with a keystroke
       [not found]     ` <mailman.4960.1232045493.26697.help-gnu-emacs@gnu.org>
@ 2009-01-16 11:19       ` Muurimäki Perttu
  0 siblings, 0 replies; 6+ messages in thread
From: Muurimäki Perttu @ 2009-01-16 11:19 UTC (permalink / raw
  To: help-gnu-emacs

Tassilo Horn <tassilo@member.fsf.org> writes:

> Dired can do that automatically.  See
>
>       (info "(emacs)Subdirectories in Dired") <== C-x C-e here!
>
> in the emacs manual.

Thank You for the '<==' tip! I've never realised before that doing C-x
C-e in Gnus article buffer gets me right to the referenced info
page. Very useful :)

-- 

Perttu Muurimäki
sähköposti: perttu.muurimaki@iki.fi


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

end of thread, other threads:[~2009-01-16 11:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-14 11:32 changing a variable with a keystroke Joff
2009-01-14 12:14 ` Juanma Barranquero
     [not found] <mailman.4829.1231934186.26697.help-gnu-emacs@gnu.org>
2009-01-14 22:05 ` Xah Lee
2009-01-15  8:48   ` Joff
2009-01-15 18:51     ` Tassilo Horn
     [not found]     ` <mailman.4960.1232045493.26697.help-gnu-emacs@gnu.org>
2009-01-16 11:19       ` Muurimäki Perttu

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.