0

Say I have an existing, simple block of code that uses an indent width of 4, as such

def incDictValue(d, k):

    if not k in d:
        d[k] = 1
    else:
        d[k] += 1

    return

and setup python-mode in my init.el file to use a 2 space indent offset, like such

(add-hook 'python-mode-hook
  (lambda ()
    (setq indent-tabs-mode nil)
    (setq tab-width 2)
    (setq python-indent-guess-indent-offset nil)
    (setq py-indent-offset 2)))

If I place the cursor on any line and hit TAB, the existing code is properly indented to match the indent offset of 2.

However, if I highlight the entire code block and M-x indent-region, only the second level indents are adjusted to 2. The first level indents (the 'if', 'else', and 'return' statements) are kept at 4.

def incDictValue(d, k):

    if not k in d:
      d[k] = 1
    else:
      d[k] += 1

    return

Why is this? This clearly isn't the desired result that one would want when applying indent-region. I would like indent-region to apply the indent settings of python-mode to the entire block of code, to all levels of indentation (much as if I hit TAB on each line sequentially)

  • I originally had it incorrectly as python-indent-offset. Fixed this to py-indent-offset, yet this indenting issue still exists – onlinespending Jan 05 '22 at 20:12
  • Are you sure? I only have `python-indent-offset` defined, not what you changed it to. But I get the same behavior as you regardless: maybe send a bug report? – NickD Jan 06 '22 at 01:27
  • @NickD yeah, I was just going by the latest python-mode.el that I see on github. I'll file a bug there, as other prog modes don't exhibit this behavior – onlinespending Jan 06 '22 at 14:31
  • I believe there are *two* python modes: make sure that you do not confuse them. The one distributed with emacs is `python.el` originally by Fabian Gallina; the other one is `python-mode.el` originally by Barry Warsaw and maintained by Andreas Röhler. – NickD Jan 06 '22 at 16:25
  • @NickD ah, thanks for the heads up. Do you find one better than the other? – onlinespending Jan 06 '22 at 17:15

0 Answers0