Skip to content

Commit db61733

Browse files
committed
hywike.el - Dynamically append #section from wikiword to referent
1 parent 349ffa1 commit db61733

File tree

2 files changed

+62
-28
lines changed

2 files changed

+62
-28
lines changed

ChangeLog

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
2024-12-27 Bob Weiner <[email protected]>
2+
3+
* hywiki.el (hywiki-display-referent): Stop removing any #section from
4+
wikiword sent to referent functions.
5+
(hywiki-add-prompted-referent): Remove #section from wikiword
6+
before creating hywiki-referent-menu prefix string.
7+
(hywiki--add-section-to-referent): Add to insert #section into
8+
referent value. Use in 'hywiki-get-referent'.
9+
(hywiki-display-page , hywiki-display-referent, hywiki-get-referent):
10+
Exclude # from 'section' for consistency.
11+
112
2024-12-26 Bob Weiner <[email protected]>
213

314
* test/hywiki-tests.el (hywiki-tests--add-activity,

hywiki.el

+51-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
;; Author: Bob Weiner
44
;;
55
;; Orig-Date: 21-Apr-24 at 22:41:13
6-
;; Last-Mod: 26-Dec-24 at 22:52:28 by Bob Weiner
6+
;; Last-Mod: 27-Dec-24 at 11:10:28 by Bob Weiner
77
;;
88
;; SPDX-License-Identifier: GPL-3.0-or-later
99
;;
@@ -640,21 +640,23 @@ After successfully finding a page and reading it into a buffer, run
640640
(unless buffer-file-name
641641
(error "(hywiki-display-referent): No `wikiword' given; buffer must have an attached file"))
642642
(setq wikiword (file-name-sans-extension (file-name-nondirectory buffer-file-name))))
643-
(when (string-match "#[^#]+$" wikiword)
644-
(setq wikiword (substring wikiword 0 (match-beginning 0))))
645-
(let* ((referent (cond (prompt-flag
646-
(hywiki-add-prompted-referent wikiword))
647-
((hywiki-get-referent wikiword))
648-
(t (hywiki-add-page wikiword))))
649-
(referent-value (cdr referent)))
650-
(when referent
651-
;; Ensure highlight any page name at point in case called as a
643+
(let* ((section (when (string-match "#[^#]+$" wikiword)
644+
(substring wikiword (1+ (match-beginning 0)))))
645+
(htable-referent (cond (prompt-flag
646+
(hywiki-add-prompted-referent wikiword))
647+
((hywiki-get-referent wikiword))
648+
(t (hywiki-add-page wikiword))))
649+
referent)
650+
;; HyWikiWord instance may contain a section that must be
651+
;; added to the referent value.
652+
(if (not (setq referent (hywiki--add-section-to-referent
653+
section htable-referent)))
654+
(error "(hywiki-display-referent): Invalid `%s' referent: %s"
655+
wikiword htable-referent)
656+
;; Ensure highlight any page name at point in case called as a
652657
;; Hyperbole action type
653658
(hywiki-maybe-highlight-page-name t)
654-
(if (and (consp referent) (not (listp referent-value)))
655-
(hywiki-display-referent-type wikiword referent)
656-
(error "(hywiki-display-referent): Invalid `%s' referent: %s"
657-
wikiword referent))
659+
(hywiki-display-referent-type wikiword referent)
658660
(hywiki-maybe-highlight-page-names)
659661
(run-hooks 'hywiki-display-referent-hook)
660662
referent)))
@@ -728,14 +730,16 @@ After successfully finding a page and reading it into a buffer, run
728730
(hui:menu-act 'hywiki-referent-menu
729731
(list (cons 'hywiki-referent-menu
730732
(cons (list (format "%s RefType>"
731-
wikiword))
733+
(if (string-match "#[^#]+$" wikiword)
734+
(substring wikiword 0 (match-beginning 0))
735+
wikiword)))
732736
(cdr hywiki-referent-menu)))))))
733737
(or referent
734738
(when (called-interactively-p 'interactive)
735739
(user-error "(hywiki-add-prompted-referent): Invalid HyWikiWord: '%s'; must be capitalized, all alpha" wikiword)))))
736740

737741
(defun hywiki-add-referent (wikiword referent)
738-
"Add WIKIWORD that displays REFERENT to HyWiki.
742+
"Add WIKIWORD (sans any #section) that displays REFERENT to HyWiki.
739743
Return REFERENT if WIKIWORD is of valid format, otherwise return nil.
740744
REFERENT must be a cons of (<referent-type) . <referent-value>) or
741745
an error is triggered."
@@ -1098,7 +1102,8 @@ Use `hywiki-get-referent' to determine whether a HyWiki page exists."
10981102
"Display an optional WIKIWORD page and return the page file.
10991103
Use `hywiki-display-page-function' to display the page.
11001104
1101-
If REFERENT is provided, the page file is its `cdr'.
1105+
If REFERENT is provided, its `cdr' is the page file with any #section
1106+
from the WIKIWORD included.
11021107
11031108
If WIKIWORD is omitted or nil and `hywiki-display-page-function'
11041109
is an interactive function, it is called interactively and prompts for
@@ -2021,18 +2026,13 @@ value returns nil."
20212026
If it is a pathname, expand it relative to `hywiki-directory'."
20222027
(when (and (stringp wikiword) (not (string-empty-p wikiword))
20232028
(string-match hywiki-word-with-optional-section-exact-regexp wikiword))
2024-
(let* ((_section (when (match-string-no-properties 2 wikiword)
2025-
(prog1 (substring wikiword (match-beginning 2))
2026-
;; Remove any #section suffix in `wikiword'.
2027-
(setq wikiword (match-string-no-properties 1 wikiword)))))
2029+
(let* ((section (when (match-string-no-properties 2 wikiword)
2030+
(prog1 (substring wikiword (1+ (match-beginning 2)))
2031+
;; Remove any #section suffix in `wikiword'.
2032+
(setq wikiword (match-string-no-properties 1 wikiword)))))
20282033
(referent (hash-get (hywiki-get-singular-wikiword wikiword)
2029-
(hywiki-get-referent-hasht)))
2030-
(referent-type (car referent))
2031-
(referent-value (cdr referent)))
2032-
(when (and (consp referent)
2033-
(symbolp referent-type)
2034-
(not (listp referent-value)))
2035-
referent))))
2034+
(hywiki-get-referent-hasht))))
2035+
(hywiki--add-section-to-referent section referent))))
20362036

20372037
(defun hywiki-get-page-files ()
20382038
"Return the list of existing HyWiki page file names.
@@ -2586,6 +2586,29 @@ Highlight/dehighlight HyWiki page names across all frames on change."
25862586
;;; Private functions
25872587
;;; ************************************************************************
25882588

2589+
(defun hywiki--add-section-to-referent (section referent)
2590+
"Add #SECTION to REFERENT's value and return REFERENT.
2591+
SECTION excludes # prefix Return nil if any input is invalid."
2592+
(if (or (null section) (and (stringp section) (string-empty-p section)))
2593+
referent
2594+
(when (consp referent)
2595+
(let ((referent-type (car referent))
2596+
(referent-value (cdr referent)))
2597+
(when (and (symbolp referent-type) referent-value)
2598+
(if (and (stringp section)
2599+
(stringp referent-value)
2600+
(memq referent-type '(page path-link))
2601+
(not (seq-contains-p referent-value ?# #'=)))
2602+
;; Need to insert #section into referent's value
2603+
(progn
2604+
(if (string-match hpath:line-and-column-regexp referent-value)
2605+
(setq referent-value (concat (substring 0 (match-beginning 0))
2606+
"#" section
2607+
(match-string 0 referent-value)))
2608+
(setq referent-value (concat referent-value "#" section)))
2609+
(cons referent-type referent-value))
2610+
referent))))))
2611+
25892612
(defun hywiki--get-delimited-range-backward ()
25902613
"Return a list of (start end) if not between/after end ]] or >>.
25912614
Otherwise, return nil."

0 commit comments

Comments
 (0)