PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/emacs-24.2/lisp/progmodes/compile.el

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