自动在c++中插入成员函数 (emacs 扩展)

王朝网络·c/c++·作者佚名  2006-01-09
宽屏版  字体: |||超大  

总算花了几天功夫看了一下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)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有