]> code.delx.au - gnu-emacs/commitdiff
tildify.el: introduce a `tildify-foreach-region-function' variable
authorMichal Nazarewicz <mina86@mina86.com>
Mon, 17 Nov 2014 13:41:37 +0000 (14:41 +0100)
committerMichal Nazarewicz <mina86@mina86.com>
Mon, 17 Nov 2014 23:49:11 +0000 (00:49 +0100)
* textmodes/tildify.el (tildify-foreach-region-function): New
variable specifying a function determining portions of buffer that
should be tildified.  It allows major modes to create a filtering
function more elaborate than a set of regular expressions.
Initialised to `tildify--deprecated-ignore-evironments' by default
to handle now deprecated `tildify-ignored-environments-alist'
variable.
(tildify--foreach-region): A new function that takes
`tildify-foreach-region-function' into account and calls callback
for regions of the buffer that should be tildified.
(tildify-foreach-ignore-environments): A new function which can be
partially applied and used as `tildify-foreach-region-function'.
(tildify-ignored-environments-alist, tildify--pick-alist-entry):
Mark as obsolete.
(tildify--find-env): Rename from `tildify-find-env' and mark as
obsolete.
(tildify--deprecated-ignore-evironments): New function,
immediately marked as obsolete, used to handle deprecated
`tildify-ignored-environments-alist'.

* textmodes/tex-mode.el (tex-common-initialization): Set
`tildify-foreach-region-function' variable in all variants of TeX
mode since `tildify-ignored-environments-alist' variable is now
empty by default.

* nxml/nxml-mode.el (nxml-mode): Ditto in `nxml-mode'.

* textmodes/sgml-mode.el (sgml-mode): Ditto in `sgml-mode'.

etc/NEWS
lisp/ChangeLog
lisp/nxml/nxml-mode.el
lisp/textmodes/sgml-mode.el
lisp/textmodes/tex-mode.el
lisp/textmodes/tildify.el
test/ChangeLog
test/automated/tildify-tests.el

index 61d1cd773b0bcb8818e2effe72cc71af57d7cf28..86e21c4b8fab4541f287c9c2c9d196d4e7e3b1cb 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -301,8 +301,11 @@ use PDF instead of DVI.
 By default, 32 spaces and four TABs are considered to be too much but
 `whitespace-big-indent-regexp' can be configured to change that.
 
-** tildify: `tildify-space-string' and `tildify-pattern' variables added making
-`tildify-string-alist' and `tildify-pattern-alist' obsolete.
+** tildify: `tildify-space-string', `tildify-pattern', and
+`tildify-foreach-region-function' variables added making
+`tildify-string-alist', `tildify-pattern-alist', and
+`tildify-ignored-environments-alist' variables (as well as a few
+helper functions) obsolete.
 
 ** Obsolete packages
 
index f5bd5273366e7dc44d451dbad42bac6a2fb331cf..0075c5a79cafa65f1c2c40c5fa1e31241b6457d6 100644 (file)
@@ -7,11 +7,29 @@
        `tildify-string-alist' and `tildify-pattern-alist' respectively
        that have been used so far.  They also works better with derived
        modes.
-       (tildify-pattern, tildify-string-alist): Mark as obsolete.
+       (tildify-foreach-region-function): New variable specifying
+       a function determining portions of buffer that should be
+       tildified.  It allows major modes to create a filtering function
+       more elaborate than a set of regular expressions.  Initialised to
+       `tildify--deprecated-ignore-evironments' by default to handle now
+       deprecated `tildify-ignored-environments-alist' variable.
+       (tildify--foreach-region): A new function that takes
+       `tildify-foreach-region-function' into account and calls callback
+       for regions of the buffer that should be tildified.
+       (tildify-foreach-ignore-environments): A new function which can be
+       partially applied and used as `tildify-foreach-region-function'.
+       (tildify-ignored-environments-alist, tildify-pattern)
+       (tildify-string-alist, tildify--pick-alist-entry): Mark as obsolete.
+       (tildify--find-env): Rename from `tildify-find-env' and mark as
+       obsolete.
+       (tildify--deprecated-ignore-evironments): New function,
+       immediately marked as obsolete, used to handle deprecated
+       `tildify-ignored-environments-alist'.
 
        * textmodes/tex-mode.el (tex-common-initialization): Set
-       `tildify-space-string' variable in all variants of TeX mode since
-       `tildify-string-alist' is now empty by default.
+       `tildify-space-string' and `tildify-foreach-region-function'
+       variables in all variants of TeX mode since `tildify-string-alist'
+       and `tildify-ignored-environments-alist' are now empty by default.
 
        * nxml/nxml-mode.el (nxml-mode): Ditto in `nxml-mode'.  If
        encoding supports it use no-break space instead of character
@@ -20,7 +38,7 @@
 
        * textmodes/sgml-mode.el (sgml-mode): ditto in `sgml-mode'.  If
        encoding does not support no-break space, use numeric reference;
-       this changes previous default which used named entity (?&nbsp;?)
+       this changes previous default which used named entity (“&nbsp;”)
        in HTML mode.
 
 2014-11-17  Ulf Jasper  <ulf.jasper@web.de>
index 47f806693df80b1e7ec2214405a1468dc4ad8082..b198f4acf4bd3fa2a3909e75e328c4ee31d4cb51 100644 (file)
@@ -450,6 +450,7 @@ reference.")
       (rng-validate-while-idle (current-buffer)))))
 
 (defvar tildify-space-string)
+(defvar tildify-foreach-region-function)
 
 ;;;###autoload
 (define-derived-mode nxml-mode text-mode "nXML"
@@ -515,6 +516,11 @@ Many aspects this mode can be customized using
                           (encode-coding-string " " buffer-file-coding-system)
                           buffer-file-coding-system) " ")
                   " " "&#160;"))
+  ;; FIXME: Use the fact that we're parsing the document already
+  ;; rather than using regex-based filtering.
+  (setq-local tildify-foreach-region-function
+              (apply-partially 'tildify-foreach-ignore-environments
+                               '(("<! *--" . "-- *>") ("<" . ">"))))
   (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded")))
   ;; We'll determine the fill prefix ourselves
   (make-local-variable 'adaptive-fill-mode)
index 9d1cb0373fa1e0838bc76c601e2156d647da2620..73b52926c5be5d7673297aa578ad0bc5300114bb 100644 (file)
@@ -457,6 +457,7 @@ This function is designed for use in `fill-nobreak-predicate'.
         (eq (char-before) ?<))))
 
 (defvar tildify-space-string)
+(defvar tildify-foreach-region-function)
 
 ;;;###autoload
 (define-derived-mode sgml-mode text-mode '(sgml-xml-mode "XML" "SGML")
@@ -486,6 +487,20 @@ Do \\[describe-key] on the following bindings to discover what they do.
                           (encode-coding-string " " buffer-file-coding-system)
                           buffer-file-coding-system) " ")
                   " " "&#160;"))
+  ;; FIXME: Use the fact that we're parsing the document already
+  ;; rather than using regex-based filtering.
+  (setq-local tildify-foreach-region-function
+              (apply-partially
+               'tildify-foreach-ignore-environments
+               `((,(eval-when-compile
+                     (concat
+                      "<\\("
+                      (regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var"
+                                    "PRE" "DFN" "CODE" "SAMP" "KBD" "VAR"))
+                      "\\)\\>[^>]*>"))
+                  . ("</" 1 ">"))
+                 ("<! *--" . "-- *>")
+                 ("<" . ">"))))
   ;;(make-local-variable 'facemenu-remove-face-function)
   ;; A start or end tag by itself on a line separates a paragraph.
   ;; This is desirable because SGML discards a newline that appears
index 0cfc0cfe3dc1831cb9cb375d7500b81da885ed54..1993ff104f4fde04c923315bf953807508574d9d 100644 (file)
@@ -1204,12 +1204,31 @@ Entering SliTeX mode runs the hook `text-mode-hook', then the hook
   (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}"))
 
 (defvar tildify-space-string)
+(defvar tildify-foreach-region-function)
 
 (defun tex-common-initialization ()
   ;; Regexp isearch should accept newline and formfeed as whitespace.
   (setq-local search-whitespace-regexp "[ \t\r\n\f]+")
   ;; Use tilde as hard-space character in tildify package.
   (setq-local tildify-space-string "~")
+  ;; FIXME: Use the fact that we're parsing the document already
+  ;; rather than using regex-based filtering.
+  (setq-local tildify-foreach-region-function
+              (apply-partially
+               'tildify-foreach-ignore-environments
+               `(("\\\\\\\\" . "") ; do not remove this
+                 (,(eval-when-compile
+                     (concat "\\\\begin{\\("
+                             (regexp-opt '("verbatim" "math" "displaymath"
+                                           "equation" "eqnarray" "eqnarray*"))
+                             "\\)}"))
+                  . ("\\\\end{" 1 "}"))
+                 ("\\\\verb\\*?\\(.\\)" . (1))
+                 ("\\$\\$?" . (0))
+                 ("\\\\(" . "\\\\)")
+                 ("\\\\[[]" . "\\\\[]]")
+                 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
+                 ("%" . "$"))))
   ;; A line containing just $$ is treated as a paragraph separator.
   (setq-local paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
   ;; A line starting with $$ starts a paragraph,
index 600696836c43900e77b53f6e7fb8ea09b40257cf..d935ff7286e32d195f97c72a85bea7bf07e95c90 100644 (file)
@@ -1,10 +1,10 @@
-;;; tildify.el --- adding hard spaces into texts
+;;; tildify.el --- adding hard spaces into texts -*- lexical-binding: t -*-
 
 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
 
 ;; Author:     Milan Zamazal <pdm@zamazal.org>
 ;;             Michal Nazarewicz <mina86@mina86.com>
-;; Version:    4.5.6
+;; Version:    4.5.7
 ;; Keywords:   text, TeX, SGML, wp
 
 ;; This file is part of GNU Emacs.
@@ -143,36 +143,31 @@ mode, the item for the mode SYMBOL is looked up in the alist instead."
 (make-obsolete-variable 'tildify-string-alist
                         'tildify-space-string "25.1")
 
-(defcustom tildify-ignored-environments-alist
-  `((latex-mode
-     ("\\\\\\\\" . "")         ; do not remove this
-     (,(eval-when-compile (concat
-                           "\\\\begin{\\("
-                           (regexp-opt '("verbatim" "math" "displaymath"
-                                         "equation" "eqnarray" "eqnarray*"))
-                           "\\)}"))
-      . ("\\\\end{" 1 "}"))
-     ("\\\\verb\\*?\\(.\\)" . (1))
-     ("\\$\\$?" . (0))
-     ("\\\\(" . "\\\\)")
-     ("\\\\[[]" . "\\\\[]]")
-     ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
-     ("%" . "$"))
-    (plain-tex-mode . latex-mode)
-    (html-mode
-     (,(eval-when-compile (concat
-                           "<\\("
-                           (regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var"
-                                         "PRE" "DFN" "CODE" "SAMP" "KBD" "VAR"))
-                           "\\)\\>[^>]*>"))
-      . ("</" 1 ">"))
-     ("<! *--" . "-- *>")
-     ("<" . ">"))
-    (sgml-mode . html-mode)
-    (xml-mode
-     ("<! *--" . "-- *>")
-     ("<" . ">"))
-    (nxml-mode . xml-mode))
+(defcustom tildify-foreach-region-function
+  'tildify--deprecated-ignore-evironments
+  "A function calling a callback on portions of the buffer to tildify.
+
+The function is called from `tildify-buffer' function with three arguments: FUNC
+BEG END.  FUNC is a callback accepting two arguments -- REG-BEG REG-END --
+specifying a portion of buffer to operate on.
+
+The BEG and END arguments may be used to limit portion of the buffer being
+scanned, but the `tildify-foreach-region-function' is not required to make use
+of them.  IT must, however, terminate as soon as FUNC returns nil.
+
+For example, if `tildify-buffer' function should operate on the whole buffer,
+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)
+See `tildify-foreach-ignore-environments' function for other ways to use the
+variable."
+  :version "25.1"
+  :group 'tildify
+  :type 'function)
+
+(defcustom tildify-ignored-environments-alist ()
   "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
@@ -186,13 +181,8 @@ MAJOR-MODE defines major mode, for which the item applies.  It can be either:
 - t for default item, this applies to all major modes not defined in another
   alist item
 
-BEG-REGEX is a regexp matching beginning of a text part to be skipped.
-END-REGEX defines end of the corresponding text part and can be either:
-- a regexp matching the end of the skipped text part
-- a list of regexps and numbers, which will compose the ending regexp by
-  concatenating themselves, while replacing the numbers with corresponding
-  subexpressions of BEG-REGEX (this is used to solve cases like
-  \\\\verb<character> in TeX)."
+See `tildify-foreach-ignore-environments' function for description of BEG-REGEX
+and END-REGEX."
   :group 'tildify
   :type '(repeat
           (cons :tag "Entry for major mode"
@@ -210,6 +200,8 @@ END-REGEX defines end of the corresponding text part and can be either:
                                       (choice (regexp  :tag "Regexp")
                                               (integer :tag "Group "))))))
                  (symbol :tag "Like other")))))
+(make-obsolete-variable 'tildify-ignored-environments-alist
+                        'tildify-foreach-region-function "25.1")
 
 
 ;;; *** Interactive functions ***
@@ -225,14 +217,15 @@ If DONT-ASK is set, or called interactively with prefix argument, user
 won't be prompted for confirmation of each substitution."
   (interactive "*rP")
   (let (case-fold-search (count 0) (ask (not dont-ask)))
-    (tildify-foreach-region-outside-env beg end
+    (tildify--foreach-region
       (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))))
+            t)))
+      beg end)
     (message "%d spaces replaced." count)))
 
 ;;;###autoload
@@ -258,40 +251,76 @@ won't be prompted for confirmation of each substitution."
             (symbolp alist))
        (tildify--pick-alist-entry mode-alist alist)
       alist)))
+(make-obsolete 'tildify--pick-alist-entry
+               "it should not be used in new code." "25.1")
+
+(defun tildify--deprecated-ignore-evironments (callback beg end)
+  "Call CALLBACK on regions between BEG and END.
 
-(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))
+Call CALLBACK on each region outside of environment to ignore.  Stop scanning
+the region as soon as CALLBACK returns nil.  Environments to ignore are
+defined by deprecated `tildify-ignored-environments-alist'.   CALLBACK may be
+called on portions of the buffer outside of [BEG END)."
   (let ((pairs (tildify--pick-alist-entry tildify-ignored-environments-alist)))
-    (if (not pairs)
-        (funcall callback beg end)
-      (let ((func (lambda (b e)
-                    (let ((b (max b beg)) (e (min e end)))
-                    (if (< b e) (funcall callback b e) t))))
-            (beg-re (concat "\\(?:"
-                            (mapconcat 'car pairs "\\)\\|\\(?:")
-                            "\\)"))
-            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))
-                            (re-search-forward end-re nil t)))))))))))
-
-(defun tildify-find-env (regexp pairs)
+    (if pairs
+        (tildify-foreach-ignore-environments pairs callback beg end)
+      (funcall callback beg end))))
+(make-obsolete 'tildify--deprecated-ignore-evironments
+               "it should not be used in new code." "25.1")
+
+(defun tildify-foreach-ignore-environments (pairs callback _beg end)
+  "Outside of environments defined by PAIRS call CALLBACK.
+
+PAIRS is a list of (BEG-REGEX . END-REGEX) cons.  BEG-REGEX is a regexp matching
+beginning of a text part to be skipped.  END-REGEX defines end of the
+corresponding text part and can be either:
+- a regexp matching the end of the skipped text part
+- a list of regexps and numbers, which will compose the ending regexp by
+  concatenating themselves, while replacing the numbers with corresponding
+  subexpressions of BEG-REGEX (this is used to solve cases like
+  \\\\verb<character> in TeX).
+
+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.
+
+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
+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
+                     '((\"<! *--\" . \"-- *>\") (\"<\" . \">\"))))"
+  (let ((beg-re (concat "\\(?:" (mapconcat 'car pairs "\\)\\|\\(?:") "\\)"))
+        p end-re)
+    (save-excursion
+      (save-restriction
+        (widen)
+        (goto-char (point-min))
+        (while (and (< (setq p (point)) end)
+                    (if (setq end-re (tildify--find-env beg-re pairs))
+                        (and (funcall callback p (match-beginning 0))
+                             (< (point) end)
+                             (re-search-forward end-re nil t))
+                      (funcall callback p end)
+                      nil)))))))
+
+(defun tildify--foreach-region (callback beg end)
+  "Call CALLBACK on portions of the buffer between BEG and END.
+
+Which portions to call CALLBACK on is determined by
+`tildify-foreach-region-function' variable.  This function merely makes sure
+CALLBACK is not called with portions of the buffer outside of [BEG END)."
+  (let ((func (lambda (reg-beg reg-end)
+                (setq reg-beg (max reg-beg beg) reg-end (min reg-end end))
+                (and (or (>= reg-beg reg-end)
+                         (funcall callback reg-beg reg-end))
+                     (< reg-end end)))))
+    (funcall tildify-foreach-region-function func beg end)))
+
+(defun tildify--find-env (regexp pairs)
   "Find environment using REGEXP.
 Return regexp for the end of the environment found in PAIRS or nil if
 no environment was found."
index 1c739d456acf0d00a5fc3d695174b97a6c980119..475b6a3f4738a5f8c31fd06ddcb78c4d69badc49 100644 (file)
@@ -2,6 +2,14 @@
 
        * automated/tildify-tests.el (tildify-test-html, tildify-test-xml):
        HTML and XML now use no-break space as hard space.  Update tests.
+       (tildify-test-foreach-ignore-environments)
+       (tildify-test-foreach-ignore-environments-early-return,
+       (tildify-test-foreach-region)
+       (tildify-test-foreach-region-early-return)
+       (tildify-test-foreach-region-limit-region): New tests of
+       `tildify-foreach-ignore-environments' and
+       `tildify--foreach-region' functions.
+       (with-test-foreach): New helper macro for the above tests.
 
 2014-11-17  Glenn Morris  <rgm@gnu.org>
 
index e532cf00686c4a05384e698050492ebe22ebdbac..55d2d576721a3a47cfe71aa3cd3a67954926e643 100644 (file)
@@ -1,4 +1,4 @@
-;;; tildify-test.el --- ERT tests for tildify.el
+;;; tildify-test.el --- ERT tests for tildify.el -*- lexical-binding: t -*-
 
 ;; Copyright (C) 2014 Free Software Foundation, Inc.
 
@@ -117,8 +117,8 @@ latter is missing, SENTENCE will be used in all placeholder positions."
     (insert "foo whatever end-foo")
     (goto-char (point-min))
     (should (string-equal "end-foo"
-                          (tildify-find-env "foo\\|bar"
-                                            '(("foo\\|bar" . ("end-" 0))))))))
+                          (tildify--find-env "foo\\|bar"
+                                             '(("foo\\|bar" . ("end-" 0))))))))
 
 
 (ert-deftest tildify-test-find-env-group-index-bug ()
@@ -129,7 +129,60 @@ latter is missing, SENTENCE will be used in all placeholder positions."
           (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)"))
       (insert "open-foo whatever close-foo")
       (goto-char (point-min))
-      (should (string-equal "close-foo" (tildify-find-env beg-re pairs))))))
+      (should (string-equal "close-foo" (tildify--find-env beg-re pairs))))))
+
+
+(defmacro with-test-foreach (expected &rest body)
+  "Helper macro for testing foreach functions.
+BODY has access to pairs variable and called lambda."
+  (declare (indent 1))
+  (let ((got (make-symbol "got")))
+    `(with-temp-buffer
+       (insert "1 /- 2 -/ 3 V~ 4 ~ 5 /- 6 -/ 7")
+       (let* ((pairs '(("/-" . "-/") ("V\\(.\\)" . (1))))
+              (,got "")
+              (called (lambda (s e)
+                        (setq ,got (concat ,got (buffer-substring s e))))))
+         (setq-local tildify-foreach-region-function
+                     (apply-partially 'tildify-foreach-ignore-environments
+                                      pairs))
+         ,@body
+         (should (string-equal ,expected ,got))))))
+
+(ert-deftest tildify-test-foreach-ignore-environments ()
+    "Basic test of `tildify-foreach-ignore-environments'"
+  (with-test-foreach "1  3  5  7"
+    (tildify-foreach-ignore-environments pairs called (point-min) (point-max))))
+
+
+(ert-deftest tildify-test-foreach-ignore-environments-early-return ()
+    "Test whether `tildify-foreach-ignore-environments' returns early
+The function must terminate as soon as callback returns nil."
+  (with-test-foreach "1 "
+    (tildify-foreach-ignore-environments
+     pairs (lambda (start end) (funcall called start end) nil)
+     (point-min) (point-max))))
+
+(ert-deftest tildify-test-foreach-region ()
+    "Basic test of `tildify--foreach-region'"
+  (with-test-foreach "1  3  5  7"
+    (tildify--foreach-region called (point-min) (point-max))))
+
+(ert-deftest tildify-test-foreach-region-early-return ()
+    "Test whether `tildify--foreach-ignore' returns early
+The function must terminate as soon as callback returns nil."
+  (with-test-foreach "1 "
+    (tildify--foreach-region (lambda (start end) (funcall called start end) nil)
+      (point-min) (point-max))))
+
+(ert-deftest tildify-test-foreach-region-limit-region ()
+    "Test whether `tildify--foreach-ignore' limits callback to given region"
+  (with-test-foreach "3 "
+    (tildify--foreach-region called
+      (+ (point-min) 10) (+ (point-min) 16))) ; start at "3" end past "4"
+  (with-test-foreach "3  5"
+    (tildify--foreach-region called
+      (+ (point-min) 10) (+ (point-min) 20)))) ; start at "3" end past "5"
 
 
 (provide 'tildify-tests)