diff options
author | Michal Nazarewicz <mina86@mina86.com> | 2014-10-15 10:01:26 +0200 |
---|---|---|
committer | Michal Nazarewicz <mina86@mina86.com> | 2015-01-20 13:55:02 +0100 |
commit | 571441fc790fadfdc48a6287328828495719e990 (patch) | |
tree | 42be47520cddbf1f0f96bcab4ddfef4cade0d155 | |
parent | f9775f21fcddd3d3715cb3249090d99322488a45 (diff) |
tildify: add `tildify-space' and `tildify-mode'
* 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.
-rw-r--r-- | etc/NEWS | 5 | ||||
-rw-r--r-- | lisp/ChangeLog | 15 | ||||
-rw-r--r-- | lisp/textmodes/tildify.el | 88 | ||||
-rw-r--r-- | test/ChangeLog | 8 | ||||
-rw-r--r-- | test/automated/tildify-tests.el | 38 |
5 files changed, 153 insertions, 1 deletions
@@ -494,6 +494,11 @@ As a result of the above, these commands are now obsolete: ** let-alist is a new macro (and a package) that allows one to easily let-bind the values stored in an alist. +** `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/ChangeLog b/lisp/ChangeLog index 22a06a9168..8bcfe977a9 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,18 @@ +2014-01-20 Michal Nazarewicz <mina86@mina86.com> + + * 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' + 2015-01-20 Daniel Colascione <dancol@dancol.org> * vc/vc-dir.el (vc-dir): Default to repository root, not diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el index 9382b32845..b11d7dae27 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/ChangeLog b/test/ChangeLog index 4b9e7a9262..0325f2703f 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,11 @@ +2014-01-20 Michal Nazarewicz <mina86@mina86.com> + + * 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. + 2015-01-18 Stefan Monnier <monnier@iro.umontreal.ca> * automated/Makefile.in (EMACS_EXTRAOPT): New var. diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el index b1f3de94fc..dcbbbf137e 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) " " "<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 |