all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Executing part of the code instead of another
@ 2020-10-07  0:19 Christopher Dimech
  2020-10-07  0:50 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 23+ messages in thread
From: Christopher Dimech @ 2020-10-07  0:19 UTC (permalink / raw)
  To: Help Gnu Emacs


I would like to have some numeric if condition
so I can set some keybindings when variable a has
value 1, but if variable a is 2, I want to set a
different set of keybindings. How can I do it?

Regards
C*




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

* Re: Executing part of the code instead of another
  2020-10-07  0:19 Executing part of the code instead of another Christopher Dimech
@ 2020-10-07  0:50 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07  0:55   ` Christopher Dimech
  0 siblings, 1 reply; 23+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-10-07  0:50 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Dimech wrote:

> I would like to have some numeric if condition so
> I can set some keybindings when variable a has
> value 1, but if variable a is 2, I want to set
> a different set of keybindings. How can I do it?

It is probably better to base this on the many
different Emacs _modes_, but to answer your question:

(if (= var-a 1)
    (do-something)
  (do-something-else) )

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Executing part of the code instead of another
  2020-10-07  0:50 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-10-07  0:55   ` Christopher Dimech
  2020-10-07  1:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 23+ messages in thread
From: Christopher Dimech @ 2020-10-07  0:55 UTC (permalink / raw)
  To: moasenwood; +Cc: help-gnu-emacs

I have been trying to do the following

(setq optn 1)
(if (= optn 1)

   ( global-unset-key (kbd "C-<home>") )
   ( global-unset-key (kbd "C-<end>") )
   ( global-unset-key (kbd "C-<mouse-4>") )
   ( global-unset-key (kbd "C-<mouse-5>") )

   ( global-set-key (kbd "C-<home>")
       (lambda () (interactive) (scroll-down 1)) ; Scrolls the page Down
   )

   ( global-set-key (kbd "C-<end>")
       (lambda () (interactive) (scroll-up 1))   ; Scrolls the page Up
   )

   ( global-set-key (kbd "C-<mouse-4>")
       (lambda () (interactive) (scroll-down 1)) ; Scrolls the page Down
   )

   ( global-set-key (kbd "C-<mouse-5>")
      (lambda () (interactive) (scroll-up 1))    ; Scrolls the page Up
   )
)






> Sent: Wednesday, October 07, 2020 at 2:50 AM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Executing part of the code instead of another
>
> Christopher Dimech wrote:
>
> > I would like to have some numeric if condition so
> > I can set some keybindings when variable a has
> > value 1, but if variable a is 2, I want to set
> > a different set of keybindings. How can I do it?
>
> It is probably better to base this on the many
> different Emacs _modes_, but to answer your question:
>
> (if (= var-a 1)
>     (do-something)
>   (do-something-else) )
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



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

* Re: Executing part of the code instead of another
  2020-10-07  0:55   ` Christopher Dimech
@ 2020-10-07  1:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07  7:01       ` Christopher Dimech
  0 siblings, 1 reply; 23+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-10-07  1:05 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Dimech wrote:

> I have been trying to do the following
>
> (setq optn 1)
> (if (= optn 1)
>
>    ( global-unset-key (kbd "C-<home>") )
>    [...]
>    ( global-set-key (kbd "C-<mouse-5>")
>       (lambda () (interactive) (scroll-up 1))    ; Scrolls the page Up
>    )
> )

If there is no else branch, use `when' (or `unless').

(when (= var-a 1)
  (do-this)
  (and-this)
  (and-this-as-well) )

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Executing part of the code instead of another
  2020-10-07  1:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-10-07  7:01       ` Christopher Dimech
  2020-10-07  7:13         ` Emanuel Berg via Users list for the GNU Emacs text editor
                           ` (4 more replies)
  0 siblings, 5 replies; 23+ messages in thread
From: Christopher Dimech @ 2020-10-07  7:01 UTC (permalink / raw)
  To: moasenwood; +Cc: help-gnu-emacs

What can one use if there is an else branch with multiple commands, as I would
like to use it.


> Sent: Wednesday, October 07, 2020 at 3:05 AM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Executing part of the code instead of another
>
> Christopher Dimech wrote:
>
> > I have been trying to do the following
> >
> > (setq optn 1)
> > (if (= optn 1)
> >
> >    ( global-unset-key (kbd "C-<home>") )
> >    [...]
> >    ( global-set-key (kbd "C-<mouse-5>")
> >       (lambda () (interactive) (scroll-up 1))    ; Scrolls the page Up
> >    )
> > )
>
> If there is no else branch, use `when' (or `unless').
>
> (when (= var-a 1)
>   (do-this)
>   (and-this)
>   (and-this-as-well) )
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



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

* Re: Executing part of the code instead of another
  2020-10-07  7:01       ` Christopher Dimech
@ 2020-10-07  7:13         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07  7:20         ` Robert Pluim
                           ` (3 subsequent siblings)
  4 siblings, 0 replies; 23+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-10-07  7:13 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Dimech wrote:

> What can one use if there is an else branch with
> multiple commands, as I would like to use it.

if, cond, pcase...

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Executing part of the code instead of another
  2020-10-07  7:01       ` Christopher Dimech
  2020-10-07  7:13         ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-10-07  7:20         ` Robert Pluim
  2020-10-07 13:09           ` Christopher Dimech
  2020-10-07  9:38         ` Gregory Heytings via Users list for the GNU Emacs text editor
                           ` (2 subsequent siblings)
  4 siblings, 1 reply; 23+ messages in thread
From: Robert Pluim @ 2020-10-07  7:20 UTC (permalink / raw)
  To: Christopher Dimech; +Cc: help-gnu-emacs, moasenwood

>>>>> On Wed, 7 Oct 2020 09:01:00 +0200, Christopher Dimech <dimech@gmx.com> said:

    Christopher> What can one use if there is an else branch with multiple commands, as I would
    Christopher> like to use it.

From 'C-h f if'

    if is a special form in `C source code'.

    (if COND THEN ELSE...)

      Probably introduced at or before Emacs version 1.1.

    If COND yields non-nil, do THEN, else do ELSE...
    Returns the value of THEN or the value of the last of the ELSE's.
=>  THEN must be one expression, but ELSE... can be zero or more expressions.
    If COND yields nil, and there are no ELSE's, the value is nil.


Robert
-- 



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

* Re: Executing part of the code instead of another
  2020-10-07  7:01       ` Christopher Dimech
  2020-10-07  7:13         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07  7:20         ` Robert Pluim
@ 2020-10-07  9:38         ` Gregory Heytings via Users list for the GNU Emacs text editor
  2020-10-07  9:46           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07 15:15         ` Drew Adams
  2020-10-18 23:02         ` Douglas Lewan
  4 siblings, 1 reply; 23+ messages in thread
From: Gregory Heytings via Users list for the GNU Emacs text editor @ 2020-10-07  9:38 UTC (permalink / raw)
  To: Christopher Dimech; +Cc: moasenwood, help-gnu-emacs


>
> What can one use if there is an else branch with multiple commands, as I 
> would like to use it.
>

In general you can do:

(if [condition]
   (progn
     [multiple statements of the "then" part])
  [multiple statements of the "else" part"])



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

* Re: Executing part of the code instead of another
  2020-10-07  9:38         ` Gregory Heytings via Users list for the GNU Emacs text editor
@ 2020-10-07  9:46           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07  9:53             ` Robert Pluim
  2020-10-07 13:48             ` Christopher Dimech
  0 siblings, 2 replies; 23+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-10-07  9:46 UTC (permalink / raw)
  To: help-gnu-emacs

Gregory Heytings via Users list for the GNU Emacs text editor wrote:

> In general you can do:
>
> (if [condition]
>   (progn
>     [multiple statements of the "then" part])
>  [multiple statements of the "else" part"])

And the value returned, if condition, is the one of
the last one in the "then" or progn part - the
nth one!

There is also a `prog1'!

But no prog2 :(

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Executing part of the code instead of another
  2020-10-07  9:46           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-10-07  9:53             ` Robert Pluim
  2020-10-07 11:17               ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07 13:28               ` Stefan Monnier
  2020-10-07 13:48             ` Christopher Dimech
  1 sibling, 2 replies; 23+ messages in thread
From: Robert Pluim @ 2020-10-07  9:53 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> On Wed, 07 Oct 2020 11:46:02 +0200, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> said:

    Emanuel> Gregory Heytings via Users list for the GNU Emacs text editor wrote:
    >> In general you can do:
    >> 
    >> (if [condition]
    >> (progn
    >> [multiple statements of the "then" part])
    >> [multiple statements of the "else" part"])

    Emanuel> And the value returned, if condition, is the one of
    Emanuel> the last one in the "then" or progn part - the
    Emanuel> nth one!

    Emanuel> There is also a `prog1'!

    Emanuel> But no prog2 :(

? I see a prog2 macro in subr.el (it used to be a function, going back
to at least 1994).

Robert
-- 



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

* Re: Executing part of the code instead of another
  2020-10-07  9:53             ` Robert Pluim
@ 2020-10-07 11:17               ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07 11:28                 ` Robert Pluim
  2020-10-07 13:28               ` Stefan Monnier
  1 sibling, 1 reply; 23+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-10-07 11:17 UTC (permalink / raw)
  To: help-gnu-emacs

Robert Pluim wrote:

>> But no prog2 :(
>
> ? I see a prog2 macro in subr.el (it used to be
> a function, going back to at least 1994).

I meant there is no prog3 :(

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Executing part of the code instead of another
  2020-10-07 11:17               ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-10-07 11:28                 ` Robert Pluim
  0 siblings, 0 replies; 23+ messages in thread
From: Robert Pluim @ 2020-10-07 11:28 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> On Wed, 07 Oct 2020 13:17:19 +0200, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> said:

    Emanuel> Robert Pluim wrote:
    >>> But no prog2 :(
    >> 
    >> ? I see a prog2 macro in subr.el (it used to be
    >> a function, going back to at least 1994).

    Emanuel> I meant there is no prog3 :(

(prog2 (progn 1 2) 3)

macroization left as an exercise for the reader :-)

Robert
-- 



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

* Re: Executing part of the code instead of another
  2020-10-07  7:20         ` Robert Pluim
@ 2020-10-07 13:09           ` Christopher Dimech
  2020-10-07 17:29             ` Nick Dokos
  0 siblings, 1 reply; 23+ messages in thread
From: Christopher Dimech @ 2020-10-07 13:09 UTC (permalink / raw)
  To: Robert Pluim; +Cc: help-gnu-emacs, moasenwood

I have tried executing the following condition (if (nb > na))

(setq na 2)
(setq nb 5)
( if (> nb na)
    (message "nb > na [condition true]")
    (message "condition false")
)

I highlighted the region then pressed C-x C-e
It returned the first message as expected, but when I tried
the next one, I still got the first message.

(setq na 2)
(setq nb 1)
( if (> nb na)
    (message "nb > na [condition true]")
    (message "condition false")
)




> Sent: Wednesday, October 07, 2020 at 9:20 AM
> From: "Robert Pluim" <rpluim@gmail.com>
> To: "Christopher Dimech" <dimech@gmx.com>
> Cc: moasenwood@zoho.eu, help-gnu-emacs@gnu.org
> Subject: Re: Executing part of the code instead of another
>
> >>>>> On Wed, 7 Oct 2020 09:01:00 +0200, Christopher Dimech <dimech@gmx.com> said:
>
>     Christopher> What can one use if there is an else branch with multiple commands, as I would
>     Christopher> like to use it.
>
> From 'C-h f if'
>
>     if is a special form in `C source code'.
>
>     (if COND THEN ELSE...)
>
>       Probably introduced at or before Emacs version 1.1.
>
>     If COND yields non-nil, do THEN, else do ELSE...
>     Returns the value of THEN or the value of the last of the ELSE's.
> =>  THEN must be one expression, but ELSE... can be zero or more expressions.
>     If COND yields nil, and there are no ELSE's, the value is nil.
>
>
> Robert
> --
>



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

* Re: Executing part of the code instead of another
  2020-10-07  9:53             ` Robert Pluim
  2020-10-07 11:17               ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-10-07 13:28               ` Stefan Monnier
  2020-10-07 13:40                 ` Robert Pluim
  1 sibling, 1 reply; 23+ messages in thread
From: Stefan Monnier @ 2020-10-07 13:28 UTC (permalink / raw)
  To: help-gnu-emacs

> ? I see a prog2 macro in subr.el (it used to be a function, going back
> to at least 1994).

AFAIK it was a special form, not a function.


        Stefan




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

* Re: Executing part of the code instead of another
  2020-10-07 13:28               ` Stefan Monnier
@ 2020-10-07 13:40                 ` Robert Pluim
  0 siblings, 0 replies; 23+ messages in thread
From: Robert Pluim @ 2020-10-07 13:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

>>>>> On Wed, 07 Oct 2020 09:28:34 -0400, Stefan Monnier <monnier@iro.umontreal.ca> said:

    >> ? I see a prog2 macro in subr.el (it used to be a function, going back
    >> to at least 1994).

    Stefan> AFAIK it was a special form, not a function.

What, you think I actually *looked* before saying that? (yes, it was a
special form).

Robert
-- 



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

* Re: Executing part of the code instead of another
  2020-10-07  9:46           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-10-07  9:53             ` Robert Pluim
@ 2020-10-07 13:48             ` Christopher Dimech
  2020-10-07 14:01               ` tomas
  1 sibling, 1 reply; 23+ messages in thread
From: Christopher Dimech @ 2020-10-07 13:48 UTC (permalink / raw)
  To: moasenwood; +Cc: help-gnu-emacs

It would be better if you explain to me this progn stuff

Have tried an example like this

(setq na 8)
(setq nb 13)
( if (> nb na)
    progn (
      (message "nb > na condition [condition is true]")
      (message "nb > na condition [condition is true]")
    )
    progn (
      (message "condition false")
      (message "condition false")
    )
)


> Sent: Wednesday, October 07, 2020 at 11:46 AM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Executing part of the code instead of another
>
> Gregory Heytings via Users list for the GNU Emacs text editor wrote:
>
> > In general you can do:
> >
> > (if [condition]
> >   (progn
> >     [multiple statements of the "then" part])
> >  [multiple statements of the "else" part"])
>
> And the value returned, if condition, is the one of
> the last one in the "then" or progn part - the
> nth one!
>
> There is also a `prog1'!
>
> But no prog2 :(
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



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

* Re: Executing part of the code instead of another
  2020-10-07 13:48             ` Christopher Dimech
@ 2020-10-07 14:01               ` tomas
  2020-10-07 14:05                 ` Christopher Dimech
  0 siblings, 1 reply; 23+ messages in thread
From: tomas @ 2020-10-07 14:01 UTC (permalink / raw)
  To: Christopher Dimech; +Cc: help-gnu-emacs, moasenwood

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

On Wed, Oct 07, 2020 at 03:48:51PM +0200, Christopher Dimech wrote:
> It would be better if you explain to me this progn stuff
> 
> Have tried an example like this
> 
> (setq na 8)
> (setq nb 13)
> ( if (> nb na)
>     progn (
>       (message "nb > na condition [condition is true]")
>       (message "nb > na condition [condition is true]")
>     )

No. Written in Lisp, it's

  (progn
    (thing 1)
    (thing 2)
    ...)

In C (and in conventional maths, you'd write it as you did
above).

Note that it's the same way as you write (message "foo")
[you don't write message("foo"), as you'd do in C or
Python or what not).

Progn is a form to bundle a sequence of forms, which are
evaluated one after the other. The value of progn is
that of the last form evaluated (that's the -n), as opposed
to prog1, which would do the same as progn, but return the
value of its first form.

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: Executing part of the code instead of another
  2020-10-07 14:01               ` tomas
@ 2020-10-07 14:05                 ` Christopher Dimech
  2020-10-08 17:43                   ` Leo Butler
  0 siblings, 1 reply; 23+ messages in thread
From: Christopher Dimech @ 2020-10-07 14:05 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs, moasenwood


Thank you so very much Tomas. Ok. Got It.


> Sent: Wednesday, October 07, 2020 at 4:01 PM
> From: tomas@tuxteam.de
> To: "Christopher Dimech" <dimech@gmx.com>
> Cc: moasenwood@zoho.eu, help-gnu-emacs@gnu.org
> Subject: Re: Executing part of the code instead of another
>
> On Wed, Oct 07, 2020 at 03:48:51PM +0200, Christopher Dimech wrote:
> > It would be better if you explain to me this progn stuff
> >
> > Have tried an example like this
> >
> > (setq na 8)
> > (setq nb 13)
> > ( if (> nb na)
> >     progn (
> >       (message "nb > na condition [condition is true]")
> >       (message "nb > na condition [condition is true]")
> >     )
>
> No. Written in Lisp, it's
>
>   (progn
>     (thing 1)
>     (thing 2)
>     ...)
>
> In C (and in conventional maths, you'd write it as you did
> above).
>
> Note that it's the same way as you write (message "foo")
> [you don't write message("foo"), as you'd do in C or
> Python or what not).
>
> Progn is a form to bundle a sequence of forms, which are
> evaluated one after the other. The value of progn is
> that of the last form evaluated (that's the -n), as opposed
> to prog1, which would do the same as progn, but return the
> value of its first form.
>
> Cheers
>  - t
>



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

* RE: Executing part of the code instead of another
  2020-10-07  7:01       ` Christopher Dimech
                           ` (2 preceding siblings ...)
  2020-10-07  9:38         ` Gregory Heytings via Users list for the GNU Emacs text editor
@ 2020-10-07 15:15         ` Drew Adams
  2020-10-18 23:02         ` Douglas Lewan
  4 siblings, 0 replies; 23+ messages in thread
From: Drew Adams @ 2020-10-07 15:15 UTC (permalink / raw)
  To: Christopher Dimech, moasenwood; +Cc: help-gnu-emacs

> What can one use if there is an else branch with multiple commands, as I
> would like to use it.

Please consider asking Emacs:

`C-h i', choose `Emacs Lisp Intro', and start
learning about Emacs Lisp.

`i conditional RET' looks up "conditional" in
the index and takes you to the first index entry.
`,' takes you to other entries.



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

* Re: Executing part of the code instead of another
  2020-10-07 13:09           ` Christopher Dimech
@ 2020-10-07 17:29             ` Nick Dokos
  2020-10-07 18:01               ` Christopher Dimech
  0 siblings, 1 reply; 23+ messages in thread
From: Nick Dokos @ 2020-10-07 17:29 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Dimech <dimech@gmx.com> writes:

> I have tried executing the following condition (if (nb > na))
>
> (setq na 2)
> (setq nb 5)
> ( if (> nb na)
>     (message "nb > na [condition true]")
>     (message "condition false")
> )
>
> I highlighted the region then pressed C-x C-e
> It returned the first message as expected, but when I tried
> the next one, I still got the first message.
>
`C-x C-e` is bound to `eval-last-sexp` which evaluates the *last*
sexp: the if.  It does not evaluate the setq's (setting the region does
*NOT* matter for it - try `M-x eval-region' for that)


> (setq na 2)
> (setq nb 1)
> ( if (> nb na)
>     (message "nb > na [condition true]")
>     (message "condition false")
> )
>
>
>
>
>> Sent: Wednesday, October 07, 2020 at 9:20 AM
>> From: "Robert Pluim" <rpluim@gmail.com>
>> To: "Christopher Dimech" <dimech@gmx.com>
>> Cc: moasenwood@zoho.eu, help-gnu-emacs@gnu.org
>> Subject: Re: Executing part of the code instead of another
>>
>> >>>>> On Wed, 7 Oct 2020 09:01:00 +0200, Christopher Dimech <dimech@gmx.com> said:
>>
>>     Christopher> What can one use if there is an else branch with multiple commands, as I would
>>     Christopher> like to use it.
>>
>> From 'C-h f if'
>>
>>     if is a special form in `C source code'.
>>
>>     (if COND THEN ELSE...)
>>
>>       Probably introduced at or before Emacs version 1.1.
>>
>>     If COND yields non-nil, do THEN, else do ELSE...
>>     Returns the value of THEN or the value of the last of the ELSE's.
>> =>  THEN must be one expression, but ELSE... can be zero or more expressions.
>>     If COND yields nil, and there are no ELSE's, the value is nil.
>>
>>
>> Robert
>> --
>>
>
>

-- 
Nick

"There are only two hard problems in computer science: cache
invalidation, naming things, and off-by-one errors." -Martin Fowler




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

* Re: Executing part of the code instead of another
  2020-10-07 17:29             ` Nick Dokos
@ 2020-10-07 18:01               ` Christopher Dimech
  0 siblings, 0 replies; 23+ messages in thread
From: Christopher Dimech @ 2020-10-07 18:01 UTC (permalink / raw)
  To: Nick Dokos; +Cc: help-gnu-emacs

Noted Nick.  Thank you.

---------------------
Christopher Dimech
Chief Administrator - Naiad Informatics - GNU Project (Geocomputation)
- Geophysical Simulation
- Geological Subsurface Mapping
- Disaster Preparedness and Mitigation
- Natural Resource Exploration and Production
- Free Software Advocacy


> Sent: Wednesday, October 07, 2020 at 7:29 PM
> From: "Nick Dokos" <ndokos@gmail.com>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Executing part of the code instead of another
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > I have tried executing the following condition (if (nb > na))
> >
> > (setq na 2)
> > (setq nb 5)
> > ( if (> nb na)
> >     (message "nb > na [condition true]")
> >     (message "condition false")
> > )
> >
> > I highlighted the region then pressed C-x C-e
> > It returned the first message as expected, but when I tried
> > the next one, I still got the first message.
> >
> `C-x C-e` is bound to `eval-last-sexp` which evaluates the *last*
> sexp: the if.  It does not evaluate the setq's (setting the region does
> *NOT* matter for it - try `M-x eval-region' for that)
>
>
> > (setq na 2)
> > (setq nb 1)
> > ( if (> nb na)
> >     (message "nb > na [condition true]")
> >     (message "condition false")
> > )
> >
> >
> >
> >
> >> Sent: Wednesday, October 07, 2020 at 9:20 AM
> >> From: "Robert Pluim" <rpluim@gmail.com>
> >> To: "Christopher Dimech" <dimech@gmx.com>
> >> Cc: moasenwood@zoho.eu, help-gnu-emacs@gnu.org
> >> Subject: Re: Executing part of the code instead of another
> >>
> >> >>>>> On Wed, 7 Oct 2020 09:01:00 +0200, Christopher Dimech <dimech@gmx.com> said:
> >>
> >>     Christopher> What can one use if there is an else branch with multiple commands, as I would
> >>     Christopher> like to use it.
> >>
> >> From 'C-h f if'
> >>
> >>     if is a special form in `C source code'.
> >>
> >>     (if COND THEN ELSE...)
> >>
> >>       Probably introduced at or before Emacs version 1.1.
> >>
> >>     If COND yields non-nil, do THEN, else do ELSE...
> >>     Returns the value of THEN or the value of the last of the ELSE's.
> >> =>  THEN must be one expression, but ELSE... can be zero or more expressions.
> >>     If COND yields nil, and there are no ELSE's, the value is nil.
> >>
> >>
> >> Robert
> >> --
> >>
> >
> >
>
> --
> Nick
>
> "There are only two hard problems in computer science: cache
> invalidation, naming things, and off-by-one errors." -Martin Fowler
>
>
>



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

* Re: Executing part of the code instead of another
  2020-10-07 14:05                 ` Christopher Dimech
@ 2020-10-08 17:43                   ` Leo Butler
  0 siblings, 0 replies; 23+ messages in thread
From: Leo Butler @ 2020-10-08 17:43 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Dimech <dimech@gmx.com> writes:

> Thank you so very much Tomas. Ok. Got It.
>
>
>> Sent: Wednesday, October 07, 2020 at 4:01 PM
>> From: tomas@tuxteam.de
>> To: "Christopher Dimech" <dimech@gmx.com>
>> Cc: moasenwood@zoho.eu, help-gnu-emacs@gnu.org
>> Subject: Re: Executing part of the code instead of another
>>
>> On Wed, Oct 07, 2020 at 03:48:51PM +0200, Christopher Dimech wrote:
>> > It would be better if you explain to me this progn stuff
>> >
>> > Have tried an example like this
>> >
>> > (setq na 8)
>> > (setq nb 13)
>> > ( if (> nb na)
>> >     progn (
>> >       (message "nb > na condition [condition is true]")
>> >       (message "nb > na condition [condition is true]")
>> >     )
>>
>> No. Written in Lisp, it's
>>
>>   (progn
>>     (thing 1)
>>     (thing 2)
>>     ...)
>>

Note that Emacs has its own repl, too.

M-x ielm RET

Take a while to learn how to think in lisp. The constructs you are using
are C-like and very primitive (non-expressive, if you prefer) compared
to what you can do in lisp.

Specifically, it seems like what you are trying to do is usually
accomplished with minor-modes in elisp.

Leo



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

* Re: Executing part of the code instead of another
  2020-10-07  7:01       ` Christopher Dimech
                           ` (3 preceding siblings ...)
  2020-10-07 15:15         ` Drew Adams
@ 2020-10-18 23:02         ` Douglas Lewan
  4 siblings, 0 replies; 23+ messages in thread
From: Douglas Lewan @ 2020-10-18 23:02 UTC (permalink / raw)
  To: help-gnu-emacs

(cond) is what comes to my mind for such a thing.

On 10/7/20 3:01 AM, Christopher Dimech wrote:
> What can one use if there is an else branch with multiple commands, as I would
> like to use it.
>
>
>> Sent: Wednesday, October 07, 2020 at 3:05 AM
>> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
>> To: help-gnu-emacs@gnu.org
>> Subject: Re: Executing part of the code instead of another
>>
>> Christopher Dimech wrote:
>>
>>> I have been trying to do the following
>>>
>>> (setq optn 1)
>>> (if (= optn 1)
>>>
>>>     ( global-unset-key (kbd "C-<home>") )
>>>     [...]
>>>     ( global-set-key (kbd "C-<mouse-5>")
>>>        (lambda () (interactive) (scroll-up 1))    ; Scrolls the page Up
>>>     )
>>> )
>> If there is no else branch, use `when' (or `unless').
>>
>> (when (= var-a 1)
>>    (do-this)
>>    (and-this)
>>    (and-this-as-well) )
>>
>> --
>> underground experts united
>> http://user.it.uu.se/~embe8573
>> https://dataswamp.org/~incal
>>
>>
>>
-- 
,Doug
d.lewan2000@gmail.com
(908) 720 7908

You know, it's amazing how much closer to 0 that 8 067 332 is than 15 is. (2020 Oct 18)




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

end of thread, other threads:[~2020-10-18 23:02 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-07  0:19 Executing part of the code instead of another Christopher Dimech
2020-10-07  0:50 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-10-07  0:55   ` Christopher Dimech
2020-10-07  1:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-10-07  7:01       ` Christopher Dimech
2020-10-07  7:13         ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-10-07  7:20         ` Robert Pluim
2020-10-07 13:09           ` Christopher Dimech
2020-10-07 17:29             ` Nick Dokos
2020-10-07 18:01               ` Christopher Dimech
2020-10-07  9:38         ` Gregory Heytings via Users list for the GNU Emacs text editor
2020-10-07  9:46           ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-10-07  9:53             ` Robert Pluim
2020-10-07 11:17               ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-10-07 11:28                 ` Robert Pluim
2020-10-07 13:28               ` Stefan Monnier
2020-10-07 13:40                 ` Robert Pluim
2020-10-07 13:48             ` Christopher Dimech
2020-10-07 14:01               ` tomas
2020-10-07 14:05                 ` Christopher Dimech
2020-10-08 17:43                   ` Leo Butler
2020-10-07 15:15         ` Drew Adams
2020-10-18 23:02         ` Douglas Lewan

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.