2

这是一个令人沮丧的绝妙练习。出于某种原因,我的 python-mode / elpy 缓冲区总是有intent-tabs-mode nil. 我正在使用 python-mode 6.2.3、elpy 1.32.0 和 emacs 25.2.2。我的 .emacs 文件包含:

;; =============================================================================
;; Trying to get indentation to be tabs of width 4 spaces in Python is a real
;; pain. For some unknown reason, unlike EVERY other progmode file in the distro
;; python.el contains this gem:
;;
;;     5480: (set (make-local-variable 'tab-width) 8)
;;
;; which clobbers any defaults you might have, no questoins asked. It also has
;; this wonderful nugget:
;;
;;     5481: (set (make-local-variable 'indent-tabs-mode) nil)
;;
;; Because someone wants to force using spaces (so wby bother forcing
;; tab-width?)
;; -----------------------------------------------------------------------------
;; Do this first. Because of the above, if you don't python.el will clobber
;; your attempts to customize this stuff.
(load "python-mode")
;; Now follow the instructions sprinked all over the Interwebs by the thousands
;; of people frustrated by the above clobbering.
(add-hook 'python-mode-hook
      ;; Don't forget lambda(), God forbid
      (lambda ()
        ;; Now set the tab-width you really want here and cross your fingers
        (setq tab-width 4)
        ;; And tell python-mode to use tabs. I know lots of folks hate on
        ;; tabs, but whatever.
        (setq indent-tabs-mode t)
        ;; If you want to make sure you don't have trailing whitespaces
        ;; at the end of line, uncomment this.
        ;;(add-to-list 'write-file-functions 'delete-trailing-whitespace)
        )
      )

indent-tabs-mode仍然存在nil。当然,仅在 python 模式下。从describe-variablepython-mode / elpy 缓冲区中:

indent-tabs-mode is a variable defined in ‘C source code’.
Its value is nil
Original value was t
Local in buffer config; global value is t

  Automatically becomes buffer-local when set.
  This variable is safe as a file local variable if its value
  satisfies the predicate ‘booleanp’.

Documentation:
Indentation can insert tabs if this is non-nil.

You can customize this variable.

我使用的 python-mode.el 版本包含以下 [第 955-964 行]:

  "Python-mode starts `indent-tabs-mode' with the value specified here, default is nil. "
  :type 'boolean
  :tag "py-indent-tabs-mode"
  :group 'python-mode)

[第 5159-5165 行]

       ["indent-tabs-mode"
        (setq indent-tabs-mode
          (not indent-tabs-mode))
        :help "Indentation can insert tabs if this is non-nil.

Use `M-x customize-variable' to set it permanently"
        :style toggle :selected indent-tabs-mode]

[第 6787-6820 行]

(defun py-toggle-indent-tabs-mode ()
  "Toggle `indent-tabs-mode'.

Returns value of `indent-tabs-mode' switched to. "
  (interactive)
  (when
      (setq indent-tabs-mode (not indent-tabs-mode))
    (setq tab-width py-indent-offset))
  (when (and py-verbose-p (called-interactively-p 'any)) (message "indent-tabs-mode %s  py-indent-offset %s" indent-tabs-mode py-indent-offset))
  indent-tabs-mode)

(defun py-indent-tabs-mode (arg &optional iact)
  "With positive ARG switch `indent-tabs-mode' on.

With negative ARG switch `indent-tabs-mode' off.
Returns value of `indent-tabs-mode' switched to. "
  (interactive "p")
  (if (< 0 arg)
      (progn
        (setq indent-tabs-mode t)
        (setq tab-width py-indent-offset))
    (setq indent-tabs-mode nil))
  (when (and py-verbose-p (or iact (called-interactively-p 'any))) (message "indent-tabs-mode %s   py-indent-offset %s" indent-tabs-mode py-indent-offset))
  indent-tabs-mode)

(defun py-indent-tabs-mode-on (arg)
  "Switch `indent-tabs-mode' on. "
  (interactive "p")
  (py-indent-tabs-mode (abs arg)(called-interactively-p 'any)))

(defun py-indent-tabs-mode-off (arg)
  "Switch `indent-tabs-mode' off. "
  (interactive "p")
  (py-indent-tabs-mode (- (abs arg))(called-interactively-p 'any)))

[第 22259-22266 行]

;;  unconditional Hooks
;;  (orgstruct-mode 1)
(add-hook 'python-mode-hook
      (lambda ()
        (setq imenu-create-index-function py--imenu-create-index-function)
        (setq indent-tabs-mode py-indent-tabs-mode)))

(remove-hook 'python-mode-hook 'python-setup-brm)

再次(与上面几乎相同)[第 25021-25033 行]:

          ["indent-tabs-mode"
           (setq indent-tabs-mode
             (not indent-tabs-mode))
           :help "Indentation can insert tabs if this is non-nil.

Use `M-x customize-variable' to set it permanently"
           :style toggle :selected indent-tabs-mode]

          ["Tab indent"
           (setq py-tab-indent
             (not py-tab-indent))
           :help "Non-nil means TAB in Python mode calls `py-indent-line'.Use `M-x customize-variable' to set it permanently"
           :style toggle :selected py-tab-indent]

indent-tabs-mode我已经设置了可以在 to 的任何 *.el 文件中找到的任何定义的每个版本,但t没有任何效果。

4

1 回答 1

2

事实证明,对于某些版本的 python.el,您需要添加(耶!)另一个项目。这终于奏效了:

;; =============================================================================
;; Trying to get indentation to be tabs of width 4 spaces in Python is a real
;; pain. For some unknown reason, unlike EVERY other progmode file in the distro
;; python.el contains this gem:
;;
;;     5480: (set (make-local-variable 'tab-width) 8)
;;
;; which clobbers any defaults you might have, no questoins asked. It also has
;; this wonderful nugget:
;;
;;     5481: (set (make-local-variable 'indent-tabs-mode) nil)
;;
;; Because someone wants to force using spaces (so wby bother forcing
;; tab-width?)
;; -----------------------------------------------------------------------------
;; Do this first. Because of the above, if you don't python.el will clobber
;; your attempts to customize this stuff.
(load "python-mode")
;; Now follow the instructions sprinked all over the Interwebs by the thousands
;; of people frustrated by the above clobbering.
(add-hook 'python-mode-hook
      ;; Don't forget lambda(), God forbid
      (lambda ()
        ;; Now set the tab-width you really want here and cross your fingers
        (setq tab-width 4)
        ;; And tell python-mode to use tabs. I know lots of folks hate on
        ;; tabs, but whatever.
        (setq indent-tabs-mode t)
        ;; Some verions of python.el use this, others don't
        (setq py-indent-tabs-mode t)
        ;; If you want to make sure you don't have trailing whitespaces
        ;; at the end of line, uncomment this.
        ;;(add-to-list 'write-file-functions 'delete-trailing-whitespace)
        )
      )
于 2020-02-08T01:53:06.810 回答