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

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

bug#18730: [PATCH] tildify.el: Add `auto-tildify' and `auto-tildify-mode


From: Michal Nazarewicz
Subject: bug#18730: [PATCH] tildify.el: Add `auto-tildify' and `auto-tildify-mode'.
Date: Wed, 15 Oct 2014 10:01:26 +0200
User-agent: Notmuch/0.17+15~gb65ca8e (http://notmuchmail.org) Emacs/24.4.50.1 (x86_64-unknown-linux-gnu)

The `auto-tildify' function can be used as
a `post-self-insert-hook' to automatically convert spaces into
hard spaces.  It is configured via two new customize variables:
`auto-tildify-pattern-alist' and `auto-tildify-check-envs'.

`auto-tildify-mode' makes use of that function to enable
eletric behaviour of space character.
---
 etc/NEWS                        |  6 +++
 lisp/textmodes/tildify.el       | 96 ++++++++++++++++++++++++++++++++++++++++-
 test/automated/tildify-tests.el | 32 ++++++++++++++
 3 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index e336ff5..7ef73a4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -254,6 +254,12 @@ These emulations of old editors are believed to be no 
longer relevant

 * New Modes and Packages in Emacs 25.1
 
+** `auto-tildify-mode' allows to automatically add hard spaces as one types
+the text.  Breaking line after a single-character words are 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 50fee2f..aed060a 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.3
+;; Version:    4.6
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -344,6 +344,100 @@ replacements done and response is one of symbols: t (all 
right), nil
                         (t t))))))
 
 
+;;; *** Auto Tildify ***
+
+(defcustom auto-tildify-pattern-alist
+  '((t . "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvwz]"))
+  "Alist specifying whether to insert hard space at point.
+
+Each alist item is of the form (MAJOR-MODE . REGEXP) or
+\(MAJOR-MODE . SYMBOL).
+
+MAJOR-MODE defines major mode, for which the item applies.  It can be either:
+- a symbol equal to the major mode of the buffer to be fixed
+- t for default item, this applies to all major modes not defined in another
+  alist item
+
+REGEXP is a regular expression matching the part of a text that
+needs a hard space to be inserted instead of a space.  The regexp
+is always case sensitive, regardless of the current
+`case-fold-search' setting.
+
+The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE.  For this
+mode, the item for the mode SYMBOL is looked up in the alist instead."
+  :group 'tildify
+  :type '(repeat (cons (choice (const  :tag "Default" t)
+                               (symbol :tag "For mode "))
+                       (choice (regexp :tag "Regexp   ")
+                               (symbol :tag "Like mode")))))
+
+(defcustom auto-tildify-check-envs t
+  "Should `auto-tildify' check if point is inside ignored environment."
+  :group 'tildify
+  :type 'boolean)
+
+
+;;;###autoload
+(defun auto-tildify ()
+  "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),
+ * pattern from `auto-tildify-pattern-alist' matches when
+   `looking-back' (no more than 10 characters) from before the space
+   character, and
+ * `auto-tildify-check-envs' is nil or point is not inside of an
+   environment to ignore
+replace the space character with a hard space defined in
+`auto-tildify-string'.
+
+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 space pattern)
+    (when (and (> (- p (point-min)) 2)
+               (eq (preceding-char) ?\s)
+               (eq (char-syntax (char-before (1- p))) ?w)
+               (setq space (tildify-mode-alist tildify-string-alist))
+               (not (string-equal " " space))
+               (setq pattern (tildify-mode-alist auto-tildify-pattern-alist))
+               (save-excursion
+                 (goto-char (1- p))
+                 (looking-back pattern (max (point-min) (- p 10))))
+               (or (not auto-tildify-check-envs)
+                   (catch 'found
+                     (tildify-foreach-region-outside-env (- p 2) (1- p)
+                       (lambda (beg end) (throw 'found t))))))
+      (delete-char -1)
+      (insert space)
+      t)))
+
+;;;###autoload
+(define-minor-mode auto-tildify-mode
+  "Adds electric behaviour to space character.
+
+When space is inserted into a buffer in a position where hard
+space is required instead, that space character is replaced by
+a hard space correct for given mode.
+
+Converting of the space is done by `auto-tildify'."
+  nil " ~" nil
+  (when auto-tildify-mode
+    (when (let ((space (tildify-mode-alist tildify-string-alist)))
+            (or (not space) (string-equal " " space)))
+      (message "Hard space for %s is a single space character, auto-tildify 
won't have any effect." major-mode)
+      (setq auto-tildify-mode nil))
+    (when (not (tildify-mode-alist auto-tildify-pattern-alist))
+      (message "No auto-pattern defined for %s, auto-tildify won't have any 
effect." major-mode)
+      (setq auto-tildify-mode nil)))
+  (if auto-tildify-mode
+      (add-hook 'post-self-insert-hook 'auto-tildify nil t)
+    (remove-hook 'post-self-insert-hook 'auto-tildify t)))
+
+
 ;;; *** Announce ***
 
 (provide 'tildify)
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el
index 53c2e62..f8a4d8c 100644
--- a/test/automated/tildify-tests.el
+++ b/test/automated/tildify-tests.el
@@ -132,6 +132,38 @@ latter is missing, SENTENCE will be used in all 
placeholder positions."
       (should (string-equal "close-foo" (tildify-find-env beg-re pairs))))))
 
 
+(defun auto-tildify-test--test (modes nbsp env-open)
+  (with-temp-buffer
+    (dolist (mode modes)
+      (erase-buffer)
+      (funcall mode)
+      (let ((header (concat "Testing `auto-tildify' in "
+                            (symbol-name mode) "\n")))
+        (insert header "Lorem v ")
+        (should (auto-tildify))
+        (should (string-equal (concat header "Lorem v" nbsp) (buffer-string))))
+      (erase-buffer)
+      (let ((header (concat "Testing `auto-tildify' in "
+                            (symbol-name mode) "\n")))
+        (insert header env-open "Lorem v ")
+        (should (not (auto-tildify)))
+        (should (string-equal (concat header env-open "Lorem v ")
+                              (buffer-string)))))))
+
+(ert-deftest auto-tildify-test-html ()
+  "Tests auto-tildification in an HTML document"
+  (auto-tildify-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>"))
+
+(ert-deftest auto-tildify-test-xml ()
+  "Tests auto-tildification in an XML document"
+  (auto-tildify-test--test '(nxml-mode) "&#160;" "<! -- "))
+
+(ert-deftest auto-tildify-test-tex ()
+  "Tests tildification in a TeX document"
+  (auto-tildify-test--test '(tex-mode latex-mode plain-tex-mode)
+                           "~" "\\verb# "))
+
+
 (provide 'tildify-tests)
 
 ;;; tildify-tests.el ends here





reply via email to

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