* How to see that a variable holds t
@ 2010-01-03 17:11 Cecil Westerhof
2010-01-03 19:25 ` Andreas Politz
` (3 more replies)
0 siblings, 4 replies; 13+ messages in thread
From: Cecil Westerhof @ 2010-01-03 17:11 UTC (permalink / raw)
To: help-gnu-emacs
At the moment I have the following function:
(defun switch-gnus-idle-daemon-do-log ()
(interactive)
(setq gnus-idle-daemon-do-log
(case gnus-idle-daemon-do-log
(10 t)
(otherwise 10)))
(message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
I know that it looks like overkill a case for switching to two values,
but I expect that in the future there will be more values.
What I really would like is;
(defun switch-gnus-idle-daemon-do-log ()
(interactive)
(setq gnus-idle-daemon-do-log
(case gnus-idle-daemon-do-log
(t 10)
(otherwise t)))
(message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
Because I would like the default to be t and not 10. (For when the value
is not one of the defined values.) But when I do this, it is always set
to 10, because the case does not make a difference between t and 10. How
do I solve this?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-03 17:11 How to see that a variable holds t Cecil Westerhof
@ 2010-01-03 19:25 ` Andreas Politz
2010-01-03 20:32 ` Lennart Borgman
` (2 subsequent siblings)
3 siblings, 0 replies; 13+ messages in thread
From: Andreas Politz @ 2010-01-03 19:25 UTC (permalink / raw)
To: help-gnu-emacs
Cecil Westerhof <Cecil@decebal.nl> writes:
> At the moment I have the following function:
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> (10 t)
> (otherwise 10)))
> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>
> I know that it looks like overkill a case for switching to two values,
> but I expect that in the future there will be more values.
>
> What I really would like is;
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> (t 10)
> (otherwise t)))
> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>
> Because I would like the default to be t and not 10. (For when the value
> is not one of the defined values.) But when I do this, it is always set
> to 10, because the case does not make a difference between t and 10. How
> do I solve this?
I guess you need a different form/macro.
(defmacro case2 (expr &rest keylist)
(declare (indent 1))
(let ((true (make-symbol "true"))
(expr-var (make-symbol "expr-var")))
`(let ((,expr-var (eval ,expr)))
(when (eq ,expr-var t)
(setq ,expr-var ',true))
(case ,expr-var
,@(mapcar #'(lambda (kl)
(cond
((listp (car kl))
(cons (substitute true t (car kl))
(cdr kl)))
((eq (car kl) t)
(cons true (cdr kl)))
(t kl)))
keylist)))))
-ap
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-03 17:11 How to see that a variable holds t Cecil Westerhof
2010-01-03 19:25 ` Andreas Politz
@ 2010-01-03 20:32 ` Lennart Borgman
2010-01-03 21:46 ` Tim X
[not found] ` <87wrzysjty.fsf@hubble.informatimago.com>
3 siblings, 0 replies; 13+ messages in thread
From: Lennart Borgman @ 2010-01-03 20:32 UTC (permalink / raw)
To: Cecil Westerhof; +Cc: help-gnu-emacs
On Sun, Jan 3, 2010 at 6:11 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> At the moment I have the following function:
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> (10 t)
> (otherwise 10)))
> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>
> I know that it looks like overkill a case for switching to two values,
> but I expect that in the future there will be more values.
>
> What I really would like is;
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> (t 10)
> (otherwise t)))
> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>
> Because I would like the default to be t and not 10. (For when the value
> is not one of the defined values.) But when I do this, it is always set
> to 10, because the case does not make a difference between t and 10. How
> do I solve this?
Doesn't this work:
(let ((T t))
(case T
((eq T t) (message "was t!"))
(t (message "T=%S" T))))
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-03 17:11 How to see that a variable holds t Cecil Westerhof
2010-01-03 19:25 ` Andreas Politz
2010-01-03 20:32 ` Lennart Borgman
@ 2010-01-03 21:46 ` Tim X
[not found] ` <87wrzysjty.fsf@hubble.informatimago.com>
3 siblings, 0 replies; 13+ messages in thread
From: Tim X @ 2010-01-03 21:46 UTC (permalink / raw)
To: help-gnu-emacs
Cecil Westerhof <Cecil@decebal.nl> writes:
> At the moment I have the following function:
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> (10 t)
> (otherwise 10)))
> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>
> I know that it looks like overkill a case for switching to two values,
> but I expect that in the future there will be more values.
>
> What I really would like is;
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> (t 10)
> (otherwise t)))
> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>
> Because I would like the default to be t and not 10. (For when the value
> is not one of the defined values.) But when I do this, it is always set
> to 10, because the case does not make a difference between t and 10. How
> do I solve this?
You don't get a difference between t and 10 because all values except
nil are true. Therefore, your 'default' needs to be the last one i.e.
(otherwise t). As long as your other values come before that value, it
will match on those values. If you don't know what all the other
possible values will be, you need something more general. For example,
you could use cond as in
(cond ((< gnus-idle-daemon-do-log 10)
(setq gnus-idle-daemon-do-log 10))
((= gnus-idle-daemon-do-log 10)
(setq gnus-idle-daemon-do-log t))
(t
(setq gnus-idle-daemon-do-log t)))
the main thing to remember is that in boolean tests nil (and the empty
list) are the only 'false' values - anything else is true.
Tim
--
tcross (at) rapttech dot com dot au
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
[not found] ` <87wrzysjty.fsf@hubble.informatimago.com>
@ 2010-01-04 9:29 ` Cecil Westerhof
2010-01-04 16:44 ` Pascal J. Bourguignon
2010-01-06 13:29 ` Cecil Westerhof
1 sibling, 1 reply; 13+ messages in thread
From: Cecil Westerhof @ 2010-01-04 9:29 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
>> What I really would like is;
>> (defun switch-gnus-idle-daemon-do-log ()
>> (interactive)
>> (setq gnus-idle-daemon-do-log
>> (case gnus-idle-daemon-do-log
>> (t 10)
>> (otherwise t)))
>> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>>
>> Because I would like the default to be t and not 10. (For when the value
>> is not one of the defined values.) But when I do this, it is always set
>> to 10, because the case does not make a difference between t and 10. How
>> do I solve this?
>
> The first clause is always selected because in emacs lisp, t and
> otherwise are equivalent in case.
>
> I don't understand you people! How fucking difficult is it to type
> C-h f case RET and READ the documentation?
I had looked up case, but not in Emacs itself. In the definition I found
it was not mentioned that t is the same as otherwise.
> Write it as:
>
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> ((t) 10)
> (otherwise t)))
> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>
> (switch-gnus-idle-daemon-do-log)
> "gnus-idle-daemon-do-log: t"
> (switch-gnus-idle-daemon-do-log)
> "gnus-idle-daemon-do-log: 10"
> (switch-gnus-idle-daemon-do-log)
> "gnus-idle-daemon-do-log: t"
Works like a charm. Thanks.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-04 9:29 ` Cecil Westerhof
@ 2010-01-04 16:44 ` Pascal J. Bourguignon
2010-01-04 17:11 ` Cecil Westerhof
0 siblings, 1 reply; 13+ messages in thread
From: Pascal J. Bourguignon @ 2010-01-04 16:44 UTC (permalink / raw)
To: help-gnu-emacs
Cecil Westerhof <Cecil@decebal.nl> writes:
> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>
>>> What I really would like is;
>>> (defun switch-gnus-idle-daemon-do-log ()
>>> (interactive)
>>> (setq gnus-idle-daemon-do-log
>>> (case gnus-idle-daemon-do-log
>>> (t 10)
>>> (otherwise t)))
>>> (message "gnus-idle-daemon-do-log: %s" gnus-idle-daemon-do-log))
>>>
>>> Because I would like the default to be t and not 10. (For when the value
>>> is not one of the defined values.) But when I do this, it is always set
>>> to 10, because the case does not make a difference between t and 10. How
>>> do I solve this?
>>
>> The first clause is always selected because in emacs lisp, t and
>> otherwise are equivalent in case.
>>
>> I don't understand you people! How fucking difficult is it to type
>> C-h f case RET and READ the documentation?
>
> I had looked up case, but not in Emacs itself. In the definition I found
> it was not mentioned that t is the same as otherwise.
Yes, you have to be careful. In general operators that are common to
emacs lisp and Common Lisp are rather similar, but there are sometimes
some significant differences, and sometimes more subtle differences.
Always check with the doc. You could even bind space to automatically
find the doc for you:
(defun space-doc ()
(interactive)
(save-excursion
(backward-sexp) (backward-char)
(when (looking-at "(")
(forward-char)
(when (thing-at-point 'symbol)
(let* ((start (point))
(end (progn (forward-sexp) (point)))
(symname (buffer-substring start end)))
(ignore-errors (describe-function (intern symname)))))))
(insert " "))
(global-set-key (kbd "SPC") 'space-doc)
;; Or use local-set-key in mode hooks where you write emacs lisp code
;; such as emacs-lisp-mode-hook.
--
__Pascal Bourguignon__ http://www.informatimago.com/
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-04 16:44 ` Pascal J. Bourguignon
@ 2010-01-04 17:11 ` Cecil Westerhof
0 siblings, 0 replies; 13+ messages in thread
From: Cecil Westerhof @ 2010-01-04 17:11 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
> Cecil Westerhof <Cecil@decebal.nl> writes:
> Yes, you have to be careful. In general operators that are common to
> emacs lisp and Common Lisp are rather similar, but there are sometimes
> some significant differences, and sometimes more subtle differences.
Format is a good example.
> Always check with the doc.
I'll do this more.
> You could even bind space to automatically
> find the doc for you:
That is properly a good idea.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
[not found] ` <87wrzysjty.fsf@hubble.informatimago.com>
2010-01-04 9:29 ` Cecil Westerhof
@ 2010-01-06 13:29 ` Cecil Westerhof
2010-01-06 18:12 ` Stefan Monnier
2010-01-06 18:51 ` Pascal J. Bourguignon
1 sibling, 2 replies; 13+ messages in thread
From: Cecil Westerhof @ 2010-01-06 13:29 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
> (defun switch-gnus-idle-daemon-do-log ()
> (interactive)
> (setq gnus-idle-daemon-do-log
> (case gnus-idle-daemon-do-log
> ((t) 10)
> (otherwise t)))
> (message "gnus-idle-daemon-do-log: %s"
> gnus-idle-daemon-do-log))
As earlier said: it works, but I do not understand the difference
between:
((t) 10)
and:
(t 10)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-06 13:29 ` Cecil Westerhof
@ 2010-01-06 18:12 ` Stefan Monnier
2010-01-06 18:30 ` Cecil Westerhof
2010-01-06 18:51 ` Pascal J. Bourguignon
1 sibling, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2010-01-06 18:12 UTC (permalink / raw)
To: help-gnu-emacs
> As earlier said: it works, but I do not understand the difference
> between:
> ((t) 10)
> and:
> (t 10)
The first only matches when gnus-idle-daemon-do-log is equal to t,
whereas the second always matches (it's an "otherwise" clause).
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-06 18:12 ` Stefan Monnier
@ 2010-01-06 18:30 ` Cecil Westerhof
2010-01-06 20:09 ` Pascal J. Bourguignon
0 siblings, 1 reply; 13+ messages in thread
From: Cecil Westerhof @ 2010-01-06 18:30 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> As earlier said: it works, but I do not understand the difference
>> between:
>> ((t) 10)
>> and:
>> (t 10)
>
> The first only matches when gnus-idle-daemon-do-log is equal to t,
> whereas the second always matches (it's an "otherwise" clause).
I was not clear with my question. This I know, but why does the first
only matches when gnus-idle-daemon-do-log is equal to t?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-06 13:29 ` Cecil Westerhof
2010-01-06 18:12 ` Stefan Monnier
@ 2010-01-06 18:51 ` Pascal J. Bourguignon
2010-01-06 19:13 ` Cecil Westerhof
1 sibling, 1 reply; 13+ messages in thread
From: Pascal J. Bourguignon @ 2010-01-06 18:51 UTC (permalink / raw)
To: help-gnu-emacs
Cecil Westerhof <Cecil@decebal.nl> writes:
> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>
>> (defun switch-gnus-idle-daemon-do-log ()
>> (interactive)
>> (setq gnus-idle-daemon-do-log
>> (case gnus-idle-daemon-do-log
>> ((t) 10)
>> (otherwise t)))
>> (message "gnus-idle-daemon-do-log: %s"
>> gnus-idle-daemon-do-log))
>
> As earlier said: it works, but I do not understand the difference
> between:
> ((t) 10)
This is a clause that matches only t.
> and:
> (t 10)
This is the default clause. It matches anything.
(t) is a list of literal values specifying the case.
t is a symbol that is interpreted as meaning 'anything'.
--
__Pascal Bourguignon__ http://www.informatimago.com/
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-06 18:51 ` Pascal J. Bourguignon
@ 2010-01-06 19:13 ` Cecil Westerhof
0 siblings, 0 replies; 13+ messages in thread
From: Cecil Westerhof @ 2010-01-06 19:13 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
>> As earlier said: it works, but I do not understand the difference
>> between:
>> ((t) 10)
>
> This is a clause that matches only t.
>
>
>> and:
>> (t 10)
>
> This is the default clause. It matches anything.
>
> (t) is a list of literal values specifying the case.
> t is a symbol that is interpreted as meaning 'anything'.
Thanks, that makes it clear.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: How to see that a variable holds t
2010-01-06 18:30 ` Cecil Westerhof
@ 2010-01-06 20:09 ` Pascal J. Bourguignon
0 siblings, 0 replies; 13+ messages in thread
From: Pascal J. Bourguignon @ 2010-01-06 20:09 UTC (permalink / raw)
To: help-gnu-emacs
Cecil Westerhof <Cecil@decebal.nl> writes:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>>> As earlier said: it works, but I do not understand the difference
>>> between:
>>> ((t) 10)
>>> and:
>>> (t 10)
>>
>> The first only matches when gnus-idle-daemon-do-log is equal to t,
>> whereas the second always matches (it's an "otherwise" clause).
>
> I was not clear with my question. This I know, but why does the first
> only matches when gnus-idle-daemon-do-log is equal to t?
Because this is what is specified and documented. Have you read C-h f case RET ?
--
__Pascal Bourguignon__ http://www.informatimago.com/
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2010-01-06 20:09 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-03 17:11 How to see that a variable holds t Cecil Westerhof
2010-01-03 19:25 ` Andreas Politz
2010-01-03 20:32 ` Lennart Borgman
2010-01-03 21:46 ` Tim X
[not found] ` <87wrzysjty.fsf@hubble.informatimago.com>
2010-01-04 9:29 ` Cecil Westerhof
2010-01-04 16:44 ` Pascal J. Bourguignon
2010-01-04 17:11 ` Cecil Westerhof
2010-01-06 13:29 ` Cecil Westerhof
2010-01-06 18:12 ` Stefan Monnier
2010-01-06 18:30 ` Cecil Westerhof
2010-01-06 20:09 ` Pascal J. Bourguignon
2010-01-06 18:51 ` Pascal J. Bourguignon
2010-01-06 19:13 ` Cecil Westerhof
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).