PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/site-lisp/smart-compile/smart-compile.el

https://github.com/redguardtoo/emacs.d
Emacs Lisp | 231 lines | 154 code | 33 blank | 44 comment | 0 complexity | 6aa7cbed620bfa0da6ea615f075187c2 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
  1. ;;; smart-compile.el --- an interface to `compile'
  2. ;; Copyright (C) 1998-2012 by Seiji Zenitani
  3. ;; Author: Seiji Zenitani <zenitani@mac.com>
  4. ;; $Id: smart-compile.el 764 2012-07-10 15:58:08Z zenitani $
  5. ;; Keywords: tools, unix
  6. ;; Created: 1998-12-27
  7. ;; Compatibility: Emacs 21 or later
  8. ;; URL(en): http://www.emacswiki.org/emacs/smart-compile.el
  9. ;; URL(jp): http://th.nao.ac.jp/MEMBER/zenitani/elisp-j.html#smart-compile
  10. ;; Contributors: Sakito Hisakura, Greg Pfell
  11. ;; This file is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15. ;; This file is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to
  21. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23. ;;; Commentary:
  24. ;; This package provides `smart-compile' function.
  25. ;; You can associates a particular file with a particular compile functions,
  26. ;; by editing `smart-compile-alist'.
  27. ;;
  28. ;; To use this package, add these lines to your .emacs file:
  29. ;; (require 'smart-compile)
  30. ;;
  31. ;; Note that it requires emacs 21 or later.
  32. ;;; Code:
  33. (defgroup smart-compile nil
  34. "An interface to `compile'."
  35. :group 'processes
  36. :prefix "smart-compile")
  37. (defcustom smart-compile-alist '(
  38. (emacs-lisp-mode . (emacs-lisp-byte-compile))
  39. (html-mode . (browse-url-of-buffer))
  40. (web-mode . (browse-url-of-buffer))
  41. (nxhtml-mode . (browse-url-of-buffer))
  42. (nxml-mode . (browse-url-of-buffer))
  43. (html-helper-mode . (browse-url-of-buffer))
  44. (octave-mode . (run-octave))
  45. ("\\.c\\'" . "gcc -O2 %f -lm -o %n")
  46. ;; ("\\.c\\'" . "gcc -O2 %f -lm -o %n && ./%n")
  47. ("\\.[Cc]+[Pp]*\\'" . "g++ -O2 %f -lm -o %n")
  48. ("\\.m\\'" . "gcc -O2 %f -lobjc -lpthread -o %n")
  49. ("[Gg]runtfile.js" . "grunt")
  50. ("\\.java\\'" . "javac %f")
  51. ("\\.php\\'" . "php -l %f")
  52. ("\\.f90\\'" . "gfortran %f -o %n")
  53. ("\\.[Ff]\\'" . "gfortran %f -o %n")
  54. ("\\.cron\\(tab\\)?\\'" . "crontab %f")
  55. ("\\.tex\\'" . (tex-file))
  56. ("\\.texi\\'" . "makeinfo %f")
  57. ("\\.mp\\'" . "mptopdf %f")
  58. ("\\.pl\\'" . "perl -cw %f")
  59. ("\\.rb\\'" . "ruby -cw %f")
  60. ) "Alist of filename patterns vs corresponding format control strings.
  61. Each element looks like (REGEXP . STRING) or (MAJOR-MODE . STRING).
  62. Visiting a file whose name matches REGEXP specifies STRING as the
  63. format control string. Instead of REGEXP, MAJOR-MODE can also be used.
  64. The compilation command will be generated from STRING.
  65. The following %-sequences will be replaced by:
  66. %F absolute pathname ( /usr/local/bin/netscape.bin )
  67. %f file name without directory ( netscape.bin )
  68. %n file name without extension ( netscape )
  69. %e extension of file name ( bin )
  70. %o value of `smart-compile-option-string' ( \"user-defined\" ).
  71. If the second item of the alist element is an emacs-lisp FUNCTION,
  72. evaluate FUNCTION instead of running a compilation command.
  73. "
  74. :type '(repeat
  75. (cons
  76. (choice
  77. (regexp :tag "Filename pattern")
  78. (function :tag "Major-mode"))
  79. (choice
  80. (string :tag "Compilation command")
  81. (sexp :tag "Lisp expression"))))
  82. :group 'smart-compile)
  83. (put 'smart-compile-alist 'risky-local-variable t)
  84. (defconst smart-compile-replace-alist '(
  85. ("%F" . (buffer-file-name))
  86. ("%f" . (file-name-nondirectory (buffer-file-name)))
  87. ("%n" . (file-name-sans-extension
  88. (file-name-nondirectory (buffer-file-name))))
  89. ("%e" . (or (file-name-extension (buffer-file-name)) ""))
  90. ("%o" . smart-compile-option-string)
  91. ;; ("%U" . (user-login-name))
  92. ))
  93. (put 'smart-compile-replace-alist 'risky-local-variable t)
  94. (defvar smart-compile-check-makefile t)
  95. (make-variable-buffer-local 'smart-compile-check-makefile)
  96. (defcustom smart-compile-make-program "make "
  97. "The command by which to invoke the make program."
  98. :type 'string
  99. :group 'smart-compile)
  100. (defcustom smart-compile-option-string ""
  101. "The option string that replaces %o. The default is empty."
  102. :type 'string
  103. :group 'smart-compile)
  104. ;;;###autoload
  105. (defun smart-compile (&optional arg)
  106. "An interface to `compile'.
  107. It calls `compile' or other compile function,
  108. which is defined in `smart-compile-alist'."
  109. (interactive "p")
  110. (let ((name (buffer-file-name))
  111. (not-yet t))
  112. (if (not name)
  113. (error "cannot get filename."))
  114. (save-buffer)
  115. (cond
  116. ;; local command
  117. ;; The prefix 4 (C-u M-x smart-compile) skips this section
  118. ;; in order to re-generate the compile-command
  119. ((and (not (= arg 4)) ; C-u M-x smart-compile
  120. (local-variable-p 'compile-command)
  121. compile-command)
  122. (call-interactively 'compile)
  123. (setq not-yet nil)
  124. )
  125. ;; make?
  126. ((and smart-compile-check-makefile
  127. (or (file-readable-p "Makefile")
  128. (file-readable-p "makefile")))
  129. (if (y-or-n-p "Makefile is found. Try 'make'? ")
  130. (progn
  131. (set (make-local-variable 'compile-command) "make ")
  132. (call-interactively 'compile)
  133. (setq not-yet nil)
  134. )
  135. (setq smart-compile-check-makefile nil)))
  136. ) ;; end of (cond ...)
  137. ;; compile
  138. (let( (alist smart-compile-alist)
  139. (case-fold-search nil)
  140. (function nil) )
  141. (while (and alist not-yet)
  142. (if (or
  143. (and (symbolp (caar alist))
  144. (eq (caar alist) major-mode))
  145. (and (stringp (caar alist))
  146. (string-match (caar alist) name))
  147. )
  148. (progn
  149. (setq function (cdar alist))
  150. (if (stringp function)
  151. (progn
  152. (set (make-local-variable 'compile-command)
  153. (smart-compile-string function))
  154. (call-interactively 'compile)
  155. )
  156. (if (listp function)
  157. (eval function)
  158. ))
  159. (setq alist nil)
  160. (setq not-yet nil)
  161. )
  162. (setq alist (cdr alist)) )
  163. ))
  164. ;; If compile-command is not defined and the contents begins with "#!",
  165. ;; set compile-command to filename.
  166. (if (and not-yet
  167. (not (memq system-type '(windows-nt ms-dos)))
  168. (not (string-match "/\\.[^/]+$" name))
  169. (not
  170. (and (local-variable-p 'compile-command)
  171. compile-command))
  172. )
  173. (save-restriction
  174. (widen)
  175. (if (equal "#!" (buffer-substring 1 (min 3 (point-max))))
  176. (set (make-local-variable 'compile-command) name)
  177. ))
  178. )
  179. ;; compile
  180. (if not-yet (call-interactively 'compile) )
  181. ))
  182. (defun smart-compile-string (format-string)
  183. "Document forthcoming..."
  184. (if (and (boundp 'buffer-file-name)
  185. (stringp buffer-file-name))
  186. (let ((rlist smart-compile-replace-alist)
  187. (case-fold-search nil))
  188. (while rlist
  189. (while (string-match (caar rlist) format-string)
  190. (setq format-string
  191. (replace-match
  192. (eval (cdar rlist)) t nil format-string)))
  193. (setq rlist (cdr rlist))
  194. )
  195. ))
  196. format-string)
  197. (provide 'smart-compile)
  198. ;;; smart-compile.el ends here