* Re: trouble writing a conditional, or with lambda
2003-05-24 16:09 trouble writing a conditional, or with lambda Florian von Savigny
@ 2003-05-24 15:37 ` Thomas Gehrlein
2003-05-24 16:08 ` Oliver Scholz
2003-05-24 16:14 ` lawrence mitchell
2 siblings, 0 replies; 5+ messages in thread
From: Thomas Gehrlein @ 2003-05-24 15:37 UTC (permalink / raw)
Florian von Savigny <florian265@uboot.com> writes:
> some basic lisp, I'm afraid, but I did consult the manual and tested
> in lisp-interaction-mode, but did not get any the wiser.
>
> I'm trying to get a function to work differently depending on whether
> emacs runs under X or on a terminal:
Not tested:
(if (eq window-system nil)
;; running under a terminal
(progn
(split-window)
(switch-to-buffer "*foo*"))
;; running under a window system
(select-frame (make-frame))
(set-frame-size (selected-frame) 50 24)
(set-frame-position (selected-frame) 150 120))
Thomas
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: trouble writing a conditional, or with lambda
2003-05-24 16:09 trouble writing a conditional, or with lambda Florian von Savigny
2003-05-24 15:37 ` Thomas Gehrlein
@ 2003-05-24 16:08 ` Oliver Scholz
2003-05-24 16:14 ` lawrence mitchell
2 siblings, 0 replies; 5+ messages in thread
From: Oliver Scholz @ 2003-05-24 16:08 UTC (permalink / raw)
Florian von Savigny <florian265@uboot.com> writes:
> Sigh ...
>
>
> some basic lisp, I'm afraid, but I did consult the manual and tested
> in lisp-interaction-mode, but did not get any the wiser.
>
> I'm trying to get a function to work differently depending on whether
> emacs runs under X or on a terminal:
>
> (if (eq window-system nil)
> ; running under a terminal
> (lambda ()
> (split-window)
> (switch-to-buffer "*foo*")
> )
> ; running under a window system
> (lambda ()
> (select-frame (make-frame))
> (set-frame-size (selected-frame) 50 24)
> (set-frame-position (selected-frame) 150 120)
> ))
Two notes:
1) You should not check the variable `window-system', if you can avoid
it. Better check for the specific feature you need. In this case
you could probably use the function `display-multi-frame-p'.
2) The expression above does not call `split-window', `select-frame'
and the like, it just returns an /anonymous function/ (aka
"lambda-function"). You could call `funcall' the return value of
your expression, but this is probably not what you want. I think,
this is more likely to do what you want to achieve:
(if (display-multi-frame-p)
;; Running under a terminal
(progn
(split-window)
(switch-to-buffer "*foo*"))
;; running under a window system
;; (`progn' is not necessary here, because `if' accepts more than
;; three arguments. All arguments from the third one onward
;; constitute the THEN clause.)
(select-frame (make-frame))
(set-frame-size (selected-frame) 50 24)
(set-frame-position (selected-frame) 150 120))
> It seems that everything in the lambda expressions is ignored
> (i.e. nothing happens). I used these lambda expressions because
> simply putting a body of functions got error messages about "Invalid
> function"s. But it seems I don't get these right.
>
>
> (lambda (
> (split-window)
> (switch-to-buffer "*foo*")
> ))
>
> also seems to be valid syntax, but is also ignored.
No, it is not valid syntax. But it is not evaluated either, therefore
you don't get an error.
You could have a look at the "Introduction to Emacs Lisp" by Robert
Chassell, which explains some basics of Emacs Lisp.
Oliver
--
5 Prairial an 211 de la Révolution
Liberté, Egalité, Fraternité!
^ permalink raw reply [flat|nested] 5+ messages in thread
* trouble writing a conditional, or with lambda
@ 2003-05-24 16:09 Florian von Savigny
2003-05-24 15:37 ` Thomas Gehrlein
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Florian von Savigny @ 2003-05-24 16:09 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1295 bytes --]
Sigh ...
some basic lisp, I'm afraid, but I did consult the manual and tested
in lisp-interaction-mode, but did not get any the wiser.
I'm trying to get a function to work differently depending on whether
emacs runs under X or on a terminal:
(if (eq window-system nil)
; running under a terminal
(lambda ()
(split-window)
(switch-to-buffer "*foo*")
)
; running under a window system
(lambda ()
(select-frame (make-frame))
(set-frame-size (selected-frame) 50 24)
(set-frame-position (selected-frame) 150 120)
))
It seems that everything in the lambda expressions is ignored
(i.e. nothing happens). I used these lambda expressions because
simply putting a body of functions got error messages about "Invalid
function"s. But it seems I don't get these right.
(lambda (
(split-window)
(switch-to-buffer "*foo*")
))
also seems to be valid syntax, but is also ignored.
Can anybody help how to get this simple conditional to work?
--
Florian v. Savigny
If you are going to reply in private, please be patient, as I only
check for mail something like once a week. - Si vous allez répondre
personellement, patientez s.v.p., car je ne lis les courriels
qu'environ une fois par semaine.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: trouble writing a conditional, or with lambda
2003-05-24 16:09 trouble writing a conditional, or with lambda Florian von Savigny
2003-05-24 15:37 ` Thomas Gehrlein
2003-05-24 16:08 ` Oliver Scholz
@ 2003-05-24 16:14 ` lawrence mitchell
2003-05-25 12:04 ` Florian von Savigny
2 siblings, 1 reply; 5+ messages in thread
From: lawrence mitchell @ 2003-05-24 16:14 UTC (permalink / raw)
Florian von Savigny wrote:
> Sigh ...
> some basic lisp, I'm afraid, but I did consult the manual and tested
> in lisp-interaction-mode, but did not get any the wiser.
Have you tried reading the Emacs Lisp Introduction? It might
already be available on your system, try by doing
C-h i d m Emacs Lisp Intro RET
> I'm trying to get a function to work differently depending on whether
> emacs runs under X or on a terminal:
> (if (eq window-system nil)
> ; running under a terminal
> (lambda ()
> (split-window)
> (switch-to-buffer "*foo*")
> )
> ; running under a window system
> (lambda ()
> (select-frame (make-frame))
> (set-frame-size (selected-frame) 50 24)
> (set-frame-position (selected-frame) 150 120)
> ))
Note that LAMBDA is a self-quoting form, and hence, the above
would return a lambda expression which you would have to FUNCALL
to achieve the result you're looking for (though this is
probably not what you want):
(lambda ()
(split-window)
(switch-to-buffer "*foo*"))
=> (lambda ()
(split-window)
(switch-to-buffer "*foo*"))
I presume you're using lambdas because the "then" part of an IF
statement in emacs lisp has to be a single expression. However,
you probably want to be using PROGN:
(if some-condition
(progn (do-first-thing) (do-second-thing)))
The "else" branch has what is known as an implicit PROGN, i.e.,
you can execute multiple statements without needing to wrap them
in a PROGN.
(if some-condition-that-isn't-true
nil
;; both these will be executed.
(do-first-thing)
(do-second-thing))
Note also that you do not need to check WINDOW-SYSTEM being NIL,
you can just reverse the logic of your IF statement. This is
due to the fact that NIL is the only false truth value in elisp.
(if window-system
;; This will be executed unless running on a tty
(progn (do-stuff-for-window-system))
;; This will be executed when running on a tty
(do-stuff-for-tty))
[...]
> Can anybody help how to get this simple conditional to work?
Try something like:
(if window-system
(progn (select-frame (make-frame))
(set-frame-size (selected-frame) 50 24)
(set-frame-position (selected-frame) 150 120)))
(split-window)
(switch-to-buffer (get-buffer-create "*foo*")))
--
lawrence mitchell <wence@gmx.li>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: trouble writing a conditional, or with lambda
2003-05-24 16:14 ` lawrence mitchell
@ 2003-05-25 12:04 ` Florian von Savigny
0 siblings, 0 replies; 5+ messages in thread
From: Florian von Savigny @ 2003-05-25 12:04 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1113 bytes --]
Thomas, Oliver, and Lawrence,
Thanks to all of you, all of this was most helpful and taken together,
it covered about every conceivable aspect of my question. I have to
admit I own a printed copy of Chassel's introduction, but did not have
enough patience to find what I was looking for (maybe out of fear of
getting lost somewhere else). In general, I have the feeling that Lisp
documentation is often hard to digest (if concise).
I have now understood the difference between lambda and progn (I used
to know the latter, but forgot about it); I think I tried lambda
because it is used when you want to add a bunch of functions to a hook
in one go - I have now realised that this is precisely because those
functions are not executed then but rather, well, named (I'll find it
in Chassel now ... ).
Thanks again, I'll store your answers for future reference ...
Florian
--
If you are going to reply in private, please be patient, as I only
check for mail something like once a week. - Si vous allez répondre
personellement, patientez s.v.p., car je ne lis les courriels
qu'environ une fois par semaine.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2003-05-25 12:04 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-24 16:09 trouble writing a conditional, or with lambda Florian von Savigny
2003-05-24 15:37 ` Thomas Gehrlein
2003-05-24 16:08 ` Oliver Scholz
2003-05-24 16:14 ` lawrence mitchell
2003-05-25 12:04 ` Florian von Savigny
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).