关于在Linux下写C++代码的工具,一开始我是用vim的,因为对vim比较熟悉,而且用起来也很顺手。但是vim唯一麻烦的是它只能写code,不能编译,更别说debug了。所以看了一些网络上的介绍,其实Linux下有不少的IDE(Integrated Development Environment)的,其中最受欢迎和使用最广的就是Emacs。
说是在的,一直觉得Emacs挺博大精深的,所以当初也就选了vim而避开了Emacs,想不到现在还是得接触了。在网络上找到这篇不错的文档,专门针对Emacs环境下的C和C++编程的:http://www.caole.net/diary/emacs_write_cpp.html
(附上版权说明,以下的Emacs的配置,是部分参考了上面的这篇文档的,文档的出处是http://www.caole.net/,作者是曹乐,依据
GPL条款,可以自由的修改、传播和使用,但请一定遵循GPL条款。)
根据这篇文档的帮助,首先到这里http://cc-mode.sourceforge.net/下载CC-Mode这个插件,然后按照所附带的README文档的说明安装cc-mode,在shell下采用如下命令安装:
emacs -batch -no-site-file -q -f batch-byte-compile cc-*.el
然后将编译生成的文件copy到emacs的目录下。
在安装好cc-mode之后,按照上面的那个文档所说的,进行如下的设置:
###############################################################################
编辑环境配置
要写C++程序,当然要用到cc-mode插件。CC-Mode原本是支持C语言的,但现在也能支持很多语言,比如C++,Java,Objective -C,CORBA,AWK,Pike等等。CC-Mode是gnu-emacs的标准插件。如果您要求不高,那么默认的配置或许就能满足。CC-Mode 的各种行为都可以自由地定制,您可以参考这里的文档:CC-Mode参考文档
这里是我的.emacs文件中关于CC-Mode配置的部分,仅供参考:
;;;; CC-mode配置 http://cc-mode.sourceforge.net/
(require 'cc-mode)
(c-set-offset 'inline-open 0)
(c-set-offset 'friend '-)
(c-set-offset 'substatement-open 0)
;;;;我的C/C++语言编辑策略
(defun my-c-mode-common-hook()
(setq tab-width 4 indent-tabs-mode nil)
;;; hungry-delete and auto-newline
(c-toggle-auto-hungry-state 1)
;;按键定义
(define-key c-mode-base-map [(control \`)] 'hs-toggle-hiding)
(define-key c-mode-base-map [(return)] 'newline-and-indent)
(define-key c-mode-base-map [(f7)] 'compile)
(define-key c-mode-base-map [(meta \`)] 'c-indent-command)
;; (define-key c-mode-base-map [(tab)] 'hippie-expand)
(define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
(define-key c-mode-base-map [(meta ?/)] 'semantic-ia-complete-symbol-menu)
注意一下,上面最后两行是代码自动补齐的快捷键。后面我会提到代码自动补齐。
;;预处理设置
(setq c-macro-shrink-window-flag t)
(setq c-macro-preprocessor "cpp")
(setq c-macro-cppflags " ")
(setq c-macro-prompt-flag t)
(setq hs-minor-mode t)
(setq abbrev-mode t)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
;;;;我的C++语言编辑策略
(defun my-c++-mode-hook()
(setq tab-width 4 indent-tabs-mode nil)
(c-set-style "stroustrup")
;; (define-key c++-mode-map [f3] 'replace-regexp)
)
################################################################################
再接下来就是修改.emacs,将它配置得符合你的要求。在网上找到了如下的emacs的配置,都不错,可以直接copy来用:
http://www.caole.net/diary/emacs_setting.html
http://preferlinux.yculblog.com/post.358730.html
http://ann77.stu.cdut.edu.cn/EmacsTweak.html
最后杂七杂八的弄了一下,现在我的emacs的配置文件的内容如下:(十分长,而且重复的地方应该不少,没辙,新手,还有许多地方没弄明白)
################################################################################
;; .emacs
;;; uncomment this line to disable loading of "default.el" at startup
;; (setq inhibit-default-init t)
;; turn on font-lock mode
(when (fboundp 'global-font-lock-mode)
(global-font-lock-mode t))
;; enable visual feedback on selections
;(setq transient-mark-mode t)
;; default to better frame titles
(setq frame-title-format
(concat "%b - emacs@" (system-name)))
;; default to unified diffs
(setq diff-switches "-u")
;; always end a file with a newline
;(setq require-final-newline 'query)
;; 显示行号与列号
(setq column-number-mode t)
(setq line-number-mode t)
;; 配色方案
(setq default-frame-alist
'(
(foreground-color . "Wheat")
(background-color . "DarkSlateGray")
(cursor-color . "Orchid")
)
)
;;字体设定
(set-fontset-font
"fontset-default" nil
"-*-simsun-*-*-*-*-18-*-*-*-*-*-gb2312.1980-*" nil 'prepend)
(set-fontset-font
"fontset-courier" 'kana
"-*-simsun-*-*-*-*-18-*-*-*-*-*-gbk-0" nil 'prepend)
(set-fontset-font
"fontset-courier" 'han
"-*-simsun-*-*-*-*-18-*-*-*-*-*-gbk-0" nil 'prepend)
(set-fontset-font
"fontset-courier" 'cjk-misc
"-*-simsun-*-*-*-*-18-*-*-*-*-*-gbk-0" nil 'prepend)
;;语言环境设定
(set-language-environment 'Chinese-GBK)
(set-selection-coding-system 'chinese-gbk)
(set-terminal-coding-system 'chinese-gbk)
(set-keyboard-coding-system 'chinese-gbk)
(set-language-environment 'Chinese-GBK)
(setq locale-coding-system 'chinese-gbk)
(setq current-language-environment "Chinese-GBK")
(set-selection-coding-system 'chinese-gbk)
(prefer-coding-system 'chinese-gbk)
(setq file-coding-system-alist
(list file-coding-system-alist
`(".*" chinese-gbk)))
(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING)
;;;;以下是我的快捷键设置
;; ----------------------- Short Cut -------------------------
(global-set-key [(meta g)] 'goto-line)
;;(global-set-key [(meta ?/)] 'hippie-expand)
;; F12 t 从当前缓冲区建立任务
(global-set-key (kbd "<f12> t") 'planner-create-task-from-buffer)
;; F12 g 切换到当日的任务
;;(global-set-key (kbd "<f12> g") 'planner-goto-today)
(global-set-key (kbd "<f12> g") 'planner-goto-today)
(global-set-key (kbd "<f12> p") 'plan)
;; F12 r 添加当前备忘录
(global-set-key (kbd "<f12> r") 'remember)
;; F12 c 切换到日历
(global-set-key (kbd "<f12> c") 'calendar)
;;;F2:进入wiki-mode模式
(global-set-key [(f2)] 'emacs-wiki-find-file)
;;;F3:切换到dired模式
(global-set-key [(f3)] 'dired)
;;;F4:切换到shell模式
(global-set-key [(f4)] 'ansi-term)
;;;F5:打开speedbar
(global-set-key [(f5)] 'speedbar)
;;;;F7:调用compile
(global-set-key (kbd "<f7>") 'compile)
;;;Alt+s : 让speedbar获得焦点
(global-set-key (kbd "M-s") 'speedbar-get-focus)
;;;F9:列举我的书签
(global-set-key (kbd "<f9>") 'list-bookmarks)
;;;F10:添加当前页到书签中
(global-set-key [(f10)] 'bookmark-set)
;;;F12 u 更新当前版本
(global-set-key (kbd "<f12> u") 'vc-next-action)
;;------------------------End of Short Cut ---------------------)
;;;; 显示时间
(setq display-time-24hr-format t)
(setq display-time-day-and-date t)
(display-time)
;;;; 关闭启动画面
(setq inhibit-startup-message t)
;;;;设置大的kill ring
(setq kill-ring-max 150)
;; 设置 hippie-expand 很好用的功能。 M-x hippie-expand
(setq hippie-expand-try-functions-list
'(
try-expand-dabbrev
try-expand-dabbrev-visible
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-file-name-partially
try-complete-file-name
try-expand-all-abbrevs
try-expand-list
try-expand-line
try-complete-lisp-symbol-partially
try-complete-lisp-symbol))
;;;;自动补齐策略
(defun my-indent-or-complete ()
(interactive)
(if (looking-at "\\>")
(hippie-expand nil)
(indent-for-tab-command))
)
(global-set-key [(control tab)] 'my-indent-or-complete)
(autoload 'senator-try-expand-semantic "senator")
(setq hippie-expand-try-functions-list
'(
senator-try-expand-semantic
try-expand-dabbrev
try-expand-dabbrev-visible
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-expand-list
try-expand-list-all-buffers
try-expand-line
try-expand-line-all-buffers
try-complete-file-name-partially
try-complete-file-name
try-expand-whole-kill
)
)
;;;; CC-mode配置 http://cc-mode.sourceforge.net/
(require 'cc-mode)
(c-set-offset 'inline-open 0)
(c-set-offset 'friend '-)
(c-set-offset 'substatement-open 0)
;;;;根据后缀判断所用的mode
;;;;注意:我在这里把.h关联到了c++-mode
(setq auto-mode-alist
(append '(("\\.h$" . c++-mode)) auto-mode-alist))
;;;;我的C/C++语言编辑策略
(defun my-c-mode-common-hook()
(setq tab-width 4 indent-tabs-mode nil)
;;; hungry-delete and auto-newline
(c-toggle-auto-hungry-state 1)
;;按键定义
(define-key c-mode-base-map [(control \`)] 'hs-toggle-hiding)
(define-key c-mode-base-map [(return)] 'newline-and-indent)
(define-key c-mode-base-map [(f7)] 'compile)
(define-key c-mode-base-map [(f8)] 'ff-get-other-file)
(define-key c-mode-base-map [(meta \`)] 'c-indent-command)
;; (define-key c-mode-base-map [(tab)] 'hippie-expand)
(define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
(define-key c-mode-base-map [(meta ?/)] 'semantic-ia-complete-symbol-menu)
;;预处理设置
(setq c-macro-shrink-window-flag t)
(setq c-macro-preprocessor "cpp")
(setq c-macro-cppflags " ")
(setq c-macro-prompt-flag t)
(setq hs-minor-mode t)
(setq abbrev-mode t)
(setq tab-width 4 indent-tabs-mode nil)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
;;;;我的C++语言编辑策略
(defun my-c++-mode-hook()
(setq tab-width 4 indent-tabs-mode nil)
(c-set-style "stroustrup")
;; (define-key c++-mode-map [f3] 'replace-regexp)
)
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
;;;;C/C++语言启动时自动加载semantic对/usr/include的索引数据库
(setq semanticdb-search-system-databases t)
(add-hook 'c-mode-common-hook
(lambda ()
(setq semanticdb-project-system-databases
(list (semanticdb-create-database
semanticdb-new-database-class
"/usr/include")))))
;;;;退出Emacs时保存所有正在编辑的文档
(load "desktop")
(desktop-save-mode)
(desktop-read)
评论