unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 18730@debbugs.gnu.org
Subject: bug#18730: [PATCH 2/2] tildify.el: Add `tildify-double-space-undos'.
Date: Mon, 24 Nov 2014 15:20:09 +0100	[thread overview]
Message-ID: <1416838809-19397-2-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <1416838809-19397-1-git-send-email-mina86@mina86.com>

* 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/textmodes/tildify.el       | 53 +++++++++++++++++++++++++++--------------
 test/automated/tildify-tests.el | 33 +++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index f5275d9..3d82330 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.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/automated/tildify-tests.el b/test/automated/tildify-tests.el
index fc68787..8fd87ab 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) "&nbsp;" "<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
-- 
2.1.0.rc2.206.gedb03e5






  reply	other threads:[~2014-11-24 14:20 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-15  8:01 bug#18730: [PATCH] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Michal Nazarewicz
2014-10-15 14:35 ` Stefan Monnier
2014-10-16  9:34   ` Michal Nazarewicz
2014-10-16 14:03     ` Stefan Monnier
2014-10-16 14:57       ` Stefan Monnier
2014-10-16 16:07       ` Michal Nazarewicz
2014-10-16 19:39         ` Stefan Monnier
2014-10-17  8:44           ` Michal Nazarewicz
2014-10-17 13:06             ` Stefan Monnier
2014-10-22 23:19               ` Michal Nazarewicz
2014-10-24 22:51                 ` Stefan Monnier
2014-10-28 22:01                   ` bug#18730: [PATCH] tildify.el: introduce a `tildify-space-string' variable Michal Nazarewicz
2014-10-30 16:27                     ` Stefan Monnier
2014-11-03 15:59                       ` Michal Nazarewicz
2014-11-03 17:00                         ` Stefan Monnier
2014-11-17 15:41                           ` bug#18730: [PATCH 1/3] " Michal Nazarewicz
2014-11-17 15:41                             ` bug#18730: [PATCH 2/3] tildify.el: introduce a `tildify-pattern' variable Michal Nazarewicz
2014-11-17 15:41                             ` bug#18730: [PATCH 3/3] tildify.el: introduce a `tildify-foreach-region-function' variable Michal Nazarewicz
2014-11-17 17:38                             ` bug#18730: [PATCH 1/3] tildify.el: introduce a `tildify-space-string' variable Stefan Monnier
2014-10-16 13:17 ` bug#18730: [PATCH] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Ted Zlatanov
2014-10-16 14:16   ` Michal Nazarewicz
2014-10-16 14:55     ` Stefan Monnier
2014-10-16 17:17       ` Ted Zlatanov
2014-10-16 13:19 ` Ted Zlatanov
2014-10-16 15:34 ` bug#18730: [PATCHv2 1/2] tildify.el (tildify--pick-alist-entry): rename from tildify-mode-alist Michal Nazarewicz
2014-10-16 15:34   ` bug#18730: [PATCHv2 2/2] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Michal Nazarewicz
2014-10-16 19:30   ` bug#18730: [PATCHv2 1/2] tildify.el (tildify--pick-alist-entry): rename from tildify-mode-alist Stefan Monnier
2014-11-24 14:20 ` bug#18730: [PATCH 1/2] tildify.el: Add `tildify-space' and `tildify-mode' Michal Nazarewicz
2014-11-24 14:20   ` Michal Nazarewicz [this message]
2014-12-10 17:44   ` Michal Nazarewicz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1416838809-19397-2-git-send-email-mina86@mina86.com \
    --to=mina86@mina86.com \
    --cc=18730@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).