2011-08-25 52 views
0

我是emacs的新手,試圖弄清楚是否有一種「簡單」的方法來編寫將爲標準函數規範創建模板的宏(請參閱下面的#行)。例如,我想執行一個命令來提取輸入和輸出變量,並放置在上述函數這個標準格式(使用R語言):用於製作模板的宏

#This function does something 
#Input: 
# var1 - h 
# var2 - 
# var3 - 
# var4 - 
# Output: 
# myoutput - 
MyFunction <- function(var1,var2,var3=13,var4=NULL){ 
... 
... 
return(myoutput) 
} 

回答

1

我不知道R所以我只是猜測它從你的一個例子看起來如何。添加到您的Emacs初始化文件(並對其進行評估或重新啓動),去一個函數定義行和M-X我-R-插入函數模板

(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"))))) 
0

我不使用ř但它看起來像ESSr-autoyas可以用來做你想做的。它使用YASnippet(Emacs的模板包)。