all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Heime <heimeborgia@protonmail.com>
To: Heime <heimeborgia@protonmail.com>
Cc: Heime via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>
Subject: Re: Handling defaults values for both interactive and non-interactive use
Date: Wed, 11 Sep 2024 00:23:10 +0000	[thread overview]
Message-ID: <jS0yo48KpC9SkMuPWhZK8Okpxt7e-yoqAdCdyhMZmMHumXwNWrr6JBjWSeP_ukURQEJF1kzgX8xTnx8Znwiyn7_wVJk9RmOde4Dd91juLiA=@protonmail.com> (raw)
In-Reply-To: <B_k6Na4oLqefywsWr927rlmXlL5Eis4C2yR2_47KhR9lKaHaNKP7F7UOJa8NopVHpJ1or_jye39XKT9ZO4Oj740qyQ8-RgSATVcUc0Veck4=@protonmail.com>


On Wednesday, September 11th, 2024 at 5:29 AM, Heime <heimeborgia@protonmail.com> wrote:
 
> Sent with Proton Mail secure email.
> 
> On Tuesday, September 10th, 2024 at 3:37 PM, Heime heimeborgia@protonmail.com wrote:
> 
> > How is one to handle setting defaults for both interactive
> > and non-interactive use, properly ?
> > 
> > (defun xiakos-context (search-text &optional n bfrn)
> > 
> > (setq n (or n 8))
> > (setq bfrn (or bfrn "Context"))
> > 
> > (interactive
> > (list
> > (read-string " Search Text: ")
> > (read-number " Number of Context Lines: ")
> > (read-string " Buffer Name: ")))
> > 
> > (if (string-match-p "^\\s-*$" bfrn) "Context")
> 
> 
> Would one handle the default non-interactive and default
> interactive separately ? Or does one handle the defaults
> after having parsed the non-interactive or the interactive
> part ? In summary, would the default values be handled
> after the interactive clause ? Or does one set the default
> for the interactive part inside the interactive clause ?

I decided that the best strategy would be to set the default 
for the interactive case within the interactive clause.
For the non-interactive case, I check if the optional arguments 
are nil and set default values accordingly after the interactive 
clause.

Here is how I structured it

(defun xiakos-context (search-text &optional n bfrn)

  "Search for SEARCH-TEXT and show context of N lines in buffer BFRN."

  (interactive

   (list

    (read-string "Search Text: ")

    ;; If the user does not provide a number of context lines, default to 8

    (let ((context-lines (read-number "Number of Context Lines (default 8): ")))

      (if (zerop context-lines) 8 context-lines))

    ;; If the user does not provide a buffer name, default to 'Context'

    (let ((buffer-name (read-string "Buffer Name (default Context): ")))

      (if (string= buffer-name "") "Context" buffer-name))))

  ;; -----------------------------------------------------------------------  
  ;; Handle defaults for non-interactive use

  ;; Default to 8 context lines if n is nil

  (setq n (or n 8))

  ;; Default to "Context" if bfrn is nil or empty

  (setq bfrn (if (or (not bfrn) (string= bfrn "")) "Context" bfrn))

  ;; The rest of the function logic
  (message "Search Text: %s, Context Lines: %d, Buffer: %s" search-text n bfrn))


Should one check strings like this for the interactive case ?

   (if (string-match-p "^\\s-*$" bfrn) "Context")






      reply	other threads:[~2024-09-11  0:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-10  3:37 Handling defaults values for both interactive and non-interactive use Heime
2024-09-10 17:29 ` Heime
2024-09-11  0:23   ` Heime [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='jS0yo48KpC9SkMuPWhZK8Okpxt7e-yoqAdCdyhMZmMHumXwNWrr6JBjWSeP_ukURQEJF1kzgX8xTnx8Znwiyn7_wVJk9RmOde4Dd91juLiA=@protonmail.com' \
    --to=heimeborgia@protonmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.