-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhywiki.el
More file actions
4713 lines (4250 loc) · 197 KB
/
hywiki.el
File metadata and controls
4713 lines (4250 loc) · 197 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; hywiki.el --- Hyperbole's auto-wikiword note-taking system -*- lexical-binding: t -*-
;;
;; Author: Bob Weiner
;;
;; Orig-Date: 21-Apr-24 at 22:41:13
;; Last-Mod: 12-Apr-26 at 15:09:20 by Bob Weiner
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; Copyright (C) 2024-2026 Free Software Foundation, Inc.
;; See the "HY-COPY" file for license information.
;;
;; This file is part of GNU Hyperbole.
;;; Commentary:
;;
;; This is Hyperbole's markup-free personal Wiki system for note-taking
;; and automatic wiki word highlighting and hyperlinking. It uses Org
;; mode for note taking and adds automatic hyperlinking of HyWikiWords
;; within Org files in `hywiki-directory' (default = "~/hywiki"), where
;; a HyWikiWord is a capitalized word that contains upper and lowercase
;; letters only and has a corresponding HyWikiWord.org wiki page file
;; below `hywiki-directory'. HyWikiWords require no delimiters.
;;
;; HyWikiWords are also recognized in text buffers after the global
;; minor mode, `hywiki-mode' is enabled via {M-x hywiki-mode RET}. To
;; create or jump to a HyWiki page, simply type o ut a potential
;; HyWikiWord or move point onto one and press the Action Key {M-RET}.
;; This will create the associated page if it does not exist. This
;; also highlights any other instances of HyWikiWords across all
;; visible Emacs windows. HyWiki is built for scalability and has been
;; tested to be performant with 10,000 HyWikiWords.
;;
;; Once Hyperbole has been loaded and activated, HyWikiWords (with or
;; without delimiters) are automatically highlighted and active in
;; the following contexts:
;; - HyWiki page buffers;
;; - non-special text buffers, when `hywiki-mode' is enabled;
;; - comments and strings in programming buffers, when
;; `hywiki-mode' is enabled.
;;
;; As HyWikiWords are typed, highlighting occurs after a trailing
;; whitespace or punctuation character is added, or when it is
;; surrounded by a matching pair of characters such as curly braces
;; or single square brackets. Since Org links use double square
;; brackets and Org targets use double or triple angle brackets,
;; HyWikiWords within these delimiters are ignored.
;;
;; You can also create Org links to HyWikiWords in any non-special text
;; buffer by surrounding them with double square brackets and the
;; 'hy:' prefix, as in: [[hy:MyWikiWord]]. If you set
;; `hywiki-org-link-type-required' to `nil', then you don't need the
;; prefix, e.g. [[MyWikiWord]]; existing HyWiki page names then will
;; override Org's standard handling of such links. To prevent Org
;; mode's binding of {M-RET} from splitting lines and creating new
;; headlines when on a HyWikiWord whose page has not yet been
;; created, set `hsys-org-enable-smart-keys' to `t' so that
;; Hyperbole's Action Key does the right thing in this context.
;;
;; HyWikiWord links can also link to a section headline within a page
;; by simply following the page name with a '#' character and then the
;; section headline name. For example, if your Emacs page has a "Major
;; Modes" section, then either Emacs#Major-Modes or [[hy:Emacs#Major
;; Modes]] will work as a link to that section. Note that without the
;; square bracket delimiters, you must convert spaces in section names
;; to '-' characters. As long as the page exists, section links are
;; highlighted regardless of whether associated sections exist or not.
;; When activating a link with a section reference, you will get an
;; error if the section does not exist.
;;
;; By default (hywiki-mode = :pages), HyWikiWords are
;; auto-highlighted within HyWiki pages only. Outside of such pages,
;; `hywiki-mode' must be set to :all to enable auto-highlighting in
;; programming and text modes. Auto-highlighting depends on pre- and
;; `post-command-hook' settings. If an error occurs running one of
;; these, the associated hook is removed. To restore the
;; auto-highlight hooks use {C-u C-h h h m} to toggle `hywiki-mode';
;; this also enables auto-highlighting when `hywiki-mode' is non-nil.
;; The custom setting, `hywiki-exclude-major-modes' (default = nil), is
;; a list of major modes to exclude from HyWikiWord auto-highlighting
;; and recognition.
;;
;; Within programming modes, HyWikiWords are highlighted/hyperlinked
;; within comments and double-quoted strings only. For programming
;; modes in which you want HyWikiWords recognized everywhere, add
;; them to the custom setting, `hywiki-highlight-all-in-prog-modes'
;; (default = '(lisp-interaction-mode)).
;;
;; HyWiki adds two implicit button types to Hyperbole:
;; `hywiki-word' - create and display HyWikiWord referents;
;; `hywiki-existing-word' - display an existing HyWikiWord referent.
;;
;; `hywiki-word' is one of the lowest priority implicit button types
;; so that it triggers only when other types are not recognized first.
;;
;; A HyWiki can be exported to HTML for publishing to the web via Org
;; mode's publish a project feature. {M-x hywiki-publish-to-html RET}
;; will and that's it! Add a prefix argument to force regeneration of all
;; HyWiki pages, rather than only those that have been updated.
;;
;; The full set of HyWiki-specific Org publish properties are set in
;; the variable `hywiki-org-publish-project-alist'. When the HyWiki
;; code is loaded into Emacs, it automatically integrates these
;; properties with Org's publishing framework, so when in a HyWiki
;; page, you can use the standard {C-c C-e P p} current project publish
;; command.
;;
;; There are a few publishing settings you can customize prior to
;; loading Hyperbole's HyWiki code.
;;
;; HyWiki html files are saved in:
;; (hywiki-org-get-publish-property :publishing-directory)
;; Customize this directory with:
;; {M-x customize-variable RET hywiki-org-publishing-directory RET}.
;;
;; HyWiki html files are generated by the function given by:
;; (hywiki-org-get-publish-property :publishing-function)
;; Customize the value of this function if necessary with:
;; {M-x customize-variable RET hywiki-org-publishing-function RET}.
;;
;; This section summarizes HyWikiWord Actions based on the
;;
;; hywiki-referent-prompt-flag When nil When t
;; -------------------------------------------------------------------------------------
;; Action Key hywiki-word-create-and-display
;; or HyWiki/Create Create Page and Display Create Referent and Display
;; Assist Key hywiki-word-create-and-display
;; or C-u HyWiki/Create Create Referent and Display Create Page and Display
;; hywiki-word-create hywiki-create-page with Msg hywiki-create-referent with Msg
;; C-u hywiki-word-create hywiki-create-referent with Msg hywiki-create-page with Msg
;;; Code:
;;; ************************************************************************
;;; Other required Elisp libraries
;;; ************************************************************************
(require 'hactypes) ;; For `link-to-file-interactively'
(require 'hargs)
(require 'hasht)
(require 'hbut) ;; For `hbut:syntax-table'
;; (unless (featurep 'hibtypes)
;; (require 'hibtypes)) ;; For `pathname' and `pathname-line-and-column'
(require 'hpath)
(require 'hproperty)
(require 'hsys-consult)
(eval-when-compile (require 'consult nil t))
(require 'hui) ;; For `hui:actype'
(require 'hui-mini) ;; For `hui:menu-act'
(require 'hypb) ;; Requires `seq'
(require 'outline) ;; For `outline-mode-syntax-table'
(require 'seq) ;; For `seq-contains-p', `seq-difference' and `seq-intersection'
(require 'subr-x) ;; For `string-remove-prefix'
(require 'thingatpt)
;;; ************************************************************************
;;; Public declarations
;;; ************************************************************************
(defvar action-key-modeline-buffer-id-function) ;; "hui-mouse.el"
(defvar bookmark-current-bookmark) ;; "bookmark.el"
(defvar consult-grep-args) ;; "consult.el"
(defvar consult-ripgrep-args) ;; "consult.el"
(defvar hkey-value) ;; "hui-mouse.el"
(defvar hywiki-referent-menu nil) ;; "hywiki.el"
(defvar org-agenda-buffer-tmp-name) ;; "org-agenda.el"
(defvar org-export-with-broken-links) ;; "ox.el"
(defvar org-publish-project-alist) ;; "ox-publish.el"
(declare-function activities-completing-read "activities" (:prompt prompt :default default))
(declare-function activities-new "activities" (name))
(declare-function activities-resume "activities" (activity :resetp resetp))
(declare-function bookmark-completing-read "bookmark" (prompt &optional default))
(declare-function bookmark-location "bookmark" (bookmark-name-or-record))
(declare-function consult--async-command "ext:consult")
(declare-function consult--async-process "ext:consult")
(declare-function consult--async-refresh-timer "ext:consult")
(declare-function consult--async-sink "ext:consult")
(declare-function consult--async-split "ext:consult")
(declare-function consult--async-throttle "ext:consult")
(declare-function consult--lookup-member "ext:consult")
(declare-function consult--read "ext:consult")
(declare-function hsys-org-at-tags-p "hsys-org")
(declare-function hywiki-org-format-heading "hywiki")
(declare-function ibtypes::pathname "hpath")
(declare-function ibtypes::pathname-line-and-column "hpath")
(declare-function org-fold-core-remove-optimisation "org-fold-core")
(declare-function org-fold-core-update-optimisation "org-fold-core")
(declare-function org-fold-show-entry "org-fold")
(declare-function org-link-store-props "ol" (&rest plist))
(declare-function org-publish-property "ox-publish" (property project &optional default))
(declare-function org-roam-node-from-title-or-alias "org-roam-node" (s &optional nocase))
(declare-function org-roam-node-open "org-roam" (note &optional cmd force))
(declare-function org-roam-node-read "org-roam" (&optional initial-input filter-fn sort-fn require-match prompt))
(declare-function org-roam-node-title "org-roam-node" (node))
(declare-function smart-treemacs-edit "hui-treemacs" (&optional dir))
;;; ************************************************************************
;;; Private variables
;;; ************************************************************************
(defvar hywiki--prior-mode nil)
(defvar-local hywiki--buffer-modified-tick nil
"Used to determine if a command modifies a buffer or not.
The `pre-command-hook' saves this value for a buffer and `post-command-hook'
checks it to determine if any buffer modification has occurred or not.")
;; Must be set after `hywiki-get-buttonize-characters' is defined
(defvar hywiki--buttonize-character-regexp nil
"Regexp matching a single separating character following a HyWikiWord.
Each such key self-inserts before highlighting any prior HyWikiWord
in `hywiki-mode'.")
(defvar hywiki--buttonize-characters-cache nil
"Single string cache of Org-mode syntax table punctuation/symbol characters.")
(defconst hywiki--close-open-hasht (hash-make '(("\"" . ?\")
("'" . ?\')
("}" . ?{)
("]" . ?\[)
(">" . ?<)
(")" . ?\())
t)
"Delimiter htable with (close-delim-string . open-delim-char) key-value pairs.")
(defconst hywiki--open-close-hasht (hash-make '(("\"" . ?\")
("'" . ?\')
("{" . ?})
("[" . ?\])
("<" . ?>)
("(" . ?\)))
t)
"Delimiter htable with (open-delim-string . close-delim-char) key-value pairs.")
(defvar hywiki--directory-checksum ""
"String checksum for `hywiki-directory' page names.")
(defvar hywiki--directory-mod-time nil
"Last mod time for `hywiki-directory' or nil if the value has not been read.
See `current-time' function for the mod time format.")
(defvar hywiki--org-heading-regexp nil
"Cache standard `org-complex-heading-regexp' value.
Group 4 is the title. Call `hywiki--org-set-heading-regexp'
to re-initialize.")
(defvar hywiki--org-todo-regexp nil
"Cache regular and custom Org todo keywords for `hywiki-directory'.
Call `hywiki--org-set-heading-regexp' to re-initialize.")
;; Redefine the `org-mode-syntax-table' for use in `hywiki-get-buttonize-characters'
;; so do not have to load all of Org mode there.
(defvar hywiki--org-mode-syntax-table
(let ((st (make-syntax-table outline-mode-syntax-table)))
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\\ "_" st)
(modify-syntax-entry ?~ "_" st)
(modify-syntax-entry ?< "(>" st)
(modify-syntax-entry ?> ")<" st)
st)
"Standard syntax table for Org mode buffers with HyWiki support.")
(defvar hywiki--pages-directory nil)
(defvar hywiki--referent-alist nil
"HyWiki alist generated from `hywiki--referent-hasht' for storage in cache.
Each element is of the form: (\"wikiword\" . (referent-type . referent-value)).")
(defvar hywiki--referent-hasht nil
"HyWiki hash table for fast WikiWord referent lookup.")
(defvar hywiki--word-and-buttonize-character-regexp nil
"Regexp matching HyWikiWord#section plus a valid word separating character.
Group 1 is the entire HyWikiWord#section:Lnum:Cnum expression.")
;; Globally set these values to avoid using 'let' with stack allocations
;; within `hywiki-maybe-highlight-reference' frequently.
(defvar hywiki--any-wikiword-regexp-list nil)
(defvar hywiki--current-page nil)
(defvar hywiki--highlighting-done-flag t)
(defvar hywiki--word-pre-command nil)
(defvar hywiki--word-only nil)
(defvar hywiki--save-case-fold-search nil)
(defvar hywiki--save-org-link-type-required nil)
;; Prevents multiple runs of hywiki pre and post hooks
(defvar hywiki--command-executed-flag nil)
(defvar-local hywiki--buts nil)
(defvar-local hywiki--but-end nil)
(defvar-local hywiki--but-start nil)
(defvar-local hywiki--buttonize-end (make-marker)) ;; This must always stay a marker
(defvar-local hywiki--buttonize-start (make-marker)) ;; This must always stay a marker
(defvar-local hywiki--buttonize-range nil)
(defvar-local hywiki--char-before nil)
(defvar-local hywiki--end-pos nil)
(defvar-local hywiki--end nil)
(defvar-local hywiki--range nil)
(defvar-local hywiki--start nil)
(defvar-local hywiki--start-pos nil)
;;;
;;; ************************************************************************
;;; Public variables
;;; ************************************************************************
;; Clear the cache if any changes to the `global-map' keymap
(add-variable-watcher 'global-map
#'hywiki--clear-buttonize-characters-cache)
(defcustom hywiki-exclude-major-modes nil
"List of major modes to exclude from HyWikiWord highlighting and recognition."
:type '(list symbol)
:group 'hyperbole-hywiki)
(defcustom hywiki-highlight-all-in-prog-modes '(lisp-interaction-mode)
"List of programming major modes to highlight HyWikiWords outside of comments."
:type '(list symbol)
:group 'hyperbole-hywiki)
(defcustom hywiki-mode-lighter " HyWiki"
"String to display in mode line when the HyWiki global minor mode is enabled.
Use nil for no HyWiki mode indicator."
:type 'string
:group 'hyperbole-hywiki)
(defconst hywiki-ignore-face-list '(button hbut-face hbut-item-face
ibut-face org-link)
"Skip highlighting of HyWikiWords in regions which have any of these faces.")
(defvar hywiki-allow-suffix-referent-types '(page path-link)
"List of referent type symbols that support # and :L line number suffixes.")
;;;###autoload
(defun hywiki-let-directory (option value)
(set option value)
(hywiki-clear-referent-hasht)
(hywiki-make-referent-hasht))
;;;###autoload
(defun hywiki-set-directory (option value)
(unless (and (boundp 'hywiki-directory)
(equal hywiki-directory (file-name-as-directory value))
(hash-table-p hywiki--referent-hasht))
(set-default option (file-name-as-directory value))
(hywiki-clear-referent-hasht)
(hywiki-make-referent-hasht))
(hywiki-org-set-publish-project))
(defcustom hywiki-directory "~/hywiki/"
"Directory that holds all HyWiki pages in Org format.
See `hywiki-org-publishing-directory' for exported pages in html format."
:initialize #'custom-initialize-default
:set #'hywiki-set-directory
:type 'string
:group 'hyperbole-hywiki)
(defun hywiki-directory-changed (option set-to-value operation _where)
"Watch function for variable `hywiki-directory'.
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)))
;; This next line is needed to invoke `hywiki-set-directory' when
;; `hywiki-directory' is changed via `setq' or `let' rather than
;; `customize-set-variable'.
(add-variable-watcher 'hywiki-directory #'hywiki-directory-changed)
(defvar-local hywiki-buffer-highlighted-state nil
"State of HyWikiWords highlighting in the associated buffer.
\\='h means the buffer was already highlighted;
\\='d means the buffer was dehighlighted;
nil means no full buffer highlighting has occurred.")
(defvar hywiki-non-character-commands
'(;; Org mode
org-cycle ;; TAB
org-open-line ;; C-o
org-return ;; RET, \r
org-return-and-maybe-indent ;; C-j, \n
;; Markdown mode
markdown-cycle ;; TAB
markdown-enter-key ;; RET, \r
electric-newline-and-maybe-indent ;; C-j, \n
;; Global
newline ;; RET, \r
newline-and-indent ;; RET, \r
open-line ;; C-o
quoted-insert ;; C-q
)
"List of non-character commands.
Commands that insert characters but whose input events do not
arrive as characters or that quote another character for input.")
;; Define the keymap for hywiki-mode.
(defvar hywiki-mode-map nil
"Keymap for `hywiki-mode'.
Presently, there are no key bindings; this is for future use.")
(defconst hywiki-org-link-type "hy"
"HyWiki string prefix type for Org links. Excludes trailing colon.")
(defvar hywiki-org-link-type-required t
"When t, [[hy:HyWiki Org links]] must start with `hywiki-org-link-type':.
Otherwise, this prefix is not needed and HyWikiWord Org links
override standard Org link lookups. See \"(org)Internal Links\".")
;; Clear the cache if any changes to the `hywiki--org-mode-syntax-table'
(add-variable-watcher 'hywiki--org-mode-syntax-table
#'hywiki--clear-buttonize-characters-cache)
(defcustom hywiki-org-publishing-broken-links 'mark
"HyWiki Org publish option that determines how invalid links are handled.
The default is \\='mark.
When this option is non-nil, broken HyWiki links are ignored,
without stopping the export process. If it is set to \\='mark,
broken links are marked with a string like:
[BROKEN LINK: path]
where PATH is the un-resolvable reference."
:initialize #'custom-initialize-default
:set (lambda (option value)
(set option value)
(hywiki-org-set-publish-project))
:type 'symbol
:group 'hyperbole-hywiki)
(defcustom hywiki-org-publishing-directory "~/public_hywiki"
"Directory where HyWiki pages are converted into html and published."
:initialize #'custom-initialize-default
:set (lambda (option value)
(set option value)
(hywiki-org-set-publish-project))
:type 'string
:group 'hyperbole-hywiki)
(defcustom hywiki-org-publishing-function 'org-html-publish-to-html
"HyWiki Org publish function used to export a HyWiki page to html."
:initialize #'custom-initialize-default
:set (lambda (option value)
(set option value)
(hywiki-org-set-publish-project))
:type 'symbol
:group 'hyperbole-hywiki)
(defcustom hywiki-org-publishing-sitemap-title
(let ((dir-name (file-name-base
(directory-file-name
(file-name-as-directory hywiki-directory)))))
(if (equal dir-name "hywiki")
"HyWiki"
dir-name))
"HyWiki Org publish sitemap title."
:initialize #'custom-initialize-default
:set (lambda (option value)
(set option value)
(hywiki-org-set-publish-project))
:type 'string
:group 'hyperbole-hywiki)
(defun hywiki--export-preparation-function (_project-plist)
"Setup export hook functions."
(message "Hywiki export being prepared...")
(add-hook 'org-export-before-parsing-functions #'hywiki-org-export-function))
(defun hywiki--export-completion-function (_project-plist)
"Remove export hook function."
(remove-hook 'org-export-before-parsing-functions #'hywiki-org-export-function)
(message "Hywiki export completed."))
(defvar hywiki-org-publish-project-alist nil
"HyWiki-specific export properties added to `org-publish-project-alist'.")
(defun hywiki-org-make-publish-project-alist ()
(setq org-export-with-broken-links hywiki-org-publishing-broken-links
hywiki-org-publish-project-alist
(list
"hywiki"
:preparation-function 'hywiki--export-preparation-function
:completion-function 'hywiki--export-completion-function
:auto-sitemap t
:base-directory (expand-file-name hywiki-directory)
:html-head (format
"<link rel=\"stylesheet\" type=\"text/css\" href=\"%sman/hyperbole.css\"/>"
hyperb:dir)
:html-link-home "index.html"
;; :html-link-up "theindex.html"
;; !! TODO: The :makeindex property is disabled for now, until a process is
;; developed to force the Org publish process to regenerate the
;; index after index entries are inserted into the temporary Org
;; buffer prior to export to HTML.
:html-postamble t
:html-postable-format '(("en" "<p class=\"author\">Author: %a (%e)</p>
<p class=\"last-mod\">Last Modified: %C</p>
<p class=\"creator\">%c</p>"))
:html-prefer-user-labels t
:makeindex nil
:publishing-directory hywiki-org-publishing-directory
:publishing-function hywiki-org-publishing-function
:section-numbers t
:shell "shell-command"
:sitemap-filename "index.org"
;; sitemap (TOC) is stored in "sitemap.html"
:sitemap-title hywiki-org-publishing-sitemap-title
:with-date t
:with-toc nil)))
(defvar-local hywiki-page-flag nil
"Set to t after finding a HyWiki page file, else nil.
The file must be below `hywiki-directory'.
For reference, this is set when `window-buffer-change-functions' calls
`hywiki-maybe-highlight-references' which calls `hywiki-in-page-p'.")
(defcustom hywiki-referent-prompt-flag nil
"Non-nil means the Action Key and HyWiki/Create always prompt for referent type.
Nil by default."
:type 'boolean
:initialize #'custom-initialize-default
:group 'hyperbole-hywiki)
(defvar hywiki-file-suffix ".org"
"File suffix string (including period) to use when creating HyWiki pages.")
(defconst hywiki-word-regexp
(format "\\<\\([[:upper:]][[:alpha:]]+\\)\\>\\(?:%s\\)?"
(regexp-quote hywiki-file-suffix))
"Regexp that matches a HyWikiWord only.
Do not use a start or end line/string anchor in this regexp.")
(defconst hywiki-word-section-regexp
"\\(#[^][# \t\n\r\f]+\\)"
"Regexp that matches a non-delimited HyWikiWord #section extension.
After the first # character, this may contain any non-square-bracket,
non-# and non-whitespace characters.")
(defconst hywiki-word-line-and-column-numbers-regexp
(concat "\\(" hpath:line-and-column-numbers "\\)")
"Group 2 is the 1-based line number.
Group 4 is the optional 0-based column number.")
(defconst hywiki-word-with-optional-suffix-regexp
(concat hywiki-word-regexp hywiki-word-section-regexp "??"
hywiki-word-line-and-column-numbers-regexp "?")
"Regexp for a HyWikiWord with an 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 HyWikiWord.
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 "?\\'")
"Exact match regexp for a HyWikiWord with an optional #section.
The section may contain spaces or tabs but not square brackets;
it is preferable, however, to substitute '-' for whitespace in
the section/headline name to simplify recognition.
Group 1 is the HyWikiWord.
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-suffix-regexp "\\(#\\|::\\|:L\\)\\(.+\\)\\'"
"Regexp matching any trailing part of a HyWikiWord reference.
It may be a section or a line number reference. Group one is the type
of reference and group two is the rest of the suffix reference.")
(defface hywiki--word-face
'((((min-colors 88) (background dark)) (:foreground "orange" :underline t))
(((background dark)) (:background "orange" :foreground "black" :underline t))
(((min-colors 88)) (:foreground "orange" :underline t))
(t (:background "orange" :underline t)))
"Face for HyWikiWord highlighting."
:group 'hyperbole-hywiki)
(defcustom hywiki-word-face 'hywiki--word-face
"Hyperbole face for HyWikiWord highlighting."
:initialize #'custom-initialize-default
:type 'face
:group 'hyperbole-hywiki)
(defcustom hywiki-display-page-function #'hpath:find
"Hyperbole function to display HyWiki page pathnames.
Only argument is the page's pathname."
:initialize #'custom-initialize-default
:type 'string
:group 'hyperbole-hywiki)
(defcustom hywiki-allow-plurals-flag t
"Non-nil means plural HyWikiWords have the same referent as the singular form.
Non-nil is the default."
:initialize #'custom-initialize-default
:set (lambda (option value)
(set option value)
(setq hywiki--any-wikiword-regexp-list nil))
:type 'boolean
:group 'hyperbole-hywiki)
;;; ************************************************************************
;;; hywiki minor mode and text edit command hooks
;;; ************************************************************************
(defun hywiki-ignore-command-hooks-p ()
"Prevent duplicate runs of hywiki command hooks within a single command."
(and hywiki--command-executed-flag
(hyperb:stack-frame '(hypb:eval))))
(defun hywiki-word-store-around-point ()
"Store any HyWikiWord before or after point for post-command comparison.
Markers are stored into `hywiki--buttonize-start' and `hywiki--buttonize-end'.
HyWikiWords are stored only outside of `hywiki-non-hook-context-p' contexts.
This is triggered by `pre-command-hook' for non-character commands,
including deletion commands and those in `hywiki-non-character-commands'."
(unless (hywiki-ignore-command-hooks-p)
;; (when ert--running-tests
;; (message "Running pre-command-hook..."))
(setq hywiki--buffer-modified-tick (buffer-modified-tick)
hywiki--word-pre-command nil)
(unless (bound-and-true-p edebug-active)
(set-marker hywiki--buttonize-start nil)
(set-marker hywiki--buttonize-end nil)
(setq hywiki--buttonize-range nil))
(when (and (current-idle-time) (hywiki-non-hook-context-p) (hywiki-word-at))
;; Dehighlight any previously highlighted WikiWord at point if
;; it is outside of a valid context.
(save-restriction
(narrow-to-region (line-beginning-position) (line-end-position 2))
(hywiki-maybe-dehighlight-reference)))
(if (and (symbolp this-command)
(string-match-p "\\(^\\|-?\\)\\(insert\\|undo\\)\\(-\\|$\\)\\|eval-last-sexp\\|eval-expression\\|read--expression-try-read" (symbol-name this-command)))
;; prior to an insertion command
(progn
(setq hywiki--start (point)
hywiki--end nil)
;; Use these to store any range of a delimited HyWikiWord#section
(set-marker hywiki--buttonize-start nil)
(set-marker hywiki--buttonize-end nil)
hywiki--start)
(unless (hywiki-non-hook-context-p)
;; Record the WikiWord from any WikiWord ref that point is on
(setq hywiki--word-pre-command (hywiki-get-singular-wikiword
(hywiki-word-at)))
(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))))
;; Test if at delimiters surrounding a single WikiWord reference
;; and if so, record those for use by post hooks.
(cl-destructuring-bind (start end)
;; Get delimited region only if before or after delimiters,
;; else return (nil nil).
(setq hywiki--buttonize-range
(hywiki-at-range-delimiter)) ;; includes delimiters
(setq hywiki--start (point))
;; Use these to store any range of a delimited HyWikiWord#section
(set-marker hywiki--buttonize-start start)
(set-marker hywiki--buttonize-end end)
start))))
(setq hywiki--command-executed-flag t)))
(defun hywiki-word-highlight-post-self-insert ()
"Turn any HyWikiWords around point into highlighted Hyperbole buttons.
Triggered by `post-self-insert-hook' after self-inserting one or
more characters while the command is still executing. The
`post-command-hook' runs later after the command has finished."
;; (when ert--running-tests
;; (message "Running post-self-insert-hook..."))
(unless (or (hywiki-ignore-command-hooks-p)
(hywiki-non-hook-context-p))
(setq hywiki--range nil)
;; Dehighlight any previously highlighted WikiWord at point
;; before we move to the start of any current WikiWord and
;; rehighlight that.
(hywiki--maybe-dehighlight-at-point))
(save-excursion
(cond ((marker-position hywiki--buttonize-start)
;; Point was before or after a WikiWord delimiter
(goto-char hywiki--buttonize-start)
(skip-chars-backward "-" (line-beginning-position))
(goto-char (1- (point))))
((not (equal (setq hywiki--range (hywiki-highlight-word-get-range))
'(nil nil nil)))
(cl-destructuring-bind (_ start end)
hywiki--range
(if (and start end)
(progn
;; On a non-delimited HyWikiWord
(set-marker hywiki--buttonize-start start)
(set-marker hywiki--buttonize-end end)
(goto-char start)
(skip-chars-backward "-" (line-beginning-position))
t)
(setq hywiki--range nil))))
((not (member (setq hywiki--range (hywiki-at-range-delimiter))
'(nil nil)))
;; At delimiters surrounding a WikiWord
(let ((start (nth 0 hywiki--range))
(end (nth 1 hywiki--range)))
(when (and start end)
;; Use these to store any range of a delimited HyWikiWord#section
(set-marker hywiki--buttonize-start (1+ start))
(set-marker hywiki--buttonize-end (1- end))))))
(unless (hywiki-non-hook-context-p)
;; This first rehighlighting is needed to ensure
;; any wikiword before an inserted whitespace character is
;; properly highlighted when separating two words or after a
;; closing delimiter.
(save-excursion
(goto-char (max (1- (point)) (point-min)))
(hywiki--maybe-rehighlight-at-point))
(hywiki--maybe-rehighlight-at-point))))
(defun hywiki-word-highlight-post-command ()
"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'."
;; (when ert--running-tests
;; (message "Running post-command-hook..."))
(unless (or (eq hywiki--buffer-modified-tick (buffer-modified-tick))
(hywiki-ignore-command-hooks-p))
(setq hywiki--range nil)
(cond ((and (symbolp this-command)
(string-match-p "\\(^\\|-?\\)\\(insert\\|undo\\)\\(-\\|$\\)\\|eval-last-sexp\\|eval-expression\\|read--expression-try-read"
(symbol-name this-command)))
(setq hywiki--end (point))
(when (and hywiki--start (not (eq hywiki--start hywiki--end)))
;; Something has been inserted
(cl-destructuring-bind (start end)
(hywiki--extend-region
(min hywiki--start hywiki--end)
(max hywiki--start hywiki--end))
(hywiki-maybe-dehighlight-references start end)
(hywiki-maybe-highlight-references start end))))
((when (or (memq this-command hywiki-non-character-commands)
(and (symbolp this-command)
(string-match-p "^\\(org-\\)?\\(delete-\\|kill-\\)\\|\\(-delete\\|-kill\\|eval-last-sexp\\|eval-expression\\)\\(-\\|$\\)\\|^\\(hkey-either\\|action-key\\|assist-key\\)" (symbol-name this-command))))
(save-excursion
;; Dehighlight any previously highlighted WikiWord at point
;; before we move to the start of any current WikiWord and
;; rehighlight that.
;; Dehighlight if point is on or between a HyWikiWord
(save-restriction
(narrow-to-region (line-beginning-position) (line-end-position 2))
(hywiki-maybe-dehighlight-between-references))
;; Record the WikiWord from any WikiWord ref that point is on
(unless hywiki--word-pre-command
(setq hywiki--word-pre-command (hywiki-get-singular-wikiword
(or (unless (hywiki-non-hook-context-p)
(hywiki-word-at))
(progn (goto-char (max (point-min)
(1- (point))))
(unless (hywiki-non-hook-context-p)
(hywiki-word-at)))))))
(cond ((marker-position hywiki--buttonize-start)
;; Point was before or after a WikiWord delimiter
(goto-char (1+ hywiki--buttonize-start))
(unless (hywiki-non-hook-context-p)
(set-marker hywiki--buttonize-start nil)
(set-marker hywiki--buttonize-end nil)))
((not (equal (setq hywiki--range
(hywiki-highlight-word-get-range))
'(nil nil nil)))
(cl-destructuring-bind (_ start end)
hywiki--range
(if (and start end)
(progn
;; On a non-delimited HyWikiWord
(set-marker hywiki--buttonize-start start)
(set-marker hywiki--buttonize-end end)
(goto-char start)
(skip-chars-backward "-" (line-beginning-position))
t)
(setq hywiki--range nil)))))
(unless (hywiki-non-hook-context-p)
;; This first rehighlighting is needed to ensure
;; any wikiword before an inserted whitespace character is
;; properly highlighted when separating two words or after a
;; closing delimiter.
(save-excursion
(goto-char (max (1- (point)) (point-min)))
(hywiki--maybe-rehighlight-at-point))
(hywiki--maybe-rehighlight-at-point))))))
(setq hywiki--command-executed-flag nil)))
(defun hywiki-get-buttonize-characters ()
"Return a string of Org self-insert keys that have punctuation/symbol syntax.
These trigger HyWiki reference highlighting. Cache the string and
automatically invalidate it when `global-map' or `outline-mode-syntax-table'
changes."
(if (stringp hywiki--buttonize-characters-cache)
hywiki--buttonize-characters-cache
(let (key
cmd
key-cmds
result)
;; Org and other text mode self-insert-command bindings are just
;; remaps inherited from global-map. Create key-cmds list of
;; parsable (key . cmd) combinations where key may be a
;; (start-key . end-key) range of keys.
(map-keymap (lambda (key cmd) (setq key-cmds (cons (cons key cmd) key-cmds))) (current-global-map))
(with-syntax-table hywiki--org-mode-syntax-table
(setq hywiki--buttonize-characters-cache
(dolist (key-cmd key-cmds (apply #'string (seq-difference (nreverse result)
"-_*#:" #'=)))
(setq key (car key-cmd)
cmd (cdr key-cmd))
(when (eq cmd 'self-insert-command)
(cond ((and (characterp key)
(= (char-syntax key) ?.))
;; char with punctuation syntax
(setq result (cons key result)))
((and (consp key)
(characterp (car key))
(characterp (cdr key))
(<= (cdr key) 256))
;; ASCII char range, some of which has punctuation/symbol syntax
(dolist (k (number-sequence (car key) (cdr key)))
(when (memq (char-syntax k) '(?. ?_))
(setq result (cons k result)))))))))))))
(defun hywiki-non-hook-context-p ()
"Return non-nil when HyWiki command hooks should do nothing.
When used within a `post-command-hook', point must be moved back to
its location prior to the associated command run before this is called
since the command may have moved it off a HyWikiWord."
(or (minibuffer-window-active-p (selected-window))
;; (and (bound-and-true-p 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))))))
(defcustom hywiki-default-mode :pages
"Customizable initial mode setting for HyWiki minor mode.
HyWiki mode has three states, any one of which can be set as the default:
- :pages - highlight HyWikiWords in HyWiki pages only (Org files in
`hywiki-directory')
- :all - highlight HyWikiWords in all editable buffers except those
with a major mode in `hywiki-exclude-major-modes'.
- nil - no highlighting, the mode is disabled."
:type 'string
:group 'hyperbole-hywiki)
(defvar hywiki-mode nil
"Non-nil when the global hywiki minor mode is enabled.
Don't set this directly, instead call the function `hywiki-mode'
with the value you want as its argument. See the documentation for the
customization, `hywiki-default-mode', for valid values.")
(defun hywiki-mode-normalize (to-mode)
"Normalize `hywiki-mode' and TO-MODE values for `hywiki-mode' function.
See the documentation for the customization, `hywiki-default-mode', for
valid values."
;; Normalize `hywiki-default-mode' setting
(cond
((or (and (integerp hywiki-default-mode) (= hywiki-default-mode 1))
(memq hywiki-default-mode '(:all all t)))
(setq hywiki-default-mode :all))
((or (null hywiki-default-mode)
(and (integerp hywiki-default-mode) (<= hywiki-default-mode 0)))
(setq hywiki-default-mode nil))
(t ;; (> hywiki-default-mode 1)
(setq hywiki-default-mode :pages)))
;; Normalize `hywiki-mode' setting
(cond
((or (and (integerp hywiki-mode) (= hywiki-mode 1))
(memq hywiki-mode '(:all all t)))
;; Enable across all editable buffers
(setq hywiki-mode :all))
((or (null hywiki-mode)
(and (integerp hywiki-mode) (<= hywiki-mode 0)))
;; Disable mode flag
(setq hywiki-mode nil))
(t ;; (> hywiki-mode 1)
;; Enable in HyWiki page buffers only
(setq hywiki-mode :pages)))
;; Normalize `to-mode' and set mode
(when (eq to-mode 'toggle)
;; Toggle across all editable buffers
(setq to-mode (if hywiki-mode
nil
(or hywiki--prior-mode hywiki-default-mode :pages))))
(cond
((or (and (integerp to-mode) (= to-mode 1))
(memq to-mode '(:all all t)))
;; Enable across all editable buffers
(setq to-mode :all))
((or (null to-mode)
(and (integerp to-mode) (<= to-mode 0)))
;; Disable across all editable buffers
(setq to-mode nil))
(t ;; (> to-mode 1)
;; Enable in HyWiki page buffers only
(setq to-mode :pages))))
;;;###autoload
(define-minor-mode hywiki-mode
"Toggle HyWiki global minor mode with \\[hywiki-mode].
HyWiki minor mode automatically highlights and turns HyWikiWord
references into implicit buttons that either link to HyWiki pages
or activate typed referents such as bookmarks.
HyWiki minor mode has three states as tracked by the `hywiki-mode'
variable. See the documentation for the customization, `hywiki-default-mode',
for valid values.
HyWikiWord references may also include optional suffixes:
- a #section reference that links to a HyWiki page Org headline or
other outline file. Spaces in the headline must be converted
to dash characters for proper recognition;
- optionally followed by :L<line-number>:C<column-number>
where the column part is also optional. If a section is
given, the line number is relative to the section and the
section headline is line 1.
See the Info documentation at \"(hyperbole)HyWiki\".
\\{hywiki-mode-map}"
:global t
:lighter hywiki-mode-lighter
:keymap hywiki-mode-map
:group 'hyperbole-hywiki
;; Prevent definition of a custom-variable since it makes no sense to
;; customize this variable.
:variable hywiki-mode
(progn
;; Set mode and highlighting
(pcase arg
(:all (progn
;; Enable across all editable buffers
;; Need hyperbole-mode
(unless hyperbole-mode
(hyperbole-mode 1))
(hywiki-word-set-auto-highlighting hywiki--prior-mode arg)
(setq hywiki-mode arg)))
('nil (progn
;; Disable across all editable buffers.
;; Dehighlight HyWikiWords in this buffer when 'hywiki-mode' is
;; disabled and this is not a HyWiki page buffer. If this is a
;; HyWiki page buffer, then dehighlight when `hywiki-mode' is nil.
(hywiki-word-set-auto-highlighting hywiki--prior-mode arg)
(setq hywiki-mode arg)))
(:pages (progn
;; Enable in HyWiki page buffers only
;; Need hyperbole-mode
(unless hyperbole-mode
(hyperbole-mode 1))
(hywiki-word-set-auto-highlighting hywiki--prior-mode arg)
(setq hywiki-mode arg))))))
(defun hywiki-mode-around-advice (hywiki-mode-fn &optional to-mode)
(setq to-mode (hywiki-mode-normalize to-mode))
(unless (eq hywiki-mode to-mode)
(setq hywiki--prior-mode hywiki-mode))
(funcall hywiki-mode-fn to-mode))
(unless (advice-member-p #'hywiki-mode-around-advice #'hywiki-mode)
(advice-add 'hywiki-mode :around #'hywiki-mode-around-advice))
;;; ************************************************************************
;;; Public Implicit Button and Action Types
;;; ************************************************************************
(defun hywiki-display-referent-type (wikiword referent)
"Display WIKIWORD REFERENT, a cons of (<referent-type> . <referent-value>).
Function used to display is \"hywiki-display-<referent-type>\"."
(let ((referent-type (and (consp referent) (car referent))))
(unless (and referent-type (symbolp referent-type))
(error "(hywiki-display-referent-type): Referent type must be a symbol, not: referent-type = %S; referent = %S"
referent referent-type))
(let* ((referent-value (cdr referent))
(display-function (intern-soft (concat "hywiki-display-"
(symbol-name referent-type)))))