summaryrefslogtreecommitdiffstats
path: root/tools/emacs/flymake-chromium.el
blob: 070fada13a9e22d38cdad4e405470e79c1b15fe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
;; Copyright (c) 2011 The Chromium Authors. All rights reserved.
;; Use of this source code is governed by a BSD-style license that can be
;; found in the LICENSE file.

;; Set up flymake for use with chromium code.  Uses ninja (since none of the
;; other chromium build systems have latency that allows interactive use).
;;
;; Requires a modern emacs (GNU Emacs >= 23) and that gyp has already generated
;; the build.ninja file(s).  See defcustoms below for settable knobs.


(require 'flymake)

(defcustom cr-flymake-ninja-build-file "out/Debug/build.ninja"
  "Relative path from chromium's src/ directory to the
  build.ninja file to use.")

(defcustom cr-flymake-ninja-executable "ninja"
  "Ninja executable location; either in $PATH or explicitly given.")

(defun cr-flymake-chromium-src ()
  "Return chromium's src/ directory, or nil on failure."
  (locate-dominating-file buffer-file-truename cr-flymake-ninja-build-file))

(defun cr-flymake-string-prefix-p (prefix str)
  "Return non-nil if PREFIX is a prefix of STR (23.2 has string-prefix-p but
  that's case insensitive and also 23.1 doesn't have it)."
  (string= prefix (substring str 0 (length prefix))))

(defun cr-flymake-current-file-name ()
  "Return the relative path from chromium's src/ directory to the
  file backing the current buffer or nil if it doesn't look like
  we're under chromium/src/."
  (when (and (cr-flymake-chromium-src)
             (cr-flymake-string-prefix-p
              (cr-flymake-chromium-src) buffer-file-truename))
    (substring buffer-file-truename (length (cr-flymake-chromium-src)))))

(defun cr-flymake-from-build-to-src-root ()
  "Return a path fragment for getting from the build.ninja file to src/."
  (replace-regexp-in-string
   "[^/]+" ".."
   (substring
    (file-name-directory
     (file-truename (or (and (cr-flymake-string-prefix-p
                              "/" cr-flymake-ninja-build-file)
                             cr-flymake-ninja-build-file)
                        (concat (cr-flymake-chromium-src)
                                cr-flymake-ninja-build-file))))
    (length (cr-flymake-chromium-src)))))

(defun cr-flymake-getfname (file-name-from-error-message)
  "Strip cruft from the passed-in filename to help flymake find the real file."
  (file-name-nondirectory file-name-from-error-message))

(defun cr-flymake-ninja-command-line ()
  "Return the command-line for running ninja, as a list of strings, or nil if
  we're not during a save"
  (unless (buffer-modified-p)
    (list cr-flymake-ninja-executable
          (list "-C"
                (concat (cr-flymake-chromium-src)
                        (file-name-directory cr-flymake-ninja-build-file))
                (concat (cr-flymake-from-build-to-src-root)
                        (cr-flymake-current-file-name) "^")))))

(defun cr-flymake-kick-off-check-after-save ()
  "Kick off a syntax check after file save, if flymake-mode is on."
  (when flymake-mode (flymake-start-syntax-check)))

(defadvice next-error (around cr-flymake-next-error activate)
  "If flymake has something to say, let it say it; otherwise
   revert to normal next-error behavior."
  (if (not flymake-err-info)
      (condition-case msg
          ad-do-it
        (error (message "%s" (prin1-to-string msg))))
    (flymake-goto-next-error)
    ;; copy/pasted from flymake-display-err-menu-for-current-line because I
    ;; couldn't find a way to have it tell me what the relevant error for this
    ;; line was in a single call:
    (let* ((line-no (flymake-current-line-no))
           (line-err-info-list
            (nth 0 (flymake-find-err-info flymake-err-info line-no)))
           (menu-data (flymake-make-err-menu-data line-no line-err-info-list)))
      (prin1 (car (car (car (cdr menu-data)))) t))))

(defun cr-flymake-find-file ()
  "Enable flymake, but only if it makes sense, and immediately
  disable timer-based execution."
  (when (and (not flymake-mode)
             (not buffer-read-only)
             (cr-flymake-current-file-name))
    ;; Since flymake-allowed-file-name-masks requires static regexps to match
    ;; against, can't use cr-flymake-chromium-src here.  Instead we add a
    ;; generic regexp, but only to a buffer-local version of the variable.
    (set (make-local-variable 'flymake-allowed-file-name-masks)
         (list (list "\\.c\\(\\|c\\|pp\\)"
                     'cr-flymake-ninja-command-line
                     'ignore
                     'cr-flymake-getfname)))
    (flymake-find-file-hook)
    (if flymake-mode
        (cancel-timer flymake-timer)
      (kill-local-variable 'flymake-allowed-file-name-masks))))

(add-hook 'find-file-hook 'cr-flymake-find-file 'append)
(add-hook 'after-save-hook 'cr-flymake-kick-off-check-after-save)

;; Show flymake infrastructure ERRORs in hopes of fixing them.  Set to 3 for
;; DEBUG-level output from flymake.el.
(setq flymake-log-level 0)

(provide 'flymake-chromium)