diff options
| author | Michal Nazarewicz | 2014-11-17 14:41:37 +0100 |
|---|---|---|
| committer | Michal Nazarewicz | 2014-11-18 00:49:11 +0100 |
| commit | 1901029f6b6aadd210001b2154282bec0b9d3b07 (patch) | |
| tree | 90132bfad2806843c57eb276590d3403ef85e6f5 | |
| parent | d5ec102b7aa0aa07bdd44ecd0b471275511fed7e (diff) | |
| download | emacs-1901029f6b6aadd210001b2154282bec0b9d3b07.tar.gz emacs-1901029f6b6aadd210001b2154282bec0b9d3b07.zip | |
tildify.el: introduce a `tildify-foreach-region-function' variable
* 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'.
| -rw-r--r-- | etc/NEWS | 7 | ||||
| -rw-r--r-- | lisp/ChangeLog | 26 | ||||
| -rw-r--r-- | lisp/nxml/nxml-mode.el | 6 | ||||
| -rw-r--r-- | lisp/textmodes/sgml-mode.el | 15 | ||||
| -rw-r--r-- | lisp/textmodes/tex-mode.el | 19 | ||||
| -rw-r--r-- | lisp/textmodes/tildify.el | 175 | ||||
| -rw-r--r-- | test/ChangeLog | 8 | ||||
| -rw-r--r-- | test/automated/tildify-tests.el | 61 |
8 files changed, 234 insertions, 83 deletions
| @@ -301,8 +301,11 @@ use PDF instead of DVI. | |||
| 301 | By default, 32 spaces and four TABs are considered to be too much but | 301 | By default, 32 spaces and four TABs are considered to be too much but |
| 302 | `whitespace-big-indent-regexp' can be configured to change that. | 302 | `whitespace-big-indent-regexp' can be configured to change that. |
| 303 | 303 | ||
| 304 | ** tildify: `tildify-space-string' and `tildify-pattern' variables added making | 304 | ** tildify: `tildify-space-string', `tildify-pattern', and |
| 305 | `tildify-string-alist' and `tildify-pattern-alist' obsolete. | 305 | `tildify-foreach-region-function' variables added making |
| 306 | `tildify-string-alist', `tildify-pattern-alist', and | ||
| 307 | `tildify-ignored-environments-alist' variables (as well as a few | ||
| 308 | helper functions) obsolete. | ||
| 306 | 309 | ||
| 307 | ** Obsolete packages | 310 | ** Obsolete packages |
| 308 | 311 | ||
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index f5bd5273366..0075c5a79ca 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -7,11 +7,29 @@ | |||
| 7 | `tildify-string-alist' and `tildify-pattern-alist' respectively | 7 | `tildify-string-alist' and `tildify-pattern-alist' respectively |
| 8 | that have been used so far. They also works better with derived | 8 | that have been used so far. They also works better with derived |
| 9 | modes. | 9 | modes. |
| 10 | (tildify-pattern, tildify-string-alist): Mark as obsolete. | 10 | (tildify-foreach-region-function): New variable specifying |
| 11 | a function determining portions of buffer that should be | ||
| 12 | tildified. It allows major modes to create a filtering function | ||
| 13 | more elaborate than a set of regular expressions. Initialised to | ||
| 14 | `tildify--deprecated-ignore-evironments' by default to handle now | ||
| 15 | deprecated `tildify-ignored-environments-alist' variable. | ||
| 16 | (tildify--foreach-region): A new function that takes | ||
| 17 | `tildify-foreach-region-function' into account and calls callback | ||
| 18 | for regions of the buffer that should be tildified. | ||
| 19 | (tildify-foreach-ignore-environments): A new function which can be | ||
| 20 | partially applied and used as `tildify-foreach-region-function'. | ||
| 21 | (tildify-ignored-environments-alist, tildify-pattern) | ||
| 22 | (tildify-string-alist, tildify--pick-alist-entry): Mark as obsolete. | ||
| 23 | (tildify--find-env): Rename from `tildify-find-env' and mark as | ||
| 24 | obsolete. | ||
| 25 | (tildify--deprecated-ignore-evironments): New function, | ||
| 26 | immediately marked as obsolete, used to handle deprecated | ||
| 27 | `tildify-ignored-environments-alist'. | ||
| 11 | 28 | ||
| 12 | * textmodes/tex-mode.el (tex-common-initialization): Set | 29 | * textmodes/tex-mode.el (tex-common-initialization): Set |
| 13 | `tildify-space-string' variable in all variants of TeX mode since | 30 | `tildify-space-string' and `tildify-foreach-region-function' |
| 14 | `tildify-string-alist' is now empty by default. | 31 | variables in all variants of TeX mode since `tildify-string-alist' |
| 32 | and `tildify-ignored-environments-alist' are now empty by default. | ||
| 15 | 33 | ||
| 16 | * nxml/nxml-mode.el (nxml-mode): Ditto in `nxml-mode'. If | 34 | * nxml/nxml-mode.el (nxml-mode): Ditto in `nxml-mode'. If |
| 17 | encoding supports it use no-break space instead of character | 35 | encoding supports it use no-break space instead of character |
| @@ -20,7 +38,7 @@ | |||
| 20 | 38 | ||
| 21 | * textmodes/sgml-mode.el (sgml-mode): ditto in `sgml-mode'. If | 39 | * textmodes/sgml-mode.el (sgml-mode): ditto in `sgml-mode'. If |
| 22 | encoding does not support no-break space, use numeric reference; | 40 | encoding does not support no-break space, use numeric reference; |
| 23 | this changes previous default which used named entity (? ?) | 41 | this changes previous default which used named entity (“ ”) |
| 24 | in HTML mode. | 42 | in HTML mode. |
| 25 | 43 | ||
| 26 | 2014-11-17 Ulf Jasper <ulf.jasper@web.de> | 44 | 2014-11-17 Ulf Jasper <ulf.jasper@web.de> |
diff --git a/lisp/nxml/nxml-mode.el b/lisp/nxml/nxml-mode.el index 47f806693df..b198f4acf4b 100644 --- a/lisp/nxml/nxml-mode.el +++ b/lisp/nxml/nxml-mode.el | |||
| @@ -450,6 +450,7 @@ reference.") | |||
| 450 | (rng-validate-while-idle (current-buffer))))) | 450 | (rng-validate-while-idle (current-buffer))))) |
| 451 | 451 | ||
| 452 | (defvar tildify-space-string) | 452 | (defvar tildify-space-string) |
| 453 | (defvar tildify-foreach-region-function) | ||
| 453 | 454 | ||
| 454 | ;;;###autoload | 455 | ;;;###autoload |
| 455 | (define-derived-mode nxml-mode text-mode "nXML" | 456 | (define-derived-mode nxml-mode text-mode "nXML" |
| @@ -515,6 +516,11 @@ Many aspects this mode can be customized using | |||
| 515 | (encode-coding-string " " buffer-file-coding-system) | 516 | (encode-coding-string " " buffer-file-coding-system) |
| 516 | buffer-file-coding-system) " ") | 517 | buffer-file-coding-system) " ") |
| 517 | " " " ")) | 518 | " " " ")) |
| 519 | ;; FIXME: Use the fact that we're parsing the document already | ||
| 520 | ;; rather than using regex-based filtering. | ||
| 521 | (setq-local tildify-foreach-region-function | ||
| 522 | (apply-partially 'tildify-foreach-ignore-environments | ||
| 523 | '(("<! *--" . "-- *>") ("<" . ">")))) | ||
| 518 | (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded"))) | 524 | (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded"))) |
| 519 | ;; We'll determine the fill prefix ourselves | 525 | ;; We'll determine the fill prefix ourselves |
| 520 | (make-local-variable 'adaptive-fill-mode) | 526 | (make-local-variable 'adaptive-fill-mode) |
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el index 9d1cb0373fa..73b52926c5b 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'. | |||
| 457 | (eq (char-before) ?<)))) | 457 | (eq (char-before) ?<)))) |
| 458 | 458 | ||
| 459 | (defvar tildify-space-string) | 459 | (defvar tildify-space-string) |
| 460 | (defvar tildify-foreach-region-function) | ||
| 460 | 461 | ||
| 461 | ;;;###autoload | 462 | ;;;###autoload |
| 462 | (define-derived-mode sgml-mode text-mode '(sgml-xml-mode "XML" "SGML") | 463 | (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. | |||
| 486 | (encode-coding-string " " buffer-file-coding-system) | 487 | (encode-coding-string " " buffer-file-coding-system) |
| 487 | buffer-file-coding-system) " ") | 488 | buffer-file-coding-system) " ") |
| 488 | " " " ")) | 489 | " " " ")) |
| 490 | ;; FIXME: Use the fact that we're parsing the document already | ||
| 491 | ;; rather than using regex-based filtering. | ||
| 492 | (setq-local tildify-foreach-region-function | ||
| 493 | (apply-partially | ||
| 494 | 'tildify-foreach-ignore-environments | ||
| 495 | `((,(eval-when-compile | ||
| 496 | (concat | ||
| 497 | "<\\(" | ||
| 498 | (regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var" | ||
| 499 | "PRE" "DFN" "CODE" "SAMP" "KBD" "VAR")) | ||
| 500 | "\\)\\>[^>]*>")) | ||
| 501 | . ("</" 1 ">")) | ||
| 502 | ("<! *--" . "-- *>") | ||
| 503 | ("<" . ">")))) | ||
| 489 | ;;(make-local-variable 'facemenu-remove-face-function) | 504 | ;;(make-local-variable 'facemenu-remove-face-function) |
| 490 | ;; A start or end tag by itself on a line separates a paragraph. | 505 | ;; A start or end tag by itself on a line separates a paragraph. |
| 491 | ;; This is desirable because SGML discards a newline that appears | 506 | ;; 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 0cfc0cfe3dc..1993ff104f4 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 | |||
| 1204 | (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}")) | 1204 | (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}")) |
| 1205 | 1205 | ||
| 1206 | (defvar tildify-space-string) | 1206 | (defvar tildify-space-string) |
| 1207 | (defvar tildify-foreach-region-function) | ||
| 1207 | 1208 | ||
| 1208 | (defun tex-common-initialization () | 1209 | (defun tex-common-initialization () |
| 1209 | ;; Regexp isearch should accept newline and formfeed as whitespace. | 1210 | ;; Regexp isearch should accept newline and formfeed as whitespace. |
| 1210 | (setq-local search-whitespace-regexp "[ \t\r\n\f]+") | 1211 | (setq-local search-whitespace-regexp "[ \t\r\n\f]+") |
| 1211 | ;; Use tilde as hard-space character in tildify package. | 1212 | ;; Use tilde as hard-space character in tildify package. |
| 1212 | (setq-local tildify-space-string "~") | 1213 | (setq-local tildify-space-string "~") |
| 1214 | ;; FIXME: Use the fact that we're parsing the document already | ||
| 1215 | ;; rather than using regex-based filtering. | ||
| 1216 | (setq-local tildify-foreach-region-function | ||
| 1217 | (apply-partially | ||
| 1218 | 'tildify-foreach-ignore-environments | ||
| 1219 | `(("\\\\\\\\" . "") ; do not remove this | ||
| 1220 | (,(eval-when-compile | ||
| 1221 | (concat "\\\\begin{\\(" | ||
| 1222 | (regexp-opt '("verbatim" "math" "displaymath" | ||
| 1223 | "equation" "eqnarray" "eqnarray*")) | ||
| 1224 | "\\)}")) | ||
| 1225 | . ("\\\\end{" 1 "}")) | ||
| 1226 | ("\\\\verb\\*?\\(.\\)" . (1)) | ||
| 1227 | ("\\$\\$?" . (0)) | ||
| 1228 | ("\\\\(" . "\\\\)") | ||
| 1229 | ("\\\\[[]" . "\\\\[]]") | ||
| 1230 | ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "") | ||
| 1231 | ("%" . "$")))) | ||
| 1213 | ;; A line containing just $$ is treated as a paragraph separator. | 1232 | ;; A line containing just $$ is treated as a paragraph separator. |
| 1214 | (setq-local paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$") | 1233 | (setq-local paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$") |
| 1215 | ;; A line starting with $$ starts a paragraph, | 1234 | ;; A line starting with $$ starts a paragraph, |
diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el index 600696836c4..d935ff7286e 100644 --- a/lisp/textmodes/tildify.el +++ b/lisp/textmodes/tildify.el | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | ;;; tildify.el --- adding hard spaces into texts | 1 | ;;; tildify.el --- adding hard spaces into texts -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1997-2014 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 1997-2014 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; Author: Milan Zamazal <pdm@zamazal.org> | 5 | ;; Author: Milan Zamazal <pdm@zamazal.org> |
| 6 | ;; Michal Nazarewicz <mina86@mina86.com> | 6 | ;; Michal Nazarewicz <mina86@mina86.com> |
| 7 | ;; Version: 4.5.6 | 7 | ;; Version: 4.5.7 |
| 8 | ;; Keywords: text, TeX, SGML, wp | 8 | ;; Keywords: text, TeX, SGML, wp |
| 9 | 9 | ||
| 10 | ;; This file is part of GNU Emacs. | 10 | ;; 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." | |||
| 143 | (make-obsolete-variable 'tildify-string-alist | 143 | (make-obsolete-variable 'tildify-string-alist |
| 144 | 'tildify-space-string "25.1") | 144 | 'tildify-space-string "25.1") |
| 145 | 145 | ||
| 146 | (defcustom tildify-ignored-environments-alist | 146 | (defcustom tildify-foreach-region-function |
| 147 | `((latex-mode | 147 | 'tildify--deprecated-ignore-evironments |
| 148 | ("\\\\\\\\" . "") ; do not remove this | 148 | "A function calling a callback on portions of the buffer to tildify. |
| 149 | (,(eval-when-compile (concat | 149 | |
| 150 | "\\\\begin{\\(" | 150 | The function is called from `tildify-buffer' function with three arguments: FUNC |
| 151 | (regexp-opt '("verbatim" "math" "displaymath" | 151 | BEG END. FUNC is a callback accepting two arguments -- REG-BEG REG-END -- |
| 152 | "equation" "eqnarray" "eqnarray*")) | 152 | specifying a portion of buffer to operate on. |
| 153 | "\\)}")) | 153 | |
| 154 | . ("\\\\end{" 1 "}")) | 154 | The BEG and END arguments may be used to limit portion of the buffer being |
| 155 | ("\\\\verb\\*?\\(.\\)" . (1)) | 155 | scanned, but the `tildify-foreach-region-function' is not required to make use |
| 156 | ("\\$\\$?" . (0)) | 156 | of them. IT must, however, terminate as soon as FUNC returns nil. |
| 157 | ("\\\\(" . "\\\\)") | 157 | |
| 158 | ("\\\\[[]" . "\\\\[]]") | 158 | For example, if `tildify-buffer' function should operate on the whole buffer, |
| 159 | ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "") | 159 | a simple pass through function could be used: |
| 160 | ("%" . "$")) | 160 | (setq-local tildify-foreach-region-function |
| 161 | (plain-tex-mode . latex-mode) | 161 | (lambda (cb beg end) (funcall cb beg end))) |
| 162 | (html-mode | 162 | or better still: |
| 163 | (,(eval-when-compile (concat | 163 | (setq-local tildify-foreach-region-function 'funcall) |
| 164 | "<\\(" | 164 | See `tildify-foreach-ignore-environments' function for other ways to use the |
| 165 | (regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var" | 165 | variable." |
| 166 | "PRE" "DFN" "CODE" "SAMP" "KBD" "VAR")) | 166 | :version "25.1" |
| 167 | "\\)\\>[^>]*>")) | 167 | :group 'tildify |
| 168 | . ("</" 1 ">")) | 168 | :type 'function) |
| 169 | ("<! *--" . "-- *>") | 169 | |
| 170 | ("<" . ">")) | 170 | (defcustom tildify-ignored-environments-alist () |
| 171 | (sgml-mode . html-mode) | ||
| 172 | (xml-mode | ||
| 173 | ("<! *--" . "-- *>") | ||
| 174 | ("<" . ">")) | ||
| 175 | (nxml-mode . xml-mode)) | ||
| 176 | "Alist specifying ignored structured text environments. | 171 | "Alist specifying ignored structured text environments. |
| 177 | Parts of text defined in this alist are skipped without performing hard space | 172 | Parts of text defined in this alist are skipped without performing hard space |
| 178 | insertion on them. These setting allow skipping text parts like verbatim or | 173 | 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: | |||
| 186 | - t for default item, this applies to all major modes not defined in another | 181 | - t for default item, this applies to all major modes not defined in another |
| 187 | alist item | 182 | alist item |
| 188 | 183 | ||
| 189 | BEG-REGEX is a regexp matching beginning of a text part to be skipped. | 184 | See `tildify-foreach-ignore-environments' function for description of BEG-REGEX |
| 190 | END-REGEX defines end of the corresponding text part and can be either: | 185 | and END-REGEX." |
| 191 | - a regexp matching the end of the skipped text part | ||
| 192 | - a list of regexps and numbers, which will compose the ending regexp by | ||
| 193 | concatenating themselves, while replacing the numbers with corresponding | ||
| 194 | subexpressions of BEG-REGEX (this is used to solve cases like | ||
| 195 | \\\\verb<character> in TeX)." | ||
| 196 | :group 'tildify | 186 | :group 'tildify |
| 197 | :type '(repeat | 187 | :type '(repeat |
| 198 | (cons :tag "Entry for major mode" | 188 | (cons :tag "Entry for major mode" |
| @@ -210,6 +200,8 @@ END-REGEX defines end of the corresponding text part and can be either: | |||
| 210 | (choice (regexp :tag "Regexp") | 200 | (choice (regexp :tag "Regexp") |
| 211 | (integer :tag "Group ")))))) | 201 | (integer :tag "Group ")))))) |
| 212 | (symbol :tag "Like other"))))) | 202 | (symbol :tag "Like other"))))) |
| 203 | (make-obsolete-variable 'tildify-ignored-environments-alist | ||
| 204 | 'tildify-foreach-region-function "25.1") | ||
| 213 | 205 | ||
| 214 | 206 | ||
| 215 | ;;; *** Interactive functions *** | 207 | ;;; *** Interactive functions *** |
| @@ -225,14 +217,15 @@ If DONT-ASK is set, or called interactively with prefix argument, user | |||
| 225 | won't be prompted for confirmation of each substitution." | 217 | won't be prompted for confirmation of each substitution." |
| 226 | (interactive "*rP") | 218 | (interactive "*rP") |
| 227 | (let (case-fold-search (count 0) (ask (not dont-ask))) | 219 | (let (case-fold-search (count 0) (ask (not dont-ask))) |
| 228 | (tildify-foreach-region-outside-env beg end | 220 | (tildify--foreach-region |
| 229 | (lambda (beg end) | 221 | (lambda (beg end) |
| 230 | (let ((aux (tildify-tildify beg end ask))) | 222 | (let ((aux (tildify-tildify beg end ask))) |
| 231 | (setq count (+ count (car aux))) | 223 | (setq count (+ count (car aux))) |
| 232 | (if (not (eq (cdr aux) 'force)) | 224 | (if (not (eq (cdr aux) 'force)) |
| 233 | (cdr aux) | 225 | (cdr aux) |
| 234 | (setq ask nil) | 226 | (setq ask nil) |
| 235 | t)))) | 227 | t))) |
| 228 | beg end) | ||
| 236 | (message "%d spaces replaced." count))) | 229 | (message "%d spaces replaced." count))) |
| 237 | 230 | ||
| 238 | ;;;###autoload | 231 | ;;;###autoload |
| @@ -258,40 +251,76 @@ won't be prompted for confirmation of each substitution." | |||
| 258 | (symbolp alist)) | 251 | (symbolp alist)) |
| 259 | (tildify--pick-alist-entry mode-alist alist) | 252 | (tildify--pick-alist-entry mode-alist alist) |
| 260 | alist))) | 253 | alist))) |
| 254 | (make-obsolete 'tildify--pick-alist-entry | ||
| 255 | "it should not be used in new code." "25.1") | ||
| 256 | |||
| 257 | (defun tildify--deprecated-ignore-evironments (callback beg end) | ||
| 258 | "Call CALLBACK on regions between BEG and END. | ||
| 261 | 259 | ||
| 262 | (defun tildify-foreach-region-outside-env (beg end callback) | 260 | Call CALLBACK on each region outside of environment to ignore. Stop scanning |
| 263 | "Scan region from BEG to END calling CALLBACK on portions out of environments. | 261 | the region as soon as CALLBACK returns nil. Environments to ignore are |
| 264 | Call CALLBACK on each region outside of environment to ignore. | 262 | defined by deprecated `tildify-ignored-environments-alist'. CALLBACK may be |
| 265 | CALLBACK will only be called for regions which have intersection | 263 | called on portions of the buffer outside of [BEG END)." |
| 266 | with [BEG END]. It must be a function that takes two point | ||
| 267 | arguments specifying the region to operate on. Stop scanning the | ||
| 268 | region as soon as CALLBACK returns nil. Environments to ignore | ||
| 269 | are determined from `tildify-ignored-environments-alist'." | ||
| 270 | (declare (indent 2)) | ||
| 271 | (let ((pairs (tildify--pick-alist-entry tildify-ignored-environments-alist))) | 264 | (let ((pairs (tildify--pick-alist-entry tildify-ignored-environments-alist))) |
| 272 | (if (not pairs) | 265 | (if pairs |
| 273 | (funcall callback beg end) | 266 | (tildify-foreach-ignore-environments pairs callback beg end) |
| 274 | (let ((func (lambda (b e) | 267 | (funcall callback beg end)))) |
| 275 | (let ((b (max b beg)) (e (min e end))) | 268 | (make-obsolete 'tildify--deprecated-ignore-evironments |
| 276 | (if (< b e) (funcall callback b e) t)))) | 269 | "it should not be used in new code." "25.1") |
| 277 | (beg-re (concat "\\(?:" | 270 | |
| 278 | (mapconcat 'car pairs "\\)\\|\\(?:") | 271 | (defun tildify-foreach-ignore-environments (pairs callback _beg end) |
| 279 | "\\)")) | 272 | "Outside of environments defined by PAIRS call CALLBACK. |
| 280 | p end-re) | 273 | |
| 281 | (save-excursion | 274 | PAIRS is a list of (BEG-REGEX . END-REGEX) cons. BEG-REGEX is a regexp matching |
| 282 | (save-restriction | 275 | beginning of a text part to be skipped. END-REGEX defines end of the |
| 283 | (widen) | 276 | corresponding text part and can be either: |
| 284 | (goto-char (point-min)) | 277 | - a regexp matching the end of the skipped text part |
| 285 | (while (and (< (setq p (point)) end) | 278 | - a list of regexps and numbers, which will compose the ending regexp by |
| 286 | (if (not (setq end-re | 279 | concatenating themselves, while replacing the numbers with corresponding |
| 287 | (tildify-find-env beg-re pairs))) | 280 | subexpressions of BEG-REGEX (this is used to solve cases like |
| 288 | (progn (funcall func p end) nil) | 281 | \\\\verb<character> in TeX). |
| 289 | (funcall func p (match-beginning 0)) | 282 | |
| 290 | (when (< (point) end) | 283 | CALLBACK is a function accepting two arguments -- REG-BEG and REG-END -- that |
| 291 | (setq p (point)) | 284 | will be called for portions of the buffer outside of the environments defined by |
| 292 | (re-search-forward end-re nil t))))))))))) | 285 | PAIRS regexes. |
| 293 | 286 | ||
| 294 | (defun tildify-find-env (regexp pairs) | 287 | The function will return as soon as CALLBACK returns nil or point goes past END. |
| 288 | CALLBACK may be called on portions of the buffer outside of [BEG END); in fact | ||
| 289 | BEG argument is ignored. | ||
| 290 | |||
| 291 | This function is meant to be used to set `tildify-foreach-region-function' | ||
| 292 | variable. For example, for an XML file one might use: | ||
| 293 | (setq-local tildify-foreach-region-function | ||
| 294 | (apply-partially 'tildify-foreach-ignore-environments | ||
| 295 | '((\"<! *--\" . \"-- *>\") (\"<\" . \">\"))))" | ||
| 296 | (let ((beg-re (concat "\\(?:" (mapconcat 'car pairs "\\)\\|\\(?:") "\\)")) | ||
| 297 | p end-re) | ||
| 298 | (save-excursion | ||
| 299 | (save-restriction | ||
| 300 | (widen) | ||
| 301 | (goto-char (point-min)) | ||
| 302 | (while (and (< (setq p (point)) end) | ||
| 303 | (if (setq end-re (tildify--find-env beg-re pairs)) | ||
| 304 | (and (funcall callback p (match-beginning 0)) | ||
| 305 | (< (point) end) | ||
| 306 | (re-search-forward end-re nil t)) | ||
| 307 | (funcall callback p end) | ||
| 308 | nil))))))) | ||
| 309 | |||
| 310 | (defun tildify--foreach-region (callback beg end) | ||
| 311 | "Call CALLBACK on portions of the buffer between BEG and END. | ||
| 312 | |||
| 313 | Which portions to call CALLBACK on is determined by | ||
| 314 | `tildify-foreach-region-function' variable. This function merely makes sure | ||
| 315 | CALLBACK is not called with portions of the buffer outside of [BEG END)." | ||
| 316 | (let ((func (lambda (reg-beg reg-end) | ||
| 317 | (setq reg-beg (max reg-beg beg) reg-end (min reg-end end)) | ||
| 318 | (and (or (>= reg-beg reg-end) | ||
| 319 | (funcall callback reg-beg reg-end)) | ||
| 320 | (< reg-end end))))) | ||
| 321 | (funcall tildify-foreach-region-function func beg end))) | ||
| 322 | |||
| 323 | (defun tildify--find-env (regexp pairs) | ||
| 295 | "Find environment using REGEXP. | 324 | "Find environment using REGEXP. |
| 296 | Return regexp for the end of the environment found in PAIRS or nil if | 325 | Return regexp for the end of the environment found in PAIRS or nil if |
| 297 | no environment was found." | 326 | no environment was found." |
diff --git a/test/ChangeLog b/test/ChangeLog index 1c739d456ac..475b6a3f473 100644 --- a/test/ChangeLog +++ b/test/ChangeLog | |||
| @@ -2,6 +2,14 @@ | |||
| 2 | 2 | ||
| 3 | * automated/tildify-tests.el (tildify-test-html, tildify-test-xml): | 3 | * automated/tildify-tests.el (tildify-test-html, tildify-test-xml): |
| 4 | HTML and XML now use no-break space as hard space. Update tests. | 4 | HTML and XML now use no-break space as hard space. Update tests. |
| 5 | (tildify-test-foreach-ignore-environments) | ||
| 6 | (tildify-test-foreach-ignore-environments-early-return, | ||
| 7 | (tildify-test-foreach-region) | ||
| 8 | (tildify-test-foreach-region-early-return) | ||
| 9 | (tildify-test-foreach-region-limit-region): New tests of | ||
| 10 | `tildify-foreach-ignore-environments' and | ||
| 11 | `tildify--foreach-region' functions. | ||
| 12 | (with-test-foreach): New helper macro for the above tests. | ||
| 5 | 13 | ||
| 6 | 2014-11-17 Glenn Morris <rgm@gnu.org> | 14 | 2014-11-17 Glenn Morris <rgm@gnu.org> |
| 7 | 15 | ||
diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el index e532cf00686..55d2d576721 100644 --- a/test/automated/tildify-tests.el +++ b/test/automated/tildify-tests.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; tildify-test.el --- ERT tests for tildify.el | 1 | ;;; tildify-test.el --- ERT tests for tildify.el -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2014 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2014 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -117,8 +117,8 @@ latter is missing, SENTENCE will be used in all placeholder positions." | |||
| 117 | (insert "foo whatever end-foo") | 117 | (insert "foo whatever end-foo") |
| 118 | (goto-char (point-min)) | 118 | (goto-char (point-min)) |
| 119 | (should (string-equal "end-foo" | 119 | (should (string-equal "end-foo" |
| 120 | (tildify-find-env "foo\\|bar" | 120 | (tildify--find-env "foo\\|bar" |
| 121 | '(("foo\\|bar" . ("end-" 0)))))))) | 121 | '(("foo\\|bar" . ("end-" 0)))))))) |
| 122 | 122 | ||
| 123 | 123 | ||
| 124 | (ert-deftest tildify-test-find-env-group-index-bug () | 124 | (ert-deftest tildify-test-find-env-group-index-bug () |
| @@ -129,7 +129,60 @@ latter is missing, SENTENCE will be used in all placeholder positions." | |||
| 129 | (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)")) | 129 | (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)")) |
| 130 | (insert "open-foo whatever close-foo") | 130 | (insert "open-foo whatever close-foo") |
| 131 | (goto-char (point-min)) | 131 | (goto-char (point-min)) |
| 132 | (should (string-equal "close-foo" (tildify-find-env beg-re pairs)))))) | 132 | (should (string-equal "close-foo" (tildify--find-env beg-re pairs)))))) |
| 133 | |||
| 134 | |||
| 135 | (defmacro with-test-foreach (expected &rest body) | ||
| 136 | "Helper macro for testing foreach functions. | ||
| 137 | BODY has access to pairs variable and called lambda." | ||
| 138 | (declare (indent 1)) | ||
| 139 | (let ((got (make-symbol "got"))) | ||
| 140 | `(with-temp-buffer | ||
| 141 | (insert "1 /- 2 -/ 3 V~ 4 ~ 5 /- 6 -/ 7") | ||
| 142 | (let* ((pairs '(("/-" . "-/") ("V\\(.\\)" . (1)))) | ||
| 143 | (,got "") | ||
| 144 | (called (lambda (s e) | ||
| 145 | (setq ,got (concat ,got (buffer-substring s e)))))) | ||
| 146 | (setq-local tildify-foreach-region-function | ||
| 147 | (apply-partially 'tildify-foreach-ignore-environments | ||
| 148 | pairs)) | ||
| 149 | ,@body | ||
| 150 | (should (string-equal ,expected ,got)))))) | ||
| 151 | |||
| 152 | (ert-deftest tildify-test-foreach-ignore-environments () | ||
| 153 | "Basic test of `tildify-foreach-ignore-environments'" | ||
| 154 | (with-test-foreach "1 3 5 7" | ||
| 155 | (tildify-foreach-ignore-environments pairs called (point-min) (point-max)))) | ||
| 156 | |||
| 157 | |||
| 158 | (ert-deftest tildify-test-foreach-ignore-environments-early-return () | ||
| 159 | "Test whether `tildify-foreach-ignore-environments' returns early | ||
| 160 | The function must terminate as soon as callback returns nil." | ||
| 161 | (with-test-foreach "1 " | ||
| 162 | (tildify-foreach-ignore-environments | ||
| 163 | pairs (lambda (start end) (funcall called start end) nil) | ||
| 164 | (point-min) (point-max)))) | ||
| 165 | |||
| 166 | (ert-deftest tildify-test-foreach-region () | ||
| 167 | "Basic test of `tildify--foreach-region'" | ||
| 168 | (with-test-foreach "1 3 5 7" | ||
| 169 | (tildify--foreach-region called (point-min) (point-max)))) | ||
| 170 | |||
| 171 | (ert-deftest tildify-test-foreach-region-early-return () | ||
| 172 | "Test whether `tildify--foreach-ignore' returns early | ||
| 173 | The function must terminate as soon as callback returns nil." | ||
| 174 | (with-test-foreach "1 " | ||
| 175 | (tildify--foreach-region (lambda (start end) (funcall called start end) nil) | ||
| 176 | (point-min) (point-max)))) | ||
| 177 | |||
| 178 | (ert-deftest tildify-test-foreach-region-limit-region () | ||
| 179 | "Test whether `tildify--foreach-ignore' limits callback to given region" | ||
| 180 | (with-test-foreach "3 " | ||
| 181 | (tildify--foreach-region called | ||
| 182 | (+ (point-min) 10) (+ (point-min) 16))) ; start at "3" end past "4" | ||
| 183 | (with-test-foreach "3 5" | ||
| 184 | (tildify--foreach-region called | ||
| 185 | (+ (point-min) 10) (+ (point-min) 20)))) ; start at "3" end past "5" | ||
| 133 | 186 | ||
| 134 | 187 | ||
| 135 | (provide 'tildify-tests) | 188 | (provide 'tildify-tests) |