|
|
自动在c++中插入成员函数 (emacs 扩展) |
|
|
作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站 |
总算花了几天功夫看了一下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)

|
|
相关文章:相关软件: |
|