Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs 24.3 python: Can't guess python-indent-offset, using defaults 4

Can anyone who understands Lisp please help resolve this warning?

I upgraded to Emacs 24.3 and whenever I create a Python file using Emacs I get this warning message. Searched in python.el and found the following section of code that produces the warning:

(let ((indentation (when block-end
                     (goto-char block-end)
                     (python-util-forward-comment)
                     (current-indentation))))
  (if indentation
      (set (make-local-variable 'python-indent-offset) indentation)
    (message "Can't guess python-indent-offset, using defaults: %s"
             python-indent-offset)))

And here is my .emacs setup:

(setq-default c-basic-offset   4
              tab-width        4
              indent-tabs-mode nil)

(add-hook 'c-mode-common-hook
          (lambda ()
            (c-set-offset 'arglist-intro '+)
            (c-set-offset 'arglist-close 0)))

(add-hook 'python-mode-hook
          (lambda ()
            (c-set-offset 'arglist-intro '+)
            (c-set-offset 'arglist-close 0)))
like image 972
bohanl Avatar asked Sep 13 '13 05:09

bohanl


2 Answers

When you open a python file, emacs guesses the indentation offset (number of spaces to indent) based on that file style. When you create a file (the case you describe), emacs cannot guess (file is empty) so it uses your default (4) and notifies the user.

In other words: it is a harmless warning; if you find this is a bug please report it as such.

If you don't like emacs guessing the offset, customize the variable python-indent-guess-indent-offset to nil, and then emacs will use always your default (very unsafe in python, where indentation has meaning and you could be editing a file created by somebody else with other defaults).

like image 78
juanleon Avatar answered Nov 11 '22 05:11

juanleon


If all you want is to silence the warnings, while letting emacs still guess the offset as juanleon's answer explains, you can switch the python-indent-guess-indent-offset-verbose variable off.

(setq python-indent-guess-indent-offset t)  
(setq python-indent-guess-indent-offset-verbose nil)

Per this comprehensive answer on the emacs SE.

like image 32
Nikana Reklawyks Avatar answered Nov 11 '22 05:11

Nikana Reklawyks