Initial Commit
[packages] / xemacs-packages / prog-modes / mode-compile.el
1 ;;; mode-compile.el ---  Smart command for compiling files
2 ;;                       according to major-mode.
3 ;;
4 ;;   Copyright (c) 1994 - 2003 heddy Boubaker C.E.N.A.
5 ;;
6 ;;   Author: Heddy Boubaker <heddy.Boubaker@cena.fr>
7 ;;   Maintainer: Heddy Boubaker <heddy.Boubaker@cena.fr>
8 ;;   Created: June 1994
9 ;;   Last modified: 2003/04/01 13:52:47
10 ;;   Version: 2.28
11 ;;   Keywords: compile, compilation, modes, languages
12 ;;   Tested for:
13 ;;     XEmacs (Lucid GNU Emacs) >= 19.10
14 ;;     Must work with FSF GNU Emacs > 19.31 ;-)
15 ;;     Do not work anymore for Emacsen <= 18
16 ;;   Ftp access:
17 ;;    archive.cis.ohio-state.edu:pub/gnu/emacs/elisp-archive/misc/mode-compile.el.Z
18 ;;   WWW access:
19 ;;    <URL http://www.tls.cena.fr/~boubaker/Emacs/>
20 ;;
21 ;; LCD Archive Entry:
22 ;; mode-compile|Heddy Boubaker|boubaker@cena.fr|
23 ;; Smart command for compiling files according to major-mode and more.|
24 ;; 2003/04/01 13:52:47|2.28|~/misc/mode-compile.el.Z|
25 ;;
26 ;;; This file is NOT part of GNU Emacs but the same permissions apply.
27 ;;
28 ;; GNU Emacs is free software; you can redistribute  it and/or modify it under
29 ;; the terms  of  the GNU General   Public License as   published  by the Free
30 ;; Software Foundation;    either version 2,   or (at  your option)  any later
31 ;; version.
32 ;;
33 ;; GNU Emacs  is distributed in  the hope that it will  be useful, but WITHOUT
34 ;; ANY  WARRANTY; without  even the  implied   warranty of MERCHANTABILITY  or
35 ;; FITNESS  FOR A PARTICULAR PURPOSE.  See  the GNU General Public License for
36 ;; more details.
37 ;;
38 ;; You  should have received a  copy of the GNU  General  Public License along
39 ;; with GNU Emacs; see the  file COPYING.  If  not, write to the Free Software
40 ;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
41 ;;
42 ;; @ Purpose:
43 ;; ==========
44 ;;
45 ;;  Provide `mode-compile' function as a replacement  for the use of `compile'
46 ;;  command which  is  very dumb  for  creating its  compilation command (use
47 ;;  "make  -k" by default).  `mode-compile'  is  a layer  above `compile'; Its
48 ;;  purpose is mainly   to  build a  smart  compile-command for  `compile'  to
49 ;;  execute  it. This   compile-command is   built   according  to number   of
50 ;;  parameters:
51 ;;   - the major-mode.
52 ;;   - presence or not of a makefile in current directory.
53 ;;   - the buffer-file-name and extension.
54 ;;   - what is in the current buffer (`main' function,"#!/path/shell", ...).
55 ;;   - and more ... (see Commentary section below).
56 ;;  Most  of  these  parameters  are  highly customizable  through Emacs  Lisp
57 ;;  variables  (to be  set  in your  .emacs  or  through Customization  menu).
58 ;;  Running  mode-compile after   an  universal-argument (C-u)  allows  remote
59 ;;  compilations, user is  prompted  for a host name   to run the  compilation
60 ;;  command on.   Another  function  provided  is  `mode-compile-kill'   which
61 ;;  terminate a running compilation session launched by `mode-compile'.
62 ;;
63 ;; @ Installation:
64 ;; ===============
65 ;;
66 ;;   Byte compile this file (*) somewhere in your `load-path' and add in
67 ;;  your .emacs:
68 ;;  (autoload 'mode-compile "mode-compile"
69 ;;   "Command to compile current buffer file based on the major mode" t)
70 ;;  (global-set-key "\C-cc" 'mode-compile)
71 ;;  (autoload 'mode-compile-kill "mode-compile"
72 ;;   "Command to kill a compilation launched by `mode-compile'" t)
73 ;;  (global-set-key "\C-ck" 'mode-compile-kill)
74 ;;
75 ;;  By  default mode-compile is  very verbose  and  waits a  few seconds (1 by
76 ;;  default) after  each message for   the user to have  time  to read it. You
77 ;;  could      set           variables      `mode-compile-expert-p'        and
78 ;;  `mode-compile-reading-time'   to  change  this    behaviour.  On X-Windows
79 ;;  systems  setting the variable  `mode-compile-other-frame-p'  will create a
80 ;;  new frame and launch the compilation command in it.
81 ;;
82 ;;  (*) Don't take care of messages:
83 ;;        ** reference to free variable efs-remote-shell-file-name
84 ;;      This is perfectly normal ;-}. But if you know a way to avoid it let me
85 ;;      know.
86 ;;
87 ;; @ Bug Reports:
88 ;; ==============
89 ;;
90 ;;   To   report a  bug  please  use function `mode-compile-submit-bug-report'
91 ;;   Please note that this bug-report facility uses Barry Warsaw's reporter.el
92 ;;   which is part of GNU Emacs v19 and bundled with many  other packages.  If
93 ;;   needed, you can obtain a copy of reporter.el at the elisp-archive.
94 ;;
95 ;; @ Documentation:
96 ;; ================
97 ;;
98 ;;  This section will explain how the `compile-command' are built according to
99 ;;  the `major-mode' and how to  customize it.  The major modes `mode-compile'
100 ;;  currently known are:
101 ;;   - c-mode, c++-mode, makefile-mode, dired-mode, ada-mode, emacs-lisp-mode,
102 ;;     lisp-interaction-mode, sh-mode, csh-mode, fundamental-mode,  text-mode,
103 ;;     indented-text-mode     compilation-mode,  fortran-mode,    c?perl-mode,
104 ;;     zsh-mode java-mode, tcl-mode, python-mode
105 ;;  For other modes a default behaviour is provided.
106 ;;
107 ;;  When    running    `mode-compile'  or     `mode-compile-kill'   the  hooks
108 ;;  `mode-compile-(before|after)-(compile|kill)-hook'  are   executed.     The
109 ;;  current   buffer   could   be      automatically    saved    if    variable
110 ;;  `mode-compile-always-save-buffer-p' is  set  to   `t'.  ALL  the  modified
111 ;;  buffers could  be automatically saved if variable `mode-compile-save-all-p'
112 ;;  is set to `t'.
113 ;;
114 ;; @@ fundamental-mode, text-mode, indented-text-mode & UNKNOWN MODES:
115 ;;    *** THIS IS TOO THE DEFAULT BEHAVIOR FOR UNKNOWN MODES ***
116 ;;    Try to guess what the file is by:
117 ;;   - 1st looking at its name and extension (see variable
118 ;;     `mode-compile-filename-regexp-alist').
119 ;;   - 2nd looking at string "#!/path/shell" at first line to extract shell
120 ;;     to run the script with (see variable `mode-compile-shell-alist').
121 ;;   - 3rd looking at a makefile in current directory.
122 ;;   - then calling `compile' with the last compile command which is
123 ;;     asked to be edited by user ...
124 ;;   The `kill-compile' command is then bound dynamically (buffer-local).
125 ;;
126 ;; @@ compilation-mode:
127 ;;    Call `compile' with the last compile command.
128 ;;
129 ;; @@ makefile-mode:
130 ;;    The makefile is run with make through `compile' (user is prompted
131 ;;   for        the        rule         to      run,      see         variable
132 ;;   `mode-compile-preferred-default-makerule'  to  see how  a   default choice
133 ;;   could be selected).
134 ;;
135 ;; @@ emacs-lisp-mode, lisp-interaction-mode:
136 ;;    If the buffer is a .el file byte-compile it to produce a .elc file,
137 ;;   else  just  byte-compile  the   buffer  (this   don't  use  `compile' but
138 ;;   `byte-compile').
139 ;;
140 ;; @@ dired-mode:
141 ;;   Find a makefile   in   the directory and  run    make with  it   (like in
142 ;;   makefile-mode),  else try  to byte-recompile  all .el   files older than
143 ;;   their associated  .elc  files (unlike  `byte-recompile-directory' this is
144 ;;   not  recursive),  finally if  no  .el  files  are present ask compilation
145 ;;   command to  user by  calling  `default-compile'.  To  find  a  makefile a
146 ;;   regexp is provided which name is `mode-compile-makefile-regexp'.
147 ;;
148 ;; @@ sh-mode, csh-mode, zsh-mode:
149 ;;    Run "[cz]?sh" with debugging arguments as specified in
150 ;;   `[cz]?sh-dbg-flags' on the currently edited file.
151 ;;
152 ;; @@ c?perl-mode:
153 ;;    Run  file with "perl   -w"  (can step   through errors  with  compile's
154 ;;    `next-error' command).
155 ;;
156 ;; @@ tcl-mode:
157 ;;    Run     file  with "wish"  (can     step  through errors with compile's
158 ;;    `next-error' command).
159 ;;
160 ;; @@ c-mode, c++-mode:
161 ;;   First it try to see if there is a makefile in the directory, makefiles to
162 ;;   look for are  specified  by the variable  `mode-compile-makefile-regexp'.
163 ;;   If yes two cases could happen: there  is only one  makefile so use it, or
164 ;;   there is more  than one (sometimes when  you need to  write portable soft
165 ;;   you  could have  some makefiles by  system:  SunOs.make, HP.make ...), in
166 ;;   that case prompt to  user for choice  (with smart completion).   Once the
167 ;;   makefile has been selected  it extract the rules from  it and ask to user
168 ;;   to   choose  a   rule  to  make  (with  smart  completion,  see  variable
169 ;;   `mode-compile-preferred-default-makerule'  to  see  how  a default choice
170 ;;   could be selected).
171 ;;
172 ;;   There are some cases where no makefiles are  presents (YES I KNOW this is
173 ;;   bad  practice but you  sometimes have no  needs  to write a Makefile). In
174 ;;   that case the  function try  to build  the most intelligent   compilation
175 ;;   command by using the favourite  user C/C++ compiler: value of environment
176 ;;   variable "CC"  or "CXX"  or  first found,   in  the PATH,   of  compilers
177 ;;   specified in variable  `cc-compilers-list' or `c++-compilers-list'.  Then
178 ;;   it look for the  varenv "CFLAGS" of  "CXXFLAGS" to append to the compiler
179 ;;   command,        find            the     file         to          compile:
180 ;;   <name-of-the-file-to-compiled>.(c|cc|C|cpp)  (see    *)   and   ask   for
181 ;;   confirmation.  If  you  really trust mode-compile   will build  the right
182 ;;   command  and  want to  bypass confirmation  you  could  set the  variable
183 ;;   `mode-compile-never-edit-command-p' to t.
184 ;;
185 ;;   (*) How to find <name-of-the-file-to-compiled>:
186 ;;    In both case the command try to guess which file has to be compiled:
187 ;;   It's a trivial choice when current buffer file is a
188 ;;   .(c|C|cc|cpp... -any file with extension specified in
189 ;;   `cc-source-file-ext-list' or `c++-source-file-ext-list') file but
190 ;;   when it's a .(h|H|hh) file what to do?  The variable
191 ;;   `cc-companion-file-regexp' or `c++-companion-file-regexp' specify
192 ;;   how to find a .(c|C|cc|cpp...) file from a .(h|H|hh...); This is
193 ;;   done by appending .(c|C|cc|cpp) to
194 ;;   <filename-without-matching-regexp>.  In c-mode with default value
195 ;;   it produce:
196 ;;      file.h, file_[Pp].h -> file.c
197 ;;      I sometimes use files _p.h to indicate that the file is a private header
198 ;;      file for a .c file.
199 ;;   In c++-mode with default value it produce:
200 ;;      file.hh, file_[Pp].hh -> file.cc
201 ;;      I sometimes use files _p.cc to indicate that the file is a private header
202 ;;      file for a .cc file.
203 ;;   The output of compilation will be a
204 ;;   <name-of-the-file-to-compiled>.o file if no `main' function is
205 ;;   found inside or a <name-of-the-file-to-compiled> EXECUTABLE file
206 ;;   if `main' function found.
207 ;;
208 ;; @@ ada-mode:
209 ;;   Same  as c/c++-mode but  run Ada compiler on the  Ada file.  There are no
210 ;;   companion file and no way to find a main function in Ada.
211 ;;
212 ;; @@ fortran-mode:
213 ;;    Same as c-mode but run Fortran compiler on .[Ff](or)? files.
214 ;;
215 ;; @@ java-mode:
216 ;;    Same as c-mode but call "javac" without the -o option on .java files
217 ;;
218 ;; @@  python-mode:
219 ;;    Run file with "python" (can step through errors with compile's
220 ;;    `next-error' command).
221 ;;
222 ;; @@  message-mode:
223 ;;    Run `message-send'.
224 ;;
225 ;; @ WhatsNew:
226 ;; ===========
227 ;;
228 ;;  Support for cperl-mode
229 ;;  Require cl 
230 ;;
231 ;; @ Contributors/Helpers:
232 ;; =======================
233 ;;
234 ;;   Adrian Aichner <aichner@ecf.teradyne.com>
235 ;;   "William A. Perkins" <wa_perkins@pnl.gov>
236 ;;   Bin Mu <mubin@DerivaTech.COM>
237 ;;   Gael MARZIOU <Gael_Marziou@grenoble.hp.com>
238 ;;   Christian Motschke <motschke@prosun.first.gmd.de>
239 ;;   boris <boris@cs.rochester.edu>
240 ;;   Edward Hartnett <ejh@larry.gsfc.nasa.gov>.
241 ;;   Hartmut MANZ <manz@intes-stuttgart.de>.
242 ;;   Henry Guillaume <henryg@tusc.com.au>.
243 ;;   Ian Young <imy@wcl-rs.bham.ac.uk>
244 ;;   Ilya Zakharevich <ilya@math.ohio-state.edu>.
245 ;;   Kevin Broadey <KevinB@bartley.demon.co.uk>.
246 ;;   Lawrence R. Dodd <dodd@roebling.poly.edu>.
247 ;;   Martin Jost <asictest@ztivax.zfe.siemens.de>.
248 ;;   Michael Welsh Duggan <md5i+@andrew.cmu.edu>.
249 ;;   Rolf EBERT <rolf@gundog.lbl.gov>.
250 ;;   Scott Hofmann <scotth@visix.com>.
251 ;;   Stefan Schoef <Stefan.Schoef@arbi.informatik.uni-oldenburg.de>.
252 ;;   John W. Harwell <ccjohnh@showme.missouri.edu> - JWH.
253 ;;
254 ;; @ ToDo:
255 ;; =======
256 ;;
257 ;;   Extending this to some others programming languages (modes).
258 ;;   Writing an Info documentation.
259 ;;   Contributors are greatly accepted (send me diffs and don't forget to
260 ;;   update documentation and all comments too please).
261 ;;   Maybe Using ange-ftp parse .netrc utilities for remote host and
262 ;;   user infos.
263 ;;
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 \f
266
267 ;; @ Requirements ;;;
268
269 ;; mode-compile is not a replacement for compile
270 ;; it is just a layer above it.
271 (require 'compile)
272 ;;; For Emacs-Lisp files compilations
273 (require 'byte-compile "bytecomp")
274 ;;; For easy macros
275 (require 'backquote)
276 (require 'cl)
277 (load-library "cl-macs")
278 ;; Pretty print elisp
279 (require 'pp)
280 ;;; Setting obsolete vars
281 (eval-and-compile
282   (condition-case ()
283       (require 'obsolete)
284     (error nil))
285   (if (and (featurep 'obsolete) (fboundp 'define-obsolete-variable-alias))
286       nil ;; We've got what we needed
287     (defmacro define-obsolete-variable-alias (old new)
288       (` (progn (defalias (, old) (, new))
289                 (put (, old) 'byte-obsolete-variable (, new)))
290          ))))
291 ;;; For using custom - stolen from w3-cus.el -
292 (eval-and-compile
293   (condition-case ()
294       (require 'custom)
295     (error nil))
296   (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
297       nil ;; We've got what we needed
298     ;; We have the old custom-library, hack around it!
299     (defmacro defgroup (&rest args)
300       nil)
301     (defmacro defcustom (var value doc &rest args)
302       (` (defvar (, var) (, value) (, doc))))))
303
304 ;; Custom groups
305 (defgroup compilation nil
306   "Compilations from within Emacs variables."
307   :link '(url-link :tag "Author's Emacs Page"
308                    "http://www.tls.cena.fr/~boubaker/Emacs/")
309   :group 'tools
310   :group 'development)
311 (defgroup compilation-lang nil
312   "Language specific compilation options."
313   :group 'languages
314   :group 'compilation)
315 (defgroup compilation-script nil
316   "Scripts compilation options."
317   :group 'compilation)
318 (defgroup compilation-elisp nil
319   "Emacs development compilation options."
320   :group 'lisp
321   :group 'compilation)
322
323 ;; @ User variables ;;;
324 ;; @@ Common variables to mode-compile for all modes ;;;
325
326 (defcustom mode-compile-modes-alist
327   '((c-mode                . (cc-compile        kill-compilation))
328     (java-mode             . (java-compile      kill-compilation))
329     (c++-mode              . (c++-compile       kill-compilation))
330     (ada-mode              . (ada-compile       kill-compilation))
331     (fortran-mode          . (f77-compile       kill-compilation))
332     (dired-mode            . (dired-compile     kill-compilation))
333     (emacs-lisp-mode       . (elisp-compile     keyboard-quit)) ; I'm SURE IT'S NOT the best way
334     (lisp-interaction-mode . (elisp-compile     keyboard-quit)) ; to kill a byte-compilation.
335     (makefile-mode         . (makefile-compile  kill-compilation))
336     (sh-mode               . (sh-compile        kill-compilation))
337     (csh-mode              . (csh-compile       kill-compilation))
338     (zsh-mode              . (zsh-compile       kill-compilation))
339     (perl-mode             . (perl-compile      kill-compilation))
340     (cperl-mode            . (perl-compile      kill-compilation))
341     (tcl-mode              . (tcl-compile       kill-compilation)) ; JWH
342     (python-mode           . (python-compile    kill-compilation)) ; BM
343     ;(message-mode          . (message-compile   kill-compilation))
344     (fundamental-mode      . (guess-compile     nil)) ; bound dynamically
345     (text-mode             . (guess-compile     nil)) ; itou
346     (indented-text-mode    . (guess-compile     nil)) ; itou
347     (compilation-mode      . (default-compile   kill-compilation)))
348   "Assoc list of compile/kill functions for some known modes.
349
350 Each element look like (MODE . (COMPILE-FUNCTION KILL-FUNCTION))
351  `mode-compile' will call COMPILE-FUNCTION and `mode-compile-kill'
352  KILL-FUNCTION if current major-mode is MODE.
353
354 If you want to add or modify a COMPILE-FUNCTION and its associated
355 KILL-FUNCTION for MODE and don't want to hack `mode-compile' you could
356 do the following (it exists however a more subtle method for
357 modifying, this is left as an exercise for the reader :-):
358  (defun my-mode-compile() ...)
359  (defun my-mode-compile-kill() ...)
360  (setq mode-compile-modes-alist
361        (append '((my-mode . (my-mode-compile my-mode-compile-kill)))
362                mode-compile-modes-alist))"
363   :type '(repeat
364           (cons :tag "Association: mode/compilation functions"
365                 (function :tag "Mode")
366                 (list :tag "Compilation functions"
367                       (choice :tag "Function to run the compilation"
368                               (function-item :tag "Default" :value default-compile)
369                               (function-item :tag "Guess" :value guess-compile)
370                               (function-item :tag "Emacs lisp byte compilation" :value elisp-compile)
371                               (function :tag "Your choice, take care..."))
372                       (choice :tag "Function to kill a running compilation"
373                               (function-item :tag "Just kill" :value kill-compilation)
374                               (const :tag "Nothing -- use with guess-compile --" :value nil)
375                               (function-item :tag "To use with elisp-compile" :value keyboard-quit)
376                               ;; This item could not be selected due to a custom (hum) feature ...
377                               (function :tag "Your choice, take care..." :value nil)))))
378   :group 'compilation)
379
380 (defcustom mode-compile-filename-regexp-alist
381   ;; These could be in auto-mode-alist. But if you are like me
382   ;; and don't like these modes (prefer to edit these kind of
383   ;; files in text-mode) this is a nice way to compile them
384   ;; without to be bored with their associated modes.
385   '((mode-compile-makefile-regexp . makefile-mode)
386     ("\\.sh$"                     . sh-mode)
387     ("\\.csh$"                    . csh-mode)
388     ("\\.zsh$"                    . zsh-mode))
389   "Assoc list of major-modes for some filenames regexp.
390
391 Each element look like (REGEXP . MODE) This variable is really similar
392 to `auto-mode-alist' in the fact that it associate a MODE to a REGEXP
393 matching a filename. The only differences is that you are not obliged
394 to have the specified MODE available to use it (`guess-compile' use
395 it), the MODE is only a pointer to an association in
396 `mode-compile-modes-alist' to get the COMPILE-FUNCTION and the
397 KILL-FUNCTION. The REGEXP could be a form which evaluate to a string.
398
399 To add a new filename regexp do the following:
400  (setq mode-compile-filename-regexp-alist
401        (append '((my-filename-regexp . some-mode-mode-compile-know)
402                mode-compile-modes-alist))"
403   :type '(repeat
404           (cons :tag "Association: filename/mode"
405            (choice :tag "Filename regexp match"
406             (regexp :tag "Regexp as a string")
407             (symbol :tag "Variable containing the regexp")
408             (sexp :tag "Form which evaluate to a string"))
409            ;; I need to bind dynamically this with const, ideas??
410            ;;`(choice
411            ;; ,@(mapcar (lambda (x) `(const ,(car x))) mode-compile-modes-alist))))
412            (function :tag "Mode to use -- should be a valid association in mode-compile-modes-alist --")))
413   :group 'compilation)
414
415 (defcustom mode-compile-shell-alist
416   '(("sh"     .  sh-mode)
417     ("csh"    .  csh-mode)
418     ("zsh"    .  zsh-mode)
419     ("perl"   .  perl-mode)
420     ("tcl"    .   tcl-mode) ; JWH
421     ("python" . python-mode)) ; BM
422   "Assoc list of compile function for some known shells.
423
424 Each element look like (SHELL . MODE) This variable look like
425 `auto-mode-alist' in the fact that it associate a MODE to a name; A
426 SHELL name here. The main difference is that you are not obliged to
427 have the specified MODE available to use it (`guess-compile' use it),
428 the MODE is only a pointer to an association in `mode-compile-modes-alist'
429 to get the COMPILE-FUNCTION and the KILL-FUNCTION.
430
431 To add a new shell do the following:
432  (setq mode-compile-filename-shell-alist
433        (append '((my-shell-name . some-mode-mode-compile-know)
434                mode-compile-modes-alist))"
435   :type '(repeat
436           (cons :tag "Association: shell name/mode"
437            (string :tag "Shell name")
438            ;; I need to bind dynamically this with const, ideas??
439            ;;`(choice
440            ;; ,@(mapcar (lambda (x) `(const ,(car x))) mode-compile-modes-alist))))
441            (function :tag "Mode to use -- should be a valid association in mode-compile-modes-alist --")))
442   :group 'compilation)
443
444 ;;;###autoload
445 (defcustom mode-compile-make-program "make"
446   "*The `make' program used to process makefiles.
447
448 If you have GNU make installed with name \"gmake\" use it."
449   :type 'string
450   :group 'compilation)
451
452 (defcustom mode-compile-makefile-regexp
453   "\\(^[Mm]akefile\\|.*\\.[mM][aA]?[kK][eE]?\\.?.*$\\)"
454   "Regexp matching 'could be' makefiles filenames."
455   :type 'regexp
456   :group 'compilation)
457
458 (defcustom mode-compile-makefile-backups-regexp
459   "\\(\\(~\\|\\.[bB][aA][cC]?[kK]\\)$\\)\\|\\(\\(^\\|/\\)[.,][^/]+$\\)"
460   "Regexp to find if a Makefile is a backup or not"
461   :type 'regexp
462   :group 'compilation)
463
464 ;;;###autoload
465 (defcustom mode-compile-ignore-makefile-backups t
466   "*Tell mode compile to ignore makefiles backup files when selecting the Makefile to use."
467   :type 'boolean
468   :group 'compilation)
469
470 ;;;###autoload
471 (defvar mode-compile-default-make-options "-k"
472   "Default options to give to `make'.")
473 ;;;###autoload
474 (defcustom mode-compile-make-options (eval mode-compile-default-make-options)
475   "*Options to give to `make'.
476 This could be any form evaluating to a string.
477
478 Some people asked me a way to modify the make options every time a
479 compilation command is launched, do that:
480  (defun my-mode-compile-ask-make-options()
481    \"*Hook called by mode-compile, asking for make options.\"
482    (interactive)
483    (read-string \"Make options: \"
484                 mode-compile-default-make-options))
485  (setq mode-compile-make-options
486            'my-mode-compile-ask-make-options)"
487   :type '(choice
488           string
489           (sexp :tag "Form evaluating to a string"))
490   :group 'compilation)
491
492 ;; XEmacs change: backwards compatibility.
493 ;; APA: Make sure variable typo fix does not cause breakage!
494 ;; Keep this before the associated defcustom.
495 (define-obsolete-variable-alias
496   'mode-compile-prefered-default-makerule
497   'mode-compile-preferred-default-makerule)
498
499 ;;;###autoload
500 (defcustom mode-compile-preferred-default-makerule 'none
501   "*Default makerule you would like to see in minibuffer as a default choice
502 when selecting the make rule to build.
503
504 Possible values are:
505 'none    -- let mode-compile deciding for you.
506 'all     -- try hard to show you the \"all\" rule.
507 'default -- try hard to show you the \"default\" rule.
508 'file    -- try to show you the name of the file which will be
509             result of compilation.
510 The 'none action is taken as default is something fail."
511   :type '(radio :tag "Symbol"
512                 (const :tag "None - Let mode compile made the choice" :value none)
513                 (const :tag "All - Show the \"all\" rule" :value all)
514                 (const :tag "Default - Show the \"default\" rule" :value default)
515                 (const :tag "File - Show the \"result file name\" rule" :value file))
516   :group 'compilation)
517
518 ;;;###autoload
519 (defcustom mode-compile-ignore-makerule-regexp nil
520   "*Makefile rules which must be ignored when building completion list.
521
522 For example if you want to remove all `files rules' set
523 it to: \"\\\\.\\\\([aoc]\\\\|s[ao][.0-9]*\\\\)\". "
524   :type '(choice (const :tag "none" :value nil)
525                  (const :tag "The `all files' rule" :value "\\.\\([aoc]\\|s[ao][.0-9]*\\)")
526                  regexp)
527   :group 'compilation)
528
529 ;;;###autoload
530 (defcustom mode-compile-save-all-p nil
531   "*Non-nil means save ALL the modified buffers without asking
532 before launching compilation command."
533   :type 'boolean
534   :group 'compilation)
535
536 ;;;###autoload
537 (defcustom mode-compile-always-save-buffer-p nil
538   "*Non-nil means save the current buffer without asking
539 before launching compilation command."
540   :type 'boolean
541   :group 'compilation)
542
543 ;;;###autoload
544 (defcustom mode-compile-never-edit-command-p nil
545   "*Non-nil means never ask to user to edit the compile command."
546   :type 'boolean
547   :group 'compilation)
548
549 ;; @@ Compilation in other frame vars ;;;
550 (defgroup compilation-frame nil
551   "Compile in another frame variables."
552   :group 'frames
553   :group 'compilation)
554
555 ;;;###autoload
556 (defcustom mode-compile-other-frame-p nil
557   "*Non-nil means compile in another frame.
558
559 A new Emacs FRAME is created and the compilation command is executed
560 in this other frame.  To specify the frame parameters see also
561 variable `mode-compile-frame-parameters-alist'."
562   :type 'boolean
563   :group 'compilation-frame)
564
565 (defcustom mode-compile-other-frame-name "COMPILATION"
566   "Name of mode-compile's other frame.
567
568 This name could be used in your .Xdefaults or .Xresources file as:
569 Emacs.MODE-COMPILE-OTHER-FRAME-NAME.resource_to_be_set: ..."
570   :type 'string
571   :group 'compilation-frame)
572
573 (defconst mode-compile-default-frame-parameters
574   (list
575    (cons 'name   mode-compile-other-frame-name)
576    (cons 'width  85)  ; columns
577    (cons 'height 30)) ; lines
578    "Default parameters for mode-compile's other frame.")
579
580 (defvar mode-compile-frame-parameters-alist
581   (purecopy mode-compile-default-frame-parameters)
582   "Parameters for the new Compilation Screen created
583 if variable `mode-compile-other-frame-p' is non nil..
584
585 See also variable `mode-compile-default-frame-parameters' and
586 `mode-compile-other-frame-name'.
587
588 For informations about Screen/Frame parameters see:
589 - Info, Nodes: Lispref::Screen::Screen Parameters
590 - GNU Emacs Lisp Reference Manual, chapter 26 p375: Frames.")
591
592 ;; @@ Hooks ;;;
593
594 ;;;###autoload
595 (defcustom mode-compile-before-compile-hook nil
596   "Hook to be run before compile command is executed
597 when `mode-compile' is invoked."
598   :type 'hook
599   :group 'compilation)
600
601 ;;;###autoload
602 (defcustom mode-compile-after-compile-hook nil
603   "Hook to be run after compile command is executed
604 when `mode-compile' is invoked."
605   :type 'hook
606   :group 'compilation)
607
608 ;;;###autoload
609 (defcustom mode-compile-before-kill-hook nil
610   "Hook to be run before killing compile command is executed
611 when `mode-compile-kill' is invoked."
612   :type 'hook
613   :group 'compilation)
614
615 ;;;###autoload
616 (defcustom mode-compile-after-kill-hook nil
617   "Hook to be run after killing compile command is executed
618 when `mode-compile-kill' is invoked."
619   :type 'hook
620   :group 'compilation)
621
622 ;; @@ System dependencies ;;;
623
624 (defvar mode-compile-exe-file-ext
625   (cond
626    ((memq system-type '(ms-dos emx windows-95 windows-98 windows-nt)) ".exe")
627    (t ""))
628   "*Extension of executable files (with dot included)")
629
630 (defvar mode-compile-dir-separator-char
631   (cond
632    ;; MSDOSish file systems
633    ((memq system-type '(ms-dos emx windows-95 windows-98 windows-nt)) "\\")
634    ;; Unixish file systems
635    (t "/"))
636   "*Separator char between directories")
637
638 ;; @@ Facilities variables ;;;
639
640 ;; XEmacs change: backwards compatibility.
641 ;; APA: Make sure variable typo fix does not cause breakage!
642 ;; Keep this before the associated defvar.
643 (define-obsolete-variable-alias
644   'mode-compile-choosen-compiler
645   'mode-compile-chosen-compiler)
646
647 ;;;###autoload
648 (defvar mode-compile-chosen-compiler nil
649   "*Global variable containing the name of the compiler
650 which will be used for compiling without makefile.
651
652  Could be used in combination with
653  (cc|c++|ada|f77)-default-compiler-options
654 to automatically choose the compiler specific options.
655
656 example:
657  (defun my-compiler-get-options()
658    (cond
659     ((string= mode-compile-chosen-compiler \"gcc\")
660       \"-Wall -pedantic-errors\")
661     ((string= mode-compile-chosen-compiler \"cc\")
662       \"cc options whatever they are...\")
663     (t
664      (message \"Don't know this compiler: %s\" mode-compile-chosen-compiler)
665      (read-string
666       (format \"Options for %s compiler: \" mode-compile-chosen-compiler)))))
667
668   (setq cc-default-compiler-options 'my-compiler-get-options)")
669
670 ;; @@ User level ;;;
671
672 ;;;###autoload
673 (defcustom mode-compile-expert-p nil
674   "*Non nil means `mode-compile' will not speaks too much.
675
676 See also variable variable mode-compile-reading-time."
677   :type 'boolean
678   :group 'compilation)
679
680 ;;;###autoload
681 (defcustom mode-compile-reading-time 1
682   "*Seconds to wait in verbose mode after printing a message.
683
684 In verbose mode mode-compile print too much messages that it is
685 almost impossible to read them. Just setting this delay leave you the
686 time to read all the messages. If you don't want any delay set it to
687 `0'.
688
689 See also function sit-for."
690   :type 'integer
691   :group 'compilation)
692 \f
693
694 ;; @@ Remote compilation vars ;;;
695 (defgroup compilation-remote nil
696   "Remote compilations options."
697   :group 'compilation)
698
699 (defcustom mode-compile-remote-hosts-alist '()
700   "Alist of favourite hosts names and the username
701 to use to log on (HOSTNAME . USERNAME).
702
703 If USERNAME is a function it will be called with HOSTNAME as argument
704 and should return an USERNAME string (for example you could use
705 something like efs-get-user - not tested -), if it is nil the function
706 user-login-name will be used."
707   :type '(repeat
708           (cons
709            (string :tag "Hostname")
710            (choice
711             (const :tag "We'll use \'user-login-name" :value nil)
712             (string :tag "Username")
713             (function :tag "Function which return USERNAME given a HOSTNAME" :value efs-get-user))))
714   :group 'compilation-remote)
715
716 (defcustom mode-compile-remote-execute-command "rsh"
717   "The shell command used to run a command remotely.
718 \"rsh\" is the only choice I know but I'm far to know everything...
719
720  This variable is set automatically with the value of
721 remote-shell-program or efs-remote-shell-file-name at load time."
722   :type 'string
723   :group 'compilation)
724 (eval-when 'load
725   (cond
726    ((not (string= mode-compile-remote-execute-command "rsh"))
727     ;; user changed default
728     nil)
729    ;; Try to not multiply definitions of the same stuff
730    ;; in too many emacs lisp packages ...
731    ((and (boundp 'remote-shell-program) remote-shell-program)
732     (setq mode-compile-remote-execute-command remote-shell-program))
733    ((and (boundp 'efs-remote-shell-file-name) efs-remote-shell-file-name)
734     (setq mode-compile-remote-execute-command efs-remote-shell-file-name))
735    ))
736
737 (defcustom mode-compile-remote-execute-set-host-arg ""
738   "Argument To set the remote host name to the
739 mode-compile-remote-execute-command,
740
741 None is required for \"rsh\"."
742   :type 'string
743   :group 'compilation-remote)
744
745 (defcustom mode-compile-remote-execute-set-command-arg ""
746   "Argument to set the command to be run remotely to the
747 mode-compile-remote-execute-command.
748
749 None is required for \"rsh\"."
750   :type 'string
751   :group 'compilation-remote)
752
753 (defcustom mode-compile-remote-execute-set-username-arg "-l"
754   "Argument to set the username under which we will log on
755 on the remote host, to give to mode-compile-remote-execute-command."
756   :type 'string
757   :group 'compilation-remote)
758
759 (defcustom mode-compile-remote-execute-misc-args ""
760   "Misc additional arguments to give to the
761 mode-compile-remote-execute-command."
762   :type 'string
763   :group 'compilation-remote)
764
765 \f
766
767 ;; @@ c-mode compile variables ;;;
768 (defgroup compile-c nil
769   "C Compilation options."
770   :group 'c
771   :group 'compilation-lang)
772
773 (defcustom cc-compilers-list '( "gcc" "c89" "acc" "cc" )
774   "List of user's favourite C compilers in order of preference."
775   :type '(repeat (string :tag "C Compiler name"))
776   :group 'compile-c)
777
778 (defcustom cc-companion-file-regexp "\\(_[Pp]\\)?\\.[pP]?h"
779   "Regexp to find associated .c file from a .h."
780   :type 'regexp
781   :group 'compile-c)
782
783 (defcustom cc-default-compiler "cc"
784   "*Default C compiler to use when everything else fails.
785
786 This could be any form evaluating to a string, so you could map it to
787 a function asking you interactively to choose the compiler.
788
789 example:
790  (defun my-choose-compiler()
791    (read-string \"C compiler: \"))
792  (setq cc-compilers-list '()
793        cc-default-compiler 'my-choose-compiler)"
794   :type '(choice string function)
795   :group 'compile-c)
796
797 (defcustom cc-compiler-varenv "CC"
798   "Varenv indicating the C compiler to use."
799   :type 'string
800   :group 'compile-c)
801
802 (defcustom cc-cflags-varenv "CFLAGS"
803   "Varenv indicating the C compiler flags to use."
804   :type 'string
805   :group 'compile-c)
806
807 (defcustom cc-source-ext-list '( "c" )
808   "Extensions for C compilable source files."
809   :type '(repeat string)
810   :group 'compile-c)
811
812 (defcustom cc-headers-ext-list '( "h" )
813   "Extensions for C headers source files."
814   :type '(repeat string)
815   :group 'compile-c)
816
817 (defcustom cc-default-compiler-options "-g"
818   "*Default options to give to the C compiler.
819
820 This could be any form evaluating to a string.
821 See `mode-compile-chosen-compiler' variable."
822   :type '(choice
823           string
824           (sexp :tag "Form evaluating to a string"))
825   :group 'compile-c)
826
827 (defcustom cc-source-file-ext-regexp "\\.c"
828   "Regexp to find, from its name, if a C file is compilable."
829   :type 'string
830   :group 'compile-c)
831
832 (defcustom cc-build-output-args t
833   "Build output-args for c-mode."
834   :type 'boolean
835   :group 'compile-c)
836
837 (defcustom cc-object-file-ext "o"
838   "Extension of objects file (result of compilation)
839 in c mode."
840   :type 'string
841   :group 'compile-c)
842
843 \f
844
845 ;; @@ java-mode compile variables ;;;
846 (defgroup compile-java nil
847   "Java compilation options."
848   :group 'compilation-lang)
849
850 (defcustom java-compilers-list '( "javac" )
851   "List of user's favourite java compilers in order of preference."
852   :type '(repeat (string :tag "Java Compiler name"))
853   :group 'compile-java)
854
855 (defcustom java-companion-file-regexp ""
856   "Regexp to find associated compilable Java companion file.
857
858 This is useless in Java because there do not exist uncompilable files."
859   :type 'regexp
860   :group 'compile-java)
861
862 (defcustom java-default-compiler "javac"
863   "*Default C compiler to use when everything else fails.
864
865 This could be any form evaluating to a string, so you could map it to
866 a function asking you interactively to choose the compiler.
867
868 example:
869  (defun my-choose-compiler()
870    (read-string \"Java compiler: \"))
871  (setq java-default-compiler 'my-choose-compiler)."
872   :type '(choice string function)
873   :group 'compile-java)
874
875 (defcustom java-compiler-varenv "JAVAC"
876   "Varenv indicating the C compiler to use."
877   :type 'string
878   :group 'compile-java)
879
880 (defcustom java-cflags-varenv "JAVAC_FLAGS"
881   "Varenv indicating the C compiler flags to use."
882   :type 'string
883   :group 'compile-java)
884
885 (defcustom java-source-ext-list '( "java" )
886   "Extensions for Java compilable source files."
887   :type '(repeat string)
888   :group 'compile-java)
889
890 (defcustom java-headers-ext-list '( "java" )
891   "Extensions for Java source files."
892   :type '(repeat string)
893   :group 'compile-java)
894
895 (defcustom java-default-compiler-options "-O"
896   "*Default options to give to the Java compiler.
897
898 This could be any form evaluating to a string.  See
899 `mode-compile-chosen-compiler' variable."
900   :type '(choice
901           string
902           (sexp :tag "Form evaluating to a string"))
903   :group 'compile-java)
904
905 (defcustom java-source-file-ext-regexp "\\.java"
906   "Regexp to find, from its name, if a Java file is compilable."
907   :type 'regexp
908   :group 'compile-java)
909
910 (defcustom java-build-output-args nil
911   "Don't build output-args for Java-mode."
912   :type 'boolean
913   :group 'compile-java)
914
915 (defcustom java-object-file-ext "class"
916   "Extension of objects file (result of compilation)
917 in java mode."
918   :type 'string
919   :group 'compile-java)
920
921 \f
922
923 ;; @@ c++-mode compile variables ;;;
924 (defgroup compile-c++ nil
925   "C++ compilation options"
926   :group 'compilation-lang)
927
928 (defcustom c++-compilers-list '( "g++" "gcc" "CC" )
929   "List of user's favourite C++ compilers in order of preference."
930   :type '(repeat (string :tag "C++ Compiler name"))
931   :group 'compile-c++)
932
933 (defcustom c++-companion-file-regexp "\\(_[Pp]\\)?\\.\\([pP]?[Hh][Hh]?\\|[Hh]\\+\\+?\\)"
934   "Regexp to find associated compilable C++ companion file
935 from a header file."
936   :type 'regexp
937   :group 'compile-c++)
938
939 (defcustom c++-default-compiler "CC"
940   "*Default C++ compiler to use when everything else fails..
941
942 This could be any form evaluating to a string, so you could map it to
943 a function asking you interactively to choose the compiler.
944
945 example:
946  (defun my-choose-compiler()
947    (read-string \"C++ compiler: \"))
948  (setq c++-default-compiler 'my-choose-compiler)"
949   :type '(choice string function)
950   :group 'compile-c++)
951
952 (defcustom c++-compiler-varenv "CXX"
953   "Varenv indicating the C++ compiler to use."
954   :type 'string
955   :group 'compile-c++)
956
957 (defcustom c++-cflags-varenv "CXXFLAGS"
958   "Varenv indicating the C++ compiler flags to use."
959   :type 'string
960   :group 'compile-c++)
961
962 (defcustom c++-source-ext-list '( "cc" "C" "CC" "cpp" "cxx" "c++" "c+" )
963   "Extensions for C++ compilable source files."
964   :type '(repeat string)
965   :group 'compile-c++)
966
967 (defcustom c++-headers-ext-list '( "H" "hh" "HH" "h++" "h+" "h" "hpp" "hxx" )
968   "Extensions for C++ headers source files."
969   :type '(repeat string)
970   :group 'compile-c++)
971
972 (defcustom c++-default-compiler-options "-g"
973   "*Default options to give to the C++ compiler.
974 This could be any form evaluating to a string.  See
975 `mode-compile-chosen-compiler' variable."
976   :type '(choice
977           string
978           (sexp :tag "Form evaluating to a string"))
979   :group 'compile-c++)
980
981 (defcustom c++-source-file-ext-regexp "\\.\\(cc\\|CC?\\|c\\+\\+?\\|cpp\\|cxx\\)"
982   "Regexp to find, from its name, if a C++ file is compilable."
983   :type 'regexp
984   :group 'compile-c++)
985
986 (defcustom c++-build-output-args t
987   "Build output-args for c++-mode."
988   :type 'boolean
989   :group 'compile-c++)
990
991 (defcustom c++-object-file-ext "o"
992   "Extension of objects file (result of compilation)
993 in c++ mode."
994   :type 'string
995   :group 'compile-c++)
996
997 \f
998
999 ;; @@ ada-mode compile variables ;;;
1000 (defgroup compile-ada nil
1001   "Ada compilation options"
1002   :group 'compilation-lang)
1003
1004 (defcustom ada-compilers-list
1005   '( "gcc" "gnat" "ada" )
1006   "List of user's favourite Ada compilers in order of preference."
1007   :type '(repeat (string :tag "Ada Compiler name"))
1008   :group 'compile-ada)
1009
1010 (defcustom ada-companion-file-regexp ""
1011   "Regexp to find associated compilable Ada companion file from a spec file.
1012
1013 This is useless in Ada because there do not exist uncompilable files."
1014   :type 'regexp
1015   :group 'compile-ada)
1016
1017 (defcustom ada-default-compiler "ada"
1018   "*Default Ada compiler to use when everything else fails.
1019
1020 This could be any form evaluating to a string, so you could map it to
1021 a function asking you interactively to choose the compiler.
1022
1023 example:
1024  (defun my-choose-compiler()
1025    (read-string \"Ada compiler: \"))
1026  (setq ada-default-compiler 'my-choose-compiler)"
1027   :type '(choice string function)
1028   :group 'compile-ada)
1029
1030 (defcustom ada-compiler-varenv "ADA"
1031   "Varenv indicating the Ada compiler to use."
1032   :type 'string
1033   :group 'compile-ada)
1034
1035 (defcustom ada-aflags-varenv "AFLAGS"
1036   "Varenv indicating the Ada compiler flags to use."
1037   :type 'string
1038   :group 'compile-ada)
1039
1040 (defcustom ada-source-ext-list '( "ads" "adb" "ada" "a" )
1041   "Extensions for Ada compilable source files."
1042   :type '(repeat string)
1043   :group 'compile-ada)
1044
1045 (defcustom ada-headers-ext-list '( "ads" "ada" "a" )
1046   "Extensions for Ada spec source files."
1047   :type '(repeat string)
1048   :group 'compile-ada)
1049
1050 (defcustom ada-default-compiler-options "-g"
1051   "*Default options to give to the Ada compiler.
1052
1053 This could be any form evaluating to a string.  See
1054 `mode-compile-chosen-compiler' variable."
1055   :type '(choice
1056           string
1057           (sexp :tag "Form evaluating to a string"))
1058   :group 'compile-ada)
1059
1060 (defcustom ada-source-file-ext-regexp "\\.\\(ad[abs]\\|a\\)"
1061   "Regexp to find, from its name, if an Ada file is compilable.
1062
1063 This is useless in Ada because there do not exist uncompilable files."
1064   :type 'regexp
1065   :group 'compile-ada)
1066
1067 (defcustom ada-build-output-args t
1068   "Build output-args for ada-mode."
1069   :type 'boolean
1070   :group 'compile-ada)
1071
1072 (defcustom ada-object-file-ext "o"
1073   "Extension of objects file (result of compilation)
1074 in ada mode."
1075   :type 'string
1076   :group 'compile-ada)
1077
1078 \f
1079
1080 ;; @@ fortran-mode compile variables ;;;
1081 (defgroup compile-fortran nil
1082   "Fortran compilation options"
1083   :group 'compilation-lang)
1084
1085 (defcustom f77-compilers-list '( "f77" "fc" )
1086   "List of user's favourite Fortran compilers in order of preference."
1087   :type '(repeat (string :tag "C Compiler name"))
1088   :group 'compile-fortran)
1089
1090 (defcustom f77-companion-file-regexp "\\(_[Pp]\\)?\\.[pP]?inc"
1091   "Regexp to find associated .f file from a .inc."
1092   :type 'regexp
1093   :group 'compile-fortran)
1094
1095 (defcustom f77-default-compiler "f77"
1096   "*Default fortran compiler to use when everything else fails..
1097
1098 This could be any form evaluating to a string, so you could map it to
1099 a function asking you interactively to choose the compiler.
1100
1101 example:
1102  (defun my-choose-compiler()
1103    (read-string \"Fortran compiler: \"))
1104  (setq f77-default-compiler 'my-choose-compiler)"
1105   :type '(choice string function)
1106   :group 'compile-fortran)
1107
1108 (defcustom f77-compiler-varenv "F77"
1109   "Varenv indicating the fortran compiler to use."
1110   :type 'string
1111   :group 'compile-fortran)
1112
1113 (defcustom f77-cflags-varenv "FCOPTS"
1114   "Varenv indicating the fortran compiler flags to use."
1115   :type 'string
1116   :group 'compile-fortran)
1117
1118 (defcustom f77-source-ext-list '( "f" "F" "for" "For" )
1119   "Extensions for fortran compilable source files."
1120   :type '(repeat string)
1121   :group 'compile-fortran)
1122
1123 (defcustom f77-headers-ext-list '( "inc" "h")
1124   "Extensions for fortran include files."
1125   :type '(repeat string)
1126   :group 'compile-fortran)
1127
1128 (defcustom f77-default-compiler-options "-w66 -a"
1129   "*Default options to give to the fortran compiler.
1130
1131 This could be any form evaluating to a string.  See
1132 `mode-compile-chosen-compiler' variable."
1133   :type '(choice
1134           string
1135           (sexp :tag "Form evaluating to a string"))
1136   :group 'compile-fortran)
1137
1138 (defcustom f77-source-file-ext-regexp "\\.\\([Ff]\\|for\\)"
1139   "Regexp to find, from its name, if a fortran file is compilable."
1140   :type 'regexp
1141   :group 'compile-fortran)
1142
1143 (defcustom f77-build-output-args t
1144   "Build output-args for f77-mode."
1145   :type 'boolean
1146   :group 'compile-fortran)
1147
1148 (defcustom f77-object-file-ext "o"
1149   "Extension of objects file (result of compilation)
1150 in Fortran mode."
1151   :type 'string
1152   :group 'compile-fortran)
1153
1154 \f
1155
1156 ;; @@ sh-mode compile variables ;;;
1157 (defgroup compile-sh nil
1158   "Sh (Bourne Shell scripts) compilation options"
1159   :group 'compilation-script)
1160
1161 (defcustom sh-command "sh"
1162   "Command to run sh scripts"
1163   :type 'string
1164   :group 'compile-sh)
1165
1166 (defcustom sh-dbg-flags "-fvx"
1167   "*Flags to give to sh for debugging a Bourne Shell script.
1168
1169 The -f flag must always be present."
1170   :type 'string
1171   :group 'compile-sh)
1172
1173 (defvar sh-compilation-error-regexp-alist nil
1174   ;; I'd never seen a Bourne shell returning file+line where a syntax
1175   ;; error occurred.
1176   "Alist that specifies how to match errors in sh output.
1177
1178 See variable compilation-error-regexp-alist for more details.")
1179 \f
1180
1181 ;; @@ csh-mode compile variables ;;;
1182 (defgroup compile-csh nil
1183   "Csh (C Shell) compilation options"
1184   :group 'compilation-script)
1185
1186 (defcustom csh-command "csh"
1187   "Command to run csh scripts"
1188   :type 'string
1189   :group 'compile-csh)
1190
1191 (defcustom csh-dbg-flags "-fVX"
1192   "*Flags to give to csh for debugging a C Shell script.
1193
1194 The -f flag must always be present."
1195   :type 'string
1196   :group 'compile-csh)
1197
1198 (defvar csh-compilation-error-regexp-alist nil
1199   ;; I'd never seen a C shell returning file+line where a syntax
1200   ;; error occurred.
1201   "Alist that specifies how to match errors in csh output.
1202
1203 See variable compilation-error-regexp-alist for more details.")
1204 \f
1205
1206 ;; @@ zsh-mode compile variables ;;;
1207 (defgroup compile-zsh nil
1208   "Zsh (Z Shell scripts) compilation options"
1209   :group 'compilation-script)
1210
1211 (defcustom zsh-command "zsh"
1212   "Command to run zsh scripts"
1213   :type 'string
1214   :group 'compile-zsh)
1215
1216 (defcustom zsh-dbg-flags "-nCvx"
1217   "*Flags to give to zsh for debugging a Z Shell script."
1218   :type 'string
1219   :group 'compile-zsh)
1220
1221 (defvar zsh-compilation-error-regexp-alist nil
1222   ;; I'd never seen a Z shell returning file+line where a syntax
1223   ;; error occurred.
1224   "Alist that specifies how to match errors in csh output.
1225
1226 See variable compilation-error-regexp-alist for more details.")
1227 \f
1228
1229 ;; @@ tcl-mode compile variables - JWH ;;;
1230 (defgroup compile-tcl nil
1231   "Tcl compilation options"
1232   :group 'compilation-script)
1233
1234 (defcustom tcl-command "wish"
1235   "Command to run tcl scripts"
1236   :type 'string
1237   :group 'compile-tcl)
1238
1239 (defcustom tcl-dbg-flags ""
1240   "*Flags to give to tcl -- none."
1241   :type 'string
1242   :group 'compile-tcl)
1243
1244 (defvar tcl-compilation-error-regexp-alist
1245   ;; TK  (file "/directory-path/filename.tcl" line XY)
1246   '(
1247     ("file \"\\([^ ]+\\)\" line \\([0-9]+\\)[)]" 1 2)
1248     )
1249   "Alist that specifies how to match errors in tcl output.
1250
1251 See variable compilation-error-regexp-alist for more details.")
1252 \f
1253
1254 ;; @@ python-mode compile variables - BM ;;;
1255 (defgroup compile-python nil
1256   "Python compilation options"
1257   :group 'compilation-script)
1258
1259 (defcustom python-command "python"
1260   "Command to run python scripts"
1261   :type 'string
1262   :group 'compile-python)
1263
1264 (defcustom python-dbg-flags ""
1265   "*Flags to give to python -- none."
1266   :type 'string
1267   :group 'compile-python)
1268
1269 (defvar python-compilation-error-regexp-alist
1270   ;; TK  (file "/directory-path/filename.tcl" line XY in ZZZ)
1271   '(
1272     ("File \"\\([^ ]+\\)\", line \\([0-9]+\\).*" 1 2)
1273     )
1274   "Alist that specifies how to match errors in python output.
1275
1276 See variable compilation-error-regexp-alist for more details.")
1277 \f
1278
1279 ;; @@ perl-mode compile variables ;;;
1280 (defgroup compile-perl nil
1281   "Perl compilation options"
1282   :group 'compilation-script)
1283
1284 (defcustom perl-command "perl"
1285   "Command to run perl."
1286   :type 'string
1287   :group 'compile-perl)
1288
1289 (defcustom perl-dbg-flags "-w"
1290   "*Flags to give to perl for debugging a Perl script."
1291   :type 'string
1292   :group 'compile-perl)
1293
1294 (defvar perl-compilation-error-regexp-alist
1295   ;; Contributed by Martin Jost
1296   '(
1297     ;; PERL 4
1298     ("in file \\([^ ]+\\) at line \\([0-9]+\\).*" 1 2)
1299     ;; PERL 5   Blubber at FILE line XY, <XY> line ab.
1300     ("at \\([^ ]+\\) line \\([0-9]+\\)," 1 2)
1301     ;; PERL 5   Blubber at FILE line XY.
1302     ("at \\([^ ]+\\) line \\([0-9]+\\)." 1 2)
1303     )
1304   ;; This look like a paranoiac regexp: could anybody find a better one? (which WORK).
1305   ;;'(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\.,]" 2 3))
1306   "Alist that specifies how to match errors in perl output.
1307
1308 See variable compilation-error-regexp-alist for more details.")
1309 \f
1310
1311 ;; @@ emacs lisp compile variables ;;;
1312
1313 ;; XEmacs change: backwards compatibility.
1314 ;; APA: Moved this before the associated defcustom.
1315 (define-obsolete-variable-alias
1316   'mode-compile-byte-compile-dir-interactive-p
1317   'emacs-lisp-byte-compile-dir-interactive-p)
1318
1319 ;;;###autoload
1320 (defcustom emacs-lisp-byte-compile-dir-interactive-p t
1321   "*Non-nil means when byte-compiling a directory ask for each file
1322 needing to be recompiled or not."
1323   :type 'boolean
1324   :group 'compilation-elisp)
1325
1326 (defcustom emacs-lisp-sources-regexp
1327   (cond
1328    ((boundp 'emacs-lisp-file-regexp)
1329     emacs-lisp-file-regexp)
1330    (t
1331     "\\.el$"))
1332   "Regexp to find emacs lisp sources files."
1333   :type 'regexp
1334   :group 'compilation-elisp)
1335
1336 (defcustom emacs-lisp-bytecomp-ext "c"
1337   "Extension added to byte-compiled emacs sources files."
1338   :type 'string
1339   :group 'compilation-elisp)
1340 \f
1341
1342 ;; @@ Misc declarations ;;;
1343
1344 ;;;###autoload
1345 (defconst mode-compile-version "2.28"
1346   "Current version of mode-compile package.
1347
1348 mode-compile.el,v 2.28 2003/04/01 13:52:47 boubaker Exp
1349 Please send bugs-fixes/contributions/comments to boubaker@cena.fr")
1350
1351 (defconst mode-compile-help-address "heddy.Boubaker@cena.fr"
1352   "E-Mail address of mode-compile maintainer.")
1353 \f
1354
1355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1356 ;; @ No user modifiable stuff below this line ;;;
1357 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1358
1359 ;; Save old compile function. In case someone will bound
1360 ;; mode-compile on 'compile.
1361 (or (fboundp 'mc--compile-sav)
1362     (if (fboundp 'compile)
1363         (progn
1364           (fset 'mc--compile-sav (symbol-function 'compile))
1365           (put 'compile 'compile-saved-on-mc--compile-sav t))
1366       (error "`compile' function not known to be defined...")))
1367
1368 ;; @@ Internals variables and constants ;;;
1369
1370 ;; Mode specific
1371 (defvar mc--comp-lst          nil) ; c-mode,c++-mode,ada-mode,fortran-mode
1372 (defvar mc--def-comp          nil) ; itou
1373 (defvar mc--compfile-regexp   nil) ; itou
1374 (defvar mc--comp-varenv       nil) ; itou
1375 (defvar mc--comp-options      nil) ; itou
1376 (defvar mc--cflags-varenv     nil) ; itou
1377 (defvar mc--source-ext-lst    nil) ; itou
1378 (defvar mc--head-ext-lst      nil) ; itou
1379 (defvar mc--source-ext-regexp nil) ; itou
1380 (defvar mc--build-op-args     nil) ; itou
1381 (defvar mc--outfile-ext       nil) ; itou
1382
1383 ;; remote stuff
1384 (defvar mc--efs-path-list       nil)
1385 (defvar mc--remote-host         nil)
1386 (defvar mc--remote-host-history nil)
1387 (defvar mc--remote-username     nil)
1388 (defvar mc--remote-command      nil)
1389 (defvar mc--remote-pathname     nil)
1390
1391 ;; Frames/Windows stuff
1392 (defvar mc--other-frame nil)
1393 (defvar mc--ws (or
1394                 (and (fboundp 'console-type) (console-type))
1395                 (and (fboundp 'device-type)  (device-type))
1396                 window-system))
1397
1398 (defvar mc--compile-command nil)
1399 ;; Compile command used when no makefile has been found.
1400 ;; This variable is buffer local to keep history for read-string.
1401 ;; Unfortunately not a real history, keep only memory of
1402 ;; the last compile command used.
1403 (make-variable-buffer-local 'mc--compile-command)
1404
1405 (defvar mc--kill-compile nil)
1406 ;; kill-compile command bound dynamically by `guess-compile'.
1407 (make-variable-buffer-local 'mc--kill-compile)
1408
1409 (defvar mc--selected-makefile nil)
1410 (defvar mc--selected-makefile-history nil)
1411 ;; User selected makefile among the list, to run make with.
1412 ;; This variable is buffer local to keep history for completing-read
1413 ;; Unfortunately not a real history, keep only memory of
1414 ;; the last makefile used.
1415 (make-variable-buffer-local 'mc--selected-makefile)
1416
1417 (defvar mc--selected-makerule nil)
1418 (defvar mc--selected-makerule-history nil)
1419 ;; User selected make rule to rebuild.
1420 ;; This variable is buffer local to keep history for completing-read
1421 ;; Unfortunately not a real history, keep only memory of
1422 ;; the last makerule used.
1423 (make-variable-buffer-local 'mc--selected-makerule)
1424
1425 (defconst mc--find-C-main-regexp
1426   "^[ \t]*\\(int\\|void\\)?[ \t\n]*main[ \t\n]*\(+" )
1427 ;; Regexp to find the main() function in a C/C++ file
1428
1429 (defconst mc--makefile-rules-regexp
1430   "^\n*\\([^.$ \t#\n][^$ \t#\n:]*\\)[ \t]*:")
1431 ;; Regexp to extract makefiles rules.
1432 ;; But only those not containing references to $(VARIABLES)
1433 ;; and not starting with `.'
1434
1435 (defvar mc--makefile-rules nil)
1436 ;; List of all rules extracted from makefile
1437 (make-variable-buffer-local 'mc--makefile-rules)
1438
1439 (defvar mc--mkfl-buffer-tick nil)
1440 ;; Tick counter for the buffer at the time of the rules extraction.
1441 (make-variable-buffer-local 'mc--mkfl-buffer-tick)
1442
1443 (defvar mc--shell-args nil)
1444 ;; Shell arguments for the script to debug.
1445 ;; This variable is buffer local to keep history for read-string.
1446 ;; Unfortunately not a real history, keep only memory of
1447 ;; the last shell arguments used.
1448 (make-variable-buffer-local 'mc--shell-args)
1449
1450 ;; nil in GNU FSF Emacs, >= 0 in GNU Lucid Emacs/XEmacs
1451 (defconst mc--lucid-emacs-p (or (string-match "Lucid"  emacs-version)
1452                                 (string-match "XEmacs" emacs-version)))
1453
1454 ;; @@ Internals functions and macros ;;;
1455
1456 (if (not (fboundp 'defsubst))
1457     ;; Emacs 18
1458     (fset 'defsubst (symbol-function 'defun)))
1459
1460 (defun mc--compile (compile-command)
1461   ;; Call compile with the compile command
1462   ;; but append the remote-command before
1463   (if (null mc--remote-command)
1464       ;; local compile
1465       (mc--compile-sav compile-command)
1466     ;; remote compile
1467     (let ((thisdir (expand-file-name (or default-directory "~"))))
1468       (mc--compile-sav
1469        (concat
1470         ;; The command to launch remote commands
1471         mc--remote-command
1472         ;; Change to this buffer directory ...
1473         "'( cd " thisdir " ; "
1474         ;; then run the compile command
1475         compile-command " )'")))))
1476
1477 (defsubst mc--msg (msg &rest args)
1478   ;; Print MSG with ARGS and wait to let time to user
1479   ;; to read the message in minibuffer.
1480   (cond ((not mode-compile-expert-p)
1481          (apply 'message (concat "mode-compile: " msg) args)
1482          (sit-for mode-compile-reading-time))))
1483
1484 (cond
1485  ;; Check available frames functions
1486  ((fboundp 'make-frame)
1487   ;; GNU Emacs
1488   (fset 'mc--make-frame         (symbol-function 'make-frame))
1489   (fset 'mc--select-frame       (symbol-function 'select-frame))
1490   (fset 'mc--frame-live-p       (symbol-function 'frame-live-p))
1491   (fset 'mc--make-frame-visible (symbol-function 'make-frame-visible))
1492   (fset 'mc--raise-frame        (symbol-function 'raise-frame)))
1493  ((fboundp 'make-screen)
1494   ;; XEmacs
1495   (fset 'mc--make-frame         (symbol-function 'make-screen))
1496   (fset 'mc--select-frame       (symbol-function 'select-screen))
1497   (fset 'mc--frame-live-p       (symbol-function 'screen-live-p))
1498   (fset 'mc--make-frame-visible (symbol-function 'make-screen-visible))
1499   (fset 'mc--raise-frame        (symbol-function 'raise-screen)))
1500  ((fboundp 'new-screen)
1501   ;; Lucid Emacs/obsolete
1502   (fset 'mc--make-frame         (symbol-function 'new-screen))
1503   (fset 'mc--select-frame       (symbol-function 'select-screen))
1504   (fset 'mc--frame-live-p       (symbol-function 'screen-live-p))
1505   (fset 'mc--make-frame-visible (symbol-function 'make-screen-visible))
1506   (fset 'mc--raise-frame        (symbol-function 'raise-screen))))
1507
1508 (defsubst mc--funcall (command &rest params)
1509   ;; Run command with params in another frame or not:
1510   ;; only if user ask for it and if window system is X
1511   ;; (maybe test window-system is set will be enough?).
1512   (cond ((and (eq mc--ws 'x)
1513               mode-compile-other-frame-p)
1514          ;; switch to another frame
1515          (mc--msg "Switching to another frame to compile...")
1516          (let ((buffer   (current-buffer))
1517                (win-attr (or mode-compile-frame-parameters-alist
1518                              mode-compile-default-frame-parameters))
1519                (frame   (cond ((fboundp 'mc--frame-live-p)
1520                                 (if (mc--frame-live-p mc--other-frame)
1521                                     mc--other-frame
1522                                   nil))
1523                                (t
1524                                 (mc--msg "Don't know how to check frame existence.")
1525                                 nil))))
1526            (cond ((fboundp 'mc--make-frame)
1527                   (mc--select-frame (or frame
1528                                          (setq mc--other-frame
1529                                                (mc--make-frame win-attr))))
1530                   ;; I really don't understand why the 3 following
1531                   ;; are necessary (raise-frame must be enough?).
1532                   (mc--make-frame-visible mc--other-frame)
1533                   (mc--raise-frame        mc--other-frame)
1534                   (switch-to-buffer        buffer))
1535                  (t
1536                   (mc--msg "Don't know how to create a new frame."))))))
1537   ;; Just run the command with its parameters
1538   (apply command params))
1539
1540 (defun mc--byte-compile-buffer()
1541   (if (fboundp 'byte-compile-buffer) (byte-compile-buffer)
1542     ;; No byte-compile-buffer
1543     ;; Save current-buffer in a temporary file and byte-compile it.
1544     (let ((tmp-file (concat (or (getenv "TMPDIR")
1545                                 (concat mode-compile-dir-separator-char "tmp"))
1546                             mode-compile-dir-separator-char (make-temp-name "mc--"))))
1547       (save-restriction
1548         (widen)
1549         (write-region (point-min) (point-max) tmp-file)
1550         (condition-case err
1551             (byte-compile-file tmp-file)
1552           ;; handler
1553           (error (mc--msg "Failing to byte-compile %s, #error %s"
1554                           (buffer-name) err)))
1555         (delete-file tmp-file)
1556         (let ((elc-file (concat tmp-file emacs-lisp-bytecomp-ext)))
1557           (if (file-writable-p elc-file)
1558               (condition-case err
1559                   (delete-file elc-file)
1560                 ;; handler
1561                 (error (mc--msg "Failing to delete %s, #error %s"
1562                                 elc-file err)))))
1563         (message nil))))) ; to clean minibuffer
1564
1565 (fset 'mc--member
1566       (if (fboundp 'member)
1567           (symbol-function 'member)
1568         ;; No member function
1569         (function
1570          (lambda (elt list)
1571            (catch 'elt-is-member
1572              (while list
1573                (if (equal elt (car list))
1574                    (throw 'elt-is-member list))
1575                (setq list (cdr list))))))))
1576
1577 (fset 'mc--run-hooks
1578       (if (fboundp 'run-hooks)
1579           (symbol-function 'run-hooks)
1580         ;; No run-hooks
1581         (function
1582          (lambda (hooklist)
1583            (mapcar '(lambda (x)
1584                       ;; report an error if x not a function
1585                       (funcall x))
1586                    hooklist)))))
1587
1588 (defsubst mc--read-string (prompt &optional initial-contents)
1589   ;; On Lucid Emacs I use compile-history as 3rd argument but
1590   ;; no history is possible with GNU emacs.
1591   (if mc--lucid-emacs-p
1592       (read-string prompt initial-contents 'compile-history)
1593     (read-string prompt initial-contents)))
1594
1595 (defmacro mc--eval (sym &optional arg)
1596   ;; Evaluate symbol
1597   (` (cond
1598       ((and (symbolp (, sym))
1599             (fboundp (, sym)))
1600        (funcall (, sym) (, arg)))
1601       (t
1602        (eval (, sym))))))
1603
1604 (defmacro mc--common-completion (alist)
1605   ;; Return the greatest common string for all
1606   ;; possible completions in alist.
1607   (` (try-completion "" (, alist))))
1608
1609 (defun mc--byte-recompile-files (files)
1610   ;; Byte recompile all FILES which are older than their
1611   ;; .elc files in the current directory
1612   (let ((tmp-fl files))
1613     (while (car-safe tmp-fl)
1614       (let* ((el-file  (car tmp-fl))
1615              (elc-file (concat el-file emacs-lisp-bytecomp-ext)))
1616         (mc--msg "Checking file %s ..." el-file)
1617         ;; is el-file newer than elc-file (if exists)
1618         (if (and (file-newer-than-file-p el-file elc-file)
1619                  (or (not emacs-lisp-byte-compile-dir-interactive-p)
1620                      (y-or-n-p (format "byte-recompile file %s? " el-file))))
1621             (condition-case err
1622                 (byte-compile-file el-file)
1623               ;; handler
1624               (error (mc--msg "Failing to byte-compile %s, #error %s"
1625                               el-file err))))
1626         (setq tmp-fl (cdr-safe tmp-fl))))
1627     (mc--msg "All files processed")))
1628
1629 (defun mc--which (file)
1630   ;; Find an executable FILE in exec-path
1631   (if (not (stringp file))
1632       (error "mc--which: nil FILE arg"))
1633   (if mc--lucid-emacs-p
1634       ;; Some Emacsen don't have locate-file some have...
1635       ;; Lucid have it in standard, some others (GNU) have it
1636       ;; (add-on pkg) but the syntax is not always consistent...
1637 ;      (locate-file file exec-path nil 1)
1638       (locate-file file exec-path mode-compile-exe-file-ext 1)
1639     (let ((tmp-p-lst  exec-path)
1640           (found      nil)
1641           (file-found nil))
1642       (while (and (car-safe tmp-p-lst)
1643                   (not (setq found
1644                              (file-executable-p
1645                               (setq file-found
1646                                     (concat (car tmp-p-lst)
1647                                             mode-compile-dir-separator-char
1648                                             file
1649                                             mode-compile-exe-file-ext))))))
1650         (setq tmp-p-lst (cdr-safe tmp-p-lst)))
1651       (if found file-found nil))))
1652
1653 (defun mc--find-compiler ()
1654   ;; Find user's favourite mode compiler
1655   (mc--msg "Searching for your favourite %s compiler ..." mode-name)
1656   (let ((tmp-comp-lst mc--comp-lst)
1657         (compiler     nil))
1658     (or (getenv mc--comp-varenv)
1659         (progn
1660           (while (and tmp-comp-lst
1661                       (not (setq compiler
1662                                  (mc--which (car tmp-comp-lst)))))
1663             (setq tmp-comp-lst (cdr tmp-comp-lst)))
1664           (file-name-nondirectory (or compiler (mc--eval mc--def-comp)))))))
1665
1666 (defun mc--find-to-compile-file (&optional fname)
1667   ;; Find the name of the file to compile.
1668   (let ((file-name (or fname
1669                        (buffer-file-name)
1670                        (error "Compilation abort: Buffer %s has no filename."
1671                               (buffer-name))))
1672         (assoc-file nil)
1673         (found      nil)
1674         (pos        0))
1675     (cond
1676      ((string-match mc--source-ext-regexp file-name)
1677       ;; buffer is a compilable file
1678       (file-name-nondirectory file-name))
1679
1680      ((setq pos (string-match mc--compfile-regexp file-name))
1681       ;; Buffer is not a compilable file, try to find associated
1682       ;; compilable file.
1683       (let ((tmp-ext-lst mc--source-ext-lst))
1684         (mc--msg "Looking for a compilable companion file for %s..."
1685                  (file-name-nondirectory file-name))
1686         (while (and tmp-ext-lst
1687                     (not (setq found
1688                                (file-readable-p
1689                                 (setq assoc-file
1690                                       (concat
1691                                        (substring file-name 0 pos)
1692                                        "." (car tmp-ext-lst)))))))
1693           (setq tmp-ext-lst (cdr tmp-ext-lst))))
1694       (if found
1695           ;; A compilable companion source file found
1696           (file-name-nondirectory assoc-file)
1697         ;; No compilable companion source file found
1698         (mc--msg "Couldn't find any compilable companion file for %s ..."
1699                  (file-name-nondirectory file-name))
1700         nil))
1701
1702      (t
1703       ;; Buffer has an unknown file extension
1704       ;; Could I be more cool?
1705       (error "Compilation abort: Don't know how to compile %s."
1706              (file-name-nondirectory file-name))))))
1707
1708 (defun mc--guess-compile-result-fname (infile)
1709   ;; Try to guess if outfile will be an object file or
1710   ;; an executable file by grepping for `main()' in INFILE.
1711   (let ((base-fname
1712          (substring infile 0
1713                     (string-match mc--source-ext-regexp infile))))
1714     (save-excursion
1715       ;; Create a temporary buffer containing infile contents
1716       (set-buffer (find-file-noselect infile))
1717       (save-excursion
1718         (save-restriction
1719           (widen)
1720           (goto-char (point-min))
1721           ;; Grep into tmp buffer for main function
1722           ;; THIS WILL NOT WORK FOR PROGRAMMING LANGUAGES
1723           ;; WHICH ARE NOT C DIALECTS:
1724           ;; In non C-ish modes I hope this regexp will never be found :-(
1725           (if (re-search-forward mc--find-C-main-regexp (point-max) t)
1726               (concat base-fname mode-compile-exe-file-ext)
1727             (concat base-fname "." mc--outfile-ext)))))))
1728
1729 (defun mc--build-output-args (infile)
1730   ;; Build output arguments for compile command by scanning INFILE.
1731   (mc--msg "Looking into %s to build compile command ..." infile)
1732   (let ((out-file (mc--guess-compile-result-fname infile)))
1733     (concat (if (string-match
1734                  (concat "\\." mc--outfile-ext "$")
1735                  out-file)
1736                 ;; outfile will be an object file
1737                 " -c "
1738               ;; outfile will be an executable file
1739               " ")
1740             "\"" infile "\" -o \"" out-file "\"")))
1741
1742 (defun mc--set-remote-cmd (remote-host &optional username pathname)
1743   ;; Check validity of remote-host or ask one to user
1744   ;; Then build the first part of the remote command
1745   (if (stringp remote-host)
1746       ;; Arg is the remote host name
1747       (let ((host-infos (assoc remote-host
1748                                mode-compile-remote-hosts-alist)))
1749         (setq mc--remote-host
1750               remote-host)
1751         (setq mc--remote-username
1752               (or username
1753                   (let ((usrnam (cdr host-infos)))
1754                     (if usrnam
1755                         (cond
1756                          ((stringp usrnam)
1757                           usrnam)
1758                          ((functionp usrnam)
1759                           ;; usrnam is a function call it with hostname arg
1760                           (funcall usrnam mc--remote-host))
1761                          (t
1762                           ;; What's that??
1763                           (mc--msg "%s is not a valid option using user-login-name" (pp-to-string usrnam))
1764                           (user-login-name)))
1765                       (user-login-name)))))
1766         (setq mc--remote-pathname pathname)
1767         ;; Add host to the user's list
1768         (or host-infos
1769             (setq mode-compile-remote-hosts-alist
1770                   (append (list (list remote-host))
1771                           mode-compile-remote-hosts-alist)))
1772         ;; Prepare the command
1773         (setq mc--remote-command
1774               (concat
1775                ;; "rsh host -l username"
1776                mode-compile-remote-execute-command           " "
1777                mode-compile-remote-execute-set-host-arg      " "
1778                mc--remote-host                               " "
1779                mode-compile-remote-execute-set-username-arg  " "
1780                mc--remote-username                           " "
1781                mode-compile-remote-execute-misc-args         " "
1782                mode-compile-remote-execute-set-command-arg   " "
1783                 )))
1784     ;; Arg is: Ask to user then check
1785     (let ((rhost (completing-read
1786                   "Remote host to compile to: "
1787                   mode-compile-remote-hosts-alist
1788                   nil nil
1789                   ;; Initial contents
1790                   (or mc--remote-host
1791                       (car-safe (car-safe mode-compile-remote-hosts-alist)))
1792                   mc--remote-host-history)))
1793       (or (string= rhost "")
1794           (mc--set-remote-cmd rhost)))))
1795
1796 ;(defmacro mc--makefile-test-p (makefile)
1797 ;  (` (and (, makefile)
1798 ;          (not (string-equal     (, makefile) ""))
1799 ;          (not (file-directory-p (, makefile)))
1800 ;          (file-readable-p       (, makefile)))))
1801 (defun mc--makefile-test-p (makefile)
1802   (cond
1803    ((or (not makefile)
1804         (string-equal makefile ""))
1805     (mc--msg "Empty makefile selection")
1806     nil)
1807    ((file-directory-p makefile)
1808     (mc--msg "Makefile selection %s is a directory !!" makefile)
1809     nil)
1810    ((not (file-readable-p makefile))
1811     (mc--msg "Makefile %s unreadable" makefile)
1812     nil)
1813    (t)))
1814
1815
1816 (if (not (fboundp 'buffer-modified-tick))
1817     (fset 'buffer-modified-tick
1818           ;; always indicate modified
1819           (function
1820            (lambda()
1821              (if mc--mkfl-buffer-tick
1822                  (+ mc--mkfl-buffer-tick 1)
1823                1)))))
1824
1825 (defun  mc--get-makefile-rules (makefile)
1826   ;; Try to find if makefile's buffer has been modified
1827   ;; since last rule extraction
1828   (if (or (not mc--mkfl-buffer-tick)
1829           (not (equal mc--mkfl-buffer-tick
1830                       (buffer-modified-tick))))
1831       (save-excursion
1832         (save-restriction
1833           (widen)
1834           (goto-char (point-min))
1835           (setq mc--mkfl-buffer-tick (buffer-modified-tick))
1836           (setq mc--makefile-rules   nil)
1837           (mc--msg "Extracting rules from %s ..." makefile)
1838           ;; Grep into tmp buffer for makefile rules
1839           (while (re-search-forward mc--makefile-rules-regexp nil t)
1840             (let ((rule (buffer-substring
1841                          (match-beginning 1)
1842                          (match-end       1))))
1843               ;; add rule to list if it don't match the ignore regexp
1844               ;; and if not already in rules list.
1845               (if (and
1846                    (or (not mode-compile-ignore-makerule-regexp)
1847                        (not (string-match
1848                              mode-compile-ignore-makerule-regexp
1849                              rule)))
1850                    (not (mc--member rule mc--makefile-rules)))
1851                   (setq mc--makefile-rules
1852                         (append mc--makefile-rules
1853                                 (list rule))))))))
1854     (mc--msg "Rules had already been extracted from %s ..." makefile))
1855   ;; Always add an empty rule to allow `default' choice.
1856   (append mc--makefile-rules '([])))
1857
1858 (defun mc--makerule-completion (alist outfile &optional pref)
1859   ;; Return the makerule completion according to the preferred
1860   ;; default makerule
1861   (let ((preference (or pref
1862                         mode-compile-preferred-default-makerule)))
1863     (mc--msg "Preferred makerule choice is '%s" preference)
1864     (cond
1865      ((eq preference 'none)
1866       ;; Just show max common completion string to user
1867       (or (mc--common-completion alist) ""))
1868
1869      ((eq preference 'all)
1870       ;; Find the all rule or 'none
1871       (if (assoc "all" alist) "all"
1872         (mc--makerule-completion alist outfile 'none)))
1873
1874      ((eq preference 'file)
1875       ;; The out file is preferred or 'none
1876       (or outfile (mc--makerule-completion alist outfile 'none)))
1877
1878      ((eq preference 'default)
1879       ;; Find the default rule or ""
1880       (if (assoc "default" alist) "default" ""))
1881
1882      (t
1883       ;; Invalid preference return 'none
1884       (mc--msg "Invalid `mode-compile-preferred-default-makerule': '%s"
1885                mode-compile-preferred-default-makerule)
1886       (mc--makerule-completion alist outfile 'none)))))
1887
1888 (defun mc--choose-makefile-rule (makefile &optional outfile)
1889   ;; Choose the makefile rule and set it makefile local
1890   (save-excursion
1891     ;; Switch to makefile buffer
1892     (set-buffer (find-file-noselect makefile))
1893     (setq mc--selected-makerule
1894           ;; Add the name of the out file to the makefile
1895           ;; rules list if not already in.
1896           (let* ((mk-rules-alist (mc--get-makefile-rules makefile))
1897                  (choices        (mapcar '(lambda (x) (list x))
1898                                          (if (or (not outfile)
1899                                                  (mc--member outfile
1900                                                              mk-rules-alist))
1901                                              mk-rules-alist
1902                                            (append mk-rules-alist
1903                                                    (list outfile))))))
1904             (completing-read
1905              (if mode-compile-expert-p
1906                  "Make rule: "
1907                "Using `make', enter rule to rebuild ([TAB] to complete): ")
1908              choices
1909              nil nil
1910              ;; initial contents
1911              (or mc--selected-makerule
1912                  (mc--makerule-completion choices outfile
1913                                           (if outfile 'file)))
1914              mc--selected-makerule-history
1915              )))))
1916
1917 (defmacro mc--cleanup-makefile-list (makefile-list)
1918   ;; Remove unusable and/or backups makefiles from list
1919   (` (let ((newlist))
1920        (mapcar
1921         '(lambda (x)
1922            (if (and (mc--makefile-test-p x)
1923                     (or (not mode-compile-ignore-makefile-backups)
1924                         (not (string-match
1925                               mode-compile-makefile-backups-regexp
1926                               x))))
1927                (setq newlist (cons x newlist))
1928              (mc--msg "Removing makefile \"%s\" from completion list"
1929                       x)))
1930         (, makefile-list))
1931        newlist)))
1932
1933 (defun mc--makefile-to-use (&optional directory)
1934   ;; Find the makefile to use in the current directory
1935   (let ((makefile-list (mc--cleanup-makefile-list
1936                         (directory-files
1937                          ;; I do not use the 5th parameter in Lucid Emacs
1938                          ;; to be compatible with GNU Emacs which accept
1939                          ;; only 4 parameters - no NOSORT -.
1940                          (or directory default-directory)
1941                          nil mode-compile-makefile-regexp t))))
1942     (cond
1943      ((not makefile-list)
1944       ;; No makefile found
1945       nil)
1946
1947      ((and (not (cdr-safe makefile-list))
1948            (mc--makefile-test-p (car makefile-list)))
1949       ;; Only one valid makefile
1950       (car makefile-list))
1951
1952      (t
1953       ;; Many makefiles in directory ask user to select one
1954       (let ((choices  (mapcar
1955                        '(lambda (x) (list x))
1956                        makefile-list))
1957             (makefile nil))
1958         (while
1959             ;; While the makefile do not pass the test.
1960             (not (mc--makefile-test-p
1961                   (setq makefile
1962                         (completing-read
1963                          (if mode-compile-expert-p
1964                              "Makefile: "
1965                            "Using `make', select makefile to use ([TAB] to complete): ")
1966                          choices
1967                          nil t
1968                          ;; initial contents
1969                          (or mc--selected-makefile
1970                              (mc--common-completion choices))
1971                          mc--selected-makefile-history
1972                          )))))
1973         makefile)))))
1974
1975 (defun mc--set-command (&optional file)
1976   ;; Return a compilation command, built according to the existence
1977   ;; of a makefile or not, to compile FILE .
1978   (setq completion-ignore-case nil) ; let completion be case sensitive
1979   (let ((to-compile-fname (or file (mc--find-to-compile-file))))
1980     (if (setq mc--selected-makefile (mc--makefile-to-use))
1981         (progn
1982           ;; A makefile found in the directory:
1983           ;; using make to compile
1984           (let ((out-fname (if to-compile-fname
1985                                (mc--guess-compile-result-fname
1986                                 to-compile-fname)
1987                              nil)))
1988             ;; build make command by asking rule to user
1989             (concat mode-compile-make-program " "
1990                     (or (mc--eval mode-compile-make-options) "")
1991                     " -f \"" mc--selected-makefile "\" "
1992                     (mc--choose-makefile-rule
1993                      mc--selected-makefile out-fname))))
1994       ;; else
1995       ;; No makefile: build compile command asking  for confirmation to user.
1996       ;; Should this be replaced by the creation of a makefile (and then
1997       ;; running it) as rms proposed me?
1998       (or mc--compile-command
1999           (setq mc--compile-command
2000                 (concat (setq mode-compile-chosen-compiler
2001                               (mc--find-compiler)) " "
2002                         (or (getenv mc--cflags-varenv)
2003                             (mc--eval mc--comp-options))
2004                         (if to-compile-fname
2005                             (if mc--build-op-args
2006                                 (mc--build-output-args to-compile-fname)
2007                               (concat " " to-compile-fname)
2008                               )
2009                           " "))))
2010       (if (not mode-compile-never-edit-command-p)
2011           (setq mc--compile-command
2012                 (mc--read-string
2013                  (if mode-compile-expert-p
2014                      "Compile command: "
2015                    (if to-compile-fname
2016                        (format "Edit command to compile %s: "
2017                                to-compile-fname)
2018                      "Edit compile command: " ))
2019                  mc--compile-command))
2020         mc--compile-command))))
2021
2022 (defun mc--shell-compile (shell dbgflags &optional errors-regexp-alist)
2023   ;; Run SHELL with debug flags DBGFLAGS on current-buffer
2024   (let* ((shcmd   (or (mc--which shell)
2025                       (error "Compilation abort: command %s not found" shell)))
2026          (shfile  (or mc--remote-pathname (buffer-file-name)
2027                       (error "Compilation abort: Buffer %s has no filename"
2028                              (buffer-name))))
2029          (run-cmd (concat shcmd " " dbgflags " \"" shfile "\" "
2030                           (setq mc--shell-args
2031                                 (read-string (if mode-compile-expert-p
2032                                                  "Argv: "
2033                                                (format "Arguments to %s %s script: "
2034                                                        shfile shell))
2035                                              mc--shell-args)))))
2036     ;; Modify compilation-error-regexp-alist if needed
2037     (if errors-regexp-alist
2038         (progn
2039           ;; Set compilation-error-regexp-alist from compile
2040           (or (listp errors-regexp-alist)
2041               (error "Compilation abort: In mc--shell-compile errors-regexp-alist not a list."))
2042           ;; Add new regexp alist to compilation-error-regexp-alist
2043           (mapcar '(lambda(x)
2044                      (if (mc--member x compilation-error-regexp-alist) nil
2045                        (setq compilation-error-regexp-alist
2046                              (append (list x)
2047                                      compilation-error-regexp-alist))))
2048                   errors-regexp-alist)))
2049     ;; Run compile with run-cmd
2050     (mc--compile run-cmd)))
2051
2052 (defmacro mc--assq-get-fcomp (asq)
2053   ;; Return compile-function associated to ASQ
2054   (` (let* ((mode  (cdr  (, asq)))
2055             (massq (assq mode mode-compile-modes-alist)))
2056        (if massq (car-safe (cdr massq))))))
2057
2058 (defmacro mc--assq-get-fkill (asq)
2059   ;; Return kill-compile-function associated to ASQ
2060   (` (let* ((mode  (cdr  (, asq)))
2061             (massq (assq mode mode-compile-modes-alist)))
2062        (if massq (car-safe (cdr-safe (cdr massq)))))))
2063
2064 (defun mc--lookin-for-shell ()
2065   ;; Look into current-buffer to see if it is a shell script
2066   ;; and return function to compile it or nil.
2067   (mc--msg "Looking if buffer %s is a shell script..." (buffer-name))
2068   (save-excursion
2069     (save-restriction
2070       (widen)
2071       (goto-char (point-min))
2072       (if (looking-at "#![ \t]*/\\([^ \t\n]+/\\)\\([^ \t\n]+\\)")
2073           (let* ((shell-name (buffer-substring (match-beginning 2)
2074                                                (match-end       2)))
2075                  (shell-assq (assoc shell-name mode-compile-shell-alist)))
2076             (if shell-assq
2077                 (progn
2078                   (mc--msg "Buffer is a %s script" shell-name)
2079                   (setq mc--kill-compile (mc--assq-get-fkill shell-assq))
2080                   (mc--assq-get-fcomp shell-assq))
2081               nil))))))
2082
2083 (defun mc--lookat-name ()
2084   ;; Look at buffer file name to see if it can return a function
2085   ;; to compile it or nil.
2086   (mc--msg "Trying to guess compile command from buffer %s file name..."
2087            (buffer-name))
2088   (let ((fname (buffer-file-name)))
2089     (if (not fname) nil
2090       ;; try regexp from mode-compile-filename-regexp-alist
2091       (let ((tmp-al mode-compile-filename-regexp-alist)
2092             (found  nil))
2093         (while (and tmp-al (car tmp-al) (not found))
2094           ;; evaluate to string
2095           (let ((regxp (mc--eval (car (car tmp-al)))))
2096             (if (string-match regxp fname)
2097                 (setq found (car tmp-al)))
2098             (setq tmp-al (cdr tmp-al))))
2099         (if (not found)
2100             nil
2101           (mc--msg "File %s matches regexp %s" fname (car found))
2102           (setq mc--kill-compile (mc--assq-get-fkill found))
2103           (mc--assq-get-fcomp found))))))
2104 \f
2105
2106 ;; @ mode specific functions ;;;
2107
2108 (defun cc-compile ()
2109   "Run `compile' with a dynamically built command for `c-mode'.
2110
2111 The command is built depending of the existence of a makefile (which could
2112 be specified by changing value of variable mode-compile-makefile-regexp) in
2113 the current directory or not.
2114 If no makefile is found try to run a C compiler on the file or its companion.
2115
2116 See also variables:
2117  -- cc-compilers-list
2118  -- cc-default-compiler
2119  -- cc-companion-file-regexp
2120  -- cc-compiler-varenv
2121  -- cc-cflags-varenv
2122  -- cc-source-ext-list
2123  -- cc-headers-ext-list
2124  -- cc-source-file-ext-regexp"
2125   (setq
2126    mc--comp-lst          cc-compilers-list
2127    mc--def-comp          cc-default-compiler
2128    mc--compfile-regexp   cc-companion-file-regexp
2129    mc--comp-varenv       cc-compiler-varenv
2130    mc--comp-options      cc-default-compiler-options
2131    mc--cflags-varenv     cc-cflags-varenv
2132    mc--source-ext-lst    cc-source-ext-list
2133    mc--head-ext-lst      cc-headers-ext-list
2134    mc--source-ext-regexp cc-source-file-ext-regexp
2135    mc--build-op-args     cc-build-output-args
2136    mc--outfile-ext       cc-object-file-ext
2137    )
2138   (mc--compile (mc--set-command)))
2139
2140 (defun java-compile ()
2141   "Run `compile' with a dynamically built command for `java-mode'.
2142
2143 The command is built depending of the existence of a makefile (which could
2144 be specified by changing value of variable mode-compile-makefile-regexp) in
2145 the current directory or not.
2146 If no makefile is found try to run a Java compiler on the file or its
2147 companion.
2148
2149 See also variables:
2150  -- java-compilers-list
2151  -- java-default-compiler
2152  -- java-companion-file-regexp
2153  -- java-compiler-varenv
2154  -- java-cflags-varenv
2155  -- java-source-ext-list
2156  -- java-headers-ext-list
2157  -- java-source-file-ext-regexp"
2158   (setq
2159    mc--comp-lst          java-compilers-list
2160    mc--def-comp          java-default-compiler
2161    mc--compfile-regexp   java-companion-file-regexp
2162    mc--comp-varenv       java-compiler-varenv
2163    mc--comp-options      java-default-compiler-options
2164    mc--cflags-varenv     java-cflags-varenv
2165    mc--source-ext-lst    java-source-ext-list
2166    mc--head-ext-lst      java-headers-ext-list
2167    mc--source-ext-regexp java-source-file-ext-regexp
2168    mc--build-op-args     java-build-output-args
2169    mc--outfile-ext       java-object-file-ext
2170    )
2171   (mc--compile (mc--set-command)))
2172
2173 (defun c++-compile ()
2174   "Run `compile' with a dynamically built command for `c++-mode'.
2175
2176 The command is built depending of the existence of a makefile (which could
2177 be specified by changing value of variable mode-compile-makefile-regexp) in
2178 the current directory or not.
2179 If no makefile is found try to run a C++ compiler on the file or its
2180 companion.
2181
2182 See also variables:
2183  -- c++-compilers-list
2184  -- c++-default-compiler
2185  -- c++-companion-file-regexp
2186  -- c++-compiler-varenv
2187  -- c++-cflags-varenv
2188  -- c++-source-ext-list
2189  -- c++-headers-ext-list
2190  -- c++-source-file-ext-regexp"
2191   (setq
2192    mc--comp-lst          c++-compilers-list
2193    mc--def-comp          c++-default-compiler
2194    mc--compfile-regexp   c++-companion-file-regexp
2195    mc--comp-varenv       c++-compiler-varenv
2196    mc--comp-options      c++-default-compiler-options
2197    mc--cflags-varenv     c++-cflags-varenv
2198    mc--source-ext-lst    c++-source-ext-list
2199    mc--head-ext-lst      c++-headers-ext-list
2200    mc--source-ext-regexp c++-source-file-ext-regexp
2201    mc--build-op-args     c++-build-output-args
2202    mc--outfile-ext       c++-object-file-ext
2203    )
2204   (mc--compile (mc--set-command)))
2205
2206
2207 (defun ada-compile ()
2208   "Run `compile' with a dynamically built command for `ada-mode'.
2209
2210 The command is built depending of the existence of a makefile (which could
2211 be specified by changing value of variable mode-compile-makefile-regexp) in
2212 the current directory or not.
2213 If no makefile is found try to run an Ada compiler on the file.
2214
2215 See also variables:
2216  -- ada-compilers-list
2217  -- ada-default-compiler
2218  -- ada-companion-file-regexp
2219  -- ada-compiler-varenv
2220  -- ada-aflags-varenv
2221  -- ada-source-ext-list
2222  -- ada-headers-ext-list
2223  -- ada-source-file-ext-regexp)"
2224   (setq
2225    mc--comp-lst          ada-compilers-list
2226    mc--def-comp          ada-default-compiler
2227    mc--compfile-regexp   ada-companion-file-regexp
2228    mc--comp-varenv       ada-compiler-varenv
2229    mc--comp-options      ada-default-compiler-options
2230    mc--cflags-varenv     ada-aflags-varenv
2231    mc--source-ext-lst    ada-source-ext-list
2232    mc--head-ext-lst      ada-headers-ext-list
2233    mc--source-ext-regexp ada-source-file-ext-regexp
2234    mc--build-op-args     ada-build-output-args
2235    mc--outfile-ext       ada-object-file-ext
2236    )
2237   (mc--compile (mc--set-command)))
2238
2239
2240 (defun f77-compile ()
2241   "Run `compile' with a dynamically built command for `fortran-mode'.
2242
2243 The command is built depending of the existence of a makefile (which could
2244 be specified by changing value of variable mode-compile-makefile-regexp) in
2245 the current directory or not.
2246 If no makefile is found try to run a Fortran compiler on the file or
2247 its companion..
2248
2249 See also variables:
2250  -- f77-compilers-list
2251  -- f77-default-compiler
2252  -- f77-companion-file-regexp
2253  -- f77-compiler-varenv
2254  -- f77-cflags-varenv
2255  -- f77-source-ext-list
2256  -- f77-headers-ext-list
2257  -- f77-source-file-ext-regexp)"
2258   (setq
2259    mc--comp-lst          f77-compilers-list
2260    mc--def-comp          f77-default-compiler
2261    mc--compfile-regexp   f77-companion-file-regexp
2262    mc--comp-varenv       f77-compiler-varenv
2263    mc--cflags-varenv     f77-cflags-varenv
2264    mc--comp-options      f77-default-compiler-options
2265    mc--source-ext-lst    f77-source-ext-list
2266    mc--head-ext-lst      f77-headers-ext-list
2267    mc--source-ext-regexp f77-source-file-ext-regexp
2268    mc--build-op-args     f77-build-output-args
2269    mc--outfile-ext       f77-object-file-ext
2270    )
2271   (mc--compile (mc--set-command)))
2272
2273
2274 (defun elisp-compile ()
2275   "Run `byte-compile' on the current Emacs lisp buffer.
2276 For `emacs-lisp-mode' and `lisp-interaction-mode'.
2277
2278 Produce a `.elc' file if possible or `byte-compile' only the buffer."
2279   (let ((comp-file (or (buffer-file-name) "")))
2280     (if (string-match emacs-lisp-sources-regexp comp-file)
2281         (progn
2282           (mc--msg "Byte compiling file %s ..." comp-file)
2283           (byte-compile-file comp-file))
2284       (mc--msg "Byte compiling buffer %s #No .elc produced ..." (buffer-name))
2285       (mc--byte-compile-buffer))))
2286
2287
2288 (defun makefile-compile (&optional makefile)
2289   "Run `make' on the current-buffer (`makefile-mode').
2290
2291 The user is prompted for a selection of make rules to build."
2292   (let ((mkfile (or makefile (buffer-file-name)
2293                     (error
2294                      "Compilation abort: buffer %s has no file name"
2295                      (buffer-name)))))
2296     (setq mc--selected-makefile mkfile)
2297     (setq mc--compile-command
2298           (concat mode-compile-make-program " "
2299                   (or (mc--eval mode-compile-make-options) "")
2300                   " -f \"" mkfile "\" "
2301                   (mc--choose-makefile-rule mkfile))))
2302     (mc--compile mc--compile-command))
2303
2304
2305 (defun dired-compile ()
2306   "Run `make' if a Makefile is present in current directory (`dired-mode').
2307
2308 The user is prompted for a selection of a makefile to choose if many
2309 matching `mode-compile-makefile-regexp' are present in the directory and
2310 for the make rules to build. If directory contain no makefile the function
2311 try to find if there are some un-byte-compiled .el files and recompile them
2312 if needed.
2313 Ask for the complete `compile-command' if no makefile and no .el files found."
2314   (let ((makefile (mc--makefile-to-use)))
2315     (if makefile
2316         ;; Makefile exists, compile with it
2317         (makefile-compile makefile)
2318       ;; No makefile found look for some .el files
2319       (mc--msg "No makefile found, looking for .el files ...")
2320       (let ((el-files (directory-files
2321                        default-directory nil emacs-lisp-sources-regexp)))
2322         (if el-files
2323             ;; Some .el files found byte-recompile them
2324             (mc--byte-recompile-files el-files)
2325           ;; No .el files ask compile command to user
2326           (mc--msg "No .el files found in directory %s" default-directory)
2327           (default-compile))))))
2328
2329
2330 (defun sh-compile ()
2331   "Run `sh-command' (Bourne Shell) with `sh-dbg-flags' on current-buffer (`sh-mode').
2332
2333 User is prompted for arguments to run his sh program with.
2334 If you want to step through errors set the variable `sh-compilation-error-regexp-alist'
2335 to a value understandable by compile's `next-error'.
2336 See variables compilation-error-regexp-alist or sh-compilation-error-regexp-alist."
2337   (mc--shell-compile sh-command sh-dbg-flags sh-compilation-error-regexp-alist))
2338
2339
2340 (defun csh-compile ()
2341   "Run `csh-command' (C Shell) with `csh-dbg-flags' on current-buffer (`csh-mode').
2342
2343 User is prompted for arguments to run his csh program with.
2344 If you want to step through errors set the variable `csh-compilation-error-regexp-alist'
2345 to a value understandable by compile's `next-error'.
2346 See variables compilation-error-regexp-alist or csh-compilation-error-regexp-alist."
2347   (mc--shell-compile csh-command csh-dbg-flags csh-compilation-error-regexp-alist))
2348
2349
2350 (defun zsh-compile ()
2351   "Run `zsh-command' (Z Shell) with `zsh-dbg-flags' on current-buffer (`zsh-mode').
2352
2353 User is prompted for arguments to run his zsh program with.
2354 If you want to step through errors set the variable `zsh-compilation-error-regexp-alist'
2355 to a value understandable by compile's `next-error'.
2356 See variables compilation-error-regexp-alist or zsh-compilation-error-regexp-alist."
2357   (mc--shell-compile zsh-command zsh-dbg-flags zsh-compilation-error-regexp-alist))
2358
2359
2360 (defun perl-compile ()
2361   "Run Perl with `perl-dbg-flags' on current-buffer (`perl-mode').
2362
2363 User is prompted for arguments to run his perl program with.
2364 If you want to step through errors set the variable `perl-compilation-error-regexp-alist'
2365 to a value understandable by compile's `next-error'.
2366 See variables compilation-error-regexp-alist or perl-compilation-error-regexp-alist."
2367   (mc--shell-compile perl-command perl-dbg-flags perl-compilation-error-regexp-alist))
2368
2369
2370 (defun tcl-compile ()
2371   ;; JWH
2372   "Run `tcl-command' with `tcl-dbg-flags' on current-buffer (`tcl-mode').
2373
2374 User is prompted for arguments to run his Tcl/Tk program with.
2375 If you want to step through errors set the variable `tcl-compilation-error-regexp-alist'
2376 to a value understandable by compile's `next-error'.
2377 See variables compilation-error-regexp-alist or tcl-compilation-error-regexp-alist."
2378   (mc--shell-compile tcl-command tcl-dbg-flags tcl-compilation-error-regexp-alist))
2379
2380
2381 (defun python-compile ()
2382   ;; BM
2383   "Run `python-command' with `python-dbg-flags' on current-buffer (`python-mode').
2384
2385 User is prompted for arguments to run his Python program with.
2386 If you want to step through errors set the variable `python-compilation-error-regexp-alist'
2387 to a value understandable by compile's `next-error'.
2388 See variables compilation-error-regexp-alist or python-compilation-error-regexp-alist."
2389   (mc--shell-compile python-command python-dbg-flags python-compilation-error-regexp-alist))
2390
2391
2392 (defun default-compile ()
2393   "Default function invoked by `mode-compile' (\\[mode-compile])
2394 when everything else failed.
2395
2396 Ask to user to edit `compile-command' and run `compile' (\\[compile]) with it."
2397   (setq mc--compile-command
2398         (mc--read-string
2399          (if mode-compile-expert-p
2400              "Compile command: "
2401            (format "Edit command to compile %s : " (buffer-name)))
2402          (or mc--compile-command compile-command)))
2403   (mc--compile mc--compile-command))
2404
2405 (defvar mc--makefile) ;; Just to avoid compiler warning
2406 (defun guess-compile ()
2407   "Try to guess how to compile current-buffer.
2408
2409 When the compile command could not be extrapolated from major-mode this function
2410 is called which try to guess from number of parameters which command to build.
2411 The steps to guess which command to use to compile are:
2412   1st : Look into the file to check if it is a shell script
2413         See variable mode-compile-shell-alist
2414   2nd : Try to guess from the file name
2415         See variable mode-compile-filename-regexp-alist
2416   3rd : Look for a makefile in the current directory
2417         See variable mode-compile-makefile-regexp
2418   Last: Give up and ask user for the command to use
2419         See function default-compile"
2420   (mc--msg "Trying to guess how to compile buffer %s ..." (buffer-name))
2421   (let ((mc--makefile))
2422     (funcall
2423      (or
2424       ;; step 1
2425       (mc--lookin-for-shell)
2426       ;; step 2
2427       (mc--lookat-name)
2428       ;; step 3
2429       (progn
2430         (mc--msg "Looking for a makefile in current directory...")
2431         (if (setq mc--makefile (mc--makefile-to-use
2432                                 (and (buffer-file-name)
2433                                      (file-name-directory (buffer-file-name)))))
2434             (progn
2435               (setq mc--kill-compile 'kill-compilation)
2436               ;; Byte-compiling says `makefile' is not referenced.
2437               '(lambda () (makefile-compile mc--makefile)))))
2438       ;; step 4
2439       (progn
2440         (mc--msg "Don't know how to compile %s, giving up..."
2441                  (buffer-name))
2442         (setq mc--kill-compile 'kill-compilation)
2443         'default-compile)))))
2444 \f
2445
2446 ;; @ user accessible/exported function ;;;
2447
2448 ;; get reporter-submit-bug-report when byte-compiling
2449 (and (fboundp 'eval-when-compile)
2450      (eval-when-compile (require 'reporter)))
2451
2452 ;;;###autoload
2453 (defun mode-compile-submit-bug-report ()
2454   "*Submit via mail a bug report on mode-compile v2.27."
2455   (interactive)
2456   (and
2457    (y-or-n-p "Do you REALLY want to submit a report on mode-compile? ")
2458    (require 'reporter)
2459    (reporter-submit-bug-report
2460     mode-compile-help-address
2461     (concat "mode-compile " mode-compile-version)
2462     (list
2463      ;; Interesting mode-compile variables
2464      'mode-compile-modes-alist
2465      'mode-compile-filename-regexp-alist
2466      'mode-compile-shell-alist
2467      'mode-compile-makefile-regexp
2468      'mode-compile-make-program
2469      'mode-compile-default-make-options
2470      'mode-compile-make-options
2471      'mode-compile-reading-time
2472      'mode-compile-expert-p
2473      'mode-compile-never-edit-command-p
2474      'mode-compile-save-all-p
2475      'mode-compile-always-save-buffer-p
2476      'mode-compile-before-compile-hook
2477      'mode-compile-after-compile-hook
2478      'mode-compile-before-kill-hook
2479      'mode-compile-after-kill-hook
2480      'mode-compile-other-frame-p
2481      'mode-compile-other-frame-name
2482      'mode-compile-frame-parameters-alist
2483      'mode-compile-preferred-default-makerule
2484      'emacs-lisp-byte-compile-dir-interactive-p
2485      ;; others variables
2486      'features
2487      'compilation-error-regexp-alist
2488      'compile-command
2489      )
2490     nil
2491     nil
2492     "Dear Heddy,")))
2493 \f
2494
2495 ;;;###autoload
2496 (defun mode-compile (&optional remote-host)
2497   "*Compile the file in the current buffer with a dynamically built command.
2498
2499 The command is built according to the current major mode the function
2500 was invoked from.
2501
2502 Running this command preceded by universal-argument (\\[universal-argument])
2503 allows remote compilation, the user is prompted for a host name to run the
2504 compilation command on.
2505
2506 Currently know how to compile in:
2507  `c-mode' ,              -- function cc-compile.
2508  `java-mode' ,           -- function java-compile.
2509  `c++-mode',             -- function c++-compile.
2510  `ada-mode',             -- function ada-compile.
2511  `fortran-mode',         -- function f77-compile.
2512  `emacs-lisp-mode'       -- function elisp-compile.
2513  `lisp-interaction-mode' -- function elisp-compile.
2514  `makefile-mode'         -- function makefile-compile.
2515  `dired-mode'            -- function dired-compile.
2516  `sh-mode'               -- function sh-compile.
2517  `csh-mode'              -- function csh-compile.
2518  `zsh-mode'              -- function zsh-compile.
2519  `perl-mode'             -- function perl-compile.
2520  `cperl-mode'            -- function perl-compile.
2521  `tcl-mode'              -- function tcl-compile.
2522  `python-mode'           -- function python-compile.
2523  `fundamental-mode'      -- function guess-compile.
2524  `text-mode'             -- function guess-compile.
2525  `indented-text-mode'    -- function guess-compile.
2526  `compilation-mode'      -- function default-compile.
2527  The function `guess-compile' is called when mode is unknown.
2528
2529 The variable `mode-compile-modes-alist' contain description of known
2530 modes.  The hooks variables `mode-compile-before-compile-hook' and
2531 `mode-compile-after-compile-hook' are run just before and after
2532 invoking the compile command of the mode.
2533
2534 Use the command `mode-compile-kill' (\\[mode-compile-kill]) to abort a
2535 running compilation.
2536
2537 Bound on \\[mode-compile]."
2538   (interactive "P")
2539   ;; reinit
2540   (setq
2541    mc--efs-path-list  nil
2542    mc--remote-command nil)
2543   (if remote-host
2544       ;; Remote compilation asked
2545       ;; prepare remote command
2546       (mc--set-remote-cmd remote-host)
2547     ;; Not asked but check buffer-file-name to see
2548     ;; if it is not an efs file
2549     (setq mc--efs-path-list (and (fboundp 'efs-ftp-path)
2550                                  (buffer-file-name)
2551                                  (efs-ftp-path (buffer-file-name))))
2552     (and mc--efs-path-list (mc--set-remote-cmd
2553                             (car mc--efs-path-list)
2554                             (nth 1 mc--efs-path-list)
2555                             (nth 2 mc--efs-path-list))))
2556   (if (and mode-compile-always-save-buffer-p
2557            (buffer-file-name))
2558       ;; save-buffer already check if buffer had been modified
2559       (save-buffer))
2560   (if mode-compile-save-all-p (save-some-buffers t))
2561   ;; Check if compile-command set as local variable
2562   (if (and
2563        (boundp 'compile-command)
2564        (local-variable-p 'compile-command (current-buffer))
2565        compile-command ; not null
2566        (if mc--compile-command
2567            (equal compile-command mc--compile-command)
2568          t)
2569        )
2570       ;; Just ask user and go
2571       (progn
2572         (mc--run-hooks 'mode-compile-before-compile-hook)
2573         (default-compile)
2574         (mc--run-hooks 'mode-compile-after-compile-hook)
2575         )
2576     ;; Here is the real work
2577     (let ((mode-elem (assq major-mode mode-compile-modes-alist)))
2578       (if mode-elem
2579           ;; known mode
2580           (progn
2581             (mc--msg (substitute-command-keys
2582                       "Compiling in %s mode ... \\[mode-compile-kill] to kill.")
2583                      mode-name)
2584             (mc--run-hooks 'mode-compile-before-compile-hook)
2585             ;; mc--funcall can launch the compilation
2586             ;; in another frame.
2587             (mc--funcall   (car (cdr mode-elem)))
2588             (mc--run-hooks 'mode-compile-after-compile-hook))
2589         ;; unknown mode: try to guess
2590         (mc--msg (substitute-command-keys
2591                   "Don't know how to compile in %s mode, guessing... \\[mode-compile-kill] to kill.")
2592                  mode-name)
2593         (mc--run-hooks 'mode-compile-before-compile-hook)
2594         ;; mc--funcall can launch the compilation
2595         ;; in another frame.
2596         (mc--funcall   'guess-compile)
2597         (mc--run-hooks 'mode-compile-after-compile-hook)))))
2598
2599 (provide 'mode-compile)
2600 \f
2601
2602 ;;;###autoload
2603 (defun mode-compile-kill()
2604   "*Kill the running compilation launched by `mode-compile' (\\[mode-compile]) \
2605 command.
2606
2607 The compilation command is killed according to the current major mode
2608 the function was invoked from.
2609
2610 Currently know how to kill compilations from:
2611  `c-mode' ,              -- function kill-compilation.
2612  `java-mode' ,           -- function kill-compilation.
2613  `c++-mode' ,            -- function kill-compilation.
2614  `ada-mode' ,            -- function kill-compilation.
2615  `fortran-mode' ,        -- function kill-compilation.
2616  `emacs-lisp-mode'       -- function keyboard-quit.
2617  `lisp-interaction-mode' -- function keyboard-quit.
2618  `makefile-mode'         -- function kill-compilation.
2619  `dired-mode'            -- function kill-compilation.
2620  `sh-mode'               -- function kill-compilation.
2621  `csh-mode'              -- function kill-compilation.
2622  `zsh-mode'              -- function kill-compilation.
2623  `perl-mode'             -- function kill-compilation.
2624  `cperl-mode'            -- function kill-compilation.
2625  `tcl-mode'              -- function kill-compilation.
2626  `python-mode'           -- function kill-compilation.
2627  `fundamental-mode'      -- Bound dynamically.
2628  `text-mode'             -- Bound dynamically.
2629  `indented-text-mode'    -- Bound dynamically.
2630  `compilation-mode'      -- function kill-compilation.
2631
2632 The variable `mode-compile-modes-alist' contain description of ALL
2633 known modes.  The hooks variables `mode-compile-before-kill-hook' and
2634 `mode-compile-after-kill-hook' are run just before and after invoking
2635 the kill compile command of the mode.
2636
2637 Bound on \\[mode-compile-kill]."
2638   (interactive)
2639   (let ((mode-elem (assq major-mode mode-compile-modes-alist)))
2640     (if mode-elem
2641         ;; known mode
2642         (progn
2643           (mc--run-hooks 'mode-compile-before-kill-hook)
2644           (mc--msg "Killing compilation in %s mode..." mode-name)
2645           (let ((killfun (or (car-safe (cdr (cdr mode-elem)))
2646                              mc--kill-compile
2647                              nil)))
2648             (if killfun
2649                 ;; I don't call mc--funcall here caus' we don't need
2650                 ;; to switch to another frame to kill a compilation.
2651                 (funcall killfun)
2652               (mc--msg "Unable to kill compilation in %s mode..." mode-name))
2653             (mc--run-hooks 'mode-compile-after-kill-hook)))
2654       ;; unknown mode
2655       (mc--msg "Don't know how to kill compilation in %s mode"
2656                mode-name))))
2657
2658 (provide 'mode-compile-kill)
2659
2660 \f
2661
2662 ;;; Local variables:
2663 ;;; outline-regexp: ";; @+"
2664 ;;; eval: (outline-minor-mode 1)
2665 ;;; End:
2666
2667 ;;; mode-compile.el ends here