7

Is it possible to link to a function definition in a source code file, rather than a fixed line number?

1
  • Are you referring to external links like file:mysource_file.c:555? How should the link look like when jumping to a function my_function() in mysource_file.c instead of line 555 in that file? Note that the hook org-open-at-point-functions can be used to customize link behavior. Also new link types can be added using the org-add-link-type function Commented Jun 21, 2017 at 20:46

3 Answers 3

5

Using the suggested global binding C-c l for storing a link when you are visiting any file (not just an org-mode file) and the one for inserting a link into an org file, C-c C-l, should work fine. It does not do the elisp find-function bit that John Kitchin describes in his answer, but it should be adequate for almost all situations. But you need org-context-in-file-links to be set to t: AFAICT that has been the default since 2008, but if you get line numbers instead of context in file links, check the setting of that variable.

3
  • Thanks for your answer. I already do this, but I'm looking for something more intelligent than a static line number to cope with when functions move around over time.
    – retrodev
    Commented Jun 22, 2017 at 8:33
  • The links I get contain match patterns, not static line numbers. Try setting org-context-in-file-links to t.
    – NickD
    Commented Jun 22, 2017 at 13:39
  • Edited the answer to add the info about org-context-in-file-links.
    – NickD
    Commented Jun 22, 2017 at 14:23
10

For elisp that is certainly possible. The general strategy might be something like:

[[elisp:(find-function 'describe-function)]]

You could add a description if you wanted to also, e.g.

[[elisp:(find-function 'describe-function)][describe-function]]

For other languages it wouldn't be that easy. You can make a link like this:

[[file:~/vc/projects/neural-network/aenet-1.0.0/src/aenet.f90::subroutine%20aenet_init(atom_types,%20stat)]] 

which will open that file and search for the contents after the ::. Those contents are escaped.

2
  • Thanks, John. Is there anyway to capture the contents after::. when I capture the link. Perhaps by highlighting some text?
    – retrodev
    Commented Jun 22, 2017 at 8:34
  • It looks like you can highlight some text in a file, and then type C-c l to store a link to that text. It is still search based. Is that what you mean? Commented Jun 22, 2017 at 12:48
1

Yes, it is in fact possible to link directly to a help page, which should serve the purpose of linking to a function. Although it's not yet documented on https://orgmode.org/org.html#External-Links, the help: link type works, at least since 2016. For example, [[help:org-link-parameters]] links to the function where the help: link (and many of the other built-in ones) are defined. Furthermore, running org-store-link (C-c l) on a Help page will store a link to the Help page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.