Subject: bug#12443: 24.2.50; Default values in the minibuffer prompt (fix inconsisntecy)

From: Lars Ingebrigtsen <[email-masked]>
Date: Sun, 06 Sep 2020 20:10:42 +0200

Lars Ingebrigtsen <[email-masked]> writes:

> It doesn't sound that difficult to do, but I
> thought I'd ask before I start typing...

Well, whaddaya know, I started typing.  *sigh*

Anyway, this seems to actually work, and I'm amazed at how speedy an all
Emacs solution for this is: Running it over all the Lisp files on trunk
takes 3.2s (on this laptop).

Is this something Emacs should have?  It's not very...  user-friendly;
you have to write the predicate yourself, but it should allow people to
refactor with slightly more ease than with `M-x grep'.

Usage example:

  (lars-find-sexp-in-files "~/src/emacs/trunk/lisp"
			   "[.]el\\'"
			   'completing-read
			    (lambda (form)
			      (and (nth 7 form)
				   (stringp (nth 1 form)))))

(defun lars-find-sexp-in-files (directory match symbol predicate)
  (pop-to-buffer "*matches*")
  (let ((inhibit-read-only t))
    (erase-buffer)
    (dolist (file (directory-files-recursively directory match))
      (message "%s" file)
      (lars-find-sexp-in-file file symbol predicate))
    (grep-mode)))

(defun lars-find-sexp-in-file (file symbol predicate)
  (let ((lines
	 (with-temp-buffer
	   (insert-file-contents file)
	   (lars-find-sexp-in-buffer symbol predicate))))
    (dolist (line lines)
      (insert file ":" line))))

(defun lars-find-sexp-in-buffer (symbol predicate)
  (let ((lines nil))
    (while (re-search-forward (format "(%s[^-a-zA-Z0-9]" symbol) nil t)
      (let ((start (match-beginning 0)))
	(goto-char start)
	(let ((form (ignore-errors (read (current-buffer)))))
	  (when (and (eq (car form) symbol)
		     (funcall predicate form))
	    (goto-char (1+ start))
	    (push (format "%d:%s\n" (count-lines (point-min) start)
			  (buffer-substring-no-properties
			   (line-beginning-position) (line-end-position)))
		  lines)))))
    (nreverse lines)))

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no