Subject: 24.3; Make it possible to modify compilation-find-file

From: Gareth Rees <[email-masked]>
Date: Tue, 7 May 2013 20:19:04 +0100

BACKGROUND

I'm working on the Perforce/Emacs integration
<https://github.com/gareth-rees/p4.el>, and I recently added support for
the "p4 grep" command. This command is just like "grep" except that it
searches current or past revisions of files stored on the Perforce
server. Typical output from the command looks like this:

# find occurrences of FIXME in *.c as of 1st January 2013. 
$ p4 grep -e FIXME '*.c@2013/01/01'
//info.ravenbrook.com/project/mps/master/code/eventcnv.c#17:        /* FIXME: This attempted to print the event stats on a row that
//info.ravenbrook.com/project/mps/master/code/mps.c#14:#include "span.c"       /* generic stack probe FIXME: Is this correct? */

The filename on the left of each hit is expressed in Perforce server
format: thus the first hit is for revision number 17 of eventcnv.c.

An Emacs interface to this command is easy to implement, using the
grep function and setting compilation-error-regexp-alist accordingly.
Except for one problem.

If the user runs the p4 grep command via my interface, and then clicks
on one of the hits, I want Emacs to jump to the corresponding line in
that revision of the file. But the filename is given in Perforce
server syntax, and compilation-next-error-function does not know how
to visit that file. There seems to be no way to customize the
behaviour of compilation-find-file.

SUGGESTION

The simplest way to support my use case would be to add a new variable
compilation-find-file-function. If this is non-NIL, then call it
instead of compilation-find-file. Suggested patch against 24.3 below.

WORKAROUND

I've set next-error-function to this function, which overrides
compilation-find-file during the call to
compilation-next-error-function.

(defun p4-grep-next-error-function (n &optional reset)
  "Advance to the next error message and visit the file where the error was.
This is the value of `next-error-function' in P4 Grep buffers."
  (interactive "p")
  (let ((cff (symbol-function 'compilation-find-file)))
    (unwind-protect
        (progn (fset 'compilation-find-file 'p4-grep-find-file)
               (compilation-next-error-function n reset))
      (fset 'compilation-find-file cff))))

PATCH

--- compile.el-24.3	2013-05-07 20:07:58.000000000 +0100
+++ compile.el	2013-05-07 20:15:17.000000000 +0100
@@ -83,6 +83,15 @@
 that value, otherwise it uses the value in the *compilation*
 buffer.  This enables a major-mode to specify its own value.")
 
+(defvar compilation-find-file-function nil
+  "Function to call to visit a file found by `next-error' and
+similar commands. It takes three arguments: MARKER FILENAME
+DIRECTORY, where FILENAME is the name of the file to visit, MARKER
+is the location in the compilation output where the file was
+mentioned, and DIRECTORY is the \"current\" directory. If
+DIRECTORY is relative, it is combined with `default-directory'.
+If DIRECTORY is nil, that means use `default-directory'.")
+
 (defvar compilation-parse-errors-filename-function nil
   "Function to call to post-process filenames while parsing error messages.
 It takes one arg FILENAME which is the name of a file as found
@@ -1972,6 +1981,7 @@
 		   compilation-first-column
 		   compilation-mode-font-lock-keywords
 		   compilation-page-delimiter
+		   compilation-find-file-function
 		   compilation-parse-errors-filename-function
 		   compilation-process-setup-function
 		   compilation-scroll-output
@@ -2370,7 +2380,7 @@
                  ;;            (setq timestamp compilation-buffer-modtime)))
                  )
       (with-current-buffer
-          (compilation-find-file
+          (funcall (or compilation-find-file-function 'compilation-find-file)
            marker
            (caar (compilation--loc->file-struct loc))
            (cadr (car (compilation--loc->file-struct loc))))

-- 
Gareth Rees