[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] tildify d9af6e0 2/2: tildify: add `tildify-double-space-un
From: |
Michal Nazarewicz |
Subject: |
[Emacs-diffs] tildify d9af6e0 2/2: tildify: add `tildify-double-space-undos' |
Date: |
Tue, 20 Jan 2015 12:56:32 +0000 |
branch: tildify
commit d9af6e01b87c0f73749936c9db6711d38f1d1274
Author: Michal Nazarewicz <address@hidden>
Commit: Michal Nazarewicz <address@hidden>
tildify: add `tildify-double-space-undos'
* lisp/textmodes/tildify.el (tildify-double-space-undos): A new
variable specifying whether pressing space in `tildify-mode' after
a space has been replaced with hard space undos the substitution.
(tildify-space): Add code branch for handling `tildify-doule-space'.
* tests/automated/tildify-tests.el (tildify-space-undo-test--test):
A new helper function for testing `tildify-double-space-undos'
behaviour in the `tildify-space' function.
(tildify-space-undo-test-html, tildify-space-undo-test-html-nbsp)
(tildify-space-undo-test-xml, tildify-space-undo-test-tex): New
tests for `tildify-doule-space-undos' behaviour.
---
lisp/ChangeLog | 7 +++++
lisp/textmodes/tildify.el | 53 +++++++++++++++++++++++++-------------
test/ChangeLog | 7 +++++
test/automated/tildify-tests.el | 33 ++++++++++++++++++++++++
4 files changed, 82 insertions(+), 18 deletions(-)
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 8bcfe97..6c2f8e8 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,12 @@
2014-01-20 Michal Nazarewicz <address@hidden>
+ * textmodes/tildify.el (tildify-double-space-undos): A new
+ variable specifying whether pressing space in `tildify-mode' after
+ a space has been replaced with hard space undos the substitution.
+ (tildify-space): Add code branch for handling `tildify-doule-space'.
+
+2014-01-20 Michal Nazarewicz <address@hidden>
+
* 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.
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index b11d7da..0eae67a 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -4,7 +4,7 @@
;; Author: Milan Zamazal <address@hidden>
;; Michal Nazarewicz <address@hidden>
-;; Version: 4.6
+;; Version: 4.6.1
;; Keywords: text, TeX, SGML, wp
;; This file is part of GNU Emacs.
@@ -419,6 +419,11 @@ current `case-fold-search' setting."
:group 'tildify
:type '(repeat 'function))
+(defcustom tildify-double-space-undos t
+ "Weather `tildify-space' should undo hard space when space is typed again."
+ :version "25.1"
+ :group 'tildify
+ :type 'boolean)
;;;###autoload
(defun tildify-space ()
@@ -431,27 +436,39 @@ If
* `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).
+replace the space character with value of `tildify-space-string' and
+return t.
-Return t if conversion happened, nil otherwise.
+Otherwise, if
+ * `tildify-double-space-undos' variable is non-nil,
+ * character before point is a space character, and
+ * text before that is a hard space as defined by
+ `tildify-space-string' variable,
+remove the hard space and leave only the space character.
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)))
+ (let* ((p (point)) (p-1 (1- p)) (n (- p (point-min)))
+ (l (length tildify-space-string)) (l+1 (1+ l))
+ case-fold-search)
+ (when (and (> n 2) (eq (preceding-char) ?\s))
+ (cond
+ ((and (eq (char-syntax (char-before p-1)) ?w)
+ (save-excursion
+ (goto-char p-1)
+ (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)
+ ((and tildify-double-space-undos
+ (> n l+1)
+ (string-equal tildify-space-string
+ (buffer-substring (- p l+1) p-1)))
+ (goto-char p-1)
+ (delete-char (- l))
+ (goto-char (1+ (point)))
+ nil)))))
(defun tildify-space-region-predicate ()
"Check whether character before point should be tildified.
diff --git a/test/ChangeLog b/test/ChangeLog
index 0325f27..c37b986 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,5 +1,12 @@
2014-01-20 Michal Nazarewicz <address@hidden>
+ * automated/tildify-tests.el (tildify-space-undo-test--test):
+ A new helper function for testing `tildify-double-space-undos'
+ behaviour in the `tildify-space' function.
+ (tildify-space-undo-test-html, tildify-space-undo-test-html-nbsp)
+ (tildify-space-undo-test-xml, tildify-space-undo-test-tex): New
+ tests for `tildify-doule-space-undos' behaviour.
+
* 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)
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el
index dcbbbf1..b53f58c 100644
--- a/test/automated/tildify-tests.el
+++ b/test/automated/tildify-tests.el
@@ -223,6 +223,39 @@ The function must terminate as soon as callback returns
nil."
"~" "\\verb# "))
+(defun tildify-space-undo-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 double-space-undos in "
+ (symbol-name mode) "\n")))
+ (erase-buffer)
+ (insert header "Lorem v" nbsp " ")
+ (should (not (tildify-space)))
+ (should (string-equal (concat header "Lorem v ") (buffer-string)))))))
+
+(ert-deftest tildify-space-undo-test-html ()
+ "Tests auto-tildification in an HTML document"
+ (tildify-space-undo-test--test '(html-mode sgml-mode) " " "<pre>"))
+
+(ert-deftest tildify-space-undo-test-html-nbsp ()
+ "Tests auto-tildification in an HTML document"
+ (tildify-space-undo-test--test '(html-mode sgml-mode) " " "<pre>" t))
+
+(ert-deftest tildify-space-undo-test-xml ()
+ "Tests auto-tildification in an XML document"
+ (tildify-space-undo-test--test '(nxml-mode) " " "<! -- "))
+
+(ert-deftest tildify-space-undo-test-tex ()
+ "Tests tildification in a TeX document"
+ (tildify-space-undo-test--test '(tex-mode latex-mode plain-tex-mode)
+ "~" "\\verb# "))
+
+
+
(provide 'tildify-tests)
;;; tildify-tests.el ends here