From 273f42ef5610606842db36006ddd09bf9c1856e6 Mon Sep 17 00:00:00 2001 From: Siddhartha Date: Tue, 30 Apr 2024 18:22:05 -0700 Subject: [PATCH] More conservative whitespace handling in symex bounds Include at most one trailing whitespace character in symex bounds. Fix whitespace regex. Don't eliminate intervening trailing whitespace on deletion. Some whitespace-related utilities. --- symex-primitives-lisp.el | 13 +++++++++---- symex-utils.el | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/symex-primitives-lisp.el b/symex-primitives-lisp.el index 1db2d210..fadf57de 100644 --- a/symex-primitives-lisp.el +++ b/symex-primitives-lisp.el @@ -165,7 +165,7 @@ (defvar symex--re-blank-line "^[[:space:]]*$" "A blank line, either empty or containing only whitespace.") -(defvar symex--re-whitespace "[[:space:]|\n]*" +(defvar symex--re-whitespace "[[:space:]|\n]+" "Whitespace that may extend over many lines.") (defvar symex--re-symex-line "^[[:space:]]*[^;[:space:]\n]" @@ -211,6 +211,10 @@ (cond ((looking-at "\\s(") (forward-list 1) (backward-char 1)) ((looking-at "\\s)") (forward-char 1) (backward-list 1)))) +(defun symex-whitespace-p () + "Check if we're looking at whitespace." + (looking-at-p symex--re-whitespace)) + (defun symex-comment-line-p () "Check if we're currently at the start of a comment line." (save-excursion @@ -424,8 +428,9 @@ including trailing whitespace at the end of the last symex." (let ((endpoint (symex-lisp--get-end-point-helper count))) (if include-whitespace (progn (goto-char endpoint) - (if (symex--go-to-next-non-whitespace-char) - (1- (point)) + (if (and (not (eobp)) + (symex-whitespace-p)) + (1+ endpoint) endpoint)) endpoint)))) @@ -678,7 +683,7 @@ line." (error nil)) (symex--join-to-match symex--re-right)))))))) ((symex-right-p) (fixup-whitespace)) ; abc <>) - (t (symex--join-to-non-whitespace)))) + (t nil))) ;;; Utilities diff --git a/symex-utils.el b/symex-utils.el index 680aa10d..20e4cf79 100644 --- a/symex-utils.el +++ b/symex-utils.el @@ -71,6 +71,9 @@ the following recipe instead." (defvar symex--re-non-whitespace "[^[:space:]\n]" "A non-whitespace character.") +(defvar symex--re-non-whitespace-or-newline "[^[:space:]]" + "A non-whitespace character.") + (defun symex--go-to-next-non-whitespace-char () "Move point to the next non-whitespace character. @@ -101,6 +104,13 @@ This eliminates whitespace between the original position and the found match." (symex--join-to-match symex--re-non-whitespace)) +(defun symex--join-to-non-whitespace-or-newline () + "Join current position to the next non-whitespace character. + +This eliminates whitespace between the original position and the found +match." + (symex--join-to-match symex--re-non-whitespace-or-newline)) + ;; `with-undo-collapse` macro, to treat a sequence of operations ;; as a single entry in the undo list. ;; From: https://emacs.stackexchange.com/questions/7558/collapsing-undo-history/7560#7560