emacs 配置编译

来源:互联网 发布:mac删除战网 编辑:程序博客网 时间:2024/04/27 18:19

emacs complation

emacs ide

emacs经过配置可以编程一个集成开发环境,从ide最原始的功能开始,编译运行

几个emacs内置函数

(compile "string") ; 编译函数 (concat "string1" "string2") ; 连接字符串函数(global-set-key [] 'function-name) ; 键绑定函数(file-name-extension) ; 文件扩展名函数

配置按照文件扩展名编译

基本思想: 定义变量 extension 来保存当前文件扩展名, 定义变量 compstr 来保存 编译命令
使用emacs内置函数 (eql extension "file-extension") 来执行判断,如果检测到当前文件的扩展名匹配,就把 compstr 赋为对应的操作命令
最后调用compile命令执行相应的命令

(defun compile-buffer ()  "Compile current buffer file."  (interactive)  (let ((file (buffer-file-name)) base-name)    (if (not file)        (message "No file associated with this buffer!")      (setq base-name (file-name-nondirectory file))      (let ((extension (file-name-extension file)))        (cond         ((equal extension "cpp")          (compile (format "g++ -g -Wall %s -o %s" file (file-name-base))))         ((equal extension "c")          (compile (format "gcc -g -Wall %s -o %s" file (file-name-base))))         ((equal extension "java")          (compile (format "javac -g %s" file)))         ((equal extension "pl")          (compile (format "perl -w %s" file)))         ((equal extension "py")          (compile (format "python %s" file))))))))
最后用global-set-key 或者 其他的你想要的函数来个这个函数定义一个快捷键即可。

配置按照文件扩展名运行

可以沿用上面的思路,按照当前文件扩展名来配置运行命令,如果是java文件运行的就是 java file  如果是c文件就直接运行file(windows) , linux下 ./file
然后再配置一个键绑定,就可以实现一键编译,一键运行了
注:这个运行是直接在emacs中运行了,没有交互,比如在c中使用了scanf是不会停下来等待输入,而会赋任意值接着运行

几点问题和优化

编译工程

上面的程序只能编译当前文件,不能编译工程,比如c语言,如果一个工程中含有多个文件,这个命令就不能起作用了
可以让上面的编译在最后的 compile 命令前停下来,等待用户,如果直接回车就直接使用compstr编译,如果想要编辑编译命令也可以编辑

编译成功之后自动关闭 *compilation* buffer

如果在编译 python 和 perl 文件时程序的结果会显示在这个buffer中,但是如果是编译c语言,编译成功之后这个buffer还在就有点占空间
可以写一个函数,如果编译成功就自动关闭编译buffer,如果有错误就仍然按照默认设置

使用emacs内置变量 compilation-exit-message-function
Documentation:
If non-nil, called when a compilation process dies to return a status message.
This should be a function of three arguments: process status, exit status,
and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
write into the compilation buffer, and to put in its mode line.
;; Helper for compilation.;; Close the compilation window if there was no error at all.(setq compilation-exit-message-function      (lambda (status code msg)        ;; If M-x compile exists with a 0        (when (and (eq status 'exit) (zerop code))          ;; then bury the *compilation* buffer, so C-x b doesn't go there          (bury-buffer "*compilation*")          ;; and delete the *compilation* window          (delete-window (get-buffer-window (get-buffer "*compilation*"))))          ;; or to choose to return to whatever were looking at before          ;; (replace-buffer-in-windows "*compilation*"))        ;; Always return anticipated result compilation-exit-message-function        (cons msg code)))

当然,你可以再函数,当是 python 或 perl 模式时就不调用这个~


0 0
原创粉丝点击