[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tex-parens 11e41cc44d 27/62: add tp-max-delim-length, o
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tex-parens 11e41cc44d 27/62: add tp-max-delim-length, optimize tp--search-backward |
Date: |
Thu, 6 Jun 2024 04:00:57 -0400 (EDT) |
branch: externals/tex-parens
commit 11e41cc44de04717666b721709fc54a0872bf73b
Author: Paul Nelson <ultrono@gmail.com>
Commit: Paul Nelson <ultrono@gmail.com>
add tp-max-delim-length, optimize tp--search-backward
---
tex-parens.el | 90 +++++++++++++++++++++++++++++++++--------------------------
1 file changed, 50 insertions(+), 40 deletions(-)
diff --git a/tex-parens.el b/tex-parens.el
index e338f99760..11507246cc 100644
--- a/tex-parens.el
+++ b/tex-parens.el
@@ -115,6 +115,12 @@ Here `defun' means top-level environment."
"Left/right delimiters not to be combined with modifiers."
:type '(repeat (cons string string)))
+(defcustom tp-max-delim-length 25
+ "Maximum length of a delimiter.
+This is comfortably larger than `\biggl\langle' and
+`\begin{proposition}', for example."
+ :type 'integer)
+
(defun tp--reduce-append (func list1 list2)
"List consisting of FUNC applied to pairs from LIST1 and LIST2."
(cl-reduce #'append
@@ -231,6 +237,13 @@ defun-based commands."
:group 'tex-parens)
(defun tp--ignore (m-str m-begin m-end)
+ "Check if M-STR should be ignored.
+M-STR is the string matched by the search, M-BEGIN is the
+beginning of the match, and M-END is the end of the match. If
+TP-IGNORE-COMMENTS is non-nil, then ignore comments. If M-STR is
+a double prime in math mode, then ignore it. If M-STR is a
+dollar delimiter that does not demarcate math mode, then ignore
+it."
(or
(and
tp-ignore-comments
@@ -253,6 +266,7 @@ defun-based commands."
(tp--math-face-p))))))
(defun tp--search-forward (regexp bound)
+ "Search forward for REGEXP up to BOUND."
(let (success done)
(while (not done)
(if (re-search-forward regexp bound t)
@@ -265,39 +279,35 @@ defun-based commands."
(when success
(match-string 0))))
-;; TODO (not essential): speed this up
-
-(defun tp--search-backward (regexp bound)
- (let* ((text (buffer-substring bound (point)))
- (buf (current-buffer))
- result)
- (with-temp-buffer
- (insert (reverse text))
- (goto-char (point-min))
- (setq result
- (let (success done)
- (while (not done)
- (if (re-search-forward regexp nil t)
- (when (not
- (let ((new-point (point))
- (m-string (match-string 0)))
- (with-current-buffer buf
- (save-excursion
- (backward-char (1- new-point))
- (tp--ignore m-string
- (point)
- (+ (point) (length m-string)))))))
- (setq done t
- success t))
- (setq done t)))
- (when success
- (cons (point) (match-string 0))))))
- (when result
- (let ((new-point (car result))
- (match (cdr result)))
- (backward-char (1- new-point))
- ;; (goto-char (- (point) (1- new-point)))
- (reverse match)))))
+(defun tp--search-backward (regexp regexp-reverse bound)
+ "Search backward, greedily, for REGEXP up to BOUND.
+Assumes that REGEXP-REVERSE is the reverse of REGEXP."
+ (let (done success match)
+ (while (not done)
+ (if (re-search-backward regexp bound t)
+ (progn
+ ;; find the longest match by applying the backward regexp
+ ;; to the reversed text
+ (goto-char (match-end 0))
+ (let ((text (buffer-substring-no-properties
+ (save-excursion (backward-char tp-max-delim-length)
+ (point))
+ (point))))
+ (with-temp-buffer
+ (insert (reverse text))
+ (goto-char (point-min))
+ (re-search-forward regexp-reverse nil t)
+ (setq match (reverse (match-string 0))))
+ (backward-char (length match))
+ (when (not (tp--ignore match
+ (point)
+ (+ (point) (length match))))
+ (setq done t)
+ (setq success t))))
+ ;; didn't find anything, so we failed
+ (setq done t)))
+ (when success
+ match)))
(defun tp--forward-bound ()
"Return the bound for forward search."
@@ -323,7 +333,7 @@ Return the delimiter found, or nil if none is found."
"Search for the previous delimiter up to BOUND.
Return the delimiter found, or nil if none is found."
(unless bound (setq bound (tp--backward-bound)))
- (tp--search-backward tp--regexp-reverse bound))
+ (tp--search-backward tp--regexp tp--regexp-reverse bound))
(defun tp--close-of-open (delim)
"Check if DELIM is opening, return the corresponding closing.
@@ -827,7 +837,7 @@ and point is before (zot), \\[raise-sexp] will give you
(defun tp--barf-left ()
"Barf the next sexp out of the current one, to the right."
(when-let* ((pos (point))
- (bound (save-excursion (backward-char 30) (point)))
+ (bound (save-excursion (backward-char tp-max-delim-length)
(point)))
(text (buffer-substring bound pos))
(reversed-text (reverse text))
(reverse-match
@@ -865,8 +875,8 @@ Otherwise, call `self-insert-command'."
(interactive)
(cond
((and
- (not (looking-back tp--regexp-open (save-excursion (backward-char 30)
(point))))
- (looking-back tp--regexp-close (save-excursion (backward-char 30)
(point))))
+ (not (looking-back tp--regexp-open (save-excursion (backward-char
tp-max-delim-length) (point))))
+ (looking-back tp--regexp-close (save-excursion (backward-char
tp-max-delim-length) (point))))
(tp--barf-left))
((and
(not (looking-at tp--regexp-close))
@@ -901,7 +911,7 @@ Otherwise, call `self-insert-command'."
(defun tp--slurp-right ()
"Slurp the next sexp into the current one, to the right."
(when-let* ((pos (point))
- (bound (save-excursion (backward-char 30) (point)))
+ (bound (save-excursion (backward-char tp-max-delim-length)
(point)))
(text (buffer-substring bound pos))
(reversed-text (reverse text))
(reverse-match
@@ -931,8 +941,8 @@ Otherwise, call `self-insert-command'."
(interactive)
(cond
((and
- (not (looking-back tp--regexp-open (save-excursion (backward-char 30)
(point))))
- (looking-back tp--regexp-close (save-excursion (backward-char 30)
(point))))
+ (not (looking-back tp--regexp-open (save-excursion (backward-char
tp-max-delim-length) (point))))
+ (looking-back tp--regexp-close (save-excursion (backward-char
tp-max-delim-length) (point))))
(tp--slurp-right))
((and
(not (looking-at tp--regexp-close))
- [elpa] externals/tex-parens 2794e7a338 09/62: fix bug in tex-parens-backward-up, (continued)
- [elpa] externals/tex-parens 2794e7a338 09/62: fix bug in tex-parens-backward-up, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens d3aa2a152a 10/62: some renamining, and better handling of unmatched forward/backward, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 44386cb173 14/62: clean-up, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens e6d1456573 13/62: adding docs, etc, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens ac86432b45 19/62: add cfg mention, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 5362f5a284 20/62: fix dump bug introduced in revision, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 06303e66ba 23/62: add arg initialization where appropriate, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 2bdbd9c65c 32/62: tweak tp--end-of-defun, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 348fe1de62 01/62: Initial commit, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens ceef53e3d4 03/62: fix typo, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 11e41cc44d 27/62: add tp-max-delim-length, optimize tp--search-backward,
ELPA Syncer <=
- [elpa] externals/tex-parens 4850085c65 39/62: sort out bug w/ previous commit, and up the default delim limit, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 2ce7313354 46/62: add #'undo to list of commands that open previews/folds, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 72fc4b0e6e 50/62: add basic package info, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 73b7fc98ad 57/62: update readme to indicate how to remap keys, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 84b9b5e76e 06/62: added sexp stuff, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens ddc4663eaa 11/62: add other math delimiters like $$, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 145e9eafc4 15/62: lots of cleanup, support for $\text{..$..$..}$, etc, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 82eff30aa5 16/62: ignore dollar delimiters that do not demarcate math mode, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 665c91c331 18/62: typo, ELPA Syncer, 2024/06/06
- [elpa] externals/tex-parens 683add8b7c 29/62: fix silly tp-kill-sexp bug, ELPA Syncer, 2024/06/06