This commit is contained in:
ry
2022-01-11 18:05:13 -08:00
parent 2046befee2
commit 8e7b654716
846 changed files with 71287 additions and 4 deletions

View File

@@ -0,0 +1,81 @@
#+TITLE: editor/word-wrap
#+DATE: August 26, 2019
#+SINCE: v2.1
* Table of Contents :TOC_3:noexport:
- [[#description][Description]]
- [[#module-flags][Module Flags]]
- [[#plugins][Plugins]]
- [[#configuration][Configuration]]
* Description
This module adds a minor-mode ~+word-wrap-mode~, which intelligently wraps long
lines in the buffer without modifying the buffer content.
Wrapped lines will be indented to match the preceding line. In code buffers,
lines which are not inside a string or comment will have extra indentation as
determined by ~+word-wrap-extra-indent~. The default is to increase the indent
by twice the major-mode indent.
The ~+word-wrap-extra-indent~ variable supports the following values:
- ~double~: indent by twice the major-mode indentation
- ~single~: indent by the major-mode indentation
- a positive integer: indent by this fixed amount
- a negative integer: dedent by this fixed amount
- ~nil~: no extra indent
This module also includes a global minor-mode ~+global-word-wrap-mode~ to
automatically enable wrapping in most buffers. Wrapping will not be enabled in
buffers whose major mode is marked "special", or are listed in
~+word-wrap-disabled-modes~.
The ~+word-wrap-text-modes~ variable lists modes which shouldn't have any extra
indentation, regardless of the ~+word-wrap-extra-indent~ setting. This is useful
for modes which are primarily text, such as ~text-mode~ and ~markdown-mode~.
The ~+word-wrap-visual-modes~ variable lists modes which should only enable
~visual-line-mode~ and not provide any prefix indentation. This is useful for
modes like ~org-mode~ which handle prefix indentation themselves.
** Module Flags
This module provides no flags.
** Plugins
+ [[https://elpa.gnu.org/packages/adaptive-wrap.html][adaptive-wrap]]
* Configuration
Word wrapping is not enabled by default.
Wrapping can be toggled in the current buffer with ~M-x +word-wrap-mode~. The
default doom bindings bind this to ~SPC t w~ for ~evil~ users.
To enable wrapping in a specific mode, add it to the appropriate hook in your
~config.el~:
#+BEGIN_SRC emacs-lisp
;; enable word-wrap in C/C++/ObjC/Java
(add-hook 'c-mode-common-hook #'+word-wrap-mode)
#+END_SRC
To customize the behaviour in a specific mode:
#+BEGIN_SRC emacs-lisp
;; use a single indent in json-mode
(add-hook! 'json-mode-hook
(setq-local +word-wrap-extra-indent 'single)
(+word-wrap-mode +1))
#+END_SRC
To turn on word wrapping (almost) everywhere:
#+BEGIN_SRC emacs-lisp
;; enable word-wrap (almost) everywhere
(+global-word-wrap-mode +1)
#+END_SRC
To disable global word-wrapping in a specific mode:
#+BEGIN_SRC emacs-lisp
;; disable global word-wrap in emacs-lisp-mode
(add-to-list '+word-wrap-disabled-modes 'emacs-lisp-mode)
#+END_SRC

View File

@@ -0,0 +1,91 @@
;;; editor/word-wrap/autoload.el -*- lexical-binding: t; -*-
(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)
(defvar adaptive-wrap-extra-indent)
(defun +word-wrap--adjust-extra-indent-a (fn beg end)
"Contextually adjust extra word-wrap indentation."
(let ((adaptive-wrap-extra-indent (+word-wrap--calc-extra-indent beg)))
(funcall fn beg end)))
(defun +word-wrap--calc-extra-indent (p)
"Calculate extra word-wrap indentation at point."
(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)))
('single
(symbol-value +word-wrap--major-mode-indent-var))
((and (pred integerp) fixed)
fixed)
(_ 0))
0))
;;;###autoload
(define-minor-mode +word-wrap-mode
"Wrap long lines in the buffer with language-aware indentation.
This mode configures `adaptive-wrap' and `visual-line-mode' to wrap long lines
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. 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
(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--enable-adaptive-wrap-mode
(and (not (bound-and-true-p adaptive-wrap-prefix-mode))
(not +word-wrap--major-mode-is-visual)))
(setq-local +word-wrap--enable-visual-line-mode
(not (bound-and-true-p visual-line-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))
(when +word-wrap--enable-visual-line-mode
(visual-line-mode +1)))
;; disable +word-wrap-mode
(unless +word-wrap--major-mode-is-visual
(advice-remove #'adaptive-wrap-fill-context-prefix #'+word-wrap--adjust-extra-indent-a))
(when +word-wrap--enable-adaptive-wrap-mode
(adaptive-wrap-prefix-mode -1))
(when +word-wrap--enable-visual-line-mode
(visual-line-mode -1))))
(defun +word-wrap--enable-global-mode ()
"Enable `+word-wrap-mode' for `+word-wrap-global-mode'.
Wrapping will be automatically enabled in all modes except special modes, or
modes explicitly listed in `+word-wrap-disabled-modes'."
(unless (or (eq (get major-mode 'mode-class) 'special)
(memq major-mode +word-wrap-disabled-modes))
(+word-wrap-mode +1)))
;;;###autoload
(define-globalized-minor-mode +global-word-wrap-mode
+word-wrap-mode
+word-wrap--enable-global-mode)

View File

@@ -0,0 +1,30 @@
;;; editor/word-wrap/config.el -*- lexical-binding: t; -*-
(defvar +word-wrap-extra-indent 'double
"The amount of extra indentation for wrapped code lines.
When 'double, indent by twice the major-mode indentation.
When 'single, indent by the major-mode indentation.
When a positive integer, indent by this fixed amount.
When a negative integer, dedent by this fixed amount.
Otherwise no extra indentation will be used.")
(defvar +word-wrap-disabled-modes
'(fundamental-mode so-long-mode)
"Major-modes where `+global-word-wrap-mode' should not enable
`+word-wrap-mode'.")
(defvar +word-wrap-visual-modes
'(org-mode)
"Major-modes where `+word-wrap-mode' should not use
`adaptive-wrap-prefix-mode'.")
(defvar +word-wrap-text-modes
'(text-mode markdown-mode markdown-view-mode gfm-mode gfm-view-mode rst-mode
latex-mode LaTeX-mode)
"Major-modes where `+word-wrap-mode' should not provide extra indentation.")
(when (memq 'visual-line-mode text-mode-hook)
(remove-hook 'text-mode-hook #'visual-line-mode)
(add-hook 'text-mode-hook #'+word-wrap-mode))

View File

@@ -0,0 +1,4 @@
;; -*- no-byte-compile: t; -*-
;;; editor/word-wrap/packages.el
(package! adaptive-wrap :pin "0d5b4a07de76d87dd64333a566a8a0a845f2b9f0")