[Top][All Lists]

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

Re: [el-search] How to search string excluding docstring?

From: Michael Heerdegen
Subject: Re: [el-search] How to search string excluding docstring?
Date: Mon, 25 Dec 2017 18:51:36 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Chunyang Xu <> writes:

> I also realize another way, which is not general at all but seeming
> working. To check if the current string is docstring
> (save-excursion
>   (backward-up-list)
>   (pcase (read (current-buffer))
>     (`(defun ,_ ,_ ,(and (pred stringp) docstring) . ,_)
>      (string= docstring string))))
> then to search docstring
>   (and (string) string (guard ...))
> It has a bug, for example, when search the following, it matches both
> the first and the second "foo" (the second is not wanted)
>   (defun hello ()
>     "foo"
>     "foo"
>     (message "foo"))

Yes, that's one of the pitfalls you can run into.  One could try to find
out at which list position the current expression is - I've not really
needed something like this, however.

> So my current approach of searching string excluding docstring is
>   (defun el-search--docstring-p (string)
>     (save-excursion
>       (backward-up-list)
>       (pcase (read (current-buffer))
>         (`(defun ,_ ,_ ,(and (pred stringp) docstring) . ,_)
>          (string= docstring string)))))
>   (and (string "foo") s (guard (not (el-search--docstring-p s))))

Yes, that should work fine (unless you have any top-level strings, but
that is never the case for code buffers).  You could simplify it a bit
by using

 (pred (string= string))

in the pattern.



reply via email to

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