PageRenderTime 55ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/.emacs.d/elisp/smart-compile.el

https://github.com/nseo/.dotfiles
Emacs Lisp | 226 lines | 149 code | 32 blank | 45 comment | 0 complexity | bae5a6c310ffe3513a083eaa0f96c1e9 MD5 | raw file
  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. (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\\'" . "gfortran %f -o %n")
  50. ("\\.[Ff]\\'" . "gfortran %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. ) "Alist of filename patterns vs corresponding format control strings.
  58. Each element looks like (REGEXP . STRING) or (MAJOR-MODE . STRING).
  59. Visiting a file whose name matches REGEXP specifies STRING as the
  60. format control string. Instead of REGEXP, MAJOR-MODE can also be used.
  61. The compilation command will be generated from STRING.
  62. The following %-sequences will be replaced by:
  63. %F absolute pathname ( /usr/local/bin/netscape.bin )
  64. %f file name without directory ( netscape.bin )
  65. %n file name without extension ( netscape )
  66. %e extension of file name ( bin )
  67. %o value of `smart-compile-option-string' ( \"user-defined\" ).
  68. If the second item of the alist element is an emacs-lisp FUNCTION,
  69. evaluate FUNCTION instead of running a compilation command.
  70. "
  71. :type '(repeat
  72. (cons
  73. (choice
  74. (regexp :tag "Filename pattern")
  75. (function :tag "Major-mode"))
  76. (choice
  77. (string :tag "Compilation command")
  78. (sexp :tag "Lisp expression"))))
  79. :group 'smart-compile)
  80. (put 'smart-compile-alist 'risky-local-variable t)
  81. (defconst smart-compile-replace-alist '(
  82. ("%F" . (buffer-file-name))
  83. ("%f" . (file-name-nondirectory (buffer-file-name)))
  84. ("%n" . (file-name-sans-extension
  85. (file-name-nondirectory (buffer-file-name))))
  86. ("%e" . (or (file-name-extension (buffer-file-name)) ""))
  87. ("%o" . smart-compile-option-string)
  88. ;; ("%U" . (user-login-name))
  89. ))
  90. (put 'smart-compile-replace-alist 'risky-local-variable t)
  91. (defvar smart-compile-check-makefile t)
  92. (make-variable-buffer-local 'smart-compile-check-makefile)
  93. (defcustom smart-compile-make-program "make "
  94. "The command by which to invoke the make program."
  95. :type 'string
  96. :group 'smart-compile)
  97. (defcustom smart-compile-option-string ""
  98. "The option string that replaces %o. The default is empty."
  99. :type 'string
  100. :group 'smart-compile)
  101. ;;;###autoload
  102. (defun smart-compile (&optional arg)
  103. "An interface to `compile'.
  104. It calls `compile' or other compile function,
  105. which is defined in `smart-compile-alist'."
  106. (interactive "p")
  107. (let ((name (buffer-file-name))
  108. (not-yet t))
  109. (if (not name)(error "cannot get filename."))
  110. ;; (message (number-to-string arg))
  111. (cond
  112. ;; local command
  113. ;; The prefix 4 (C-u M-x smart-compile) skips this section
  114. ;; in order to re-generate the compile-command
  115. ((and (not (= arg 4)) ; C-u M-x smart-compile
  116. (local-variable-p 'compile-command)
  117. compile-command)
  118. (call-interactively 'compile)
  119. (setq not-yet nil)
  120. )
  121. ;; make?
  122. ((and smart-compile-check-makefile
  123. (or (file-readable-p "Makefile")
  124. (file-readable-p "makefile")))
  125. (if (y-or-n-p "Makefile is found. Try 'make'? ")
  126. (progn
  127. (set (make-local-variable 'compile-command) "make ")
  128. (call-interactively 'compile)
  129. (setq not-yet nil)
  130. )
  131. (setq smart-compile-check-makefile nil)))
  132. ) ;; end of (cond ...)
  133. ;; compile
  134. (let( (alist smart-compile-alist)
  135. (case-fold-search nil)
  136. (function nil) )
  137. (while (and alist not-yet)
  138. (if (or
  139. (and (symbolp (caar alist))
  140. (eq (caar alist) major-mode))
  141. (and (stringp (caar alist))
  142. (string-match (caar alist) name))
  143. )
  144. (progn
  145. (setq function (cdar alist))
  146. (if (stringp function)
  147. (progn
  148. (set (make-local-variable 'compile-command)
  149. (smart-compile-string function))
  150. (call-interactively 'compile)
  151. )
  152. (if (listp function)
  153. (eval function)
  154. ))
  155. (setq alist nil)
  156. (setq not-yet nil)
  157. )
  158. (setq alist (cdr alist)) )
  159. ))
  160. ;; If compile-command is not defined and the contents begins with "#!",
  161. ;; set compile-command to filename.
  162. (if (and not-yet
  163. (not (memq system-type '(windows-nt ms-dos)))
  164. (not (string-match "/\\.[^/]+$" name))
  165. (not
  166. (and (local-variable-p 'compile-command)
  167. compile-command))
  168. )
  169. (save-restriction
  170. (widen)
  171. (if (equal "#!" (buffer-substring 1 (min 3 (point-max))))
  172. (set (make-local-variable 'compile-command) name)
  173. ))
  174. )
  175. ;; compile
  176. (if not-yet (call-interactively 'compile) )
  177. ))
  178. (defun smart-compile-string (format-string)
  179. "Document forthcoming..."
  180. (if (and (boundp 'buffer-file-name)
  181. (stringp buffer-file-name))
  182. (let ((rlist smart-compile-replace-alist)
  183. (case-fold-search nil))
  184. (while rlist
  185. (while (string-match (caar rlist) format-string)
  186. (setq format-string
  187. (replace-match
  188. (eval (cdar rlist)) t nil format-string)))
  189. (setq rlist (cdr rlist))
  190. )
  191. ))
  192. format-string)
  193. (provide 'smart-compile)
  194. ;;; smart-compile.el ends here