all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH 0/5] Auto tildify improvements
@ 2014-03-02 21:55 Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 1/5] tildify.el: Improve defcustom's types Michal Nazarewicz
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-03-02 21:55 UTC (permalink / raw
  To: emacs-devel, Stefan Monnier, Pavel Janík, Milan Zamazal

From: Michal Nazarewicz <mina86@mina86.com>

Some smaller changes at the end leading to definition of a `auto-tildify-mode'
to automatically insert hard spaces where one is desired.

Michal Nazarewicz (5):
  tildify.el: Improve defcustom's types.
  tildify.el: Change XML hard space to numeric reference.
  tildify.el: Optimise environments regexes
  tildify.el: Rewrite `tildify-region' and co., add foreach function.
  tildify.el: Add `auto-tildify' and `auto-tildify-mode'.

 lisp/textmodes/tildify.el | 338 +++++++++++++++++++++++++++++-----------------
 1 file changed, 214 insertions(+), 124 deletions(-)

-- 
1.9.0.279.gdc9e3eb




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] tildify.el: Improve defcustom's types.
  2014-03-02 21:55 [PATCH 0/5] Auto tildify improvements Michal Nazarewicz
@ 2014-03-02 21:55 ` Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 2/5] tildify.el: Change XML hard space to numeric reference Michal Nazarewicz
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-03-02 21:55 UTC (permalink / raw
  To: emacs-devel, Stefan Monnier, Pavel Janík, Milan Zamazal

From: Michal Nazarewicz <mina86@mina86.com>

Add more tags explaining what each value means and replace
“sexp” used in `tildify-ignored-environments-alist' with
a full type declaration.
---
 lisp/textmodes/tildify.el | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index 9732e7f..f534bdb 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
 
 ;; Author:     Milan Zamazal <pdm@zamazal.org>
-;; Version:    4.5
+;; Version:    4.5.1
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -77,7 +77,13 @@ by the hard space character.
 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 (choice (list symbol regexp integer) (cons symbol symbol))))
+  :type '(repeat (cons :tag "Entry for major mode"
+                       (choice (const  :tag "Default" t)
+                               (symbol :tag "Major mode"))
+                       (choice (list   :tag "Regexp"
+                                       regexp
+                                       (integer :tag "Group "))
+                               (symbol :tag "Like other")))))
 
 (defcustom tildify-string-alist
   '((latex-mode . "~")
@@ -104,7 +110,12 @@ for SGML.
 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 symbol (choice string symbol))))
+  :type '(repeat (cons :tag "Entry for major mode"
+                       (choice (const  :tag "Default" t)
+                               (symbol :tag "Major mode"))
+                       (choice (const  :tag "No-break space (U+00A0)" "\u00A0")
+                               (string :tag "String    ")
+                               (symbol :tag "Like other")))))
 
 (defcustom tildify-ignored-environments-alist
   '((latex-mode
@@ -160,7 +171,22 @@ END-REGEX defines end of the corresponding text part and can be either:
   subexpressions of BEG-REGEX (this is used to solve cases like
   \\\\verb<character> in TeX)."
   :group 'tildify
-  :type '(repeat (cons symbol (choice symbol (repeat sexp)))))
+  :type '(repeat
+          (cons :tag "Entry for major mode"
+                (choice (const  :tag "Default" t)
+                        (symbol :tag "Major mode"))
+                (choice
+                 (const  :tag "None")
+                 (repeat
+                  :tag "Environments"
+                  (cons :tag "Regexp pair"
+                        (regexp :tag "Open ")
+                        (choice :tag "Close"
+                                (regexp :tag "Regexp")
+                                (list :tag "Regexp and groups (concatenated)"
+                                      (choice (regexp  :tag "Regexp")
+                                              (integer :tag "Group "))))))
+                 (symbol :tag "Like other")))))
 
 
 ;;; *** Internal variables ***
-- 
1.9.0.279.gdc9e3eb




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/5] tildify.el: Change XML hard space to numeric reference.
  2014-03-02 21:55 [PATCH 0/5] Auto tildify improvements Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 1/5] tildify.el: Improve defcustom's types Michal Nazarewicz
@ 2014-03-02 21:55 ` Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 3/5] tildify.el: Optimise environments regexes Michal Nazarewicz
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-03-02 21:55 UTC (permalink / raw
  To: emacs-devel, Stefan Monnier, Pavel Janík, Milan Zamazal

From: Michal Nazarewicz <mina86@mina86.com>

XML does not define “&nbsp;” and so the proper entity reference
for a no-break space is numeric one such as “&#160;”.  While at
it add `nxml-mode' to the list of modes.
---
 lisp/textmodes/tildify.el | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index f534bdb..dc23fbe 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
 
 ;; Author:     Milan Zamazal <pdm@zamazal.org>
-;; Version:    4.5.1
+;; Version:    4.5.2
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -90,8 +90,9 @@ mode, the item for the mode SYMBOL is looked up in the alist instead."
     (tex-mode . latex-mode)
     (plain-tex-mode . latex-mode)
     (sgml-mode . "&nbsp;")
-    (xml-mode . sgml-mode)
     (html-mode . sgml-mode)
+    (xml-mode . "&#160;") ; XML does not define &nbsp; so use numeric reference
+    (nxml-mode . xml-mode)
     (t . " "))
   "Alist specifying what is a hard space in the current major mode.
 
-- 
1.9.0.279.gdc9e3eb




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/5] tildify.el: Optimise environments regexes
  2014-03-02 21:55 [PATCH 0/5] Auto tildify improvements Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 1/5] tildify.el: Improve defcustom's types Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 2/5] tildify.el: Change XML hard space to numeric reference Michal Nazarewicz
@ 2014-03-02 21:55 ` Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 4/5] tildify.el: Rewrite `tildify-region' and co., add foreach function Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 5/5] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Michal Nazarewicz
  4 siblings, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-03-02 21:55 UTC (permalink / raw
  To: emacs-devel, Stefan Monnier, Pavel Janík, Milan Zamazal

From: Michal Nazarewicz <mina86@mina86.com>

Each time beginning of an environment to ignore is found,
`tildify-find-env' needs to identify regexp for the ending
of the environment.  This is done by trying all the opening
regexes on matched text in a loop, so to speed that up, this
loop should have fewer things to match, which can be done by
using alternatives in the opening regexes.

Coincidentally, this should make matching of the opening
regexp faster as well thanks to the use of `regexp-opt' and
having common prefix pulled from many regexes.
---
 lisp/textmodes/tildify.el | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)

diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index dc23fbe..9accce8 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
 
 ;; Author:     Milan Zamazal <pdm@zamazal.org>
-;; Version:    4.5.2
+;; Version:    4.5.3
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -119,38 +119,35 @@ mode, the item for the mode SYMBOL is looked up in the alist instead."
                                (symbol :tag "Like other")))))
 
 (defcustom tildify-ignored-environments-alist
-  '((latex-mode
+  `((latex-mode
      ("\\\\\\\\" . "")		; do not remove this
-     ("\\\\begin{verbatim}" . "\\\\end{verbatim}")
+     (,(eval-when-compile (concat
+                           "\\\\begin{\\("
+                           (regexp-opt '("verbatim" "math" "displaymath"
+                                         "equation" "eqnarray" "eqnarray*"))
+                           "}"))
+      "\\\\end{" 1 "}")
      ("\\\\verb\\*?\\(.\\)" . (1))
-     ("\\$\\$" . "\\$\\$")
-     ("\\$" . "\\$")
+     ("\\$\\$?" 0)
      ("\\\\(" . "\\\\)")
      ("\\\\[[]" . "\\\\[]]")
-     ("\\\\begin{math}" . "\\\\end{math}")
-     ("\\\\begin{displaymath}" . "\\\\end{displaymath}")
-     ("\\\\begin{equation}" . "\\\\end{equation}")
-     ("\\\\begin{eqnarray\\*?}" . "\\\\end{eqnarray\\*?}")
      ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
      ("%" . "$"))
     (plain-tex-mode . latex-mode)
     (html-mode
-     ("<pre[^>]*>" . "</pre>")
-     ("<dfn>" . "</dfn>")
-     ("<code>" . "</code>")
-     ("<samp>" . "</samp>")
-     ("<kbd>" . "</kbd>")
-     ("<var>" . "</var>")
-     ("<PRE[^>]*>" . "</PRE>")
-     ("<DFN>" . "</DFN>")
-     ("<CODE>" . "</CODE>")
-     ("<SAMP>" . "</SAMP>")
-     ("<KBD>" . "</KBD>")
-     ("<VAR>" . "</VAR>")
+     (,(eval-when-compile (concat
+                           "<\\("
+                           (regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var"
+                                         "PRE" "DFN" "CODE" "SAMP" "KBD" "VAR"))
+                           "\\)\\>[^>]*>"))
+      "</" 1 ">")
      ("<! *--" . "-- *>")
      ("<" . ">"))
     (sgml-mode . html-mode)
-    (t nil))
+    (xml-mode
+     ("<! *--" . "-- *>")
+     ("<" . ">"))
+    (nxml-mode . html-mode))
   "Alist specifying ignored structured text environments.
 Parts of text defined in this alist are skipped without performing hard space
 insertion on them.  These setting allow skipping text parts like verbatim or
-- 
1.9.0.279.gdc9e3eb




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/5] tildify.el: Rewrite `tildify-region' and co., add foreach function.
  2014-03-02 21:55 [PATCH 0/5] Auto tildify improvements Michal Nazarewicz
                   ` (2 preceding siblings ...)
  2014-03-02 21:55 ` [PATCH 3/5] tildify.el: Optimise environments regexes Michal Nazarewicz
@ 2014-03-02 21:55 ` Michal Nazarewicz
  2014-03-02 21:55 ` [PATCH 5/5] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Michal Nazarewicz
  4 siblings, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-03-02 21:55 UTC (permalink / raw
  To: emacs-devel, Stefan Monnier, Pavel Janík, Milan Zamazal

From: Michal Nazarewicz <mina86@mina86.com>

Pull the loop looking for environments to ignore out of
`tildify-region' function into a new
`tildify-foreach-region-outside-env' function which takes
callback argument.  With that function, rewrite
`tildify-region' to be considerably shorter.

This also gets rid of `tildify-build-regexp' function whose
functionality is now inlined in the foreach function and
thanks to the use of `mapconcat' much shorter then
`tildify-build-regexp' has been.

Furthermore rewrite `tildify-find-env' so that it takes
pairs of regexes as an argument instead of having to look it
up in `tildify-ignored-environments-alist' each time.  Move to
use `mapconcat' also shortened the function a bit.

Lastly, rewrite `tildify-tildify' so that it returns number of
applied changes along the response.  This allows to get rid of
the `tildify-count' variable.
---
 lisp/textmodes/tildify.el | 171 +++++++++++++++++++---------------------------
 1 file changed, 72 insertions(+), 99 deletions(-)

diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index 9accce8..d18721a 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -3,7 +3,8 @@
 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
 
 ;; Author:     Milan Zamazal <pdm@zamazal.org>
-;; Version:    4.5.3
+;;             Michal Nazarewicz <mina86@mina86.com>
+;; Version:    4.5.4
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -187,12 +188,6 @@ END-REGEX defines end of the corresponding text part and can be either:
                  (symbol :tag "Like other")))))
 
 
-;;; *** Internal variables ***
-
-(defvar tildify-count nil
-  "Counter for replacements.")
-
-
 ;;; *** Interactive functions ***
 
 ;;;###autoload
@@ -203,51 +198,16 @@ See variables `tildify-pattern-alist', `tildify-string-alist', and
 parameters.
 This function performs no refilling of the changed text."
   (interactive "*r")
-  (setq tildify-count 0)
-  (let (a
-	z
-	(marker-end (copy-marker end))
-	end-env
-	finish
-	(ask t)
-	(case-fold-search nil)
-	(regexp (tildify-build-regexp))	; beginnings of environments
-	aux)
-    (if regexp
-	;; Yes, ignored environments exist for the current major mode,
-	;; tildify just texts outside them
-	(save-excursion
-	  (save-restriction
-	    (widen)
-	    (goto-char (point-min))
-	    (while (not finish)
-	      (setq a (point))
-	      (setq end-env (tildify-find-env regexp))
-	      (setq z (copy-marker (if end-env (1- (point)) (point-max))))
-	      (if (>= (marker-position z) beg)
-		  (progn
-		    (or (>= a beg) (setq a beg))
-		    (or (<= (marker-position z) (marker-position marker-end))
-			(setq z marker-end))
-		    (setq aux (tildify-tildify a (marker-position z) ask))
-		    (if (eq aux 'force)
-			(setq ask nil)
-		      (if (eq aux nil)
-			  (setq finish t)))))
-	      (if (>= (marker-position z) (marker-position marker-end))
-		  (setq finish t))
-	      (or (>= (point) (marker-position z))
-		  (goto-char (marker-position z)))
-	      (if (not finish)
-		  (if (re-search-forward end-env nil t)
-		      (if (> (point) (marker-position marker-end))
-			  (setq finish t))
-		    (message
-		     "End of environment not found: %s" end-env)
-		    (setq finish t))))))
-      ;; No ignored environments, tildify directly
-      (tildify-tildify beg end ask)))
-  (message "%d spaces replaced." tildify-count))
+  (let (case-fold-search (count 0) (ask t))
+    (tildify-foreach-region-outside-env beg end
+      (lambda (beg end)
+        (let ((aux (tildify-tildify beg end ask)))
+          (setq count (+ count (car aux)))
+          (if (not (eq (cdr aux) 'force))
+              (cdr aux)
+            (setq ask nil)
+            t))))
+    (message "%d spaces replaced." count)))
 
 ;;;###autoload
 (defun tildify-buffer ()
@@ -262,55 +222,67 @@ This function performs no refilling of the changed text."
 
 ;;; *** Auxiliary functions ***
 
-(defun tildify-build-regexp ()
-  "Build start of environment regexp."
-  (let ((alist (tildify-mode-alist tildify-ignored-environments-alist))
-	regexp)
-    (when alist
-      (setq regexp (caar alist))
-      (setq alist (cdr alist))
-      (while alist
-	(setq regexp (concat regexp "\\|" (caar alist)))
-	(setq alist (cdr alist)))
-      regexp)))
-
 (defun tildify-mode-alist (mode-alist &optional mode)
   "Return alist item for the MODE-ALIST in the current major MODE."
-  (if (null mode)
-      (setq mode major-mode))
-  (let ((alist (cdr (or (assoc mode mode-alist)
+  (let ((alist (cdr (or (assoc (or mode major-mode) mode-alist)
 			(assoc t mode-alist)))))
     (if (and alist
 	     (symbolp alist))
 	(tildify-mode-alist mode-alist alist)
       alist)))
 
-(defun tildify-find-env (regexp)
+(defun tildify-foreach-region-outside-env (beg end callback)
+  "Scan region from BEG to END calling CALLBACK on portions out of environments.
+Call CALLBACK on each region outside of environment to ignore.
+CALLBACK will only be called for regions which have intersection
+with [BEG END].  It must be a function that takes two point
+arguments specifying the region to operate on.  Stop scanning the
+region as soon as CALLBACK returns nil.  Environments to ignore
+are determined from `tildify-ignored-environments-alist'."
+  (declare (indent 2))
+  (let* ((pairs (tildify-mode-alist tildify-ignored-environments-alist))
+         (beg-re (if pairs (mapconcat 'car pairs "\\|") pairs)))
+    (if (not pairs)
+        (funcall callback beg end)
+      (let ((func (lambda (b e)
+                    (let ((b (min b beg)) (e (min e beg)))
+                    (if (< b e) (funcall callback beg end) t))))
+            p end-re)
+        (save-excursion
+          (save-restriction
+            (widen)
+            (goto-char (point-min))
+            (while (and (< (setq p (point)) end)
+                        (if (not (setq end-re
+                                       (tildify-find-env beg-re pairs)))
+                            (progn (funcall func p end) nil)
+                          (funcall func p (match-beginning 0))
+                          (when (< (point) end)
+                            (setq p (point))
+                            (if (re-search-forward end-re nil t)
+                                t
+                              (funcall func p end)
+                              nil)))))))))))
+
+(defun tildify-find-env (regexp pairs)
   "Find environment using REGEXP.
-Return regexp for the end of the environment or nil if no environment was
-found."
+Return regexp for the end of the environment found in PAIRS or nil if
+no environment was found."
   ;; Find environment
-  (if (re-search-forward regexp nil t)
-      ;; Build end-env regexp
-      (let ((match (match-string 0))
-	    (alist (tildify-mode-alist tildify-ignored-environments-alist))
-	    expression)
-	(save-match-data
-	  (while (not (eq (string-match (caar alist) match) 0))
-	    (setq alist (cdr alist))))
-	(if (stringp (setq expression (cdar alist)))
-	    expression
-	  (let ((result "")
-		aux)
-	    (while expression
-	      (setq result (concat result
-				   (if (stringp (setq aux (car expression)))
-				       expression
-				     (regexp-quote (match-string aux)))))
-	      (setq expression (cdr expression)))
-	    result)))
-    ;; Return nil if not found
-    nil))
+  (when (re-search-forward regexp nil t)
+    ;; Find regexp pair that matched
+    (let ((match (match-string 0)))
+      (save-match-data
+        (while (not (eq (string-match (caar pairs) match) 0))
+          (setq pairs (cdr pairs)))))
+      ;; Build end-re regexp
+      (let ((expression (cdar pairs)))
+        (if (stringp expression)
+            expression
+          (mapconcat (lambda (el) (if (stringp el) el
+                                    expression
+                                    (regexp-quote (match-string el))))
+                     expression "")))))
 
 (defun tildify-tildify (beg end ask)
   "Add tilde characters in the region between BEG and END.
@@ -319,8 +291,9 @@ macros.
 
 If ASK is nil, perform replace without asking user for confirmation.
 
-Returns one of symbols: t (all right), nil (quit), force (replace without
-further questions)."
+Returns (count . response) cons where count is number of string
+replacements done and response is one of symbols: t (all right), nil
+(quit), force (replace without further questions)."
   (save-excursion
     (goto-char beg)
     (let* ((alist (tildify-mode-alist tildify-pattern-alist))
@@ -332,7 +305,8 @@ further questions)."
 	   bad-answer
 	   replace
 	   quit
-	   (message-log-max nil))
+	   (message-log-max nil)
+	   (count 0))
       (while (and (not quit)
 		  (re-search-forward regexp (marker-position end-marker) t))
 	(when (or (not ask)
@@ -359,12 +333,11 @@ further questions)."
 		      (setq bad-answer t)))
 		    replace))
 	  (replace-match tilde t t nil match-number)
-	  (setq tildify-count (1+ tildify-count))))
+	  (setq count (1+ count))))
       ;; Return value
-      (cond
-       (quit nil)
-       ((not ask) 'force)
-       (t t)))))
+      (cons count (cond (quit nil)
+                        ((not ask) 'force)
+                        (t t))))))
 
 
 ;;; *** Announce ***
-- 
1.9.0.279.gdc9e3eb




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 5/5] tildify.el: Add `auto-tildify' and `auto-tildify-mode'.
  2014-03-02 21:55 [PATCH 0/5] Auto tildify improvements Michal Nazarewicz
                   ` (3 preceding siblings ...)
  2014-03-02 21:55 ` [PATCH 4/5] tildify.el: Rewrite `tildify-region' and co., add foreach function Michal Nazarewicz
@ 2014-03-02 21:55 ` Michal Nazarewicz
  4 siblings, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-03-02 21:55 UTC (permalink / raw
  To: emacs-devel, Stefan Monnier, Pavel Janík, Milan Zamazal

From: Michal Nazarewicz <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.
---
 lisp/textmodes/tildify.el | 95 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 94 insertions(+), 1 deletion(-)

diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index d18721a..9228bb6 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.
@@ -340,6 +340,99 @@ replacements done and response is one of symbols: t (all right), nil
                         (t t))))))
 
 
+;;; *** Auto Tildify ***
+
+(defcustom auto-tildify-pattern-alist
+  '((t . "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvwz]"))
+  "Alist specifying whether to insert hard space 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 auto-tildify-check-envs t
+  "Should `auto-tildify' check if point is inside ignored environment."
+  :group 'tildify
+  :type 'boolean)
+
+
+;;;###autoload
+(defun auto-tildify ()
+  "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 `auto-tildify-pattern-alist' matches when
+   `looking-back' (no more than 10 characters) from before the space
+   character, and
+ * `auto-tildify-check-envs' is nil or point is not inside of an
+   environment to ignore
+replace the space character with a hard space defined in
+`auto-tildify-string'.
+
+Return t if conversion happened, nil otherwise.
+
+This function is meant to be used as a `post-self-insert-hook'."
+  (interactive)
+  (let (case-fold-search space pattern)
+    (when (and (> (- (point) (point-min)) 2)
+               (eq (preceding-char) ?\s)
+               (eq (char-syntax (char-before (1- (point)))) ?w)
+               (setq space (tildify-mode-alist tildify-string-alist))
+               (not (string-equal " " space))
+               (setq pattern (tildify-mode-alist auto-tildify-pattern-alist))
+               (save-excursion
+                 (goto-char (1- (point)))
+                 (looking-back pattern (max (point-min) (- (point) 10))))
+               (or (not auto-tildify-check-envs)
+                   (let (found)
+                     (tildify-foreach-region-outside-env
+                         (- (point) 2) (1- (point))
+                       (lambda (beg end) (setq found t) nil))
+                     found)))
+      (delete-char -1)
+      (insert space)
+      t)))
+
+;;;###autoload
+(define-minor-mode auto-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 `auto-tildify'."
+  nil " ~" nil
+  (when (let ((space (tildify-mode-alist tildify-string-alist)))
+          (or (not space) (string-equal " " space)))
+    (message "Hard space for %s is single space character, auto-tildify won't have any effect." major-mode))
+  (when (not (tildify-mode-alist auto-tildify-pattern-alist))
+    (message "No auto-pattern defined for %s, auto-tildify won't have any effect." major-mode))
+  (if auto-tildify-mode
+      (add-hook 'post-self-insert-hook 'auto-tildify nil t)
+    (remove-hook 'post-self-insert-hook 'auto-tildify t)))
+
+
 ;;; *** Announce ***
 
 (provide 'tildify)
-- 
1.9.0.279.gdc9e3eb




^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-03-02 21:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-02 21:55 [PATCH 0/5] Auto tildify improvements Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 1/5] tildify.el: Improve defcustom's types Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 2/5] tildify.el: Change XML hard space to numeric reference Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 3/5] tildify.el: Optimise environments regexes Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 4/5] tildify.el: Rewrite `tildify-region' and co., add foreach function Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 5/5] tildify.el: Add `auto-tildify' and `auto-tildify-mode' Michal Nazarewicz

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.