branch: externals/hyperbole
commit d41a0d9ef7babee384c19f299e9d06852f3be8ad
Merge: afb12832eb 0e151a03ef
Author: Robert Weiner <[email protected]>
Commit: GitHub <[email protected]>

    Merge pull request #852 from rswgnu/rsw
    
    hywiki-delimited-p, hywiki-word-at - Fix to handle edge cases
---
 ChangeLog            | 15 +++++++++++++++
 hibtypes.el          |  4 ++--
 hywiki.el            | 46 +++++++++++++++++++++++++++++++---------------
 test/hywiki-tests.el |  8 ++++----
 4 files changed, 52 insertions(+), 21 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5d5233047f..fa0c0e192c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2026-02-02  Bob Weiner  <[email protected]>
+
+* test/hywiki-tests.el (hywiki-tests--edit-string-pairs): Enable two more tests
+    that now pass.
+
+* hywiki.el (hywiki-word-at): Near the end, change the string-match to not
+    have to match the entire string since it may contain disallowed characters.
+    Cut the reference string off at the first invalid character.
+    This fixes a problem that prevented highlighting a reference when a 
trailing
+    double quote was deleted.
+            (hywiki-delimited-p): Remove doc constraint that only delimiters 
around
+    a single HyWikiWord reference are allowed; also trim any trailing 
whitespace.
+            (hywiki-delimited-p): Fix to handle matching quotes when checking
+    matching delimiters.
+
 2026-02-02  Mats Lidell  <[email protected]>
 
 * hibtypes.el (hywiki-active-in-current-buffer-p)
diff --git a/hibtypes.el b/hibtypes.el
index b8287ede12..3c465fa4b5 100644
--- a/hibtypes.el
+++ b/hibtypes.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    19-Sep-91 at 20:45:31
-;; Last-Mod:      5-Jan-26 at 23:42:19 by Bob Weiner
+;; Last-Mod:      2-Feb-26 at 18:22:19 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -65,8 +65,8 @@
 (declare-function htype:def-symbol "hact")
 (declare-function hui:help-ebut-highlight "hui")
 (declare-function hyperb:stack-frame "hversion")
-(declare-function hywiki-active-in-current-buffer-p "hywiki")
 (declare-function hyrolo-get-file-list "hyrolo")
+(declare-function hywiki-active-in-current-buffer-p "hywiki")
 (declare-function hywiki-get-singular-wikiword "hywiki")
 (declare-function hywiki-highlight-word-get-range "hywiki")
 (declare-function hywiki-referent-exists-p "hywiki")
diff --git a/hywiki.el b/hywiki.el
index c071db996f..511d23a0e4 100644
--- a/hywiki.el
+++ b/hywiki.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    21-Apr-24 at 22:41:13
-;; Last-Mod:      1-Feb-26 at 19:16:29 by Bob Weiner
+;; Last-Mod:      2-Feb-26 at 23:16:23 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -3432,14 +3432,19 @@ non-nil or this will return nil."
                                               end   (match-end 0)
                                               ;; No following char
                                               wikiword (string-trim 
(match-string-no-properties 0)))))))))
-                    ;; If `wikiword' reference has a #section, ensure there are
-                    ;; no invalid chars.  One set of \n\r characters is 
allowed.
+                    ;; If `wikiword' reference has a #section, ensure
+                    ;; it stops when there are any disallowed characters
+                    ;; and reset the value of 'end' to match any reduction.
+                    ;; One set of \n\r characters is allowed but no
+                    ;; whitespace at the end of the reference.
                     (if (and (stringp wikiword) (string-match "#" wikiword))
-                        (string-match "#[^][#()<>{}\"\f]+\\'" wikiword)
+                        (when (string-match 
"#[^][#()<>{}\"\f]*[^][#()<>{}\"\f\t\n\r ]" wikiword)
+                          (setq end (- end (- (length wikiword)
+                                              (match-end 0)))
+                                wikiword (substring wikiword 0 (match-end 0))))
                       t))
                (if range-flag
-                   (progn
-                     (list wikiword start end))
+                   (list wikiword start end)
                  wikiword)
              (when range-flag
                '(nil nil nil))))))
@@ -3533,9 +3538,6 @@ or this will return nil."
 Any non-nil value returned is a list of (hywikiword-ref start-pos end-pos).
 The delimited range must be two lines or less with point on the first line.
 
-Matching delimiters around anything other than a single HyWikiWord reference
-are ignored.
-
 Use `hywiki-word-at', which calls this, to determine whether there is
 a HyWikiWord at point."
   (save-excursion
@@ -3545,13 +3547,27 @@ a HyWikiWord at point."
       ;; Limit balanced pair checks to current through next lines for speed.
       ;; Point must be either on the opening line.
       (narrow-to-region (line-beginning-position) (line-end-position 2))
-      (or (hypb:in-string-p nil t)
-         (let ((range (hargs:delimited "[\[<\(\{]" "[\]\}\)\>]" t t t)))
-           (and range
-                ;; Ensure closing delimiter is a match for the opening one
-                (eq (matching-paren (char-before (nth 1 range)))
+      (let* ((range (or (hypb:in-string-p nil t)
+                       (hargs:delimited "[\[<\(\{]" "[\]\}\)\>]" t t t)))
+            (wikiword (car range))
+            range-trimmed
+            wikiword-trimmed)
+       (if (and wikiword (string-match "[ \t\n\r\f]+\\'" wikiword))
+           ;; Strip any trailing whitespace
+           (setq wikiword-trimmed (substring wikiword 0 (match-beginning 0))
+                 range-trimmed (list wikiword-trimmed (nth 1 range)
+                                     (- (nth 2 range) (length (match-string
+                                                               0 wikiword)))))
+         (setq range-trimmed range))
+       (and range-trimmed
+            ;; Ensure closing delimiter is a match for the opening one
+            (or (eq (matching-paren (char-before (nth 1 range)))
                     (char-after (nth 2 range)))
-                range))))))
+                ;; May be string quotes where matching-paren returns nil.
+                (and (eq (char-before (nth 1 range))
+                         (char-after (nth 2 range)))
+                     (eq (char-syntax (char-before (nth 1 range))) ?\")))
+            range-trimmed)))))
 
 (defun hywiki-word-face-at-p (&optional pos)
   "Non-nil if point or optional POS has the `hywiki-word-face' property.
diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el
index 3dee417b6d..42b31bf33a 100644
--- a/test/hywiki-tests.el
+++ b/test/hywiki-tests.el
@@ -3,7 +3,7 @@
 ;; Author:       Mats Lidell
 ;;
 ;; Orig-Date:    18-May-24 at 23:59:48
-;; Last-Mod:      2-Feb-26 at 00:32:30 by Bob Weiner
+;; Last-Mod:      2-Feb-26 at 23:37:48 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -28,12 +28,12 @@
 
 (defconst hywiki-tests--edit-string-pairs
    [
-    ;; !! TODO: These tests fail
-    ;; ("Hi#a<insert-char ?b> cd" "{Hi#ab} cd")
+    ;; !! TODO: This test fails
     ;; ("\"WikiWord#section with spaces\"<backward-delete-char 1>" 
"\"{WikiWord#section} with spaces") ;; shrink highlight to "{WikiWord#section}
-    ;; ("\"WikiWord#a b c<backward-delete-char 2>" "\"{WikiWord#a} b")
 
     ;; These tests pass
+    ("Hi#a<insert-char ?b> cd" "{Hi#ab} cd")
+    ("\"WikiWord#a b c<backward-delete-char 2>" "\"{WikiWord#a} b")
     ("Hi" "{Hi}")
     ("HyWikiW<kill-word 1>ord<yank 1> HyW<kill-word 1>ikiWord<yank 1>"
      "{HyWikiWord} {HyWikiWord}")

Reply via email to