The documentation of a Fortran code, being done in org-mode, is intended to be done in such a way that changing the Fortran source code to be documented should be kept at a minimum.

There is the problem that text like "type t_obs", which is a definition, does also occur in comments and is not unique. A naive hyperlink

[[./obs.f90::type t_obs][obs.f90::t_obs definition]]

finds the first occurence in the file, which is not the desired one. Specifying a line number in the link is not acceptable, as the code is under development by a group other than those doing the documentation.

Specifying hyperlinks to match a unique target could be attempted using regular expressions, e.g. of the form [[file:~/xx.org::/regexp/]].

To be specific, I have tried the hyperlink:

[[./obs.f90::/^ *type t_obs/][obs.f90::t_obs definition]]

However, clicking on this link then opens an Occur buffer that shows exactly one match. It then requires to additionally click on the match in order to jump to the link target.

Is there a trick to avoid the need for clicking on the match in the occur buffer and immediately jump to this / the first match?

Furthermore, as my colleagures tell me, the above hyperlink with the regex will not work properly in a PDF generated from the org file when viewed with a PDF viewer.

Do I need to write a specific file-search-function, or is there an easier way?

  • Can you please be more specific about what you've tried and just what the problem is with it?
    – Drew
    Sep 1, 2020 at 15:19
  • Well, I have as hyperlink [[./obs.f90::/^\ type t_obs/][obs.f90::t_obs definition]]. Clicking on that link opens an *occur buffer with the results, which is just one. The reason for the regex is to ignore occurences of the string "type t_obs" in comments. So it is desired to avoid clicking on the link. Furthermore, as my colleagures tell me, the resulting hyperlink will not work properly in a PDF.
    – Harald
    Sep 1, 2020 at 15:27
  • Read: So it is desired to avoid the need for clicking on the match in the occur biffer.
    – Harald
    Sep 1, 2020 at 15:34
  • Please put all relevant info in the question itself. Comments can be deleted at any time. Thx.
    – Drew
    Sep 1, 2020 at 15:37
  • You might have some luck with the following two sections of the manual (the second one in particular will allow you to customize export of those links so that there is a chance of satisfying PDF viewers): Custom seraches and Adding Hyperlink Types.
    – NickD
    Sep 1, 2020 at 16:46

1 Answer 1


OK, here's what I am working with right now:

(defun org-execute-file-search-in-fortran (s)
  "Find the link search string S as a key.  May be a regex."
  (when (eq major-mode 'f90-mode)
    ;; Yes, we want to do the search in this file.
    (goto-char (point-min))
    (if (string-match "^/\\(.*\\)/$" s)
        ;; A regular expression
        (setq ss (match-string 1 s))
        ;(message "org-execute-file-search-in-fortran with regex: '%s'" ss)
        (and (re-search-forward ss nil t)
          (goto-char (match-beginning 0))))
        ;; An ordinary string.  We construct a regexp that searches for the key
        ;(message "org-execute-file-search-in-fortran with string: '%s'" s)
        (and (re-search-forward (regexp-quote s) nil t)
          (goto-char (match-beginning 0)))))
        ;; return t to indicate that the search is done.

That's already doing mostly what I need.

The only thing I can think of now is a possible syntax in a link for getting the n-th match of a regex.

Would sth. like file.f90::/regex/,n or file.f90::/regex/:n make sense, or is there already a similar solution?

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.