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

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

Re: Handling defaults values for both interactive and non-interactive us


From: Heime
Subject: Re: Handling defaults values for both interactive and non-interactive use
Date: Wed, 11 Sep 2024 00:23:10 +0000

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 via email to

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