Search code examples
emacsorg-mode

org-mode source inclusion line numbers


Org-mode has a great feature to include source code like this:

#+begin_src java -n
    /**
     * @param foo
     */
    public static void doBar(Baz ba)
    {
        Collection<String> strings = ba.getStrings(true);
        return strings;
    }
#+end_src

The -n option shows line numbers.

There's a +n option to have the numbering continue from the last block.

Is there any option to set the starting number? This would be useful for source code snippets where you want the line numbers to correspond to the full file.


Solution

  • This answer is out of date, see the other ones

    No implemented options that I know of.

    A very hacky solution I just tested would be to define yourself the counter org uses for +n to work. It goes :

    #+begin_src emacs-lisp :exports results
    ;; we need it not be a buffer-local value
    (setq-default org-export-last-code-line-counter-value 42)
    #+end_src
    

    As the result of being a dirty hack, it only works once, though, for the very first block, but I'm not sure how ±n behaves . It is evaluated (because of :exports results, without actually exporting anything when I tried) before numbering any other blocks, so it can be anywhere in your buffer.

    I guess it wouldn't be very hard to implement, or that it could be done better by someone who actually knows elisp, but I hope it'll help.