]> code.delx.au - gnu-emacs/blobdiff - lisp/textmodes/tildify.el
* lisp/textmodes/tex-mode.el: Complete local envs. Use #'.
[gnu-emacs] / lisp / textmodes / tildify.el
index b11d7dae272b345e6c6408d4735490c1208587eb..598060e9ec817712aaa95f9dfb121f84159e1b31 100644 (file)
@@ -1,10 +1,10 @@
 ;;; tildify.el --- adding hard spaces into texts -*- lexical-binding: t -*-
 
-;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
+;; Copyright (C) 1997-2016 Free Software Foundation, Inc.
 
 ;; 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.
@@ -160,7 +160,7 @@ a simple pass through function could be used:
     (setq-local tildify-foreach-region-function
                 (lambda (cb beg end) (funcall cb beg end)))
 or better still:
-    (setq-local tildify-foreach-region-function 'funcall)
+    (setq-local tildify-foreach-region-function \\='funcall)
 See `tildify-foreach-ignore-environments' function for other ways to use the
 variable."
   :version "25.1"
@@ -282,7 +282,7 @@ corresponding text part and can be either:
 
 CALLBACK is a function accepting two arguments -- REG-BEG and REG-END -- that
 will be called for portions of the buffer outside of the environments defined by
-PAIRS regexes.
+PAIRS regexps.
 
 The function will return as soon as CALLBACK returns nil or point goes past END.
 CALLBACK may be called on portions of the buffer outside of [BEG END); in fact
@@ -291,8 +291,8 @@ BEG argument is ignored.
 This function is meant to be used to set `tildify-foreach-region-function'
 variable.  For example, for an XML file one might use:
   (setq-local tildify-foreach-region-function
-    (apply-partially 'tildify-foreach-ignore-environments
-                     '((\"<! *--\" . \"-- *>\") (\"<\" . \">\"))))"
+    (apply-partially \\='tildify-foreach-ignore-environments
+                     \\='((\"<! *--\" . \"-- *>\") (\"<\" . \">\"))))"
   (let ((beg-re (concat "\\(?:" (mapconcat 'car pairs "\\)\\|\\(?:") "\\)"))
         p end-re)
     (save-excursion
@@ -417,8 +417,13 @@ current `case-fold-search' setting."
   "A list of predicate functions for `tildify-space' function."
   :version "25.1"
   :group 'tildify
-  :type '(repeat 'function))
+  :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 ()
@@ -426,32 +431,44 @@ current `case-fold-search' setting."
 
 If
  * character before point is a space character,
- * character before that has “w” character syntax (i.e. it's a word
+ * character before that has \"w\" 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).
+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.
@@ -462,7 +479,7 @@ which is assumed to be a space character, should be replaced with a hard space."
 
 ;;;###autoload
 (define-minor-mode tildify-mode
-  "Adds electric behaviour to space character.
+  "Adds electric behavior to space character.
 
 When space is inserted into a buffer in a position where hard space is required
 instead (determined by `tildify-space-pattern' and `tildify-space-predicates'),
@@ -491,9 +508,4 @@ variable will be set to the representation."
 
 (provide 'tildify)
 
-
-;; Local variables:
-;; coding: utf-8
-;; End:
-
 ;;; tildify.el ends here