Emacs - compile command
authorPeng Li <seudut@gmail.com>
Wed, 13 Sep 2017 07:32:29 +0000 (15:32 +0800)
committerPeng Li <seudut@gmail.com>
Wed, 13 Sep 2017 07:44:18 +0000 (15:44 +0800)
emacs.d/config.org

index 8629ba2..38a77d1 100644 (file)
@@ -2187,20 +2187,21 @@ Change the compile-command to set the default command run when call =compile=
 Mapping =s-r= (on Mac, it's =Command + R= to run the script. Here =current-prefix-arg= is set
 to call =compilation=  interactively.
 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
-  (defun my-perl-hook ()
-    (progn
-      (setq-local compilation-read-command nil)
-      (set (make-local-variable 'compile-command)
-           (concat "/usr/bin/perl "
-                   (if buffer-file-name
-                       (shell-quote-argument buffer-file-name))))
-      (local-set-key (kbd "s-r")
-                     (lambda ()
-                       (interactive)
-                                          ;                       (setq current-prefix-arg '(4)) ; C-u
-                       (call-interactively 'compile)))))
-
-  (add-hook 'cperl-mode-hook 'my-perl-hook)
+  ;; this has be implemented in below compile part
+  ;; (defun my-perl-hook ()
+  ;;   (progn
+  ;;     (setq-local compilation-read-command nil)
+  ;;     (set (make-local-variable 'compile-command)
+  ;;          (concat "/usr/bin/perl "
+  ;;                  (if buffer-file-name
+  ;;                      (shell-quote-argument buffer-file-name))))
+  ;;     (local-set-key (kbd "s-r")
+  ;;                    (lambda ()
+  ;;                      (interactive)
+  ;;                                         ;                       (setq current-prefix-arg '(4)) ; C-u
+  ;;                      (call-interactively 'compile)))))
+
+  ;; (add-hook 'cperl-mode-hook 'my-perl-hook)
 #+END_SRC
 
 ** C & C++
@@ -2352,16 +2353,19 @@ irony-company
 #+BEGIN_SRC emacs-lisp :tangle yes :results silent
   (defun my-cpp-hook ()
     (let* ((current-file-name)
-           (a-dot-out-file))
+           (out-file-name))
       (when buffer-file-name
         (setq current-file-name (shell-quote-argument buffer-file-name))
-        (setq a-dot-out-file (concat (file-name-directory buffer-file-name) "a.out")))
+        (setq out-file-name (shell-quote-argument (concat (file-name-sans-extension buffer-file-name) ".out"))))
       (setq-local compilation-read-command t)
       (set (make-local-variable 'compile-command)
            (concat "g++ -Wall "
                    current-file-name
+                   " -o "
+                   out-file-name
                    " && "
-                   a-dot-out-file))
+                   out-file-name
+                   ))
       (local-set-key (kbd "s-r") 'compile)))
 
   (add-hook 'c-mode-hook 'my-cpp-hook)
@@ -2419,7 +2423,9 @@ Set the environments vairables in compilation mode
     (setq compilation-environment (cons "LC_ALL=C" compilation-environment))
     (setq compilation-auto-jump-to-first-error t)
     (setq compilation-auto-jump-to-next t)
-    (setq compilation-scroll-output 'first-error))
+    (setq compilation-scroll-output 'first-error)
+    ;; this will save all the modified buffers before compile
+    (setq compilation-ask-about-save nil))
 
   ;; super-r to compile
   (with-eval-after-load "compile"
@@ -2428,6 +2434,37 @@ Set the environments vairables in compilation mode
     (define-key compilation-mode-map (kbd "p") 'compilation-previous-error)
     (define-key compilation-mode-map (kbd "r") #'recompile))
 
+
+  ;; (loop for (mode . program) in '(
+  ;;                                 (lua-mode-hook . "lua")
+  ;;                                 (perl-mode-hook . "perl")
+  ;;                                 (python-mode-hook . "python")
+  ;;                                 (shell-mode-hook . "sh"))
+  ;;       do (add-hook mode `(lambda ()
+  ;;                           (unless (or (file-exists-p "makefile")
+  ;;                                       (file-exists-p "Makefile"))
+  ;;                             (set (make-local-variable 'compile-command)
+  ;;                                  (concat ,program
+  ;;                                          " "
+  ;;                                          (if buffer-file-name
+  ;;                                              (shell-quote-argument buffer-file-name))))))))
+
+  ;; here note dynamic binding the value of vv, otherwise it will resport error when run the hook.
+  ;; https://emacs.stackexchange.com/questions/10394/scope-in-lambda
+  (dolist (vv '(
+                (cperl-mode-hook . "perl")
+                (lua-mode-hook . "lua")
+                (python-mode-hook . "python")
+                (shell-mode-hook . "sh")))
+    (add-hook (car vv) `(lambda ()
+                          (unless (or (file-exists-p "makefile")
+                                      (file-exists-p "Makefile"))
+                            (set (make-local-variable 'compile-command)
+                                 (concat (cdr ',vv)
+                                         " "
+                                         (if buffer-file-name
+                                             (shell-quote-argument buffer-file-name))))))))
+
   (global-set-key (kbd "s-r") 'compile)
 #+END_SRC