From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michal Nazarewicz Newsgroups: gmane.emacs.bugs Subject: bug#18730: [PATCH 1/2] tildify.el: Add `tildify-space' and `tildify-mode'. Date: Mon, 24 Nov 2014 15:20:08 +0100 Message-ID: <1416838809-19397-1-git-send-email-mina86@mina86.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1416838891 22615 80.91.229.3 (24 Nov 2014 14:21:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 24 Nov 2014 14:21:31 +0000 (UTC) Cc: 18730@debbugs.gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Nov 24 15:21:22 2014 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XsuVy-0007bj-CN for geb-bug-gnu-emacs@m.gmane.org; Mon, 24 Nov 2014 15:21:18 +0100 Original-Received: from localhost ([::1]:52896 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XsuVx-0003Ya-V1 for geb-bug-gnu-emacs@m.gmane.org; Mon, 24 Nov 2014 09:21:17 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XsuVp-0003WN-5v for bug-gnu-emacs@gnu.org; Mon, 24 Nov 2014 09:21:14 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XsuVi-00068I-Qb for bug-gnu-emacs@gnu.org; Mon, 24 Nov 2014 09:21:09 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:46134) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XsuVi-000688-N3 for bug-gnu-emacs@gnu.org; Mon, 24 Nov 2014 09:21:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1XsuVi-0000ig-Hz for bug-gnu-emacs@gnu.org; Mon, 24 Nov 2014 09:21:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Michal Nazarewicz Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 24 Nov 2014 14:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 18730 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 18730-submit@debbugs.gnu.org id=B18730.14168388182691 (code B ref 18730); Mon, 24 Nov 2014 14:21:02 +0000 Original-Received: (at 18730) by debbugs.gnu.org; 24 Nov 2014 14:20:18 +0000 Original-Received: from localhost ([127.0.0.1]:43346 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1XsuUz-0000hK-LN for submit@debbugs.gnu.org; Mon, 24 Nov 2014 09:20:18 -0500 Original-Received: from mail-lb0-f201.google.com ([209.85.217.201]:46327) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1XsuUu-0000gv-Mr for 18730@debbugs.gnu.org; Mon, 24 Nov 2014 09:20:14 -0500 Original-Received: by mail-lb0-f201.google.com with SMTP id 10so157494lbg.0 for <18730@debbugs.gnu.org>; Mon, 24 Nov 2014 06:20:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=f1Nm/UEn/RGAKt6apAgdbLdYn6tdnAYR7pV8apvOuWY=; b=UYzT4PMJ0DZfYVxFKSHIos1w2+4SYL9cMWnfRXDHEP4dHYuLgjPCgA7Je2tvOQ4zZF DHvKGZAD08SXiuCfjXsNDSLuZxI8B1Tz//JJpFnYo9AtQdtyS8M0hBdiyqgClzlxvRDj W/xwxfupXYWZNmuwzfiZpCTtdZ08VypYacOGA+b/ViI3x9FV/Iq90GeFWkbkynC7Fldm FOLEC4trIAKTIzxayO2b3494lQtZHxYlO8uzgB/pdIy98lr8Mc4fL4UIZU4ZlUAdCGvK BYUwX2erX4esxsb11EqaJT7nA7/h/uc+ir8iWHo6tt1wsAPZbU2f9/Oxm3CngspFv+wD HBhg== X-Gm-Message-State: ALoCoQlO7S9fjtAJy7nmIt8Qoj8Z7xOJNGP8eH+5JDkq6NnY16GQ5gsSDqMMjAauULszpefiO+As8s4WOLBxfy65cOBxnysMRMtgd9VkRQnDlGKsvjKi3XOnXSuGN3S2UDM6oBTmLj9Lj9LWw0XOF2BxYI5cpsrecxHdVeQLp/L1Srock+0hnJM= X-Received: by 10.112.14.2 with SMTP id l2mr1443046lbc.5.1416838811684; Mon, 24 Nov 2014 06:20:11 -0800 (PST) Original-Received: from corpmail-nozzle1-2.eem.corp.google.com ([172.25.144.13]) by gmr-mx.google.com with ESMTPS id el8si432754wib.3.2014.11.24.06.20.11 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Nov 2014 06:20:11 -0800 (PST) Original-Received: from mpn.zrh.corp.google.com ([172.16.113.113]) by corpmail-nozzle1-2.eem.corp.google.com with ESMTP id e5FIIN5U.1; Mon, 24 Nov 2014 06:20:11 -0800 Original-Received: by mpn.zrh.corp.google.com (Postfix, from userid 126942) id 20C541E0184; Mon, 24 Nov 2014 15:20:11 +0100 (CET) X-Mailer: git-send-email 2.1.0.rc2.206.gedb03e5 In-Reply-To: X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:96500 Archived-At: * 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. --- etc/NEWS | 6 +++ lisp/textmodes/tildify.el | 88 +++++++++++++++++++++++++++++++++++= +++++- test/automated/tildify-tests.el | 38 ++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index 5154066..0c01560 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -346,6 +346,12 @@ helper functions) obsolete. * New Modes and Packages in Emacs 25.1 =20 ** scss-mode (a minor variant of css-mode) + +** `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. =0C * Incompatible Lisp Changes in Emacs 25.1 =20 diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el index d935ff7..f5275d9 100644 --- a/lisp/textmodes/tildify.el +++ b/lisp/textmodes/tildify.el @@ -4,7 +4,7 @@ =20 ;; Author: Milan Zamazal ;; Michal Nazarewicz -;; Version: 4.5.7 +;; Version: 4.6 ;; Keywords: text, TeX, SGML, wp =20 ;; 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)))))) =20 =20 +;;; *** Tildify Mode *** + +(defcustom tildify-space-pattern "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvw= z]" + "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 =E2=80=9Cw=E2=80=9D 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-predicat= es)) + (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 befo= re, +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- (poin= t)) (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 r= equired +instead (determined by `tildify-space-pattern' and `tildify-space-predic= ates'), +that space character is replaced by a hard space specified by +`tildify-space-string'. Converting of the space is done by `tildify-spa= ce'. + +When `tildify-mode' is enabled, if `tildify-string-alist' specifies a ha= rd 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, tild= ify-" + "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 *** =20 (provide 'tildify) diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tes= ts.el index 55d2d57..fc68787 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 retu= rns nil." (+ (point-min) 10) (+ (point-min) 20)))) ; start at "3" end past "= 5" =20 =20 +(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-str= ing))) + ;; 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) "=C2=A0" "
"))
+
+(ert-deftest tildify-space-test-html-nbsp ()
+  "Tests auto-tildification in an HTML document"
+  (tildify-space-test--test '(html-mode sgml-mode) " " "
" t))
+
+(ert-deftest tildify-space-test-xml ()
+  "Tests auto-tildification in an XML document"
+  (tildify-space-test--test '(nxml-mode) "=C2=A0" "