PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/elisp/smart-compile.el

https://github.com/calas/dotfiles
Emacs Lisp | 218 lines | 146 code | 30 blank | 42 comment | 0 complexity | 3d8db6d874b7810cd5f13d31d8ec9b41 MD5 | raw file
Possible License(s): GPL-2.0
  1. ;;; smart-compile.el --- an interface to `compile'
  2. ;; Copyright (C) 1998-2008 by Seiji Zenitani
  3. ;; Author: Seiji Zenitani <zenitani@mac.com>
  4. ;; $Id$
  5. ;; Keywords: tools, unix
  6. ;; Created: 1998-12-27
  7. ;; Compatibility: Emacs 21, 22
  8. ;; URL(en): http://homepage.mac.com/zenitani/comp-e.html
  9. ;; URL(jp): http://homepage.mac.com/zenitani/elisp-j.html#smart-compile
  10. ;; Contributors: Sakito Hisakura, William XWL
  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 "smarct-compile")
  37. (defcustom smart-compile-alist '(
  38. (emacs-lisp-mode . (emacs-lisp-byte-compile))
  39. (html-mode . (browse-url-of-buffer))
  40. (nxhtml-mode . (browse-url-of-buffer))
  41. (html-helper-mode . (browse-url-of-buffer))
  42. (octave-mode . (run-octave))
  43. ("\\.c\\'" . "gcc -O2 %f -lm -o %n")
  44. ;; ("\\.c\\'" . "gcc -O2 %f -lm -o %n && ./%n")
  45. ("\\.[Cc]+[Pp]*\\'" . "g++ -O2 %f -lm -o %n")
  46. ("\\.m\\'" . "gcc -O2 %f -lobjc -lpthread -o %n")
  47. ("\\.java\\'" . "javac %f")
  48. ("\\.php\\'" . "php -l %f")
  49. ("\\.f90\\'" . "f90 %f -o %n")
  50. ("\\.[Ff]\\'" . "f77 %f -o %n")
  51. ("\\.cron\\(tab\\)?\\'" . "crontab %f")
  52. ("\\.tex\\'" . (tex-file))
  53. ("\\.texi\\'" . "makeinfo %f")
  54. ("\\.mp\\'" . "mptopdf %f")
  55. ("\\.pl\\'" . "perl -cw %f")
  56. ("\\.rb\\'" . "ruby -cw %f")
  57. ) "List of compile commands. In argument,
  58. some keywords beginning with '%' will be replaced by:
  59. %F absolute pathname ( /usr/local/bin/netscape.bin )
  60. %f file name without directory ( netscape.bin )
  61. %n file name without extension ( netscape )
  62. %e extension of file name ( bin )
  63. "
  64. :type '(repeat
  65. (cons
  66. (choice
  67. (regexp :tag "Filename pattern")
  68. (function :tag "Major-mode"))
  69. (choice
  70. (string :tag "Compilation command")
  71. (sexp :tag "Lisp expression"))))
  72. :group 'smart-compile)
  73. (put 'smart-compile-alist 'risky-local-variable t)
  74. (defvar smart-compile-replace-alist '(
  75. ("%F" . (buffer-file-name))
  76. ("%f" . (file-name-nondirectory (buffer-file-name)))
  77. ("%n" . (file-name-sans-extension
  78. (file-name-nondirectory (buffer-file-name))))
  79. ("%e" . (or (file-name-extension (buffer-file-name)) ""))
  80. ))
  81. (put 'smart-compile-replace-alist 'risky-local-variable t)
  82. (defvar smart-compile-check-makefile t)
  83. (make-variable-buffer-local 'smart-compile-check-makefile)
  84. (defcustom smart-compile-make-program "make "
  85. "The command by which to invoke the make program."
  86. :type 'string
  87. :group 'smart-compile)
  88. ;;;###autoload
  89. (defun smart-compile ()
  90. "An interface to `compile'.
  91. It calls `compile' or other compile function,
  92. which is defined in `smart-compile-alist'."
  93. (interactive)
  94. (let ((name (buffer-file-name))
  95. (not-yet t))
  96. (if (not name)(error "cannot get filename."))
  97. (cond
  98. ;; local command
  99. ((and (local-variable-p 'compile-command)
  100. compile-command)
  101. (progn
  102. (call-interactively 'compile)
  103. (setq not-yet nil)
  104. ))
  105. ;; make?
  106. ((and smart-compile-check-makefile
  107. (or (file-readable-p "Makefile")
  108. (file-readable-p "makefile")))
  109. (if (y-or-n-p "Makefile is found. Try 'make'? ")
  110. (progn
  111. (set (make-local-variable 'compile-command) "make ")
  112. (call-interactively 'compile)
  113. (setq not-yet nil)
  114. )
  115. (setq smart-compile-check-makefile nil)))
  116. ;; ant ? (from smart-compile+.el by William XWL)
  117. ((and smart-compile-check-makefile
  118. (file-readable-p "build.xml"))
  119. (if (y-or-n-p "build.xml is found. Try 'ant'? ")
  120. (progn
  121. (set (make-local-variable 'compile-command) "ant ")
  122. (call-interactively 'compile)
  123. (setq not-yet nil)
  124. )
  125. (setq smart-compile-check-makefile nil)))
  126. )
  127. ;; compile
  128. (let( (alist smart-compile-alist)
  129. (case-fold-search nil)
  130. (function nil) )
  131. (while (and alist not-yet)
  132. (if (or
  133. (and (symbolp (caar alist))
  134. (eq (caar alist) major-mode))
  135. (and (stringp (caar alist))
  136. (string-match (caar alist) name))
  137. )
  138. (progn
  139. (setq function (cdar alist))
  140. (if (stringp function)
  141. (progn
  142. (set (make-local-variable 'compile-command)
  143. (smart-compile-string function))
  144. (call-interactively 'compile)
  145. )
  146. (if (listp function)
  147. (eval function)
  148. ))
  149. (setq alist nil)
  150. (setq not-yet nil)
  151. )
  152. (setq alist (cdr alist)) )
  153. ))
  154. ;; If compile-command is not defined and the contents begins with "#!",
  155. ;; set compile-command to filename.
  156. (if (and not-yet
  157. (not (memq system-type '(windows-nt ms-dos)))
  158. (not (string-match "/\\.[^/]+$" name))
  159. (not
  160. (and (local-variable-p 'compile-command)
  161. compile-command))
  162. )
  163. (save-restriction
  164. (widen)
  165. (if (equal "#!" (buffer-substring 1 (min 3 (point-max))))
  166. (set (make-local-variable 'compile-command) name)
  167. ))
  168. )
  169. ;; compile
  170. (if not-yet (call-interactively 'compile) )
  171. ))
  172. (defun smart-compile-string (arg)
  173. "Document forthcoming..."
  174. (if (and (boundp 'buffer-file-name)
  175. (stringp buffer-file-name))
  176. (let ((rlist smart-compile-replace-alist)
  177. (case-fold-search nil))
  178. (while rlist
  179. (while (string-match (caar rlist) arg)
  180. (setq arg
  181. (replace-match
  182. (eval (cdar rlist)) t nil arg)))
  183. (setq rlist (cdr rlist))
  184. )
  185. ))
  186. arg)
  187. (provide 'smart-compile)
  188. ;;; smart-compile.el ends here