Using outline mode in Emacs for Julia code
I used to use literate programming with org mode in Emacs to manage code writing. This worked well generally but I found it frustrating to not have access to any support systems that expect code to be in specific files, e.g. a .jl
file for Julia code. The support I would like includes the Xref tools in Emacs.
One of the nicest features (for me) of org mode when writing code is being able to selectively hide parts of the file so I can focus on a specific function, say. Not using org mode for coding means I lose this capability… but I need not do so. After all, org mode is based on outline mode. So the challenge is to make it easy to use outline mode for coding.
Outline mode assumes there are two types of lines in a file, headings and text. Headings are defined, by default, by lines that start with one or more asterisks (*) followed by a space. Anything else is plain text. Org mode uses the same default behaviour for outlining. However, outline mode allows you to change the default behaviour (not surprising: this is pretty much true for everything in Emacs). The variable outline-regexp
is used to identify the leading string that specifies a headline.
For Julia, I have the following local variables defined in each file where I want to use outline mode:
# Local Variables: # outline-default-state: 4 # outline-regexp: "##+" # eval: (outline-minor-mode 1) # End:
When I open a file with these local variables, all the headings are identified and all the text is hidden:
If we expand or open one of the headings, we get this:
An aside: the screenshots above show an Emacs buffer where line numbers are being displayed, using 'virtual
line numbers, and global-hl-line-mode
as been turned on (to highlight the current line). The colour theme is my own hand-crafted theme.
You can find me on Mastodon should you wish to comment on this entry or the whole blog.