TransWikia.com

How do I make emacs indent relative to the beginning of the previous line?

Emacs Asked by x-yuri on September 2, 2021

Here’s how emacs indents e.g. Python code:

a = myfun(b,
          c)

I’d like it to be this way:

a = myfun(b,
    c)

Reasoning? If I later replace myfun with myfunction, in the first case it becomes:

a = myfunction(b,
          c)

I can probably use aggressive-indent-mode, but not everybody uses emacs. If I’d like to contribute to, say, an open source project, I can’t just impose it on other people.

Is there a way out? For Python? Ruby? Or Javascript? Any of these?

One Answer

You might want to stick to the defaults, since:

r = my_long_function_name(
    param1, param2)

is arguably preferable over:

r = my_long_function_name(param1,
    param2)

But if you care (Emacs 26.3), for Python:

(eval-after-load "python"
  (lambda ()
    (fset 'old-python-indent--calculate-indentation
      (symbol-function 'python-indent--calculate-indentation))
    (defun python-indent--calculate-indentation ()
      (save-excursion
        (pcase (python-indent-context)
          (`(,:inside-paren . ,start)
           (goto-char start)
           (+ (current-indentation) python-indent-offset))
          (_ (old-python-indent--calculate-indentation)))))

    ;; Alternatively, use advice-add
    ;; (defun my/python-indent--calculate-indentation (orig-fun &rest args)
    ;;   ...
    ;;   (apply orig-fun args))
    ;; (advice-add 'python-indent--calculate-indentation :around #'my/python-indent--calculate-indentation)
  ))

For Javascript:

(setq js-indent-align-list-continuation nil)

For Ruby I tried to monkey-patch the Ruby mode, but by default it uses smie-indent-line. Which in its turn might be used not only by the Ruby mode. And I didn't see a better way than to copy the smie-indent-keyword and fix a thing or two. But I'm far from understanding how it works, so I decided to go with the Enhanced Ruby Mode. After installation, you need to add:

(require 'ruby-mode) ;; https://emacs.stackexchange.com/questions/59782/autoloaded-variable-overrides-the-one-from-the-init-file#comment93779_59782
(setq auto-mode-alist
      (mapcar
       (lambda (x)
         (if (eq (cdr x) 'ruby-mode)
             (cons (car x) 'enh-ruby-mode)
           x)) auto-mode-alist))
(setq interpreter-mode-alist
      (mapcar
       (lambda (x)
         (if (eq (cdr x) 'ruby-mode)
             (cons (car x) 'enh-ruby-mode)
           x)) interpreter-mode-alist))

Then to fix the indentation:

(setq enh-ruby-deep-indent-paren nil)

Correct answer by x-yuri on September 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP