Chinaunix首页 | 论坛 | 博客
  • 博客访问: 450329
  • 博文数量: 88
  • 博客积分: 2677
  • 博客等级: 少校
  • 技术积分: 893
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-13 08:01
文章分类

全部博文(88)

文章存档

2017年(3)

2016年(1)

2012年(4)

2011年(4)

2010年(57)

2009年(19)

我的朋友

分类: LINUX

2010-01-17 01:22:57

当各位已经正确的配置了Emacs + global 或称之为 Emacs + gtags 之后,就可以使用下记内容了。
(可参考:http://blog.chinaunix.net/u3/98822/showart_2137437.html)

这个是什么?

在Emacs上编辑C,C++等的源代码时,当保存文件的时候,会在bufferwindow上,自动地实行make命令。

< 优点 >:
  • 不必逐一的在终端中进行make,提高开发效率
  • 因为在保存的时候被执行,所以早期的发现编译错误,提高开发效率

动作感觉


通过'C-x C-s'进行保存,即自动实行。

如果编译结束的话,表示OK(如果发生错误,则表示错误)

f:id:higepon:20061107173959p:image


安装方法

将最下方的auto-compile.el文件的代码保存后,放入目录中。

.emacs文件中追加:
 

(require 'auto-compile)

;; auto-compile动作有效的path的设置

(setq auto-compile-target-path-regexp-list (list "Linux" "FreeBSD" "Foo\/src"))


例如:
你的源程序是
/home/xxx/source/hello/hello.c hello.h Makefile
将auto-compile动作有效的path设置为:

(setq auto-compile-target-path-regexp-list (list "/home/xxx/source/hello" "随意的另一组源文件" "再另一组源文件"))


※※ 附加功能
GNU Global的GTAGS的自动更新。
(每次对源代码修改后,都要'global -uv'更新G* 4个文件,很烦吧!)



※下记内容为源文件和插图



auto-compile.el文件的源代码

;;; auto-compile.el --- auto compile support for developers.

;; Copyright (C) 2006 Higepon

;; Version: 0.0.1
;; Author: Higepon
;; Maintainer: Higepon
;; Require : compile.el

;; History

;;; Commentary:

;;; What is auto-compile?
;;; When you are writing "hello.c" source code with Emacs.
;;; After you save file with C-x C-s, "make" command runs automatically on background.
;;; So you have no need to change Window to terminal,enter make command, and back to Emacs.

;;; Install
;; (require 'auto-compile)
;; ;; enable auto-compile for your projects
;; (setq auto-compile-target-path-regexp-list (list "Linux" "FreeBSD" "Foo\/src"))

;;; Variables
(defvar auto-compile-auto-update-gtags t "*update gtags if t")
(defvar auto-compile-target-file-regexp-list (list "\.cpp$" "\.h$" "\.c$" "\.asm$") "*target file type")
(defvar auto-compile-target-path-regexp-list (list "src") "*target file type")

;;; Code
(defun auto-compile-after-save-hook ()
  (if (and (auto-compile-target-path-p) (auto-compile-target-file-type-p))
      (progn
        (if auto-compile-auto-update-gtags
            (auto-compile-update-gtags))
        (auto-compile-do-compile))))

(defun auto-compile-do-compile ()
  (let ((makefile (expand-file-name "Makefile")))
    (if (file-exists-p makefile)
      (auto-compile-silent-compile makefile "make" 'auto-compile-do-compile-finish-function))))

(defun auto-compile-do-compile-finish-function (buffer result)
  (if (string-match "abnormally" result)
      (progn
        (setq compilation-finish-function nil)
        (auto-compile-show-compile-error result))
    (message "[auto-compile]:compile ok.")))

;;; Code utilities
(defun auto-compile-show-compile-error (error)
  "show compile result"
  (replace-regexp-in-string "\n" "" error)
  (message "[auto-compile]:%s" error))

(defun auto-compile-silent-compile (makefile command finish-function)
  "Compile with minimum window height."
  (let ((save-height compilation-window-height))
    (save-current-buffer
      (setq compilation-window-height 1)
      (setq compilation-finish-function finish-function)
      (set-buffer (find-file-noselect makefile))
      (ad-activate-regexp "auto-compile-yes-or-no-p-always-yes")
      (message "[auto-compile]:%s at %s" command makefile)
      (compile command)
      (ad-deactivate-regexp "auto-compile-yes-or-no-p-always-yes")
      (setq compilation-window-height save-height))))

(defun auto-compile-target-path-p ()
  "Current buffer is target?"
  (auto-compile-list-or
   (mapcar (lambda (x) (string-match x (buffer-file-name)))
           auto-compile-target-path-regexp-list)))

(defun auto-compile-target-file-type-p ()
  "Current buffer is source file?"
  (auto-compile-list-or
   (mapcar (lambda (x) (string-match x (buffer-file-name)))
           auto-compile-target-file-regexp-list)))

(defun auto-compile-list-or (list)
  (if (consp list)
      (or (car list) (auto-compile-list-or (cdr list)))))

(defun auto-compile-update-gtags ()
  "Update GTAGS file"
  (let ((status (call-process "global" nil nil nil "-uv")))
    (if (= status 0)
        (message "[auto-compile]:GTAGS updated"))))

(defun auto-compile-cleanup ()
  "Clean up add-hooks for auto-compile.el."
  (remove-hook 'after-save-hook 'auto-compile-after-save-hook)
  (setq compilation-finish-function nil))

(defadvice yes-or-no-p (around auto-compile-yes-or-no-p-always-yes)
  "Return always yes."
  (setq ad-return-value t))

(defadvice compilation-start (around auto-compile-compilation-start)
  (message "[auto-compile]:now compiling")
  ad-do-it)

;;; Code install
(auto-compile-cleanup)
(add-hook 'after-save-hook 'auto-compile-after-save-hook)
(ad-activate-regexp "auto-compile-compilation-start")

(provide 'auto-compile)



阅读(2106) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~