unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#8252: Please add a function to get files in *grep*
@ 2011-03-14 21:46 Lennart Borgman
  2011-03-15  2:16 ` Stefan Monnier
  2021-07-17 14:54 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 4+ messages in thread
From: Lennart Borgman @ 2011-03-14 21:46 UTC (permalink / raw)
  To: 8252

For example something like this (which I just added to
ourcomments-util.el in nXhtml) works now:

(defun grep-grepped-file (pt)
  "Return grepped file at PT in a `grep-mode' buffer.
The returned file name is relative."
  (let* ((msg (get-text-property (point) 'compilation-message))
         (loc (when msg (compilation--message->loc msg)))
         (file (when loc (caar (compilation--loc->file-struct loc)))))
    file))





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#8252: Please add a function to get files in *grep*
  2011-03-14 21:46 bug#8252: Please add a function to get files in *grep* Lennart Borgman
@ 2011-03-15  2:16 ` Stefan Monnier
  2011-03-15  9:31   ` Lennart Borgman
  2021-07-17 14:54 ` Lars Ingebrigtsen
  1 sibling, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2011-03-15  2:16 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: 8252

Could you motivate your request?


        Stefan





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#8252: Please add a function to get files in *grep*
  2011-03-15  2:16 ` Stefan Monnier
@ 2011-03-15  9:31   ` Lennart Borgman
  0 siblings, 0 replies; 4+ messages in thread
From: Lennart Borgman @ 2011-03-15  9:31 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 8252

On Tue, Mar 15, 2011 at 3:16 AM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
> Could you motivate your request?

There can be a lot of uses for it. In my case I am using it for this:

(defun grep-get-buffer-files ()
  "Return list of files in a `grep-mode' buffer."
  (or (and (compilation-buffer-p (current-buffer))
           (derived-mode-p 'grep-mode))
      (error "Not in a grep buffer"))
  (let ((here (point))
        files)
    (font-lock-fontify-buffer)
    (goto-char (point-min))
    (while (not (eobp))
      (let ((file (grep-grepped-file (point))))
        (when file (add-to-list 'files file)))
      (forward-line))
    (goto-char here)
    ;;(message "files=%s" files)
    files))


(defun grep-query-replace(from to &optional delimited)
  "Do `query-replace-regexp' of FROM with TO, on all files in *grep*.
Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
with the command \\[tags-loop-continue].

Must be called from a `grep-mode' buffer."
  (interactive
   (let ((common
          ;; Use the regexps that have been used in grep
          (let ((query-replace-from-history-variable 'grep-regexp-history)
                (query-replace-defaults (or grep-query-replace-defaults
                                            query-replace-defaults)))
            (unless (derived-mode-p 'grep-mode) (error "This command
must be used in a grep output buffer"))
            (query-replace-read-args
             "Query replace regexp in grepped files" t t))))
     (setq grep-query-replace-defaults (cons (nth 0 common)
                                             (nth 1 common)))
     (list (nth 0 common) (nth 1 common) (nth 2 common))))
  (unless (derived-mode-p 'grep-mode) (error "This command must be
used in a grep output buffer"))
  (dolist (file (grep-get-buffer-files))
    (let ((buffer (get-file-buffer file)))
      (if (and buffer (with-current-buffer buffer
			buffer-read-only))
	  (error "File `%s' is visited read-only" file))))
  (tags-query-replace from to delimited
		      '(grep-get-buffer-files)))





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#8252: Please add a function to get files in *grep*
  2011-03-14 21:46 bug#8252: Please add a function to get files in *grep* Lennart Borgman
  2011-03-15  2:16 ` Stefan Monnier
@ 2021-07-17 14:54 ` Lars Ingebrigtsen
  1 sibling, 0 replies; 4+ messages in thread
From: Lars Ingebrigtsen @ 2021-07-17 14:54 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: 8252

Lennart Borgman <lennart.borgman@gmail.com> writes:

> For example something like this (which I just added to
> ourcomments-util.el in nXhtml) works now:
>
> (defun grep-grepped-file (pt)
>   "Return grepped file at PT in a `grep-mode' buffer.
> The returned file name is relative."
>   (let* ((msg (get-text-property (point) 'compilation-message))
>          (loc (when msg (compilation--message->loc msg)))
>          (file (when loc (caar (compilation--loc->file-struct loc)))))
>     file))

I think that sounds generally useful -- writing things on top of grep
mode is quite popular, and it isn't obvious how to get at the file name
(which is what people generally want).  So I've added your function to
Emacs 28 (with some minor tweaks) under the name `grep-file-at-point'.

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





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-17 14:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-14 21:46 bug#8252: Please add a function to get files in *grep* Lennart Borgman
2011-03-15  2:16 ` Stefan Monnier
2011-03-15  9:31   ` Lennart Borgman
2021-07-17 14:54 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).