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

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

Re: replacing a function with another one


From: Michael Heerdegen
Subject: Re: replacing a function with another one
Date: Thu, 13 Mar 2014 08:19:12 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

lee <address@hidden> writes:

> So what is that supposed to mean? `let' keeps driving me insane already
> because it requires so many brackets.  Then finally, I do something like
>
>
>     (let ((end-marker (concat "^" comment-start 
> lsl-hi-lock-patterns-end-marker))
>       ((marker-pos (re-search-forward end-marker (point-max) t))))
>       (do-stuff))
>
>
> and it doesn´t work because end-marker is undefined despite I just
> defined it :(  So I have
>
>
>     (let ((end-marker (concat "^" comment-start 
> lsl-hi-lock-patterns-end-marker)))
>       (let ((marker-pos (re-search-forward end-marker (point-max) t)))
>       (do-something)))
>

The functionality you want, and that you reached with the nested `let',
is exactly what `let*' does.  When I was learning LISP, I was also
confused about `let'.  Standard `let' first evaluates all
expressions, then binds the variables, so in works "parallel" - see

  (info "(elisp) Local Variables")

> And in the end, I´m left with the unanswerable question of how to
> intentionally returning something in particular from a function:
>
>
> (defun lsl-get-patterns-from-file (file)
>   "Read hi-lock-mode highlighting-patterns from a file and return
> the patterns read."
>   (with-current-buffer
>       (find-file-noselect file)
>     (goto-char (point-min))
>     (let ((end-marker (concat "^" comment-start 
> lsl-hi-lock-patterns-end-marker)))
>       (let ((marker-pos (re-search-forward end-marker (point-max) t)))
>       (when marker-pos
>         (goto-char marker-pos)
>         (previous-line)
>         (end-of-line)
>         (setq marker-pos (point))
>         (goto-char (point-min))
>         (message "reading hi-lock patterns from %s (%d..%d)"
>                  (buffer-name)
>                  (point-min) marker-pos)
>         (let ((patterns nil))
>           (while (< (point) marker-pos)
>             (setq patterns (append (read (current-buffer)) patterns)))
>           (setq patterns patterns)))))))
>
>
> I need this function to return `patterns'.  Without the last line, it
> seems to return nil because the setq is enclosed in the while.

`let' returns the value returned by the last body expression (in
particular, that isn't necessarily the value of the variable `patterns'
in your example!).  And `while' always returns nil (strictly speaking,
the return value isn't documented, so don't rely on it at all).  So this
is what you want:

          (let ((patterns nil))
            (while (< (point) marker-pos)
              (setq patterns (append (read (current-buffer)) patterns)))
            patterns)



Michael.




reply via email to

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