From c789430331948e76b38091aa95bb9a9602a08289 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 23 Apr 2022 14:12:18 +0200 Subject: [PATCH] Add new minor mode word-wrap-whitespace-mode * doc/emacs/display.texi (Visual Line Mode): Document it. * lisp/textmodes/word-wrap-mode.el: New minor mode. --- doc/emacs/display.texi | 14 ++++- etc/NEWS | 5 ++ lisp/textmodes/word-wrap-mode.el | 91 ++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 lisp/textmodes/word-wrap-mode.el diff --git a/doc/emacs/display.texi b/doc/emacs/display.texi index 534bf5881e7..2ac0dca6223 100644 --- a/doc/emacs/display.texi +++ b/doc/emacs/display.texi @@ -1899,12 +1899,22 @@ logical lines, so having a fringe indicator for each wrapped line would be visually distracting. You can change this by customizing the variable @code{visual-line-fringe-indicators}. +@vindex word-wrap-whitespace-mode + By default, Emacs only breaks lines after whitespace characters like +@key{SPC} and @key{TAB}, but does not break after whitespace +characters like @key{EN QUAD}. Emacs provides a minor mode called +@code{word-wrap-whitespace-mode} that switches on word wrapping in the +current mode, and sets up which characters to wrap lines on based on +the @code{word-wrap-whitespace-characters} user option. There's also +a globalized version of that mode called +@code{global-word-wrap-whitespace-mode}. + @vindex word-wrap-by-category @findex modify-category-entry @findex char-category-set @findex category-set-mnemonics - By default, Emacs only breaks lines after whitespace characters. -That produces incorrect results when CJK and Latin text are mixed + Only breaking after whitespace character produces incorrect +results when CJK and Latin text are mixed together (because CJK characters don't use whitespace to separate words). You can customize the option @code{word-wrap-by-category} to allow Emacs to break lines after any character with @samp{|} category diff --git a/etc/NEWS b/etc/NEWS index c0b9ce654e1..57bcef36f13 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -642,6 +642,11 @@ script that was used in ancient South Asia. A new input method, * Changes in Specialized Modes and Packages in Emacs 29.1 ++++ +** New minor mode 'word-wrap-whitespace-mode' for extending 'word-wrap'. +This mode switches 'word-wrap' on, and breaks on all the whitespace +characters instead of just SPC and TAB. + --- ** New mode, 'emacs-news-mode', for editing the NEWS file. This mode adds some highlighting, fixes the 'M-q' command, and has diff --git a/lisp/textmodes/word-wrap-mode.el b/lisp/textmodes/word-wrap-mode.el new file mode 100644 index 00000000000..78823c4f133 --- /dev/null +++ b/lisp/textmodes/word-wrap-mode.el @@ -0,0 +1,91 @@ +;;; word-wrap-mode.el --- minor mode for `word-wrap' tweaks -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Free Software Foundation, Inc. + +;; Maintainer: emacs-devel@gnu.org + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;;; Code: + +;; The list below lists all characters that have a general-category of +;; Zs, but with the ones we don't want to add here commented out. +(defcustom word-wrap-whitespace-characters + '(;;?\N{SPACE} + ;;?\N{NO-BREAK SPACE} + ?\N{OGHAM SPACE MARK} + ?\N{EN QUAD} + ?\N{EM QUAD} + ?\N{EN SPACE} + ?\N{EM SPACE} + ?\N{THREE-PER-EM SPACE} + ?\N{FOUR-PER-EM SPACE} + ?\N{SIX-PER-EM SPACE} + ?\N{FIGURE SPACE} + ?\N{PUNCTUATION SPACE} + ?\N{THIN SPACE} + ?\N{HAIR SPACE} + ;;?\N{NARROW NO-BREAK SPACE} + ?\N{MEDIUM MATHEMATICAL SPACE} + ?\N{IDEOGRAPHIC SPACE} + ;; Not in the Zs category: + ?\N{ZERO WIDTH SPACE}) + "Characters that `word-wrap-whitespace-mode' should add to `word-wrap'." + :version "29.1" + :type '(repeat char) + :group 'display) + +(defvar word-wrap-mode--previous-state) + +;;;###autoload +(define-minor-mode word-wrap-whitespace-mode + "Allow `word-wrap' to fold on all breaking whitespace characters. +The characters to break on are defined by `word-wrap-whitespace-characters'." + :group 'display + (if word-wrap-whitespace-mode + (progn + (setq-local word-wrap-mode--previous-state + (list (category-table) + (local-variable-p 'word-wrap-by-category) + word-wrap-by-category + (local-variable-p 'word-wrap) + word-wrap)) + (set-category-table (copy-category-table)) + (dolist (char word-wrap-whitespace-characters) + (modify-category-entry char ?|)) + (setq-local word-wrap-by-category t + word-wrap t)) + (pcase-let ((`(,table ,lby-cat ,by-cat + ,lwrap ,wrap) + word-wrap-mode--previous-state)) + (if lby-cat + (setq-local word-wrap-by-category by-cat) + (kill-local-variable 'word-wrap-by-category)) + (if lwrap + (setq-local word-wrap wrap) + (kill-local-variable 'word-wrap)) + (set-category-table table)))) + +;;;###autoload +(define-globalized-minor-mode global-word-wrap-whitespace-mode + word-wrap-whitespace-mode word-wrap-whitespace-mode + :group 'display) + +(provide 'word-wrap-mode) + +;;; word-wrap-mode.el ends here -- 2.39.2