From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michal Nazarewicz Newsgroups: gmane.emacs.bugs Subject: bug#17699: [PATCH 6/7] tildify.el: Rewrite `tildify-region' and co., add foreach function. Date: Thu, 5 Jun 2014 13:27:35 +0200 Message-ID: <1401967656-16171-6-git-send-email-mina86@mina86.com> References: <1401967656-16171-1-git-send-email-mina86@mina86.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1401968493 11858 80.91.229.3 (5 Jun 2014 11:41:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 5 Jun 2014 11:41:33 +0000 (UTC) To: 17699@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Jun 05 13:41:24 2014 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WsW2t-0005N9-4q for geb-bug-gnu-emacs@m.gmane.org; Thu, 05 Jun 2014 13:41:23 +0200 Original-Received: from localhost ([::1]:40033 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsW2s-0001tn-Kf for geb-bug-gnu-emacs@m.gmane.org; Thu, 05 Jun 2014 07:41:22 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40812) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsW2i-0001qz-El for bug-gnu-emacs@gnu.org; Thu, 05 Jun 2014 07:41:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WsW2Z-0005NI-Kb for bug-gnu-emacs@gnu.org; Thu, 05 Jun 2014 07:41:12 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:45944) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsW2Z-0005Mr-Gg for bug-gnu-emacs@gnu.org; Thu, 05 Jun 2014 07:41:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1WsVq2-0007nC-FS for bug-gnu-emacs@gnu.org; Thu, 05 Jun 2014 07:28:06 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Michal Nazarewicz Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 05 Jun 2014 11:28:06 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 17699 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 17699-submit@debbugs.gnu.org id=B17699.140196768529938 (code B ref 17699); Thu, 05 Jun 2014 11:28:06 +0000 Original-Received: (at 17699) by debbugs.gnu.org; 5 Jun 2014 11:28:05 +0000 Original-Received: from localhost ([127.0.0.1]:44816 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WsVpz-0007md-M7 for submit@debbugs.gnu.org; Thu, 05 Jun 2014 07:28:04 -0400 Original-Received: from mail-la0-f73.google.com ([209.85.215.73]:48588) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WsVpo-0007kT-FQ for 17699@debbugs.gnu.org; Thu, 05 Jun 2014 07:28:00 -0400 Original-Received: by mail-la0-f73.google.com with SMTP id e16so85946lan.2 for <17699@debbugs.gnu.org>; Thu, 05 Jun 2014 04:27:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=r3tKJ4wm9AjAvV5zMxl2sThwFXn4rrO2/FTpJMC+LHE=; b=mxSMnGfi4+raJN6J4wcrx+lQJauqkPHahFGE6Cwml5Dp/Sd4EHe698H3oi2GTBoU4s hBezF7IzQLNLEEJu3jCJcsme5UAjAu8ZBi6QFoDG1xTNlDoC5mGOpNUjxzn/ZqU+aD8v exJacEaCYCCZ1p8ool7fjLrO8qDKSOfAy9hPcxrdxsrdE52K1Oee1Kt85wnzrnOh0IQN 2cgGSfRkOCQpz21si1ffWKFHiT1Q0B5WGTtpZJMfBRMAP1dvdqkvTuWE4HwZgX3mQduo yR533de/BP11uH0oqUdiGfgprLXNxt7M/t9fRXYC7CXPyeld9TCV5iFVHUb+ZqFu6Syy MLaQ== X-Gm-Message-State: ALoCoQlUl3CAOBqYkgIt5bBfaoLDXjtP6d0XX6iz+6+tjdp7eCGQ6bbuqCEt8Qx2ejjAhYAp3u7AzIv7y+XAngE0nj7N7IKkZKZff9N8EdTCzK7OIDO492qdoZEl7zptr8hvYFDF8ByJ X-Received: by 10.15.35.134 with SMTP id g6mr3239295eev.3.1401967666362; Thu, 05 Jun 2014 04:27:46 -0700 (PDT) Original-Received: from corp2gmr1-1.eem.corp.google.com (corp2gmr1-1.eem.corp.google.com [172.25.138.99]) by gmr-mx.google.com with ESMTPS id s1si901287wiw.3.2014.06.05.04.27.46 for <17699@debbugs.gnu.org> (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Jun 2014 04:27:46 -0700 (PDT) Original-Received: from hpgntag-ubiq62.eem.corp.google.com (hpgntag-ubiq62.eem.corp.google.com [172.25.135.103]) by corp2gmr1-1.eem.corp.google.com (Postfix) with ESMTP id 14D021CA35C for <17699@debbugs.gnu.org>; Thu, 5 Jun 2014 04:27:46 -0700 (PDT) Original-Received: by hpgntag-ubiq62.eem.corp.google.com (Postfix, from userid 126942) id E7B3A10B6E3; Thu, 5 Jun 2014 13:27:45 +0200 (CEST) X-Mailer: git-send-email 2.0.0.526.g5318336 In-Reply-To: <1401967656-16171-1-git-send-email-mina86@mina86.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:90038 Archived-At: * lisp/textmodes/tildify.el (tildify-foreach-region-outside-env): New function which calls a callback on portions of the buffer that are outside of ignored environments. (tildify-build-regexp): Remove function since it is now incorporated in `tildify-foreach-region-outside-env' where it is optimised and simplified by the use of `mapconcat'. (tildify-tildify): Return number of substitutions made so that… (tildify-count): …can be removed. (tildify-find-env): Accept a new PAIRS argument which was previously looked up in `tildify-ignored-environments-alist' each time the function was called. With this change, the lookup is performed only once in `tildify-foreach-region-outside-env'. (tildify-region): Greatly simplify the function since now most of the work is done by `tildify-foreach-region-outside-env'. (tildify-mode-alist): Simplify slightly by avoiding if and setq and instead using or. * tests/automated/tildify-tests.el (tildify-test-find-env-end-re-bug) (tildify-test-find-env-group-index-bug): Update to support new signature of the `tildify-foreach-region-outside-env' function. Namely, it now takes pairs as an argument instead of looking it up in `tildify-ignored-environments-alist'. --- lisp/ChangeLog | 17 +++++ lisp/textmodes/tildify.el | 145 +++++++++++++++++----------------------- test/ChangeLog | 6 ++ test/automated/tildify-tests.el | 17 +++-- 4 files changed, 92 insertions(+), 93 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 0351d54..075a424 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,22 @@ 2014-06-05 Michal Nazarewicz + * textmodes/tildify.el (tildify-foreach-region-outside-env): New + function which calls a callback on portions of the buffer that are + outside of ignored environments. + (tildify-build-regexp): Remove function since it is now + incorporated in `tildify-foreach-region-outside-env' where it is + optimised and simplified by the use of `mapconcat'. + (tildify-tildify): Return number of substitutions made so that… + (tildify-count): …can be removed. + (tildify-find-env): Accept a new PAIRS argument which was + previously looked up in `tildify-ignored-environments-alist' each + time the function was called. With this change, the lookup is + performed only once in `tildify-foreach-region-outside-env'. + (tildify-region): Greatly simplify the function since now most of + the work is done by `tildify-foreach-region-outside-env'. + (tildify-mode-alist): Simplify slightly by avoiding if and setq + and instead using or. + * textmodes/tildify.el (tildify-ignored-environments-alist): Optimise environments regexes diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el index 39ccad7..50fee2f 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 -;; Version: 4.5.2 +;; Michal Nazarewicz +;; Version: 4.5.3 ;; 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 @@ -205,51 +200,16 @@ This function performs no refilling of the changed text. If DONT-ASK is set, or called interactively with prefix argument, user won't be prompted for confirmation of each substitution." (interactive "*rP") - (setq tildify-count 0) - (let (a - z - (marker-end (copy-marker end)) - end-env - finish - (ask (not dont-ask)) - (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 (not dont-ask))) + (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 (&optional dont-ask) @@ -266,42 +226,58 @@ won't be prompted for confirmation of each substitution." ;;; *** 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))) + (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) "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 (when (re-search-forward regexp nil t) (save-match-data - ;; Build end-env regexp - (let ((match (match-string 0)) - (alist (tildify-mode-alist tildify-ignored-environments-alist))) - (while (not (eq (string-match (caar alist) match) 0)) - (setq alist (cdr alist))) - (let ((expression (cdar alist))) + (let ((match (match-string 0))) + (while (not (eq (string-match (caar pairs) match) 0)) + (setq pairs (cdr pairs))) + (let ((expression (cdar pairs))) (if (stringp expression) expression (mapconcat @@ -319,8 +295,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 +309,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 +337,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 *** diff --git a/test/ChangeLog b/test/ChangeLog index 38a4feb..6248d6c 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,5 +1,11 @@ 2014-06-05 Michal Nazarewicz + * automated/tildify-tests.el (tildify-test-find-env-end-re-bug) + (tildify-test-find-env-group-index-bug): Update to support new + signature of the `tildify-foreach-region-outside-env' function. + Namely, it now takes pairs as an argument instead of looking it up in + `tildify-ignored-environments-alist'. + * automated/tildify-tests.el (tildify-test--example-html): Add support for generating XML code, so that… (tildify-test-xml) …test can be added to check handling of XML diff --git a/test/automated/tildify-tests.el b/test/automated/tildify-tests.el index dd404fc..cf18320 100644 --- a/test/automated/tildify-tests.el +++ b/test/automated/tildify-tests.el @@ -114,23 +114,22 @@ latter is missing, SENTENCE will be used in all placeholder positions." (ert-deftest tildify-test-find-env-end-re-bug () "Tests generation of end-regex using mix of indexes and strings" (with-temp-buffer - (let ((tildify-ignored-environments-alist - `((,major-mode ("foo\\|bar" . ("end-" 0)))))) - (insert "foo whatever end-foo") - (goto-char (point-min)) - (should (string-equal "end-foo" (tildify-find-env "foo\\|bar")))))) + (insert "foo whatever end-foo") + (goto-char (point-min)) + (should (string-equal "end-foo" + (tildify-find-env "foo\\|bar" + '(("foo\\|bar" . ("end-" 0)))))))) (ert-deftest tildify-test-find-env-group-index-bug () "Tests generation of match-string indexes" (with-temp-buffer - (let ((tildify-ignored-environments-alist - `((,major-mode ("start-\\(foo\\|bar\\)" . ("end-" 1)) - ("open-\\(foo\\|bar\\)" . ("close-" 1))))) + (let ((pairs '(("start-\\(foo\\|bar\\)" . ("end-" 1)) + ("open-\\(foo\\|bar\\)" . ("close-" 1)))) (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)))))) + (should (string-equal "close-foo" (tildify-find-env beg-re pairs)))))) (provide 'tildify-tests) -- 2.0.0.526.g5318336