定义函数的简单方法(Emacs、Ocaml)

发布于 2024-12-15 11:59:20 字数 311 浏览 4 评论 0原文

我正在 Emacs 下编写 Ocaml...

我想知道是否有快捷方式可以跳转到函数的定义(光标所在的位置)。目前,要做到这一点,我必须在整个文件中搜索函数的名称,或者查找 let the_name_of_the_functionlet rec the_name_of_the_functionand the_name_of_the_function 这显然很乏味......

顺便说一下,我已经有了文件.annot

有人可以帮忙吗?谢谢你!

I am coding Ocaml under Emacs...

I would like to know if there is a shortcut to jump to the definition of a function (where the cursor is). At the moment, to do so I have to search the name of the function in the whole file, or look for let the_name_of_the_function and let rec the_name_of_the_function and and the_name_of_the_function which is obviously tedious...

By the way, I have alreay the file .annot.

Could anyone help? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一江春梦 2024-12-22 11:59:20

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

所以尝试: cd /path/to/Ocaml/sources/ && etags -R . 构建索引,然后在 emacs 中,M-.ret搜索光标下的标签。

My ctags(1) (from the exuberant-ctags package) supports the OCaml language and Emacs supports ctags when it is executed as etags.

So try: cd /path/to/Ocaml/sources/ && etags -R . to build an index, and then within emacs, M-.ret to search for the tag under the cursor.

一抹苦笑 2024-12-22 11:59:20

这个问题可以用 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 &

The problem could be solved with merlin (https://github.com/the-lambda-church/merlin). Merlin can be installed easily with opam:

opam install merlin

Follow the instructions provided by opam to configure ~/.emacs file. To finish the configuration you will have to provide a .merlin file that tells merlin where the source files and build files are located and which packages are used in the project. A brief overview of the .merlin file is given in https://github.com/the-lambda-church/merlin/wiki/emacs-from-scratch#configuring-your-project

Now, to jump to the function definition in Emacs:

C-c C-l

To move back to the function call:

C-c &
苦笑流年记忆 2024-12-22 11:59:20

当您等待更好的解决方案时(其中有一些,请参见例如 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)

While you are waiting for a better solution (of which there are some, see for example OCamlSpotter) you can use the poor-man commands listed below. Assumes tuareg-mode.

(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)
海的爱人是光 2024-12-22 11:59:20

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

从项目页面:

Otags 从 OCaml 生成适合 emacs 和 vi/vim 的 TAGS 文件
来源。 Otags 使用 camlp4 进行解析。

要使用它,请尝试以下操作:

otags -r src/

其中 src 是包含 OCaml 源文件的子目录。
它应该创建一个 TAGS 文件。
那么您应该能够在 Emacs 中执行 M-.

您可以使用 opam 安装 otags

You can try otags located here:
http://askra.de/software/otags/

From the project page:

Otags generates TAGS files suitable for emacs and vi/vim from OCaml
sources. Otags employs camlp4 for parsing.

To use it, try something like:

otags -r src/

where src is the subdirectory containing your OCaml source files.
It should create a TAGS file.
Then you should be able to do M-. in Emacs.

You can install otags with opam.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文