branch: externals/hyperbole
commit 2a2ddb85eee32ad50ecec523c55c6920f4a6910b
Author: bw <r...@gnu.org>
Commit: bw <r...@gnu.org>

    hywiki.el - Simplify per-char hook functions; improve hywiki-word-at
---
 ChangeLog            |  28 +++++
 hargs.el             |   4 +-
 hib-kbd.el           |   7 +-
 hui-select.el        |  28 ++---
 hywiki.el            | 295 ++++++++++++++++++++++++++++++---------------------
 test/hywiki-tests.el |  16 ++-
 6 files changed, 231 insertions(+), 147 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index abeb4d441b..2c0c59facf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,9 +1,37 @@
+2025-02-25  Bob Weiner  <r...@gnu.org>
+
+* hywiki.el (hywiki-word-with-optional-spaces-suffix-exact-regexp): Remove
+    use in 'hywiki-word-is-p' and replace with
+    'hywiki-word-with-optional-suffix-exact-regexp' which actually allows 
spaces.
+            (hywiki-word-at): Add support for finding delimited WikiWords with
+    #sections that contain multiple space separated words and returning the
+    wikiword, start and end.
+            (hywiki-word-at): Fix last 'looking-at' expression to not match to
+    end of buffer.
+            (hywiki-buttonize-character-commands,
+             hywiki-buttonize-non-character-commands,
+             hywiki-debuttonize-non-character-commands): Rewrite and simplify.
+
+2025-02-24  Bob Weiner  <r...@gnu.org>
+
+* hywiki.el (hywiki--maybe-de/highlight-sexp): Ensure return the result of
+    calling 'func'.
+            (hywiki-buttonize-non-character-commands): Remove insert commands
+    handled by 'hywiki-buttonize-character-commands' attached to
+    'post-self-insert-hook'.
+            (hywiki-non-hook-context-p): Add.
+
 2025-02-23  Bob Weiner  <r...@gnu.org>
 
 * hywiki.el (hywiki-referent-menu): Re-add accidentally deleted "Keys" key
     series referent type in the menu.
+            (hywiki-set-directory): Call as part of loading hywiki.el to ensure
+    HyWiki Org project publish settings are initialized.
             (hywiki--org-export-new-title-reference): Replace '--any' call from
     dash.el package with 'cl-some'.
+            (hywiki-buttonize-character-commands): In prog modes, limit to 
comments
+    and strings.  If within a delimited pair, highlight #sections with multiple
+    words.
 
 * test/hywiki-tests.el (hywiki-tests--convert-words-to-org-link): Fix to not
     expect 'hy:' prefix in HyWiki Org links.
diff --git a/hargs.el b/hargs.el
index 4e9cb7067a..7e21020fc2 100644
--- a/hargs.el
+++ b/hargs.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    31-Oct-91 at 23:17:35
-;; Last-Mod:      6-Oct-24 at 22:47:25 by Bob Weiner
+;; Last-Mod:     25-Feb-25 at 02:16:13 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -221,7 +221,7 @@ button key (no spaces)."
        ;; disk drive prefix, in which case the backslash is
        ;; considered part of a pathname.
        (and (if (and (> end (point-min))
-                     (= (char-before end) ?\\)
+                     (= (or (char-before end) 0) ?\\)
                      (not (string-match (concat "\\(\\`[\\][\\]\\)\\|"
                                                 hpath:mswindows-mount-prefix)
                                         (hargs:buffer-substring start end))))
diff --git a/hib-kbd.el b/hib-kbd.el
index 53cb6a2d3d..0d446a0f6a 100644
--- a/hib-kbd.el
+++ b/hib-kbd.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    22-Nov-91 at 01:37:57
-;; Last-Mod:     23-Nov-24 at 21:15:04 by Bob Weiner
+;; Last-Mod:     25-Feb-25 at 02:14:39 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -144,7 +144,10 @@ Any key sequence within the series must be a string of one 
of the following:
                  ;; In Texinfo, allow for @bkbd{} or @kbd{}, so an
                  ;; alpha char preceding
                  (and (derived-mode-p 'texinfo-mode)
-                      (= (char-syntax (char-before start)) ?w)))
+                      (= (if (char-before start)
+                             (char-syntax (char-before start))
+                           0)
+                         ?w)))
          (when (and (stringp key-series) (not (string-empty-p key-series)))
            ;; Replace any ${} internal or env vars; leave
            ;; $VAR untouched for the shell to evaluate.
diff --git a/hui-select.el b/hui-select.el
index e2556b7d90..7eded1718d 100644
--- a/hui-select.el
+++ b/hui-select.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    19-Oct-96 at 02:25:27
-;; Last-Mod:     22-Feb-25 at 22:15:12 by Bob Weiner
+;; Last-Mod:     25-Feb-25 at 02:24:41 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -430,13 +430,13 @@ returned is the function to call to select that syntactic 
unit."
   (unless (smart-eobp)
     (or (numberp pos) (setq pos (point)))
     (setq hui-select-previous 'char)
-    (let* ((syntax (char-syntax (or (char-after pos) (char-before pos))))
+    (let* ((syntax (char-syntax (or (char-after pos) (char-before pos) 0)))
           (pair (assq syntax hui-select-syntax-alist)))
       (and pair (or hui-select-whitespace (not (eq (cdr pair) 
'thing-whitespace)))
           ;; Ignore matches that are preceded by '\' as a quote, e.g. ?\'
           (or (not (char-after pos))
               (= pos (point-min))
-              (and (char-before pos) (/= ?\\ (char-before pos))))
+              (and (char-before pos) (/= ?\\ (or (char-before pos) 0))))
           (cdr pair)))))
 
 ;;;###autoload
@@ -849,7 +849,7 @@ If an error occurs during syntax scanning, return nil."
   (setq hui-select-previous 'char)
   (if (save-excursion (goto-char pos) (eolp))
       (hui-select-line pos)
-    (let* ((syntax (char-syntax (or (char-after pos) (char-before pos))))
+    (let* ((syntax (char-syntax (or (char-after pos) (char-before pos) 0)))
           (pair (assq syntax hui-select-syntax-alist)))
       (cond ((and pair
                  (or hui-select-whitespace
@@ -903,7 +903,7 @@ Use `hui-select-mark-delimited-sexp' to select it."
          (syn-after  (if (char-after)  (char-syntax (char-after)) 0)))
       (or (and (/= syn-before ?\\) (or (= syn-after ?\() (= syn-after ?\))))
          (and (= syn-before ?\)) (char-before (1- (point)))
-              (/= ?\\ (char-syntax (char-before (1- (point))))))))))
+              (/= ?\\ (if (char-before (1- (point))) (char-syntax (char-before 
(1- (point)))) 0)))))))
 
 (defun hui-select-mark-delimited-sexp ()
   "When point is before or after an sexp deactivate the mark and mark the sexp.
@@ -924,7 +924,7 @@ end sexp delimiters, ignore it, and return nil."
               (backward-sexp)
               (funcall mark-sexp-func))
              ((and (not (eolp))
-                   (setq syn-before (char-syntax (char-before)))
+                   (setq syn-before (if (char-before) (char-syntax 
(char-before)) 0))
                    (eq syn-before ?\)))
               (backward-sexp)
               (funcall mark-sexp-func)))))))
@@ -1050,14 +1050,14 @@ string."
   (with-syntax-table hbut:syntax-table
     (or (and (equal start-delim "\"") (equal end-delim "\"")
             (ignore-errors
-              (cond ((and (= (char-after) ?\")
-                          (/= (char-before) ?\\))
+              (cond ((and (= (or (char-after) 0) ?\")
+                          (/= (or (char-before) 0) ?\\))
                      (if (hypb:in-string-p)
                          (hui-select-set-region (1+ (point))
                                                 (scan-sexps (1+ (point)) -1))
                        (hui-select-set-region (point) (scan-sexps (point) 1))))
-                    ((and (= (char-before) ?\")
-                          (/= (char-before (1- (point))) ?\\))
+                    ((and (= (or (char-before) 0) ?\")
+                          (/= (or (char-before (1- (point))) 0) ?\\))
                      (if (hypb:in-string-p)
                          (hui-select-set-region (1- (point)) (scan-sexps (1- 
(point)) 1))
                        (hui-select-set-region (point) (scan-sexps (point) 
-1)))))))
@@ -1338,8 +1338,8 @@ included in the list, hui-select-brace-modes."
 The region includes sexpressions before and after POS"
   (or (hui-select-markup-pair pos)
       (hui-select-delimited-thing-call #'hui-select-thing)
-      (and (or (and (= (char-after) ?\") (/= (char-before) ?\\))
-              (and (= (char-before) ?\") (/= (char-before (1- (point))) ?\\)))
+      (and (or (and (= (or (char-after) 0) ?\") (/= (or (char-before) 0) ?\\))
+              (and (= (or (char-before) 0) ?\") (/= (or (char-before (1- 
(point))) 0) ?\\)))
           (hui-select-string pos))
       (hui-select-comment pos)
       (hui-select-preprocessor-def pos)
@@ -1347,9 +1347,9 @@ The region includes sexpressions before and after POS"
       (save-excursion
        (setq hui-select-previous 'punctuation)
        (goto-char (min (1+ pos) (point-max)))
-       (cond ((and (char-after pos) (= ?\  (char-syntax (char-after pos))))
+       (cond ((and (char-after pos) (= ?\  (if (char-after pos) (char-syntax 
(char-after pos)) 0)))
               (hui-select-set-region pos (1+ pos)))
-             ((and (char-before pos) (= ?\  (char-syntax (char-before pos))))
+             ((and (char-before pos) (= ?\  (if (char-before pos) (char-syntax 
(char-before pos)) 0)))
               (hui-select-set-region (1- pos) pos))
              (t (goto-char pos)
                 (ignore-errors (hui-select-set-region
diff --git a/hywiki.el b/hywiki.el
index c27652406b..c542d31475 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:     23-Feb-25 at 11:50:21 by Bob Weiner
+;; Last-Mod:     25-Feb-25 at 02:39:08 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -234,6 +234,7 @@ Each element is of the form: (wikiword . (referent-type . 
referent-value)).")
 (defvar hywiki--buttonize-start (make-marker)) ;; This must always stay a 
marker
 (defvar hywiki--current-page nil)
 (defvar hywiki--end nil)
+(defvar hywiki--flag nil)
 (defvar hywiki--highlighting-done-flag t)
 (defvar hywiki--page-name nil)
 (defvar hywiki--range nil)
@@ -311,7 +312,7 @@ See `hywiki-org-publishing-directory' for exported pages in 
html format."
 
 (defun hywiki-directory-changed (option set-to-value operation _where)
   "Watch function for variable `hywiki-directory'.
-Function is called with 4 arguments: (SYMBOL SET-TO-VALUE OPERATION WHERE)."
+Function is called with 4 arguments: (OPTION SET-TO-VALUE OPERATION WHERE)."
   (if (memq operation '(let unlet)) ;; not setting global value
       (hywiki-let-directory option set-to-value)
     (hywiki-set-directory option set-to-value)))
@@ -487,19 +488,6 @@ file-based referents (relative to any section given).
 Group 6 is any optional 0-based column number to jump to for any
 file-based referents.")
 
-(defconst hywiki-word-with-optional-spaces-suffix-exact-regexp
-  (concat "\\`" hywiki-word-with-optional-suffix-regexp "\\'")
-  "Exact regexp for a HyWiki word with optional #section, :Lline-num, 
:Ccol-num.
-Section may not contain whitespace or square brackets.  Use '-' to
-substitute for spaces in the section/headline name.
-
-Group 1 is the HyWiki word.
-Group 2 is any optional #section with the # included.
-Group 4 is any optional 1-based line number to jump to for any
-file-based referents (relative to any section given).
-Group 6 is any optional 0-based column number to jump to for any
-file-based referents.")
-
 (defconst hywiki-word-with-optional-suffix-exact-regexp
   (concat "\\`" hywiki-word-regexp "\\(#[^][\n\r\f]+\\)??"
          hywiki-word-line-and-column-numbers-regexp "?\\'")
@@ -555,55 +543,90 @@ Non-nil is the default."
 ;;; hywiki minor mode
 ;;; ************************************************************************
 
+(defun hywiki-non-hook-context-p ()
+  (or (minibuffer-window-active-p (selected-window))
+      (and (boundp 'edebug-active) edebug-active
+          (active-minibuffer-window))
+      (and (derived-mode-p 'prog-mode)
+          (not (apply #'derived-mode-p hywiki-highlight-all-in-prog-modes))
+          ;; Not inside a comment or a string
+          (not (or (nth 4 (syntax-ppss)) (hypb:in-string-p))))))
+
 (defun hywiki-buttonize-character-commands ()
   "Turn any HyWikiWords between point into highlighted Hyperbole buttons.
 Triggered by `post-self-insert-hook' for self-inserting characters.
 Highlight after inserting any non-word character."
-  (unless (or (minibuffer-window-active-p (selected-window))
-             (and (boundp 'edebug-active) edebug-active
-                  (active-minibuffer-window)))
-    (hywiki-maybe-highlight-between-page-names)))
+  (unless (setq hywiki--flag (hywiki-non-hook-context-p))
+    (setq hywiki--range nil)
+    (save-excursion
+      (cond ((marker-position hywiki--buttonize-start)
+            (goto-char (1- hywiki--buttonize-start)))
+           ((setq hywiki--range (hywiki-word-at :range))
+            (cl-destructuring-bind (_ start end)
+                hywiki--range
+              (set-marker hywiki--buttonize-start start)
+              (set-marker hywiki--buttonize-end end))
+            (goto-char (1- hywiki--buttonize-start))))
+      (if (marker-position hywiki--buttonize-start)
+         (hywiki--maybe-de/highlight-sexp #'hywiki-maybe-highlight-page-names 
1)
+       (hywiki-maybe-highlight-between-page-names)))
+    (set-marker hywiki--buttonize-start nil)
+    (set-marker hywiki--buttonize-end nil)))
 
 (defun hywiki-buttonize-non-character-commands ()
   "Highlight any HyWikiWord before or after point as a Hyperbole button.
 Triggered by `post-command-hook' for non-character-commands, including
 deletion commands and those in `hywiki-non-character-commands'."
-  (unless (or (minibuffer-window-active-p (selected-window))
-             (and (boundp 'edebug-active) edebug-active
-                  (active-minibuffer-window))
-             (and (derived-mode-p 'prog-mode)
-                  (not (apply #'derived-mode-p 
hywiki-highlight-all-in-prog-modes))
-                ;; Not inside a comment or a string
-                  (not (or (nth 4 (syntax-ppss)) (hypb:in-string-p)))))
+  (unless (or hywiki--flag (hywiki-non-hook-context-p))
     (when (or (memq this-command hywiki-non-character-commands)
              (and (symbolp this-command)
-                  (string-match-p 
"^\\(org-\\)?\\(delete-\\|kill-\\)\\|\\(-delete\\|-kill\\|insert\\)\\(-\\|$\\)" 
(symbol-name this-command))))
-      (when (and (marker-position hywiki--buttonize-start)
-                (marker-position hywiki--buttonize-end))
-       ;; This means the command just deleted an opening or closing
-       ;; delimiter of a range that now needs any HyWikiWords
-       ;; inside to be re-highlighted.
-       (save-excursion
-         (goto-char hywiki--buttonize-start)
-         (let ((opening-char (char-after))
-               closing-char)
-           (when (memq opening-char '(?\( ?\"))
-             (delete-char 1))
-           (goto-char hywiki--buttonize-end)
-           (setq closing-char (char-before))
-           (when (memq closing-char '(?\) ?\"))
-             (delete-char -1)
-             (insert " "))
-           (goto-char hywiki--buttonize-start)
-           (hywiki-maybe-highlight-between-page-names)
-           (when (memq opening-char '(?\( ?\"))
-             (insert opening-char))
-           (when (memq closing-char '(?\) ?\"))
-             (goto-char (1+ hywiki--buttonize-end))
-             (delete-char -1)
-             (insert closing-char)
-             ))))
-      (hywiki-maybe-highlight-between-page-names))))
+                  (string-match-p 
"^\\(org-\\)?\\(delete-\\|kill-\\)\\|\\(-delete\\|-kill\\)\\(-\\|$\\)" 
(symbol-name this-command))))
+      (setq hywiki--range nil)
+      (save-excursion
+       (cond ((marker-position hywiki--buttonize-start)
+              (goto-char hywiki--buttonize-start))
+             ((setq hywiki--range (hywiki-word-at :range))
+              (cl-destructuring-bind (_ start end)
+                  hywiki--range
+                (set-marker hywiki--buttonize-start start)
+                (set-marker hywiki--buttonize-end end))
+              (goto-char hywiki--buttonize-start)))
+       (if (marker-position hywiki--buttonize-start)
+           (hywiki--maybe-de/highlight-sexp 
#'hywiki-maybe-highlight-page-names 1)
+         (hywiki-maybe-highlight-between-page-names)))
+      (set-marker hywiki--buttonize-start nil)
+      (set-marker hywiki--buttonize-end nil))))
+
+
+      ;; (when (and (marker-position hywiki--buttonize-start)
+      ;;                (marker-position hywiki--buttonize-end))
+
+      ;;       ;; When these markers are set, it means the command just
+      ;;       ;; edited a char within a delimited range that now needs any
+      ;;       ;; HyWikiWords inside to be re-highlighted.
+      ;;       (save-excursion
+      ;;         (goto-char hywiki--buttonize-start)
+      ;;         ;; (message "%s" (point))
+      ;;         (let ((opening-char (char-after))
+      ;;               closing-char)
+      ;;           (when (memq opening-char '(?\( ?\"))
+      ;;             (delete-char 1))
+      ;;           (hywiki-maybe-highlight-between-page-names)
+      ;;           (when (memq opening-char '(?\( ?\"))
+      ;;             (insert-before-markers opening-char))
+
+      ;;           (setq closing-char (char-before))
+      ;;           (when (memq closing-char '(?\) ?\"))
+      ;;             ;; (delete-char -1)
+      ;;             ;; (insert " ")
+      ;;             )
+      ;;           ;; (goto-char hywiki--buttonize-start)
+      ;;           (when (memq closing-char '(?\) ?\"))
+      ;;             ;; (goto-char (+ 2 hywiki--buttonize-end))
+      ;;             ;; (delete-char -1)
+      ;;             ;; (insert-before-markers closing-char)
+      ;;             ))))
+      ;; (hywiki-maybe-highlight-between-page-names))))
 
 (defun hywiki-debuttonize-non-character-commands ()
   "Dehighlight any HyWikiWord before or after point.
@@ -612,24 +635,22 @@ deletion commands and those in 
`hywiki-non-character-commands'."
   (when (and (markerp hywiki--buttonize-start) (markerp hywiki--buttonize-end))
     (set-marker hywiki--buttonize-start nil)
     (set-marker hywiki--buttonize-end nil))
-  (when (and (or (memq this-command hywiki-non-character-commands)
-                (and (symbolp this-command)
-                     (string-match-p 
"\\`\\(org-\\)?\\(delete-\\|kill-\\)\\|-delete-\\|-kill-"
-                                     (symbol-name this-command))))
-            (or (not (derived-mode-p 'prog-mode))
-                (apply #'derived-mode-p hywiki-highlight-all-in-prog-modes)
-                ;; Inside a comment or a string
-                (nth 4 (syntax-ppss))
-                (hypb:in-string-p)))
-    (cl-destructuring-bind (start end)
-       (hywiki-get-delimited-range) ;; includes delimiters
-      ;; Use these to store any range of a delimited HyWikiWord#section
-      (set-marker hywiki--buttonize-start start)
-      (set-marker hywiki--buttonize-end end)
-      ;; Enable dehighlighting in HyWiki pages
-      (unless (and start end)
-       ;; Dehighlight any page name at point
-       (hywiki-maybe-dehighlight-between-page-names)))))
+  (unless (hywiki-non-hook-context-p)
+    (when (or (memq this-command hywiki-non-character-commands)
+             (and (symbolp this-command)
+                  (string-match-p 
"^\\(org-\\)?\\(delete-\\|kill-\\)\\|\\(-delete\\|-kill\\)\\(-\\|$\\)" 
(symbol-name this-command))))
+      (cl-destructuring-bind (start end)
+         ;; Get delimited region only if before or after delimiters,
+         ;; else return (nil nil).
+         (hywiki-get-delimited-range) ;; includes delimiters
+       ;; Use these to store any range of a delimited HyWikiWord#section
+       (set-marker hywiki--buttonize-start start)
+       (set-marker hywiki--buttonize-end end)
+       ;; Enable dehighlighting in HyWiki pages
+       (unless (and start end)
+         ;; Dehighlight any page name at point
+         (hywiki-maybe-dehighlight-between-page-names)))))
+  (setq hywiki--flag nil))
 
 (defun hywiki-buttonize-word (func start end face)
   "Create a HyWikiWord button by calling FUNC with START and END positions.
@@ -656,7 +677,7 @@ the button."
            cmd (cdr key-cmd))
       (when (eq cmd 'self-insert-command)
        (cond ((and (characterp key)
-                   (= (char-syntax key) ?.))
+                   (eq (char-syntax key) ?.))
               ;; char with punctuation/symbol syntax
               (setq result (cons key result)))
              ((and (consp key)
@@ -1861,7 +1882,7 @@ in a programming mode, must be within a comment."
   (hywiki-maybe-dehighlight-page-name
    ;; Flag on-page-name if on a whitespace character
    (or (= (point) (point-max))
-       (= (char-syntax (char-after)) ? ))))
+       (eq (char-syntax (char-after)) ? ))))
 
 (defun hywiki-maybe-dehighlight-on-page-name ()
   "Dehighlight any non-Org link HyWiki page#section at or one char before 
point.
@@ -2061,9 +2082,9 @@ the current page unless they have sections attached."
   (hywiki-maybe-highlight-page-name
    ;; flag on-page-name if on a whitespace character
    (and (or (= (point) (point-max))
-           (= (char-syntax (char-after)) ? ))
+           (= (if (char-after) (char-syntax (char-after)) 0) ?\ ))
        (or (= (point) (point-min))
-           (/= (char-syntax (char-before)) ? )))))
+           (/= (if (char-before) (char-syntax (char-before)) 0) ?\ )))))
 
 (defun hywiki-maybe-highlight-on-page-name ()
   "Highlight any non-Org link HyWiki page#section at or one char before point.
@@ -2879,47 +2900,77 @@ or this will return nil."
               (wikiword (nth 0 wikiword-start-end))
               (start    (nth 1 wikiword-start-end))
               (end      (nth 2 wikiword-start-end)))
-         (when (if wikiword
-                   ;; Handle an Org link [[HyWikiWord]] [[hy:HyWikiWord]]
-                   ;; or [[HyWikiWord#section][Description Text]].
-                   ;; Get the HyWikiWord link reference, ignoring any
-                   ;; description given in the link
-                   ;; Don't use next line so don't have to load all of Org
-                   ;; mode just to check for HyWikiWords; however, disables
-                   ;; support for Org mode aliases.
-                   ;; (setq wikiword (org-link-expand-abbrev 
(org-link-unescape (string-trim wikiword))))
-                   (progn
-                     (setq wikiword (hywiki-strip-org-link wikiword))
-                     (when (and wikiword end)
-                       ;; Update start and end to newly stripped
-                       ;; string positions
-                       (save-excursion
-                         (save-restriction
-                           (narrow-to-region start end)
-                           (goto-char (point-min))
-                           (when (search-forward wikiword nil t)
-                             (setq start (match-beginning 0)
-                                   end   (match-end 0))))))
-                     (hywiki-word-is-p wikiword))
-                 ;; Handle a non-delimited HyWiki word with optional
-                 ;; #section:Lnum:Cnum; if it is an Org link, it may
-                 ;; optionally have a hy: link-type prefix.  Ignore
-                 ;; wikiwords preceded by any non-whitespace
-                 ;; character, except any of these: "([\"'`'"
-                 (let ((case-fold-search nil))
-                   (skip-chars-backward "-_*#:[:alnum:]")
-                   (when (hywiki-maybe-at-wikiword-beginning)
-                     (cond ((looking-at 
hywiki--word-and-buttonize-character-regexp)
-                            (setq start (match-beginning 1)
-                                  end (match-end 1)
-                                  wikiword (string-trim
-                                            (buffer-substring-no-properties 
start end))))
-                           ((looking-at (concat 
hywiki-word-with-optional-suffix-regexp "\\'"))
-                            (setq start (match-beginning 0)
-                                  end   (match-end 0)
-                                  ;; No following char
-                                  wikiword (string-trim
-                                            (buffer-substring-no-properties 
start end))))))))
+         (when (cond (wikiword
+                      ;; Handle an Org link [[HyWikiWord]] [[hy:HyWikiWord]]
+                      ;; or [[HyWikiWord#section][Description Text]].
+                      ;; Get the HyWikiWord link reference, ignoring any
+                      ;; description given in the link
+                      ;; Don't use next line so don't have to load all of Org
+                      ;; mode just to check for HyWikiWords; however, disables
+                      ;; support for Org mode aliases.
+                      ;; (setq wikiword (org-link-expand-abbrev 
(org-link-unescape (string-trim wikiword))))
+                      (setq wikiword (hywiki-strip-org-link wikiword))
+                      (when (and wikiword end)
+                        ;; Update start and end to newly stripped
+                        ;; string positions
+                        (save-excursion
+                          (save-restriction
+                            (narrow-to-region start end)
+                            (goto-char (point-min))
+                            (when (search-forward wikiword nil t)
+                              (setq start (match-beginning 0)
+                                    end   (match-end 0))))))
+                      (hywiki-word-is-p wikiword))
+
+                     ;; Handle when pre-command-hook has dehighlighted a
+                     ;; delimited HyWikiWord reference with multiple
+                     ;; words in its section, e.g. (WikiWord#one two
+                     ;; three).  That is, allow for spaces in the
+                     ;; section.
+                     ((with-syntax-table hywiki--org-mode-syntax-table
+                        (unless (or (= (if (char-after) (char-syntax 
(char-after)) 0) ?\()
+                                    (= (if (char-before) (char-syntax 
(char-before)) 0) ?\)))
+                          (save-excursion
+                            (condition-case ()
+                                (backward-up-list)
+                              (error nil))
+                            (when (= (if (char-after) (char-syntax 
(char-after)) 0) ?\()
+                              (goto-char (1+ (point)))
+                              (when (looking-at (concat hywiki-word-regexp 
"\\(#[^][()<>{}\"\n\r\f]+\\)?"
+                                                        
hywiki-word-line-and-column-numbers-regexp "?"))
+                                (setq start (match-beginning 0)
+                                      end   (match-end 0)
+                                      ;; No following char
+                                      wikiword (string-trim
+                                                
(buffer-substring-no-properties start end)))))))))
+
+                     ;; Handle a non-delimited HyWikiWord with optional
+                     ;; #section:Lnum:Cnum; if it is an Org link, it may
+                     ;; optionally have a hy: link-type prefix.  Ignore
+                     ;; wikiwords preceded by any non-whitespace
+                     ;; character, except any of these: "([\"'`'"
+                     (t (let ((case-fold-search nil))
+                          (progn
+                            ;; May be a closing delimiter that we have to skip 
past
+                            (skip-chars-backward (regexp-quote 
(hywiki-get-buttonize-characters)))
+                            ;; Skip past HyWikiWord or section
+                            (skip-syntax-backward "^-$()<>._\"\'")
+                            (skip-chars-backward "-_*#:[:alnum:]"))
+                          (when (hywiki-maybe-at-wikiword-beginning)
+                            (cond ((looking-at 
hywiki--word-and-buttonize-character-regexp)
+                                   (setq start (match-beginning 1)
+                                         end (match-end 1)
+                                         wikiword (string-trim
+                                                   
(buffer-substring-no-properties start end))))
+                                  ((and (looking-at 
hywiki-word-with-optional-suffix-regexp)
+                                        ;; Can't be followed by a # character
+                                        (/= (or (char-after (match-end 0)) 0)
+                                            ?#))
+                                   (setq start (match-beginning 0)
+                                         end   (match-end 0)
+                                         ;; No following char
+                                         wikiword (string-trim
+                                                   
(buffer-substring-no-properties start end)))))))))
            (if range-flag
                (list wikiword start end)
              wikiword)))))))
@@ -2961,7 +3012,7 @@ these are handled by the Org mode link handler."
             ;; For now this next version allows spaces and tabs in
             ;; the suffix part
             (eq 0 (string-match
-                   hywiki-word-with-optional-spaces-suffix-exact-regexp
+                   hywiki-word-with-optional-suffix-exact-regexp
                    word))))))
 
 (defun hywiki-word-read (&optional prompt)
@@ -3180,10 +3231,10 @@ DIRECTION-NUMBER is 1 for forward scanning and -1 for 
backward scanning."
          ;; need to be reversed.
          (list (min sexp-start sexp-end) (max sexp-start sexp-end))
        ;; Increment sexp-start so regexp matching excludes the
-       ;; delimiter and starts with the page name.  But include any
+       ;; delimiter and starts with the HyWikiWord.  But include any
        ;; trailing delimiter or regexp matching will not work.
-       (funcall func (1+ start) end)
-       (setq hywiki--highlighting-done-flag nil)))))
+       (prog1 (funcall func (1+ start) end)
+         (setq hywiki--highlighting-done-flag nil))))))
 
 ;;; ************************************************************************
 ;;; Private Org export override functions
@@ -3334,6 +3385,10 @@ matching DATUM before creating a new reference."
 (hywiki-word-highlight-flag-changed 'hywiki-word-highlight-flag
                                    hywiki-word-highlight-flag 'set nil)
 
+;; Ensures HyWiki referent lookup table is initialized as are HyWiki Org
+;; Publish settings.
+(hywiki-set-directory 'hywiki-directory hywiki-directory)
+
 (provide 'hywiki)
 
 ;;; hywiki.el ends here
diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el
index f897d95658..e406e55a75 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:     23-Feb-25 at 11:04:10 by Bob Weiner
+;; Last-Mod:     24-Feb-25 at 00:46:05 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -958,14 +958,13 @@ Note special meaning of `hywiki-allow-plurals-flag'."
           (insert wiki-referent)
           (goto-char 4)
 
-          (should (hact 'kbd-key "C-u C-h hhck{ABC} RET"))
-          (hy-test-helpers:consume-input-events)
+          (kbd-key:act "C-u C-h h h c")
+          ;; (hy-test-helpers:consume-input-events)
+         (sit-for 0.1)
+          (kbd-key:act "k {ABC} RET")
+          ;; (hy-test-helpers:consume-input-events)
 
-          ;; The buffer contents is changed and now reads
-          ;; "Wik{ABC}iReferent" as the next should verifies. The
-          ;; second should is the expected behavior. No change in the
-          ;; WikiPage buffer.
-          (should (string= "Wik{ABC}iReferent" (buffer-substring-no-properties 
(point-min) (point-max))))
+          ;; The expected behavior here is no change in the WikiPage buffer
           (should (string= wiki-referent (buffer-substring-no-properties 
(point-min) (point-max))))
 
           (should (file-exists-p (hywiki-cache-default-file)))
@@ -981,7 +980,6 @@ Note special meaning of `hywiki-allow-plurals-flag'."
 (ert-deftest hywiki-tests--delete-parenthesised-char ()
   "Verify removing a char between parentheses only removes the char.
 See gh#rswgnu/hyperbole/669."
-  :expected-result :failed
   (with-temp-buffer
     (insert "(a)")
     (goto-char 2)

Reply via email to