Easy way to get to function definition (Emacs, Ocaml)

I am coding Ocaml under Emacs ...

I would like to know if there is a shortcut to go to the function definition (where is the cursor). At the moment, for this, I need to look for the name of the function in the entire 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 clearly tedious ...

By the way, I wrote a .annot file.

Can anyone help? Thanks!

+8
emacs ocaml keyboard-shortcuts
source share
4 answers

My ctags(1) (from the exuberant-ctags package) supports OCaml, and Emacs supports ctags when it runs as etags .

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

+4
source share

While you are expecting a better solution (of which there are some, see for example the OCamlSpotter ) man 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 "Cc Cd") 'camldev-goto-def) (define-key tuareg-mode-map (kbd "Cc CSd") 'camldev-goto-spec) 
+3
source share

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

 opam install merlin 

Follow opam instructions to configure the ~ / .emacs file. To complete the setup, you need to provide a .merlin file, which tells you where the source files and assembly files are located, and which packages are used in the project. A quick overview of the .merlin file is available at https://github.com/the-lambda-church/merlin/wiki/emacs-from-scratch#configuring-your-project

Now, to move on to defining a function in Emacs:

 Cc Cl 

To return to the function call:

 Cc & 
+2
source share

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

On the project page:

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

To use it, try something like:

 otags -r src/ 

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

You can install otags with opam .

0
source share

All Articles