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

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

Re: elisp: can a function/defun return two strings..? prompt for two str


From: Pascal J. Bourguignon
Subject: Re: elisp: can a function/defun return two strings..? prompt for two strings??
Date: Sun, 27 Feb 2011 12:07:48 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

ken <address@hidden> 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 {}.


reply via email to

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