Subject: bug#8684: 24.0.50; Grep mode trips up on Entering directory lines

From: Brent Goodrick <[email-masked]>
Date: Tue, 17 May 2011 08:52:12 -0700

This bug report will be sent to the Free Software Foundation,
not to your local site managers!
Please write in English if possible, because the Emacs maintainers
usually do not have translators to read other languages for them.

Your report will be posted to the [email-masked] mailing list
and the gnu.emacs.bug news group, and at http://debbugs.gnu.org.

Please describe exactly what actions triggered the bug
and the precise symptoms of the bug.  If you can, give
a recipe starting from `emacs -Q':

When you are searching build logs for matches using M-x grep, and the
resulting grep matches lines of the form:

  Entering directory `/some/bogus/directory/here'

And if you then click on lines _after_ the line such as the "Entering
directory" line, then you get a prompt asking about "Find this grep
hit in" which is not correct grep mode like it probably is correct in
compile mode.

For some reason, this seems to only occur for cases where the grep
buffer is saved to a file and then reloaded, but I have seen this
occur in my non-"-Q" session for my own scripts I use to run grep
(versus running grep directly), and I have not debugged it further
than this bug I am reporting now that relies upon first saving the
grep mode buffer to a file and reloading it (see below).

You can reproduce this on Emacs built from scratch by following the
following recipe that simulates it:

1. On Linux, cut and paste the following into a file called
   /tmp/demo-bug.el:

--- cut here ---
(let* ((tmpdir (expand-file-name (make-temp-name "emacs-bug-demo-dir")
temporary-file-directory))
       (fn1 (expand-file-name "somefile1.txt" tmpdir))
       (fn2 (expand-file-name "somefile2.txt" tmpdir))
       (saved-grep-file (expand-file-name "somegrep.txt" tmpdir)))
  (make-directory tmpdir t)
  (with-temp-file fn1
    (insert "# | make[3]: Entering directory `/some/bogus/directory/here'"))
  (with-temp-file fn2
    (insert "This is a line that also has the Entering word on it.
Click on this line in grep mode."))
  ;; For debugging, just wipe out all other grep buffers from the current
  ;; session:
  (mapc (lambda (buffer)
      (with-current-buffer buffer
        (when (eq major-mode 'grep-mode)
          (kill-buffer buffer))))
    (buffer-list))
  ;; Force default-directory in some buffer to be that where our temporary files
  ;; reside, e.g., fn2 is good enough:
  (with-current-buffer (progn
             (find-file fn2)
             (current-buffer))
    (grep "grep -n Entering *.txt")
    ;; Let the grep finish:
    (accept-process-output nil 1))
  (with-current-buffer (get-buffer "*grep*")
    (let ((contents (buffer-string)))
      ;; Store the grep contents into a file (without this, the error does not
      ;; reproduce ... I do not know why):
      (with-temp-file saved-grep-file
    (insert contents))
      (find-file saved-grep-file)
      (goto-char (point-min))
      (re-search-forward "Click on this line in grep mode")
      (call-interactively 'compile-goto-error))))

--- cut here ---

2. Execute the above file via this command:

   emacs -Q -l /tmp/demo-bug.el:

Notice that I call `compile-goto-error' interactively above to
simulate what you would do if you hit the RETurn key on that line. You
should expect to see a prompt for "Find this grep hit in" versus what
it should do which is drive straight to that line that contains the
"Click on this line in grep mode" string shown.

Theory of cause:

I conjecture that the fault lies in the fact that grep mode needs to
define a variable called `grep-directory-matcher' that is the analog
of `compilation-directory-matcher' which is deployed as a buffer local
variable when the grep mode is defined with its call to
`define-compilation-mode'. I have proved this by my hackaround this
bug that I have in place right now, which is this in my .emacs:

;; Require grep mode first
(require 'grep)

;; Hack in our definition of grep-directory-matcher and force grep mode to
;; use it. We have to do this after the above require of grep:
(defvar grep-directory-matcher nil)
(let* ((base (find-lisp-object-file-name 'grep (symbol-function 'grep)))
       (grep-defining-elisp-file
    (car
     (remove-if-not (lambda (fn)
              (file-exists-p fn))
            (list base
                  (concat base ".gz"))))))
  (unless grep-defining-elisp-file
    (error "ASSERTION FAILED: Could not find grep defining file in
order to hack it"))
  ;; Force the file to load which forces another evaluation of the call to
  ;; define-compilation-mode from within that file. This is the only way I could
  ;; figure out how to hack around this problem other than globally setting
  ;; compilation-directory-matcher to nil which is not right for all compilation
  ;; mode buffers in general:
  (load grep-defining-elisp-file))

Thanks,

bgoodr

If Emacs crashed, and you have the Emacs process in the gdb debugger,
please include the output from the following gdb commands:
    `bt full' and `xbacktrace'.
For information about debugging Emacs, please read the file
/home/brentg/install/Linux.x86_64/share/emacs/24.0.50/etc/DEBUG.

In GNU Emacs 24.0.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 2.20.1)
 of 2011-01-15 on hungover
Windowing system distributor `The X.Org Foundation', version 11.0.10707000
configured using `configure  '--with-x-toolkit' '--with-xft'
'--prefix=/home/brentg/install/Linux.x86_64''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: nil
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Grep

Minor modes in effect:
  tooltip-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
C-g M-x r e p o r t - e m a <tab> <return>

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Grep finished (matches found)
Mark set
Quit

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr message sendmail rfc822 mml easymenu
mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mailabbrev mail-utils gmm-utils mailheader
emacsbug grep compile comint regexp-opt ring tooltip ediff-hook vc-hooks
lisp-float-type mwheel x-win x-dnd tool-bar dnd fontset image fringe
lisp-mode register page menu-bar rfn-eshadow timer select scroll-bar
mouse jit-lock font-lock syntax facemenu font-core frame cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese case-table epa-hook jka-cmpr-hook help simple abbrev
loaddefs button minibuffer faces cus-face files text-properties overlay
md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote make-network-process dbusbind
dynamic-setting system-font-setting font-render-setting move-toolbar gtk
x-toolkit x multi-tty emacs)