From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Visuwesh Newsgroups: gmane.emacs.bugs Subject: bug#70820: [PATCH] Editable grep buffers Date: Wed, 08 May 2024 10:41:10 +0530 Message-ID: <87cypwlwgh.fsf@gmail.com> References: <87seytlhcq.fsf@gmail.com> <87o79hkndt.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7987"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 70820@debbugs.gnu.org To: Jim Porter Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed May 08 07:12:01 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s4Zay-0001tY-2R for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 08 May 2024 07:12:00 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4Zab-000888-Fn; Wed, 08 May 2024 01:11:37 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s4Zaa-00087w-8Z for bug-gnu-emacs@gnu.org; Wed, 08 May 2024 01:11:36 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s4ZaZ-0004UM-IC for bug-gnu-emacs@gnu.org; Wed, 08 May 2024 01:11:35 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1s4Zay-0001Ak-NE for bug-gnu-emacs@gnu.org; Wed, 08 May 2024 01:12:00 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Visuwesh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 08 May 2024 05:12:00 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70820 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 70820-submit@debbugs.gnu.org id=B70820.17151451094355 (code B ref 70820); Wed, 08 May 2024 05:12:00 +0000 Original-Received: (at 70820) by debbugs.gnu.org; 8 May 2024 05:11:49 +0000 Original-Received: from localhost ([127.0.0.1]:46814 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s4Zam-00018B-PL for submit@debbugs.gnu.org; Wed, 08 May 2024 01:11:49 -0400 Original-Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]:48352) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s4Zak-00017x-AM for 70820@debbugs.gnu.org; Wed, 08 May 2024 01:11:47 -0400 Original-Received: by mail-pj1-x1044.google.com with SMTP id 98e67ed59e1d1-2b33e342c03so3415606a91.0 for <70820@debbugs.gnu.org>; Tue, 07 May 2024 22:11:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715145074; x=1715749874; darn=debbugs.gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=OURMamFewAP4qDWZveJVugL2RB6IYxH//XM2cyorinQ=; b=UfoYCS0FSOWQgQC5UckiQ7T7ziioCG64rCfubc0Rw3Ssx0HfiYSmXjGW8ENwr6yfQL 0hFuKvclSrV3658My5rRMyEvoL5H48rhmFnRhE7IezC7NcYizJWrsgCsrBbV27rVPBEu fdCWq4WojDKNHyhYQKAlYZvUGMK8LY7qSbLInQCi9c/8I1/+Fu0jdFc17kqLUiO6+TRx eEwGYM18lPtNi0kf33kzolOUPMV2R5HI4PCJAggSYMDG4Wg4lC5Mt9jjkK43AObW151L j8IuZr41grATAOablp8NIQAlvXpISRWXDQQJLy8DvZgVN+6GioG5Cd6jVbUEfGovOiyl qzEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715145074; x=1715749874; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=OURMamFewAP4qDWZveJVugL2RB6IYxH//XM2cyorinQ=; b=fmZ4xkUEWClY2yg0eGpZbgsLAHI67pwuiVK+lzIHdhj+WtDYA5ragly2vXSPiR8oON Ohhj3ykJNOmhMOKv0JWXvbWe2K+Azsrty8mASksw+Q7orC8z9zZ7GxKHxfMNjr4Briq8 Yt/yapaAMUZ3Mc4aYrZwhx5rIS1iNv7dRqDF/dAISsli0adlr0SocAFroUEIppMVCv31 HarZQIXwtsde6IwXTVn8s69HXlKC0FyPGPy0WIX/i0sfOIcxN9JdGsjMw47K2Ho4jQPO KbqPxPh3AKAja2Xtoo1Q2c4VXNLC9CLKqwNIcVw5FWY7rb3Cvq1dXmkOpwZV7OKjcBdy bE9A== X-Gm-Message-State: AOJu0Yz+LMRjdGVCwlMO/mqrRVq11C/RHzuOFzRy9bbNWTrjJSgPWgbq xnQD1l2r2J4bshzEY26NCZG9Je56tikqZfB8HpkuuITQahsPY/ST X-Google-Smtp-Source: AGHT+IE5efjlwxt24/PG6Irq+CFeV0aASJ+pU0bxr+hDWwDzOjp0tvNfdUFF46VA6UEXEMh5J6yzLA== X-Received: by 2002:a17:90a:a90:b0:2a4:e9d:9888 with SMTP id 98e67ed59e1d1-2b6165a3afdmr1550641a91.16.1715145073551; Tue, 07 May 2024 22:11:13 -0700 (PDT) Original-Received: from localhost ([49.204.130.239]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2b62863cd9dsm465127a91.12.2024.05.07.22.11.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 22:11:12 -0700 (PDT) In-Reply-To: (Jim Porter's message of "Tue, 7 May 2024 21:11:42 -0700") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:284669 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable [=E0=AE=9A=E0=AF=86=E0=AE=B5=E0=AF=8D=E0=AE=B5=E0=AE=BE=E0=AE=AF=E0=AF=8D = =E0=AE=AE=E0=AF=87 07, 2024] Jim Porter wrote: > On 5/7/2024 8:12 PM, Visuwesh wrote: >> Thanks for your input. If I missed something from wgrep that you use, >> please let me known. I will play around with wgrep in some time to >> learn more about what it offers myself. > > In addition to the things you mentioned, here are the most important > features from wgrep for me: Thanks. > * Mark all non-result parts of the buffer (the command string, file > names, line numbers, etc) as read-only. This is especially valuable > if you want to use 'query-replace' or similar to modify the > results. Then you can't inadvertently edit those bits. This is now done. > * Fontify any results with changes (both in the grep-mode buffer and > the original files). This is really useful for being able to see > what I've changed. I agree that it would be nice to have this in *grep* buffer but I would like to avoid editing the file on-the-fly since it introduces typing lag IME with occur-edit-mode. If you want to know the edits that were made, you can use diff-buffer-with-file so I hope leaving out the highlighting in the file would be okay. > * Adding all the necessary hooks/functions so other grep-like modes > can use this. For example, see this file from wgrep, which lets you > use wgrep with ag.el: > . = (This > matters to me partly because I'm the author of Urgrep - a "universal > recursive grep" mode that can use any grep-like program to do > searches. I've added my own support in Urgrep for wgrep.) With grep-edit-minor-mode, as long as the compilation-message text-property is present at the beginning of the line, you do not need to do anything extra. I look at this to gain the information about the filename and the line number. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=grep-edit.diff diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index 657349cbdff..517d1bd5ecd 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -31,6 +31,7 @@ (eval-when-compile (require 'cl-lib)) (require 'compile) +(require 'track-changes) (defgroup grep nil "Run `grep' and display the results." @@ -295,6 +296,8 @@ grep-mode-map (define-key map "}" #'compilation-next-file) (define-key map "\t" #'compilation-next-error) (define-key map [backtab] #'compilation-previous-error) + + (define-key map "e" #'grep-edit-minor-mode) map) "Keymap for grep buffers. `compilation-minor-mode-map' is a cdr of this.") @@ -1029,6 +1032,86 @@ grep command-args) #'grep-mode)) +(defvar-local grep-edit--tracker nil + "ID of the `track-changes' tracker.") + +(defun grep-edit--track-changes-signal (_id &optional _distance) + ;; Empty because we want to simply accumulate the changes at end + ;; when the user calls the finish function. + nil) + +(defun grep-edit--track-changes-finalise (beg end _before) + (let ((grep-buf (current-buffer)) + (cur-res beg)) ; Point at current grep result. + (save-excursion + (while (<= cur-res end) + (goto-char cur-res) + (let* ((change-beg (next-single-char-property-change (pos-bol) 'compilation-message)) + ;; `1-' is required to ignore the newline. + (change-end (1- (next-single-char-property-change (pos-eol) 'compilation-message))) + (loc (compilation--message->loc + (get-text-property (pos-bol) 'compilation-message))) + (line (compilation--loc->line loc)) + (file (caar (compilation--loc->file-struct loc)))) + (with-current-buffer (find-file-noselect file) + (save-excursion + (goto-char (point-min)) + (forward-line (1- line)) + (delete-region (pos-bol) (pos-eol)) + (insert-buffer-substring-no-properties grep-buf change-beg change-end)))) + (setq cur-res (next-single-property-change cur-res 'compilation-message)))))) + +(defun grep-edit--mark-read-only () + "Mark annotations and info regions read-only." + (save-excursion + (goto-char (point-min)) + (let ((inhibit-read-only t) + match) + (while (setq match (text-property-search-forward 'compilation-annotation)) + (add-text-properties (prop-match-beginning match) (prop-match-end match) + '(read-only t))) + (goto-char (point-min)) + (while (setq match (text-property-search-forward 'compilation-message)) + (add-text-properties (prop-match-beginning match) (prop-match-end match) + '(read-only t)))))) + +(define-minor-mode grep-edit-minor-mode + "Minor mode for editing *grep* buffers. +In this mode, changes to the *grep* buffer are applied to the +originating files upon saving using \\[grep-save-changes]." + :lighter " Grep-Edit" + (if (null grep-edit-minor-mode) + (progn + (setq buffer-read-only t) + (buffer-disable-undo) + (use-local-map grep-mode-map)) + (grep-edit--mark-read-only) + (unless grep-edit--tracker + (use-local-map grep-edit-minor-mode-map) + (setq buffer-read-only nil) + (buffer-enable-undo) + (setq grep-edit--tracker + (track-changes-register #'grep-edit--track-changes-signal + :disjoint t))) + (message (substitute-command-keys + "Editing: \\[grep-edit-save-changes] to return to Grep mode.")))) + +(defun grep-edit-save-changes () + "Save the changes made to the *grep* buffer." + (interactive) + (when (and grep-edit-minor-mode grep-edit--tracker) + (track-changes-fetch grep-edit--tracker #'grep-edit--track-changes-finalise) + (message "Applied edits, switching to Grep mode.") + (track-changes-unregister grep-edit--tracker) + (setq grep-edit--tracker nil) + (grep-edit-minor-mode -1))) + +(defvar grep-edit-minor-mode-map + (let ((map (make-sparse-keymap))) + (set-keymap-parent map text-mode-map) + (define-key map (kbd "C-c C-c") #'grep-edit-save-changes) + map) + "Keymap for `grep-edit-minor-mode'.") ;;;###autoload (defun grep-find (command-args) --=-=-=--