bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#18730: [PATCH 3/3] tildify.el: introduce a `tildify-foreach-region-f


From: Michal Nazarewicz
Subject: bug#18730: [PATCH 3/3] tildify.el: introduce a `tildify-foreach-region-function' variable
Date: Mon, 17 Nov 2014 16:41:10 +0100

The `tildify-foreach-region-function' variable is a function which
will allow major modes (or users) to create a more elaborate
environment filtering functions rather than just a simple set of
regular expressions.  Furthermore since major modes already know how
to parse contents of the buffer, the custom made function should be
much faster.

The variable is set to `tildify--deprecated-ignore-evironments'
function by default which interprets the now deprecated
`tildify-ignored-environments-alist' variable.  It is, however,
overwritten in tex-mode, sgml-mode, and nxml-mode.  The consequence of
that is user-costomisation of the deprecated variable for those modes
will no longer work, and users will have to change
`tildify-foreach-region-function' variable directly.

A new `tildify-foreach-ignore-environments' function makes it rather
easy to convert existing customisation of the alist variable into
customisation of the function variable.
---
 etc/NEWS                        |   7 +-
 lisp/nxml/nxml-mode.el          |   6 ++
 lisp/textmodes/sgml-mode.el     |  15 ++++
 lisp/textmodes/tex-mode.el      |  19 +++++
 lisp/textmodes/tildify.el       | 175 +++++++++++++++++++++++-----------------
 test/automated/tildify-tests.el |  61 +++++++++++++-
 6 files changed, 204 insertions(+), 79 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 955aa7f..3753d2c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -298,8 +298,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
 
diff --git a/lisp/nxml/nxml-mode.el b/lisp/nxml/nxml-mode.el
index 47f8066..b198f4a 100644
--- a/lisp/nxml/nxml-mode.el
+++ b/lisp/nxml/nxml-mode.el
@@ -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) " ")
                   " " " "))
+  ;; 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)
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el
index 9d1cb03..73b5292 100644
--- a/lisp/textmodes/sgml-mode.el
+++ b/lisp/textmodes/sgml-mode.el
@@ -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
diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el
index 0cfc0cf..1993ff1 100644
--- a/lisp/textmodes/tex-mode.el
+++ b/lisp/textmodes/tex-mode.el
@@ -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,
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el
index 6006968..d935ff7 100644
--- a/lisp/textmodes/tildify.el
+++ b/lisp/textmodes/tildify.el
@@ -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."
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el
index e532cf0..55d2d57 100644
--- a/test/automated/tildify-tests.el
+++ b/test/automated/tildify-tests.el
@@ -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)
-- 
2.1.0.rc2.206.gedb03e5






reply via email to

[Prev in Thread] Current Thread [Next in Thread]