unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Handling defaults values for both interactive and non-interactive use
@ 2024-09-10  3:37 Heime
  2024-09-10 17:29 ` Heime
  0 siblings, 1 reply; 3+ messages in thread
From: Heime @ 2024-09-10  3:37 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

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




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

* Re: Handling defaults values for both interactive and non-interactive use
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Heime @ 2024-09-10 17:29 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor






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 ?



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

* Re: Handling defaults values for both interactive and non-interactive use
  2024-09-10 17:29 ` Heime
@ 2024-09-11  0:23   ` Heime
  0 siblings, 0 replies; 3+ messages in thread
From: Heime @ 2024-09-11  0:23 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor


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






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

end of thread, other threads:[~2024-09-11  0:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).