Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/smart-compile.el
987 views
1
;;; smart-compile.el --- an interface to `compile'
2
3
;; Copyright (C) 1998-2009 by Seiji Zenitani
4
5
;; Author: Seiji Zenitani <[email protected]>
6
;; $Id$
7
;; Keywords: tools, unix
8
;; Created: 1998-12-27
9
;; Compatibility: Emacs 21 or later
10
;; URL(en): http://homepage.mac.com/zenitani/comp-e.html
11
;; URL(jp): http://homepage.mac.com/zenitani/elisp-j.html#smart-compile
12
13
;; Contributors: Sakito Hisakura
14
15
;; This file is free software; you can redistribute it and/or modify
16
;; it under the terms of the GNU General Public License as published by
17
;; the Free Software Foundation; either version 2, or (at your option)
18
;; any later version.
19
20
;; This file is distributed in the hope that it will be useful,
21
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
;; GNU General Public License for more details.
24
25
;; You should have received a copy of the GNU General Public License
26
;; along with GNU Emacs; see the file COPYING. If not, write to
27
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28
;; Boston, MA 02111-1307, USA.
29
30
;;; Commentary:
31
32
;; This package provides `smart-compile' function.
33
;; You can associates a particular file with a particular compile functions,
34
;; by editing `smart-compile-alist'.
35
;;
36
;; To use this package, add these lines to your .emacs file:
37
;; (require 'smart-compile)
38
;;
39
;; Note that it requires emacs 21 or later.
40
41
;;; Code:
42
43
(defgroup smart-compile nil
44
"An interface to `compile'."
45
:group 'processes
46
:prefix "smarct-compile")
47
48
(defcustom smart-compile-alist '(
49
(emacs-lisp-mode . (emacs-lisp-byte-compile))
50
(html-mode . (browse-url-of-buffer))
51
(nxhtml-mode . (browse-url-of-buffer))
52
(html-helper-mode . (browse-url-of-buffer))
53
(octave-mode . (run-octave))
54
("\\.c\\'" . "gcc -O2 %f -lm -o %n")
55
;; ("\\.c\\'" . "gcc -O2 %f -lm -o %n && ./%n")
56
("\\.[Cc]+[Pp]*\\'" . "g++ -O2 %f -lm -o %n")
57
("\\.m\\'" . "gcc -O2 %f -lobjc -lpthread -o %n")
58
("\\.java\\'" . "javac %f")
59
("\\.php\\'" . "php -l %f")
60
("\\.f90\\'" . "gfortran %f -o %n")
61
("\\.[Ff]\\'" . "gfortran %f -o %n")
62
("\\.cron\\(tab\\)?\\'" . "crontab %f")
63
("\\.tex\\'" . (tex-file))
64
("\\.texi\\'" . "makeinfo %f")
65
("\\.mp\\'" . "mptopdf %f")
66
("\\.pl\\'" . "perl -cw %f")
67
("\\.rb\\'" . "ruby -cw %f")
68
) "List of compile commands. In argument,
69
some keywords beginning with '%' will be replaced by:
70
71
%F absolute pathname ( /usr/local/bin/netscape.bin )
72
%f file name without directory ( netscape.bin )
73
%n file name without extension ( netscape )
74
%e extension of file name ( bin )
75
76
"
77
:type '(repeat
78
(cons
79
(choice
80
(regexp :tag "Filename pattern")
81
(function :tag "Major-mode"))
82
(choice
83
(string :tag "Compilation command")
84
(sexp :tag "Lisp expression"))))
85
:group 'smart-compile)
86
(put 'smart-compile-alist 'risky-local-variable t)
87
88
(defconst smart-compile-replace-alist '(
89
("%F" . (buffer-file-name))
90
("%f" . (file-name-nondirectory (buffer-file-name)))
91
("%n" . (file-name-sans-extension
92
(file-name-nondirectory (buffer-file-name))))
93
("%e" . (or (file-name-extension (buffer-file-name)) ""))
94
))
95
(put 'smart-compile-replace-alist 'risky-local-variable t)
96
97
(defvar smart-compile-check-makefile t)
98
(make-variable-buffer-local 'smart-compile-check-makefile)
99
100
(defcustom smart-compile-make-program "make "
101
"The command by which to invoke the make program."
102
:type 'string
103
:group 'smart-compile)
104
105
;; (defvar smart-compile-command nil)
106
;; (make-variable-buffer-local 'smart-compile-command)
107
;; (put 'smart-compile-command 'safe-local-variable 'stringp)
108
109
;;;###autoload
110
(defun smart-compile ()
111
"An interface to `compile'.
112
It calls `compile' or other compile function,
113
which is defined in `smart-compile-alist'."
114
(interactive)
115
(let ((name (buffer-file-name))
116
(not-yet t))
117
118
(if (not name)(error "cannot get filename."))
119
120
(cond
121
122
;; local command
123
((and (local-variable-p 'compile-command)
124
compile-command)
125
(call-interactively 'compile)
126
(setq not-yet nil)
127
)
128
129
;; ;; use template string
130
;; ((and (local-variable-p 'smart-compile-command)
131
;; smart-compile-command)
132
;; (set (make-local-variable 'compile-command)
133
;; (smart-compile-string smart-compile-command))
134
;; (call-interactively 'compile)
135
;; (setq not-yet nil)
136
;; )
137
138
;; make?
139
((and smart-compile-check-makefile
140
(or (file-readable-p "Makefile")
141
(file-readable-p "makefile")))
142
(if (y-or-n-p "Makefile is found. Try 'make'? ")
143
(progn
144
(set (make-local-variable 'compile-command) "make ")
145
(call-interactively 'compile)
146
(setq not-yet nil)
147
)
148
(setq smart-compile-check-makefile nil)))
149
150
) ;; end of (cond ...)
151
152
;; compile
153
(let( (alist smart-compile-alist)
154
(case-fold-search nil)
155
(function nil) )
156
(while (and alist not-yet)
157
(if (or
158
(and (symbolp (caar alist))
159
(eq (caar alist) major-mode))
160
(and (stringp (caar alist))
161
(string-match (caar alist) name))
162
)
163
(progn
164
(setq function (cdar alist))
165
(if (stringp function)
166
(progn
167
(set (make-local-variable 'compile-command)
168
(smart-compile-string function))
169
(call-interactively 'compile)
170
)
171
(if (listp function)
172
(eval function)
173
))
174
(setq alist nil)
175
(setq not-yet nil)
176
)
177
(setq alist (cdr alist)) )
178
))
179
180
;; If compile-command is not defined and the contents begins with "#!",
181
;; set compile-command to filename.
182
(if (and not-yet
183
(not (memq system-type '(windows-nt ms-dos)))
184
(not (string-match "/\\.[^/]+$" name))
185
(not
186
(and (local-variable-p 'compile-command)
187
compile-command))
188
)
189
(save-restriction
190
(widen)
191
(if (equal "#!" (buffer-substring 1 (min 3 (point-max))))
192
(set (make-local-variable 'compile-command) name)
193
))
194
)
195
196
;; compile
197
(if not-yet (call-interactively 'compile) )
198
199
))
200
201
(defun smart-compile-string (arg)
202
"Document forthcoming..."
203
(if (and (boundp 'buffer-file-name)
204
(stringp buffer-file-name))
205
(let ((rlist smart-compile-replace-alist)
206
(case-fold-search nil))
207
(while rlist
208
(while (string-match (caar rlist) arg)
209
(setq arg
210
(replace-match
211
(eval (cdar rlist)) t nil arg)))
212
(setq rlist (cdr rlist))
213
)
214
))
215
arg)
216
217
(provide 'smart-compile)
218
219
;;; smart-compile.el ends here
220
221