2011-07-31 65 views
9

插入原型的功能如何配置的emacs從.H自動插入原型功能打開相應的.cc文件時?自動從.H使用Emacs

+0

呃,當然是有宏。但我認爲你真的在問「誰寫了這樣的宏,所以我不必?」我個人從未將最重要的工作留給工具。我現在會離開。 –

+0

@Hans Passant:你能解釋一下爲什麼這個工具不好嗎?我只是不想重複鍵入相同的功能...另外,如果你知道一個有用的宏,你可以指導我的源?也許我可以修改它以適應我的需求。 – Mark

+0

@Mark - 猜猜漢斯有剪貼的編輯,所以他不必輸入兩遍。 –

回答

3

我有這樣的事情,我用來做這個的時候我在做更多的C++編碼,使用成員函數包:

(require 'member-function) 

;;expand member functions automatically when entering a cpp file 
(defun c-file-enter() 
    "Expands all member functions in the corresponding .h file" 
    (let* ((c-file (buffer-file-name (current-buffer))) 
     (h-file-list (list (concat (substring c-file 0 -3) "h") 
          (concat (substring c-file 0 -3) "hpp") 
          (concat (substring c-file 0 -1) "h") 
          (concat (substring c-file 0 -1) "hpp")))) 
    (if (or (equal (substring c-file -2) ".c") 
      (equal (substring c-file -4) ".cpp")) 
     (mapcar (lambda (h-file) 
        (if (file-exists-p h-file) 
         (expand-member-functions h-file c-file))) 
       h-file-list)))) 

(add-hook 'c++-mode-hook c-file-enter) 

您可以找到成員functions.el: http://www.emacswiki.org/emacs/member-functions.el

+0

這看起來很有前途。當我回家時我會嘗試。 – Mark

+0

我有一些麻煩字節編譯member-functions.el。獲取錯誤:符號的值作爲變量是void:warning-suppress-types – Mark

+0

得到了解決方法簡單插入(setq warning-suppress-types nil)到.emacs中 – Mark

1

我認爲,你可以用CEDET的參議員「複製標籤」結合autoinsert包,但是這將需要一些elisp的程序打開文件,它的動力解析,然後再遍歷功能標籤...

儘管如此,如果你想複製整個.H,它會更容易,但也將涉及的elisp - 你需要通過auto-insert功能插入空文件,然後使用操作功能,也就是第二個元素在auto-insert-alist變量拷貝.h文件中的內容。功能看起來像這樣:

(defun my-auto-insert-h-content() 
    (when buffer-file-name 
    (let ((h-filename (concat (file-name-sans-extension buffer-file-name) ".h"))) 
     (when (file-exists-p h-filename) 
     (goto-char (point-min)) 
     (insert-file-contents h-filename))))) 
+0

你能給我一些關於如何做第一部分的提示嗎?我之前使用過lisp,所以我不介意寫宏... – Mark

+0

在https://github.com/alexott/emacs-configs/blob/master/rc/emacs-rc-auto-insert查看我的配置.el - 你需要提供文件掩碼到cons的映射,由模板文件(我在https://github.com/alexott/emacs-configs/tree/master/auto-insert)和函數組成模板文件插入到新文件後執行... –

4

我寫了這個黑客一陣子......這不是很好,但可能是一個起點。要使用它,開始在.cc文件要插入的功能實現,切換到.h文件,然後轉到函數定義,然後M-x my-c-make-function-from-prototype(我把它綁定到課程的關鍵)。

(require 'ffap) 
(defun my-c-make-function-from-prototype() 
    "Turn a function prototype into a skeleton implementation." 
    (interactive) 
    (let (ret-val fcn-name args const namespaces start-of-fcn) 
    (save-excursion 
     (end-of-line) 
     (c-beginning-of-statement 1) 
     (beginning-of-line) 
     (when (re-search-forward 
      "\\s-*\\(.*\\)\\s-+\\([-a-zA-Z0-9_!=<>~]+\\)\\s-*[(]" nil t) 
     (setq ret-val (match-string 1)) 
     (setq ret-val (replace-regexp-in-string "\\(virtual\\|static\\)\\s-*" "" ret-val)) 
     (setq fcn-name (match-string 2)) 
     (when (re-search-forward "\\([^)]*\\)[)]" nil t) 
      (setq args (match-string 1)) 
      (setq args (replace-regexp-in-string "\\s-*=.+?," "," args)) 
      (setq args (replace-regexp-in-string "\\s-*=.+?)" ")" args)) 
      (setq args (replace-regexp-in-string "\\s-*=.+?$" "" args)) 
      (if (looking-at "\\s-*const") 
       (setq const " const") 
      (setq const "")) 
      (condition-case nil 
       (while 't 
       (backward-up-list 1) 
       (when (re-search-backward 
        "\\(class\\|namespace\\|struct\\)\\s-+\\([a-zA-Z0-9_]+\\)" nil t) 
        (setq namespaces (concat (match-string 2) "::" namespaces)))) 
      (error nil))))) 
    ;; Switch to other file and insert implementation 
    (ff-get-other-file) 
    (setq start-of-fcn (point)) 
    (insert (concat ret-val (unless (string= ret-val "") "\n") namespaces fcn-name "(" args ")" const)) 
    (insert "\n{\n/** @todo Fill in this function. */\n}\n") 
    (unless (eobp) 
     (insert "\n")) 
    (indent-region start-of-fcn (point) nil) 
    (goto-char start-of-fcn) 
    (when (fboundp 'doxymacs-insert-function-comment) 
     (doxymacs-insert-function-comment))))