Subject: bug#30503: 27.0.50; allow hiding M-x grep command line

From: [email-masked] (Charles A. Roelli)
Date: Wed, 21 Feb 2018 21:30:40 +0100

> From: Juri Linkov <[email-masked]>
> Organization: LINKOV.NET
> Date: Tue, 20 Feb 2018 22:54:34 +0200
> 
> retitle 30503 allow hiding M-x rgrep/lgrep/zrgrep command line
> thanks
> 
> > The first thing you see after M-x grep is a 1382-character line like
> > this:
> >
> > find . -type d \( -path \*/SCCS -o -path \*/RCS -o -path \*/CVS -o -path
> > \*/MCVS -o -path \*/.src -o -path \*/.svn -o -path \*/.git -o -path
> > \*/.hg -o -path \*/.bzr -o -path \*/_MTN -o -path \*/_darcs -o -path
> > \*/\{arch\} \) -prune -o \! -type d \( -name .\#\* -o -name \*.o -o -name
> > \*\~ -o -name \*.bin -o -name \*.lbin -o -name \*.so -o -name \*.a -o -name
> > \*.ln -o -name \*.blg -o -name \*.bbl -o -name \*.elc -o -name
> > \*.lof -o -name \*.glo -o -name \*.idx -o -name \*.lot -o -name
> > \*.fmt -o -name \*.tfm -o -name \*.class -o -name \*.fas -o -name
> > \*.lib -o -name \*.mem -o -name \*.x86f -o -name \*.sparcf -o -name
> > \*.dfsl -o -name \*.pfsl -o -name \*.d64fsl -o -name \*.p64fsl -o -name
> > \*.lx64fsl -o -name \*.lx32fsl -o -name \*.dx64fsl -o -name
> > \*.dx32fsl -o -name \*.fx64fsl -o -name \*.fx32fsl -o -name
> > \*.sx64fsl -o -name \*.sx32fsl -o -name \*.wx64fsl -o -name
> > \*.wx32fsl -o -name \*.fasl -o -name \*.ufsl -o -name \*.fsl -o -name
> > \*.dxl -o -name \*.lo -o -name \*.la -o -name \*.gmo -o
> >   -name \*.mo -o -name \*.toc -o -name \*.aux -o -name \*.cp -o -name
> > \*.fn -o -name \*.ky -o -name \*.pg -o -name \*.tp -o -name \*.vr -o -name
> > \*.cps -o -name \*.fns -o -name \*.kys -o -name \*.pgs -o -name
> > \*.tps -o -name \*.vrs -o -name \*.pyc -o -name \*.pyo \) -prune -o -type
> > f \( -iname \* -o -iname .\[\!.\]\* -o -iname ..\?\* \) -exec
> > grep --color -nH --null -e Emacs \{\} +
> >
> > Could we provide an option to hide the barrage of ignored directories
> > and files?  It might also be worth adding a text button or keybinding
> > to toggle their visibility interactively.
> 
> Yes, this is a real problem.  Even though I set truncate-lines to t,
> often there is a need to see grep switches at the end of the
> command line.  So there is a patch to hide uninteresting parts
> under a button like is used to hide part of output by
> elisp-last-sexp-toggle-display.  It supports rgrep, lgrep and zrgrep.
>
> 
> diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
> index 14e251e..34a9b2f 100644
> --- a/lisp/progmodes/grep.el
> +++ b/lisp/progmodes/grep.el
> @@ -452,7 +452,13 @@ grep-mode-font-lock-keywords
>       ;; "filename=linenumber=" for lines with function names in "git grep -p".
>       ("^.+?\\([-=\0]\\)[0-9]+\\([-=]\\).*\n" (0 grep-context-face)
>        (1 (if (eq (char-after (match-beginning 1)) ?\0)
> -             `(face nil display ,(match-string 2))))))
> +             `(face nil display ,(match-string 2)))))
> +     ;; Hide excessive part of the command from rgrep
> +     ("^find \\(\\. -type d .*\\\\)\\)"
> +      (1 (rgrep-command-hide)))
> +     ;; Hide excessive part of the command from lgrep
> +     ("^grep \\( *--exclude.*--exclude[^ ]+\\)"
> +      (1 (rgrep-command-hide))))
>     "Additional things to highlight in grep output.
>  This gets tacked on the end of the generated expressions.")
>  
> @@ -1166,6 +1173,32 @@ rgrep-default-command
>                   (shell-quote-argument ")")
>                   " -prune -o ")))))
>  
> +(defun rgrep-command-hide ()
> +  (let ((map (make-sparse-keymap)))
> +    (define-key map [down-mouse-2] 'mouse-set-point)
> +    (define-key map [mouse-2] 'rgrep-command-show)
> +    (define-key map "\C-m" 'rgrep-command-show)
> +    `(face nil display "[...]" mouse-face highlight
> +	   help-echo "RET, mouse-2: toggle truncated command"
> +	   keymap ,map)))
> +
> +(defun rgrep-command-show ()
> +  (interactive)
> +  (when (get-text-property (point) 'display)
> +    (let ((beg (or (previous-single-property-change
> +                    (min (point-max) (1+ (point))) 'display)
> +                   (point)))
> +          (end (or (next-single-property-change
> +                    (point) 'display)
> +                   (point)))
> +          (inhibit-modification-hooks t)
> +          (inhibit-read-only t)
> +	  (buffer-undo-list t)
> +	  (modified (buffer-modified-p)))
> +      (remove-list-of-text-properties
> +       beg end '(display help-echo mouse-face help-echo keymap))
> +      (set-buffer-modified-p modified))))
> +
>  ;;;###autoload
>  (defun zrgrep (regexp &optional files dir confirm template)
>    "Recursively grep for REGEXP in gzipped FILES in tree rooted at DIR.

Thanks, this is a great start.

I iterated on your patch, with a few changes:

- Change naming to start with `grep' instead of `rgrep' (the position
  of the functions may still have to change within the file).
- Use `with-silent-modifications'.
- Provide a toggling command for hiding/showing the shortened command.

What still remains to be added is the doc, a possible keybinding/menu
item, and a customization variable that determines whether the hiding
is done immediately for every rgrep/lgrep/rzgrep buffer.  And I think
there's some room for error in my change to the beg/end let-bindings
in grep-toggle-shortened-command (e.g. if beg ends up being nil), so
I'll fix that eventually as well.

diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index 14e251e..6699787 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -452,7 +452,13 @@ grep-mode-font-lock-keywords
      ;; "filename=linenumber=" for lines with function names in "git grep -p".
      ("^.+?\\([-=\0]\\)[0-9]+\\([-=]\\).*\n" (0 grep-context-face)
       (1 (if (eq (char-after (match-beginning 1)) ?\0)
-             `(face nil display ,(match-string 2))))))
+             `(face nil display ,(match-string 2)))))
+     ;; Hide excessive part of the command from rgrep
+     ("^find \\(\\. -type d .*\\\\)\\)"
+      (1 (grep-shortened-command-properties)))
+     ;; Hide excessive part of the command from lgrep
+     ("^grep \\( *--exclude.*--exclude[^ ]+\\)"
+      (1 (grep-shortened-command-properties))))
    "Additional things to highlight in grep output.
 This gets tacked on the end of the generated expressions.")
 
@@ -1166,6 +1172,30 @@ rgrep-default-command
                  (shell-quote-argument ")")
                  " -prune -o ")))))
 
+(defun grep-shortened-command-properties ()
+  "Return a list of text properties applied to verbose command arguments."
+  (let ((map (make-sparse-keymap)))
+    (define-key map [down-mouse-2] 'mouse-set-point)
+    (define-key map [mouse-2] 'grep-toggle-shortened-command)
+    (define-key map "\C-m" 'grep-toggle-shortened-command)
+    `(face nil display "[...]" mouse-face highlight
+	   help-echo "RET, mouse-2: toggle shortened command"
+	   keymap ,map
+	   shortened-command t)))
+
+(defun grep-toggle-shortened-command ()
+  "Toggle the display of a shortened command in `grep-mode' buffers."
+  (interactive)
+  (with-silent-modifications
+    (let* ((beg (next-single-property-change
+		 (point-min) 'shortened-command))
+	   (end (next-single-property-change
+		 beg 'shortened-command)))
+      (if (get-text-property beg 'display)
+	  (remove-list-of-text-properties
+	   beg end '(display help-echo mouse-face help-echo keymap))
+	(add-text-properties beg end (grep-shortened-command-properties))))))
+
 ;;;###autoload
 (defun zrgrep (regexp &optional files dir confirm template)
   "Recursively grep for REGEXP in gzipped FILES in tree rooted at DIR.