* elisp: can a function/defun return two strings..? prompt for two strings??
@ 2011-02-27 9:24 ken
2011-02-27 10:39 ` Andrea Crotti
0 siblings, 1 reply; 5+ messages in thread
From: ken @ 2011-02-27 9:24 UTC (permalink / raw)
To: GNU Emacs List
In one line of a file/buffer (which might exist or might not) will be
two strings, both of which must be fetched and returned to the calling
function. I was going to write two separate functions, one for each
string, but since both strings are in the same line, it seemed highly
inefficient to search for the same line of text twice... made more sense
to search for it once, and get both strings in the same function.
Here's pseudo-code:
;; search buffer for line of interest.
;; if the line exists
;; does it specify str1?
;; if it does, grab that str1, hold it for eventual return
;; if it doesn't, prompt user for it, and hold it for eventual return.
;; does the same line specify str2?
;; if it does, grab that str2, hold it for eventual return
;; if it doesn't, prompt user for it, and hold it for eventual return.
;; if the line doesn't exist,
;; prompt user for str1 and str2
;; create/insert new line in buffer, inserting str1 & str2 into it.
;; return str1 and str2 to calling function
Most of the coding for the above will busy itself with finding the line
of interest-- or determining that it doesn't exist. So why should I do
that twice, once for each string? Sure, I could save to a variable the
location of the line of interest to avoid having to search for it again,
but then I'm back to working with two variables, the location and just
one string. So that's a non-solution.
So how to "return" two variables to a calling function, possibly have to
prompt for one or both of them? (I can think of a half dozen ways to do
this in C, but this is elisp.)
Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: elisp: can a function/defun return two strings..? prompt for two strings??
[not found] <mailman.2.1298798656.16925.help-gnu-emacs@gnu.org>
@ 2011-02-27 10:31 ` Harald Hanche-Olsen
2011-02-27 11:07 ` Pascal J. Bourguignon
2011-02-27 21:10 ` Tim X
2 siblings, 0 replies; 5+ messages in thread
From: Harald Hanche-Olsen @ 2011-02-27 10:31 UTC (permalink / raw)
To: help-gnu-emacs
[ken <gebser@mousecar.com>]
> So how to "return" two variables to a calling function, possibly have to
> prompt for one or both of them? (I can think of a half dozen ways to do
> this in C, but this is elisp.)
Since elisp doesn't have multiple return values (as Common Lisp does),
you'll have to wrap the two return values in a single item. A cons cell
or a list are the most likely choices:
(cons v1 v2) ; retreive using car and cdr
(list v1 v2) ; retreive using car and cadr
As an alternative, if most of the functions that will be using this only
need the first value, your function might just return v1 after first
assigning v2 to some variable that interested callers can access. But I
find that slightly distasteful.
--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
when there is no ground whatsoever for supposing it is true.
-- Bertrand Russell
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: elisp: can a function/defun return two strings..? prompt for two strings??
2011-02-27 9:24 elisp: can a function/defun return two strings..? prompt for two strings?? ken
@ 2011-02-27 10:39 ` Andrea Crotti
0 siblings, 0 replies; 5+ messages in thread
From: Andrea Crotti @ 2011-02-27 10:39 UTC (permalink / raw)
To: gebser; +Cc: GNU Emacs List
Il giorno 27/feb/2011, alle ore 10.24, ken ha scritto:
> In one line of a file/buffer (which might exist or might not) will be
> two strings, both of which must be fetched and returned to the calling
> function. I was going to write two separate functions, one for each
> string, but since both strings are in the same line, it seemed highly
> inefficient to search for the same line of text twice... made more sense
> to search for it once, and get both strings in the same function.
>
> Here's pseudo-code:
>
> ;; search buffer for line of interest.
> ;; if the line exists
> ;; does it specify str1?
> ;; if it does, grab that str1, hold it for eventual return
> ;; if it doesn't, prompt user for it, and hold it for eventual return.
> ;; does the same line specify str2?
> ;; if it does, grab that str2, hold it for eventual return
> ;; if it doesn't, prompt user for it, and hold it for eventual return.
> ;; if the line doesn't exist,
> ;; prompt user for str1 and str2
> ;; create/insert new line in buffer, inserting str1 & str2 into it.
> ;; return str1 and str2 to calling function
The easiest way in my opinion is just to return a list, and append to it what you get.
It looks quite long your procedure to look for strings anyway, I think that if you
use regular expressions it will be much much faster.
If you write exactly the input and output that you expect maybe it will be easier to help
;; pseudocode
(let ((result ()))
(if .... (add-to-list ... result)
...
result)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: elisp: can a function/defun return two strings..? prompt for two strings??
[not found] <mailman.2.1298798656.16925.help-gnu-emacs@gnu.org>
2011-02-27 10:31 ` Harald Hanche-Olsen
@ 2011-02-27 11:07 ` Pascal J. Bourguignon
2011-02-27 21:10 ` Tim X
2 siblings, 0 replies; 5+ messages in thread
From: Pascal J. Bourguignon @ 2011-02-27 11:07 UTC (permalink / raw)
To: help-gnu-emacs
ken <gebser@mousecar.com> writes:
> In one line of a file/buffer (which might exist or might not) will be
> two strings, both of which must be fetched and returned to the calling
> function. I was going to write two separate functions, one for each
> string, but since both strings are in the same line, it seemed highly
> inefficient to search for the same line of text twice... made more sense
> to search for it once, and get both strings in the same function.
>
> Here's pseudo-code:
(defun pseudo-code ()
> ;; search buffer for line of interest.
(let ((line-of-interest (search-buffer-for-it)))
> ;; if the line exists
(if (line-exists-p line-of-interest)
(list
> ;; does it specify str1?
(if (line-specify-str1-p line-of-interest)
> ;; if it does, grab that str1, hold it for eventual return
(grab-str1 line-of-interest)
> ;; if it doesn't, prompt user for it, and hold it for eventual
> return.
(read-from-minibuffer "prompt for str1"))
> ;; does the same line specify str2?
(if (line-specify-str2-p line-of-interest)
> ;; if it does, grab that str2, hold it for eventual return
(grab-str2 line-of-interest)
> ;; if it doesn't, prompt user for it, and hold it for eventual
> return.
(read-from-minibuffer "prompt for str2")))
> ;; if the line doesn't exist,
> ;; prompt user for str1 and str2
(let ((str1 (read-from-minibuffer "prompt for str1"))
(str2 (read-from-minibuffer "prompt for str2")))
(insert "%s %s\n" str1 str2)
(list str1 str2)))))
> ;; create/insert new line in buffer, inserting str1 & str2 into it.
> ;; return str1 and str2 to calling function
Now you just need to implement the missing functions used above...
> Most of the coding for the above will busy itself with finding the line
> of interest-- or determining that it doesn't exist.
I doubt so. Clearly, the coding above busies itself with something
else, as the pseudo code clearly indicates.
> So why should I do
> that twice, once for each string? Sure, I could save to a variable the
> location of the line of interest to avoid having to search for it again,
> but then I'm back to working with two variables, the location and just
> one string. So that's a non-solution.
What are you talking about???
> So how to "return" two variables to a calling function, possibly have to
> prompt for one or both of them? (I can think of a half dozen ways to do
> this in C, but this is elisp.)
You cannot return variables, only values, resulting from the evaluation
of expressions.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: elisp: can a function/defun return two strings..? prompt for two strings??
[not found] <mailman.2.1298798656.16925.help-gnu-emacs@gnu.org>
2011-02-27 10:31 ` Harald Hanche-Olsen
2011-02-27 11:07 ` Pascal J. Bourguignon
@ 2011-02-27 21:10 ` Tim X
2 siblings, 0 replies; 5+ messages in thread
From: Tim X @ 2011-02-27 21:10 UTC (permalink / raw)
To: help-gnu-emacs
ken <gebser@mousecar.com> writes:
> In one line of a file/buffer (which might exist or might not) will be
> two strings, both of which must be fetched and returned to the calling
> function. I was going to write two separate functions, one for each
> string, but since both strings are in the same line, it seemed highly
> inefficient to search for the same line of text twice... made more sense
> to search for it once, and get both strings in the same function.
>
> Here's pseudo-code:
>
> ;; search buffer for line of interest.
> ;; if the line exists
> ;; does it specify str1?
> ;; if it does, grab that str1, hold it for eventual return
> ;; if it doesn't, prompt user for it, and hold it for eventual return.
> ;; does the same line specify str2?
> ;; if it does, grab that str2, hold it for eventual return
> ;; if it doesn't, prompt user for it, and hold it for eventual return.
> ;; if the line doesn't exist,
> ;; prompt user for str1 and str2
> ;; create/insert new line in buffer, inserting str1 & str2 into it.
> ;; return str1 and str2 to calling function
>
> Most of the coding for the above will busy itself with finding the line
> of interest-- or determining that it doesn't exist. So why should I do
> that twice, once for each string? Sure, I could save to a variable the
> location of the line of interest to avoid having to search for it again,
> but then I'm back to working with two variables, the location and just
> one string. So that's a non-solution.
>
> So how to "return" two variables to a calling function, possibly have to
> prompt for one or both of them? (I can think of a half dozen ways to do
> this in C, but this is elisp.)
>
>
Just return the strings as a list of two strings i.e.
(list str1 str2)
Tim
--
tcross (at) rapttech dot com dot au
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-02-27 21:10 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-27 9:24 elisp: can a function/defun return two strings..? prompt for two strings?? ken
2011-02-27 10:39 ` Andrea Crotti
[not found] <mailman.2.1298798656.16925.help-gnu-emacs@gnu.org>
2011-02-27 10:31 ` Harald Hanche-Olsen
2011-02-27 11:07 ` Pascal J. Bourguignon
2011-02-27 21:10 ` Tim X
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).