bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#18730: [PATCH 1/2] tildify.el: Add `tildify-space' and `tildify-mode


From: Michal Nazarewicz
Subject: bug#18730: [PATCH 1/2] tildify.el: Add `tildify-space' and `tildify-mode'.
Date: Mon, 24 Nov 2014 15:20:08 +0100

* lisp/textmodes/tildify.el (tildify-space): A new function
which can be used as a `post-self-insert-hook' to automatically
convert spaces into hard spaces.
(tildify-space-pattern): A new variable specifying pattern where
`tildify-space' should take effect.
(tildify-space-predicates): A new variable specifying list of
predicate functions that all must return non-nil for
`tildify-space' to take effect.
(tildify-space-region-predicate): A new functions meant to be
used as a predicate in `tildify-space-predicates' list.
(tildify-mode): A new minor mode enabling `tildify-space' as a
`post-self-insert-hook'

* tests/automated/tildify-tests.el (tildify-space-test--test):
A new helper function for testing `tildify-space' function.
(tildify-space-test-html, tildify-space-test-html-nbsp)
(tildify-space-test-xml, tildify-space-test-tex): New tests for
`tildify-space' function.
---
 etc/NEWS                        |  6 +++
 lisp/textmodes/tildify.el       | 88 ++++++++++++++++++++++++++++++++++++++++-
 test/automated/tildify-tests.el | 38 ++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 5154066..0c01560 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -346,6 +346,12 @@ helper functions) obsolete.
 * New Modes and Packages in Emacs 25.1
 
 ** scss-mode (a minor variant of css-mode)
+
+** `tildify-mode' allows to automatically insert hard spaces as one
+types the text.  Breaking line after a single-character words is
+forbidden by Czech and Polish typography (and may be discouraged in
+other languages), so `auto-tildify-mode' makes it easier to create
+a typographically-correct documents.
 
 * Incompatible Lisp Changes in Emacs 25.1
 
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index d935ff7..f5275d9 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -4,7 +4,7 @@
 
 ;; Author:     Milan Zamazal <pdm@zamazal.org>
 ;;             Michal Nazarewicz <mina86@mina86.com>
-;; Version:    4.5.7
+;; Version:    4.6
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -401,6 +401,92 @@ replacements done and response is one of symbols: t (all 
right), nil
                         (t t))))))
 
 
+;;; *** Tildify Mode ***
+
+(defcustom tildify-space-pattern "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvwz]"
+  "Pattern specifying whether to insert a hard space at point.
+
+If the pattern matches `looking-back', a hard space needs to be inserted 
instead
+of a space at point.  The regexp is always case sensitive, regardless of the
+current `case-fold-search' setting."
+  :version "25.1"
+  :group 'tildify
+  :type 'string)
+
+(defcustom tildify-space-predicates '(tildify-space-region-predicate)
+  "A list of predicate functions for `tildify-space' function."
+  :version "25.1"
+  :group 'tildify
+  :type '(repeat 'function))
+
+
+;;;###autoload
+(defun tildify-space ()
+  "Convert space before point into a hard space if the context is right.
+
+If
+ * character before point is a space character,
+ * character before that has “w” character syntax (i.e. it's a word
+   constituent),
+ * `tildify-space-pattern' matches when `looking-back' (no more than 10
+   characters) from before the space character, and
+ * all predicates in `tildify-space-predicates' return non-nil,
+replace the space character with a hard space specified by
+`tildify-space-string' (note that the function does not take
+`tildify-string-alist' into consideration).
+
+Return t if conversion happened, nil otherwise.
+
+This function is meant to be used as a `post-self-insert-hook'."
+  (interactive)
+  (let ((p (point)) case-fold-search)
+    (when (and (> (- p (point-min)) 2)
+               (eq (preceding-char) ?\s)
+               (eq (char-syntax (char-before (1- p))) ?w)
+               (not (string-equal " " tildify-space-string))
+               (save-excursion
+                 (goto-char (1- p))
+                 (looking-back tildify-space-pattern
+                               (max (point-min) (- p 10))))
+               (run-hook-with-args-until-failure 'tildify-space-predicates))
+      (delete-char -1)
+      (insert tildify-space-string)
+      t)))
+
+(defun tildify-space-region-predicate ()
+  "Check whether character before point should be tildified.
+Based on `tildify-foreach-region-function', check whether character before,
+which is assumed to be a space character, should be replaced with a hard 
space."
+  (catch 'found
+    (tildify--foreach-region (lambda (_b _e) (throw 'found t)) (1- (point)) 
(point))))
+
+;;;###autoload
+(define-minor-mode tildify-mode
+  "Adds electric behaviour to space character.
+
+When space is inserted into a buffer in a position where hard space is required
+instead (determined by `tildify-space-pattern' and `tildify-space-predicates'),
+that space character is replaced by a hard space specified by
+`tildify-space-string'.  Converting of the space is done by `tildify-space'.
+
+When `tildify-mode' is enabled, if `tildify-string-alist' specifies a hard 
space
+representation for current major mode, the `tildify-space-string' buffer-local
+variable will be set to the representation."
+  nil " ~" nil
+  (when tildify-mode
+    (let ((space (tildify--pick-alist-entry tildify-string-alist)))
+      (if (not (string-equal " " (or space tildify-space-string)))
+          (when space
+            (setq tildify-space-string space))
+        (message (eval-when-compile
+                   (concat "Hard space is a single space character, tildify-"
+                           "mode won't have any effect, disabling.")))
+        (setq tildify-mode nil))))
+  (if tildify-mode
+      (add-hook 'post-self-insert-hook 'tildify-space nil t)
+    (remove-hook 'post-self-insert-hook 'tildify-space t)))
+
+
 ;;; *** Announce ***
 
 (provide 'tildify)
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el
index 55d2d57..fc68787 100644
--- a/test/automated/tildify-tests.el
+++ b/test/automated/tildify-tests.el
@@ -185,6 +185,44 @@ The function must terminate as soon as callback returns 
nil."
       (+ (point-min) 10) (+ (point-min) 20)))) ; start at "3" end past "5"
 
 
+(defun tildify-space-test--test (modes nbsp env-open &optional 
set-space-string)
+  (with-temp-buffer
+    (dolist (mode modes)
+      (funcall mode)
+      (when set-space-string
+        (setq-local tildify-space-string nbsp))
+      (let ((header (concat "Testing `tildify-space' in "
+                            (symbol-name mode) "\n")))
+        ;; Replace space with hard space.
+        (erase-buffer)
+        (insert header "Lorem v ")
+        (should (tildify-space))
+        (should (string-equal (concat header "Lorem v" nbsp) (buffer-string)))
+        ;; Inside and ignore environment, replacing does not happen.
+        (erase-buffer)
+        (insert header env-open "Lorem v ")
+        (should (not (tildify-space)))
+        (should (string-equal (concat header env-open "Lorem v ")
+                              (buffer-string)))))))
+
+(ert-deftest tildify-space-test-html ()
+  "Tests auto-tildification in an HTML document"
+  (tildify-space-test--test '(html-mode sgml-mode) " " "<pre>"))
+
+(ert-deftest tildify-space-test-html-nbsp ()
+  "Tests auto-tildification in an HTML document"
+  (tildify-space-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>" t))
+
+(ert-deftest tildify-space-test-xml ()
+  "Tests auto-tildification in an XML document"
+  (tildify-space-test--test '(nxml-mode) " " "<! -- "))
+
+(ert-deftest tildify-space-test-tex ()
+  "Tests tildification in a TeX document"
+  (tildify-space-test--test '(tex-mode latex-mode plain-tex-mode)
+                            "~" "\\verb# "))
+
+
 (provide 'tildify-tests)
 
 ;;; tildify-tests.el ends here
-- 
2.1.0.rc2.206.gedb03e5






reply via email to

[Prev in Thread] Current Thread [Next in Thread]