PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/raw-elisp/yinwm/elisp/20/emacslib/compile.el

https://bitbucket.org/yinwm/usemacs/
Emacs Lisp | 1955 lines | 1330 code | 192 blank | 433 comment | 44 complexity | 46105ea1b81b6d4169289493b84129b8 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
  2. ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
  3. ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
  4. ;; Maintainer: FSF
  5. ;; Keywords: tools, processes
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  17. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. ;; Boston, MA 02111-1307, USA.
  19. ;;; Commentary:
  20. ;; This package provides the compile and grep facilities documented in
  21. ;; the Emacs user's manual.
  22. ;;; Code:
  23. (defgroup compilation nil
  24. "Run compiler as inferior of Emacs, parse error messages."
  25. :group 'tools
  26. :group 'processes)
  27. ;;;###autoload
  28. (defcustom compilation-mode-hook nil
  29. "*List of hook functions run by `compilation-mode' (see `run-hooks')."
  30. :type 'hook
  31. :group 'compilation)
  32. ;;;###autoload
  33. (defcustom compilation-window-height nil
  34. "*Number of lines in a compilation window. If nil, use Emacs default."
  35. :type '(choice (const :tag "Default" nil)
  36. integer)
  37. :group 'compilation)
  38. (defvar compile-auto-highlight nil
  39. "*Specify how many compiler errors to highlight (and parse) initially.
  40. \(Highlighting applies to an error message when the mouse is over it.)
  41. If this is a number N, all compiler error messages in the first N lines
  42. are highlighted and parsed as soon as they arrive in Emacs.
  43. If t, highlight and parse the whole compilation output as soon as it arrives.
  44. If nil, don't highlight or parse any of the buffer until you try to
  45. move to the error messages.
  46. Those messages which are not parsed and highlighted initially
  47. will be parsed and highlighted as soon as you try to move to them.")
  48. (defvar compilation-error-list nil
  49. "List of error message descriptors for visiting erring functions.
  50. Each error descriptor is a cons (or nil). Its car is a marker pointing to
  51. an error message. If its cdr is a marker, it points to the text of the
  52. line the message is about. If its cdr is a cons, it is a list
  53. \(\(DIRECTORY . FILE\) LINE [COLUMN]\). Or its cdr may be nil if that
  54. error is not interesting.
  55. The value may be t instead of a list; this means that the buffer of
  56. error messages should be reparsed the next time the list of errors is wanted.
  57. Some other commands (like `diff') use this list to control the error
  58. message tracking facilities; if you change its structure, you should make
  59. sure you also change those packages. Perhaps it is better not to change
  60. it at all.")
  61. (defvar compilation-old-error-list nil
  62. "Value of `compilation-error-list' after errors were parsed.")
  63. (defvar compilation-parse-errors-function 'compilation-parse-errors
  64. "Function to call to parse error messages from a compilation.
  65. It takes args LIMIT-SEARCH and FIND-AT-LEAST.
  66. If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
  67. If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
  68. many new errors.
  69. It should read in the source files which have errors and set
  70. `compilation-error-list' to a list with an element for each error message
  71. found. See that variable for more info.")
  72. ;;;###autoload
  73. (defvar compilation-process-setup-function nil
  74. "*Function to call to customize the compilation process.
  75. This functions is called immediately before the compilation process is
  76. started. It can be used to set any variables or functions that are used
  77. while processing the output of the compilation process.")
  78. ;;;###autoload
  79. (defvar compilation-buffer-name-function nil
  80. "Function to compute the name of a compilation buffer.
  81. The function receives one argument, the name of the major mode of the
  82. compilation buffer. It should return a string.
  83. nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
  84. ;;;###autoload
  85. (defvar compilation-finish-function nil
  86. "Function to call when a compilation process finishes.
  87. It is called with two arguments: the compilation buffer, and a string
  88. describing how the process finished.")
  89. ;;;###autoload
  90. (defvar compilation-finish-functions nil
  91. "Functions to call when a compilation process finishes.
  92. Each function is called with two arguments: the compilation buffer,
  93. and a string describing how the process finished.")
  94. (defvar compilation-last-buffer nil
  95. "The most recent compilation buffer.
  96. A buffer becomes most recent when its compilation is started
  97. or when it is used with \\[next-error] or \\[compile-goto-error].")
  98. (defvar compilation-in-progress nil
  99. "List of compilation processes now running.")
  100. (or (assq 'compilation-in-progress minor-mode-alist)
  101. (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
  102. minor-mode-alist)))
  103. (defvar compilation-parsing-end nil
  104. "Position of end of buffer when last error messages were parsed.")
  105. (defvar compilation-error-message "No more errors"
  106. "Message to print when no more matches are found.")
  107. (defvar compilation-arguments nil
  108. "Arguments that were given to `compile-internal'.")
  109. (defvar compilation-num-errors-found)
  110. (defvar compilation-error-regexp-alist
  111. '(
  112. ;; NOTE! See also grep-regexp-alist, below.
  113. ;; 4.3BSD grep, cc, lint pass 1:
  114. ;; /usr/src/foo/foo.c(8): warning: w may be used before set
  115. ;; or GNU utilities:
  116. ;; foo.c:8: error message
  117. ;; or HP-UX 7.0 fc:
  118. ;; foo.f :16 some horrible error message
  119. ;; or GNU utilities with column (GNAT 1.82):
  120. ;; foo.adb:2:1: Unit name does not match file name
  121. ;; or with column and program name:
  122. ;; jade:dbcommon.dsl:133:17:E: missing argument for function call
  123. ;;
  124. ;; We'll insist that the number be followed by a colon or closing
  125. ;; paren, because otherwise this matches just about anything
  126. ;; containing a number with spaces around it.
  127. ("\\([a-zA-Z]+:\\)?\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\
  128. \\([) \t]\\|:\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 2 3 6)
  129. ;; Microsoft C/C++:
  130. ;; keyboard.c(537) : warning C4005: 'min' : macro redefinition
  131. ;; d:\tmp\test.c(23) : error C2143: syntax error : missing ';' before 'if'
  132. ;; This used to be less selective and allow characters other than
  133. ;; parens around the line number, but that caused confusion for
  134. ;; GNU-style error messages.
  135. ;; This used to reject spaces and dashes in file names,
  136. ;; but they are valudnow; so I made it more strict about the error
  137. ;; message that follows.
  138. ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \
  139. : \\(error\\|warning\\) C[0-9]+:" 1 3)
  140. ;; Borland C++:
  141. ;; Error ping.c 15: Unable to open include file 'sys/types.h'
  142. ;; Warning ping.c 68: Call to function 'func' with no prototype
  143. ("\\(Error\\|Warning\\) \\([a-zA-Z]?:?[^:( \t\n]+\\)\
  144. \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
  145. ;; 4.3BSD lint pass 2
  146. ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)
  147. (".*[ \t:]\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$"
  148. 1 2)
  149. ;; 4.3BSD lint pass 3
  150. ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
  151. ;; This used to be
  152. ;; ("[ \t(]+\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
  153. ;; which is regexp Impressionism - it matches almost anything!
  154. (".*([ \t]*\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
  155. ;; MIPS lint pass<n>; looks good for SunPro lint also
  156. ;; TrimMask (255) in solomon.c may be indistinguishable from TrimMasks (93) in solomon.c due to truncation
  157. ("[^\n ]+ (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
  158. ;; name defined but never used: LinInt in cmap_calc.c(199)
  159. (".*in \\([^(\n]+\\)(\\([0-9]+\\))$" 1 2)
  160. ;; Ultrix 3.0 f77:
  161. ;; fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
  162. ;; Some SGI cc version:
  163. ;; cfe: Warning 835: foo.c, line 2: something
  164. ("\\(cfe\\|fort\\): [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 2 3)
  165. ;; Error on line 3 of t.f: Execution error unclassifiable statement
  166. ;; Unknown who does this:
  167. ;; Line 45 of "foo.c": bloofle undefined
  168. ;; Absoft FORTRAN 77 Compiler 3.1.3
  169. ;; error on line 19 of fplot.f: spelling error?
  170. ;; warning on line 17 of fplot.f: data type is undefined for variable d
  171. ("\\(.* on \\)?[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
  172. of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2)
  173. ;; Apollo cc, 4.3BSD fc:
  174. ;; "foo.f", line 3: Error: syntax error near end of statement
  175. ;; IBM RS6000:
  176. ;; "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
  177. ;; Microtec mcc68k:
  178. ;; "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
  179. ;; GNAT (as of July 94):
  180. ;; "foo.adb", line 2(11): warning: file name does not match ...
  181. ;; IBM AIX xlc compiler:
  182. ;; "src/swapping.c", line 30.34: 1506-342 (W) "/*" detected in comment.
  183. (".*\"\\([^,\" \n\t]+\\)\", lines? \
  184. \\([0-9]+\\)\\([\(.]\\([0-9]+\\)\)?\\)?[:., (-]" 1 2 4)
  185. ;; Caml compiler:
  186. ;; File "foobar.ml", lines 5-8, characters 20-155: blah blah
  187. ("^File \"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[-0-9]*, characters? \\([0-9]+\\)" 1 2 3)
  188. ;; MIPS RISC CC - the one distributed with Ultrix:
  189. ;; ccom: Error: foo.c, line 2: syntax error
  190. ;; DEC AXP OSF/1 cc
  191. ;; /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
  192. ("[a-z0-9/]+: \\([eE]rror\\|[wW]arning\\): \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 2 4)
  193. ;; IBM AIX PS/2 C version 1.1:
  194. ;; ****** Error number 140 in line 8 of file errors.c ******
  195. (".*in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
  196. ;; IBM AIX lint is too painful to do right this way. File name
  197. ;; prefixes entire sections rather than being on each line.
  198. ;; SPARCcompiler Pascal:
  199. ;; 20 linjer : array[1..4] of linje;
  200. ;; e 18480-----------^--- Inserted ';'
  201. ;; and
  202. ;; E 18520 line 61 - 0 is undefined
  203. ;; These messages don't contain a file name. Instead the compiler gives
  204. ;; a message whenever the file being compiled is changed.
  205. (" +\\([0-9]+\\) +.*\n[ew] [0-9]+-+" nil 1)
  206. ("[Ew] +[0-9]+ line \\([0-9]+\\) - " nil 1)
  207. ;; Lucid Compiler, lcc 3.x
  208. ;; E, file.cc(35,52) Illegal operation on pointers
  209. ("[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
  210. ;;; This seems to be superfluous because the first pattern matches it.
  211. ;;; ;; GNU messages with program name and optional column number.
  212. ;;; ("[a-zA-Z]?:?[^0-9 \n\t:]+[^ \n\t:]*:[ \t]*\\([^ \n\t:]+\\):\
  213. ;;;\\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4)
  214. ;; Cray C compiler error messages
  215. ("\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \
  216. \\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
  217. ;; IBM C/C++ Tools 2.01:
  218. ;; foo.c(2:0) : informational EDC0804: Function foo is not referenced.
  219. ;; foo.c(3:8) : warning EDC0833: Implicit return statement encountered.
  220. ;; foo.c(5:5) : error EDC0350: Syntax error.
  221. ("\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) : " 1 2 3)
  222. ;; IAR Systems C Compiler:
  223. ;; "foo.c",3 Error[32]: Error message
  224. ;; "foo.c",3 Warning[32]: Error message
  225. ("\"\\(.*\\)\",\\([0-9]+\\)\\s-+\\(Error\\|Warning\\)\\[[0-9]+\\]:" 1 2)
  226. ;; Sun ada (VADS, Solaris):
  227. ;; /home3/xdhar/rcds_rc/main.a, line 361, char 6:syntax error: "," inserted
  228. ("\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
  229. ;; Perl -w:
  230. ;; syntax error at automake line 922, near "':'"
  231. (".* at \\([^ ]+\\) line \\([0-9]+\\)," 1 2)
  232. ;; Oracle pro*c:
  233. ;; Semantic error at line 528, column 5, file erosacqdb.pc:
  234. ("Semantic error at line \\([0-9]+\\), column \\([0-9]+\\), file \\(.*\\):"
  235. 3 1 2)
  236. ;; EPC F90 compiler:
  237. ;; Error 24 at (2:progran.f90) : syntax error
  238. ("Error [0-9]+ at (\\([0-9]*\\):\\([^)\n]+\\))" 2 1)
  239. ;; Sun F90 error messages:
  240. ;; cf90-113 f90comp: ERROR NSE, File = Hoved.f90, Line = 16, Column = 3
  241. (".* ERROR [a-zA-Z0-9 ]+, File = \\(.+\\), Line = \\([0-9]+\\), Column = \\([0-9]+\\)"
  242. 1 2 3)
  243. )
  244. "Alist that specifies how to match errors in compiler output.
  245. Each elt has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX FILE-FORMAT...])
  246. If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
  247. the LINE-IDX'th subexpression gives the line number. If COLUMN-IDX is
  248. given, the COLUMN-IDX'th subexpression gives the column number on that line.
  249. If any FILE-FORMAT is given, each is a format string to produce a file name to
  250. try; %s in the string is replaced by the text matching the FILE-IDX'th
  251. subexpression.")
  252. (defvar compilation-enter-directory-regexp-alist
  253. '(
  254. ;; Matches lines printed by the `-w' option of GNU Make.
  255. (".*: Entering directory `\\(.*\\)'$" 1)
  256. )
  257. "Alist specifying how to match lines that indicate a new current directory.
  258. Note that the match is done at the beginning of lines.
  259. Each elt has the form (REGEXP IDX).
  260. If REGEXP matches, the IDX'th subexpression gives the directory name.
  261. The default value matches lines printed by the `-w' option of GNU Make.")
  262. (defvar compilation-leave-directory-regexp-alist
  263. '(
  264. ;; Matches lines printed by the `-w' option of GNU Make.
  265. (".*: Leaving directory `\\(.*\\)'$" 1)
  266. )
  267. "Alist specifying how to match lines that indicate restoring current directory.
  268. Note that the match is done at the beginning of lines.
  269. Each elt has the form (REGEXP IDX).
  270. If REGEXP matches, the IDX'th subexpression gives the name of the directory
  271. being moved from. If IDX is nil, the last directory entered \(by a line
  272. matching `compilation-enter-directory-regexp-alist'\) is assumed.
  273. The default value matches lines printed by the `-w' option of GNU Make.")
  274. (defvar compilation-file-regexp-alist
  275. '(
  276. ;; This matches entries with date time year file-name: like
  277. ;; Thu May 14 10:46:12 1992 mom3.p:
  278. ("\\w\\w\\w \\w\\w\\w +[0-9]+ [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9] \\(.*\\):$" 1)
  279. )
  280. "Alist specifying how to match lines that indicate a new current file.
  281. Note that the match is done at the beginning of lines.
  282. Each elt has the form (REGEXP IDX).
  283. If REGEXP matches, the IDX'th subexpression gives the file name. This is
  284. used with compilers that don't indicate file name in every error message.")
  285. ;; There is no generally useful regexp that will match non messages, but
  286. ;; in special cases there might be one. The lines that are not matched by
  287. ;; a regexp take much longer time than the ones that are recognized so if
  288. ;; you have same regexeps here, parsing is faster.
  289. (defvar compilation-nomessage-regexp-alist
  290. '(
  291. )
  292. "Alist specifying how to match lines that have no message.
  293. Note that the match is done at the beginning of lines.
  294. Each elt has the form (REGEXP). This alist is by default empty, but if
  295. you have some good regexps here, the parsing of messages will be faster.")
  296. (defcustom compilation-read-command t
  297. "*If not nil, M-x compile reads the compilation command to use.
  298. Otherwise, M-x compile just uses the value of `compile-command'."
  299. :type 'boolean
  300. :group 'compilation)
  301. ;;;###autoload
  302. (defcustom compilation-ask-about-save t
  303. "*If not nil, M-x compile asks which buffers to save before compiling.
  304. Otherwise, it saves all modified buffers without asking."
  305. :type 'boolean
  306. :group 'compilation)
  307. (defvar grep-regexp-alist
  308. '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
  309. "Regexp used to match grep hits. See `compilation-error-regexp-alist'.")
  310. (defvar grep-program
  311. ;; Currently zgrep has trouble. It runs egrep instead of grep,
  312. ;; and it doesn't pass along long options right.
  313. "grep"
  314. ;;; (if (equal (condition-case nil ; in case "zgrep" isn't in exec-path
  315. ;;; (call-process "zgrep" nil nil nil
  316. ;;; "foo" null-device)
  317. ;;; (error nil))
  318. ;;; 1)
  319. ;;; "zgrep"
  320. ;;; "grep")
  321. "The default grep program for `grep-command' and `grep-find-command'.
  322. This variable's value takes effect when `grep-compute-defaults' is called.")
  323. ;; Use -e if grep supports it,
  324. ;; because that avoids lossage if the pattern starts with `-'.
  325. (defvar grep-command nil
  326. "The default grep command for \\[grep].
  327. The real default value of this variable is set up by `grep-compute-defaults';
  328. call that function before using this variable.")
  329. (defvar grep-find-use-xargs nil
  330. "Whether \\[grep-find] uses the `xargs' utility by default.
  331. If nil, it uses `grep -exec'; if `gnu', it uses `find -print0' and `xargs -0';
  332. if not nil and not `gnu', it uses `find -print' and `xargs'.
  333. This variable's value takes effect when `grep-compute-defaults' is called.")
  334. (defvar grep-find-command nil
  335. "The default find command for \\[grep-find].
  336. The default value of this variable is set up by `grep-compute-defaults';
  337. call that function before using this variable.")
  338. ;;;###autoload
  339. (defcustom compilation-search-path '(nil)
  340. "*List of directories to search for source files named in error messages.
  341. Elements should be directory names, not file names of directories.
  342. nil as an element means to try the default directory."
  343. :type '(repeat (choice (const :tag "Default" nil)
  344. (string :tag "Directory")))
  345. :group 'compilation)
  346. (defcustom compile-command "make -k "
  347. "*Last shell command used to do a compilation; default for next compilation.
  348. Sometimes it is useful for files to supply local values for this variable.
  349. You might also use mode hooks to specify it in certain modes, like this:
  350. (add-hook 'c-mode-hook
  351. (function
  352. (lambda ()
  353. (unless (or (file-exists-p \"makefile\")
  354. (file-exists-p \"Makefile\"))
  355. (make-local-variable 'compile-command)
  356. (setq compile-command
  357. (concat \"make -k \"
  358. (file-name-sans-extension buffer-file-name)))))))"
  359. :type 'string
  360. :group 'compilation)
  361. (defvar compilation-directory-stack nil
  362. "Stack of previous directories for `compilation-leave-directory-regexp'.
  363. The last element is the directory the compilation was started in.")
  364. (defvar compilation-exit-message-function nil "\
  365. If non-nil, called when a compilation process dies to return a status message.
  366. This should be a function of three arguments: process status, exit status,
  367. and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
  368. write into the compilation buffer, and to put in its mode line.")
  369. ;; History of compile commands.
  370. (defvar compile-history nil)
  371. ;; History of grep commands.
  372. (defvar grep-history nil)
  373. (defvar grep-find-history nil)
  374. (defun compilation-mode-font-lock-keywords ()
  375. "Return expressions to highlight in Compilation mode."
  376. (nconc
  377. ;;
  378. ;; Compiler warning/error lines.
  379. (mapcar (function
  380. (lambda (item)
  381. ;; Prepend "^", adjusting FILE-IDX and LINE-IDX accordingly.
  382. (let ((file-idx (nth 1 item))
  383. (line-idx (nth 2 item))
  384. (col-idx (nth 3 item))
  385. keyword)
  386. (when (numberp col-idx)
  387. (setq keyword
  388. (cons (list (1+ col-idx) 'font-lock-type-face nil t)
  389. keyword)))
  390. (when (numberp line-idx)
  391. (setq keyword
  392. (cons (list (1+ line-idx) 'font-lock-variable-name-face)
  393. keyword)))
  394. (when (numberp file-idx)
  395. (setq keyword
  396. (cons (list (1+ file-idx) 'font-lock-warning-face)
  397. keyword)))
  398. (cons (concat "^\\(" (nth 0 item) "\\)") keyword))))
  399. compilation-error-regexp-alist)
  400. (list
  401. ;;
  402. ;; Compiler output lines. Recognize `make[n]:' lines too.
  403. '("^\\([A-Za-z_0-9/\.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
  404. (1 font-lock-function-name-face) (3 font-lock-comment-face nil t)))
  405. ))
  406. ;;;###autoload
  407. (defun compile (command)
  408. "Compile the program including the current buffer. Default: run `make'.
  409. Runs COMMAND, a shell command, in a separate process asynchronously
  410. with output going to the buffer `*compilation*'.
  411. You can then use the command \\[next-error] to find the next error message
  412. and move to the source code that caused it.
  413. Interactively, prompts for the command if `compilation-read-command' is
  414. non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
  415. To run more than one compilation at once, start one and rename the
  416. \`*compilation*' buffer to some other name with \\[rename-buffer].
  417. Then start the next one.
  418. The name used for the buffer is actually whatever is returned by
  419. the function in `compilation-buffer-name-function', so you can set that
  420. to a function that generates a unique name."
  421. (interactive
  422. (if (or compilation-read-command current-prefix-arg)
  423. (list (read-from-minibuffer "Compile command: "
  424. compile-command nil nil
  425. '(compile-history . 1)))
  426. (list compile-command)))
  427. (setq compile-command command)
  428. (save-some-buffers (not compilation-ask-about-save) nil)
  429. (compile-internal compile-command "No more errors"))
  430. ;;; run compile with the default command line
  431. (defun recompile ()
  432. "Re-compile the program including the current buffer."
  433. (interactive)
  434. (save-some-buffers (not compilation-ask-about-save) nil)
  435. (compile-internal compile-command "No more errors"))
  436. (defun grep-process-setup ()
  437. "Set up `compilation-exit-message-function' for `grep'."
  438. (set (make-local-variable 'compilation-exit-message-function)
  439. (lambda (status code msg)
  440. (if (eq status 'exit)
  441. (cond ((zerop code)
  442. '("finished (matches found)\n" . "matched"))
  443. ((= code 1)
  444. '("finished with no matches found\n" . "no match"))
  445. (t
  446. (cons msg code)))
  447. (cons msg code)))))
  448. (defun grep-compute-defaults ()
  449. (setq grep-command
  450. (if (equal (condition-case nil ; in case "grep" isn't in exec-path
  451. (call-process grep-program nil nil nil
  452. "-e" "foo" null-device)
  453. (error nil))
  454. 1)
  455. (format "%s -n -e " grep-program)
  456. (format "%s -n " grep-program)))
  457. (unless grep-find-use-xargs
  458. (setq grep-find-use-xargs
  459. (if (equal (call-process "find" nil nil nil
  460. null-device "-print0")
  461. 0)
  462. 'gnu)))
  463. (setq grep-find-command
  464. (cond ((eq grep-find-use-xargs 'gnu)
  465. (format "find . -type f -print0 | xargs -0 -e %s"
  466. grep-command))
  467. (grep-find-use-xargs
  468. (format "find . -type f -print | xargs %s" grep-command))
  469. (t (cons (format "find . -type f -exec %s {} /dev/null \\;"
  470. grep-command)
  471. (+ 22 (length grep-command)))))))
  472. ;;;###autoload
  473. (defun grep (command-args)
  474. "Run grep, with user-specified args, and collect output in a buffer.
  475. While grep runs asynchronously, you can use \\[next-error] (M-x next-error),
  476. or \\<compilation-minor-mode-map>\\[compile-goto-error] in the grep \
  477. output buffer, to go to the lines
  478. where grep found matches.
  479. This command uses a special history list for its arguments, so you can
  480. easily repeat a grep command.
  481. A prefix argument says to default the argument based upon the current
  482. tag the cursor is over, substituting it into the last grep command
  483. in the grep command history (or into `grep-command'
  484. if that history list is empty)."
  485. (interactive
  486. (let (grep-default (arg current-prefix-arg))
  487. (unless grep-command
  488. (grep-compute-defaults))
  489. (when arg
  490. (let* ((tag-default
  491. (funcall (or find-tag-default-function
  492. (get major-mode 'find-tag-default-function)
  493. ;; We use grep-tag-default instead of
  494. ;; find-tag-default, to avoid loading etags.
  495. 'grep-tag-default))))
  496. (setq grep-default (or (car grep-history) grep-command))
  497. ;; Replace the thing matching for with that around cursor
  498. (if (string-match "[^ ]+\\s +\\(-[^ ]+\\s +\\)*\\(\"[^\"]+\"\\|[^ ]+\\)" grep-default)
  499. (setq grep-default (replace-match tag-default t t
  500. grep-default 2)))))
  501. (list (read-from-minibuffer "Run grep (like this): "
  502. (or grep-default grep-command)
  503. nil nil 'grep-history))))
  504. ;; Setting process-setup-function makes exit-message-function work
  505. ;; even when async processes aren't supported.
  506. (let* ((compilation-process-setup-function 'grep-process-setup)
  507. (buf (compile-internal (if null-device
  508. (concat command-args " " null-device)
  509. command-args)
  510. "No more grep hits" "grep"
  511. ;; Give it a simpler regexp to match.
  512. nil grep-regexp-alist)))))
  513. ;; This is a copy of find-tag-default from etags.el.
  514. (defun grep-tag-default ()
  515. (save-excursion
  516. (while (looking-at "\\sw\\|\\s_")
  517. (forward-char 1))
  518. (when (or (re-search-backward "\\sw\\|\\s_"
  519. (save-excursion (beginning-of-line) (point))
  520. t)
  521. (re-search-forward "\\(\\sw\\|\\s_\\)+"
  522. (save-excursion (end-of-line) (point))
  523. t))
  524. (goto-char (match-end 0))
  525. (buffer-substring (point)
  526. (progn (forward-sexp -1)
  527. (while (looking-at "\\s'")
  528. (forward-char 1))
  529. (point))))))
  530. ;;;###autoload
  531. (defun grep-find (command-args)
  532. "Run grep via find, with user-specified args, and collect output in a buffer.
  533. While find runs asynchronously, you can use the \\[next-error] command
  534. to find the text that grep hits refer to.
  535. This command uses a special history list for its arguments, so you can
  536. easily repeat a find command."
  537. (interactive
  538. (progn
  539. (unless grep-find-command
  540. (grep-compute-defaults))
  541. (list (read-from-minibuffer "Run find (like this): "
  542. grep-find-command nil nil
  543. 'grep-find-history))))
  544. (let ((null-device nil)) ; see grep
  545. (grep command-args)))
  546. (defcustom compilation-scroll-output nil
  547. "*Non-nil to scroll the *compilation* buffer window as output appears.
  548. Setting it causes the compilation-mode commands to put point at the
  549. end of their output window so that the end of the output is always
  550. visible rather than the begining."
  551. :type 'boolean
  552. :version "20.3"
  553. :group 'compilation)
  554. (defun compile-internal (command error-message
  555. &optional name-of-mode parser
  556. error-regexp-alist name-function
  557. enter-regexp-alist leave-regexp-alist
  558. file-regexp-alist nomessage-regexp-alist)
  559. "Run compilation command COMMAND (low level interface).
  560. ERROR-MESSAGE is a string to print if the user asks to see another error
  561. and there are no more errors. The rest of the arguments, 3-10 are optional.
  562. For them nil means use the default.
  563. NAME-OF-MODE is the name to display as the major mode in the compilation
  564. buffer. PARSER is the error parser function. ERROR-REGEXP-ALIST is the error
  565. message regexp alist to use. NAME-FUNCTION is a function called to name the
  566. buffer. ENTER-REGEXP-ALIST is the enter directory message regexp alist to use.
  567. LEAVE-REGEXP-ALIST is the leave directory message regexp alist to use.
  568. FILE-REGEXP-ALIST is the change current file message regexp alist to use.
  569. NOMESSAGE-REGEXP-ALIST is the nomessage regexp alist to use.
  570. The defaults for these variables are the global values of
  571. \`compilation-parse-errors-function', `compilation-error-regexp-alist',
  572. \`compilation-buffer-name-function', `compilation-enter-directory-regexp-alist',
  573. \`compilation-leave-directory-regexp-alist', `compilation-file-regexp-alist',
  574. \ and `compilation-nomessage-regexp-alist', respectively.
  575. For arg 7-10 a value `t' means an empty alist.
  576. Returns the compilation buffer created."
  577. (let (outbuf)
  578. (save-excursion
  579. (or name-of-mode
  580. (setq name-of-mode "Compilation"))
  581. (setq outbuf
  582. (get-buffer-create
  583. (funcall (or name-function compilation-buffer-name-function
  584. (function (lambda (mode)
  585. (concat "*" (downcase mode) "*"))))
  586. name-of-mode)))
  587. (set-buffer outbuf)
  588. (let ((comp-proc (get-buffer-process (current-buffer))))
  589. (if comp-proc
  590. (if (or (not (eq (process-status comp-proc) 'run))
  591. (yes-or-no-p
  592. (format "A %s process is running; kill it? "
  593. name-of-mode)))
  594. (condition-case ()
  595. (progn
  596. (interrupt-process comp-proc)
  597. (sit-for 1)
  598. (delete-process comp-proc))
  599. (error nil))
  600. (error "Cannot have two processes in `%s' at once"
  601. (buffer-name))
  602. )))
  603. ;; In case the compilation buffer is current, make sure we get the global
  604. ;; values of compilation-error-regexp-alist, etc.
  605. (kill-all-local-variables))
  606. (or error-regexp-alist
  607. (setq error-regexp-alist compilation-error-regexp-alist))
  608. (or enter-regexp-alist
  609. (setq enter-regexp-alist compilation-enter-directory-regexp-alist))
  610. (or leave-regexp-alist
  611. (setq leave-regexp-alist compilation-leave-directory-regexp-alist))
  612. (or file-regexp-alist
  613. (setq file-regexp-alist compilation-file-regexp-alist))
  614. (or nomessage-regexp-alist
  615. (setq nomessage-regexp-alist compilation-nomessage-regexp-alist))
  616. (or parser (setq parser compilation-parse-errors-function))
  617. (let ((thisdir default-directory)
  618. outwin)
  619. (save-excursion
  620. ;; Clear out the compilation buffer and make it writable.
  621. ;; Change its default-directory to the directory where the compilation
  622. ;; will happen, and insert a `cd' command to indicate this.
  623. (set-buffer outbuf)
  624. (setq buffer-read-only nil)
  625. (buffer-disable-undo (current-buffer))
  626. (erase-buffer)
  627. (buffer-enable-undo (current-buffer))
  628. (setq default-directory thisdir)
  629. (insert "cd " thisdir "\n" command "\n")
  630. (set-buffer-modified-p nil))
  631. ;; If we're already in the compilation buffer, go to the end
  632. ;; of the buffer, so point will track the compilation output.
  633. (if (eq outbuf (current-buffer))
  634. (goto-char (point-max)))
  635. ;; Pop up the compilation buffer.
  636. (setq outwin (display-buffer outbuf))
  637. (save-excursion
  638. (set-buffer outbuf)
  639. (compilation-mode name-of-mode)
  640. ;; (setq buffer-read-only t) ;;; Non-ergonomic.
  641. (set (make-local-variable 'compilation-parse-errors-function) parser)
  642. (set (make-local-variable 'compilation-error-message) error-message)
  643. (set (make-local-variable 'compilation-error-regexp-alist)
  644. error-regexp-alist)
  645. (set (make-local-variable 'compilation-enter-directory-regexp-alist)
  646. enter-regexp-alist)
  647. (set (make-local-variable 'compilation-leave-directory-regexp-alist)
  648. leave-regexp-alist)
  649. (set (make-local-variable 'compilation-file-regexp-alist)
  650. file-regexp-alist)
  651. (set (make-local-variable 'compilation-nomessage-regexp-alist)
  652. nomessage-regexp-alist)
  653. (set (make-local-variable 'compilation-arguments)
  654. (list command error-message
  655. name-of-mode parser
  656. error-regexp-alist name-function
  657. enter-regexp-alist leave-regexp-alist
  658. file-regexp-alist nomessage-regexp-alist))
  659. (make-local-variable 'lazy-lock-defer-on-scrolling)
  660. ;; This proves a good idea if the buffer's going to scroll
  661. ;; with lazy-lock on.
  662. (setq lazy-lock-defer-on-scrolling t)
  663. (setq default-directory thisdir
  664. compilation-directory-stack (list default-directory))
  665. (set-window-start outwin (point-min))
  666. (or (eq outwin (selected-window))
  667. (set-window-point outwin (point-min)))
  668. (compilation-set-window-height outwin)
  669. (if compilation-process-setup-function
  670. (funcall compilation-process-setup-function))
  671. ;; Start the compilation.
  672. (if (fboundp 'start-process)
  673. (let* ((process-environment (cons "EMACS=t" process-environment))
  674. (proc (start-process-shell-command (downcase mode-name)
  675. outbuf
  676. command)))
  677. (set-process-sentinel proc 'compilation-sentinel)
  678. (set-process-filter proc 'compilation-filter)
  679. (set-marker (process-mark proc) (point) outbuf)
  680. (setq compilation-in-progress
  681. (cons proc compilation-in-progress)))
  682. ;; No asynchronous processes available.
  683. (message "Executing `%s'..." command)
  684. ;; Fake modeline display as if `start-process' were run.
  685. (setq mode-line-process ":run")
  686. (force-mode-line-update)
  687. (sit-for 0) ; Force redisplay
  688. (let ((status (call-process shell-file-name nil outbuf nil "-c"
  689. command)))
  690. (cond ((numberp status)
  691. (compilation-handle-exit 'exit status
  692. (if (zerop status)
  693. "finished\n"
  694. (format "\
  695. exited abnormally with code %d\n"
  696. status))))
  697. ((stringp status)
  698. (compilation-handle-exit 'signal status
  699. (concat status "\n")))
  700. (t
  701. (compilation-handle-exit 'bizarre status status))))
  702. (message "Executing `%s'...done" command)))
  703. (if compilation-scroll-output
  704. (let ((currbuf (current-buffer)))
  705. (select-window outwin)
  706. (goto-char (point-max))
  707. (select-window (get-buffer-window currbuf)))))
  708. ;; Make it so the next C-x ` will use this buffer.
  709. (setq compilation-last-buffer outbuf)))
  710. ;; Set the height of WINDOW according to compilation-window-height.
  711. (defun compilation-set-window-height (window)
  712. (and compilation-window-height
  713. (= (window-width window) (frame-width (window-frame window)))
  714. ;; If window is alone in its frame, aside from a minibuffer,
  715. ;; don't change its height.
  716. (not (eq window (frame-root-window (window-frame window))))
  717. ;; This save-excursion prevents us from changing the current buffer,
  718. ;; which might not be the same as the selected window's buffer.
  719. (save-excursion
  720. (let ((w (selected-window)))
  721. (unwind-protect
  722. (progn
  723. (select-window window)
  724. (enlarge-window (- compilation-window-height
  725. (window-height))))
  726. (select-window w))))))
  727. (defvar compilation-minor-mode-map
  728. (let ((map (make-sparse-keymap)))
  729. (define-key map [mouse-2] 'compile-mouse-goto-error)
  730. (define-key map "\C-c\C-c" 'compile-goto-error)
  731. (define-key map "\C-m" 'compile-goto-error)
  732. (define-key map "\C-c\C-k" 'kill-compilation)
  733. (define-key map "\M-n" 'compilation-next-error)
  734. (define-key map "\M-p" 'compilation-previous-error)
  735. (define-key map "\M-{" 'compilation-previous-file)
  736. (define-key map "\M-}" 'compilation-next-file)
  737. map)
  738. "Keymap for `compilation-minor-mode'.")
  739. (defvar compilation-shell-minor-mode-map
  740. (let ((map (make-sparse-keymap)))
  741. (define-key map [mouse-2] 'compile-mouse-goto-error)
  742. (define-key map "\M-\C-m" 'compile-goto-error)
  743. (define-key map "\M-\C-n" 'compilation-next-error)
  744. (define-key map "\M-\C-p" 'compilation-previous-error)
  745. (define-key map "\M-{" 'compilation-previous-file)
  746. (define-key map "\M-}" 'compilation-next-file)
  747. ;; Set up the menu-bar
  748. (define-key map [menu-bar errors-menu]
  749. (cons "Errors" (make-sparse-keymap "Errors")))
  750. (define-key map [menu-bar errors-menu stop-subjob]
  751. '("Stop" . comint-interrupt-subjob))
  752. (define-key map [menu-bar errors-menu compilation-mode-separator2]
  753. '("----" . nil))
  754. (define-key map [menu-bar errors-menu compilation-mode-first-error]
  755. '("First Error" . first-error))
  756. (define-key map [menu-bar errors-menu compilation-mode-previous-error]
  757. '("Previous Error" . previous-error))
  758. (define-key map [menu-bar errors-menu compilation-mode-next-error]
  759. '("Next Error" . next-error))
  760. map)
  761. "Keymap for `compilation-shell-minor-mode'.")
  762. (defvar compilation-mode-map
  763. (let ((map (cons 'keymap compilation-minor-mode-map)))
  764. (define-key map " " 'scroll-up)
  765. (define-key map "\^?" 'scroll-down)
  766. ;; Set up the menu-bar
  767. (define-key map [menu-bar compilation-menu]
  768. (cons "Compile" (make-sparse-keymap "Compile")))
  769. (define-key map [menu-bar compilation-menu compilation-mode-kill-compilation]
  770. '("Stop Compilation" . kill-compilation))
  771. (define-key map [menu-bar compilation-menu compilation-mode-separator2]
  772. '("----" . nil))
  773. (define-key map [menu-bar compilation-menu compilation-mode-first-error]
  774. '("First Error" . first-error))
  775. (define-key map [menu-bar compilation-menu compilation-mode-previous-error]
  776. '("Previous Error" . previous-error))
  777. (define-key map [menu-bar compilation-menu compilation-mode-next-error]
  778. '("Next Error" . next-error))
  779. (define-key map [menu-bar compilation-menu compilation-separator2]
  780. '("----" . nil))
  781. (define-key map [menu-bar compilation-menu compilation-mode-grep]
  782. '("Search Files (grep)" . grep))
  783. (define-key map [menu-bar compilation-menu compilation-mode-recompile]
  784. '("Recompile" . recompile))
  785. (define-key map [menu-bar compilation-menu compilation-mode-compile]
  786. '("Compile..." . compile))
  787. map)
  788. "Keymap for compilation log buffers.
  789. `compilation-minor-mode-map' is a cdr of this.")
  790. (put 'compilation-mode 'mode-class 'special)
  791. ;;;###autoload
  792. (defun compilation-mode (&optional name-of-mode)
  793. "Major mode for compilation log buffers.
  794. \\<compilation-mode-map>To visit the source for a line-numbered error,
  795. move point to the error message line and type \\[compile-goto-error].
  796. To kill the compilation, type \\[kill-compilation].
  797. Runs `compilation-mode-hook' with `run-hooks' (which see)."
  798. (interactive)
  799. (kill-all-local-variables)
  800. (use-local-map compilation-mode-map)
  801. (setq major-mode 'compilation-mode
  802. mode-name (or name-of-mode "Compilation"))
  803. (compilation-setup)
  804. (set (make-local-variable 'font-lock-defaults)
  805. '(compilation-mode-font-lock-keywords t))
  806. (set (make-local-variable 'revert-buffer-function)
  807. 'compilation-revert-buffer)
  808. (run-hooks 'compilation-mode-hook))
  809. (defun compilation-revert-buffer (ignore-auto noconfirm)
  810. (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
  811. (apply 'compile-internal compilation-arguments)))
  812. ;; Prepare the buffer for the compilation parsing commands to work.
  813. (defun compilation-setup ()
  814. ;; Make the buffer's mode line show process state.
  815. (setq mode-line-process '(":%s"))
  816. (set (make-local-variable 'compilation-error-list) nil)
  817. (set (make-local-variable 'compilation-old-error-list) nil)
  818. (set (make-local-variable 'compilation-parsing-end) 1)
  819. (set (make-local-variable 'compilation-directory-stack) nil)
  820. (setq compilation-last-buffer (current-buffer)))
  821. (defvar compilation-shell-minor-mode nil
  822. "Non-nil when in compilation-shell-minor-mode.
  823. In this minor mode, all the error-parsing commands of the
  824. Compilation major mode are available but bound to keys that don't
  825. collide with Shell mode.")
  826. (make-variable-buffer-local 'compilation-shell-minor-mode)
  827. (or (assq 'compilation-shell-minor-mode minor-mode-alist)
  828. (setq minor-mode-alist
  829. (cons '(compilation-shell-minor-mode " Shell-Compile")
  830. minor-mode-alist)))
  831. (or (assq 'compilation-shell-minor-mode minor-mode-map-alist)
  832. (setq minor-mode-map-alist (cons (cons 'compilation-shell-minor-mode
  833. compilation-shell-minor-mode-map)
  834. minor-mode-map-alist)))
  835. (defvar compilation-minor-mode nil
  836. "Non-nil when in compilation-minor-mode.
  837. In this minor mode, all the error-parsing commands of the
  838. Compilation major mode are available.")
  839. (make-variable-buffer-local 'compilation-minor-mode)
  840. (or (assq 'compilation-minor-mode minor-mode-alist)
  841. (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
  842. minor-mode-alist)))
  843. (or (assq 'compilation-minor-mode minor-mode-map-alist)
  844. (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
  845. compilation-minor-mode-map)
  846. minor-mode-map-alist)))
  847. ;;;###autoload
  848. (defun compilation-minor-mode (&optional arg)
  849. "Toggle compilation minor mode.
  850. With arg, turn compilation mode on if and only if arg is positive.
  851. See `compilation-mode'.
  852. Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
  853. (interactive "P")
  854. (if (setq compilation-minor-mode (if (null arg)
  855. (null compilation-minor-mode)
  856. (> (prefix-numeric-value arg) 0)))
  857. (let ((mode-line-process))
  858. (compilation-setup)
  859. (run-hooks 'compilation-minor-mode-hook))))
  860. ;; Write msg in the current buffer and hack its mode-line-process.
  861. (defun compilation-handle-exit (process-status exit-status msg)
  862. (let ((buffer-read-only nil)
  863. (status (if compilation-exit-message-function
  864. (funcall compilation-exit-message-function
  865. process-status exit-status msg)
  866. (cons msg exit-status)))
  867. (omax (point-max))
  868. (opoint (point)))
  869. ;; Record where we put the message, so we can ignore it
  870. ;; later on.
  871. (goto-char omax)
  872. (insert ?\n mode-name " " (car status))
  873. (forward-char -1)
  874. (insert " at " (substring (current-time-string) 0 19))
  875. (forward-char 1)
  876. (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
  877. ;; Force mode line redisplay soon.
  878. (force-mode-line-update)
  879. (if (and opoint (< opoint omax))
  880. (goto-char opoint))
  881. ;; Automatically parse (and mouse-highlight) error messages:
  882. (cond ((eq compile-auto-highlight t)
  883. (compile-reinitialize-errors nil (point-max)))
  884. ((numberp compile-auto-highlight)
  885. (compile-reinitialize-errors nil
  886. (save-excursion
  887. (goto-line compile-auto-highlight)
  888. (point)))))
  889. (if compilation-finish-function
  890. (funcall compilation-finish-function (current-buffer) msg))
  891. (let ((functions compilation-finish-functions))
  892. (while functions
  893. (funcall (car functions) (current-buffer) msg)
  894. (setq functions (cdr functions))))))
  895. ;; Called when compilation process changes state.
  896. (defun compilation-sentinel (proc msg)
  897. "Sentinel for compilation buffers."
  898. (let ((buffer (process-buffer proc)))
  899. (if (memq (process-status proc) '(signal exit))
  900. (progn
  901. (if (null (buffer-name buffer))
  902. ;; buffer killed
  903. (set-process-buffer proc nil)
  904. (let ((obuf (current-buffer)))
  905. ;; save-excursion isn't the right thing if
  906. ;; process-buffer is current-buffer
  907. (unwind-protect
  908. (progn
  909. ;; Write something in the compilation buffer
  910. ;; and hack its mode line.
  911. (set-buffer buffer)
  912. (compilation-handle-exit (process-status proc)
  913. (process-exit-status proc)
  914. msg)
  915. ;; Since the buffer and mode line will show that the
  916. ;; process is dead, we can delete it now. Otherwise it
  917. ;; will stay around until M-x list-processes.
  918. (delete-process proc))
  919. (set-buffer obuf))))
  920. (setq compilation-in-progress (delq proc compilation-in-progress))
  921. ))))
  922. (defun compilation-filter (proc string)
  923. "Process filter for compilation buffers.
  924. Just inserts the text, but uses `insert-before-markers'."
  925. (if (buffer-name (process-buffer proc))
  926. (save-excursion
  927. (set-buffer (process-buffer proc))
  928. (let ((buffer-read-only nil))
  929. (save-excursion
  930. (goto-char (process-mark proc))
  931. (insert-before-markers string)
  932. (run-hooks 'compilation-filter-hook)
  933. (set-marker (process-mark proc) (point)))))))
  934. ;; Return the cdr of compilation-old-error-list for the error containing point.
  935. (defun compile-error-at-point ()
  936. (compile-reinitialize-errors nil (point))
  937. (let ((errors compilation-old-error-list))
  938. (while (and errors
  939. (> (point) (car (car errors))))
  940. (setq errors (cdr errors)))
  941. errors))
  942. (defsubst compilation-buffer-p (buffer)
  943. (save-excursion
  944. (set-buffer buffer)
  945. (or compilation-shell-minor-mode compilation-minor-mode
  946. (eq major-mode 'compilation-mode))))
  947. (defun compilation-next-error (n)
  948. "Move point to the next error in the compilation buffer.
  949. Does NOT find the source line like \\[next-error]."
  950. (interactive "p")
  951. (or (compilation-buffer-p (current-buffer))
  952. (error "Not in a compilation buffer."))
  953. (setq compilation-last-buffer (current-buffer))
  954. (let ((errors (compile-error-at-point)))
  955. ;; Move to the error after the one containing point.
  956. (goto-char (car (if (< n 0)
  957. (let ((i 0)
  958. (e compilation-old-error-list))
  959. ;; See how many cdrs away ERRORS is from the start.
  960. (while (not (eq e errors))
  961. (setq i (1+ i)
  962. e (cdr e)))
  963. (if (> (- n) i)
  964. (error "Moved back past first error")
  965. (nth (+ i n) compilation-old-error-list)))
  966. (let ((compilation-error-list (cdr errors)))
  967. (compile-reinitialize-errors nil nil n)
  968. (if compilation-error-list
  969. (nth (1- n) compilation-error-list)
  970. (error "Moved past last error"))))))))
  971. (defun compilation-previous-error (n)
  972. "Move point to the previous error in the compilation buffer.
  973. Does NOT find the source line like \\[next-error]."
  974. (interactive "p")
  975. (compilation-next-error (- n)))
  976. ;; Given an elt of `compilation-error-list', return an object representing
  977. ;; the referenced file which is equal to (but not necessarily eq to) what
  978. ;; this function would return for another error in the same file.
  979. (defsubst compilation-error-filedata (data)
  980. (setq data (cdr data))
  981. (if (markerp data)
  982. (marker-buffer data)
  983. (car data)))
  984. ;; Return a string describing a value from compilation-error-filedata.
  985. ;; This value is not necessarily useful as a file name, but should be
  986. ;; indicative to the user of what file's errors are being referred to.
  987. (defsubst compilation-error-filedata-file-name (filedata)
  988. (if (bufferp filedata)
  989. (buffer-file-name filedata)
  990. (car filedata)))
  991. (defun compilation-next-file (n)
  992. "Move point to the next error for a different file than the current one."
  993. (interactive "p")
  994. (or (compilation-buffer-p (current-buffer))
  995. (error "Not in a compilation buffer."))
  996. (setq compilation-last-buffer (current-buffer))
  997. (let ((reversed (< n 0))
  998. errors filedata)
  999. (if (not reversed)
  1000. (setq errors (or (compile-error-at-point)
  1001. (error "Moved past last error")))
  1002. ;; Get a reversed list of the errors up through the one containing point.
  1003. (compile-reinitialize-errors nil (point))
  1004. (setq errors (reverse compilation-old-error-list)
  1005. n (- n))
  1006. ;; Ignore errors after point. (car ERRORS) will be the error
  1007. ;; containing point, (cadr ERRORS) the one before it.
  1008. (while (and errors
  1009. (< (point) (car (car errors))))
  1010. (setq errors (cdr errors))))
  1011. (while (> n 0)
  1012. (setq filedata (compilation-error-filedata (car errors)))
  1013. ;; Skip past the following errors for this file.
  1014. (while (equal filedata
  1015. (compilation-error-filedata
  1016. (car (or errors
  1017. (if reversed
  1018. (error "%s the first erring file"
  1019. (compilation-error-filedata-file-name
  1020. filedata))
  1021. (let ((compilation-error-list nil))
  1022. ;; Parse some more.
  1023. (compile-reinitialize-errors nil nil 2)
  1024. (setq errors compilation-error-list)))
  1025. (error "%s is the last erring file"
  1026. (compilation-error-filedata-file-name
  1027. filedata))))))
  1028. (setq errors (cdr errors)))
  1029. (setq n (1- n)))
  1030. ;; Move to the following error.
  1031. (goto-char (car (car (or errors
  1032. (if reversed
  1033. (error "This is the first erring file")
  1034. (let ((compilation-error-list nil))
  1035. ;; Parse the last one.
  1036. (compile-reinitialize-errors nil nil 1)
  1037. compilation-error-list))))))))
  1038. (defun compilation-previous-file (n)
  1039. "Move point to the previous error for a different file than the current one."
  1040. (interactive "p")
  1041. (compilation-next-file (- n)))
  1042. (defun kill-compilation ()
  1043. "Kill the process made by the \\[compile] command."
  1044. (interactive)
  1045. (let ((buffer (compilation-find-buffer)))
  1046. (if (get-buffer-process buffer)
  1047. (interrupt-process (get-buffer-process buffer))
  1048. (error "The compilation process is not running."))))
  1049. ;; Parse any new errors in the compilation buffer,
  1050. ;; or reparse from the beginning if the user has asked for that.
  1051. (defun compile-reinitialize-errors (reparse
  1052. &optional limit-search find-at-least)
  1053. (save-excursion
  1054. (set-buffer compilation-last-buffer)
  1055. ;; If we are out of errors, or if user says "reparse",
  1056. ;; discard the info we have, to force reparsing.
  1057. (if (or (eq compilation-error-list t)
  1058. reparse)
  1059. (compilation-forget-errors))
  1060. (if (and compilation-error-list
  1061. (or (not limit-search)
  1062. (> compilation-parsing-end limit-search))
  1063. (or (not find-at-least)
  1064. (>= (length compilation-error-list) find-at-least)))
  1065. ;; Since compilation-error-list is non-nil, it points to a specific
  1066. ;; error the user wanted. So don't move it around.
  1067. nil
  1068. ;; This was here for a long time (before my rewrite); why? --roland
  1069. ;;(switch-to-buffer compilation-last-buffer)
  1070. (set-buffer-modified-p nil)
  1071. (if (< compilation-parsing-end (point-max))
  1072. ;; compilation-error-list might be non-nil if we have a non-nil
  1073. ;; LIMIT-SEARCH or FIND-AT-LEAST arg. In that case its value
  1074. ;; records the current position in the error list, and we must
  1075. ;; preserve that after reparsing.
  1076. (let ((error-list-pos compilation-error-list))
  1077. (funcall compilation-parse-errors-function
  1078. limit-search
  1079. (and find-at-least
  1080. ;; We only need enough new parsed errors to reach
  1081. ;; FIND-AT-LEAST errors past the current
  1082. ;; position.
  1083. (- find-at-least (length compilation-error-list))))
  1084. ;; Remember the entire list for compilation-forget-errors. If
  1085. ;; this is an incremental parse, append to previous list. If
  1086. ;; we are parsing anew, compilation-forget-errors cleared
  1087. ;; compilation-old-error-list above.
  1088. (setq compilation-old-error-list
  1089. (nconc compilation-old-error-list compilation-error-list))
  1090. (if error-list-pos
  1091. ;; We started in the middle of an existing list of parsed
  1092. ;; errors before parsing more; restore that position.
  1093. (setq compilation-error-list error-list-pos))
  1094. ;; Mouse-Highlight (the first line of) each error message when the
  1095. ;; mouse pointer moves over it:
  1096. (let ((inhibit-read-only t)
  1097. (error-list compilation-error-list))
  1098. (while error-list
  1099. (save-excursion
  1100. (put-text-property (goto-char (car (car error-list)))
  1101. (progn (end-of-line) (point))
  1102. 'mouse-face 'highlight))
  1103. (setq error-list (cdr error-list))))
  1104. )))))
  1105. (defun compile-mouse-goto-error (event)
  1106. (interactive "e")
  1107. (save-excursion
  1108. (set-buffer (window-buffer (posn-window (event-end event))))
  1109. (goto-char (posn-point (event-end event)))
  1110. (or (compilation-buffer-p (current-buffer))
  1111. (error "Not in a compilation buffer."))
  1112. (setq compilation-last-buffer (current-buffer))
  1113. ;; `compile-reinitialize-errors' needs to see the complete filename
  1114. ;; on the line where they clicked the mouse. Since it only looks
  1115. ;; up to point, moving point to eol makes sure the filename is
  1116. ;; visible to `compile-reinitialize-errors'.
  1117. (end-of-line)
  1118. (compile-reinitialize-errors nil (point))
  1119. ;; Move to bol; the marker for the error on this line will point there.
  1120. (beginning-of-line)
  1121. ;; Move compilation-error-list to the elt of compilation-old-error-list
  1122. ;; we want.
  1123. (setq compilation-error-list compilation-old-error-list)
  1124. (while (and compilation-error-list
  1125. (> (point) (car (car compilation-error-list))))
  1126. (setq compilation-error-list (cdr compilation-error-list)))
  1127. (or compilation-error-list
  1128. (error "No error to go to")))
  1129. (select-window (posn-window (event-end event)))
  1130. ;; Move to another window, so that next-error's window changes
  1131. ;; result in the desired setup.
  1132. (or (one-window-p)
  1133. (progn
  1134. (other-window -1)
  1135. ;; other-window changed the selected buffer,
  1136. ;; but we didn't want to do that.
  1137. (set-buffer compilation-last-buffer)))
  1138. (push-mark)
  1139. (next-error 1))
  1140. (defun compile-goto-error (&optional argp)
  1141. "Visit the source for the error message point is on.
  1142. Use this command in a compilation log buffer. Sets the mark at point there.
  1143. \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
  1144. other kinds of prefix arguments are ignored."
  1145. (interactive "P")
  1146. (or (compilation-buffer-p (current-buffer))
  1147. (error "Not in a compilation buffer."))
  1148. (setq compilation-last-buffer (current-buffer))
  1149. (compile-reinitialize-errors (consp argp) (point))
  1150. ;; Move to bol; the marker for the error on this line will point there.
  1151. (beginning-of-line)
  1152. ;; Move compilation-error-list to the elt of compilation-old-error-list
  1153. ;; we want.
  1154. (setq compilation-error-list compilation-old-error-list)
  1155. (while (and compilation-error-list
  1156. (> (point) (car (car compilation-error-list))))
  1157. (setq compilation-error-list (cdr compilation-error-list)))
  1158. ;; Move to another window, so that next-error's window changes
  1159. ;; result in the desired setup.
  1160. (or (one-window-p)
  1161. (progn
  1162. (other-window -1)
  1163. ;; other-window changed the selected buffer,
  1164. ;; but we didn't want to do that.
  1165. (set-buffer compilation-last-buffer)))
  1166. (push-mark)
  1167. (next-error 1))
  1168. ;; Return a compilation buffer.
  1169. ;; If the current buffer is a compilation buffer, return it.
  1170. ;; If compilation-last-buffer is set to a live buffer, use that.
  1171. ;; Otherwise, look for a compilation buffer and signal an error
  1172. ;; if there are none.
  1173. (defun compilation-find-buffer (&optional other-buffer)
  1174. (if (and (not other-buffer)
  1175. (compilation-buffer-p (current-buffer)))
  1176. ;; The current buffer is a compilation buffer.
  1177. (current-buffer)
  1178. (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
  1179. (compilation-buffer-p compilation-last-buffer)
  1180. (or (not other-buffer) (not (eq compilation-last-buffer
  1181. (current-buffer)))))
  1182. compilation-last-buffer
  1183. (let ((buffers (buffer-list)))
  1184. (while (and buffers (or (not (compilation-buffer-p (car buffers)))
  1185. (and other-buffer
  1186. (eq (car buffers) (current-buffer)))))
  1187. (setq buffers (cdr buffers)))
  1188. (if buffers
  1189. (car buffers)
  1190. (or (and other-buffer
  1191. (compilation-buffer-p (current-buffer))
  1192. ;; The current buffer is a compilation buffer.
  1193. (progn
  1194. (if other-buffer
  1195. (message "This is the only compilation buffer."))
  1196. (current-buffer)))
  1197. (error "No compilation started!")))))))
  1198. ;;;###autoload
  1199. (defun next-error (&optional argp)
  1200. "Visit next compilation error message and corresponding source code.
  1201. If all the error messages parsed so far have been processed already,
  1202. the message buffer is checked for new ones.
  1203. A prefix arg specifies how many error messages to move;
  1204. negative means move back to previous error messages.
  1205. Just C-u as a prefix means reparse the error message buffer
  1206. and start at the first error.
  1207. \\[next-error] normally uses the most recently started compilation or
  1208. grep buffer. However, it can operate on any buffer with output from
  1209. the \\[compile] and \\[grep] commands, or, more generally, on any
  1210. buffer in Compilation mode or with Compilation Minor mode enabled. To
  1211. specify use of a particular buffer for error messages, type
  1212. \\[next-error] in that buffer.
  1213. Once \\[next-error] has chosen the buffer for error messages,
  1214. it stays with that buffer until you use it in some other buffer which
  1215. uses Compilation mode or Compilation Minor mode.
  1216. See variables `compilation-parse-errors-function' and
  1217. \`compilation-error-regexp-alist' for customization ideas."
  1218. (interactive "P")
  1219. (setq compilation-last-buffer (compilation-find-buffer))
  1220. (compilation-goto-locus (compilation-next-error-locus
  1221. ;; We want to pass a number here only if
  1222. ;; we got a numeric prefix arg, not just C-u.
  1223. (and (not (consp argp))
  1224. (prefix-numeric-value argp))
  1225. (consp argp))))
  1226. ;;;###autoload (define-key ctl-x-map "`" 'next-error)
  1227. (defun previous-error ()
  1228. "Visit previous compilation error message and corresponding source code.
  1229. This operates on the output from the \\[compile] command."
  1230. (interactive)
  1231. (next-error -1))
  1232. (defun first-error ()
  1233. "Reparse the error message buffer and start at the first error.
  1234. Visit corresponding source code.
  1235. This operates on the output from the \\[compile] command."
  1236. (interactive)
  1237. (next-error '(4)))
  1238. (defvar compilation-skip-to-next-location nil
  1239. "*If non-nil, skip multiple error messages for the same source location.")
  1240. (defun compilation-next-error-locus (&optional move reparse silent)
  1241. "Visit next compilation error and return locus in corresponding source code.
  1242. This operates on the output from the \\[compile] command.
  1243. If all preparsed error messages have been processed,
  1244. the error message buffer is checked for new ones.
  1245. Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
  1246. location of the error message in the compilation buffer, and SOURCE is a
  1247. marker at the location in the source code indicated by the error message.
  1248. Optional first arg MOVE says how many error messages to move forwards (or
  1249. backwards, if negative); default is 1. Optional second arg REPARSE, if
  1250. non-nil, says to reparse the error message buffer and reset to the first
  1251. error (plus MOVE - 1). If optional third argument SILENT is non-nil, return
  1252. nil instead of raising an error if there are no more errors.
  1253. The current buffer should be the desired compilation output buffer."
  1254. (or move (setq move 1))
  1255. (compile-reinitialize-errors reparse nil (and (not reparse)
  1256. (if (< move 1) 0 (1- move))))
  1257. (let (next-errors next-error)
  1258. (catch 'no-next-error
  1259. (save-excursion
  1260. (set-buffer compilation-last-buffer)
  1261. ;; compilation-error-list points to the "current" error.
  1262. (setq next-errors
  1263. (if (> move 0)
  1264. (nthcdr (1- move)
  1265. compilation-error-list)
  1266. ;; Zero or negative arg; we need to move back in the list.
  1267. (let ((n (1- move))
  1268. (i 0)
  1269. (e compilation-old-error-list))
  1270. ;; See how many cdrs away the current error is from the start.
  1271. (while (not (eq e compilation-error-list))
  1272. (setq i (1+ i)
  1273. e (cdr e)))
  1274. (if (> (- n) i)
  1275. (error "Moved back past first error")
  1276. (nthcdr (+ i n) compilation-old-error-list))))
  1277. next-error (car next-errors))
  1278. (while
  1279. (if (null next-error)
  1280. (progn
  1281. (and move (/= move 1)
  1282. (error (if (> move 0)
  1283. "Moved past last error")
  1284. "Moved back past first error"))
  1285. ;; Forget existing error messages if compilation has finished.
  1286. (if (not (and (get-buffer-process (current-buffer))
  1287. (eq (process-status
  1288. (get-buffer-process
  1289. (current-buffer)))
  1290. 'run)))
  1291. (compilation-forget-errors))
  1292. (if silent
  1293. (throw 'no-next-error nil)
  1294. (error (concat compilation-error-message
  1295. (and (get-buffer-process (current-buffer))
  1296. (eq (process-status
  1297. (get-buffer-process
  1298. (current-buffer)))
  1299. 'run)
  1300. " yet")))))
  1301. (setq compilation-error-list (cdr next-errors))
  1302. (if (null (cdr next-error))
  1303. ;; This error is boring. Go to the next.
  1304. t
  1305. (or (markerp (cdr next-error))
  1306. ;; This error has a filename/lineno pair.
  1307. ;; Find the file and turn it into a marker.
  1308. (let* ((fileinfo (car (cdr next-error)))
  1309. (buffer (apply 'compilation-find-file
  1310. (car next-error) fileinfo)))
  1311. (if (null buffer)
  1312. ;; We can't find this error's file.
  1313. ;; Remove all errors in the same file.
  1314. (progn
  1315. (setq next-errors compilation-old-error-list)
  1316. (while next-errors
  1317. (and (consp (cdr (car next-errors)))
  1318. (equal (car (cdr (car next-errors)))
  1319. fileinfo)
  1320. (progn
  1321. (set-marker (car (car next-errors)) nil)
  1322. (setcdr (car next-errors) nil)))
  1323. (setq next-errors (cdr next-errors)))
  1324. ;; Look for the next error.
  1325. t)
  1326. ;; We found the file. Get a marker for this error.
  1327. ;; compilation-old-error-list is a buffer-local
  1328. ;; variable, so we must be careful to extract its value
  1329. ;; before switching to the source file buffer.
  1330. (let ((errors compilation-old-error-list)
  1331. (last-line (nth 1 (cdr next-error)))
  1332. (column (nth 2 (cdr next-error))))
  1333. (set-buffer buffer)
  1334. (save-excursion
  1335. (save-restriction
  1336. (widen)
  1337. ;(goto-line last-line)
  1338. (if (and last-line (> last-line 0))
  1339. (goto-line last-line)
  1340. (beginning-of-buffer))
  1341. (if (and column (> column 0))
  1342. ;; Columns in error msgs are 1-origin.
  1343. (forward-char (1- column))
  1344. (beginning-of-line))
  1345. (setcdr next-error (point-marker))
  1346. ;; Make all the other error messages referring
  1347. ;; to the same file have markers into the buffer.
  1348. (while errors
  1349. (and (consp (cdr (car errors)))
  1350. (equal (car (cdr (car errors))) fileinfo)
  1351. (let* ((this (nth 1 (cdr (car errors))))
  1352. (column (nth 2 (cdr (car errors))))
  1353. (lines (- this last-line)))
  1354. (if (eq selective-display t)
  1355. ;; When selective-display is t,
  1356. ;; each C-m is a line boundary,
  1357. ;; as well as each newline.
  1358. (if (< lines 0)
  1359. (re-search-backward "[\n\C-m]"
  1360. nil 'end
  1361. (- lines))
  1362. (re-search-forward "[\n\C-m]"
  1363. nil 'end
  1364. lines))
  1365. (forward-line lines))
  1366. (if (and column (> column 1))
  1367. (forward-char (1- column))
  1368. (beginning-of-line))
  1369. (setq last-line this)
  1370. (setcdr (car errors) (point-marker))))
  1371. (setq errors (cdr errors)))))))))
  1372. ;; If we didn't get a marker for this error, or this
  1373. ;; marker's buffer was killed, go on to the next one.
  1374. (or (not (markerp (cdr next-error)))
  1375. (not (marker-buffer (cdr next-error))))))
  1376. (setq next-errors compilation-error-list
  1377. next-error (car next-errors)))))
  1378. (if compilation-skip-to-next-location
  1379. ;; Skip over multiple error messages for the same source location,
  1380. ;; so the next C-x ` won't go to an error in the same place.
  1381. (while (and compilation-error-list
  1382. (equal (cdr (car compilation-error-list))
  1383. (cdr next-error)))
  1384. (setq compilation-error-list (cdr compilation-error-list))))
  1385. ;; We now have a marker for the position of the error source code.
  1386. ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
  1387. next-error))
  1388. (defun compilation-goto-locus (next-error)
  1389. "Jump to an error locus returned by `compilation-next-error-locus'.
  1390. Takes one argument, a cons (ERROR . SOURCE) of two markers.
  1391. Selects a window with point at SOURCE, with another window displaying ERROR."
  1392. (if (and (window-dedicated-p (selected-window))
  1393. (eq (selected-window) (frame-root-window)))
  1394. (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
  1395. (switch-to-buffer (marker-buffer (cdr next-error))))
  1396. (goto-char (cdr next-error))
  1397. ;; If narrowing got in the way of
  1398. ;; going to the right place, widen.
  1399. (or (= (point) (marker-position (cdr next-error)))
  1400. (progn
  1401. (widen)
  1402. (goto-char (cdr next-error))))
  1403. ;; Show compilation buffer in other window, scrolled to this error.
  1404. (let* ((pop-up-windows t)
  1405. ;; Use an existing window if it is in a visible frame.
  1406. (w (or (get-buffer-window (marker-buffer (car next-error)) 'visible)
  1407. ;; Pop up a window.
  1408. (display-buffer (marker-buffer (car next-error))))))
  1409. (set-window-point w (car next-error))
  1410. (set-window-start w (car next-error))
  1411. (compilation-set-window-height w)))
  1412. ;; Find a buffer for file FILENAME.
  1413. ;; Search the directories in compilation-search-path.
  1414. ;; A nil in compilation-search-path means to try the
  1415. ;; current directory, which is passed in DIR.
  1416. ;; If FILENAME is not found at all, ask the user where to find it.
  1417. ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
  1418. (defun compilation-find-file (marker filename dir &rest formats)
  1419. (or formats (setq formats '("%s")))
  1420. (let ((dirs compilation-search-path)
  1421. buffer thisdir fmts name)
  1422. (if (file-name-absolute-p filename)
  1423. ;; The file name is absolute. Use its explicit directory as
  1424. ;; the first in the search path, and strip it from FILENAME.
  1425. (setq filename (abbreviate-file-name (expand-file-name filename))
  1426. dirs (cons (file-name-directory filename) dirs)
  1427. filename (file-name-nondirectory filename)))
  1428. ;; Now search the path.
  1429. (while (and dirs (null buffer))
  1430. (setq thisdir (or (car dirs) dir)
  1431. fmts formats)
  1432. ;; For each directory, try each format string.
  1433. (while (and fmts (null buffer))
  1434. (setq name (expand-file-name (format (car fmts) filename) thisdir)
  1435. buffer (and (file-exists-p name)
  1436. (find-file-noselect name))
  1437. fmts (cdr fmts)))
  1438. (setq dirs (cdr dirs)))
  1439. (or buffer
  1440. ;; The file doesn't exist.
  1441. ;; Ask the user where to find it.
  1442. ;; If he hits C-g, then the next time he does
  1443. ;; next-error, he'll skip past it.
  1444. (let* ((pop-up-windows t)
  1445. (w (display-buffer (marker-buffer marker))))
  1446. (set-window-point w marker)
  1447. (set-window-start w marker)
  1448. (let ((name (expand-file-name
  1449. (read-file-name
  1450. (format "Find this error in: (default %s) "
  1451. filename)
  1452. dir filename t))))
  1453. (if (file-directory-p name)
  1454. (setq name (expand-file-name filename name)))
  1455. (and (file-exists-p name)
  1456. (find-file-noselect name)))))))
  1457. ;; Set compilation-error-list to nil, and unchain the markers that point to the
  1458. ;; error messages and their text, so that they no longer slow down gap motion.
  1459. ;; This would happen anyway at the next garbage collection, but it is better to
  1460. ;; do it right away.
  1461. (defun compilation-forget-errors ()
  1462. (while compilation-old-error-list
  1463. (let ((next-error (car compilation-old-error-list)))
  1464. (set-marker (car next-error) nil)
  1465. (if (markerp (cdr next-error))
  1466. (set-marker (cdr next-error) nil)))
  1467. (setq compilation-old-error-list (cdr compilation-old-error-list)))
  1468. (setq compilation-error-list nil
  1469. compilation-directory-stack (list default-directory)
  1470. compilation-parsing-end 1)
  1471. ;; Remove the highlighting added by compile-reinitialize-errors:
  1472. (let ((inhibit-read-only t))
  1473. (remove-text-properties (point-min) (point-max) '(mouse-face highlight)))
  1474. )
  1475. ;; This function is not needed any more by compilation mode.
  1476. ;; Does anyone else need it or can it be deleted?
  1477. (defun count-regexp-groupings (regexp)
  1478. "Return the number of \\( ... \\) groupings in REGEXP (a string)."
  1479. (let ((groupings 0)
  1480. (len (length regexp))
  1481. (i 0)
  1482. c)
  1483. (while (< i len)
  1484. (setq c (aref regexp i)
  1485. i (1+ i))
  1486. (cond ((= c ?\[)
  1487. ;; Find the end of this [...].
  1488. (while (and (< i len)
  1489. (not (= (aref regexp i) ?\])))
  1490. (setq i (1+ i))))
  1491. ((= c ?\\)
  1492. (if (< i len)
  1493. (progn
  1494. (setq c (aref regexp i)
  1495. i (1+ i))
  1496. (if (= c ?\))
  1497. ;; We found the end of a grouping,
  1498. ;; so bump our counter.
  1499. (setq groupings (1+ groupings))))))))
  1500. groupings))
  1501. (defvar compilation-current-file nil
  1502. "Used by compilation-parse-errors to store filename for file being compiled")
  1503. ;; This variable is not used as a global variable. It's defined here just to
  1504. ;; shut up the byte compiler. It's bound and used by compilation-parse-errors
  1505. ;; and set by compile-collect-regexps.
  1506. (defvar compilation-regexps nil)
  1507. (defun compilation-parse-errors (limit-search find-at-least)
  1508. "Parse the current buffer as grep, cc, lint or other error messages.
  1509. See variable `compilation-parse-errors-function' for the interface it uses."
  1510. (setq compilation-error-list nil)
  1511. (message "Parsing error messages...")
  1512. (if (null compilation-error-regexp-alist)
  1513. (error "compilation-error-regexp-alist is empty!"))
  1514. (let* ((compilation-regexps nil) ; Variable set by compile-collect-regexps.
  1515. (default-directory (car compilation-directory-stack))
  1516. (found-desired nil)
  1517. (compilation-num-errors-found 0)
  1518. ;; Set up now the expanded, abbreviated directory variables
  1519. ;; that compile-abbreviate-directory will need, so we can
  1520. ;; compute them just once here.
  1521. (orig (abbreviate-file-name default-directory))
  1522. (orig-expanded (abbreviate-file-name
  1523. (file-truename default-directory)))
  1524. (parent-expanded (abbreviate-file-name
  1525. (expand-file-name "../" orig-expanded))))
  1526. ;; Make a list of all the regexps. Each element has the form
  1527. ;; (REGEXP TYPE IDX1 IDX2 ...)
  1528. ;; where TYPE is one of leave, enter, file, error or nomessage.
  1529. (compile-collect-regexps 'leave compilation-leave-directory-regexp-alist)
  1530. (compile-collect-regexps 'enter compilation-enter-directory-regexp-alist)
  1531. (compile-collect-regexps 'file compilation-file-regexp-alist)
  1532. (compile-collect-regexps 'nomessage compilation-nomessage-regexp-alist)
  1533. (compile-collect-regexps 'error compilation-error-regexp-alist)
  1534. ;; Don't reparse messages already seen at last parse.
  1535. (goto-char compilation-parsing-end)
  1536. (when (and (bobp)
  1537. (eq major-mode 'compilation-mode))
  1538. (setq compilation-current-file nil) ; No current file at start.
  1539. ;; Don't parse the first two lines as error messages.
  1540. ;; This matters for grep.
  1541. (forward-line 2))
  1542. ;; Parse messages.
  1543. (while (not (or found-desired (eobp)))
  1544. (let ((this compilation-regexps) (prev nil) (alist nil) type)
  1545. ;; Go through the regular expressions. If a match is found,
  1546. ;; variable alist is set to the corresponding alist and the
  1547. ;; matching regexp is moved to the front of compilation-regexps
  1548. ;; to make it match faster next time.
  1549. (while (and this (null alist))
  1550. (if (not (looking-at (car (car this))))
  1551. (progn (setq prev this) ; No match, go to next.
  1552. (setq this (cdr this)))
  1553. (setq alist (cdr (car this))) ; Got a match.
  1554. ;;; (if prev ; If not the first regexp,
  1555. ;;; (progn ; move it to the front.
  1556. ;;; (setcdr prev (cdr this))
  1557. ;;; (setcdr this compilation-regexps)
  1558. ;;; (setq compilation-regexps this)))
  1559. ))
  1560. (if (and alist ; Seen a match and not to
  1561. (not (eq (setq type (car alist)) 'nomessage))) ; be ignored.
  1562. (let* ((end-of-match (match-end 0))
  1563. (filename
  1564. (compile-buffer-substring (car (setq alist (cdr alist)))))
  1565. stack)
  1566. (if (eq type 'error) ; error message
  1567. (let* ((linenum (if (numberp (car (setq alist (cdr alist))))
  1568. (string-to-int
  1569. (compile-buffer-substring (car alist)))
  1570. ;; (car alist) is not a number, must be a
  1571. ;; function that is called below to return
  1572. ;; an error position descriptor.
  1573. (car alist)))
  1574. ;; Convert to integer later if linenum not a function.
  1575. (column (compile-buffer-substring
  1576. (car (setq alist (cdr alist)))))
  1577. this-error)
  1578. ;; Check that we have a file name.
  1579. (or filename
  1580. ;; No file name in message, we must have seen it before
  1581. (setq filename compilation-current-file)
  1582. (error "\
  1583. An error message with no file name and no file name has been seen earlier."))
  1584. ;; Check for a comint-file-name-prefix and prepend it if
  1585. ;; appropriate. (This is very useful for
  1586. ;; compilation-minor-mode in an rlogin-mode buffer.)
  1587. (and (boundp 'comint-file-name-prefix)
  1588. ;; If file name is relative, default-directory will
  1589. ;; already contain the comint-file-name-prefix (done
  1590. ;; by compile-abbreviate-directory).
  1591. (file-name-absolute-p filename)
  1592. (setq filename
  1593. (concat comint-file-name-prefix filename)))
  1594. ;; Some compilers (e.g. Sun's java compiler, reportedly)
  1595. ;; produce bogus file names like "./bar//foo.c" for file
  1596. ;; "bar/foo.c"; expand-file-name will collapse these into
  1597. ;; "/foo.c" and fail to find the appropriate file. So we
  1598. ;; look for doubled slashes in the file name and fix them
  1599. ;; up in the buffer.
  1600. (setq filename (command-line-normalize-file-name filename))
  1601. (setq filename
  1602. (cons filename (cons default-directory (cdr alist))))
  1603. ;; Locate the erring file and line.
  1604. ;; Make this-error a new elt for compilation-error-list,
  1605. ;; giving a marker for the current compilation buffer
  1606. ;; location, and the file and line number of the error.
  1607. ;; Save, as the start of the error, the beginning of the
  1608. ;; line containing the match.
  1609. (setq this-error
  1610. (if (numberp linenum)
  1611. (list (point-marker) filename linenum
  1612. (and column (string-to-int column)))
  1613. ;; If linenum is not a number then it must be
  1614. ;; a function returning an error position
  1615. ;; descriptor or nil (meaning no position).
  1616. (if linenum
  1617. (save-excursion
  1618. (funcall linenum filename column))
  1619. (list (point-marker) filename 1 1))))
  1620. ;; We have an error position descriptor.
  1621. ;; If we have found as many new errors as the user
  1622. ;; wants, or if we are past the buffer position he
  1623. ;; indicated, then we continue to parse until we have
  1624. ;; seen all consecutive errors in the same file. This
  1625. ;; means that all the errors of a source file will be
  1626. ;; seen in one parsing run, so that the error positions
  1627. ;; will be recorded as markers in the source file
  1628. ;; buffer that will move when the buffer is changed.
  1629. (if (and this-error
  1630. compilation-error-list ; At least one previous.
  1631. (or (and find-at-least
  1632. (>= compilation-num-errors-found
  1633. find-at-least))
  1634. (and limit-search
  1635. (>= end-of-match limit-search)))
  1636. (not (equal ; Same filename?
  1637. (car (cdr (car compilation-error-list)))
  1638. (car (cdr this-error)))))
  1639. ;; We are past the limits and the last error
  1640. ;; parsed, didn't belong to the same source file
  1641. ;; as the earlier ones i.e. we have seen all the
  1642. ;; errors belonging to the earlier file. We don't
  1643. ;; add the error just parsed so that the next
  1644. ;; parsing run can get it and the following errors
  1645. ;; in the same file all at once.
  1646. (setq found-desired t)
  1647. (goto-char end-of-match) ; Prepare for next message.
  1648. ;; Don't add the same source line more than once.
  1649. (and this-error
  1650. (not (and
  1651. compilation-error-list
  1652. (equal (cdr (car compilation-error-list))
  1653. (cdr this-error))))
  1654. (setq compilation-error-list
  1655. (cons this-error compilation-error-list)
  1656. compilation-num-errors-found
  1657. (1+ compilation-num-errors-found)))))
  1658. ;; Not an error message.
  1659. (if (eq type `file) ; Change current file.
  1660. (and filename (setq compilation-current-file filename))
  1661. ;; Enter or leave directory.
  1662. (setq stack compilation-directory-stack)
  1663. (and filename
  1664. (file-directory-p
  1665. (setq filename
  1666. ;; The directory name in the message
  1667. ;; is a truename. Try to convert it to a form
  1668. ;; like what the user typed in.
  1669. (compile-abbreviate-directory
  1670. (file-name-as-directory
  1671. (expand-file-name filename))
  1672. orig orig-expanded parent-expanded)))
  1673. (if (eq type 'leave)
  1674. (while (and stack
  1675. (not (string-equal (car stack)
  1676. filename)))
  1677. (setq stack (cdr stack)))
  1678. (setq compilation-directory-stack
  1679. (cons filename compilation-directory-stack)
  1680. default-directory filename)))
  1681. (and (eq type 'leave)
  1682. stack
  1683. (setq compilation-directory-stack (cdr stack))
  1684. (setq stack (car compilation-directory-stack))
  1685. (setq default-directory stack)))
  1686. (goto-char end-of-match) ; Prepare to look at next message.
  1687. (and limit-search (>= end-of-match limit-search)
  1688. ;; The user wanted a specific error, and we're past it.
  1689. ;; We do this check here rather than at the end of the
  1690. ;; loop because if the last thing seen is an error
  1691. ;; message, we must carefully discard the last error
  1692. ;; when it is the first in a new file (see above in
  1693. ;; the error-message case)
  1694. (setq found-desired t)))
  1695. ;; Go to before the last character in the message so that we will
  1696. ;; see the next line also when the message ended at end of line.
  1697. ;; When we ignore the last error message above, this will
  1698. ;; cancel the effect of forward-line below so that point
  1699. ;; doesn't move.
  1700. (forward-char -1)
  1701. ;; Is this message necessary any more? Parsing is now so fast
  1702. ;; that you might not need to know how it proceeds.
  1703. (message
  1704. "Parsing error messages...%d found. %.0f%% of buffer seen."
  1705. compilation-num-errors-found
  1706. ;; Use floating-point because (* 100 (point)) frequently
  1707. ;; exceeds the range of Emacs Lisp integers.
  1708. (/ (* 100.0 (point)) (point-max)))
  1709. ))
  1710. (forward-line 1))) ; End of while loop. Look at next line.
  1711. (setq compilation-parsing-end (point))
  1712. (setq compilation-error-list (nreverse compilation-error-list))
  1713. ;;; (message "Parsing error messages...done. %d found. %.0f%% of buffer seen."
  1714. ;;; compilation-num-errors-found
  1715. ;;; (/ (* 100.0 (point)) (point-max)))
  1716. (message "Parsing error messages...done.")))
  1717. (defun compile-collect-regexps (type this)
  1718. ;; Add elements to variable compilation-regexps that is bound in
  1719. ;; compilation-parse-errors.
  1720. (and (not (eq this t))
  1721. (while this
  1722. (setq compilation-regexps
  1723. (cons (cons (car (car this)) (cons type (cdr (car this))))
  1724. compilation-regexps))
  1725. (setq this (cdr this)))))
  1726. (defun compile-buffer-substring (index)
  1727. ;; Get substring matched by INDEXth subexpression.
  1728. (if index
  1729. (let ((beg (match-beginning index)))
  1730. (if beg (buffer-substring beg (match-end index))))))
  1731. ;; If directory DIR is a subdir of ORIG or of ORIG's parent,
  1732. ;; return a relative name for it starting from ORIG or its parent.
  1733. ;; ORIG-EXPANDED is an expanded version of ORIG.
  1734. ;; PARENT-EXPANDED is an expanded version of ORIG's parent.
  1735. ;; Those two args could be computed here, but we run faster by
  1736. ;; having the caller compute them just once.
  1737. (defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
  1738. ;; Apply canonical abbreviations to DIR first thing.
  1739. ;; Those abbreviations are already done in the other arguments passed.
  1740. (setq dir (abbreviate-file-name dir))
  1741. ;; Check for a comint-file-name-prefix and prepend it if appropriate.
  1742. ;; (This is very useful for compilation-minor-mode in an rlogin-mode
  1743. ;; buffer.)
  1744. (if (boundp 'comint-file-name-prefix)
  1745. (setq dir (concat comint-file-name-prefix dir)))
  1746. (if (and (> (length dir) (length orig-expanded))
  1747. (string= orig-expanded
  1748. (substring dir 0 (length orig-expanded))))
  1749. (setq dir
  1750. (concat orig
  1751. (substring dir (length orig-expanded)))))
  1752. (if (and (> (length dir) (length parent-expanded))
  1753. (string= parent-expanded
  1754. (substring dir 0 (length parent-expanded))))
  1755. (setq dir
  1756. (concat (file-name-directory
  1757. (directory-file-name orig))
  1758. (substring dir (length parent-expanded)))))
  1759. dir)
  1760. (provide 'compile)
  1761. ;;; compile.el ends here