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

[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: Wed, 27 Dec 2017 14:58:21 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Chunyang Xu <mail@xuchunyang.me> writes:

> ;; FIXME: Update this list every time el-search starts. How?

For efficiency reasons I use in el-search a primitive type of caches
that know when they need to refresh themselves.  In this situation, this
would look like

#+begin_src emacs-lisp
;; -*- lexical-binding: t -*-

(defun el-search--documented-function-p (name)
  (gethash name (el-search--get-documented-function-table)))

(let ((helper (el-search-with-short-term-memory
               (lambda (_load-history)
                 (message "Recomputing table of documented functions...")
                 (let ((table (make-hash-table)))
                   (mapatoms
                    (lambda (sym)
                      (and (fboundp sym)
                           (get sym 'doc-string-elt)
                           (puthash sym t table))))
                   table)))))
  (defun el-search--get-documented-function-table ()
    (funcall helper load-history)))
#+end_src

The cache is a hash-table (faster lookup) that refreshes when queried
and `load-history' changed.  This is not optimal when you evaluate
definitions by hand, of course.  I could provide a
`el-search-last-search-start-time' for that purpose, or a
`el-search-start-hook' - though, I don't really want hooks to be used to
implement pattern matching.


FWIW, if you happen to want this (hack!), with a similar technique you
can let el-search load searched buffers or files by side effect:

#+begin_src emacs-lisp
(el-search-defpattern load ()
  "Match anything, load current buffer or file as side effect.
Any buffer or file is loaded at most once.

This is useful only in rare cases.  Use with caution!!!"
  (declare (heuristic-matcher #'el-search--load-matcher))
  (let ((load-matcher (el-search--load-matcher)))
    `(guard (funcall ',load-matcher (current-buffer) nil))))

(defun el-search--load-matcher ()
  (let ((test (el-search-with-short-term-memory
               (lambda (file-name-or-buffer)
                 (when-let ((file (if (bufferp file-name-or-buffer)
                                      (buffer-file-name file-name-or-buffer)
                                    file-name-or-buffer)))
                   (with-demoted-errors "Error: %S" (load file)))))))
    (lambda (file-name-or-buffer _) (funcall test file-name-or-buffer))))
#+end_src


HTH,

Michael.



reply via email to

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