Emacs wrong indentation after `import Base: *`. Here's a workaround

There is a known issue with the indentation of julia-mode in Emacs, giving your code formatted like this

import Base: *
    a = 1

This is because Emacs believes the first line ends with a hanging operator *, so considers the second line to be the rest of the same statement. This is annoying for me since I often overload arithmetic operators. I found a quick and dirty workaround to disable indentation for any line following export, import, or using, by adding the following code to my Emacs init file:

(defun my-julia-indent-hanging (orig-fun &rest args)
       (if (julia-following-import-export-using) 0 (apply orig-fun args)))
(advice-add 'julia-indent-hanging :around #'my-julia-indent-hanging)

I’m using Melpa package version julia-mode-20220418.809. It works like this: julia-mode.el from the package has a function julia-indent-hanging to calculate the indent of a line. I modify the behavior of the function with an “advice” (like a function decorator in Python) which says that I’ll first run the function julia-following-import-export-using (also defined in julia-mode.el) to see if the current line follows a import/export/using statement. If so, the indentation 0 is returned, otherwise the original function julia-indent-hanging is run to calculate the indentation amount.

My workaround is very simplistic, but is OK for my use cases since I never break up an import/export statement into multiple lines.

1 Like