我是emacs的新手,并试图弄清楚是否有一种“简单”的方法来编写一个宏,它将为标准函数规范创建一个模板(参见下面的#行)。例如,我想执行一个命令来提取输入和输出变量,并将它们放在函数上方的这种标准格式中(使用R语言):
#This function does something
#Input:
# var1 - h
# var2 -
# var3 -
# var4 -
# Output:
# myoutput -
MyFunction <- function(var1,var2,var3=13,var4=NULL){
...
...
return(myoutput)
}
答案 0 :(得分:1)
我不知道R所以我只是猜测你的一个例子看起来如何。将其添加到您的Emacs init文件(并评估或重新启动),转到函数定义行和 M-x my-r-insert-function-template :
(defun my-r-insert-function-template ()
"Insert a function template."
(interactive)
(let (name inputs output pos)
(beginning-of-line)
(save-excursion
(when (re-search-forward "\\([a-zA-Z0-9_\\.]+\\)\\s-*<-\\s-*function\\s-*(" nil t)
(setq name (match-string-no-properties 1))
(backward-char)
(forward-sexp)
(setq pos (1- (point)))
(backward-sexp)
(while (re-search-forward "[a-zA-Z0-9_\\.]+" pos 'go)
(push (match-string-no-properties 0) inputs)
(search-forward "," pos 'go))
(search-forward "{")
(setq pos (point))
(backward-char)
(forward-sexp)
(when (re-search-backward "return\\s-*(\\s-*\\([a-zA-Z0-9\\.]+\\)" pos t)
(setq output (match-string-no-properties 1)))))
(when name
(insert "# " name " : This function does something\n")
(when inputs
(insert "# Input:\n")
(setq inputs (nreverse inputs))
(dolist (input inputs)
(insert "# " input " -\n")))
(when output
(insert "# Output:\n")
(insert "# " output " -\n")))))
答案 1 :(得分:0)
我不使用R,但看起来ESSr-autoyas可以用来做你想要的。它使用YASnippet(Emacs的模板包)。