[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: Chunyang Xu
Subject: Re: [el-search] How to search string excluding docstring?
Date: Wed, 27 Dec 2017 13:38:43 +0800
User-agent: mu4e 0.9.18; emacs 27.0.50

Emanuel Berg writes:

> However here there is a collision between the
> two worlds. Because where is point?
> Over which element to begin with, when the
> query is executed? If one knows that, one could
> simply get the defun as a list (which it
> already is) and ask, "is the 4th element
> a string?". If it is, and if point's at it in
> the buffer, then yes it is a docstring.

I think point is at the beginning of the string, the following assumes
this to work. I compare point with the point of docstring (if any) to
see if it is a docstring.

#+begin_src emacs-lisp
;; FIXME: Update this list every time el-search starts. How?
(defvar el-search--symbols-with-doc-string
  (let (symbols)
     (lambda (sym)
       (and (fboundp sym)
            (get sym 'doc-string-elt)
            (push sym symbols))))
  "A list of symbols which support doc-string.")

(defun el-search--doc-string-p ()
  "Return t if point is at docstring."
  (pcase (save-excursion
           (read (current-buffer)))
    (`(,(and symbol (guard (memq symbol el-search--symbols-with-doc-string)))
       ,_ . ,_)
     (let ((op (point)))
           (forward-sexp (1+ (get symbol 'doc-string-elt)))
           (= op (point))))))))

(el-search-defpattern doc-string (&rest regexps)
  "Match any documentation string that is matched by all REGEXPS."
  `(and (string ,@regexps) (guard (el-search--doc-string-p))))

(el-search-defpattern s (&rest regexps)
  "Match any string (excluding doc string) that is matched by all REGEXPS"
  `(and (string ,@regexps) (guard (not (el-search--doc-string-p)))))

reply via email to

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