branch: externals/hyperbole
commit 430afa38fd7d14c19700b4bc788a227984bea80c
Merge: 540c4a3d74 0619ec03a8
Author: Robert Weiner <r...@gnu.org>
Commit: GitHub <nore...@github.com>

    Merge pull request #718 from rswgnu/rsw
    
    hywiki-add-bookmark - Fix to add bookmark properly @rswgnu
---
 ChangeLog            | 37 ++++++++++++++++++++
 hbut.el              | 18 +++++-----
 hibtypes.el          |  5 +--
 hpath.el             | 26 +++++++-------
 hproperty.el         |  4 +--
 hywiki.el            | 62 ++++++++++++++++++--------------
 test/hywiki-tests.el | 99 +++++++++++++++++++++++++++-------------------------
 7 files changed, 153 insertions(+), 98 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 97b151a159..b465218c40 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,40 @@
+2025-04-27  Bob Weiner  <r...@gnu.org>
+
+* test/hywiki-tests.el (hywiki-tests--save-referent-keyseries-use-menu): 
Rewrite
+    and enable this test.
+
+* hywiki.el (hywiki-referent-exists-p): Fix to stop using global which could be
+    overwritten and cause failures; use let of 'save-input-word' instead.
+
+2025-04-26  Bob Weiner  <r...@gnu.org>
+
+* test/hywiki-tests.el 
(hywiki-tests--verify-face-property-when-editing-wikiword):
+    Rewrite and eliminate false wikiword highlighting.
+
+                       (hywiki-tests--save-referent-bookmark-use-menu): This
+    test was unstable and unneeded (could not read the Hyperbole minibuffer
+    menu prompts properly), so I removed it.  
'hywiki-tests--save-referent-bookmark'
+    is enough of a test.
+
+* hywiki.el (hywiki-word-at): When called with :range flag, ensure any
+    existing HyWikiWord reference is highlighted before returning.  This
+    helps with wikiword highlighting taking precedence over 'font-lock-mode'.
+
+* hywiki.el (hywiki-add-bookmark): Fix to actually create the bookmark
+    and to not prompt for a bookmark name when called non-interactively.
+  test/hywiki-tests.el (hywiki-tests--add-bookmark): Fix
+                       (hywiki-tests--save-referent-bookmark): Fix to
+    use 'wiki-referent' from 'hywiki-tests--referent-test' call.
+                       (hywiki-tests--referent-test): Fix to return buffer
+    string with no properties to simplify comparisons.
+                       (hywiki-add-referent): This may create a new HyWikiWord
+    so highlight any occurrences of it in all windows across all frames.
+
+* hbut.el (ibut:insert-text): Remove duplicate 'actypes::link-to-file' clause.
+
+* hpath.el (hpath:file-position-to-line-and-column): Fix to shorten path
+    in the current directory, not within the buffer of path.
+
 2025-04-26  Mats Lidell  <ma...@gnu.org>
 
 * hibtypes.el (smerge): Add ibut smerge.
diff --git a/hbut.el b/hbut.el
index 349ef7f423..fbe82677d0 100644
--- a/hbut.el
+++ b/hbut.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    18-Sep-91 at 02:57:09
-;; Last-Mod:     20-Apr-25 at 22:19:05 by Mats Lidell
+;; Last-Mod:     26-Apr-25 at 10:12:23 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -2700,13 +2700,6 @@ Summary of operations based on inputs (name arg from 
\\='hbut:current attrs):
       ('actypes::link-to-ebut (progn (insert "<elink:" arg1)
                                     (when arg2 (insert ": " arg2))
                                     (insert ">")))
-      ('actypes::link-to-file
-       ;; arg2 when given is a buffer position
-       (insert "\""
-              (if arg2
-                  (hpath:file-position-to-line-and-column arg1 arg2)
-                (hpath:shorten arg1))
-              "\""))
       ('actypes::link-to-ibut (progn (insert "<ilink:" arg1)
                                     (when arg2 (insert ": " arg2))
                                     (insert ">")))
@@ -2734,6 +2727,15 @@ Summary of operations based on inputs (name arg from 
\\='hbut:current attrs):
        (if (eq arg3 0)
            (format "\"%s:%d\"" (hpath:shorten arg1) arg2)
          (format "\"%s:%d:%d\"" (hpath:shorten arg1) arg2 arg3))))
+      ('actypes::link-to-file
+       ;; arg2 when given is a buffer position
+       (insert "\""
+              (if arg2
+                  ;; includes buffer pos that we translate to line:col
+                  (hpath:file-position-to-line-and-column arg1 arg2)
+                ;; filename only
+                (hpath:shorten arg1))
+              "\""))
       ('actypes::link-to-string-match
        (insert (format "\"%s#%s%s\"" (hpath:shorten arg3) arg1
                       (if (<= arg2 1) "" (concat ":I" (number-to-string 
arg2))))))
diff --git a/hibtypes.el b/hibtypes.el
index 83f8e8e5be..58788c669f 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:     26-Apr-25 at 10:19:22 by Mats Lidell
+;; Last-Mod:     27-Apr-25 at 00:40:38 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -1755,8 +1755,9 @@ If a boolean function or variable, display its value."
               ((looking-at smerge-lower-re)
                (setq op 'smerge-keep-all))))
       (when op
+      (save-excursion
         (ibut:label-set (match-string-no-properties 0) (match-beginning 0) 
(match-end 0))
-        (hact op)))))
+        (hact op))))))
 
 (run-hooks 'hibtypes-end-load-hook)
 (provide 'hibtypes)
diff --git a/hpath.el b/hpath.el
index 89e1bd60a9..3c0b863b94 100644
--- a/hpath.el
+++ b/hpath.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:     1-Nov-91 at 00:44:23
-;; Last-Mod:     13-Apr-25 at 01:06:44 by Bob Weiner
+;; Last-Mod:     26-Apr-25 at 10:22:42 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -1401,17 +1401,19 @@ Parse out the parts and return a list, else nil."
          (list file))))))
 
 (defun hpath:file-position-to-line-and-column (path position)
-  "Return \"path:line-num:col-num\" given PATH and character POSITION.
-The path in the result is abbreviated when possible."
-  (with-current-buffer (find-file-noselect path)
-    (save-excursion
-      (goto-char position)
-      (if (zerop (current-column))
-         (format "%s:L%d" (hpath:shorten path) (line-number-at-pos (point) t))
-       (format "%s:L%d:C%d"
-               (hpath:shorten path)
-               (line-number-at-pos (point) t)
-               (current-column))))))
+  "Return \"path:L<line-num>:C<col-num>\" given PATH and character POSITION.
+If col-num is 0, the :C<col-num> is excluded.  The path in the
+result is abbreviated relative to the caller's buffer directory."
+  (let ((shortened-path (hpath:shorten path)))
+    (with-current-buffer (find-file-noselect path)
+      (save-excursion
+       (goto-char position)
+       (if (zerop (current-column))
+           (format "%s:L%d" shortened-path (line-number-at-pos (point) t))
+         (format "%s:L%d:C%d"
+                 shortened-path
+                 (line-number-at-pos (point) t)
+                 (current-column)))))))
 
 (defun hpath:find-noselect (filename)
   "Find but don't display FILENAME.
diff --git a/hproperty.el b/hproperty.el
index c7fcc9e364..17ccdfb894 100644
--- a/hproperty.el
+++ b/hproperty.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    21-Aug-92
-;; Last-Mod:     27-Feb-25 at 22:50:13 by Bob Weiner
+;; Last-Mod:     26-Apr-25 at 20:25:33 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -258,7 +258,7 @@ matching button."
                  (overlays-in start end)))))
 
 (defun hproperty:but-get-first-in-region (start end property value)
-  "Return first button in current buf between START & END with PROPERTY & 
VALUE.
+  "Return list of first button between START & END with PROPERTY & VALUE.
 Return nil if none."
   (catch 'first
     (mapc (lambda (overlay)
diff --git a/hywiki.el b/hywiki.el
index 8d91089d45..80442df403 100644
--- a/hywiki.el
+++ b/hywiki.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    21-Acpr-24 at 22:41:13
-;; Last-Mod:     20-Apr-25 at 22:27:39 by Mats Lidell
+;; Last-Mod:     27-Apr-25 at 01:21:36 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -916,7 +916,8 @@ an error is triggered."
                      (hywiki-get-referent-hasht)))
     (setq hywiki--any-wikiword-regexp-list nil)
     (unless (hyperb:stack-frame '(hywiki-maybe-highlight-wikiwords-in-frame))
-      (hywiki-cache-save))
+      (hywiki-cache-save)
+      (hywiki-maybe-highlight-wikiwords-in-frame t))
     (run-hooks 'hywiki-add-referent-hook)
     referent))
 
@@ -977,7 +978,7 @@ calling this function."
   (activities-resume activity :resetp nil))
 
 (defun hywiki-add-bookmark (wikiword)
-  "Make WIKIWORD display a bookmark and return the action.
+  "Make WIKIWORD display a bookmark at point and return the action.
 
 If WIKIWORD is invalid, trigger a `user-error' if called interactively
 or return nil if not.
@@ -987,13 +988,12 @@ After successfully adding the bookmark, run 
`hywiki-add-referent-hook'.
 Use `hywiki-get-referent' to determine whether WIKIWORD exists prior to
 calling this function."
   (interactive (list (or (hywiki-word-at)
-                        (hywiki-word-read-new "Add/Edit HyWikiWord: "))))
+                        (hywiki-word-read-new "Add/Edit Bookmark HyWikiWord: 
"))))
   (require 'bookmark)
-  (let ((bookmark (bookmark-completing-read "Bookmark: "
-                                           bookmark-current-bookmark)))
-    (if (string-empty-p bookmark)
-       (error "(hywiki-add-bookmark): No bookmark specified")
-      (hywiki-add-referent wikiword (cons 'bookmark bookmark)))))
+  (if (string-empty-p wikiword)
+      (error "(hywiki-add-bookmark): No bookmark specified")
+    (bookmark-set wikiword)
+    (hywiki-add-referent wikiword (cons 'bookmark wikiword))))
 
 (defun hywiki-display-bookmark (_wikiword bookmark)
   (let ((loc (bookmark-location bookmark)))
@@ -2843,23 +2843,23 @@ Word may be of form:
 
 When using the word at point, a call to `hywiki-active-in-current-buffer-p'
 at point must return non-nil or this function will return nil."
-  (setq hywiki--word-only word)
-  (when (stringp word)
-    (setq word (hywiki-strip-org-link word)))
-  (if (or (stringp word)
-         (setq word (hywiki-word-at word)))
-      (unless (hywiki-get-referent (if (stringp word) word (nth 0 word)))
-       (setq word nil))
-    (setq word nil))
-  (when (and (listp word) (= (length word) 3))
-    (setq start (nth 1 word)
-         end   (nth 2 word)
-         ;; `word' must be set last so list version can be referenced
-         ;; first above
-         word  (nth 0 word)))
-  (if (eq hywiki--word-only :range)
-      (list word start end)
-    word))
+  (let ((save-input-word word))
+    (when (stringp word)
+      (setq word (hywiki-strip-org-link word)))
+    (if (or (stringp word)
+           (setq word (hywiki-word-at word)))
+       (unless (hywiki-get-referent (if (stringp word) word (nth 0 word)))
+         (setq word nil))
+      (setq word nil))
+    (when (and (listp word) (= (length word) 3))
+      (setq start (nth 1 word)
+           end   (nth 2 word)
+           ;; `word' must be set last so list version can be referenced
+           ;; first above
+           word  (nth 0 word)))
+    (if (eq save-input-word :range)
+       (list word start end)
+      word)))
 
 (defun hywiki-section-to-headline-reference ()
   "Replace file#section dashes with spaces to match to an Org headline.
@@ -3138,7 +3138,13 @@ or this will return nil."
                         (string-match "#[^][#()<>{}\"\n\r\f]+\\'" wikiword)
                       t))
                (if range-flag
-                   (list wikiword start end)
+                   (progn
+                     ;; Ensure wikiword is highlighted before returning it
+                     (and wikiword start end
+                          (not (hproperty:but-get start 'face 
hywiki-word-face))
+                          (hywiki-referent-exists-p wikiword)
+                          (hproperty:but-add start end hywiki-word-face))
+                     (list wikiword start end))
                  wikiword)
              (when range-flag
                '(nil nil nil))))))
@@ -3177,6 +3183,8 @@ a HyWikiWord at point."
   "Non-nil if but at point or optional POS has `hywiki-word-face' property."
   (unless pos
     (setq pos (point)))
+  ;; Sometimes this can return a left over button/overlay that points
+  ;; to no buffer.  Ignore this case.
   (hproperty:but-get pos 'face hywiki-word-face))
 
 ;;;###autoload
diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el
index e582fc388f..ab452ea9d2 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:     18-Apr-25 at 22:22:11 by Bob Weiner
+;; Last-Mod:     27-Apr-25 at 02:47:39 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -552,23 +552,21 @@ Both mod-time and checksum must be changed for a test to 
return true."
     (let* ((hywiki-directory (make-temp-file "hywiki" t))
            (wikipage (cdr (hywiki-add-page "WikiWord"))))
       (unwind-protect
-          (progn
-            (with-temp-buffer
-              (hywiki-mode 1)
-              (insert "Wikiord")
-              (hywiki-tests--command-execute #'self-insert-command 1 ? )
-              (goto-char 5)
-              (should (looking-at-p "ord "))
-              (should-not (hywiki-word-face-at-p))
-
-              (hywiki-tests--command-execute #'self-insert-command 1 ?W)
-              (goto-char 5)
-              (should (looking-at-p "Word "))
-              (should (hywiki-word-face-at-p))
-
-              (hywiki-tests--command-execute #'delete-char 1)
-              (should (looking-at-p "ord "))
-              (should-not (hywiki-word-face-at-p))))
+          (with-temp-buffer
+            (hywiki-mode 1)
+            (insert "Wikiord ")
+            (goto-char 5)
+            (should (looking-at-p "ord "))
+            (should-not (hywiki-word-face-at-p))
+
+            (hywiki-tests--command-execute #'self-insert-command 1 ?W)
+            (goto-char 5)
+            (should (looking-at-p "Word "))
+            (should (hywiki-word-face-at-p))
+
+            (hywiki-tests--command-execute #'delete-char 1)
+            (should (looking-at-p "ord "))
+            (should-not (hywiki-word-face-at-p)))
         (hy-delete-files-and-buffers (list wikipage))
         (hy-delete-dir-and-buffer hywiki-directory)))))
 
@@ -860,6 +858,7 @@ Note special meaning of `hywiki-allow-plurals-flag'."
 
 (ert-deftest hywiki-tests--add-bookmark ()
   "Verify `hywiki-add-bookmark'."
+  (require 'bookmark)
   (let ((hywiki-directory (make-temp-file "hywiki" t))
         (bookmark-alist nil)
         (file (make-temp-file "hypb.txt")))
@@ -867,11 +866,11 @@ Note special meaning of `hywiki-allow-plurals-flag'."
         (progn
           (find-file file)
           (ert-simulate-keys "\r"
-            (should-error (hywiki-add-bookmark "WikiWord")))
-          (bookmark-set "bookmark")
-          (ert-simulate-keys "bookmark\r"
+            (should-error (hywiki-add-bookmark "")))
+          (ert-simulate-keys "WikiWord\r"
             (hywiki-add-bookmark "WikiWord")
-            (should (equal '(bookmark . "bookmark") (hywiki-get-referent 
"WikiWord")))))
+            (should (equal '(bookmark . "WikiWord")
+                          (hywiki-get-referent "WikiWord")))))
       (hy-delete-file-and-buffer file)
       (hy-delete-dir-and-buffer hywiki-directory))))
 
@@ -1010,13 +1009,15 @@ EXPECTED is the result expected from 
hywiki-get-referent.  PREPARE sets
 up the test."
   (declare (indent 0) (debug t))
   `(let* ((hywiki-directory (make-temp-file "hywiki" t))
-          (wiki-page (cdr (hywiki-add-page "WikiPage" )))
          (wiki-referent "WikiReferent")
-          (mode-require-final-newline nil))
+          (wiki-page (cdr (hywiki-add-page "WikiPage" )))
+          (mode-require-final-newline nil)
+         wiki-page-buffer)
      (unwind-protect
-         (progn
+         (save-excursion
            (should (equal '("WikiPage") (hywiki-get-wikiword-list)))
-           (find-file wiki-page)
+          (setq wiki-page-buffer (find-file wiki-page))
+          (erase-buffer)
            (insert wiki-referent)
            (save-buffer)
            (goto-char 4)
@@ -1024,9 +1025,10 @@ up the test."
            ,@prepare
 
            (should (equal ,expected (hywiki-get-referent wiki-referent)))
-
-           (should (string= wiki-referent (buffer-string)))
            (should (file-exists-p (hywiki-cache-default-file)))
+          (set-buffer wiki-page-buffer)
+           (should (string= wiki-referent (buffer-substring-no-properties
+                                          (point-min) (point-max))))
 
            ;; Simulate reload from cache
            (hywiki-cache-save)
@@ -1045,37 +1047,40 @@ up the test."
    (ert-simulate-keys "ABC\r"
      (hywiki-add-key-series wiki-referent))))
 
-;; FIXME: Not stable. Can sometimes succeed.
 (ert-deftest hywiki-tests--save-referent-keyseries-use-menu ()
   "Verify saving and loading a referent keyseries works using Hyperbole's 
menu."
-  :expected-result :failed
   ; The failure is intermittent. See expanded test case below.
   (skip-unless (not noninteractive))
-  (hywiki-tests--referent-test
-    (cons 'key-series "{ABC}")
-    (should (hact 'kbd-key "C-u C-h hhck{ABC} RET"))
-    (hy-test-helpers:consume-input-events)))
+  `(let* ((hywiki-directory (make-temp-file "hywiki" t))
+          (wiki-page (cdr (hywiki-add-page "WikiPage" )))
+          (mode-require-final-newline nil)
+         wiki-page-buffer)
+     (unwind-protect
+         (save-excursion
+          (setq wiki-page-buffer (find-file wiki-page))
+          (erase-buffer)
+          (insert "WikiWord")
+           (save-buffer)
+           (goto-char 4)
+          (should (hact 'kbd-key "C-u C-h hhck {C-e SPC ABC} RET"))
+          (should (equal (cons 'key-series "C-e SPC {ABC}")
+                         (hywiki-get-referent "WikiWord")))
+          (should (string-equal "Wiki{C-e ABC}Referent"
+                                (buffer-substring-no-properties
+                                 (point-min)
+                                 (point-max)))))
+       (hy-delete-files-and-buffers (list wiki-page 
(hywiki-cache-default-file)))
+       (hy-delete-dir-and-buffer hywiki-directory))))
 
 ;; Bookmark
 (ert-deftest hywiki-tests--save-referent-bookmark ()
   "Verify saving and loading a referent bookmark works."
   (hywiki-tests--referent-test
-   (cons 'bookmark "bmark")
-   (bookmark-set "bmark")
-   (ert-simulate-keys "bmark\r"
+   (cons 'bookmark wiki-referent)
+   (ert-simulate-keys (concat wiki-referent "\r")
      (hywiki-add-bookmark wiki-referent))))
 
-(ert-deftest hywiki-tests--save-referent-bookmark-use-menu ()
-  "Verify saving and loading a referent bookmark works using Hyperbole's menu."
-  (skip-unless (not noninteractive))
-  (hywiki-tests--referent-test
-   (cons 'bookmark "bmark")
-   (bookmark-set "bmark")
-   (should (hact 'kbd-key "C-u C-h hhcb bmark RET"))
-   (hy-test-helpers:consume-input-events)))
-
 ;; Command
-
 (defun hywiki-tests--command (wikiword)
   "Test command."
   (interactive)

Reply via email to