help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: trouble writing a conditional, or with lambda


From: lawrence mitchell
Subject: Re: trouble writing a conditional, or with lambda
Date: Sat, 24 May 2003 17:14:14 +0100
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50

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>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]