branch: externals/hyperbole
commit d1a86095dd0d7cb3fffa5796df1802940aaf6cea
Merge: dac958d152 5d10fbbe0c
Author: Robert Weiner <[email protected]>
Commit: GitHub <[email protected]>

    Merge branch 'rsw' into matsl-fix-save-referent-find-use-menu
---
 ChangeLog            |  69 +++++++++++++++
 hywiki.el            | 186 ++++++++++++++++++++++++-----------------
 test/hywiki-tests.el | 231 +++++++++++++++++++++++++++++----------------------
 3 files changed, 314 insertions(+), 172 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5ae52ecb3a..0255c17bfb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,75 @@
 * test/hywiki-tests.el (hywiki-tests--save-referent-find-use-menu): Use
     hywiki-tests--preserve-hywiki-mode macro and refactor test to utilize that.
 
+2026-02-01  Bob Weiner  <[email protected]>
+
+* test/hywiki-tests.el (hywiki-tests--word-at): Return HyWikiWord reference
+    at point only if it has a referent.
+                       (hywiki-tests--publish-special-cases): Fix matching
+    issues and remove 'ert-info' output of html file as can fail in CI/CD.
+                       
(hywiki-tests--wikiword-step-check-edit-wikiword-in-emacs-lisp-mode):
+    Fix missing double quote causing failure.
+
+* hywiki.el (hywiki-word-store-around-point): Dehighlight only if Emacs
+    is idle and within a hywikiword in a valid context.
+            (hywiki-highlighted-word-at): Rewrite so matches 'hywiki-word-at'
+    behavior based on point but requires that the HyWikiWord be highlighted
+    or returns nil/nil range.
+            (hywiki-word-get-range): Add and use in
+    'hywiki-highlight-word-get-range'.
+            (hywiki-referent-exists-p)
+             hywiki--maybe-dehighlight-at-point): Get the range but don't
+    highlight the reference.
+
+* test/hywiki-tests.el (hywiki-tests--run-test-case): Remove initial
+    'erase-buffer' since callers set up the buffer prior to this call.
+    Add 'ert-info' so know the exact buffer state when the test is run.
+
+* hywiki.el (hywiki-maybe-highlight-references): Remove conditonals at the
+    start aside from whether hywiki-mode is active in this buffer since
+    a page may have been added and references to it need to be rehighlighted
+    so can't depend on previous highlighting having been done.
+
+* test/hywiki-tests.el (hywiki-tests--wikiword-identified-in-emacs-lisp-mode,
+                        hywiki-tests--face-property-for-wikiword-with-wikipage,
+                        hywiki-tests--verify-hywiki-word):
+    Add 'ert-info' to show which test has failed and fix categorization of
+    tests so they all pass.
+
+* hywiki.el (hywiki-word-highlight-in-current-buffer): Add for use as an
+    'after-change-major-mode-hook'.
+            (hywiki-word-highlight-buffers,
+             hywiki-word-dehighlight-buffers): Most major modes inherit
+    from 'fundamental-mode' which clears the hywiki buffer-local hooks,
+    so need to restore these to the current buffer via
+    'after-change-major-mode-hook'.  Setup to do that in above functions.
+            (hywiki-word-dehighlight-in-buffers): Create this function
+    and call from 'hywiki-word-dehighlight-buffers.
+            (hywiki-get-buffers): Optimize to check 'hywiki-mode-status'
+    only once.
+            (hywiki-get-buffers-in-windows): Fix doc to show that this
+    does not filter to only hywiki buffers.  Use 'hywiki-get-buffers'
+    for that.
+            (hywiki-word-highlight-in-frame): Fix to highlight in active
+    hywiki buffers only.
+            (hywiki-maybe-highlight-references): Fix that regexp search
+      leaves point after closing string delimiter and then tests if in
+      string.  Move point to within string (grouping 1) before testing.
+
+2026-02-01  Mats Lidell  <[email protected]>
+
+  (hywiki-tests--wikiword-step-check-verification)
+    (hywiki-tests--wikiword-step-check-verification-with-surrounding-text):
+    Create wikipages, required by hywkii-tests--word-at.
+
+  (hywiki-tests--wikiword-step-check-verification-with-faces)
+    (hywiki-tests--face-property-for-wikiword-with-wikipage)
+    (hywiki-tests--no-face-property-for-no-wikipage)
+    (hywiki-tests--verify-face-property-when-editing-wikiword)
+    (hywiki-tests--verify-face-property-when-editing-wikiword-first-char)
+    (hywiki-tests--references-to-org-link, hywiki-tests--word-face-at-p):
+    Drop interactive requirement.
+
 2026-01-31  Bob Weiner  <[email protected]>
 
 * hproperty.el (hproperty:char-property-face-p): Rename to
diff --git a/hywiki.el b/hywiki.el
index d5600bdc23..c071db996f 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:     31-Jan-26 at 22:45:09 by Bob Weiner
+;; Last-Mod:      1-Feb-26 at 19:16:29 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -567,9 +567,9 @@ including deletion commands and those in 
`hywiki-non-character-commands'."
     (set-marker hywiki--buttonize-end nil)
     (setq hywiki--buttonize-range nil))
 
-  (when (hywiki-non-hook-context-p)
+  (when (and (current-idle-time) (hywiki-non-hook-context-p) (hywiki-word-at))
     ;; Dehighlight any previously highlighted WikiWord at point if
-    ;; outside of a valid context.
+    ;; it is outside of a valid context.
     (save-restriction
       (narrow-to-region (line-beginning-position) (line-end-position))
       (hywiki-maybe-dehighlight-reference)))
@@ -1373,7 +1373,8 @@ Use `hywiki-get-referent' to determine whether a HyWiki 
page exists."
              (message "HyWikiWord page exists: \"%s\"" page-file)))
          (unless (or (hyperb:stack-frame 
'(hywiki-maybe-highlight-wikiwords-in-frame))
                      (and (not force-flag) page-file-readable page-in-hasht))
-           (hywiki-cache-save))
+           (hywiki-cache-save)
+           (hywiki-maybe-highlight-wikiwords-in-frame t))
          (run-hooks 'hywiki-add-page-hook)
          (when page-file (cons 'page page-file))))
     (when (called-interactively-p 'interactive)
@@ -1797,12 +1798,14 @@ This does not test whether a referent exists for the 
HyWikiWord; call
 A call to `hywiki-active-in-current-buffer-p' at point must return non-nil
 or this will return nil."
   (when (and (hywiki-active-in-current-buffer-p)
-            (setq hywiki--range
-                  (hproperty:overlay-range (point) 'face hywiki-word-face)))
-    (let ((wikiword (buffer-substring-no-properties (car hywiki--range) (cdr 
hywiki--range))))
-      (if (string-match hywiki-word-with-optional-suffix-exact-regexp wikiword)
+            (setq hywiki--range (hywiki-word-at :range))
+            (car hywiki--range))
+    (cl-destructuring-bind (wikiword start end)
+       hywiki--range
+      (if (and (hproperty:but-get start 'face hywiki-word-face)
+              (string-match hywiki-word-with-optional-suffix-exact-regexp 
wikiword))
          (if range-flag
-             (list wikiword (car hywiki--range) (cdr hywiki--range))
+             (list wikiword start end)
            wikiword)
        (when range-flag
          '(nil nil nil))))))
@@ -2439,12 +2442,7 @@ whenever `hywiki-mode' is enabled/disabled."
   ;; Highlight HyWikiWords throughout buffers where `hywiki-mode' is enabled
   ;; or HyWiki pages below `hywiki-directory' whenever displayed in a window.
   (if (hywiki-active-in-current-buffer-p)
-      (unless (and (or (and (null region-start) (null region-end))
-                      (and (markerp region-start) (markerp region-end)
-                           (not (and (marker-position region-start)
-                                     (marker-position region-end)))))
-                  (eq hywiki-buffer-highlighted-state 'h)
-                  (not (hywiki-directory-modified-p)))
+      (progn
        (unless skip-lookups-update-flag
          ;; Rebuild lookup tables if any HyWiki page name has changed
          (hywiki-get-referent-hasht))
@@ -2459,7 +2457,7 @@ whenever `hywiki-mode' is enabled/disabled."
                (cond ((and (markerp region-start) (markerp region-end))
                       (when (and (marker-position region-start)
                                  (marker-position region-end))
-                          (narrow-to-region region-start region-end)))
+                        (narrow-to-region region-start region-end)))
                      ((and region-start region-end)
                       (narrow-to-region region-start region-end)))
                ;; Enable dehighlighting in HyWiki pages only when
@@ -2468,21 +2466,23 @@ whenever `hywiki-mode' is enabled/disabled."
                (unless (and region-start region-end)
                  (let ((hywiki-mode))
                    (hywiki-maybe-dehighlight-references)))
-               (dolist (hywiki-words-regexp hywiki--any-wikiword-regexp-list)
-                 (goto-char (point-min))
-                 (let ((highlight-in-comments-and-strings-only
-                        (and (derived-mode-p 'prog-mode)
-                             (not (apply #'derived-mode-p 
hywiki-highlight-all-in-prog-modes)))))
+               (let ((highlight-in-comments-and-strings-only
+                      (and (derived-mode-p 'prog-mode)
+                           (not (apply #'derived-mode-p 
hywiki-highlight-all-in-prog-modes))))
+                     hywiki--start
+                     hywiki--end)
+                 (dolist (hywiki-words-regexp hywiki--any-wikiword-regexp-list)
+                   (goto-char (point-min))
                    (while (re-search-forward hywiki-words-regexp nil t)
-                     (when (save-match-data
-                             (if highlight-in-comments-and-strings-only
-                                 ;; Non-nil if match is inside a comment or a 
string
-                                 (or (nth 4 (syntax-ppss)) (hypb:in-string-p))
-                               t))
-                       (setq hywiki--start (match-beginning 1)
-                             hywiki--end   (match-end 1))
-                       (save-excursion
-                         (goto-char hywiki--start)
+                     (setq hywiki--start (match-beginning 1)
+                           hywiki--end   (match-end 1))
+                     (save-excursion
+                       (goto-char hywiki--start)
+                       (when (save-match-data
+                               (if highlight-in-comments-and-strings-only
+                                   ;; Non-nil if match is inside a comment or 
a string
+                                   (or (nth 4 (syntax-ppss)) 
(hypb:in-string-p))
+                                 t))
                          ;; Otherwise, highlight any HyWikiWord found, 
including
                          ;; any #section:Lnum:Cnum.
                          (when (hywiki-maybe-at-wikiword-beginning)
@@ -2556,13 +2556,15 @@ DIRECTION-NUMBER is 1 for forward scanning and -1 for 
backward scanning."
    #'hywiki-maybe-highlight-references direction-number))
 
 (defun hywiki-maybe-highlight-wikiwords-in-frame (frame &optional 
skip-lookups-update-flag)
-  "Highlight all non-Org link HyWiki page names displayed in FRAME.
+  "Highlight all non-Org link HyWiki references displayed in FRAME.
+Do not highlight references to the current page unless they have
+sections attached.
+
 If FRAME is t, then highlight in all windows across all frames, even
 invisible ones.  With optional SKIP-LOOKUPS-UPDATE-FLAG non-nil, HyWiki
 lookup tables should have already been updated and this is skipped.
 
-Use `hywiki-word-face' to highlight.  Do not highlight references to
-the current page unless they have sections attached."
+Use `hywiki-word-face' to highlight."
   (walk-windows
    (lambda (window)
      (with-selected-window window
@@ -2588,30 +2590,31 @@ Note that HyWiki references can occur in non-HyWiki 
page buffers."
   (file-name-sans-extension (file-name-nondirectory
                             (or (hypb:buffer-file-name) (buffer-name)))))
 
-(defun hywiki-get-buffers-in-windows (&rest frames)
-  "Return the set of buffers in all windows where `hywiki-mode' is active.
-This applies to all windows in all live frames or can be filtered to optional
-rest of arguments FRAMES."
-  (apply #'set:create
-        (apply #'nconc (mapcar (lambda (frame) (mapcar #'window-buffer
-                                                       (window-list frame)))
-                               (or frames (frame-list))))))
-
 (defun hywiki-get-buffers (hywiki-mode-status)
-  "Return the list of HyWiki buffers displayed in any non-minibuffer window.
-A HyWiki buffer is one where HyWikiWord references are highlighted
-when 'hywiki-mode' is enabled.
+  "Return the set of HYWIKI-MODE-STATUS buffers in any non-minibuffer window.
+This goes across all live frames.
 
-See the function documentation for `hywiki-mode' for valid input
+See the function documentation for `hywiki-mode' for valid HYWIKI-MODE-STATUS
 values (the states of `hywiki-mode')."
-  (when hywiki-mode
-    (delq nil (mapcar (lambda (buf)
-                       (with-current-buffer buf
-                         (and (if (eq hywiki-mode-status :pages)
-                                  (hywiki-in-page-p)
-                                (hywiki-potential-buffer-p))
-                              buf)))
-                     (hywiki-get-buffers-in-windows)))))
+  (when hywiki-mode-status
+    (let ((hywiki-buf-predicate
+          (if (eq hywiki-mode-status :pages)
+              #'hywiki-in-page-p
+            #'hywiki-potential-buffer-p)))
+      (delq nil (mapcar (lambda (buf)
+                         (with-current-buffer buf
+                           (when (funcall hywiki-buf-predicate)
+                             buf)))
+                       (hywiki-get-buffers-in-windows))))))
+
+(defun hywiki-get-buffers-in-windows (&rest frames)
+  "Return the set of HyWiki buffers in all windows across all live frames.
+Or include only those in optional rest of arguments FRAMES."
+  (apply #'set:create
+        (apply #'nconc (mapcar (lambda (frame)
+                                 (mapcar #'window-buffer
+                                         (window-list frame)))
+                               (or frames (frame-list))))))
 
 (defun hywiki-get-page-file (file-stem-name)
   "Return possibly non-existent path in `hywiki-directory' from FILE-STEM-NAME.
@@ -3132,7 +3135,7 @@ at point must return non-nil or this function will return 
nil."
     (when (stringp word)
       (setq word (hywiki-strip-org-link word)))
     (if (or (stringp word)
-           (setq word (hywiki-highlight-word-get-range)))
+           (setq word (hywiki-word-get-range)))
        (unless (hywiki-get-referent (if (stringp word) word (nth 0 word)))
          (setq word nil))
       (setq word nil))
@@ -3239,7 +3242,9 @@ end-position); the positions include the entire
 HyWikiWord#section:Lnum:Cnum string but exclude any delimiters.
 
 This does not test whether a referent exists for the HyWikiWord; call
-`hywiki-referent-exists-p' without an argument for that.
+`hywiki-referent-exists-p' without an argument for that.  Nor does it
+test whether the HyWikiWord reference is within a valid context; call
+`hywiki-non-hook-context-p' for that.
 
 A call to `hywiki-active-in-current-buffer-p' at point must return
 non-nil or this will return nil."
@@ -3449,9 +3454,10 @@ Hyperbole button names."
   (unless (hproperty:but-face-p start hywiki-ignore-face-list)
     (hproperty:but-add start end hywiki-word-face)))
 
-(defun hywiki-highlight-word-get-range ()
+(defun hywiki-word-get-range ()
   "Return list of (HyWikiWord#section:Lnum:Cnum start end) around point.
-Also highlight HyWikiWord as necessary.
+Calls to `hywiki-active-in-current-buffer-p' and `hywiki-non-hook-context-p'
+must return non-nil or this will return \\='(nil nil nil).
 
 If the HyWikiWord reference is delimited, point must be within the
 delimiters.  The delimiters are excluded from start and end.  If not
@@ -3462,14 +3468,30 @@ or not.  Call `hywiki-highlighted-word-at' to test for 
a highlighted
 HyWikiWord at point.
 
 This does not test whether a referent exists for the HyWikiWord; call
-`hywiki-referent-exists-p' without an argument for that.
+`hywiki-referent-exists-p' without an argument for that."
+  (if (hywiki-non-hook-context-p)
+      '(nil nil nil)
+    (hywiki-word-at :range)))
+
+(defun hywiki-highlight-word-get-range ()
+  "Return list of (HyWikiWord#section:Lnum:Cnum start end) around point.
+Also highlight HyWikiWord as necessary.
 
 A call to `hywiki-active-in-current-buffer-p' at point must return
-non-nil or this will return \\='(nil nil nil)."
+non-nil or this will return \\='(nil nil nil).
+
+If the HyWikiWord reference is delimited, point must be within the
+delimiters.  The delimiters are excluded from start and end.  If not
+at a HyWikiWord, return \\='(nil nil nil).
+
+This works regardless of whether the HyWikiWord has been highlighted
+or not.  Call `hywiki-highlighted-word-at' to test for a highlighted
+HyWikiWord at point.
+
+This does not test whether a referent exists for the HyWikiWord; call
+`hywiki-referent-exists-p' without an argument for that."
   (cl-destructuring-bind (wikiword start end)
-      (if (hywiki-non-hook-context-p)
-         '(nil nil nil)
-       (hywiki-word-at :range))
+      (hywiki-word-get-range)
     ;; Ensure wikiword in buffer is highlighted before
     ;; returning its non-highlighted string version.
     (when (and wikiword start end
@@ -3630,8 +3652,17 @@ occurs with one of these hooks, the problematic hook is 
removed."
                hywiki-from-mode hywiki-to-mode))))
 
 (defun hywiki-word-highlight-in-frame (frame)
-  "Auto-highlight HyWikiWords in `hywiki-mode' buffers displayed FRAME."
-  (hywiki-word-highlight-in-buffers (hywiki-get-buffers-in-windows frame)))
+  "Auto-highlight HyWikiWords in `hywiki-mode' buffers displayed in FRAME."
+  (when hywiki-mode
+    (let ((hywiki-buf-predicate
+          (if (eq hywiki-mode :pages)
+              #'hywiki-in-page-p
+            #'hywiki-potential-buffer-p)))
+      (hywiki-word-highlight-in-buffers
+       (seq-filter hywiki-buf-predicate (hywiki-get-buffers-in-windows 
frame))))))
+
+(defun hywiki-word-highlight-in-current-buffer ()
+  (hywiki-word-highlight-in-buffers (list (current-buffer))))
 
 (defun hywiki-word-highlight-in-buffers (buffers)
   "Auto-highlight HyWikiWords in BUFFERS."
@@ -3649,8 +3680,9 @@ occurs with one of these hooks, the problematic hook is 
removed."
   (hywiki-maybe-directory-updated))
 
 (defun hywiki-word-highlight-buffers (buffers)
-  "Setup to auto-highlight HyWikiWords in BUFFERS."
+  "Setup HyWikiWord auto-highlighting and highlight in BUFFERS."
   (interactive)
+  (add-hook 'after-change-major-mode-hook 
'hywiki-word-highlight-in-current-buffer)
   (add-hook 'window-buffer-change-functions 'hywiki-word-highlight-in-frame)
   (add-to-list 'yank-handled-properties
               '(hywiki-word-face . hywiki-highlight-on-yank))
@@ -3658,13 +3690,9 @@ occurs with one of these hooks, the problematic hook is 
removed."
   (when (called-interactively-p 'interactive)
     (message "HyWikiWord auto-highlighting enabled")))
 
-(defun hywiki-word-dehighlight-buffers (buffers)
-  "Disable auto-highlighting of HyWikiWords in BUFFERS."
+(defun hywiki-word-dehighlight-in-buffers (buffers)
+  "Dehighlight HyWikiWords in BUFFERS."
   (interactive)
-  (remove-hook 'window-buffer-change-functions 'hywiki-word-highlight-in-frame)
-  (setq yank-handled-properties
-       (delete '(hywiki-word-face . hywiki-highlight-on-yank)
-               yank-handled-properties))
   (dolist (buf buffers)
     (with-current-buffer buf
       (remove-hook 'pre-command-hook      'hywiki-word-store-around-point 
:local)
@@ -3674,7 +3702,17 @@ occurs with one of these hooks, the problematic hook is 
removed."
       ;; long-running font-locking
       (sit-for 0)
       (hywiki-maybe-dehighlight-references)))
-  (hywiki-maybe-directory-updated)
+  (hywiki-maybe-directory-updated))
+
+(defun hywiki-word-dehighlight-buffers (buffers)
+  "Disable HyWikiWord auto-highlighting and dehighlight in BUFFERS."
+  (interactive)
+  (remove-hook 'after-change-major-mode-hook 
'hywiki-word-highlight-in-current-buffer)
+  (remove-hook 'window-buffer-change-functions 'hywiki-word-highlight-in-frame)
+  (setq yank-handled-properties
+       (delete '(hywiki-word-face . hywiki-highlight-on-yank)
+               yank-handled-properties))
+  (hywiki-word-dehighlight-in-buffers buffers)
   (when (called-interactively-p 'interactive)
     (message "HyWikiWord auto-highlighting disabled")))
 
@@ -3918,7 +3956,7 @@ the HyWikiWord reference."
                                    (buffer-substring hywiki--buttonize-start
                                                      hywiki--buttonize-end))
                                  (when (and (setq hywiki--range
-                                                  
(hywiki-highlight-word-get-range))
+                                                  (hywiki-word-get-range))
                                             (nth 1 hywiki--range))
                                    (prog1 (nth 1 hywiki--range)
                                      (setq hywiki--range nil)))
diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el
index e251e38718..45fb3012de 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:     31-Jan-26 at 17:35:30 by Bob Weiner
+;; Last-Mod:      2-Feb-26 at 00:32:30 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -772,23 +772,24 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest hywiki-tests--face-property-for-wikiword-with-wikipage ()
   "Verify WikiWord for a wiki page gets face property hywiki-word-face."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
-    (let* ((hsys-org-enable-smart-keys t))
-      (hywiki-tests--insert "WikiWor")
+    (let* ((hsys-org-enable-smart-keys t)
+          str)
+      (hywiki-tests--insert (setq str "WikiWor"))
       (hywiki-tests--command-execute #'self-insert-command 1 ?d)
       (goto-char 4)
-      (should (hywiki-word-face-at-p))
+      (ert-info ((format "str = \"%s\"" str))
+       (should (hywiki-word-face-at-p)))
 
       (erase-buffer)
-      (hywiki-tests--insert "WikiWord")
+      (hywiki-tests--insert (setq str "WikiWord"))
       (hywiki-tests--command-execute #'newline 1 'interactive)
       (goto-char 4)
-      (should (hywiki-word-face-at-p)))))
+      (ert-info ((format "str = \"%s\"" str))
+       (should (hywiki-word-face-at-p))))))
 
 (ert-deftest hywiki-tests--no-face-property-for-no-wikipage ()
   "Verify WikiWord for no wiki page does not get face property 
hywiki-word-face."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (let* ((hsys-org-enable-smart-keys t)
            (hywiki-directory (make-temp-file "hywiki" t)))
@@ -804,7 +805,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest hywiki-tests--verify-face-property-when-editing-wikiword ()
   "Verify face property changes when WikiWord is edited."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (hywiki-tests--insert "Wikiord ")
     (goto-char 5)
@@ -822,7 +822,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest 
hywiki-tests--verify-face-property-when-editing-wikiword-first-char ()
   "Verify face property changes when WikiWord is edited in the first char 
position."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (hywiki-tests--insert "WikiWord")
     (hywiki-tests--command-execute #'self-insert-command 1 ? )
@@ -841,7 +840,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest hywiki-tests--references-to-org-link ()
   "Verify `hywiki-references-to-org-links' converts WikiWords to org links."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (let* ((hywiki-directory (make-temp-file "hywiki" t))
            (wikipage (cdr (hywiki-add-page "WikiWord"))))
@@ -1115,12 +1113,13 @@ WikiWord#Csection-subsection
            (should (file-exists-p wikiword))
 
             (dolist (v `(("WikiWord WikiWord" . ,(format "%s %s" href href))
-                        ("\"WikiWord WikiWord\"" . ,(format "\"%s%s\"" href 
href))
+                        ("\"WikiWord WikiWord\"" . ,(format "\"%s %s\"" href 
href))
                         ;;                                       ^ Missing a 
space!?
                         ("WikiWord Text WikiWord" . ,(format "%s Text %s" href 
href))
-                        ("\"WikiWord Text WikiWord\"" . ,(format "\"%s%s\"" 
href href))
+                        ("\"WikiWord Text WikiWord\"" . ,(format "\"%s Text 
%s\"" href href))
                         ;;                                            ^ 
Missing " Text "
                         ("WikiWord WikiWord WikiWord" . ,(format "%s %s %s" 
href href href))
+                        ;; !! TODO FIXME
                         ;; (cons "\"WikiWord WikiWord WikiWord\"" (format 
"\"%s %s %s\"" href href href))
                         ;; ^ Crashes due to (wrong-type-argument 
integer-or-marker-p nil) caused by buffer-substring-no-properties(nil nil)
                         ))
@@ -1136,10 +1135,13 @@ WikiWord#Csection-subsection
                (hywiki-publish-to-html t)
 
                ;; Verify Export
-               (ert-info ((format "Publish '%s' => Expect '%s'" input 
regex-output))
+               (ert-info ((format "Publish '%s' => Expect '%s'"
+                                  input
+                                  regex-output))
                  (find-file wikipage-html t)
                   (revert-buffer t t)
-                  (should (= 1 (count-matches regex-output (point-min) 
(point-max))))))))
+                  (should (= 1 (count-matches (regexp-quote regex-output)
+                                             (point-min) (point-max))))))))
        ;; Unwind
        (hy-delete-files-and-buffers (list wikipage wikiword wikipage-html 
wikiword-html
                                           (expand-file-name "index.org" 
hywiki-directory)
@@ -1637,22 +1639,38 @@ comparison with expected overlays stable."
   "Non-nil to perform face validation of WikiWord.")
 
 (defun hywiki-tests--word-at ()
-  "Choose what test to perform based on value of 
`hywiki-tests--with-face-test'."
-  (if hywiki-tests--with-face-test
-      (hywiki-highlighted-word-at)
-    (hywiki-word-at)))
+  "Test if there is a HyWikiWord reference at point with a referent.
+Choose what test to perform based on value of `hywiki-tests--with-face-test'."
+  (when (hywiki-referent-exists-p)
+    (if hywiki-tests--with-face-test
+       (hywiki-highlighted-word-at)
+      (hywiki-word-at))))
 
 (defun hywiki-tests--verify-hywiki-word (expected)
   "Verify that `hywiki-word-at' returns t if a wikiword is EXPECTED.
 If EXPECTED is a string, also verify that the wikiword matches the
 string."
-  (if (not expected)
-      (should-not (hywiki-tests--word-at))
-    (let ((hywiki-word-found (hywiki-tests--word-at)))
-      (if (stringp expected)
-          (should (string= expected hywiki-word-found))
-        (should hywiki-word-found))
-      (should (hywiki-word-is-p hywiki-word-found)))))
+  (ert-info ((format (concat "buffer name = \"%s\"\n"
+                            "major-mode = %s\n"
+                            "(hywiki-active-in-current-buffer-p) = %s\n"
+                            "pre-command-hook = %s\n"
+                            "buffer contents = \"%s\"\n"
+                            "hywiki-tests--with-face-test = %s\n"
+                            "expected = \"%s\"")
+                    (buffer-name)
+                    major-mode
+                    (hywiki-active-in-current-buffer-p)
+                    pre-command-hook
+                    (buffer-substring-no-properties (point-min) (point-max))
+                    hywiki-tests--with-face-test
+                    expected))
+    (if (not expected)
+       (should-not (hywiki-tests--word-at))
+      (let ((hywiki-reference (hywiki-tests--word-at)))
+       (if (stringp expected)
+            (should (string= expected hywiki-reference))
+          (should hywiki-reference))
+       (should (hywiki-word-is-p hywiki-reference))))))
 
 (defun hywiki-tests--run-test-case (test-case)
   "Run the TEST-CASE from point.
@@ -1665,37 +1683,33 @@ or non-nil for a wikiword.  The state is checked after 
all chars
 of the string are inserted.  If equal to a string it is checked
 for match with the wikiword.  Movement of point is relative to
 point when the function is called."
-  (erase-buffer)
   (let ((origin (point)))
-
-    ;; For traceability when looking through the list of should
-    ;; clauses in a failing test.
-    (should (listp test-case))
-
-    (dolist (steps test-case)
-      (let ((step (car steps))
-            (vfy (cdr steps)))
-        (cond ((stringp step)
-               (dolist (ch (string-to-list step))
-                 (hywiki-tests--command-execute #'self-insert-command 1 ch))
-               (save-excursion
-                 (goto-char (1- (point)))
-                 (hywiki-tests--verify-hywiki-word vfy)))
-              ((integerp step)
-               (let ((forward (> step 0)))
-                 (dotimes (_ (abs step))
-                   (if forward
-                       (hywiki-tests--command-execute #'delete-forward-char 1)
-                     (hywiki-tests--command-execute #'backward-delete-char 1)))
-                 (hywiki-tests--verify-hywiki-word vfy)))
-              ((and (symbolp step) (string-prefix-p "p" (symbol-name step)))
-               (let* ((pos (string-to-number (substring (symbol-name step) 1)))
-                      (newpos (+ origin (1- pos))))
-                 (when (or (> 0 newpos) (< (point-max) newpos))
-                   (ert-fail (format "New point: '%s' is outside of buffer" 
newpos)))
-                 (goto-char newpos))
-               (hywiki-tests--verify-hywiki-word vfy))
-              (t (ert-fail (format "Unknown step: '%s' in WikiWord 
verification" step))))))))
+    (ert-info ((format "Test case => '%s'" test-case))
+      (dolist (steps test-case)
+       (let ((step (car steps))
+              (vfy (cdr steps)))
+          (cond ((stringp step)
+                (dolist (ch (string-to-list step))
+                   (hywiki-tests--command-execute #'self-insert-command 1 ch))
+                (save-excursion
+                   (goto-char (1- (point)))
+                   (hywiki-tests--verify-hywiki-word vfy)))
+               ((integerp step)
+                (let ((forward (> step 0)))
+                   (dotimes (_ (abs step))
+                     (if forward
+                        (hywiki-tests--command-execute #'delete-forward-char 1)
+                       (hywiki-tests--command-execute #'backward-delete-char 
1)))
+                   (hywiki-tests--verify-hywiki-word vfy)))
+               ((and (symbolp step) (string-prefix-p "p" (symbol-name step)))
+                (let* ((pos (string-to-number (substring (symbol-name step) 
1)))
+                       (newpos (max (min (+ origin (1- pos)) (point-max))
+                                    (point-min))))
+                   (when (or (> (point-min) newpos) (< (point-max) newpos))
+                     (ert-fail (format "New point: '%s' is outside of buffer" 
newpos)))
+                   (goto-char newpos))
+                (hywiki-tests--verify-hywiki-word vfy))
+               (t (ert-fail (format "Unknown step: '%s' in WikiWord 
verification" step)))))))))
 
 (defconst hywiki-tests--wikiword-step-check
   '(
@@ -1728,15 +1742,22 @@ point when the function is called."
 Performs each operation from the step check and verifies if the
 resulting state at point is a WikiWord or not."
   (hywiki-tests--preserve-hywiki-mode
-    (let ((hywiki-tests--with-face-test nil))
-      (dolist (testcase hywiki-tests--wikiword-step-check)
-        (hywiki-tests--run-test-case testcase)))))
+    (let* ((wikiHiHo (cdr (hywiki-add-page "HiHo")))
+           (wikiHiho (cdr (hywiki-add-page "Hiho")))
+           (wikiHi (cdr (hywiki-add-page "Hi")))
+           (wikiHo (cdr (hywiki-add-page "Ho")))
+           (wiki-page-list (list wikiHiHo wikiHiho wikiHi wikiHo))
+           (hywiki-tests--with-face-test nil))
+      (unwind-protect
+          (dolist (testcase hywiki-tests--wikiword-step-check)
+           (erase-buffer)
+            (hywiki-tests--run-test-case testcase))
+        (hy-delete-files-and-buffers wiki-page-list)))))
 
 (ert-deftest hywiki-tests--wikiword-step-check-verification-with-faces ()
   "Run the step check to verify WikiWord is identified under change.
 Perform each operation from the step check and verify whether there
 is a WikiWord at point or not."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (let* ((wikiHiHo (cdr (hywiki-add-page "HiHo")))
            (wikiHiho (cdr (hywiki-add-page "Hiho")))
@@ -1758,29 +1779,36 @@ aliquet diam euismod turpis ultricies, et porta sem 
blandit. Sed vitae."
   "Run the step check to verify WikiWord is identified under change.
 Insert test in the middle of other text."
   (hywiki-tests--preserve-hywiki-mode
-    (let ((hywiki-tests--with-face-test nil))
-      (hywiki-tests--insert hywiki-tests--lorem-ipsum)
-      (goto-char (/ (point-max) 2))
-      (let ((pos (point)))
-        (hywiki-tests--insert " HiHo ")
-        (goto-char (1+ pos))
-        (should (looking-at-p "HiHo ")))
-      (hywiki-tests--run-test-case
-       '((p3 . t)
-         (" " . "Hi")
-         (p1 . t) (p4 . t) (-1 . t)))
+    (let* ((wikiHiHo (cdr (hywiki-add-page "HiHo")))
+           (wikiHiho (cdr (hywiki-add-page "Hiho")))
+           (wikiHi (cdr (hywiki-add-page "Hi")))
+           (wikiHo (cdr (hywiki-add-page "Ho")))
+           (wiki-page-list (list wikiHiHo wikiHiho wikiHi wikiHo)))
+      (unwind-protect
+          (progn
+            (insert hywiki-tests--lorem-ipsum)
+            (goto-char (/ (point-max) 2))
+            (let ((pos (point)))
+              (hywiki-tests--insert " HiHo ")
+              (goto-char (1+ pos))
+              (should (looking-at-p "HiHo ")))
+            (hywiki-tests--run-test-case
+             '((p3 . t)
+               (" " . "Hi")
+               (p1 . t) (p4 . t) (-1 . t)))
 
-      (erase-buffer)
-      (hywiki-tests--insert hywiki-tests--lorem-ipsum)
-      (goto-char (/ (point-max) 2))
-      (let ((pos (point)))
-        (hywiki-tests--insert " Hiho ")
-        (goto-char (1+ pos))
-        (should (looking-at-p "Hiho ")))
-      (hywiki-tests--run-test-case
-       '((p3 . t)
-         (" " . "Hi")
-         (p1 . t) (p4) (-1 . "Hiho"))))))
+            (erase-buffer)
+            (insert hywiki-tests--lorem-ipsum)
+            (goto-char (/ (point-max) 2))
+            (let ((pos (point)))
+              (hywiki-tests--insert " Hiho ")
+              (goto-char (1+ pos))
+              (should (looking-at-p "Hiho ")))
+            (hywiki-tests--run-test-case
+             '((p3 . t)
+               (" " . "Hi")
+               (p1 . t) (p4) (-1 . "Hiho"))))
+        (hy-delete-files-and-buffers wiki-page-list)))))
 
 (ert-deftest 
hywiki-tests--wikiword-step-check-edit-wikiword-in-emacs-lisp-mode ()
   "Run the step check to verify WikiWord is identified under change in a 
docstring.
@@ -1790,7 +1818,7 @@ face is verified during the change."
     (emacs-lisp-mode)
     (hywiki-tests--insert "\
 (defun func ()
-  \"WikiWor)
+  \"WikiWor\"
 ")
     ;; Set point after WikiWor
     (goto-char 1)
@@ -1803,37 +1831,43 @@ face is verified during the change."
 (ert-deftest hywiki-tests--wikiword-identified-in-emacs-lisp-mode ()
   "Verify WikiWord is identified when surrounded by delimiters in 
`emacs-lisp-mode'."
   (hywiki-tests--preserve-hywiki-mode
-    (let* ((hsys-org-enable-smart-keys t))
+    (emacs-lisp-mode)
+    (let* ((hsys-org-enable-smart-keys t)
+          str)
       ;; Matches a WikiWord
-      (dolist (v '("WikiWord" "[WikiWord]" "[[WikiWord]]" "{WikiWord}" 
"(WikiWord)"
-                   "<WikiWord>" "<<WikiWord>>" "{[[WikiWord]]}" 
"([[WikiWord]])"
-                   "[WikiWord AnotherWord]"
+      (dolist (v '("WikiWord" "[WikiWord]" "{WikiWord}" "(WikiWord)"
+                   "<WikiWord>" "[WikiWord AnotherWord]"
                    ))
-        (emacs-lisp-mode)
-        (hywiki-tests--insert (format ";; %s" v))
+       (erase-buffer)
+        (hywiki-tests--insert (setq str (format ";; %s" v)))
         (hywiki-tests--command-execute #'newline 1 'interactive)
         (goto-char 9)
-        (should (string= "WikiWord" (hywiki-tests--word-at)))
+       (ert-info ((format "str = \"%s\"" str))
+          (should (string= "WikiWord" (hywiki-tests--word-at))))
 
        (erase-buffer)
-        (hywiki-tests--insert (format  "(setq var \"%s\")" v))
+        (hywiki-tests--insert (setq str (format "(setq var \"%s\")" v)))
         (hywiki-tests--command-execute #'newline 1 'interactive)
         (goto-char 16)
-        (should (string= "WikiWord" (hywiki-tests--word-at))))
+       (ert-info ((format "str = \"%s\"" str))
+          (should (string= "WikiWord" (hywiki-tests--word-at)))))
 
-      ;; Does not match as a WikiWord
-      (dolist (v '("WikiWord#"))
+      ;; Does not highlight as a WikiWord
+      (dolist (v '("WikiWord#" "[[WikiWord]]" "<<WikiWord>>"
+                  "{[[WikiWord]]}" "([[WikiWord]])"))
        (erase-buffer)
-        (hywiki-tests--insert (format ";; %s" v))
+        (hywiki-tests--insert (setq str (format ";; %s" v)))
         (hywiki-tests--command-execute #'newline 1 'interactive)
         (goto-char 9)
-        (should-not (hywiki-tests--word-at))
+       (ert-info ((format "str = \"%s\"" str))
+          (should-not (hywiki-tests--word-at)))
 
        (erase-buffer)
-        (hywiki-tests--insert (format  "(setq var \"%s\")" v))
+        (hywiki-tests--insert (setq str (format  "(setq var \"%s\")" v)))
         (hywiki-tests--command-execute #'newline 1 'interactive)
         (goto-char 16)
-        (should-not (hywiki-tests--word-at))))))
+       (ert-info ((format "str = \"%s\"" str))
+          (should-not (hywiki-tests--word-at)))))))
 
 (ert-deftest hywiki-tests--wikiword-identified-in-strings-in-emacs-lisp-mode ()
   "Verify WikiWord is identified when in strings in `emacs-lisp-mode'."
@@ -1897,6 +1931,7 @@ face is verified during the change."
       (dolist (testcase
                '((("\"Hi#a b c\"") (p3 . "Hi#a b c") (p11) (-1) (p3 . "Hi#a") 
(p10) ("\"") (p3 . "Hi#a b c"))
                  (("(Hi#s n)" . "Hi#s n") (-1) (p3 . "Hi#s") (p8) (")" . "Hi#s 
n"))))
+       (erase-buffer)
         (hywiki-tests--run-test-case testcase)))))
 
 (ert-deftest hywiki-tests--wikiword-yanked-with-extra-words ()


Reply via email to