From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#33475: [Wish list]: Display line and column numbers in warnings with `compile-defun' Date: Tue, 27 Nov 2018 07:15:31 +0000 Message-ID: <20181127071531.GA5093@ACM> References: <20181123175839.GA14199@ACM> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1543303109 5255 195.159.176.226 (27 Nov 2018 07:18:29 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 27 Nov 2018 07:18:29 +0000 (UTC) User-Agent: Mutt/1.10.1 (2018-07-13) To: 33475@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Nov 27 08:18:24 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gRXdX-0001Et-Ns for geb-bug-gnu-emacs@m.gmane.org; Tue, 27 Nov 2018 08:18:23 +0100 Original-Received: from localhost ([::1]:40436 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRXfe-0005BV-61 for geb-bug-gnu-emacs@m.gmane.org; Tue, 27 Nov 2018 02:20:34 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44765) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRXfF-0004vv-11 for bug-gnu-emacs@gnu.org; Tue, 27 Nov 2018 02:20:10 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gRXfA-0007LP-3D for bug-gnu-emacs@gnu.org; Tue, 27 Nov 2018 02:20:09 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:46116) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gRXf9-0007LC-VI for bug-gnu-emacs@gnu.org; Tue, 27 Nov 2018 02:20:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gRXf8-000603-1e for bug-gnu-emacs@gnu.org; Tue, 27 Nov 2018 02:20:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 27 Nov 2018 07:20:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33475 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 33475-submit@debbugs.gnu.org id=B33475.154330314222980 (code B ref 33475); Tue, 27 Nov 2018 07:20:01 +0000 Original-Received: (at 33475) by debbugs.gnu.org; 27 Nov 2018 07:19:02 +0000 Original-Received: from localhost ([127.0.0.1]:50374 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gRXe9-0005yQ-SF for submit@debbugs.gnu.org; Tue, 27 Nov 2018 02:19:02 -0500 Original-Received: from colin.muc.de ([193.149.48.1]:65462 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1gRXe7-0005y9-T2 for 33475@debbugs.gnu.org; Tue, 27 Nov 2018 02:19:00 -0500 Original-Received: (qmail 94687 invoked by uid 3782); 27 Nov 2018 07:18:58 -0000 Original-Received: from acm.muc.de (p2E5D5B5F.dip0.t-ipconnect.de [46.93.91.95]) by colin.muc.de (tmda-ofmipd) with ESMTP; Tue, 27 Nov 2018 08:18:57 +0100 Original-Received: (qmail 5128 invoked by uid 1000); 27 Nov 2018 07:15:31 -0000 Content-Disposition: inline In-Reply-To: <20181123175839.GA14199@ACM> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 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.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:152814 Archived-At: On Fri, Nov 23, 2018 at 17:58:39 +0000, Alan Mackenzie wrote: > Hello, Emacs. > At the moment, if a warning is displayed for a compilation started by > `compile-defun', it appears something like: > Warning: assignment to free variable `foo' > . This is all very well, but it lacks the line and column number of the > place of the warning, meaning one must search through the source and > guess where the warning is. > Why not output this information, much like is done in a batch > compilation? It would then look like: > Buffer winkler2.el:3:14:Warning: assignment to free variable `foo' > . This would save guessing and irritation. > Here is a patch which achieves this: [ .... ] This patch was all very well, but was incomplete: hitting CR on such a warning message in *Compile-Log* failed to move to the position in the .el buffer, instead spuriously prompting for a file. The following patch, incorporating the patch from my original post, fixes this by amending compile.el to handle buffers as an alternative to file names, creating emacs-lisp-compilation-mode, which extracts the buffer from one of these warning messages, and calling this new mode from byte-compile-log-file in place of compilation-mode. diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index 0b8f8824b4..8312c0153e 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el @@ -1006,6 +1006,24 @@ byte-compile-eval-before-compile ;;; byte compiler messages +(defun emacs-lisp-compilation-file-name-or-buffer (str) + "Return file name or buffer given by STR. +If STR is a \"normal\" filename, just return it. +If STR is something like \"Buffer foo.el\", return # +\(if it is still live) or the string \"foo.el\" otherwise." + (if (string-match "Buffer \\(.*\\)\\'" str) + (or (get-buffer (match-string-no-properties 1 str)) + (match-string-no-properties 1 str)) + str)) + +(defconst emacs-lisp-compilation-parse-errors-filename-function + 'emacs-lisp-compilation-file-name-or-buffer + "The value for `compilation-parse-errors-filename-function' for when +we go into emacs-lisp-compilation-mode.") + +(define-compilation-mode emacs-lisp-compilation-mode "elisp-compile" + "The variant of `compilation-mode' used for emacs-lisp error buffers") + (defvar byte-compile-current-form nil) (defvar byte-compile-dest-file nil) (defvar byte-compile-current-file nil) @@ -1160,12 +1178,14 @@ byte-compile-warning-series ;; Return the position of the start of the page in the log buffer. ;; But do nothing in batch mode. (defun byte-compile-log-file () - (and (not (equal byte-compile-current-file byte-compile-last-logged-file)) + (and (not + (and (get-buffer byte-compile-log-buffer) + (equal byte-compile-current-file byte-compile-last-logged-file))) (not noninteractive) (with-current-buffer (get-buffer-create byte-compile-log-buffer) (goto-char (point-max)) (let* ((inhibit-read-only t) - (dir (and byte-compile-current-file + (dir (and (stringp byte-compile-current-file) (file-name-directory byte-compile-current-file))) (was-same (equal default-directory dir)) pt) @@ -1180,7 +1200,7 @@ byte-compile-log-file (insert "\f\nCompiling " (if (stringp byte-compile-current-file) (concat "file " byte-compile-current-file) - (concat "buffer " + (concat "in buffer " (buffer-name byte-compile-current-file))) " at " (current-time-string) "\n") (insert "\f\nCompiling no file at " (current-time-string) "\n")) @@ -1192,7 +1212,8 @@ byte-compile-log-file (setq byte-compile-last-logged-file byte-compile-current-file byte-compile-last-warned-form nil) ;; Do this after setting default-directory. - (unless (derived-mode-p 'compilation-mode) (compilation-mode)) + (unless (derived-mode-p 'compilation-mode) + (emacs-lisp-compilation-mode)) (compilation-forget-errors) pt)))) @@ -1981,7 +2002,7 @@ compile-defun (save-excursion (end-of-defun) (beginning-of-defun) - (let* ((byte-compile-current-file nil) + (let* ((byte-compile-current-file (current-buffer)) (byte-compile-current-buffer (current-buffer)) (byte-compile-read-position (point)) (byte-compile-last-position byte-compile-read-position) diff --git a/lisp/progmodes/compile.el b/lisp/progmodes/compile.el index 7e7c18fb30..973d3a0146 100644 --- a/lisp/progmodes/compile.el +++ b/lisp/progmodes/compile.el @@ -83,7 +83,10 @@ compilation-first-column (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 -in the compilation output, and should return a transformed file name.") +in the compilation output, and should return a transformed file name +or a buffer, the one which was compiled.") +;; Note: the compilation-parse-errors-filename-function need not save the +;; match data. ;;;###autoload (defvar compilation-process-setup-function nil @@ -550,7 +553,8 @@ compilation-error-regexp-alist \(e.g. \"%s.c\") will be applied in turn to the recognized file name, until a file of that name is found. Or FILE can also be a function that returns (FILENAME) or (RELATIVE-FILENAME . DIRNAME). -In the former case, FILENAME may be relative or absolute. +In the former case, FILENAME may be relative or absolute, or it may +be a buffer. LINE can also be of the form (LINE . END-LINE) meaning a range of lines. COLUMN can also be of the form (COLUMN . END-COLUMN) @@ -944,10 +948,11 @@ compilation--loc->visited ;; FILE-STRUCTURE is a list of ;; ((FILENAME DIRECTORY) FORMATS (LINE LOC ...) ...) -;; FILENAME is a string parsed from an error message. DIRECTORY is a string -;; obtained by following directory change messages. DIRECTORY will be nil for -;; an absolute filename. FORMATS is a list of formats to apply to FILENAME if -;; a file of that name can't be found. +;; FILENAME is a string parsed from an error message, or the buffer which was +;; compiled. DIRECTORY is a string obtained by following directory change +;; messages. DIRECTORY will be nil for an absolute filename or a buffer. +;; FORMATS is a list of formats to apply to FILENAME if a file of that name +;; can't be found. ;; The rest of the list is an alist of elements with LINE as key. The keys ;; are either nil or line numbers. If present, nil comes first, followed by ;; the numbers in decreasing order. The LOCs for each line are again an alist @@ -1180,7 +1185,8 @@ compilation-internal-error-properties "Get the meta-info that will be added as text-properties. LINE, END-LINE, COL, END-COL are integers or nil. TYPE can be 0, 1, or 2, meaning error, warning, or just info. -FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME) or nil. +FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME) or (BUFFER) or +nil. FMTS is a list of format specs for transforming the file name. (See `compilation-error-regexp-alist'.)" (unless file (setq file '("*unknown*"))) @@ -2493,12 +2499,14 @@ compilation-next-error-function ;; (setq timestamp compilation-buffer-modtime))) ) (with-current-buffer - (apply #'compilation-find-file - marker - (caar (compilation--loc->file-struct loc)) - (cadr (car (compilation--loc->file-struct loc))) - (compilation--file-struct->formats - (compilation--loc->file-struct loc))) + (if (bufferp (caar (compilation--loc->file-struct loc))) + (caar (compilation--loc->file-struct loc)) + (apply #'compilation-find-file + marker + (caar (compilation--loc->file-struct loc)) + (cadr (car (compilation--loc->file-struct loc))) + (compilation--file-struct->formats + (compilation--loc->file-struct loc)))) (let ((screen-columns ;; Obey the compilation-error-screen-columns of the target ;; buffer if its major mode set it buffer-locally. @@ -2810,18 +2818,21 @@ compilation-get-file-structure (concat comint-file-name-prefix spec-directory)))))) ;; If compilation-parse-errors-filename-function is - ;; defined, use it to process the filename. + ;; defined, use it to process the filename. The result might be a + ;; buffer. (when compilation-parse-errors-filename-function - (setq filename - (funcall compilation-parse-errors-filename-function - filename))) + (save-match-data + (setq filename + (funcall compilation-parse-errors-filename-function + filename)))) ;; Some compilers (e.g. Sun's java compiler, reportedly) produce bogus ;; file names like "./bar//foo.c" for file "bar/foo.c"; ;; expand-file-name will collapse these into "/foo.c" and fail to find ;; the appropriate file. So we look for doubled slashes in the file ;; name and fix them. - (setq filename (command-line-normalize-file-name filename)) + (if (stringp filename) + (setq filename (command-line-normalize-file-name filename))) ;; Store it for the possibly unnormalized name (puthash file -- Alan Mackenzie (Nuremberg, Germany).