unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Function passing flag
@ 2022-03-08 12:24 goncholden via Users list for the GNU Emacs text editor
  2022-03-08 18:00 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 10+ messages in thread
From: goncholden via Users list for the GNU Emacs text editor @ 2022-03-08 12:24 UTC (permalink / raw)
  To: goncholden via Users list for the GNU Emacs text editor

I want to use a switch for two pieces of code, but want to use a flag passed through the function
argument. How is this customarily achieved ?

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

* Re: Function passing flag
  2022-03-08 12:24 Function passing flag goncholden via Users list for the GNU Emacs text editor
@ 2022-03-08 18:00 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-08 19:00   ` goncholden
  0 siblings, 1 reply; 10+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-08 18:00 UTC (permalink / raw)
  To: help-gnu-emacs

goncholden via Users list for the GNU Emacs text editor wrote:

> I want to use a switch for two pieces of code, but want to
> use a flag passed through the function argument. How is this
> customarily achieved ?

What is it that you want to do?

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Function passing flag
  2022-03-08 18:00 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-03-08 19:00   ` goncholden
  2022-03-08 19:14     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 10+ messages in thread
From: goncholden @ 2022-03-08 19:00 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

------- Original Message -------

On Wednesday, March 9th, 2022 at 6:00 AM, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> goncholden via Users list for the GNU Emacs text editor wrote:
>
> > I want to use a switch for two pieces of code, but want to
> >
> > use a flag passed through the function argument. How is this
> >
> > customarily achieved ?
>
> What is it that you want to do?

Want to use an if-then conditional, but based on a parameter passed to the function as argument.


> --
>
> underground experts united
>
> https://dataswamp.org/~incal



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

* Re: Function passing flag
  2022-03-08 19:00   ` goncholden
@ 2022-03-08 19:14     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-08 19:35       ` goncholden
  0 siblings, 1 reply; 10+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-08 19:14 UTC (permalink / raw)
  To: help-gnu-emacs

goncholden wrote:

> Want to use an if-then conditional, but based on a parameter
> passed to the function as argument.

Sure, do it ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Function passing flag
  2022-03-08 19:14     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-03-08 19:35       ` goncholden
  2022-03-08 20:09         ` Corwin Brust
  0 siblings, 1 reply; 10+ messages in thread
From: goncholden @ 2022-03-08 19:35 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

------- Original Message -------

On Wednesday, March 9th, 2022 at 7:14 AM, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> goncholden wrote:
>
> > Want to use an if-then conditional, but based on a parameter
> >
> > passed to the function as argument.
>
> Sure, do it ...

How does one do it with an optional argument?  Where can I read about this?

> --
>
> underground experts united
>
> https://dataswamp.org/~incal



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

* Re: Function passing flag
  2022-03-08 19:35       ` goncholden
@ 2022-03-08 20:09         ` Corwin Brust
  2022-03-08 20:15           ` Corwin Brust
  2022-03-08 20:28           ` goncholden
  0 siblings, 2 replies; 10+ messages in thread
From: Corwin Brust @ 2022-03-08 20:09 UTC (permalink / raw)
  To: goncholden; +Cc: Help Gnu Emacs mailing list, Emanuel Berg

On Tue, Mar 8, 2022 at 1:36 PM goncholden <goncholden@protonmail.com> wrote:
>
> How does one do it with an optional argument?  Where can I read about this?
>

A function with an optional argument ARG might look like this:

(defun my:own-fun (&optional arg)
  "Do something. When ARG is non-nil do it a lot."
  (if arg
      ;; when ARG is true we run this
      (do-stuff)
    ;; else we run this
    (do-more-or-different-stuff)))

Note, if you are defining a command (a function you will bind to a key
or otherwise use interactively), you will want to to use
(interactive), usually placed at the start of the function definition,
so just after the docstring.



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

* Re: Function passing flag
  2022-03-08 20:09         ` Corwin Brust
@ 2022-03-08 20:15           ` Corwin Brust
  2022-03-08 20:28           ` goncholden
  1 sibling, 0 replies; 10+ messages in thread
From: Corwin Brust @ 2022-03-08 20:15 UTC (permalink / raw)
  To: goncholden; +Cc: Help Gnu Emacs mailing list, Emanuel Berg

Apologies, I answered only half of your question, I think.  As to
where you can read about this:

I recommend reading the entire Functions section of the elisp manual
however you can get to the specific section that introduces optional
arguments via:

  M-: (info "(elisp)Defining Functions")  RET

On Tue, Mar 8, 2022 at 2:09 PM Corwin Brust <corwin@bru.st> wrote:
>
> On Tue, Mar 8, 2022 at 1:36 PM goncholden <goncholden@protonmail.com> wrote:
> >
> > How does one do it with an optional argument?  Where can I read about this?
> >
>
> A function with an optional argument ARG might look like this:
>
> (defun my:own-fun (&optional arg)
>   "Do something. When ARG is non-nil do it a lot."
>   (if arg
>       ;; when ARG is true we run this
>       (do-stuff)
>     ;; else we run this
>     (do-more-or-different-stuff)))
>
> Note, if you are defining a command (a function you will bind to a key
> or otherwise use interactively), you will want to to use
> (interactive), usually placed at the start of the function definition,
> so just after the docstring.



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

* Re: Function passing flag
  2022-03-08 20:09         ` Corwin Brust
  2022-03-08 20:15           ` Corwin Brust
@ 2022-03-08 20:28           ` goncholden
  2022-03-08 20:42             ` Corwin Brust
  1 sibling, 1 reply; 10+ messages in thread
From: goncholden @ 2022-03-08 20:28 UTC (permalink / raw)
  To: Corwin Brust; +Cc: Help Gnu Emacs mailing list, Emanuel Berg


------- Original Message -------

On Tuesday, March 8th, 2022 at 8:09 PM, Corwin Brust <corwin@bru.st> wrote:

> On Tue, Mar 8, 2022 at 1:36 PM goncholden goncholden@protonmail.com wrote:
>
> > How does one do it with an optional argument? Where can I read about this?
>
> A function with an optional argument ARG might look like this:
>
> (defun my:own-fun (&optional arg)
> "Do something. When ARG is non-nil do it a lot."
> (if arg
> ;; when ARG is true we run this
> (do-stuff)
> ;; else we run this
> (do-more-or-different-stuff)))

Would like to use arg as a switch so I can enable or disable some emacs modes (customarily using
value of 1 to enable, and -1 to disable).  If no arg is supplied, I would like to enable the modes.

How would I define the parameter outside the function, and then pass it through.  I might require
a buffer local variable.







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

* Re: Function passing flag
  2022-03-08 20:28           ` goncholden
@ 2022-03-08 20:42             ` Corwin Brust
  2022-03-08 20:55               ` goncholden
  0 siblings, 1 reply; 10+ messages in thread
From: Corwin Brust @ 2022-03-08 20:42 UTC (permalink / raw)
  To: goncholden; +Cc: Help Gnu Emacs mailing list, Emanuel Berg

On Tue, Mar 8, 2022 at 2:28 PM goncholden <goncholden@protonmail.com> wrote:
>
> Would like to use arg as a switch so I can enable or disable some emacs modes (customarily using
> value of 1 to enable, and -1 to disable).  If no arg is supplied, I would like to enable the modes.
>
> How would I define the parameter outside the function, and then pass it through.  I might require
> a buffer local variable.
>

One typical way of doing this would be to use the "universal
argument", also known as a "prefix argument":

(info "(elisp)Prefix Command Arguments")

Here's an example that would create an interactive command that
enables tool-bars and scroll-bars or, with a prefix argument, disables
them.

(defun my:enable-bars (&optional arg)
  "Enable `tool-bar-mode' and `scroll-bar-mode'.

When ARG is non-nill, disable them."
  (interactive "P")
  (if arg
      (progn (tool-bar-mode -1)
     (scroll-bar-mode -1))
    (tool-bar-mode 1)
    (scroll-bar-mode 1)))

Once you evaluate this you can ensure both tool-bar and scroll bar
modes are active with:
  M-x my:enable-bar RET

Or, to ensure both tool-bar and scroll-bar modes are disabled:
  C-u M-x my:enable-bar RET



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

* Re: Function passing flag
  2022-03-08 20:42             ` Corwin Brust
@ 2022-03-08 20:55               ` goncholden
  0 siblings, 0 replies; 10+ messages in thread
From: goncholden @ 2022-03-08 20:55 UTC (permalink / raw)
  To: Corwin Brust; +Cc: Help Gnu Emacs mailing list, Emanuel Berg

------- Original Message -------
On Tuesday, March 8th, 2022 at 8:42 PM, Corwin Brust <corwin@bru.st> wrote:
> On Tue, Mar 8, 2022 at 2:28 PM goncholden goncholden@protonmail.com wrote:

> > Would like to use arg as a switch so I can enable or disable some emacs modes (customarily using
> > value of 1 to enable, and -1 to disable). If no arg is supplied, I would like to enable the modes.

> > How would I define the parameter outside the function, and then pass it through. I might require
> > a buffer local variable.

> One typical way of doing this would be to use the "universal
> argument", also known as a "prefix argument":
> (info "(elisp)Prefix Command Arguments")

> Here's an example that would create an interactive command that
> enables tool-bars and scroll-bars or, with a prefix argument, disables
> them.

> (defun my:enable-bars (&optional arg)
> "Enable `tool-bar-mode' and` scroll-bar-mode'.
> When ARG is non-nill, disable them."
> (interactive "P")
> (if arg
> (progn (tool-bar-mode -1)
> (scroll-bar-mode -1))
> (tool-bar-mode 1)
> (scroll-bar-mode 1)))
>
> Once you evaluate this you can ensure both tool-bar and scroll bar
> modes are active with:
> M-x my:enable-bar RET
>
> Or, to ensure both tool-bar and scroll-bar modes are disabled:
> C-u M-x my:enable-bar RET

Have done like this

(defun romona (&optional action)
  "Enables or disables Emacs Bars"

  ;; If action in nil, set action to enable the bar modes
  (unless action (setq action 1))
  (if action
      (progn  (tool-bar-mode 1)
        (scroll-bar-mode 1))
    (progn  (tool-bar-mode -1)
      (scroll-bar-mode -1)) ))

Could just need a defvar.






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

end of thread, other threads:[~2022-03-08 20:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-08 12:24 Function passing flag goncholden via Users list for the GNU Emacs text editor
2022-03-08 18:00 ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-08 19:00   ` goncholden
2022-03-08 19:14     ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-08 19:35       ` goncholden
2022-03-08 20:09         ` Corwin Brust
2022-03-08 20:15           ` Corwin Brust
2022-03-08 20:28           ` goncholden
2022-03-08 20:42             ` Corwin Brust
2022-03-08 20:55               ` goncholden

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).