自动在c++中插入成员函数 (emacs 扩展)
2005-04-07 17:04
405 查看
总算花了几天功夫看了一下elisp语言,括号看得头晕,安耐不住写了一个elisp扩展,现在感觉看一般简单的lisp也不是那么痛苦了。
用法:可以在c++头文件中,将光标放在当前函数行,然后按C-c i在对应的源文件中插入成员函数。
支持多层namespace 和 class。不过还不是十分可靠,例如对默认参数值的支持等。
(defun cpp-current-scope()
"If the point is in a class/namespace/struct definition, gets the
full scope path. Return nil otherwise."
(interactive)
(let (syntax-list scope ( classnames ()))
(save-excursion
;;ensure we aren't in the arguments list
(beginning-of-line)
(c-end-of-statement)
(c-beginning-of-statement-1)
;;now point is at beginning of the funciton declaration,
;;call c-guest-basic-syntax to find if the car is inclass or innamespace
(setq syntax-list (c-guess-basic-syntax))
;;expected format ' ( (inclass 12) | (innamespace 22) ...))
(while (or (eq 'inclass (car (car syntax-list)))
(eq 'innamespace (car (car syntax-list))))
;; find outside the class / namespace name
(goto-char (elt (car syntax-list) 1))
(backward-word)
(setq classnames (cons (current-word) classnames))
;;while loop, check outside scope again
(setq syntax-list (c-guess-basic-syntax)))
;;concat the scope list like ns1::ns2::class1::
(dolist (e classnames scope)
(setq scope (concat scope e "::"))))))
(defun cpp-current-function()
"check current line functon declaration, return a list, the 0th is the
rettype, 1st is function name,
2th is the arg list, 3th is modifier
such as '('void' 'foo' 'int a, int b' 'const' )"
(interactive)
(save-excursion
(let (string list rettype func argument (modifier "")
start end)
(beginning-of-line)
(setq end (progn (c-end-of-statement) (point)))
(setq start (progn (c-beginning-of-statement-1) (point)))
(when (search-forward "(" end t)
(setq string (buffer-substring-no-properties start (1- (point))))
(setq start (point))
(when (search-forward ")" end t)
(setq argument (buffer-substring-no-properties start (1- (point))))
(setq modifier (buffer-substring-no-properties (point) (1- end)))
(setq list (split-string string))
(setq func (elt list (1- (length list))))
(if (> (length list) 2)
(setq rettype (elt list 1))
(if (= (length list) 1)
;;no rettype, construction or destruction
(setq rettype "")
(setq rettype (car list))))
(setq list (list rettype func argument modifier)))))))
(defun cpp-insert-new-method ()
"Insert a c++ member function definition into the corresponding c++ source file.
try to find the definition if exists"
(interactive)
(let (insertstr (classname (cpp-current-scope))
(phototype (cpp-current-function)))
(if (or (null classname) (null phototype))
(message "Cann't insert cpp member function!")
(ff-find-related-file)
(setq insertstr (concat
(elt phototype 0)
(unless (string= "" (elt phototype 0)) " ")
classname
(elt phototype 1) "(" (elt phototype 2) ")"
(elt phototype 3)))
(beginning-of-buffer)
(unless (search-forward insertstr (point-max) t)
(end-of-buffer)
(insert "/n/n")
(end-of-buffer)
(insert insertstr)
(insert "/n{/n")
(c-indent-defun)
(insert "}/n")
(backward-char 3)
(insert "/n")
(c-indent-line-or-region)))))
(global-set-key "/C-ci" 'cpp-insert-new-method)
用法:可以在c++头文件中,将光标放在当前函数行,然后按C-c i在对应的源文件中插入成员函数。
支持多层namespace 和 class。不过还不是十分可靠,例如对默认参数值的支持等。
(defun cpp-current-scope()
"If the point is in a class/namespace/struct definition, gets the
full scope path. Return nil otherwise."
(interactive)
(let (syntax-list scope ( classnames ()))
(save-excursion
;;ensure we aren't in the arguments list
(beginning-of-line)
(c-end-of-statement)
(c-beginning-of-statement-1)
;;now point is at beginning of the funciton declaration,
;;call c-guest-basic-syntax to find if the car is inclass or innamespace
(setq syntax-list (c-guess-basic-syntax))
;;expected format ' ( (inclass 12) | (innamespace 22) ...))
(while (or (eq 'inclass (car (car syntax-list)))
(eq 'innamespace (car (car syntax-list))))
;; find outside the class / namespace name
(goto-char (elt (car syntax-list) 1))
(backward-word)
(setq classnames (cons (current-word) classnames))
;;while loop, check outside scope again
(setq syntax-list (c-guess-basic-syntax)))
;;concat the scope list like ns1::ns2::class1::
(dolist (e classnames scope)
(setq scope (concat scope e "::"))))))
(defun cpp-current-function()
"check current line functon declaration, return a list, the 0th is the
rettype, 1st is function name,
2th is the arg list, 3th is modifier
such as '('void' 'foo' 'int a, int b' 'const' )"
(interactive)
(save-excursion
(let (string list rettype func argument (modifier "")
start end)
(beginning-of-line)
(setq end (progn (c-end-of-statement) (point)))
(setq start (progn (c-beginning-of-statement-1) (point)))
(when (search-forward "(" end t)
(setq string (buffer-substring-no-properties start (1- (point))))
(setq start (point))
(when (search-forward ")" end t)
(setq argument (buffer-substring-no-properties start (1- (point))))
(setq modifier (buffer-substring-no-properties (point) (1- end)))
(setq list (split-string string))
(setq func (elt list (1- (length list))))
(if (> (length list) 2)
(setq rettype (elt list 1))
(if (= (length list) 1)
;;no rettype, construction or destruction
(setq rettype "")
(setq rettype (car list))))
(setq list (list rettype func argument modifier)))))))
(defun cpp-insert-new-method ()
"Insert a c++ member function definition into the corresponding c++ source file.
try to find the definition if exists"
(interactive)
(let (insertstr (classname (cpp-current-scope))
(phototype (cpp-current-function)))
(if (or (null classname) (null phototype))
(message "Cann't insert cpp member function!")
(ff-find-related-file)
(setq insertstr (concat
(elt phototype 0)
(unless (string= "" (elt phototype 0)) " ")
classname
(elt phototype 1) "(" (elt phototype 2) ")"
(elt phototype 3)))
(beginning-of-buffer)
(unless (search-forward insertstr (point-max) t)
(end-of-buffer)
(insert "/n/n")
(end-of-buffer)
(insert insertstr)
(insert "/n{/n")
(c-indent-defun)
(insert "}/n")
(backward-char 3)
(insert "/n")
(c-indent-line-or-region)))))
(global-set-key "/C-ci" 'cpp-insert-new-method)
相关文章推荐
- Emacs配置C/C++-mode的代码智能提示和自动补全
- 手把手教你emacs cedet C/C++自动补全
- Visual C++ 编译器自动假定带 .C 扩展名的文件是 C 文件而不是 C++ 文件,并且拒绝 C++ 语法和关键字(c语言只能在大括号最前面申明变量)
- emacs使用melpa自动安装扩展
- Emacs-156-插入新行的时候不使用自动对齐功能
- emacs自动补全插件auto-complet和yasnippet,安装、配置和扩展
- emacs下C/C++自动补全,Yasnippet && auto-complete && auto-complete-clang-async
- emacs完美的C++的自动补全
- 验证C++移位运算符,符号位的溢出效果,符号位的自动扩展与不自动扩展的情况
- Emacs配置C/C++-mode的代码智能提示和自动补全
- Emacs快捷键自动插入当前时间
- vim自动插入C++和C函数头的脚本
- emacs自动补全插件auto-complet和yasnippet,安装、配置和扩展
- C++Builder对C++的扩展
- Excel vba开发 合并单元格拆分自动填充功能、在每一条数据后面插入空白行、将地理坐标的经纬度转换成度等功能
- BNF范式 如何用C++实现自动推导
- 扩展 TreeView 实现选择 CheckBox 自动回发
- 如何在linux 下c++中类的成员函数中创建多线程
- C++对C的函数的扩展==》函数参数相关扩展
- 在 c++ 操作 execl 中 在固定位置插入sheet页和释放sheet页的方法