Emacs version 24.4: New obnoxious loss of indentation on hitting RETURN

ⅰ亾dé卋堺 提交于 2019-11-29 10:30:38

In Emacs 24.4, electric-indent-mode is enabled by default. It seems like that's what's causing this problem in combination with paragraph-indent-minor-mode. You can avoid that by turning off Electric Indent mode everywhere (M-x electric-indent-mode) or just in the local buffer (M-x electric-indent-local-mode).

The following will try to keep electric-indent-mode from stepping on the toes of paragraph-indent-minor-mode. It doesn't attempt to be robust in all situations, but I suspect it's entirely sufficient in your situation.

(defvar-local my-local-electric-indent-status :unknown)

(defun my-local-electric-indent-disable ()
  "Make `electric-indent-mode' ineffective in the current buffer."
  (setq my-local-electric-indent-status electric-indent-mode)
  (electric-indent-local-mode -1))

(defun my-local-electric-indent-restore ()
  "Restore original status of `electric-indent-mode' in the current buffer."
  (unless (eq my-local-electric-indent-status :unknown)
    (electric-indent-local-mode my-local-electric-indent-status)))

(add-hook 'paragraph-indent-minor-mode-on-hook #'my-local-electric-indent-disable)
(add-hook 'paragraph-indent-minor-mode-off-hook #'my-local-electric-indent-restore)

If you're not running at least Emacs 24.3, replace the defvar-local with:

(defvar my-local-electric-indent-status :unknown)
(make-variable-buffer-local 'my-local-electric-indent-status)

;;(global-set-key "\em" 'newline) ;;for emacs 23

global-set-key "\em" 'electric-newline-and-maybe-indent) ;;for emacs 24

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!