2011-11-14 36 views
8

我在Emacs下编码ocaml的...简单的方法去一个功能(Emacs的,ocaml的)定义

我想知道是否有一个快捷方式跳转到一个函数的定义(光标在哪里)。目前,这样做我有搜索功能的名称在整个文件中,或者寻找let the_name_of_the_functionlet rec the_name_of_the_functionand the_name_of_the_function这显然是乏味......

顺便说一句,我有alreay文件.annot

任何人都可以帮忙吗?谢谢!

回答

4

我的ctags(1)(来自exuberant-ctags包)支持OCaml语言,当Emacs作为etags执行时,Emacs支持ctags

所以尝试:cd /path/to/Ocaml/sources/ && etags -R .打造内emacs索引,然后,中号 - ret搜索光标下的标签。

+0

对不起,我应该在哪里运行'的ETag -R .'? – SoftTimur

+1

更新了答案:您需要从任何目录拥有所有来源运行'etags -R .'。 – sarnold

+0

终端下的'etags -R .'给出'etags:跳过::它不是普通的文件.'。我尝试了'etags -R file.ml',没有生成新文件,并且在Emacs下的M-。'给出了[[不匹配]​​] ...“。 – SoftTimur

3

当您在等待更好的解决方案(其中有些解决方案(例如,请参阅OCamlSpotter))时,您可以使用下面列出的穷人命令。假设图阿雷格模式。

(defun camldev-identifier-at-point() 
    (interactive) 
    (save-excursion 
    (goto-char (1+ (point))) 
    (let* ((beg (re-search-backward "[^A-Za-z0-9_'][A-Za-z0-9_'`]")) 
      (beg (1+ beg))) 
     (goto-char (1+ beg)) 
     (let* ((end (re-search-forward "[^A-Za-z0-9_']")) 
      (end (1- end))) 
     (buffer-substring beg end))))) 

(defun camldev-goto-def() 
    "Search for definition of word around point." 
    (interactive) 
    (let (goal (word (camldev-identifier-at-point))) 
    (save-excursion 
     (re-search-backward (concat "\\(let \\([^=]*[^A-Za-z0-9_']\\|\\)" 
            word "\\([^A-Za-z0-9_'][^=]*\\|\\)=\\|" 
            "fun \\([^-]*[^A-Za-z0-9_']\\|\\)" 
            word "\\([^A-Za-z0-9_'][^-]*\\|\\)->\\|" 
            "and \\([^=]*[^A-Za-z0-9_']\\|\\)" 
            word "\\([^A-Za-z0-9_'][^=]*\\|\\)=\\)" 
           )) 
     (re-search-forward (concat "[^A-Za-z0-9_']" word "[^A-Za-z0-9_']")) 
     (setq goal (1+ (match-beginning 0)))) 
    (push-mark) 
    (goto-char goal) 
    )) 

(defun camldev-goto-spec() 
    "Search for specification in mli/ml file of word around point in ml/mli file." 
    (interactive) 
    (let* (goal 
     (word (camldev-identifier-at-point)) 
     (search-expr (concat "\\(val [^:\n]*" 
           word "[^:]*:\\|" 
           "let [^=\n]*" 
           word "[^=]*=\\|" 
           "type [^=\n]*" 
           word "[^=]*=\\)" 
          ))) 
    (tuareg-find-alternate-file) 
    (save-excursion 
     (goto-char (point-min)) 
     (re-search-forward search-expr) 
     (setq goal (match-beginning 0))) 
    (push-mark) 
    (goto-char goal) 
    )) 

(define-key tuareg-mode-map (kbd "C-c C-d") 'camldev-goto-def) 
(define-key tuareg-mode-map (kbd "C-c C-S-d") 'camldev-goto-spec) 
0

您可以尝试otags位于: http://askra.de/software/otags/

从项目页面:

Otags生成标签文件适用于Emacs和来自OCaml的 来源VI/vim的。 Otags使用camlp4进行分析。

要使用它,你可以试试:

otags -r src/ 

其中src是包含您OCaml的源文件的子目录。 它应该创建一个TAGS文件。 然后你应该可以在Emacs中做M-.

您可以安装otagsopam

2

这个问题可以用merlin解决(https://github.com/the-lambda-church/merlin)。 Merlin可以通过opam轻松安装:

opam install merlin 

按照opam提供的说明配置〜/ .emacs文件。为了完成配置,你必须提供一个.merlin文件,告诉merlin源文件和构建文件的位置以及项目中使用的包。简要概述。梅林文件在https://github.com/the-lambda-church/merlin/wiki/emacs-from-scratch#configuring-your-project

给予现在,跳转到函数定义在Emacs:

C-c C-l 

要返回函数调用:

C-c &