Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置函数声明格式的Emacs规则_C_Emacs_Code Formatting - Fatal编程技术网

设置函数声明格式的Emacs规则

设置函数声明格式的Emacs规则,c,emacs,code-formatting,C,Emacs,Code Formatting,我正在尝试定义一个命令来自定义emacs中的C/C++函数定义 我想得到的是自动重新格式化代码,使其看起来像这样: type func(type arg1, type arg2, ... type argN) { 从通用声明开始,例如: type func(type arg1, type arg2, ..., type argN) { 我的想法是搜索函数定义的特定模式,并在每个逗号后用新行替换它本身,然后调整缩进 我把regexp之

我正在尝试定义一个命令来自定义emacs中的C/C++函数定义

我想得到的是自动重新格式化代码,使其看起来像这样:

type func(type arg1,
          type arg2,
          ...
          type argN) {
从通用声明开始,例如:

type func(type arg1, type arg2, ..., type argN) {
我的想法是搜索函数定义的特定模式,并在每个逗号后用新行替换它本身,然后调整缩进

我把regexp之类的东西弄乱了一段时间,但我什么都想不出来

我不知道如何在使用正则表达式获得的字符串中正确执行替换

接下来是我到目前为止得到的一切,基本上什么都没有

(defun fix-arg-definition ()
  (interactive)
  (goto-char (point-min))
  (replace-regexp "([^,()]+\\([,][^,()]+\\)+)[ ]*{" "WHAT TO PUT HERE?")
)
我对在emacs中定制编码风格是完全陌生的,事实证明这比我想象的要困难得多。感谢您的帮助

更新

我已经设法得到了一些似乎有效的东西,尽管我仍然要尝试彻底测试它

(defun fix-args ()
  (interactive)
  (goto-char 1)
     (while (search-forward-regexp "\\(([^,()]+\\([,][^,()]+\\)+)[ ]*{\\)" nil t) 
     (replace-match (replace-commas) t nil)))

(defun replace-commas ()
   (let (matchedText newText)
       (setq matchedText
       (buffer-substring-no-properties
       (match-beginning 1) (match-end 1)))
   (setq newText
   (replace-regexp-in-string "," ",\n" matchedText) )
 newText))

我可以接受这一点,然后用另一个命令手动调整缩进,至少现在是这样。

您可以通过录制键盘宏轻松做到这一点:

这是我之前准备的一个,通过
编辑命名的kbd宏
显示

;; Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: M-C-a C-SPC M-C-n <<replace-regexp>> , RET , C-q LFD RET M-C-a C-SPC M-C-n TAB

Command: c-split-formal-params 
Key: none

Macro:

M-C-a           ;; c-beginning-of-defun
C-SPC           ;; set-mark-command
M-C-n           ;; forward-list
<<replace-regexp>>  ;; replace-regexp
,           ;; c-electric-semi&comma
RET         ;; newline
,           ;; c-electric-semi&comma
C-q         ;; quoted-insert
LFD         ;; newline-and-indent
RET         ;; newline
M-C-a           ;; c-beginning-of-defun
C-SPC           ;; set-mark-command
M-C-n           ;; forward-list
TAB         ;; c-indent-line-or-region
;;键盘宏编辑器。按C-C-C完成;按C-x k RET取消。
;; 原始键:M-C-a C-SPC M-C-n、RET、C-q LFD RET M-C-a C-SPC M-C-n选项卡
命令:c-split-formal-params
关键字:无
宏:
M-C-a;;c-开始-结束
C-SPC;;设置标记命令
M-C-n;;转发列表
;; 替换regexp
,           ;; c-电子-半逗号
RET;;新线
,           ;; c-电子-半逗号
C-q;;引用插入
LFD;;换行和缩进
RET;;新线
M-C-a;;c-开始-结束
C-SPC;;设置标记命令
M-C-n;;转发列表
标签;;c-缩进线或区域
更好地理解结构导航命令,如
defun的开始
转发列表
也会对您有所帮助


有关键盘宏的更深入讨论,请参阅手册和我的答案。

有关可能有助于查看旧SO答案的两个外部工具。谢谢。我将试着进一步调查这个问题。一开始我觉得很迷茫,真的不知道从哪里开始。我添加了一个链接到我的一个答案。如果你不确定的话,请从手册开始。我必须澄清:我的意思是“首先”,在这里发布和获得一些提示之前。我真的很感谢你的帮助,因为现在我有了一个起点来加深我的“知识”。