[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [External] : Re: result of completing-read contradicting require-mat
From: |
Jean Louis |
Subject: |
Re: [External] : Re: result of completing-read contradicting require-match |
Date: |
Sun, 10 Jul 2022 20:56:45 +0300 |
User-agent: |
Mutt/+ () (2022-06-11) |
* Drew Adams <drew.adams@oracle.com> [2022-07-10 17:36]:
> > I would like to merge these two functions and use each of them
> > sometimes singly. The third merged one would check for both values.
>
> I'm not following this thread. Just happened to
> read your initial statement, which sounds like
> you want to repeat invoking a function until it
> returns nil or non-nil.
>
> If so, Emacs already gives you that:
> `run-hook-with-args-until-failure'
> (`*-success' is similar).
>
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Running-Hooks.html
Looks similar, but not that it works straight how I think it
should. It runs the hook until NIL or non-NIL, and there is no
reliability what it will return. It also has purpose to run eventually
many functions, not just one.
It is not same as what I use. These functions will return the
value from function that is invoked repeatedly until it gives
some value.
I define "nothing" as non-empty string and not null
(defun rcd-is-nothing-p (thing)
"Return TRUE if THING is nothing."
(cond ((and (stringp thing) (seq-empty-p thing)) t)
((null thing) t)
(t nil)))
(defun rcd-repeat-until-something (function &rest args)
"Repeat FUNCTION with optional ARGS until result is something.
Result shall be non empty string or number."
(let ((result))
(while (rcd-is-nothing-p (setq result (apply function args))))
result))
I use it when for example, I do not want an empty string:
(read-from-minibuffer "Tell me: ") ⇒ ""
What I do not want is empty string, I want to make sure there is some
input, so it would keep asking me until I get selection.
(rcd-repeat-until-something 'read-from-minibuffer "Tell me: ") ⇒ "OK"
Then I have for example country selection:
(rcd-repeat-until-something 'cf-country-select) ⇒ 228
- at this moment I can choose among many countries, I could type
"UNITED STAT-<TAB>" and get value 228
- but I cannot continue with ENTER in no way. And I do not need any
defaults.
That spares me programming time and evaluating values, it is less
error prone for user.
Instead of:
(let ((country (cf-country-select)))
(when country
(do something)))
or instead of:
(let ((country (cf-country-select)))
(if country
(do something)
(error "You did not select country")))
it is more convient to simply demand that selection is made and be sure of it:
(let ((country (rcd-repeat-until-something 'cf-country-select)))
(do something with country))
thus I am sparing many `when' and `if' clauses, as it ensures
that function returns "something".
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
Re: result of completing-read contradicting require-match, carlmarcos, 2022/07/02