unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Handling optional argument with t value
@ 2022-12-04 23:01 Heime
  2022-12-05 19:16 ` Jean Louis
  0 siblings, 1 reply; 4+ messages in thread
From: Heime @ 2022-12-04 23:01 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

I have this function to which I am adding the capability to print a compact version 
that excludes the whitespace.  Thus I include an optional argument "kpact", so that
when it is set to t, the (insert " ") in excluded.  Is this a good strategy.  

Does the use of (unless kpact  (insert " ")) do the job or am I missing some possible
condition that might occur?

(defun ticker-hour (lampkl &optional kpact)
  "TODO."

  (interactive)

  (unless kpact  (insert " "))

  (cond
   ((eq 'magenta lampkl)
       (insert (propertize "⚒"
                 'font-lock-face '(:background "#FF29FF"
                                   :foreground "black"))))
   ((eq 'red lampkl)
       (insert (propertize "⚒"
                 'font-lock-face '(:background "#B30000"
                                   :foreground "black"))))))









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

* Re: Handling optional argument with t value
  2022-12-04 23:01 Handling optional argument with t value Heime
@ 2022-12-05 19:16 ` Jean Louis
  2022-12-05 20:09   ` Heime
  0 siblings, 1 reply; 4+ messages in thread
From: Jean Louis @ 2022-12-05 19:16 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

* Heime <heimeborgia@protonmail.com> [2022-12-05 02:02]:
> I have this function to which I am adding the capability to print a
> compact version that excludes the whitespace.  Thus I include an
> optional argument "kpact", so that when it is set to t, the (insert
> " ") in excluded.  Is this a good strategy.

"To print a compact version..." compact version of what?

I understand you wish to print propertized version of some symbol. 

If it is good strategy or not, I guess you are the one to
decide. Because it is with special background, I would insert space
before and after, to make it more indicative or to make it more visible.

> Does the use of (unless kpact (insert " ")) do the job or am I
> missing some possible condition that might occur?

You missed to express better what you wish to achieve.


(defun ticker-hour (lampkl &optional kpact)
  "TODO."
  (interactive)
  (unless kpact  (insert " "))
  (cond
   ((eq 'magenta lampkl)
    (insert (propertize 
	     "⚒" 'font-lock-face 
	     '(:background "#FF29FF" :foreground "black"))))
   ((eq 'red lampkl)
    (insert (propertize 
	     "⚒" 'font-lock-face 
	     '(:background "#B30000" :foreground "black"))))))

(ticker-hour 'magenta) ⚒
(ticker-hour 'red) ⚒

Heime, don't give up. 🔧

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Handling optional argument with t value
  2022-12-05 19:16 ` Jean Louis
@ 2022-12-05 20:09   ` Heime
  2022-12-05 23:03     ` Emanuel Berg
  0 siblings, 1 reply; 4+ messages in thread
From: Heime @ 2022-12-05 20:09 UTC (permalink / raw)
  To: Jean Louis; +Cc: Heime via Users list for the GNU Emacs text editor

------- Original Message -------
On Monday, December 5th, 2022 at 7:16 PM, Jean Louis <bugs@gnu.support> wrote:


> * Heime heimeborgia@protonmail.com [2022-12-05 02:02]:
> 
> > I have this function to which I am adding the capability to print a
> > compact version that excludes the whitespace. Thus I include an
> > optional argument "kpact", so that when it is set to t, the (insert
> > " ") in excluded. Is this a good strategy.
> 
> 
> "To print a compact version..." compact version of what?
> 
> I understand you wish to print propertized version of some symbol.
> 
> If it is good strategy or not, I guess you are the one to
> decide. Because it is with special background, I would insert space
> before and after, to make it more indicative or to make it more visible.
> 
> > Does the use of (unless kpact (insert " ")) do the job or am I
> > missing some possible condition that might occur?
> 
> You missed to express better what you wish to achieve.

I need some advice on how to handle optional arguments, where one either 
does not pass it or passes t.

For instance, should I put a default for it, as in (setq kpact (or kpact nil)) ?

Or handle things with a "when", or using some other combination ?

(defun ticker-hour (lampkl &optional kpact)

  (setq kpact (or kpact nil))
  (when () )
)




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

* Re: Handling optional argument with t value
  2022-12-05 20:09   ` Heime
@ 2022-12-05 23:03     ` Emanuel Berg
  0 siblings, 0 replies; 4+ messages in thread
From: Emanuel Berg @ 2022-12-05 23:03 UTC (permalink / raw)
  To: help-gnu-emacs

Heime wrote:

> I need some advice on how to handle optional arguments,
> where one either does not pass it or passes t.

Optional arguments default to nil so the default version of
the function's behavior should do its thing - the default
thing - when the optional arguments are nil.

If something that isn't default should happen, you can name
the optional argument to describe this, say INSERT, meaning
the default behavior isn't to insert, but with the optional
argument set, the non-default execution kicks in and something
is indeed inserted.

Then the Elisp will look like this:

(when insert
  (insert ...) )

And vice versa, if the default is it _should_ happen, then the
optional argument can be called NO-INSERT

(unless no-insert
  (insert ... ) )

It's up to you to decide what's default and what's
optional/non-default ...

Take a look at this ...

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/meta.el
;;
;; also see:
;;   https://dataswamp.org/~incal/emacs-init/w3m/w3m-version.el
;;
;; example:
;;   GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, cairo version 1.16.0)
;;   of 2022-05-22 [commit 714970f5967f2153bb95e35823dbd917e0e5b60b]

(defun emacs-version-commit (&optional here)
  (interactive "P")
  (let ((ver (replace-regexp-in-string " \\(of\\)" "\\1"
               (format "%s [commit %s]"
                 (emacs-version)
                 (emacs-repository-get-version) ))))
    (when here
      (insert ver) )
    (message ver) ))

(defalias 'ever #'emacs-version-commit)

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




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

end of thread, other threads:[~2022-12-05 23:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-04 23:01 Handling optional argument with t value Heime
2022-12-05 19:16 ` Jean Louis
2022-12-05 20:09   ` Heime
2022-12-05 23:03     ` Emanuel Berg

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