Improve +word-wrap in non-code buffers

Adds `+word-wrap-text-modes`, a list of modes which shouldn't get any
extra indentation. This is used for text and markdown modes, which
should just indent to the parent depth.

Adds `+word-wrap-visual-modes`, a list of modes which shouldn't enable
`adaptive-wrap-prefix-mode`. This is used to fix the prefix indentation
in `org-mode`, which provides its own implementation.

Tweaks the indent behaviour to treat strings the same as comments so
they don't receive additional indentation.
This commit is contained in:
Andrew Whatson 2019-10-15 00:51:28 +10:00
parent 24b336322c
commit 930d0d134a
3 changed files with 62 additions and 29 deletions

View file

@ -1,7 +1,9 @@
;;; editor/word-wrap/autoload.el -*- lexical-binding: t; -*-
(defvar +word-wrap--prev-adaptive-wrap-mode nil)
(defvar +word-wrap--prev-visual-line-mode nil)
(defvar +word-wrap--major-mode-is-visual nil)
(defvar +word-wrap--major-mode-is-text nil)
(defvar +word-wrap--enable-adaptive-wrap-mode nil)
(defvar +word-wrap--enable-visual-line-mode nil)
(defvar +word-wrap--major-mode-indent-var nil)
(defun +word-wrap--adjust-extra-indent-a (orig-fn beg end)
@ -11,7 +13,8 @@
(defun +word-wrap--calc-extra-indent (p)
"Calculate extra word-wrap indentation at point."
(if (not (sp-point-in-comment p))
(if (not (or +word-wrap--major-mode-is-text
(sp-point-in-string-or-comment p)))
(pcase +word-wrap-extra-indent
('double
(* 2 (symbol-value +word-wrap--major-mode-indent-var)))
@ -31,34 +34,45 @@ without modifying the buffer content. This is useful when dealing with legacy
code which contains gratuitously long lines, or running emacs on your
wrist-phone.
Wrapped lines will be indented to match the preceding line. Lines which are not
inside a comment will have additional indentation according to the configuration
of `+word-wrap-extra-indent'."
Wrapped lines will be indented to match the preceding line. In code buffers,
lines which are not inside a string or comment will have additional indentation
according to the configuration of `+word-wrap-extra-indent'."
:init-value nil
(if +word-wrap-mode
(progn
(require 'adaptive-wrap)
(require 'dtrt-indent) ; for dtrt-indent--search-hook-mapping
(require 'smartparens) ; for sp-point-in-string-or-comment
(setq-local +word-wrap--major-mode-is-visual
(memq major-mode +word-wrap-visual-modes))
(setq-local +word-wrap--major-mode-is-text
(memq major-mode +word-wrap-text-modes))
(setq-local +word-wrap--prev-adaptive-wrap-mode adaptive-wrap-prefix-mode)
(setq-local +word-wrap--prev-visual-line-mode visual-line-mode)
(setq-local +word-wrap--major-mode-indent-var
(caddr (dtrt-indent--search-hook-mapping major-mode)))
(setq-local +word-wrap--enable-adaptive-wrap-mode
(and (not (bound-and-true-p adaptive-wrap-prefix-mode))
(not +word-wrap--major-mode-is-visual)))
(advice-add #'adaptive-wrap-fill-context-prefix :around #'+word-wrap--adjust-extra-indent-a)
(setq-local +word-wrap--enable-visual-line-mode
(not (bound-and-true-p visual-line-mode)))
(unless +word-wrap--prev-adaptive-wrap-mode
(unless +word-wrap--major-mode-is-visual
(require 'dtrt-indent) ; for dtrt-indent--search-hook-mapping
(require 'smartparens) ; for sp-point-in-string-or-comment
(setq-local +word-wrap--major-mode-indent-var
(caddr (dtrt-indent--search-hook-mapping major-mode)))
(advice-add #'adaptive-wrap-fill-context-prefix :around #'+word-wrap--adjust-extra-indent-a))
(when +word-wrap--enable-adaptive-wrap-mode
(adaptive-wrap-prefix-mode +1))
(unless +word-wrap--prev-visual-line-mode
(when +word-wrap--enable-visual-line-mode
(visual-line-mode +1)))
;; disable +word-wrap-mode
(advice-remove #'adaptive-wrap-fill-context-prefix #'+word-wrap--adjust-extra-indent-a)
(unless +word-wrap--major-mode-is-visual
(advice-remove #'adaptive-wrap-fill-context-prefix #'+word-wrap--adjust-extra-indent-a))
(unless +word-wrap--prev-adaptive-wrap-mode
(when +word-wrap--enable-adaptive-wrap-mode
(adaptive-wrap-prefix-mode -1))
(unless +word-wrap--prev-visual-line-mode
(when +word-wrap--enable-visual-line-mode
(visual-line-mode -1))))
(defun +word-wrap--enable-global-mode ()