From: Michal Nazarewicz <mina86@mina86.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>, 18730@debbugs.gnu.org
Cc: Milan Zamazal <pdm@zamazal.org>
Subject: bug#18730: [PATCHv2 2/2] tildify.el: Add `auto-tildify' and `auto-tildify-mode'.
Date: Thu, 16 Oct 2014 17:34:12 +0200 [thread overview]
Message-ID: <1413473652-26262-2-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <1413473652-26262-1-git-send-email-mina86@mina86.com>
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 | 5 +++
lisp/textmodes/tildify.el | 96 ++++++++++++++++++++++++++++++++++++++++-
test/automated/tildify-tests.el | 32 ++++++++++++++
3 files changed, 132 insertions(+), 1 deletion(-)
diff --git a/etc/NEWS b/etc/NEWS
index 36f1d9d..b6d4055 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -269,6 +269,11 @@ These emulations of old editors are believed to be no longer relevant
\f
* New Modes and Packages in Emacs 25.1
+** `tildify-mode' automatically hardens spaces as one types the text.
+Breaking line after a single-character words is forbidden by Czech and Polish
+typography. `tildify-mode' makes creating a typographically-correct markup in
+formats such as HTML, LaTeX, Markdown, etc.
+
\f
* Incompatible Lisp Changes in Emacs 25.1
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index 91f5a38..33067a6 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.4
+;; 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 tildify-space-pattern-alist
+ '((t . "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvwz]"))
+ "Alist specifying whether a hard space is required 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 tildify-space-check-envs t
+ "Should `tildify-space' check if point is inside ignored environment."
+ :group 'tildify
+ :type 'boolean)
+
+
+(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),
+ * pattern from `tildify-space-pattern-alist' matches when `looking-back' (no
+ more than 10 characters) from before the space character, and
+ * `tildify-space-check-envs' is nil or point is not inside of an environment to
+ ignore
+replace the space character with a hard space specified in
+`tildify-string-alist'.
+
+Return t if conversion happened, nil otherwise.
+
+This function is meant to be used as a `post-self-insert-hook'."
+ (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--pick-alist-entry tildify-string-alist))
+ (not (string-equal " " space))
+ (setq pattern (tildify--pick-alist-entry
+ tildify-space-pattern-alist))
+ (save-excursion
+ (goto-char (1- p))
+ (looking-back pattern (max (point-min) (- p 10))))
+ (or (not tildify-space-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 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 `tildify-space'."
+ nil " ~" nil
+ (when tildify-mode
+ (when (let ((space (tildify--pick-alist-entry tildify-string-alist)))
+ (or (not space) (string-equal " " space)))
+ (message (concat "Hard space for %s is a single space character, "
+ "tildify won't have any effect.") major-mode)
+ (setq tildify-mode nil))
+ (when (not (tildify--pick-alist-entry tildify-space-pattern-alist))
+ (message (concat "No pattern defined for %s, "
+ "tildify won't have any effect.") major-mode)
+ (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 53c2e62..7ba270a 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 tildify-space-test--test (modes nbsp env-open)
+ (with-temp-buffer
+ (dolist (mode modes)
+ (erase-buffer)
+ (funcall mode)
+ (let ((header (concat "Testing `tildify-space' in "
+ (symbol-name mode) "\n")))
+ (insert header "Lorem v ")
+ (should (tildify-space))
+ (should (string-equal (concat header "Lorem v" nbsp) (buffer-string))))
+ (erase-buffer)
+ (let ((header (concat "Testing `tildify-space' in "
+ (symbol-name mode) "\n")))
+ (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-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
next prev parent reply other threads:[~2014-10-16 15:34 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 ` Michal Nazarewicz [this message]
2014-10-16 19:30 ` 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 ` bug#18730: [PATCH 2/2] tildify.el: Add `tildify-double-space-undos' Michal Nazarewicz
2014-12-10 17:44 ` bug#18730: [PATCH 1/2] tildify.el: Add `tildify-space' and `tildify-mode' 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=1413473652-26262-2-git-send-email-mina86@mina86.com \
--to=mina86@mina86.com \
--cc=18730@debbugs.gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=pdm@zamazal.org \
/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).