PageRenderTime 229ms CodeModel.GetById 24ms RepoModel.GetById 2ms app.codeStats 3ms

/lisp/progmodes/compile.el

http://github.com/davidswelt/aquamacs-emacs
Emacs Lisp | 2928 lines | 2130 code | 271 blank | 527 comment | 83 complexity | 0e7b2e475197b4e3d4004d707e139899 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, GPL-2.0, AGPL-3.0
  1. ;;; compile.el --- run compiler as inferior of Emacs, parse error messages -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1985-1987, 1993-1999, 2001-2016 Free Software
  3. ;; Foundation, Inc.
  4. ;; Authors: Roland McGrath <roland@gnu.org>,
  5. ;; Daniel Pfeiffer <occitan@esperanto.org>
  6. ;; Maintainer: emacs-devel@gnu.org
  7. ;; Keywords: tools, processes
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This package provides the compile facilities documented in the Emacs user's
  21. ;; manual.
  22. ;;; Code:
  23. (eval-when-compile (require 'cl-lib))
  24. (require 'tool-bar)
  25. (require 'comint)
  26. (defgroup compilation nil
  27. "Run compiler as inferior of Emacs, parse error messages."
  28. :group 'tools
  29. :group 'processes)
  30. ;;;###autoload
  31. (defcustom compilation-mode-hook nil
  32. "List of hook functions run by `compilation-mode'."
  33. :type 'hook
  34. :group 'compilation)
  35. ;;;###autoload
  36. (defcustom compilation-start-hook nil
  37. "Hook run after starting a new compilation process.
  38. The hook is run with one argument, the new process."
  39. :type 'hook
  40. :group 'compilation)
  41. ;;;###autoload
  42. (defcustom compilation-window-height nil
  43. "Number of lines in a compilation window.
  44. If nil, use Emacs default."
  45. :type '(choice (const :tag "Default" nil)
  46. integer)
  47. :group 'compilation)
  48. (defvar compilation-filter-hook nil
  49. "Hook run after `compilation-filter' has inserted a string into the buffer.
  50. It is called with the variable `compilation-filter-start' bound
  51. to the position of the start of the inserted text, and point at
  52. its end.
  53. If Emacs lacks asynchronous process support, this hook is run
  54. after `call-process' inserts the grep output into the buffer.")
  55. (defvar compilation-filter-start nil
  56. "Position of the start of the text inserted by `compilation-filter'.
  57. This is bound before running `compilation-filter-hook'.")
  58. (defvar compilation-first-column 1
  59. "This is how compilers number the first column, usually 1 or 0.
  60. If this is buffer-local in the destination buffer, Emacs obeys
  61. that value, otherwise it uses the value in the *compilation*
  62. buffer. This enables a major-mode to specify its own value.")
  63. (defvar compilation-parse-errors-filename-function nil
  64. "Function to call to post-process filenames while parsing error messages.
  65. It takes one arg FILENAME which is the name of a file as found
  66. in the compilation output, and should return a transformed file name.")
  67. ;;;###autoload
  68. (defvar compilation-process-setup-function nil
  69. "Function to call to customize the compilation process.
  70. This function is called immediately before the compilation process is
  71. started. It can be used to set any variables or functions that are used
  72. while processing the output of the compilation process.")
  73. ;;;###autoload
  74. (defvar compilation-buffer-name-function nil
  75. "Function to compute the name of a compilation buffer.
  76. The function receives one argument, the name of the major mode of the
  77. compilation buffer. It should return a string.
  78. If nil, compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
  79. ;;;###autoload
  80. (defvar compilation-finish-function nil
  81. "Function to call when a compilation process finishes.
  82. It is called with two arguments: the compilation buffer, and a string
  83. describing how the process finished.")
  84. (make-obsolete-variable 'compilation-finish-function
  85. "use `compilation-finish-functions', but it works a little differently."
  86. "22.1")
  87. ;;;###autoload
  88. (defvar compilation-finish-functions nil
  89. "Functions to call when a compilation process finishes.
  90. Each function is called with two arguments: the compilation buffer,
  91. and a string describing how the process finished.")
  92. (defvar compilation-in-progress nil
  93. "List of compilation processes now running.")
  94. (or (assq 'compilation-in-progress minor-mode-alist)
  95. (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
  96. minor-mode-alist)))
  97. (defvar compilation-error "error"
  98. "Stem of message to print when no matches are found.")
  99. (defvar compilation-arguments nil
  100. "Arguments that were given to `compilation-start'.")
  101. (defvar compilation-num-errors-found)
  102. ;; If you make any changes to `compilation-error-regexp-alist-alist',
  103. ;; be sure to run the ERT test in test/automated/compile-tests.el.
  104. ;; emacs -batch -l compile-tests.el -f ert-run-tests-batch-and-exit
  105. (defvar compilation-error-regexp-alist-alist
  106. `((absoft
  107. "^\\(?:[Ee]rror on \\|[Ww]arning on\\( \\)\\)?[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
  108. of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2 nil (1))
  109. (ada
  110. "\\(warning: .*\\)? at \\([^ \n]+\\):\\([0-9]+\\)$" 2 3 nil (1))
  111. (aix
  112. " in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
  113. (ant
  114. "^[ \t]*\\[[^] \n]+\\][ \t]*\\(\\(?:[A-Za-z]:\\\\\\)?[^: \n]+\\):\\([0-9]+\\):\\(?:\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\):\\)?\
  115. \\( warning\\)?" 1 (2 . 4) (3 . 5) (6))
  116. (bash
  117. "^\\([^: \n\t]+\\): line \\([0-9]+\\):" 1 2)
  118. (borland
  119. "^\\(?:Error\\|Warnin\\(g\\)\\) \\(?:[FEW][0-9]+ \\)?\
  120. \\([a-zA-Z]?:?[^:( \t\n]+\\)\
  121. \\([0-9]+\\)\\(?:[) \t]\\|:[^0-9\n]\\)" 2 3 nil (1))
  122. (python-tracebacks-and-caml
  123. "^[ \t]*File \\(\"?\\)\\([^,\" \n\t<>]+\\)\\1, lines? \\([0-9]+\\)-?\\([0-9]+\\)?\\(?:$\\|,\
  124. \\(?: characters? \\([0-9]+\\)-?\\([0-9]+\\)?:\\)?\\([ \n]Warning\\(?: [0-9]+\\)?:\\)?\\)"
  125. 2 (3 . 4) (5 . 6) (7))
  126. (comma
  127. "^\"\\([^,\" \n\t]+\\)\", line \\([0-9]+\\)\
  128. \\(?:[(. pos]+\\([0-9]+\\))?\\)?[:.,; (-]\\( warning:\\|[-0-9 ]*(W)\\)?" 1 2 3 (4))
  129. (cucumber
  130. "\\(?:^cucumber\\(?: -p [^[:space:]]+\\)?\\|#\\)\
  131. \\(?: \\)\\([^(].*\\):\\([1-9][0-9]*\\)" 1 2)
  132. (msft
  133. ;; Must be before edg-1, so that MSVC's longer messages are
  134. ;; considered before EDG.
  135. ;; The message may be a "warning", "error", or "fatal error" with
  136. ;; an error code, or "see declaration of" without an error code.
  137. "^ *\\([0-9]+>\\)?\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) ?\
  138. : \\(?:see declaration\\|\\(?:warnin\\(g\\)\\|[a-z ]+\\) C[0-9]+:\\)"
  139. 2 3 nil (4))
  140. (edg-1
  141. "^\\([^ \n]+\\)(\\([0-9]+\\)): \\(?:error\\|warnin\\(g\\)\\|remar\\(k\\)\\)"
  142. 1 2 nil (3 . 4))
  143. (edg-2
  144. "at line \\([0-9]+\\) of \"\\([^ \n]+\\)\"$"
  145. 2 1 nil 0)
  146. (epc
  147. "^Error [0-9]+ at (\\([0-9]+\\):\\([^)\n]+\\))" 2 1)
  148. (ftnchek
  149. "\\(^Warning .*\\)? line[ \n]\\([0-9]+\\)[ \n]\\(?:col \\([0-9]+\\)[ \n]\\)?file \\([^ :;\n]+\\)"
  150. 4 2 3 (1))
  151. (iar
  152. "^\"\\(.*\\)\",\\([0-9]+\\)\\s-+\\(?:Error\\|Warnin\\(g\\)\\)\\[[0-9]+\\]:"
  153. 1 2 nil (3))
  154. (ibm
  155. "^\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) :\
  156. \\(?:warnin\\(g\\)\\|informationa\\(l\\)\\)?" 1 2 3 (4 . 5))
  157. ;; fixme: should be `mips'
  158. (irix
  159. "^[-[:alnum:]_/ ]+: \\(?:\\(?:[sS]evere\\|[eE]rror\\|[wW]arnin\\(g\\)\\|[iI]nf\\(o\\)\\)[0-9 ]*: \\)?\
  160. \\([^,\" \n\t]+\\)\\(?:, line\\|:\\) \\([0-9]+\\):" 3 4 nil (1 . 2))
  161. (java
  162. "^\\(?:[ \t]+at \\|==[0-9]+== +\\(?:at\\|b\\(y\\)\\)\\).+(\\([^()\n]+\\):\\([0-9]+\\))$" 2 3 nil (1))
  163. (jikes-file
  164. "^\\(?:Found\\|Issued\\) .* compiling \"\\(.+\\)\":$" 1 nil nil 0)
  165. ;; This used to be pathologically slow on long lines (Bug#3441),
  166. ;; due to matching filenames via \\(.*?\\). This might be faster.
  167. (maven
  168. ;; Maven is a popular free software build tool for Java.
  169. "\\(\\[WARNING\\] *\\)?\\([^ \n]\\(?:[^\n :]\\| [^-/\n]\\|:[^ \n]\\)*?\\):\\[\\([0-9]+\\),\\([0-9]+\\)\\] " 2 3 4 (1))
  170. (jikes-line
  171. "^ *\\([0-9]+\\)\\.[ \t]+.*\n +\\(<-*>\n\\*\\*\\* \\(?:Error\\|Warnin\\(g\\)\\)\\)"
  172. nil 1 nil 2 0
  173. (2 (compilation-face '(3))))
  174. (gcc-include
  175. "^\\(?:In file included \\| \\|\t\\)from \
  176. \\([0-9]*[^0-9\n]\\(?:[^\n :]\\| [^-/\n]\\|:[^ \n]\\)*?\\):\
  177. \\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?\\(?:\\(:\\)\\|\\(,\\|$\\)\\)?"
  178. 1 2 3 (4 . 5))
  179. (ruby-Test::Unit
  180. "^[\t ]*\\[\\([^(].*\\):\\([1-9][0-9]*\\)\\(\\]\\)?:in " 1 2)
  181. (gnu
  182. ;; The first line matches the program name for
  183. ;; PROGRAM:SOURCE-FILE-NAME:LINENO: MESSAGE
  184. ;; format, which is used for non-interactive programs other than
  185. ;; compilers (e.g. the "jade:" entry in compilation.txt).
  186. ;; This first line makes things ambiguous with output such as
  187. ;; "foo:344:50:blabla" since the "foo" part can match this first
  188. ;; line (in which case the file name as "344"). To avoid this,
  189. ;; the second line disallows filenames exclusively composed of
  190. ;; digits.
  191. ;; Similarly, we get lots of false positives with messages including
  192. ;; times of the form "HH:MM:SS" where MM is taken as a line number, so
  193. ;; the last line tries to rule out message where the info after the
  194. ;; line number starts with "SS". --Stef
  195. ;; The core of the regexp is the one with *?. It says that a file name
  196. ;; can be composed of any non-newline char, but it also rules out some
  197. ;; valid but unlikely cases, such as a trailing space or a space
  198. ;; followed by a -, or a colon followed by a space.
  199. ;;
  200. ;; The "in \\|from " exception was added to handle messages from Ruby.
  201. ,(rx
  202. bol
  203. (? (| (regexp "[[:alpha:]][-[:alnum:].]+: ?")
  204. (regexp "[ \t]+\\(?:in \\|from\\)")))
  205. (group-n 1 (: (regexp "[0-9]*[^0-9\n]")
  206. (*? (| (regexp "[^\n :]")
  207. (regexp " [^-/\n]")
  208. (regexp ":[^ \n]")))))
  209. (regexp ": ?")
  210. (group-n 2 (regexp "[0-9]+"))
  211. (? (| (: "-"
  212. (group-n 4 (regexp "[0-9]+"))
  213. (? "." (group-n 5 (regexp "[0-9]+"))))
  214. (: (in ".:")
  215. (group-n 3 (regexp "[0-9]+"))
  216. (? "-"
  217. (? (group-n 4 (regexp "[0-9]+")) ".")
  218. (group-n 5 (regexp "[0-9]+"))))))
  219. ":"
  220. (| (: (* " ")
  221. (group-n 6 (| "FutureWarning"
  222. "RuntimeWarning"
  223. "Warning"
  224. "warning"
  225. "W:")))
  226. (: (* " ")
  227. (group-n 7 (| (regexp "[Ii]nfo\\(?:\\>\\|rmationa?l?\\)")
  228. "I:"
  229. (: "[ skipping " (+ ".") " ]")
  230. "instantiated from"
  231. "required from"
  232. (regexp "[Nn]ote"))))
  233. (: (* " ")
  234. (regexp "[Ee]rror"))
  235. (: (regexp "[0-9]?")
  236. (| (regexp "[^0-9\n]")
  237. eol))
  238. (regexp "[0-9][0-9][0-9]")))
  239. 1 (2 . 4) (3 . 5) (6 . 7))
  240. (lcc
  241. "^\\(?:E\\|\\(W\\)\\), \\([^(\n]+\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)"
  242. 2 3 4 (1))
  243. (makepp
  244. "^makepp\\(?:\\(?:: warning\\(:\\).*?\\|\\(: Scanning\\|: [LR]e?l?oading makefile\\|: Imported\\|log:.*?\\) \\|: .*?\\)\
  245. `\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]\\)"
  246. 4 5 nil (1 . 2) 3
  247. (0 (progn (save-match-data
  248. (compilation-parse-errors
  249. (match-end 0) (line-end-position)
  250. `("`\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]"
  251. 2 3 nil
  252. ,(cond ((match-end 1) 1) ((match-end 2) 0) (t 2))
  253. 1)))
  254. (end-of-line)
  255. nil)))
  256. ;; Should be lint-1, lint-2 (SysV lint)
  257. (mips-1
  258. " (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
  259. (mips-2
  260. " in \\([^()\n ]+\\)(\\([0-9]+\\))$" 1 2)
  261. (msft
  262. ;; The message may be a "warning", "error", or "fatal error" with
  263. ;; an error code, or "see declaration of" without an error code.
  264. "^ *\\([0-9]+>\\)?\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \
  265. : \\(?:see declaration\\|\\(?:warnin\\(g\\)\\|[a-z ]+\\) C[0-9]+:\\)"
  266. 2 3 nil (4))
  267. (omake
  268. ;; "omake -P" reports "file foo changed"
  269. ;; (useful if you do "cvs up" and want to see what has changed)
  270. "omake: file \\(.*\\) changed" 1 nil nil nil nil
  271. ;; FIXME-omake: This tries to prevent reusing pre-existing markers
  272. ;; for subsequent messages, since those messages's line numbers
  273. ;; are about another version of the file.
  274. (0 (progn (compilation--flush-file-structure (match-string 1))
  275. nil)))
  276. (oracle
  277. "^\\(?:Semantic error\\|Error\\|PCC-[0-9]+:\\).* line \\([0-9]+\\)\
  278. \\(?:\\(?:,\\| at\\)? column \\([0-9]+\\)\\)?\
  279. \\(?:,\\| in\\| of\\)? file \\(.*?\\):?$"
  280. 3 1 2)
  281. ;; "during global destruction": This comes out under "use
  282. ;; warnings" in recent perl when breaking circular references
  283. ;; during program or thread exit.
  284. (perl
  285. " at \\([^ \n]+\\) line \\([0-9]+\\)\\(?:[,.]\\|$\\| \
  286. during global destruction\\.$\\)" 1 2)
  287. (php
  288. "\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)"
  289. 2 3 nil nil)
  290. (rxp
  291. "^\\(?:Error\\|Warnin\\(g\\)\\):.*\n.* line \\([0-9]+\\) char\
  292. \\([0-9]+\\) of file://\\(.+\\)"
  293. 4 2 3 (1))
  294. (sparc-pascal-file
  295. "^\\w\\w\\w \\w\\w\\w +[0-3]?[0-9] +[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\
  296. [12][09][0-9][0-9] +\\(.*\\):$"
  297. 1 nil nil 0)
  298. (sparc-pascal-line
  299. "^\\(\\(?:E\\|\\(w\\)\\) +[0-9]+\\) line \\([0-9]+\\) - "
  300. nil 3 nil (2) nil (1 (compilation-face '(2))))
  301. (sparc-pascal-example
  302. "^ +\\([0-9]+\\) +.*\n\\(\\(?:e\\|\\(w\\)\\) [0-9]+\\)-+"
  303. nil 1 nil (3) nil (2 (compilation-face '(3))))
  304. (sun
  305. ": \\(?:ERROR\\|WARNIN\\(G\\)\\|REMAR\\(K\\)\\) \\(?:[[:alnum:] ]+, \\)?\
  306. File = \\(.+\\), Line = \\([0-9]+\\)\\(?:, Column = \\([0-9]+\\)\\)?"
  307. 3 4 5 (1 . 2))
  308. (sun-ada
  309. "^\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., (-]" 1 2 3)
  310. (watcom
  311. "^[ \t]*\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)): ?\
  312. \\(?:\\(Error! E[0-9]+\\)\\|\\(Warning! W[0-9]+\\)\\):"
  313. 1 2 nil (4))
  314. (4bsd
  315. "\\(?:^\\|:: \\|\\S ( \\)\\(/[^ \n\t()]+\\)(\\([0-9]+\\))\
  316. \\(?:: \\(warning:\\)?\\|$\\| ),\\)" 1 2 nil (3))
  317. (gcov-file
  318. "^ *-: *\\(0\\):Source:\\(.+\\)$"
  319. 2 1 nil 0 nil)
  320. (gcov-header
  321. "^ *-: *\\(0\\):\\(?:Object\\|Graph\\|Data\\|Runs\\|Programs\\):.+$"
  322. nil 1 nil 0 nil)
  323. ;; Underlines over all lines of gcov output are too uncomfortable to read.
  324. ;; However, hyperlinks embedded in the lines are useful.
  325. ;; So I put default face on the lines; and then put
  326. ;; compilation-*-face by manually to eliminate the underlines.
  327. ;; The hyperlinks are still effective.
  328. (gcov-nomark
  329. "^ *-: *\\([1-9]\\|[0-9]\\{2,\\}\\):.*$"
  330. nil 1 nil 0 nil
  331. (0 'default)
  332. (1 compilation-line-face))
  333. (gcov-called-line
  334. "^ *\\([0-9]+\\): *\\([0-9]+\\):.*$"
  335. nil 2 nil 0 nil
  336. (0 'default)
  337. (1 compilation-info-face) (2 compilation-line-face))
  338. (gcov-never-called
  339. "^ *\\(#####\\): *\\([0-9]+\\):.*$"
  340. nil 2 nil 2 nil
  341. (0 'default)
  342. (1 compilation-error-face) (2 compilation-line-face))
  343. (perl--Pod::Checker
  344. ;; podchecker error messages, per Pod::Checker.
  345. ;; The style is from the Pod::Checker::poderror() function, eg.
  346. ;; *** ERROR: Spurious text after =cut at line 193 in file foo.pm
  347. ;;
  348. ;; Plus end_pod() can give "at line EOF" instead of a
  349. ;; number, so for that match "on line N" which is the
  350. ;; originating spot, eg.
  351. ;; *** ERROR: =over on line 37 without closing =back at line EOF in file bar.pm
  352. ;;
  353. ;; Plus command() can give both "on line N" and "at line N";
  354. ;; the latter is desired and is matched because the .* is
  355. ;; greedy.
  356. ;; *** ERROR: =over on line 1 without closing =back (at head1) at line 3 in file x.pod
  357. ;;
  358. "^\\*\\*\\* \\(?:ERROR\\|\\(WARNING\\)\\).* \\(?:at\\|on\\) line \
  359. \\([0-9]+\\) \\(?:.* \\)?in file \\([^ \t\n]+\\)"
  360. 3 2 nil (1))
  361. (perl--Test
  362. ;; perl Test module error messages.
  363. ;; Style per the ok() function "$context", eg.
  364. ;; # Failed test 1 in foo.t at line 6
  365. ;;
  366. "^# Failed test [0-9]+ in \\([^ \t\r\n]+\\) at line \\([0-9]+\\)"
  367. 1 2)
  368. (perl--Test2
  369. ;; Or when comparing got/want values, with a "fail #n" if repeated
  370. ;; # Test 2 got: "xx" (t-compilation-perl-2.t at line 10)
  371. ;; # Test 3 got: "xx" (t-compilation-perl-2.t at line 10 fail #2)
  372. ;;
  373. ;; And under Test::Harness they're preceded by progress stuff with
  374. ;; \r and "NOK",
  375. ;; ... NOK 1# Test 1 got: "1234" (t/foo.t at line 46)
  376. ;;
  377. "^\\(.*NOK.*\\)?# Test [0-9]+ got:.* (\\([^ \t\r\n]+\\) at line \
  378. \\([0-9]+\\)\\( fail #[0-9]+\\)?)"
  379. 2 3)
  380. (perl--Test::Harness
  381. ;; perl Test::Harness output, eg.
  382. ;; NOK 1# Test 1 got: "1234" (t/foo.t at line 46)
  383. ;;
  384. ;; Test::Harness is slightly designed for tty output, since
  385. ;; it prints CRs to overwrite progress messages, but if you
  386. ;; run it in with M-x compile this pattern can at least step
  387. ;; through the failures.
  388. ;;
  389. "^.*NOK.* \\([^ \t\r\n]+\\) at line \\([0-9]+\\)"
  390. 1 2)
  391. (weblint
  392. ;; The style comes from HTML::Lint::Error::as_string(), eg.
  393. ;; index.html (13:1) Unknown element <fdjsk>
  394. ;;
  395. ;; The pattern only matches filenames without spaces, since that
  396. ;; should be usual and should help reduce the chance of a false
  397. ;; match of a message from some unrelated program.
  398. ;;
  399. ;; This message style is quite close to the "ibm" entry which is
  400. ;; for IBM C, though that ibm bit doesn't put a space after the
  401. ;; filename.
  402. ;;
  403. "^\\([^ \t\r\n(]+\\) (\\([0-9]+\\):\\([0-9]+\\)) "
  404. 1 2 3)
  405. ;; Guile compilation yields file-headers in the following format:
  406. ;;
  407. ;; In sourcefile.scm:
  408. ;;
  409. ;; We need to catch those, but we also need to be aware that Emacs
  410. ;; byte-compilation yields compiler headers in similar form of
  411. ;; those:
  412. ;;
  413. ;; In toplevel form:
  414. ;; In end of data:
  415. ;;
  416. ;; We want to catch the Guile file-headers but not the Emacs
  417. ;; byte-compilation headers, because that will cause next-error
  418. ;; and prev-error to break, because the files "toplevel form" and
  419. ;; "end of data" does not exist.
  420. ;;
  421. ;; To differentiate between these two cases, we require that the
  422. ;; file-match must always contain an extension.
  423. ;;
  424. ;; We should also only treat this as "info", not "error", because
  425. ;; we do not know what lines will follow.
  426. (guile-file "^In \\(.+\\..+\\):\n" 1 nil nil 0)
  427. (guile-line "^ *\\([0-9]+\\): *\\([0-9]+\\)" nil 1 2)
  428. )
  429. "Alist of values for `compilation-error-regexp-alist'.")
  430. (defcustom compilation-error-regexp-alist
  431. (mapcar 'car compilation-error-regexp-alist-alist)
  432. "Alist that specifies how to match errors in compiler output.
  433. On GNU and Unix, any string is a valid filename, so these
  434. matchers must make some common sense assumptions, which catch
  435. normal cases. A shorter list will be lighter on resource usage.
  436. Instead of an alist element, you can use a symbol, which is
  437. looked up in `compilation-error-regexp-alist-alist'. You can see
  438. the predefined symbols and their effects in the file
  439. `etc/compilation.txt' (linked below if you are customizing this).
  440. Each elt has the form (REGEXP FILE [LINE COLUMN TYPE HYPERLINK
  441. HIGHLIGHT...]). If REGEXP matches, the FILE'th subexpression
  442. gives the file name, and the LINE'th subexpression gives the line
  443. number. The COLUMN'th subexpression gives the column number on
  444. that line.
  445. If FILE, LINE or COLUMN are nil or that index didn't match, that
  446. information is not present on the matched line. In that case the
  447. file name is assumed to be the same as the previous one in the
  448. buffer, line number defaults to 1 and column defaults to
  449. beginning of line's indentation.
  450. FILE can also have the form (FILE FORMAT...), where the FORMATs
  451. \(e.g. \"%s.c\") will be applied in turn to the recognized file
  452. name, until a file of that name is found. Or FILE can also be a
  453. function that returns (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
  454. In the former case, FILENAME may be relative or absolute.
  455. LINE can also be of the form (LINE . END-LINE) meaning a range
  456. of lines. COLUMN can also be of the form (COLUMN . END-COLUMN)
  457. meaning a range of columns starting on LINE and ending on
  458. END-LINE, if that matched.
  459. TYPE is 2 or nil for a real error or 1 for warning or 0 for info.
  460. TYPE can also be of the form (WARNING . INFO). In that case this
  461. will be equivalent to 1 if the WARNING'th subexpression matched
  462. or else equivalent to 0 if the INFO'th subexpression matched.
  463. See `compilation-error-face', `compilation-warning-face',
  464. `compilation-info-face' and `compilation-skip-threshold'.
  465. What matched the HYPERLINK'th subexpression has `mouse-face' and
  466. `compilation-message-face' applied. If this is nil, the text
  467. matched by the whole REGEXP becomes the hyperlink.
  468. Additional HIGHLIGHTs take the shape (SUBMATCH FACE), where
  469. SUBMATCH is the number of a submatch and FACE is an expression
  470. which evaluates to a face name (a symbol or string).
  471. Alternatively, FACE can evaluate to a property list of the
  472. form (face FACE PROP1 VAL1 PROP2 VAL2 ...), in which case all the
  473. listed text properties PROP# are given values VAL# as well."
  474. :type '(repeat (choice (symbol :tag "Predefined symbol")
  475. (sexp :tag "Error specification")))
  476. :link `(file-link :tag "example file"
  477. ,(expand-file-name "compilation.txt" data-directory))
  478. :group 'compilation)
  479. ;;;###autoload(put 'compilation-directory 'safe-local-variable 'stringp)
  480. (defvar compilation-directory nil
  481. "Directory to restore to when doing `recompile'.")
  482. (defvar compilation-directory-matcher
  483. '("\\(?:Entering\\|Leavin\\(g\\)\\) directory [`']\\(.+\\)'$" (2 . 1))
  484. "A list for tracking when directories are entered or left.
  485. If nil, do not track directories, e.g. if all file names are absolute. The
  486. first element is the REGEXP matching these messages. It can match any number
  487. of variants, e.g. different languages. The remaining elements are all of the
  488. form (DIR . LEAVE). If for any one of these the DIR'th subexpression
  489. matches, that is a directory name. If LEAVE is nil or the corresponding
  490. LEAVE'th subexpression doesn't match, this message is about going into another
  491. directory. If it does match anything, this message is about going back to the
  492. directory we were in before the last entering message. If you change this,
  493. you may also want to change `compilation-page-delimiter'.")
  494. (defvar compilation-page-delimiter
  495. "^\\(?:\f\\|.*\\(?:Entering\\|Leaving\\) directory [`'].+'\n\\)+"
  496. "Value of `page-delimiter' in Compilation mode.")
  497. (defvar compilation-mode-font-lock-keywords
  498. '(;; configure output lines.
  499. ("^[Cc]hecking \\(?:[Ff]or \\|[Ii]f \\|[Ww]hether \\(?:to \\)?\\)?\\(.+\\)\\.\\.\\. *\\(?:(cached) *\\)?\\(\\(yes\\(?: .+\\)?\\)\\|no\\|\\(.*\\)\\)$"
  500. (1 font-lock-variable-name-face)
  501. (2 (compilation-face '(4 . 3))))
  502. ;; Command output lines. Recognize `make[n]:' lines too.
  503. ("^\\([[:alnum:]_/.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
  504. (1 font-lock-function-name-face) (3 compilation-line-face nil t))
  505. (" --?o\\(?:utfile\\|utput\\)?[= ]\\(\\S +\\)" . 1)
  506. ("^Compilation \\(finished\\).*"
  507. (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)
  508. (1 compilation-info-face))
  509. ("^Compilation \\(exited abnormally\\|interrupt\\|killed\\|terminated\\|segmentation fault\\)\\(?:.*with code \\([0-9]+\\)\\)?.*"
  510. (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)
  511. (1 compilation-error-face)
  512. (2 compilation-error-face nil t)))
  513. "Additional things to highlight in Compilation mode.
  514. This gets tacked on the end of the generated expressions.")
  515. (defvar compilation-highlight-regexp t
  516. "Regexp matching part of visited source lines to highlight temporarily.
  517. Highlight entire line if t; don't highlight source lines if nil.")
  518. (defvar compilation-highlight-overlay nil
  519. "Overlay used to temporarily highlight compilation matches.")
  520. (defcustom compilation-error-screen-columns t
  521. "If non-nil, column numbers in error messages are screen columns.
  522. Otherwise they are interpreted as character positions, with
  523. each character occupying one column.
  524. The default is to use screen columns, which requires that the compilation
  525. program and Emacs agree about the display width of the characters,
  526. especially the TAB character.
  527. If this is buffer-local in the destination buffer, Emacs obeys
  528. that value, otherwise it uses the value in the *compilation*
  529. buffer. This enables a major-mode to specify its own value."
  530. :type 'boolean
  531. :group 'compilation
  532. :version "20.4")
  533. (defcustom compilation-read-command t
  534. "Non-nil means \\[compile] reads the compilation command to use.
  535. Otherwise, \\[compile] just uses the value of `compile-command'.
  536. Note that changing this to nil may be a security risk, because a
  537. file might define a malicious `compile-command' as a file local
  538. variable, and you might not notice. Therefore, `compile-command'
  539. is considered unsafe if this variable is nil."
  540. :type 'boolean
  541. :group 'compilation)
  542. ;;;###autoload
  543. (defcustom compilation-ask-about-save t
  544. "Non-nil means \\[compile] asks which buffers to save before compiling.
  545. Otherwise, it saves all modified buffers without asking."
  546. :type 'boolean
  547. :group 'compilation)
  548. (defcustom compilation-save-buffers-predicate nil
  549. "The second argument (PRED) passed to `save-some-buffers' before compiling.
  550. E.g., one can set this to
  551. (lambda ()
  552. (string-prefix-p my-compilation-root (file-truename (buffer-file-name))))
  553. to limit saving to files located under `my-compilation-root'.
  554. Note, that, in general, `compilation-directory' cannot be used instead
  555. of `my-compilation-root' here."
  556. :type '(choice
  557. (const :tag "Default (save all file-visiting buffers)" nil)
  558. (const :tag "Save all buffers" t)
  559. function)
  560. :group 'compilation
  561. :version "24.1")
  562. ;;;###autoload
  563. (defcustom compilation-search-path '(nil)
  564. "List of directories to search for source files named in error messages.
  565. Elements should be directory names, not file names of directories.
  566. The value nil as an element means to try the default directory."
  567. :type '(repeat (choice (const :tag "Default" nil)
  568. (string :tag "Directory")))
  569. :group 'compilation)
  570. ;;;###autoload
  571. (defcustom compile-command (purecopy "make -k ")
  572. "Last shell command used to do a compilation; default for next compilation.
  573. Sometimes it is useful for files to supply local values for this variable.
  574. You might also use mode hooks to specify it in certain modes, like this:
  575. (add-hook \\='c-mode-hook
  576. (lambda ()
  577. (unless (or (file-exists-p \"makefile\")
  578. (file-exists-p \"Makefile\"))
  579. (set (make-local-variable \\='compile-command)
  580. (concat \"make -k \"
  581. (if buffer-file-name
  582. (shell-quote-argument
  583. (file-name-sans-extension buffer-file-name))))))))"
  584. :type 'string
  585. :group 'compilation)
  586. ;;;###autoload(put 'compile-command 'safe-local-variable (lambda (a) (and (stringp a) (or (not (boundp 'compilation-read-command)) compilation-read-command))))
  587. ;;;###autoload
  588. (defcustom compilation-disable-input nil
  589. "If non-nil, send end-of-file as compilation process input.
  590. This only affects platforms that support asynchronous processes (see
  591. `start-process'); synchronous compilation processes never accept input."
  592. :type 'boolean
  593. :group 'compilation
  594. :version "22.1")
  595. ;; A weak per-compilation-buffer hash indexed by (FILENAME . DIRECTORY). Each
  596. ;; value is a FILE-STRUCTURE as described above, with the car eq to the hash
  597. ;; key. This holds the tree seen from root, for storing new nodes.
  598. (defvar compilation-locs ())
  599. (defvar compilation-debug nil
  600. "Set this to t before creating a *compilation* buffer.
  601. Then every error line will have a debug text property with the matcher that
  602. fit this line and the match data. Use `describe-text-properties'.")
  603. (defvar compilation-exit-message-function nil "\
  604. If non-nil, called when a compilation process dies to return a status message.
  605. This should be a function of three arguments: process status, exit status,
  606. and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
  607. write into the compilation buffer, and to put in its mode line.")
  608. (defcustom compilation-environment nil
  609. "List of environment variables for compilation to inherit.
  610. Each element should be a string of the form ENVVARNAME=VALUE.
  611. This list is temporarily prepended to `process-environment' prior to
  612. starting the compilation process."
  613. :type '(repeat (string :tag "ENVVARNAME=VALUE"))
  614. :options '(("LANG=C"))
  615. :group 'compilation
  616. :version "24.1")
  617. ;; History of compile commands.
  618. (defvar compile-history nil)
  619. (defface compilation-error
  620. '((t :inherit error))
  621. "Face used to highlight compiler errors."
  622. :group 'compilation
  623. :version "22.1")
  624. (defface compilation-warning
  625. '((t :inherit warning))
  626. "Face used to highlight compiler warnings."
  627. :group 'compilation
  628. :version "22.1")
  629. (defface compilation-info
  630. '((t :inherit success))
  631. "Face used to highlight compiler information."
  632. :group 'compilation
  633. :version "22.1")
  634. ;; The next three faces must be able to stand out against the
  635. ;; `mode-line' and `mode-line-inactive' faces.
  636. (defface compilation-mode-line-fail
  637. '((default :inherit compilation-error)
  638. (((class color) (min-colors 16)) (:foreground "Red1" :weight bold))
  639. (((class color) (min-colors 8)) (:foreground "red"))
  640. (t (:inverse-video t :weight bold)))
  641. "Face for Compilation mode's \"error\" mode line indicator."
  642. :group 'compilation
  643. :version "24.3")
  644. (defface compilation-mode-line-run
  645. '((t :inherit compilation-warning))
  646. "Face for Compilation mode's \"running\" mode line indicator."
  647. :group 'compilation
  648. :version "24.3")
  649. (defface compilation-mode-line-exit
  650. '((default :inherit compilation-info)
  651. (((class color) (min-colors 16))
  652. (:foreground "ForestGreen" :weight bold))
  653. (((class color)) (:foreground "green" :weight bold))
  654. (t (:weight bold)))
  655. "Face for Compilation mode's \"exit\" mode line indicator."
  656. :group 'compilation
  657. :version "24.3")
  658. (defface compilation-line-number
  659. '((t :inherit font-lock-keyword-face))
  660. "Face for displaying line numbers in compiler messages."
  661. :group 'compilation
  662. :version "22.1")
  663. (defface compilation-column-number
  664. '((t :inherit font-lock-doc-face))
  665. "Face for displaying column numbers in compiler messages."
  666. :group 'compilation
  667. :version "22.1")
  668. (defcustom compilation-message-face 'underline
  669. "Face name to use for whole messages.
  670. Faces `compilation-error-face', `compilation-warning-face',
  671. `compilation-info-face', `compilation-line-face' and
  672. `compilation-column-face' get prepended to this, when applicable."
  673. :type 'face
  674. :group 'compilation
  675. :version "22.1")
  676. (defvar compilation-error-face 'compilation-error
  677. "Face name to use for file name in error messages.")
  678. (defvar compilation-warning-face 'compilation-warning
  679. "Face name to use for file name in warning messages.")
  680. (defvar compilation-info-face 'compilation-info
  681. "Face name to use for file name in informational messages.")
  682. (defvar compilation-line-face 'compilation-line-number
  683. "Face name to use for line numbers in compiler messages.")
  684. (defvar compilation-column-face 'compilation-column-number
  685. "Face name to use for column numbers in compiler messages.")
  686. ;; same faces as dired uses
  687. (defvar compilation-enter-directory-face 'font-lock-function-name-face
  688. "Face name to use for entering directory messages.")
  689. (defvar compilation-leave-directory-face 'font-lock-builtin-face
  690. "Face name to use for leaving directory messages.")
  691. ;; Used for compatibility with the old compile.el.
  692. (defvar compilation-parse-errors-function nil)
  693. (make-obsolete-variable 'compilation-parse-errors-function
  694. 'compilation-error-regexp-alist "24.1")
  695. (defcustom compilation-auto-jump-to-first-error nil
  696. "If non-nil, automatically jump to the first error during compilation."
  697. :type 'boolean
  698. :group 'compilation
  699. :version "23.1")
  700. (defvar compilation-auto-jump-to-next nil
  701. "If non-nil, automatically jump to the next error encountered.")
  702. (make-variable-buffer-local 'compilation-auto-jump-to-next)
  703. ;; (defvar compilation-buffer-modtime nil
  704. ;; "The buffer modification time, for buffers not associated with files.")
  705. ;; (make-variable-buffer-local 'compilation-buffer-modtime)
  706. (defvar compilation-skip-to-next-location t
  707. "If non-nil, skip multiple error messages for the same source location.")
  708. (defcustom compilation-skip-threshold 1
  709. "Compilation motion commands skip less important messages.
  710. The value can be either 2 -- skip anything less than error, 1 --
  711. skip anything less than warning or 0 -- don't skip any messages.
  712. Note that all messages not positively identified as warning or
  713. info, are considered errors."
  714. :type '(choice (const :tag "Skip warnings and info" 2)
  715. (const :tag "Skip info" 1)
  716. (const :tag "No skip" 0))
  717. :group 'compilation
  718. :version "22.1")
  719. (defun compilation-set-skip-threshold (level)
  720. "Switch the `compilation-skip-threshold' level."
  721. (interactive
  722. (list
  723. (mod (if current-prefix-arg
  724. (prefix-numeric-value current-prefix-arg)
  725. (1+ compilation-skip-threshold))
  726. 3)))
  727. (setq compilation-skip-threshold level)
  728. (message "Skipping %s"
  729. (pcase compilation-skip-threshold
  730. (0 "Nothing")
  731. (1 "Info messages")
  732. (2 "Warnings and info"))))
  733. (defcustom compilation-skip-visited nil
  734. "Compilation motion commands skip visited messages if this is t.
  735. Visited messages are ones for which the file, line and column have been jumped
  736. to from the current content in the current compilation buffer, even if it was
  737. from a different message."
  738. :type 'boolean
  739. :group 'compilation
  740. :version "22.1")
  741. (defun compilation-face (type)
  742. (or (and (car type) (match-end (car type)) compilation-warning-face)
  743. (and (cdr type) (match-end (cdr type)) compilation-info-face)
  744. compilation-error-face))
  745. ;; LOC (or location) is a list of (COLUMN LINE FILE-STRUCTURE nil nil)
  746. ;; COLUMN and LINE are numbers parsed from an error message. COLUMN and maybe
  747. ;; LINE will be nil for a message that doesn't contain them. Then the
  748. ;; location refers to a indented beginning of line or beginning of file.
  749. ;; Once any location in some file has been jumped to, the list is extended to
  750. ;; (COLUMN LINE FILE-STRUCTURE MARKER TIMESTAMP . VISITED)
  751. ;; for all LOCs pertaining to that file.
  752. ;; MARKER initially points to LINE and COLUMN in a buffer visiting that file.
  753. ;; Being a marker it sticks to some text, when the buffer grows or shrinks
  754. ;; before that point. VISITED is t if we have jumped there, else nil.
  755. ;; FIXME-omake: TIMESTAMP was used to try and handle "incremental compilation":
  756. ;; `omake -P' polls filesystem for changes and recompiles when a file is
  757. ;; modified using the same *compilation* buffer. this necessitates
  758. ;; re-parsing markers.
  759. ;; (cl-defstruct (compilation--loc
  760. ;; (:constructor nil)
  761. ;; (:copier nil)
  762. ;; (:constructor compilation--make-loc
  763. ;; (file-struct line col marker))
  764. ;; (:conc-name compilation--loc->))
  765. ;; col line file-struct marker timestamp visited)
  766. ;; FIXME: We don't use a defstruct because of compilation-assq which looks up
  767. ;; and creates part of the LOC (only the first cons cell containing the COL).
  768. (defmacro compilation--make-cdrloc (line file-struct marker)
  769. `(list ,line ,file-struct ,marker nil))
  770. (defmacro compilation--loc->col (loc) `(car ,loc))
  771. (defmacro compilation--loc->line (loc) `(cadr ,loc))
  772. (defmacro compilation--loc->file-struct (loc) `(nth 2 ,loc))
  773. (defmacro compilation--loc->marker (loc) `(nth 3 ,loc))
  774. ;; (defmacro compilation--loc->timestamp (loc) `(nth 4 ,loc))
  775. (defmacro compilation--loc->visited (loc) `(nthcdr 5 ,loc))
  776. ;; FILE-STRUCTURE is a list of
  777. ;; ((FILENAME DIRECTORY) FORMATS (LINE LOC ...) ...)
  778. ;; FILENAME is a string parsed from an error message. DIRECTORY is a string
  779. ;; obtained by following directory change messages. DIRECTORY will be nil for
  780. ;; an absolute filename. FORMATS is a list of formats to apply to FILENAME if
  781. ;; a file of that name can't be found.
  782. ;; The rest of the list is an alist of elements with LINE as key. The keys
  783. ;; are either nil or line numbers. If present, nil comes first, followed by
  784. ;; the numbers in decreasing order. The LOCs for each line are again an alist
  785. ;; ordered the same way. Note that the whole file structure is referenced in
  786. ;; every LOC.
  787. (defmacro compilation--make-file-struct (file-spec formats &optional loc-tree)
  788. `(cons ,file-spec (cons ,formats ,loc-tree)))
  789. (defmacro compilation--file-struct->file-spec (fs) `(car ,fs))
  790. (defmacro compilation--file-struct->formats (fs) `(cadr ,fs))
  791. ;; The FORMATS field plays the role of ANCHOR in the loc-tree.
  792. (defmacro compilation--file-struct->loc-tree (fs) `(cdr ,fs))
  793. ;; MESSAGE is a list of (LOC TYPE END-LOC)
  794. ;; TYPE is 0 for info or 1 for warning if the message matcher identified it as
  795. ;; such, 2 otherwise (for a real error). END-LOC is a LOC pointing to the
  796. ;; other end, if the parsed message contained a range. If the end of the
  797. ;; range didn't specify a COLUMN, it defaults to -1, meaning end of line.
  798. ;; These are the value of the `compilation-message' text-properties in the
  799. ;; compilation buffer.
  800. (cl-defstruct (compilation--message
  801. (:constructor nil)
  802. (:copier nil)
  803. ;; (:type list) ;Old representation.
  804. (:constructor compilation--make-message (loc type end-loc))
  805. (:conc-name compilation--message->))
  806. loc type end-loc)
  807. (defvar compilation--previous-directory-cache nil
  808. "A pair (POS . RES) caching the result of previous directory search.
  809. Basically, this pair says that calling
  810. (previous-single-property-change POS \\='compilation-directory)
  811. returned RES, i.e. there is no change of `compilation-directory' between
  812. POS and RES.")
  813. (make-variable-buffer-local 'compilation--previous-directory-cache)
  814. (defun compilation--flush-directory-cache (start _end)
  815. (cond
  816. ((or (not compilation--previous-directory-cache)
  817. (<= (car compilation--previous-directory-cache) start)))
  818. ((or (not (cdr compilation--previous-directory-cache))
  819. (null (marker-buffer (cdr compilation--previous-directory-cache)))
  820. (<= (cdr compilation--previous-directory-cache) start))
  821. (set-marker (car compilation--previous-directory-cache) start))
  822. (t (setq compilation--previous-directory-cache nil))))
  823. (defun compilation--previous-directory (pos)
  824. "Like (previous-single-property-change POS \\='compilation-directory), but faster."
  825. ;; This avoids an N² behavior when there's no/few compilation-directory
  826. ;; entries, in which case each call to previous-single-property-change
  827. ;; ends up having to walk very far back to find the last change.
  828. (if (and compilation--previous-directory-cache
  829. (< pos (car compilation--previous-directory-cache))
  830. (or (null (cdr compilation--previous-directory-cache))
  831. (< (cdr compilation--previous-directory-cache) pos)))
  832. ;; No need to call previous-single-property-change.
  833. (cdr compilation--previous-directory-cache)
  834. (let* ((cache (and compilation--previous-directory-cache
  835. (<= (car compilation--previous-directory-cache) pos)
  836. (car compilation--previous-directory-cache)))
  837. (prev
  838. (previous-single-property-change
  839. pos 'compilation-directory nil cache))
  840. (res
  841. (cond
  842. ((null cache)
  843. (setq compilation--previous-directory-cache
  844. (cons (copy-marker pos) (if prev (copy-marker prev))))
  845. prev)
  846. ((and prev (= prev cache))
  847. (set-marker (car compilation--previous-directory-cache) pos)
  848. (cdr compilation--previous-directory-cache))
  849. (t
  850. (set-marker cache pos)
  851. (setcdr compilation--previous-directory-cache
  852. (copy-marker prev))
  853. prev))))
  854. (if (markerp res) (marker-position res) res))))
  855. ;; Internal function for calculating the text properties of a directory
  856. ;; change message. The compilation-directory property is important, because it
  857. ;; is the stack of nested enter-messages. Relative filenames on the following
  858. ;; lines are relative to the top of the stack.
  859. (defun compilation-directory-properties (idx leave)
  860. (if leave (setq leave (match-end leave)))
  861. ;; find previous stack, and push onto it, or if `leave' pop it
  862. (let ((dir (compilation--previous-directory (match-beginning 0))))
  863. (setq dir (if dir (or (get-text-property (1- dir) 'compilation-directory)
  864. (get-text-property dir 'compilation-directory))))
  865. `(font-lock-face ,(if leave
  866. compilation-leave-directory-face
  867. compilation-enter-directory-face)
  868. compilation-directory ,(if leave
  869. (or (cdr dir)
  870. '(nil)) ; nil only isn't a property-change
  871. (cons (match-string-no-properties idx) dir))
  872. ;; Place a `compilation-message' everywhere we change text-properties
  873. ;; so compilation--remove-properties can know what to remove.
  874. compilation-message ,(compilation--make-message nil 0 nil)
  875. mouse-face highlight
  876. keymap compilation-button-map
  877. help-echo "mouse-2: visit destination directory")))
  878. ;; Data type `reverse-ordered-alist' retriever. This function retrieves the
  879. ;; KEY element from the ALIST, creating it in the right position if not already
  880. ;; present. ALIST structure is
  881. ;; '(ANCHOR (KEY1 ...) (KEY2 ...)... (KEYn ALIST ...))
  882. ;; ANCHOR is ignored, but necessary so that elements can be inserted. KEY1
  883. ;; may be nil. The other KEYs are ordered backwards so that growing line
  884. ;; numbers can be inserted in front and searching can abort after half the
  885. ;; list on average.
  886. (eval-when-compile ;Don't keep it at runtime if not needed.
  887. (defmacro compilation-assq (key alist)
  888. `(let* ((l1 ,alist)
  889. (l2 (cdr l1)))
  890. (car (if (if (null ,key)
  891. (if l2 (null (caar l2)))
  892. (while (if l2 (if (caar l2) (< ,key (caar l2)) t))
  893. (setq l1 l2
  894. l2 (cdr l1)))
  895. (if l2 (eq ,key (caar l2))))
  896. l2
  897. (setcdr l1 (cons (list ,key) l2)))))))
  898. (defun compilation-auto-jump (buffer pos)
  899. (with-current-buffer buffer
  900. (goto-char pos)
  901. (let ((win (get-buffer-window buffer 0)))
  902. (if win (set-window-point win pos)))
  903. (if compilation-auto-jump-to-first-error
  904. (compile-goto-error))))
  905. ;; This function is the central driver, called when font-locking to gather
  906. ;; all information needed to later jump to corresponding source code.
  907. ;; Return a property list with all meta information on this error location.
  908. (defun compilation-error-properties (file line end-line col end-col type fmt)
  909. (unless (text-property-not-all (match-beginning 0) (point)
  910. 'compilation-message nil)
  911. (if file
  912. (when (stringp
  913. (setq file (if (functionp file) (funcall file)
  914. (match-string-no-properties file))))
  915. (let ((dir
  916. (unless (file-name-absolute-p file)
  917. (let ((pos (compilation--previous-directory
  918. (match-beginning 0))))
  919. (when pos
  920. (or (get-text-property (1- pos) 'compilation-directory)
  921. (get-text-property pos 'compilation-directory)))))))
  922. (setq file (cons file (car dir)))))
  923. ;; This message didn't mention one, get it from previous
  924. (let ((prev-pos
  925. ;; Find the previous message.
  926. (previous-single-property-change (point) 'compilation-message)))
  927. (if prev-pos
  928. ;; Get the file structure that belongs to it.
  929. (let* ((prev
  930. (or (get-text-property (1- prev-pos) 'compilation-message)
  931. (get-text-property prev-pos 'compilation-message)))
  932. (prev-file-struct
  933. (and prev
  934. (compilation--loc->file-struct
  935. (compilation--message->loc prev)))))
  936. ;; Construct FILE . DIR from that.
  937. (if prev-file-struct
  938. (setq file (cons (caar prev-file-struct)
  939. (cadr (car prev-file-struct)))))))
  940. (unless file
  941. (setq file '("*unknown*")))))
  942. ;; All of these fields are optional, get them only if we have an index, and
  943. ;; it matched some part of the message.
  944. (and line
  945. (setq line (match-string-no-properties line))
  946. (setq line (string-to-number line)))
  947. (and end-line
  948. (setq end-line (match-string-no-properties end-line))
  949. (setq end-line (string-to-number end-line)))
  950. (if col
  951. (if (functionp col)
  952. (setq col (funcall col))
  953. (and
  954. (setq col (match-string-no-properties col))
  955. (setq col (string-to-number col)))))
  956. (if (and end-col (functionp end-col))
  957. (setq end-col (funcall end-col))
  958. (if (and end-col (setq end-col (match-string-no-properties end-col)))
  959. (setq end-col (- (string-to-number end-col) -1))
  960. (if end-line (setq end-col -1))))
  961. (if (consp type) ; not a static type, check what it is.
  962. (setq type (or (and (car type) (match-end (car type)) 1)
  963. (and (cdr type) (match-end (cdr type)) 0)
  964. 2)))
  965. (when (and compilation-auto-jump-to-next
  966. (>= type compilation-skip-threshold))
  967. (kill-local-variable 'compilation-auto-jump-to-next)
  968. (run-with-timer 0 nil 'compilation-auto-jump
  969. (current-buffer) (match-beginning 0)))
  970. (compilation-internal-error-properties
  971. file line end-line col end-col type fmt)))
  972. (defun compilation-beginning-of-line (&optional n)
  973. "Like `beginning-of-line', but accounts for lines hidden by `selective-display'."
  974. (if (or (not (eq selective-display t))
  975. (null n)
  976. (= n 1))
  977. (beginning-of-line n)
  978. (re-search-forward "[\n\r]" nil 'end (1- n))
  979. (if (< n 0)
  980. (beginning-of-line))))
  981. (defun compilation-move-to-column (col screen)
  982. "Go to column COL on the current line.
  983. If SCREEN is non-nil, columns are screen columns, otherwise, they are
  984. just char-counts."
  985. (setq col (- col compilation-first-column))
  986. (if screen
  987. ;; Presumably, the compilation tool doesn't know about our current
  988. ;; `tab-width' setting, so it probably assumed 8-wide TABs (bug#21038).
  989. (let ((tab-width 8)) (move-to-column (max col 0)))
  990. (goto-char (min (+ (line-beginning-position) col) (line-end-position)))))
  991. (defun compilation-internal-error-properties (file line end-line col end-col type fmts)
  992. "Get the meta-info that will be added as text-properties.
  993. LINE, END-LINE, COL, END-COL are integers or nil.
  994. TYPE can be 0, 1, or 2, meaning error, warning, or just info.
  995. FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME) or nil.
  996. FMTS is a list of format specs for transforming the file name.
  997. (See `compilation-error-regexp-alist'.)"
  998. (unless file (setq file '("*unknown*")))
  999. (let* ((file-struct (compilation-get-file-structure file fmts))
  1000. ;; Get first already existing marker (if any has one, all have one).
  1001. ;; Do this first, as the compilation-assq`s may create new nodes.
  1002. (marker-line ; a line structure
  1003. (cadr (compilation--file-struct->loc-tree file-struct)))
  1004. (marker
  1005. (if marker-line (compilation--loc->marker (cadr marker-line))))
  1006. (screen-columns compilation-error-screen-columns)
  1007. (first-column compilation-first-column)
  1008. end-marker loc end-loc)
  1009. (if (not (and marker (marker-buffer marker)))
  1010. (setq marker nil) ; no valid marker for this file
  1011. (unless line (setq line 1)) ; normalize no linenumber to line 1
  1012. (catch 'marker ; find nearest loc, at least one exists
  1013. (dolist (x (cddr (compilation--file-struct->loc-tree
  1014. file-struct))) ; Loop over remaining lines.
  1015. (if (> (car x) line) ; Still bigger.
  1016. (setq marker-line x)
  1017. (if (> (- (or (car marker-line) 1) line)
  1018. (- line (car x))) ; Current line is nearer.
  1019. (setq marker-line x))
  1020. (throw 'marker t))))
  1021. (setq marker (compilation--loc->marker (cadr marker-line))
  1022. marker-line (or (car marker-line) 1))
  1023. (with-current-buffer (marker-buffer marker)
  1024. (let ((screen-columns
  1025. ;; Obey the compilation-error-screen-columns of the target
  1026. ;; buffer if its major mode set it buffer-locally.
  1027. (if (local-variable-p 'compilation-error-screen-columns)
  1028. compilation-error-screen-columns screen-columns))
  1029. (compilation-first-column
  1030. (if (local-variable-p 'compilation-first-column)
  1031. compilation-first-column first-column)))
  1032. (save-excursion
  1033. (save-restriction
  1034. (widen)
  1035. (goto-char (marker-position marker))
  1036. ;; Set end-marker if appropriate and go to line.
  1037. (if (not (or end-col end-line))
  1038. (compilation-beginning-of-line (- line marker-line -1))
  1039. (compilation-beginning-of-line (- (or end-line line)
  1040. marker-line -1))
  1041. (if (or (null end-col) (< end-col 0))
  1042. (end-of-line)
  1043. (compilation-move-to-column end-col screen-columns))
  1044. (setq end-marker (point-marker))
  1045. (when end-line
  1046. (compilation-beginning-of-line (- line end-line -1))))
  1047. (if col
  1048. (compilation-move-to-column col screen-columns)
  1049. (forward-to-indentation 0))
  1050. (setq marker (point-marker)))))))
  1051. (setq loc (compilation-assq line (compilation--file-struct->loc-tree
  1052. file-struct)))
  1053. (setq end-loc
  1054. (if end-line
  1055. (compilation-assq
  1056. end-col (compilation-assq
  1057. end-line (compilation--file-struct->loc-tree
  1058. file-struct)))
  1059. (if end-col ; use same line element
  1060. (compilation-assq end-col loc))))
  1061. (setq loc (compilation-assq col loc))
  1062. ;; If they are new, make the loc(s) reference the file they point to.
  1063. ;; FIXME-omake: there's a problem with timestamps here: the markers
  1064. ;; relative to which we computed the current `marker' have a timestamp
  1065. ;; almost guaranteed to be different from compilation-buffer-modtime, so if
  1066. ;; we use their timestamp, we'll never use `loc' since the timestamp won't
  1067. ;; match compilation-buffer-modtime, and if we use
  1068. ;; compilation-buffer-modtime then we have different timestamps for
  1069. ;; locations that were computed together, which doesn't make sense either.
  1070. ;; I think this points to a fundamental problem in our approach to the
  1071. ;; "omake -P" problem. --Stef
  1072. (or (cdr loc)
  1073. (setcdr loc (compilation--make-cdrloc line file-struct marker)))
  1074. (if end-loc
  1075. (or (cdr end-loc)
  1076. (setcdr end-loc
  1077. (compilation--make-cdrloc (or end-line line) file-struct
  1078. end-marker))))
  1079. ;; Must start with face
  1080. `(font-lock-face ,compilation-message-face
  1081. compilation-message ,(compilation--make-message loc type end-loc)
  1082. help-echo ,(if col
  1083. "mouse-2: visit this file, line and column"
  1084. (if line
  1085. "mouse-2: visit this file and line"
  1086. "mouse-2: visit this file"))
  1087. keymap compilation-button-map
  1088. mouse-face highlight)))
  1089. (defun compilation--put-prop (matchnum prop val)
  1090. (when (and (integerp matchnum) (match-beginning matchnum))
  1091. (put-text-property
  1092. (match-beginning matchnum) (match-end matchnum)
  1093. prop val)))
  1094. (defun compilation--remove-properties (&optional start end)
  1095. (with-silent-modifications
  1096. ;; When compile.el used font-lock directly, we could just remove all
  1097. ;; our text-properties in one go, but now that we manually place
  1098. ;; font-lock-face, we have to be careful to only remove the font-lock-face
  1099. ;; we placed.
  1100. ;; (remove-list-of-text-properties
  1101. ;; (or start (point-min)) (or end (point-max))
  1102. ;; '(compilation-debug compilation-directory compilation-message
  1103. ;; font-lock-face help-echo mouse-face))
  1104. (let (next)
  1105. (unless start (setq start (point-min)))
  1106. (unless end (setq end (point-max)))
  1107. (compilation--flush-directory-cache start end)
  1108. (while
  1109. (progn
  1110. (setq next (or (next-single-property-change
  1111. start 'compilation-message nil end)
  1112. end))
  1113. (when (get-text-property start 'compilation-message)
  1114. (remove-list-of-text-properties
  1115. start next
  1116. '(compilation-debug compilation-directory compilation-message
  1117. font-lock-face help-echo mouse-face)))
  1118. (< next end))
  1119. (setq start next)))))
  1120. (defun compilation--parse-region (start end)
  1121. (goto-char end)
  1122. (unless (bolp)
  1123. ;; We generally don't like to parse partial lines.
  1124. (cl-assert (eobp))
  1125. (when (let ((proc (get-buffer-process (current-buffer))))
  1126. (and proc (memq (process-status proc) '(run open))))
  1127. (setq end (line-beginning-position))))
  1128. (compilation--remove-properties start end)
  1129. (if compilation-parse-errors-function
  1130. ;; An old package! Try the compatibility code.
  1131. (progn
  1132. (goto-char start)
  1133. (compilation--compat-parse-errors end))
  1134. ;; compilation-directory-matcher is the only part that really needs to be
  1135. ;; parsed sequentially. So we could split it out, handle directories
  1136. ;; like syntax-propertize, and the rest as font-lock-keywords. But since
  1137. ;; we want to have it work even when font-lock is off, we'd then need to
  1138. ;; use our own compilation-parsed text-property to keep track of the parts
  1139. ;; that have already been parsed.
  1140. (goto-char start)
  1141. (while (re-search-forward (car compilation-directory-matcher)
  1142. end t)
  1143. (compilation--flush-directory-cache (match-beginning 0) (match-end 0))
  1144. (when compilation-debug
  1145. (font-lock-append-text-property
  1146. (match-beginning 0) (match-end 0)
  1147. 'compilation-debug
  1148. (vector 'directory compilation-directory-matcher)))
  1149. (dolist (elt (cdr compilation-directory-matcher))
  1150. (add-text-properties (match-beginning (car elt))
  1151. (match-end (car elt))
  1152. (compilation-directory-properties
  1153. (car elt) (cdr elt)))))
  1154. (compilation-parse-errors start end)))
  1155. (defun compilation-parse-errors (start end &rest rules)
  1156. "Parse errors between START and END.
  1157. The errors recognized are the ones specified in RULES which default
  1158. to `compilation-error-regexp-alist' if RULES is nil."
  1159. (dolist (item (or rules compilation-error-regexp-alist))
  1160. (if (symbolp item)
  1161. (setq item (cdr (assq item
  1162. compilation-error-regexp-alist-alist))))
  1163. (let ((file (nth 1 item))
  1164. (line (nth 2 item))
  1165. (col (nth 3 item))
  1166. (type (nth 4 item))
  1167. (pat (car item))
  1168. end-line end-col fmt
  1169. props)
  1170. ;; omake reports some error indented, so skip the indentation.
  1171. ;; another solution is to modify (some?) regexps in
  1172. ;; `compilation-error-regexp-alist'.
  1173. ;; note that omake usage is not limited to ocaml and C (for stubs).
  1174. ;; FIXME-omake: Doing it here seems wrong, at least it should depend on
  1175. ;; whether or not omake's own error messages are recognized.
  1176. (cond
  1177. ((not (memq 'omake compilation-error-regexp-alist)) nil)
  1178. ((string-match "\\`\\([^^]\\|\\^\\( \\*\\|\\[\\)\\)" pat)
  1179. nil) ;; Not anchored or anchored but already allows empty spaces.
  1180. (t (setq pat (concat "^ *" (substring pat 1)))))
  1181. (if (consp file) (setq fmt (cdr file) file (car file)))
  1182. (if (consp line) (setq end-line (cdr line) line (car line)))
  1183. (if (consp col) (setq end-col (cdr col) col (car col)))
  1184. (if (functionp line)
  1185. ;; The old compile.el had here an undocumented hook that
  1186. ;; allowed `line' to be a function that computed the actual
  1187. ;; error location. Let's do our best.
  1188. (progn
  1189. (goto-char start)
  1190. (while (re-search-forward pat end t)
  1191. (save-match-data
  1192. (when compilation-debug
  1193. (font-lock-append-text-property
  1194. (match-beginning 0) (match-end 0)
  1195. 'compilation-debug (vector 'functionp item)))
  1196. (add-text-properties
  1197. (match-beginning 0) (match-end 0)
  1198. (compilation--compat-error-properties
  1199. (funcall line (cons (match-string file)
  1200. (cons default-directory
  1201. (nthcdr 4 item)))
  1202. (if col (match-string col))))))
  1203. (compilation--put-prop
  1204. file 'font-lock-face compilation-error-face)))
  1205. (unless (or (null (nth 5 item)) (integerp (nth 5 item)))
  1206. (error "HYPERLINK should be an integer: %s" (nth 5 item)))
  1207. (goto-char start)
  1208. (while (re-search-forward pat end t)
  1209. (when (setq props (compilation-error-properties
  1210. file line end-line col end-col (or type 2) fmt))
  1211. (when (integerp file)
  1212. (compilation--put-prop
  1213. file 'font-lock-face
  1214. (if (consp type)
  1215. (compilation-face type)
  1216. (symbol-value (aref [compilation-info-face
  1217. compilation-warning-face
  1218. compilation-error-face]
  1219. (or type 2))))))
  1220. (compilation--put-prop
  1221. line 'font-lock-face compilation-line-face)
  1222. (compilation--put-prop
  1223. end-line 'font-lock-face compilation-line-face)
  1224. (compilation--put-prop
  1225. col 'font-lock-face compilation-column-face)
  1226. (compilation--put-prop
  1227. end-col 'font-lock-face compilation-column-face)
  1228. ;; Obey HIGHLIGHT.
  1229. (dolist (extra-item (nthcdr 6 item))
  1230. (let ((mn (pop extra-item)))
  1231. (when (match-beginning mn)
  1232. (let ((face (eval (car extra-item))))
  1233. (cond
  1234. ((null face))
  1235. ((or (symbolp face) (stringp face))
  1236. (put-text-property
  1237. (match-beginning mn) (match-end mn)
  1238. 'font-lock-face face))
  1239. ((and (listp face)
  1240. (eq (car face) 'face)
  1241. (or (symbolp (cadr face))
  1242. (stringp (cadr face))))
  1243. (compilation--put-prop mn 'font-lock-face (cadr face))
  1244. (add-text-properties
  1245. (match-beginning mn) (match-end mn)
  1246. (nthcdr 2 face)))
  1247. (t
  1248. (error "Don't know how to handle face %S"
  1249. face)))))))
  1250. (let ((mn (or (nth 5 item) 0)))
  1251. (when compilation-debug
  1252. (font-lock-append-text-property
  1253. (match-beginning 0) (match-end 0)
  1254. 'compilation-debug (vector 'std item props)))
  1255. (add-text-properties
  1256. (match-beginning mn) (match-end mn)
  1257. (cddr props))
  1258. (font-lock-append-text-property
  1259. (match-beginning mn) (match-end mn)
  1260. 'font-lock-face (cadr props)))))))))
  1261. (defvar compilation--parsed -1)
  1262. (make-variable-buffer-local 'compilation--parsed)
  1263. (defun compilation--ensure-parse (limit)
  1264. "Make sure the text has been parsed up to LIMIT."
  1265. (save-excursion
  1266. (goto-char limit)
  1267. (setq limit (line-beginning-position 2))
  1268. (unless (markerp compilation--parsed)
  1269. ;; We use a marker for compilation--parsed so that users (such as
  1270. ;; grep.el) don't need to flush-parse when they modify the buffer
  1271. ;; in a way that impacts buffer positions but does not require
  1272. ;; re-parsing.
  1273. (setq compilation--parsed (point-min-marker)))
  1274. (when (< compilation--parsed limit)
  1275. (let ((start (max compilation--parsed (point-min))))
  1276. (move-marker compilation--parsed limit)
  1277. (goto-char start)
  1278. (forward-line 0) ;Not line-beginning-position: ignore (comint) fields.
  1279. (while (and (not (bobp))
  1280. (get-text-property (1- (point)) 'compilation-multiline))
  1281. (forward-line -1))
  1282. (with-silent-modifications
  1283. (compilation--parse-region (point) compilation--parsed)))))
  1284. nil)
  1285. (defun compilation--flush-parse (start _end)
  1286. "Mark the region between START and END for re-parsing."
  1287. (if (markerp compilation--parsed)
  1288. (move-marker compilation--parsed (min start compilation--parsed))))
  1289. (defun compilation-mode-font-lock-keywords ()
  1290. "Return expressions to highlight in Compilation mode."
  1291. (append
  1292. '((compilation--ensure-parse))
  1293. compilation-mode-font-lock-keywords))
  1294. (defun compilation-read-command (command)
  1295. (read-shell-command "Compile command: " command
  1296. (if (equal (car compile-history) command)
  1297. '(compile-history . 1)
  1298. 'compile-history)))
  1299. ;;;###autoload
  1300. (defun compile (command &optional comint)
  1301. "Compile the program including the current buffer. Default: run `make'.
  1302. Runs COMMAND, a shell command, in a separate process asynchronously
  1303. with output going to the buffer `*compilation*'.
  1304. You can then use the command \\[next-error] to find the next error message
  1305. and move to the source code that caused it.
  1306. If optional second arg COMINT is t the buffer will be in Comint mode with
  1307. `compilation-shell-minor-mode'.
  1308. Interactively, prompts for the command if the variable
  1309. `compilation-read-command' is non-nil; otherwise uses `compile-command'.
  1310. With prefix arg, always prompts.
  1311. Additionally, with universal prefix arg, compilation buffer will be in
  1312. comint mode, i.e. interactive.
  1313. To run more than one compilation at once, start one then rename
  1314. the `*compilation*' buffer to some other name with
  1315. \\[rename-buffer]. Then _switch buffers_ and start the new compilation.
  1316. It will create a new `*compilation*' buffer.
  1317. On most systems, termination of the main compilation process
  1318. kills its subprocesses.
  1319. The name used for the buffer is actually whatever is returned by
  1320. the function in `compilation-buffer-name-function', so you can set that
  1321. to a function that generates a unique name."
  1322. (interactive
  1323. (list
  1324. (let ((command (eval compile-command)))
  1325. (if (or compilation-read-command current-prefix-arg)
  1326. (compilation-read-command command)
  1327. command))
  1328. (consp current-prefix-arg)))
  1329. (unless (equal command (eval compile-command))
  1330. (setq compile-command command))
  1331. (save-some-buffers (not compilation-ask-about-save)
  1332. compilation-save-buffers-predicate)
  1333. (setq-default compilation-directory default-directory)
  1334. (compilation-start command comint))
  1335. ;; run compile with the default command line
  1336. (defun recompile (&optional edit-command)
  1337. "Re-compile the program including the current buffer.
  1338. If this is run in a Compilation mode buffer, re-use the arguments from the
  1339. original use. Otherwise, recompile using `compile-command'.
  1340. If the optional argument `edit-command' is non-nil, the command can be edited."
  1341. (interactive "P")
  1342. (save-some-buffers (not compilation-ask-about-save)
  1343. compilation-save-buffers-predicate)
  1344. (let ((default-directory (or compilation-directory default-directory))
  1345. (command (eval compile-command)))
  1346. (when edit-command
  1347. (setq command (compilation-read-command (or (car compilation-arguments)
  1348. command)))
  1349. (if compilation-arguments (setcar compilation-arguments command)))
  1350. (apply 'compilation-start (or compilation-arguments (list command)))))
  1351. (defcustom compilation-scroll-output nil
  1352. "Non-nil to scroll the *compilation* buffer window as output appears.
  1353. Setting it causes the Compilation mode commands to put point at the
  1354. end of their output window so that the end of the output is always
  1355. visible rather than the beginning.
  1356. The value `first-error' stops scrolling at the first error, and leaves
  1357. point on its location in the *compilation* buffer."
  1358. :type '(choice (const :tag "No scrolling" nil)
  1359. (const :tag "Scroll compilation output" t)
  1360. (const :tag "Stop scrolling at the first error" first-error))
  1361. :version "20.3"
  1362. :group 'compilation)
  1363. (defun compilation-buffer-name (name-of-mode mode-command name-function)
  1364. "Return the name of a compilation buffer to use.
  1365. If NAME-FUNCTION is non-nil, call it with one argument NAME-OF-MODE
  1366. to determine the buffer name.
  1367. Likewise if `compilation-buffer-name-function' is non-nil.
  1368. If current buffer has the major mode MODE-COMMAND,
  1369. return the name of the current buffer, so that it gets reused.
  1370. Otherwise, construct a buffer name from NAME-OF-MODE."
  1371. (cond (name-function
  1372. (funcall name-function name-of-mode))
  1373. (compilation-buffer-name-function
  1374. (funcall compilation-buffer-name-function name-of-mode))
  1375. ((eq mode-command major-mode)
  1376. (buffer-name))
  1377. (t
  1378. (concat "*" (downcase name-of-mode) "*"))))
  1379. (defcustom compilation-always-kill nil
  1380. "If t, always kill a running compilation process before starting a new one.
  1381. If nil, ask to kill it."
  1382. :type 'boolean
  1383. :version "24.3"
  1384. :group 'compilation)
  1385. ;;;###autoload
  1386. (defun compilation-start (command &optional mode name-function highlight-regexp)
  1387. "Run compilation command COMMAND (low level interface).
  1388. If COMMAND starts with a cd command, that becomes the `default-directory'.
  1389. The rest of the arguments are optional; for them, nil means use the default.
  1390. MODE is the major mode to set in the compilation buffer. Mode
  1391. may also be t meaning use `compilation-shell-minor-mode' under `comint-mode'.
  1392. If NAME-FUNCTION is non-nil, call it with one argument (the mode name)
  1393. to determine the buffer name. Otherwise, the default is to
  1394. reuses the current buffer if it has the proper major mode,
  1395. else use or create a buffer with name based on the major mode.
  1396. If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight
  1397. the matching section of the visited source line; the default is to use the
  1398. global value of `compilation-highlight-regexp'.
  1399. Returns the compilation buffer created."
  1400. (or mode (setq mode 'compilation-mode))
  1401. (let* ((name-of-mode
  1402. (if (eq mode t)
  1403. "compilation"
  1404. (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
  1405. (thisdir default-directory)
  1406. (thisenv compilation-environment)
  1407. outwin outbuf)
  1408. (with-current-buffer
  1409. (setq outbuf
  1410. (get-buffer-create
  1411. (compilation-buffer-name name-of-mode mode name-function)))
  1412. (let ((comp-proc (get-buffer-process (current-buffer))))
  1413. (if comp-proc
  1414. (if (or (not (eq (process-status comp-proc) 'run))
  1415. (eq (process-query-on-exit-flag comp-proc) nil)
  1416. (yes-or-no-p
  1417. (format "A %s process is running; kill it? "
  1418. name-of-mode)))
  1419. (condition-case ()
  1420. (progn
  1421. (interrupt-process comp-proc)
  1422. (sit-for 1)
  1423. (delete-process comp-proc))
  1424. (error nil))
  1425. (error "Cannot have two processes in `%s' at once"
  1426. (buffer-name)))))
  1427. ;; first transfer directory from where M-x compile was called
  1428. (setq default-directory thisdir)
  1429. ;; Make compilation buffer read-only. The filter can still write it.
  1430. ;; Clear out the compilation buffer.
  1431. (let ((inhibit-read-only t)
  1432. (default-directory thisdir))
  1433. ;; Then evaluate a cd command if any, but don't perform it yet, else
  1434. ;; start-command would do it again through the shell: (cd "..") AND
  1435. ;; sh -c "cd ..; make"
  1436. (cd (cond
  1437. ((not (string-match "\\`\\s *cd\\(?:\\s +\\(\\S +?\\|'[^']*'\\|\"\\(?:[^\"`$\\]\\|\\\\.\\)*\"\\)\\)?\\s *[;&\n]"
  1438. command))
  1439. default-directory)
  1440. ((not (match-end 1)) "~")
  1441. ((eq (aref command (match-beginning 1)) ?\')
  1442. (substring command (1+ (match-beginning 1))
  1443. (1- (match-end 1))))
  1444. ((eq (aref command (match-beginning 1)) ?\")
  1445. (replace-regexp-in-string
  1446. "\\\\\\(.\\)" "\\1"
  1447. (substring command (1+ (match-beginning 1))
  1448. (1- (match-end 1)))))
  1449. ;; Try globbing as well (bug#15417).
  1450. (t (let* ((substituted-dir
  1451. (substitute-env-vars (match-string 1 command)))
  1452. ;; FIXME: This also tries to expand `*' that were
  1453. ;; introduced by the envvar expansion!
  1454. (expanded-dir
  1455. (file-expand-wildcards substituted-dir)))
  1456. (if (= (length expanded-dir) 1)
  1457. (car expanded-dir)
  1458. substituted-dir)))))
  1459. (erase-buffer)
  1460. ;; Select the desired mode.
  1461. (if (not (eq mode t))
  1462. (progn
  1463. (buffer-disable-undo)
  1464. (funcall mode))
  1465. (setq buffer-read-only nil)
  1466. (with-no-warnings (comint-mode))
  1467. (compilation-shell-minor-mode))
  1468. ;; Remember the original dir, so we can use it when we recompile.
  1469. ;; default-directory' can't be used reliably for that because it may be
  1470. ;; affected by the special handling of "cd ...;".
  1471. ;; NB: must be done after (funcall mode) as that resets local variables
  1472. (set (make-local-variable 'compilation-directory) thisdir)
  1473. (set (make-local-variable 'compilation-environment) thisenv)
  1474. (if highlight-regexp
  1475. (set (make-local-variable 'compilation-highlight-regexp)
  1476. highlight-regexp))
  1477. (if (or compilation-auto-jump-to-first-error
  1478. (eq compilation-scroll-output 'first-error))
  1479. (set (make-local-variable 'compilation-auto-jump-to-next) t))
  1480. ;; Output a mode setter, for saving and later reloading this buffer.
  1481. (insert "-*- mode: " name-of-mode
  1482. "; default-directory: "
  1483. (prin1-to-string (abbreviate-file-name default-directory))
  1484. " -*-\n"
  1485. (format "%s started at %s\n\n"
  1486. mode-name
  1487. (substring (current-time-string) 0 19))
  1488. command "\n")
  1489. (setq thisdir default-directory))
  1490. (set-buffer-modified-p nil))
  1491. ;; Pop up the compilation buffer.
  1492. ;; http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01638.html
  1493. (setq outwin (display-buffer outbuf '(nil (allow-no-window . t))))
  1494. (with-current-buffer outbuf
  1495. (let ((process-environment
  1496. (append
  1497. compilation-environment
  1498. (if (if (boundp 'system-uses-terminfo);`If' for compiler warning.
  1499. system-uses-terminfo)
  1500. (list "TERM=dumb" "TERMCAP="
  1501. (format "COLUMNS=%d" (window-width)))
  1502. (list "TERM=emacs"
  1503. (format "TERMCAP=emacs:co#%d:tc=unknown:"
  1504. (window-width))))
  1505. (list (format "INSIDE_EMACS=%s,compile" emacs-version))
  1506. (copy-sequence process-environment))))
  1507. (set (make-local-variable 'compilation-arguments)
  1508. (list command mode name-function highlight-regexp))
  1509. (set (make-local-variable 'revert-buffer-function)
  1510. 'compilation-revert-buffer)
  1511. (and outwin
  1512. ;; Forcing the window-start overrides the usual redisplay
  1513. ;; feature of bringing point into view, so setting the
  1514. ;; window-start to top of the buffer risks losing the
  1515. ;; effect of moving point to EOB below, per
  1516. ;; compilation-scroll-output, if the command is long
  1517. ;; enough to push point outside of the window. This
  1518. ;; could happen, e.g., in `rgrep'.
  1519. (not compilation-scroll-output)
  1520. (set-window-start outwin (point-min)))
  1521. ;; Position point as the user will see it.
  1522. (let ((desired-visible-point
  1523. ;; Put it at the end if `compilation-scroll-output' is set.
  1524. (if compilation-scroll-output
  1525. (point-max)
  1526. ;; Normally put it at the top.
  1527. (point-min))))
  1528. (goto-char desired-visible-point)
  1529. (when (and outwin (not (eq outwin (selected-window))))
  1530. (set-window-point outwin desired-visible-point)))
  1531. ;; The setup function is called before compilation-set-window-height
  1532. ;; so it can set the compilation-window-height buffer locally.
  1533. (if compilation-process-setup-function
  1534. (funcall compilation-process-setup-function))
  1535. (and outwin (compilation-set-window-height outwin))
  1536. ;; Start the compilation.
  1537. (if (fboundp 'make-process)
  1538. (let ((proc
  1539. (if (eq mode t)
  1540. ;; comint uses `start-file-process'.
  1541. (get-buffer-process
  1542. (with-no-warnings
  1543. (comint-exec
  1544. outbuf (downcase mode-name)
  1545. (if (file-remote-p default-directory)
  1546. "/bin/sh"
  1547. shell-file-name)
  1548. nil `("-c" ,command))))
  1549. (start-file-process-shell-command (downcase mode-name)
  1550. outbuf command))))
  1551. ;; Make the buffer's mode line show process state.
  1552. (setq mode-line-process
  1553. '(:propertize ":%s" face compilation-mode-line-run))
  1554. ;; Set the process as killable without query by default.
  1555. ;; This allows us to start a new compilation without
  1556. ;; getting prompted.
  1557. (when compilation-always-kill
  1558. (set-process-query-on-exit-flag proc nil))
  1559. (set-process-sentinel proc 'compilation-sentinel)
  1560. (unless (eq mode t)
  1561. ;; Keep the comint filter, since it's needed for proper
  1562. ;; handling of the prompts.
  1563. (set-process-filter proc 'compilation-filter))
  1564. ;; Use (point-max) here so that output comes in
  1565. ;; after the initial text,
  1566. ;; regardless of where the user sees point.
  1567. (set-marker (process-mark proc) (point-max) outbuf)
  1568. (when compilation-disable-input
  1569. (condition-case nil
  1570. (process-send-eof proc)
  1571. ;; The process may have exited already.
  1572. (error nil)))
  1573. (run-hook-with-args 'compilation-start-hook proc)
  1574. (setq compilation-in-progress
  1575. (cons proc compilation-in-progress)))
  1576. ;; No asynchronous processes available.
  1577. (message "Executing `%s'..." command)
  1578. ;; Fake mode line display as if `start-process' were run.
  1579. (setq mode-line-process
  1580. '(:propertize ":run" face compilation-mode-line-run))
  1581. (force-mode-line-update)
  1582. (sit-for 0) ; Force redisplay
  1583. (save-excursion
  1584. ;; Insert the output at the end, after the initial text,
  1585. ;; regardless of where the user sees point.
  1586. (goto-char (point-max))
  1587. (let* ((inhibit-read-only t) ; call-process needs to modify outbuf
  1588. (compilation-filter-start (point))
  1589. (status (call-process shell-file-name nil outbuf nil "-c"
  1590. command)))
  1591. (run-hooks 'compilation-filter-hook)
  1592. (cond ((numberp status)
  1593. (compilation-handle-exit
  1594. 'exit status
  1595. (if (zerop status)
  1596. "finished\n"
  1597. (format "exited abnormally with code %d\n" status))))
  1598. ((stringp status)
  1599. (compilation-handle-exit 'signal status
  1600. (concat status "\n")))
  1601. (t
  1602. (compilation-handle-exit 'bizarre status status)))))
  1603. (set-buffer-modified-p nil)
  1604. (message "Executing `%s'...done" command)))
  1605. ;; Now finally cd to where the shell started make/grep/...
  1606. (setq default-directory thisdir)
  1607. ;; The following form selected outwin ever since revision 1.183,
  1608. ;; so possibly messing up point in some other window (bug#1073).
  1609. ;; Moved into the scope of with-current-buffer, though still with
  1610. ;; complete disregard for the case when compilation-scroll-output
  1611. ;; equals 'first-error (martin 2008-10-04).
  1612. (when compilation-scroll-output
  1613. (goto-char (point-max))))
  1614. ;; Make it so the next C-x ` will use this buffer.
  1615. (setq next-error-last-buffer outbuf)))
  1616. (defun compilation-set-window-height (window)
  1617. "Set the height of WINDOW according to `compilation-window-height'."
  1618. (let ((height (buffer-local-value 'compilation-window-height (window-buffer window))))
  1619. (and height
  1620. (window-full-width-p window)
  1621. ;; If window is alone in its frame, aside from a minibuffer,
  1622. ;; don't change its height.
  1623. (not (eq window (frame-root-window (window-frame window))))
  1624. ;; Stef said that doing the saves in this order is safer:
  1625. (save-excursion
  1626. (save-selected-window
  1627. (select-window window)
  1628. (enlarge-window (- height (window-height))))))))
  1629. (defvar compilation-menu-map
  1630. (let ((map (make-sparse-keymap "Errors"))
  1631. (opt-map (make-sparse-keymap "Skip")))
  1632. (define-key map [stop-subjob]
  1633. '(menu-item "Stop Compilation" kill-compilation
  1634. :help "Kill the process made by the M-x compile or M-x grep commands"))
  1635. (define-key map [compilation-mode-separator3]
  1636. '("----" . nil))
  1637. (define-key map [compilation-next-error-follow-minor-mode]
  1638. '(menu-item
  1639. "Auto Error Display" next-error-follow-minor-mode
  1640. :help "Display the error under cursor when moving the cursor"
  1641. :button (:toggle . next-error-follow-minor-mode)))
  1642. (define-key map [compilation-skip]
  1643. (cons "Skip Less Important Messages" opt-map))
  1644. (define-key opt-map [compilation-skip-none]
  1645. '(menu-item "Don't Skip Any Messages"
  1646. (lambda ()
  1647. (interactive)
  1648. (customize-set-variable 'compilation-skip-threshold 0))
  1649. :help "Do not skip any type of messages"
  1650. :button (:radio . (eq compilation-skip-threshold 0))))
  1651. (define-key opt-map [compilation-skip-info]
  1652. '(menu-item "Skip Info"
  1653. (lambda ()
  1654. (interactive)
  1655. (customize-set-variable 'compilation-skip-threshold 1))
  1656. :help "Skip anything less than warning"
  1657. :button (:radio . (eq compilation-skip-threshold 1))))
  1658. (define-key opt-map [compilation-skip-warning-and-info]
  1659. '(menu-item "Skip Warnings and Info"
  1660. (lambda ()
  1661. (interactive)
  1662. (customize-set-variable 'compilation-skip-threshold 2))
  1663. :help "Skip over Warnings and Info, stop for errors"
  1664. :button (:radio . (eq compilation-skip-threshold 2))))
  1665. (define-key map [compilation-mode-separator2]
  1666. '("----" . nil))
  1667. (define-key map [compilation-first-error]
  1668. '(menu-item "First Error" first-error
  1669. :help "Restart at the first error, visit corresponding source code"))
  1670. (define-key map [compilation-previous-error]
  1671. '(menu-item "Previous Error" previous-error
  1672. :help "Visit previous `next-error' message and corresponding source code"))
  1673. (define-key map [compilation-next-error]
  1674. '(menu-item "Next Error" next-error
  1675. :help "Visit next `next-error' message and corresponding source code"))
  1676. map))
  1677. (defvar compilation-minor-mode-map
  1678. (let ((map (make-sparse-keymap)))
  1679. (set-keymap-parent map special-mode-map)
  1680. (define-key map [mouse-2] 'compile-goto-error)
  1681. (define-key map [follow-link] 'mouse-face)
  1682. (define-key map "\C-c\C-c" 'compile-goto-error)
  1683. (define-key map "\C-m" 'compile-goto-error)
  1684. (define-key map "\C-o" 'compilation-display-error)
  1685. (define-key map "\C-c\C-k" 'kill-compilation)
  1686. (define-key map "\M-n" 'compilation-next-error)
  1687. (define-key map "\M-p" 'compilation-previous-error)
  1688. (define-key map "\M-{" 'compilation-previous-file)
  1689. (define-key map "\M-}" 'compilation-next-file)
  1690. (define-key map "g" 'recompile) ; revert
  1691. ;; Set up the menu-bar
  1692. (define-key map [menu-bar compilation]
  1693. (cons "Errors" compilation-menu-map))
  1694. map)
  1695. "Keymap for `compilation-minor-mode'.")
  1696. (defvar compilation-shell-minor-mode-map
  1697. (let ((map (make-sparse-keymap)))
  1698. (define-key map "\M-\C-m" 'compile-goto-error)
  1699. (define-key map "\M-\C-n" 'compilation-next-error)
  1700. (define-key map "\M-\C-p" 'compilation-previous-error)
  1701. (define-key map "\M-{" 'compilation-previous-file)
  1702. (define-key map "\M-}" 'compilation-next-file)
  1703. ;; Set up the menu-bar
  1704. (define-key map [menu-bar compilation]
  1705. (cons "Errors" compilation-menu-map))
  1706. map)
  1707. "Keymap for `compilation-shell-minor-mode'.")
  1708. (defvar compilation-button-map
  1709. (let ((map (make-sparse-keymap)))
  1710. (define-key map [mouse-2] 'compile-goto-error)
  1711. (define-key map [follow-link] 'mouse-face)
  1712. (define-key map "\C-m" 'compile-goto-error)
  1713. map)
  1714. "Keymap for compilation-message buttons.")
  1715. (fset 'compilation-button-map compilation-button-map)
  1716. (defvar compilation-mode-map
  1717. (let ((map (make-sparse-keymap)))
  1718. ;; Don't inherit from compilation-minor-mode-map,
  1719. ;; because that introduces a menu bar item we don't want.
  1720. ;; That confuses C-down-mouse-3.
  1721. (set-keymap-parent map special-mode-map)
  1722. (define-key map [mouse-2] 'compile-goto-error)
  1723. (define-key map [follow-link] 'mouse-face)
  1724. (define-key map "\C-c\C-c" 'compile-goto-error)
  1725. (define-key map "\C-m" 'compile-goto-error)
  1726. (define-key map "\C-o" 'compilation-display-error)
  1727. (define-key map "\C-c\C-k" 'kill-compilation)
  1728. (define-key map "\M-n" 'compilation-next-error)
  1729. (define-key map "\M-p" 'compilation-previous-error)
  1730. (define-key map "\M-{" 'compilation-previous-file)
  1731. (define-key map "\M-}" 'compilation-next-file)
  1732. (define-key map "\t" 'compilation-next-error)
  1733. (define-key map [backtab] 'compilation-previous-error)
  1734. (define-key map "g" 'recompile) ; revert
  1735. (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
  1736. ;; Set up the menu-bar
  1737. (let ((submap (make-sparse-keymap "Compile")))
  1738. (define-key map [menu-bar compilation]
  1739. (cons "Compile" submap))
  1740. (set-keymap-parent submap compilation-menu-map))
  1741. (define-key map [menu-bar compilation compilation-separator2]
  1742. '("----" . nil))
  1743. (define-key map [menu-bar compilation compilation-grep]
  1744. '(menu-item "Search Files (grep)..." grep
  1745. :help "Run grep, with user-specified args, and collect output in a buffer"))
  1746. (define-key map [menu-bar compilation compilation-recompile]
  1747. '(menu-item "Recompile" recompile
  1748. :help "Re-compile the program including the current buffer"))
  1749. (define-key map [menu-bar compilation compilation-compile]
  1750. '(menu-item "Compile..." compile
  1751. :help "Compile the program including the current buffer. Default: run `make'"))
  1752. map)
  1753. "Keymap for compilation log buffers.
  1754. `compilation-minor-mode-map' is a parent of this.")
  1755. (defvar compilation-mode-tool-bar-map
  1756. ;; When bootstrapping, tool-bar-map is not properly initialized yet,
  1757. ;; so don't do anything.
  1758. (when (keymapp tool-bar-map)
  1759. (let ((map (copy-keymap tool-bar-map)))
  1760. (define-key map [undo] nil)
  1761. (define-key map [separator-2] nil)
  1762. (define-key-after map [separator-compile] menu-bar-separator)
  1763. (tool-bar-local-item
  1764. "left-arrow" 'previous-error-no-select 'previous-error-no-select map
  1765. :rtl "right-arrow"
  1766. :help "Goto previous error")
  1767. (tool-bar-local-item
  1768. "right-arrow" 'next-error-no-select 'next-error-no-select map
  1769. :rtl "left-arrow"
  1770. :help "Goto next error")
  1771. (tool-bar-local-item
  1772. "cancel" 'kill-compilation 'kill-compilation map
  1773. :enable '(let ((buffer (compilation-find-buffer)))
  1774. (get-buffer-process buffer))
  1775. :help "Stop compilation")
  1776. (tool-bar-local-item
  1777. "refresh" 'recompile 'recompile map
  1778. :help "Restart compilation")
  1779. map)))
  1780. (put 'compilation-mode 'mode-class 'special)
  1781. ;;;###autoload
  1782. (defun compilation-mode (&optional name-of-mode)
  1783. "Major mode for compilation log buffers.
  1784. \\<compilation-mode-map>To visit the source for a line-numbered error,
  1785. move point to the error message line and type \\[compile-goto-error].
  1786. To kill the compilation, type \\[kill-compilation].
  1787. Runs `compilation-mode-hook' with `run-mode-hooks' (which see).
  1788. \\{compilation-mode-map}"
  1789. (interactive)
  1790. (kill-all-local-variables)
  1791. (use-local-map compilation-mode-map)
  1792. ;; Let windows scroll along with the output.
  1793. (set (make-local-variable 'window-point-insertion-type) t)
  1794. (set (make-local-variable 'tool-bar-map) compilation-mode-tool-bar-map)
  1795. (setq major-mode 'compilation-mode ; FIXME: Use define-derived-mode.
  1796. mode-name (or name-of-mode "Compilation"))
  1797. (set (make-local-variable 'page-delimiter)
  1798. compilation-page-delimiter)
  1799. ;; (set (make-local-variable 'compilation-buffer-modtime) nil)
  1800. (compilation-setup)
  1801. ;; Turn off deferred fontifications in the compilation buffer, if
  1802. ;; the user turned them on globally. This is because idle timers
  1803. ;; aren't re-run after receiving input from a subprocess, so the
  1804. ;; buffer is left unfontified after the compilation exits, until
  1805. ;; some other input event happens.
  1806. (set (make-local-variable 'jit-lock-defer-time) nil)
  1807. (setq buffer-read-only t)
  1808. (run-mode-hooks 'compilation-mode-hook))
  1809. ;;;###autoload
  1810. (put 'define-compilation-mode 'doc-string-elt 3)
  1811. (defmacro define-compilation-mode (mode name doc &rest body)
  1812. "This is like `define-derived-mode' without the PARENT argument.
  1813. The parent is always `compilation-mode' and the customizable `compilation-...'
  1814. variables are also set from the name of the mode you have chosen,
  1815. by replacing the first word, e.g., `compilation-scroll-output' from
  1816. `grep-scroll-output' if that variable exists."
  1817. (let ((mode-name (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
  1818. `(define-derived-mode ,mode compilation-mode ,name
  1819. ,doc
  1820. ,@(mapcar (lambda (v)
  1821. (setq v (cons v
  1822. (intern-soft (replace-regexp-in-string
  1823. "^compilation" mode-name
  1824. (symbol-name v)))))
  1825. (and (cdr v)
  1826. (or (boundp (cdr v))
  1827. ;; FIXME: This is hackish, using undocumented info.
  1828. (if (boundp 'byte-compile-bound-variables)
  1829. (memq (cdr v) byte-compile-bound-variables)))
  1830. `(set (make-local-variable ',(car v)) ,(cdr v))))
  1831. '(compilation-buffer-name-function
  1832. compilation-directory-matcher
  1833. compilation-error
  1834. compilation-error-regexp-alist
  1835. compilation-error-regexp-alist-alist
  1836. compilation-error-screen-columns
  1837. compilation-finish-function
  1838. compilation-finish-functions
  1839. compilation-first-column
  1840. compilation-mode-font-lock-keywords
  1841. compilation-page-delimiter
  1842. compilation-parse-errors-filename-function
  1843. compilation-process-setup-function
  1844. compilation-scroll-output
  1845. compilation-search-path
  1846. compilation-skip-threshold
  1847. compilation-window-height))
  1848. ,@body)))
  1849. (defun compilation-revert-buffer (ignore-auto noconfirm)
  1850. (if buffer-file-name
  1851. (let (revert-buffer-function)
  1852. (revert-buffer ignore-auto noconfirm))
  1853. (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
  1854. (apply 'compilation-start compilation-arguments))))
  1855. (defvar compilation-current-error nil
  1856. "Marker to the location from where the next error will be found.
  1857. The global commands next/previous/first-error/goto-error use this.")
  1858. (defvar compilation-messages-start nil
  1859. "Buffer position of the beginning of the compilation messages.
  1860. If nil, use the beginning of buffer.")
  1861. (defun compilation-setup (&optional minor)
  1862. "Prepare the buffer for the compilation parsing commands to work.
  1863. Optional argument MINOR indicates this is called from
  1864. `compilation-minor-mode'."
  1865. (make-local-variable 'compilation-current-error)
  1866. (make-local-variable 'compilation-messages-start)
  1867. (make-local-variable 'compilation-error-screen-columns)
  1868. (make-local-variable 'overlay-arrow-position)
  1869. (set (make-local-variable 'overlay-arrow-string) "")
  1870. (setq next-error-overlay-arrow-position nil)
  1871. (add-hook 'kill-buffer-hook
  1872. (lambda () (setq next-error-overlay-arrow-position nil)) nil t)
  1873. ;; Note that compilation-next-error-function is for interfacing
  1874. ;; with the next-error function in simple.el, and it's only
  1875. ;; coincidentally named similarly to compilation-next-error.
  1876. (setq next-error-function 'compilation-next-error-function)
  1877. (set (make-local-variable 'comint-file-name-prefix)
  1878. (or (file-remote-p default-directory) ""))
  1879. (set (make-local-variable 'compilation-locs)
  1880. (make-hash-table :test 'equal :weakness 'value))
  1881. ;; It's generally preferable to use after-change-functions since they
  1882. ;; can be subject to combine-after-change-calls, but if we do that, we risk
  1883. ;; running our hook after font-lock, resulting in incorrect refontification.
  1884. (add-hook 'before-change-functions 'compilation--flush-parse nil t)
  1885. ;; Also for minor mode, since it's not permanent-local.
  1886. (add-hook 'change-major-mode-hook #'compilation--remove-properties nil t)
  1887. (if minor
  1888. (progn
  1889. (font-lock-add-keywords nil (compilation-mode-font-lock-keywords))
  1890. (font-lock-flush))
  1891. (setq font-lock-defaults '(compilation-mode-font-lock-keywords t))))
  1892. (defun compilation--unsetup ()
  1893. ;; Only for minor mode.
  1894. (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
  1895. (remove-hook 'before-change-functions 'compilation--flush-parse t)
  1896. (kill-local-variable 'compilation--parsed)
  1897. (compilation--remove-properties)
  1898. (font-lock-flush))
  1899. ;;;###autoload
  1900. (define-minor-mode compilation-shell-minor-mode
  1901. "Toggle Compilation Shell minor mode.
  1902. With a prefix argument ARG, enable Compilation Shell minor mode
  1903. if ARG is positive, and disable it otherwise. If called from
  1904. Lisp, enable the mode if ARG is omitted or nil.
  1905. When Compilation Shell minor mode is enabled, all the
  1906. error-parsing commands of the Compilation major mode are
  1907. available but bound to keys that don't collide with Shell mode.
  1908. See `compilation-mode'."
  1909. nil " Shell-Compile"
  1910. :group 'compilation
  1911. (if compilation-shell-minor-mode
  1912. (compilation-setup t)
  1913. (compilation--unsetup)))
  1914. ;;;###autoload
  1915. (define-minor-mode compilation-minor-mode
  1916. "Toggle Compilation minor mode.
  1917. With a prefix argument ARG, enable Compilation minor mode if ARG
  1918. is positive, and disable it otherwise. If called from Lisp,
  1919. enable the mode if ARG is omitted or nil.
  1920. When Compilation minor mode is enabled, all the error-parsing
  1921. commands of Compilation major mode are available. See
  1922. `compilation-mode'."
  1923. nil " Compilation"
  1924. :group 'compilation
  1925. (if compilation-minor-mode
  1926. (compilation-setup t)
  1927. (compilation--unsetup)))
  1928. (defun compilation-handle-exit (process-status exit-status msg)
  1929. "Write MSG in the current buffer and hack its `mode-line-process'."
  1930. (let ((inhibit-read-only t)
  1931. (status (if compilation-exit-message-function
  1932. (funcall compilation-exit-message-function
  1933. process-status exit-status msg)
  1934. (cons msg exit-status)))
  1935. (omax (point-max))
  1936. (opoint (point))
  1937. (cur-buffer (current-buffer)))
  1938. ;; Record where we put the message, so we can ignore it later on.
  1939. (goto-char omax)
  1940. (insert ?\n mode-name " " (car status))
  1941. (if (and (numberp compilation-window-height)
  1942. (zerop compilation-window-height))
  1943. (message "%s" (cdr status)))
  1944. (if (bolp)
  1945. (forward-char -1))
  1946. (insert " at " (substring (current-time-string) 0 19))
  1947. (goto-char (point-max))
  1948. ;; Prevent that message from being recognized as a compilation error.
  1949. (add-text-properties omax (point)
  1950. (append '(compilation-handle-exit t) nil))
  1951. (setq mode-line-process
  1952. (let ((out-string (format ":%s [%s]" process-status (cdr status)))
  1953. (msg (format "%s %s" mode-name
  1954. (replace-regexp-in-string "\n?$" ""
  1955. (car status)))))
  1956. (message "%s" msg)
  1957. (propertize out-string
  1958. 'help-echo msg
  1959. 'face (if (> exit-status 0)
  1960. 'compilation-mode-line-fail
  1961. 'compilation-mode-line-exit))))
  1962. ;; Force mode line redisplay soon.
  1963. (force-mode-line-update)
  1964. (if (and opoint (< opoint omax))
  1965. (goto-char opoint))
  1966. (with-no-warnings
  1967. (if compilation-finish-function
  1968. (funcall compilation-finish-function cur-buffer msg)))
  1969. (run-hook-with-args 'compilation-finish-functions cur-buffer msg)))
  1970. ;; Called when compilation process changes state.
  1971. (defun compilation-sentinel (proc msg)
  1972. "Sentinel for compilation buffers."
  1973. (if (memq (process-status proc) '(exit signal))
  1974. (let ((buffer (process-buffer proc)))
  1975. (if (null (buffer-name buffer))
  1976. ;; buffer killed
  1977. (set-process-buffer proc nil)
  1978. (with-current-buffer buffer
  1979. ;; Write something in the compilation buffer
  1980. ;; and hack its mode line.
  1981. (compilation-handle-exit (process-status proc)
  1982. (process-exit-status proc)
  1983. msg)
  1984. ;; Since the buffer and mode line will show that the
  1985. ;; process is dead, we can delete it now. Otherwise it
  1986. ;; will stay around until M-x list-processes.
  1987. (delete-process proc)))
  1988. (setq compilation-in-progress (delq proc compilation-in-progress)))))
  1989. (defun compilation-filter (proc string)
  1990. "Process filter for compilation buffers.
  1991. Just inserts the text,
  1992. handles carriage motion (see `comint-inhibit-carriage-motion'),
  1993. and runs `compilation-filter-hook'."
  1994. (when (buffer-live-p (process-buffer proc))
  1995. (with-current-buffer (process-buffer proc)
  1996. (let ((inhibit-read-only t)
  1997. ;; `save-excursion' doesn't use the right insertion-type for us.
  1998. (pos (copy-marker (point) t))
  1999. ;; `save-restriction' doesn't use the right insertion type either:
  2000. ;; If we are inserting at the end of the accessible part of the
  2001. ;; buffer, keep the inserted text visible.
  2002. (min (point-min-marker))
  2003. (max (copy-marker (point-max) t))
  2004. (compilation-filter-start (marker-position (process-mark proc))))
  2005. (unwind-protect
  2006. (progn
  2007. (widen)
  2008. (goto-char compilation-filter-start)
  2009. ;; We used to use `insert-before-markers', so that windows with
  2010. ;; point at `process-mark' scroll along with the output, but we
  2011. ;; now use window-point-insertion-type instead.
  2012. (insert string)
  2013. (unless comint-inhibit-carriage-motion
  2014. (comint-carriage-motion (process-mark proc) (point)))
  2015. (set-marker (process-mark proc) (point))
  2016. ;; (set (make-local-variable 'compilation-buffer-modtime)
  2017. ;; (current-time))
  2018. (run-hooks 'compilation-filter-hook))
  2019. (goto-char pos)
  2020. (narrow-to-region min max)
  2021. (set-marker pos nil)
  2022. (set-marker min nil)
  2023. (set-marker max nil))))))
  2024. ;;; test if a buffer is a compilation buffer, assuming we're in the buffer
  2025. (defsubst compilation-buffer-internal-p ()
  2026. "Test if inside a compilation buffer."
  2027. (local-variable-p 'compilation-locs))
  2028. ;;; test if a buffer is a compilation buffer, using compilation-buffer-internal-p
  2029. (defsubst compilation-buffer-p (buffer)
  2030. "Test if BUFFER is a compilation buffer."
  2031. (with-current-buffer buffer
  2032. (compilation-buffer-internal-p)))
  2033. (defmacro compilation-loop (< property-change 1+ error limit)
  2034. `(let (opt)
  2035. (while (,< n 0)
  2036. (setq opt pt)
  2037. (or (setq pt (,property-change pt 'compilation-message))
  2038. ;; Handle the case where where the first error message is
  2039. ;; at the start of the buffer, and n < 0.
  2040. (if (or (eq (get-text-property ,limit 'compilation-message)
  2041. (get-text-property opt 'compilation-message))
  2042. (eq pt opt))
  2043. (user-error ,error compilation-error)
  2044. (setq pt ,limit)))
  2045. ;; prop 'compilation-message usually has 2 changes, on and off, so
  2046. ;; re-search if off
  2047. (or (setq msg (get-text-property pt 'compilation-message))
  2048. (if (setq pt (,property-change pt 'compilation-message nil ,limit))
  2049. (setq msg (get-text-property pt 'compilation-message)))
  2050. (user-error ,error compilation-error))
  2051. (or (< (compilation--message->type msg) compilation-skip-threshold)
  2052. (if different-file
  2053. (eq (prog1 last
  2054. (setq last (compilation--loc->file-struct
  2055. (compilation--message->loc msg))))
  2056. last))
  2057. (if compilation-skip-visited
  2058. (compilation--loc->visited (compilation--message->loc msg)))
  2059. (if compilation-skip-to-next-location
  2060. (eq (compilation--message->loc msg) loc))
  2061. ;; count this message only if none of the above are true
  2062. (setq n (,1+ n))))))
  2063. (defun compilation-next-single-property-change (position prop
  2064. &optional object limit)
  2065. (let (parsed res)
  2066. (while (progn
  2067. ;; We parse the buffer here "on-demand" by chunks of 500 chars.
  2068. ;; But we could also just parse the whole buffer.
  2069. (compilation--ensure-parse
  2070. (setq parsed (max compilation--parsed
  2071. (min (+ position 500)
  2072. (or limit (point-max))))))
  2073. (and (or (not (setq res (next-single-property-change
  2074. position prop object limit)))
  2075. (eq res limit))
  2076. (< position (or limit (point-max)))))
  2077. (setq position parsed))
  2078. res))
  2079. (defun compilation-next-error (n &optional different-file pt)
  2080. "Move point to the next error in the compilation buffer.
  2081. This function does NOT find the source line like \\[next-error].
  2082. Prefix arg N says how many error messages to move forwards (or
  2083. backwards, if negative).
  2084. Optional arg DIFFERENT-FILE, if non-nil, means find next error for a
  2085. file that is different from the current one.
  2086. Optional arg PT, if non-nil, specifies the value of point to start
  2087. looking for the next message."
  2088. (interactive "p")
  2089. (or (compilation-buffer-p (current-buffer))
  2090. (error "Not in a compilation buffer"))
  2091. (or pt (setq pt (point)))
  2092. (compilation--ensure-parse pt)
  2093. (let* ((msg (get-text-property pt 'compilation-message))
  2094. ;; `loc', `msg', and `last' are used by the compilation-loop macro.
  2095. (loc (and msg (compilation--message->loc msg)))
  2096. last)
  2097. (if (zerop n)
  2098. (unless (or msg ; find message near here
  2099. (setq msg (get-text-property (max (1- pt) (point-min))
  2100. 'compilation-message)))
  2101. (setq pt (previous-single-property-change pt 'compilation-message nil
  2102. (line-beginning-position)))
  2103. (unless (setq msg (get-text-property (max (1- pt) (point-min))
  2104. 'compilation-message))
  2105. (setq pt (compilation-next-single-property-change
  2106. pt 'compilation-message nil
  2107. (line-end-position)))
  2108. (or (setq msg (get-text-property pt 'compilation-message))
  2109. (setq pt (point)))))
  2110. (setq last (compilation--loc->file-struct loc))
  2111. (if (>= n 0)
  2112. (compilation-loop > compilation-next-single-property-change 1-
  2113. (if (get-buffer-process (current-buffer))
  2114. "No more %ss yet"
  2115. "Moved past last %s")
  2116. (point-max))
  2117. ;; Don't move "back" to message at or before point.
  2118. ;; Pass an explicit (point-min) to make sure pt is non-nil.
  2119. (setq pt (previous-single-property-change
  2120. pt 'compilation-message nil (point-min)))
  2121. (compilation-loop < previous-single-property-change 1+
  2122. "Moved back before first %s" (point-min))))
  2123. (goto-char pt)
  2124. (or msg
  2125. (error "No %s here" compilation-error))))
  2126. (defun compilation-previous-error (n)
  2127. "Move point to the previous error in the compilation buffer.
  2128. Prefix arg N says how many error messages to move backwards (or
  2129. forwards, if negative).
  2130. Does NOT find the source line like \\[previous-error]."
  2131. (interactive "p")
  2132. (compilation-next-error (- n)))
  2133. (defun compilation-next-file (n)
  2134. "Move point to the next error for a different file than the current one.
  2135. Prefix arg N says how many files to move forwards (or backwards, if negative)."
  2136. (interactive "p")
  2137. (compilation-next-error n t))
  2138. (defun compilation-previous-file (n)
  2139. "Move point to the previous error for a different file than the current one.
  2140. Prefix arg N says how many files to move backwards (or forwards, if negative)."
  2141. (interactive "p")
  2142. (compilation-next-file (- n)))
  2143. (defun compilation-display-error ()
  2144. "Display the source for current error in another window."
  2145. (interactive)
  2146. (setq compilation-current-error (point))
  2147. (next-error-no-select 0))
  2148. (defun kill-compilation ()
  2149. "Kill the process made by the \\[compile] or \\[grep] commands."
  2150. (interactive)
  2151. (let ((buffer (compilation-find-buffer)))
  2152. (if (get-buffer-process buffer)
  2153. (interrupt-process (get-buffer-process buffer))
  2154. (error "The %s process is not running" (downcase mode-name)))))
  2155. (defalias 'compile-mouse-goto-error 'compile-goto-error)
  2156. (defun compile-goto-error (&optional event)
  2157. "Visit the source for the error message at point.
  2158. Use this command in a compilation log buffer."
  2159. (interactive (list last-input-event))
  2160. (if event (posn-set-point (event-end event)))
  2161. (or (compilation-buffer-p (current-buffer))
  2162. (error "Not in a compilation buffer"))
  2163. (compilation--ensure-parse (point))
  2164. (if (get-text-property (point) 'compilation-directory)
  2165. (dired-other-window
  2166. (car (get-text-property (point) 'compilation-directory)))
  2167. (setq compilation-current-error (point))
  2168. (next-error-internal)))
  2169. ;; This is mostly unused, but we keep it for the sake of some external
  2170. ;; packages which seem to make use of it.
  2171. (defun compilation-find-buffer (&optional avoid-current)
  2172. "Return a compilation buffer.
  2173. If AVOID-CURRENT is nil, and the current buffer is a compilation buffer,
  2174. return it. If AVOID-CURRENT is non-nil, return the current buffer only
  2175. as a last resort."
  2176. (if (and (compilation-buffer-internal-p) (not avoid-current))
  2177. (current-buffer)
  2178. (next-error-find-buffer avoid-current 'compilation-buffer-internal-p)))
  2179. ;;;###autoload
  2180. (defun compilation-next-error-function (n &optional reset)
  2181. "Advance to the next error message and visit the file where the error was.
  2182. This is the value of `next-error-function' in Compilation buffers."
  2183. (interactive "p")
  2184. (when reset
  2185. (setq compilation-current-error nil))
  2186. (let* ((screen-columns compilation-error-screen-columns)
  2187. (first-column compilation-first-column)
  2188. (last 1)
  2189. (msg (compilation-next-error (or n 1) nil
  2190. (or compilation-current-error
  2191. compilation-messages-start
  2192. (point-min))))
  2193. (loc (compilation--message->loc msg))
  2194. (end-loc (compilation--message->end-loc msg))
  2195. (marker (point-marker)))
  2196. (setq compilation-current-error (point-marker)
  2197. overlay-arrow-position
  2198. (if (bolp)
  2199. compilation-current-error
  2200. (copy-marker (line-beginning-position))))
  2201. ;; If loc contains no marker, no error in that file has been visited.
  2202. ;; If the marker is invalid the buffer has been killed.
  2203. ;; So, recalculate all markers for that file.
  2204. (unless (and (compilation--loc->marker loc)
  2205. (marker-buffer (compilation--loc->marker loc))
  2206. ;; FIXME-omake: For "omake -P", which automatically recompiles
  2207. ;; when the file is modified, the line numbers of new output
  2208. ;; may not be related to line numbers from earlier output
  2209. ;; (earlier markers), so we used to try to detect it here and
  2210. ;; force a reparse. But that caused more problems elsewhere,
  2211. ;; so instead we now flush the file-structure when we see
  2212. ;; omake's message telling it's about to recompile a file.
  2213. ;; (or (null (compilation--loc->timestamp loc)) ;A fake-loc
  2214. ;; (equal (compilation--loc->timestamp loc)
  2215. ;; (setq timestamp compilation-buffer-modtime)))
  2216. )
  2217. (with-current-buffer
  2218. (apply #'compilation-find-file
  2219. marker
  2220. (caar (compilation--loc->file-struct loc))
  2221. (cadr (car (compilation--loc->file-struct loc)))
  2222. (compilation--file-struct->formats
  2223. (compilation--loc->file-struct loc)))
  2224. (let ((screen-columns
  2225. ;; Obey the compilation-error-screen-columns of the target
  2226. ;; buffer if its major mode set it buffer-locally.
  2227. (if (local-variable-p 'compilation-error-screen-columns)
  2228. compilation-error-screen-columns screen-columns))
  2229. (compilation-first-column
  2230. (if (local-variable-p 'compilation-first-column)
  2231. compilation-first-column first-column)))
  2232. (save-restriction
  2233. (widen)
  2234. (goto-char (point-min))
  2235. ;; Treat file's found lines in forward order, 1 by 1.
  2236. (dolist (line (reverse (cddr (compilation--loc->file-struct loc))))
  2237. (when (car line) ; else this is a filename w/o a line#
  2238. (compilation-beginning-of-line (- (car line) last -1))
  2239. (setq last (car line)))
  2240. ;; Treat line's found columns and store/update a marker for each.
  2241. (dolist (col (cdr line))
  2242. (if (compilation--loc->col col)
  2243. (if (eq (compilation--loc->col col) -1)
  2244. ;; Special case for range end.
  2245. (end-of-line)
  2246. (compilation-move-to-column (compilation--loc->col col)
  2247. screen-columns))
  2248. (beginning-of-line)
  2249. (skip-chars-forward " \t"))
  2250. (if (compilation--loc->marker col)
  2251. (set-marker (compilation--loc->marker col) (point))
  2252. (setf (compilation--loc->marker col) (point-marker)))
  2253. ;; (setf (compilation--loc->timestamp col) timestamp)
  2254. ))))))
  2255. (compilation-goto-locus marker (compilation--loc->marker loc)
  2256. (compilation--loc->marker end-loc))
  2257. (setf (compilation--loc->visited loc) t)))
  2258. (defvar compilation-gcpro nil
  2259. "Internal variable used to keep some values from being GC'd.")
  2260. (make-variable-buffer-local 'compilation-gcpro)
  2261. (defun compilation-fake-loc (marker file &optional line col)
  2262. "Preassociate MARKER with FILE.
  2263. FILE should be ABSOLUTE-FILENAME or (RELATIVE-FILENAME . DIRNAME).
  2264. This is useful when you compile temporary files, but want
  2265. automatic translation of the messages to the real buffer from
  2266. which the temporary file came. This may also affect previous messages
  2267. about FILE.
  2268. Optional args LINE and COL default to 1 and beginning of
  2269. indentation respectively. The marker is expected to reflect
  2270. this. In the simplest case the marker points to the first line
  2271. of the region that was saved to the temp file.
  2272. If you concatenate several regions into the temp file (e.g. a
  2273. header with variable assignments and a code region), you must
  2274. call this several times, once each for the last line of one
  2275. region and the first line of the next region."
  2276. (or (consp file) (setq file (list file)))
  2277. (compilation--flush-file-structure file)
  2278. (let ((fs (compilation-get-file-structure file)))
  2279. ;; Between the current call to compilation-fake-loc and the first
  2280. ;; occurrence of an error message referring to `file', the data is
  2281. ;; only kept in the weak hash-table compilation-locs, so we need
  2282. ;; to prevent this entry in compilation-locs from being GC'd
  2283. ;; away. --Stef
  2284. (push fs compilation-gcpro)
  2285. (let ((loc (compilation-assq (or line 1) (cdr fs))))
  2286. (setq loc (compilation-assq col loc))
  2287. (cl-assert (null (cdr loc)))
  2288. (setcdr loc (compilation--make-cdrloc line fs marker))
  2289. loc)))
  2290. (defcustom compilation-context-lines nil
  2291. "Display this many lines of leading context before the current message.
  2292. If nil and the left fringe is displayed, don't scroll the
  2293. compilation output window; an arrow in the left fringe points to
  2294. the current message. If nil and there is no left fringe, the message
  2295. displays at the top of the window; there is no arrow."
  2296. :type '(choice integer (const :tag "No window scrolling" nil))
  2297. :group 'compilation
  2298. :version "22.1")
  2299. (defsubst compilation-set-window (w mk)
  2300. "Align the compilation output window W with marker MK near top."
  2301. (if (integerp compilation-context-lines)
  2302. (set-window-start w (save-excursion
  2303. (goto-char mk)
  2304. (compilation-beginning-of-line
  2305. (- 1 compilation-context-lines))
  2306. (point)))
  2307. ;; If there is no left fringe.
  2308. (when (equal (car (window-fringes w)) 0)
  2309. (set-window-start w (save-excursion
  2310. (goto-char mk)
  2311. (beginning-of-line 1)
  2312. (point)))))
  2313. (set-window-point w mk))
  2314. (defvar next-error-highlight-timer)
  2315. (defun compilation-goto-locus (msg mk end-mk)
  2316. "Jump to an error corresponding to MSG at MK.
  2317. All arguments are markers. If END-MK is non-nil, mark is set there
  2318. and overlay is highlighted between MK and END-MK."
  2319. ;; Show compilation buffer in other window, scrolled to this error.
  2320. (let* ((from-compilation-buffer (eq (window-buffer)
  2321. (marker-buffer msg)))
  2322. ;; Use an existing window if it is in a visible frame.
  2323. (pre-existing (get-buffer-window (marker-buffer msg) 0))
  2324. (w (if (and from-compilation-buffer pre-existing)
  2325. ;; Calling display-buffer here may end up (partly) hiding
  2326. ;; the error location if the two buffers are in two
  2327. ;; different frames. So don't do it if it's not necessary.
  2328. pre-existing
  2329. (display-buffer (marker-buffer msg) '(nil (allow-no-window . t)))))
  2330. (highlight-regexp (with-current-buffer (marker-buffer msg)
  2331. ;; also do this while we change buffer
  2332. (goto-char (marker-position msg))
  2333. (and w (compilation-set-window w msg))
  2334. compilation-highlight-regexp)))
  2335. ;; Ideally, the window-size should be passed to `display-buffer'
  2336. ;; so it's only used when creating a new window.
  2337. (when (and (not pre-existing) w)
  2338. (compilation-set-window-height w))
  2339. (if from-compilation-buffer
  2340. ;; If the compilation buffer window was selected,
  2341. ;; keep the compilation buffer in this window;
  2342. ;; display the source in another window.
  2343. (let ((pop-up-windows t))
  2344. (pop-to-buffer (marker-buffer mk) 'other-window))
  2345. (switch-to-buffer (marker-buffer mk)))
  2346. (unless (eq (goto-char mk) (point))
  2347. ;; If narrowing gets in the way of going to the right place, widen.
  2348. (widen)
  2349. (if next-error-move-function
  2350. (funcall next-error-move-function msg mk)
  2351. (goto-char mk)))
  2352. (if end-mk
  2353. (push-mark end-mk t)
  2354. (if mark-active (setq mark-active nil)))
  2355. ;; If hideshow got in the way of
  2356. ;; seeing the right place, open permanently.
  2357. (dolist (ov (overlays-at (point)))
  2358. (when (eq 'hs (overlay-get ov 'invisible))
  2359. (delete-overlay ov)
  2360. (goto-char mk)))
  2361. (when highlight-regexp
  2362. (if (timerp next-error-highlight-timer)
  2363. (cancel-timer next-error-highlight-timer))
  2364. (unless compilation-highlight-overlay
  2365. (setq compilation-highlight-overlay
  2366. (make-overlay (point-min) (point-min)))
  2367. (overlay-put compilation-highlight-overlay 'face 'next-error))
  2368. (with-current-buffer (marker-buffer mk)
  2369. (save-excursion
  2370. (if end-mk (goto-char end-mk) (end-of-line))
  2371. (let ((end (point)))
  2372. (if mk (goto-char mk) (beginning-of-line))
  2373. (if (and (stringp highlight-regexp)
  2374. (re-search-forward highlight-regexp end t))
  2375. (progn
  2376. (goto-char (match-beginning 0))
  2377. (move-overlay compilation-highlight-overlay
  2378. (match-beginning 0) (match-end 0)
  2379. (current-buffer)))
  2380. (move-overlay compilation-highlight-overlay
  2381. (point) end (current-buffer)))
  2382. (if (or (eq next-error-highlight t)
  2383. (numberp next-error-highlight))
  2384. ;; We want highlighting: delete overlay on next input.
  2385. (add-hook 'pre-command-hook
  2386. 'compilation-goto-locus-delete-o)
  2387. ;; We don't want highlighting: delete overlay now.
  2388. (delete-overlay compilation-highlight-overlay))
  2389. ;; We want highlighting for a limited time:
  2390. ;; set up a timer to delete it.
  2391. (when (numberp next-error-highlight)
  2392. (setq next-error-highlight-timer
  2393. (run-at-time next-error-highlight nil
  2394. 'compilation-goto-locus-delete-o)))))))
  2395. (when (and (eq next-error-highlight 'fringe-arrow))
  2396. ;; We want a fringe arrow (instead of highlighting).
  2397. (setq next-error-overlay-arrow-position
  2398. (copy-marker (line-beginning-position))))))
  2399. (defun compilation-goto-locus-delete-o ()
  2400. (delete-overlay compilation-highlight-overlay)
  2401. ;; Get rid of timer and hook that would try to do this again.
  2402. (if (timerp next-error-highlight-timer)
  2403. (cancel-timer next-error-highlight-timer))
  2404. (remove-hook 'pre-command-hook
  2405. 'compilation-goto-locus-delete-o))
  2406. (defun compilation-find-file (marker filename directory &rest formats)
  2407. "Find a buffer for file FILENAME.
  2408. If FILENAME is not found at all, ask the user where to find it.
  2409. Pop up the buffer containing MARKER and scroll to MARKER if we ask
  2410. the user where to find the file.
  2411. Search the directories in `compilation-search-path'.
  2412. A nil in `compilation-search-path' means to try the
  2413. \"current\" directory, which is passed in DIRECTORY.
  2414. If DIRECTORY is relative, it is combined with `default-directory'.
  2415. If DIRECTORY is nil, that means use `default-directory'.
  2416. FORMATS, if given, is a list of formats to reformat FILENAME when
  2417. looking for it: for each element FMT in FORMATS, this function
  2418. attempts to find a file whose name is produced by (format FMT FILENAME)."
  2419. (or formats (setq formats '("%s")))
  2420. (let ((dirs compilation-search-path)
  2421. (spec-dir (if directory
  2422. (expand-file-name directory)
  2423. default-directory))
  2424. buffer thisdir fmts name)
  2425. (if (file-name-absolute-p filename)
  2426. ;; The file name is absolute. Use its explicit directory as
  2427. ;; the first in the search path, and strip it from FILENAME.
  2428. (setq filename (abbreviate-file-name (expand-file-name filename))
  2429. dirs (cons (file-name-directory filename) dirs)
  2430. filename (file-name-nondirectory filename)))
  2431. ;; Now search the path.
  2432. (while (and dirs (null buffer))
  2433. (setq thisdir (or (car dirs) spec-dir)
  2434. fmts formats)
  2435. ;; For each directory, try each format string.
  2436. (while (and fmts (null buffer))
  2437. (setq name (expand-file-name (format (car fmts) filename) thisdir)
  2438. buffer (and (file-exists-p name)
  2439. (find-file-noselect name))
  2440. fmts (cdr fmts)))
  2441. (setq dirs (cdr dirs)))
  2442. (while (null buffer) ;Repeat until the user selects an existing file.
  2443. ;; The file doesn't exist. Ask the user where to find it.
  2444. (save-excursion ;This save-excursion is probably not right.
  2445. (let ((w (let ((pop-up-windows t))
  2446. (display-buffer (marker-buffer marker)
  2447. '(nil (allow-no-window . t))))))
  2448. (with-current-buffer (marker-buffer marker)
  2449. (goto-char marker)
  2450. (and w (compilation-set-window w marker)))
  2451. (let* ((name (read-file-name
  2452. (format "Find this %s in (default %s): "
  2453. compilation-error filename)
  2454. spec-dir filename t nil
  2455. ;; The predicate below is fine when called from
  2456. ;; minibuffer-complete-and-exit, but it's too
  2457. ;; restrictive otherwise, since it also prevents the
  2458. ;; user from completing "fo" to "foo/" when she
  2459. ;; wants to enter "foo/bar".
  2460. ;;
  2461. ;; Try to make sure the user can only select
  2462. ;; a valid answer. This predicate may be ignored,
  2463. ;; tho, so we still have to double-check afterwards.
  2464. ;; TODO: We should probably fix read-file-name so
  2465. ;; that it never ignores this predicate, even when
  2466. ;; using popup dialog boxes.
  2467. ;; (lambda (name)
  2468. ;; (if (file-directory-p name)
  2469. ;; (setq name (expand-file-name filename name)))
  2470. ;; (file-exists-p name))
  2471. ))
  2472. (origname name))
  2473. (cond
  2474. ((not (file-exists-p name))
  2475. (message "Cannot find file `%s'" name)
  2476. (ding) (sit-for 2))
  2477. ((and (file-directory-p name)
  2478. (not (file-exists-p
  2479. (setq name (expand-file-name filename name)))))
  2480. (message "No `%s' in directory %s" filename origname)
  2481. (ding) (sit-for 2))
  2482. (t
  2483. (setq buffer (find-file-noselect name))))))))
  2484. ;; Make intangible overlays tangible.
  2485. ;; This is weird: it's not even clear which is the current buffer,
  2486. ;; so the code below can't be expected to DTRT here. -- Stef
  2487. (dolist (ov (overlays-in (point-min) (point-max)))
  2488. (when (overlay-get ov 'intangible)
  2489. (overlay-put ov 'intangible nil)))
  2490. buffer))
  2491. (defun compilation-get-file-structure (file &optional fmt)
  2492. "Retrieve FILE's file-structure or create a new one.
  2493. FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
  2494. In the former case, FILENAME may be relative or absolute.
  2495. The file-structure looks like this:
  2496. ((FILENAME [TRUE-DIRNAME]) FMT ...)
  2497. TRUE-DIRNAME is the `file-truename' of DIRNAME, if given."
  2498. (or (gethash file compilation-locs)
  2499. ;; File was not previously encountered, at least not in the form passed.
  2500. ;; Let's normalize it and look again.
  2501. (let ((filename (car file))
  2502. ;; Get the specified directory from FILE.
  2503. (spec-directory (if (cdr file)
  2504. (file-truename (cdr file)))))
  2505. ;; Check for a comint-file-name-prefix and prepend it if appropriate.
  2506. ;; (This is very useful for compilation-minor-mode in an rlogin-mode
  2507. ;; buffer.)
  2508. (when (and (boundp 'comint-file-name-prefix)
  2509. (not (equal comint-file-name-prefix "")))
  2510. (if (file-name-absolute-p filename)
  2511. (setq filename
  2512. (concat comint-file-name-prefix filename))
  2513. (if spec-directory
  2514. (setq spec-directory
  2515. (file-truename
  2516. (concat comint-file-name-prefix spec-directory))))))
  2517. ;; If compilation-parse-errors-filename-function is
  2518. ;; defined, use it to process the filename.
  2519. (when compilation-parse-errors-filename-function
  2520. (setq filename
  2521. (funcall compilation-parse-errors-filename-function
  2522. filename)))
  2523. ;; Some compilers (e.g. Sun's java compiler, reportedly) produce bogus
  2524. ;; file names like "./bar//foo.c" for file "bar/foo.c";
  2525. ;; expand-file-name will collapse these into "/foo.c" and fail to find
  2526. ;; the appropriate file. So we look for doubled slashes in the file
  2527. ;; name and fix them.
  2528. (setq filename (command-line-normalize-file-name filename))
  2529. ;; Store it for the possibly unnormalized name
  2530. (puthash file
  2531. ;; Retrieve or create file-structure for normalized name
  2532. ;; The gethash used to not use spec-directory, but
  2533. ;; this leads to errors when files in different
  2534. ;; directories have the same name:
  2535. ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00463.html
  2536. (or (gethash (cons filename spec-directory) compilation-locs)
  2537. (puthash (cons filename spec-directory)
  2538. (compilation--make-file-struct
  2539. (list filename spec-directory) fmt)
  2540. compilation-locs))
  2541. compilation-locs))))
  2542. (defun compilation--flush-file-structure (file)
  2543. (or (consp file) (setq file (list file)))
  2544. (let ((fs (compilation-get-file-structure file)))
  2545. (cl-assert (eq fs (gethash file compilation-locs)))
  2546. (cl-assert (eq fs (gethash (cons (caar fs) (cadr (car fs)))
  2547. compilation-locs)))
  2548. (maphash (lambda (k v)
  2549. (if (eq v fs) (remhash k compilation-locs)))
  2550. compilation-locs)))
  2551. ;;; Compatibility with the old compile.el.
  2552. (defvaralias 'compilation-last-buffer 'next-error-last-buffer)
  2553. (defvar compilation-parsing-end (make-marker))
  2554. (defvar compilation-error-list nil)
  2555. (defvar compilation-old-error-list nil)
  2556. (defun compilation--compat-error-properties (err)
  2557. "Map old-style error ERR to new-style message."
  2558. ;; Old-style structure is (MARKER (FILE DIR) LINE COL) or
  2559. ;; (MARKER . MARKER).
  2560. (let ((dst (cdr err)))
  2561. (if (markerp dst)
  2562. `(compilation-message ,(compilation--make-message
  2563. (cons nil (compilation--make-cdrloc
  2564. nil nil dst))
  2565. 2 nil)
  2566. help-echo "mouse-2: visit the source location"
  2567. keymap compilation-button-map
  2568. mouse-face highlight)
  2569. ;; Too difficult to do it by hand: dispatch to the normal code.
  2570. (let* ((file (pop dst))
  2571. (line (pop dst))
  2572. (col (pop dst))
  2573. (filename (pop file))
  2574. (dirname (pop file))
  2575. (fmt (pop file)))
  2576. (compilation-internal-error-properties
  2577. (cons filename dirname) line nil col nil 2 fmt)))))
  2578. (defun compilation--compat-parse-errors (limit)
  2579. (when compilation-parse-errors-function
  2580. ;; FIXME: We should remove the rest of the compilation keywords
  2581. ;; but we can't do that from here because font-lock is using
  2582. ;; the value right now. --Stef
  2583. (save-excursion
  2584. (setq compilation-error-list nil)
  2585. ;; Reset compilation-parsing-end each time because font-lock
  2586. ;; might force us the re-parse many times (typically because
  2587. ;; some code adds some text-property to the output that we
  2588. ;; already parsed). You might say "why reparse", well:
  2589. ;; because font-lock has just removed the `compilation-message' property
  2590. ;; so have to do it all over again.
  2591. (if compilation-parsing-end
  2592. (set-marker compilation-parsing-end (point))
  2593. (setq compilation-parsing-end (point-marker)))
  2594. (condition-case nil
  2595. ;; Ignore any error: we're calling this function earlier than
  2596. ;; in the old compile.el so things might not all be setup yet.
  2597. (funcall compilation-parse-errors-function limit nil)
  2598. (error nil))
  2599. (dolist (err (if (listp compilation-error-list) compilation-error-list))
  2600. (let* ((src (car err))
  2601. (dst (cdr err))
  2602. (loc (cond ((markerp dst)
  2603. (cons nil
  2604. (compilation--make-cdrloc nil nil dst)))
  2605. ((consp dst)
  2606. (cons (nth 2 dst)
  2607. (compilation--make-cdrloc
  2608. (nth 1 dst)
  2609. (cons (cdar dst) (caar dst))
  2610. nil))))))
  2611. (when loc
  2612. (goto-char src)
  2613. ;; (put-text-property src (line-end-position)
  2614. ;; 'font-lock-face 'font-lock-warning-face)
  2615. (put-text-property src (line-end-position)
  2616. 'compilation-message
  2617. (compilation--make-message loc 2 nil)))))))
  2618. (goto-char limit)
  2619. nil)
  2620. ;; Beware! this is not only compatibility code. New code also uses it. --Stef
  2621. (defun compilation-forget-errors ()
  2622. ;; In case we hit the same file/line specs, we want to recompute a new
  2623. ;; marker for them, so flush our cache.
  2624. (clrhash compilation-locs)
  2625. (setq compilation-gcpro nil)
  2626. ;; FIXME: the old code reset the directory-stack, so maybe we should
  2627. ;; put a `directory change' marker of some sort, but where? -stef
  2628. ;;
  2629. ;; FIXME: The old code moved compilation-current-error (which was
  2630. ;; virtually represented by a mix of compilation-parsing-end and
  2631. ;; compilation-error-list) to point-min, but that was only meaningful for
  2632. ;; the internal uses of compilation-forget-errors: all calls from external
  2633. ;; packages seem to be followed by a move of compilation-parsing-end to
  2634. ;; something equivalent to point-max. So we heuristically move
  2635. ;; compilation-current-error to point-max (since the external package
  2636. ;; won't know that it should do it). --Stef
  2637. (setq compilation-current-error nil)
  2638. (let* ((proc (get-buffer-process (current-buffer)))
  2639. (mark (if proc (process-mark proc)))
  2640. (pos (or mark (point-max))))
  2641. (setq compilation-messages-start
  2642. ;; In the future, ignore the text already present in the buffer.
  2643. ;; Since many process filter functions insert before markers,
  2644. ;; we need to put ours just before the insertion point rather
  2645. ;; than at the insertion point. If that's not possible, then
  2646. ;; don't use a marker. --Stef
  2647. (if (> pos (point-min)) (copy-marker (1- pos)) pos)))
  2648. ;; Again, since this command is used in buffers that contain several
  2649. ;; compilations, to set the beginning of "this compilation", it's a good
  2650. ;; place to reset compilation-auto-jump-to-next.
  2651. (set (make-local-variable 'compilation-auto-jump-to-next)
  2652. (or compilation-auto-jump-to-first-error
  2653. (eq compilation-scroll-output 'first-error))))
  2654. (provide 'compile)
  2655. ;;; compile.el ends here