分享
 
 
 

自动在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)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有