/lisp/progmodes/compile.el

https://bitbucket.org/rev22/progressive-gc-emacsen · Emacs Lisp · 2770 lines · 2027 code · 263 blank · 480 comment · 78 complexity · 066be377957e6ff260e8017bcddfb3f5 MD5 · raw file

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