-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhyrolo.el
3703 lines (3366 loc) · 153 KB
/
hyrolo.el
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
;;; hyrolo.el --- Hierarchical, multi-file, easy-to-use record management system -*- lexical-binding: t; -*-
;;
;; Author: Bob Weiner
;;
;; Orig-Date: 7-Jun-89 at 22:08:29
;; Last-Mod: 18-Jan-25 at 22:45:52 by Bob Weiner
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; Copyright (C) 1991-2024 Free Software Foundation, Inc.
;; See the "HY-COPY" file for license information.
;;
;; This file is part of GNU Hyperbole.
;;; Commentary:
;;
;; This is Hyperbole's advanced rolo system, HyRolo, for convenient
;; management of hierarchical, record-oriented information. Most
;; often this is used for contact management but it can quickly be
;; adapted to most any record-oriented lookup task, for fast, full-text
;; retrieval.
;;
;; See all the autoloaded functions herein for interactive commands.
;; See the Info manual entry "(hyperbole)HyRolo" for usage information.
;;
;; Note that for Markdown files, HyRolo supports only the modern standard
;; of headlines that start with '#' characters, not the older, setext
;; style of underlining headers with '=' or '-' characters.
;;; Code:
;;; ************************************************************************
;;; Other required Elisp libraries
;;; ************************************************************************
(require 'custom) ;; For `defface'
(require 'hversion)
(require 'hmail)
(require 'hsys-consult)
(require 'hsys-org) ;; For `hsys-org-cycle-bob-file-list'
(require 'hypb) ;; For `hypb:mail-address-regexp' and `hypb:add-to-invisibility-spec'
(eval-when-compile
`(hyrolo-install-markdown-mode))
(require 'outline)
(require 'package)
(require 'reveal)
;; Avoid any potential library name conflict by giving the load directory.
(require 'set (expand-file-name "set" hyperb:dir))
(require 'sort)
(require 'xml)
(declare-function kotl-mode:to-valid-position "kotl/kotl-mode")
;; Quiet byte compiler warnings for these free variables.
(eval-when-compile
(unless (require 'bbdb nil t)
(defvar bbdb-file nil))
(unless (require 'google-contacts nil t)
(defvar google-contacts-buffer-name nil))
(require 'kview nil t))
;;; ************************************************************************
;;; Public declarations
;;; ************************************************************************
(declare-function google-contacts "ext:google-contacts")
(declare-function google-contacts-add-margin-to-text "ext:google-contacts")
(declare-function google-contacts-build-node-list "ext:google-contacts")
(declare-function google-contacts-data "ext:google-contacts")
(declare-function google-contacts-make-buffer "ext:google-contacts")
(declare-function google-contacts-margin-element "ext:google-contacts")
(declare-function google-contacts-oauth-token "ext:google-contacts")
(declare-function helm-org-rifle-files "ext:helm-org-rifle")
(declare-function helm-org-rifle-org-directory "ext:helm-org-rifle")
(declare-function helm-org-rifle-show-full-contents "ext:helm-org-rifle")
(declare-function kotl-mode:to-valid-position "kotl/kotl-mode")
(declare-function org-fold-initialize "org-fold")
(declare-function org-fold-core-set-folding-spec-property "org-fold")
(declare-function outline-apply-default-state "outline")
(declare-function xml-node-child-string "ext:google-contacts")
(declare-function xml-node-get-attribute-type "ext:google-contacts")
(declare-function find-library-name "find-func")
(declare-function hbut:to-key-src "hbut")
(declare-function hui:hbut-act "hui")
(declare-function ibut:at-p "hbut")
(declare-function kcell-view:indent "kotl/kview")
(declare-function kcell-view:level "kotl/kview")
(declare-function hmouse-pulse-line "hui-window")
(declare-function hpath:find "hpath")
(declare-function hpath:expand-list "hpath")
(declare-function hbut:get-key-src "hbut")
(declare-function org-outline-level "org")
(defvar org-directory) ; "org.el"
(defvar org-mode-map) ; "org-keys.el"
(defvar org-mode-syntax-table) ; "org.el"
(defvar org-outline-regexp) ; "org.el"
(defvar org-outline-regexp-bol) ; "org.el"
(defvar org-agenda-buffer-tmp-name) ; "org-agenda.el"
(defvar google-contacts-buffer-name) ; "ext:google-contacts.el"
(defvar hbut:source-prefix) ; "hbut.el"
;; markdown-mode.el
(defvar markdown-regex-header)
(defvar markdown-hide-markup)
(defvar markdown-nested-imenu-heading-index)
(defvar markdown-indent-function)
(defvar markdown-make-gfm-checkboxes-buttons)
(declare-function markdown--edit-indirect-after-commit-function "ext:markdown-mode")
(declare-function markdown--indent-region "ext:markdown-mode")
(declare-function markdown--inhibit-electric-quote "ext:markdown-mode")
(declare-function markdown-adaptive-fill-function "ext:markdown-mode")
(declare-function markdown-beginning-of-defun "ext:markdown-mode")
(declare-function markdown-end-of-defun "ext:markdown-mode")
(declare-function markdown-fill-forward-paragraph "ext:markdown-mode")
(declare-function markdown-fill-paragraph "ext:markdown-mode")
(declare-function markdown-gfm-checkbox-after-change-function "ext:markdown-mode")
(declare-function markdown-imenu-create-flat-index "ext:markdown-mode")
(declare-function markdown-imenu-create-nested-index "ext:markdown-mode")
(declare-function markdown-line-is-reference-definition-p "ext:markdown-mode")
(declare-function markdown-live-preview-if-markdown "ext:markdown-mode")
(declare-function markdown-live-preview-remove-on-kill "ext:markdown-mode")
(declare-function markdown-make-gfm-checkboxes-buttons "ext:markdown-mode")
;; This next function is replaced by `hyrolo-outline-function'
;; within `hyrolo-cache-set-major-mode'.
(declare-function markdown-outline-level "ext:markdown-mode")
(declare-function markdown-pipe-at-bol-p "ext:markdown-mode")
(declare-function markdown-remove-gfm-checkbox-overlays "ext:markdown-mode")
(defvar google-contacts-expire-time)
(defvar google-contacts-history)
(defvar google-contacts-query-string)
(defvar helm-org-rifle-show-full-contents)
(defvar helm-org-rifle-show-level-stars)
(defvar hproperty:but-emphasize-flag)
(defvar org-fold-core-style)
(defvar org-link--link-folding-spec)
(defvar org-roam-directory)
(defvar plstore-cache-passphrase-for-symmetric-encryption)
(defvar reveal-auto-hide)
(defvar hyrolo--wconfig)
(defvar hyrolo-entry-group-number)
(defvar hyrolo-entry-trailing-space-group-number)
(defvar hyrolo-hdr-format)
(defvar hyrolo-hdr-regexp)
(defvar hyrolo-hdr-prefix-regexp)
(defvar hyrolo-match-regexp)
(defvar hyrolo-mode-map)
(defvar hyrolo-mode-prefix-map)
(defvar hyrolo-mode-syntax-table)
(defvar hyrolo-reveal-ignore-this-command)
;;; ************************************************************************
;;; Public variables
;;; ************************************************************************
(defvar hyrolo-boolean-only-flag nil
"Set to prevent HyRolo from displaying an error buffer when running tests.
Return a boolean only, indicating whether the test passed or not.
See usage in `hyrolo-any-file-type-problem-p'.")
(defconst hyrolo-markdown-suffix-regexp "md\\|markdown\\|mkd\\|mdown\\|mkdn\\|mdwn"
"Regexp matching Markdown file suffixes.")
(defcustom hyrolo-file-suffix-regexp (concat "\\.\\(kotl?\\|org\\|ou?tl\\|"
hyrolo-markdown-suffix-regexp "\\)$")
"File suffix regexp used to select files to search with HyRolo."
:type 'string
:group 'hyperbole-hyrolo)
(defvar hyrolo-auto-mode-alist
(list (cons (format "\\.\\(%s\\)$" hyrolo-markdown-suffix-regexp)
'hyrolo-markdown-mode)
'("\\.org$" . hyrolo-org-mode)
'("\\.ou?tl$" . hyrolo-outline-mode))
"Entries to prepend to `auto-mode-alist' to invoke file modes used by HyRolo.
Typically, these specialized modes speed loading of files used solely
for HyRolo text matches by avoiding the time-consuming initializations
their standard major modes perform.")
(defvar hyrolo-display-buffer "*HyRolo*"
"Buffer used to display set of last matching rolo entries.")
;; Need to define the group before the defcustom variable so moved it here.
(defgroup hyperbole-hyrolo nil
"Hyperbole Rolo hierarchical contact manager customizations."
:group 'hyperbole)
(defcustom hyrolo-google-contacts-flag t
"Non-nil means search Google Contacts on each hyrolo query.
The google-contact package must be loaded and a gpg encryption
executable must be found as well (for Oauth security)."
:type 'boolean
:group 'hyperbole-hyrolo)
(defcustom hyrolo-file-list nil
"List of files containing hyrolo entries.
The first file should be a user-specific hyrolo file, typically in the home
directory and must have a suffix of either .org (Org mode) or .otl (Emacs
Outline mode). Other files in the list may use suffixes of .org, .otl, .md
\(Markdown mode) or .kotl (Koutline mode).
A hyrolo-file consists of:
(1) an optional header beginning with and ending with a line which matches
`hyrolo-hdr-regexp';
(2) one or more rolo entries which each begin with
`hyrolo-hdr-and-entry-regexp' and may be nested."
:type '(repeat file)
:initialize #'custom-initialize-default
:set #'hyrolo-set-file-list
:group 'hyperbole-hyrolo)
(defun hyrolo-file-list-changed (symbol set-to-value operation _where)
"Watch function for variable `hyrolo-file-list'.
Function is called with 4 arguments: (SYMBOL SET-TO-VALUE OPERATION WHERE)."
(if (memq operation '(let unlet)) ;; not setting global value
(hyrolo-let-file-list symbol set-to-value)
(hyrolo-set-file-list symbol set-to-value)))
;; This next line is needed to invoke `hyrolo-set-file-list' when
;; `hyrolo-file-list' is changed via `setq' or `let' rather than
;; `customize-set-variable'.
(add-variable-watcher 'hyrolo-file-list #'hyrolo-file-list-changed)
(defconst hyrolo-hdr-format
(concat
"===============================================================================\n"
"%s\n"
"===============================================================================\n")
"Header to insert before a file's first entry match when file has no header.
Used with one argument, the file name.")
(defconst hyrolo-hdr-regexp "^==="
"Regular expression to match the first and last lines of hyrolo file headers.
This header is inserted into hyrolo-display-buffer before any entries from the
file are added.")
(defvar hyrolo-entry-group-number 1
"Group number whose length represents the level of any entry matched.
See `hyrolo-hdr-and-entry-regexp'")
(defvar hyrolo-entry-trailing-space-group-number 2
"Group number within `hyrolo-hdr-and-entry-regexp' containing trailing space.")
(defvar hyrolo-hdr-prefix-regexp
(concat hyrolo-hdr-regexp
"\\|^" (if (boundp 'hbut:source-prefix) hbut:source-prefix "@loc> ")
"\\|")
"Regexp to prefix to `hyrolo-hdr-and-entry-regexp' and `outline-regexp'.
It must not contain any parenthesized match groupings.")
(defvar hyrolo-entry-regexp "^\\([*\^L]+\\)\\([ \t\n\r]+\\)"
"Regular expression to match the beginning of a HyRolo entry.
This pattern must match the beginning of a line.
`hyrolo-entry-group-number' must capture the entry's level in the
hierarchy. `hyrolo-entry-trailing-space-group-number' must capture
the entire single line whitespace following the entry hierarchy
level.")
(defvar hyrolo-hdr-and-entry-regexp
(concat hyrolo-hdr-prefix-regexp hyrolo-entry-regexp)
"Regular expression to match the beginning of a HyRolo file header or entry.
This pattern must match the beginning of a line.")
(defcustom hyrolo-date-format "%m/%d/%Y"
"Format of date string used in HyRolo automatic date stamps.
An empty string disables adding or updating HyRolo dates.
Default appearance is MM/DD/YYYY. See documentation of the function
`format-time-string' for format options."
:type 'string
:group 'hyperbole-hyrolo)
(defvar hyrolo-display-format-function
(lambda (entry)
(concat (replace-regexp-in-string "[ \t\n\r]+\\'" "" entry nil t) "\n"))
"*Function of one argument which modifies the string for display.
The argument is a rolo entry string.")
(defcustom hyrolo-email-format "%s\t\t<%s>"
"Format string to use when adding an entry with e-mail addr from a mail msg.
It must contain a %s indicating where to put the entry name and a second
%s indicating where to put the e-mail address."
:type 'string
:group 'hyperbole-hyrolo)
(defvar hyrolo-entry-name-regexp "[-_a-zA-Z0-9@.]+\\( ?, ?[-_a-zA-Z0-9@.]+\\)?"
"*Regexp matching a hyrolo entry name.
The match is after matching to `hyrolo-hdr-and-entry-regexp'.")
(defcustom hyrolo-find-file-function #'find-file
"Function to interactively display a `hyrolo-file-list' file for editing.
Use the `hyrolo-edit' function instead to edit a new or existing entry."
:type 'function
:group 'hyperbole-hyrolo)
(defcustom hyrolo-find-file-noselect-function #'find-file-noselect
"Function used by HyRolo to read `hyrolo-file-list' files into Emacs."
:type 'function
:group 'hyperbole-hyrolo)
(defvar hyrolo-next-match-function #'hyrolo-next-regexp-match
"Value is the function to find next match within a HyRolo file.
Must take one argument, `match-pattern', a regular expression.
Must leave point at the end of the match and return the start position
of the match or nil when no match.")
(defvar hyrolo-add-hook nil
"Hook run when a HyRolo item is added.")
(defvar hyrolo-edit-hook nil
"Hook run when a HyRolo item is edited.")
(declare-function hyrolo-fgrep-logical "hyrolo-logic")
(defvar hproperty:highlight-face)
;; '("~/.rolo.otl" "~/.rolo.org")
(defcustom hyrolo-highlight-face 'match
"Face used to highlight rolo search matches."
:type 'face
:initialize #'custom-initialize-default
:group 'hyperbole-hyrolo)
(defcustom hyrolo-kill-buffers-after-use nil
"Non-nil means kill rolo file buffers after searching them for entries.
Only unmodified buffers are killed."
:type 'boolean
:group 'hyperbole-hyrolo)
(defvar hyrolo-reveal-ignore-this-command nil
"Set this non-nil in any command that should ignore `hyrolo-reveal-mode'.")
(defcustom hyrolo-save-buffers-after-use t
"Non-nil means save rolo file after an entry is killed."
:type 'boolean
:group 'hyperbole-hyrolo)
;; Insert or update the entry date each time an entry is added or edited.
(add-hook 'hyrolo-add-hook #'hyrolo-set-date)
(add-hook 'hyrolo-edit-hook #'hyrolo-edit-date)
(defvar hyrolo-yank-reformat-function #'ignore
"*A function of two arguments, START and END, invoked after a `hyrolo-yank'.
It should reformat the region given by the arguments to some preferred style.
Default value is to perform no reformatting.")
;;; ************************************************************************
;;; Private variables
;;; ************************************************************************
(defvar hyrolo--expanded-file-list nil
"List of hyrolo files after directory and file wildcard expansions.
HyRolo sets this internally; never set it outside of this library.")
(defconst hyrolo-match-regexp nil
"Last regular expression used to search the hyrolo.
Nil before a search is done, including after a logical search is done.
String search expressions are converted to regular expressions.")
(defvar hyrolo--wconfig nil
"Saves frame's window configuration prior to a hyrolo search.")
(defvar hyrolo-mode-syntax-table nil
"Syntax table used while in hyrolo match mode.")
(if hyrolo-mode-syntax-table
()
(setq hyrolo-mode-syntax-table (make-syntax-table text-mode-syntax-table)))
(defvar hyrolo-mode-map nil
"Keymap for the hyrolo display match buffer.")
(defvar hyrolo-mode-prefix-map nil
"Keymap for hyrolo display match buffer bindings with a prefix, typically \\`C-c'.")
;;; ************************************************************************
;;; Commands
;;; ************************************************************************
;;;###autoload
(defun hyrolo-add (name &optional file)
"Add a new entry in personal rolo for NAME.
Last name first is best, e.g. \"Smith, John\".
With prefix argument, prompts for optional FILE to add entry within.
NAME may be of the form: parent/child to insert child below a parent
entry which begins with the parent string."
(interactive
(progn
(unless (fboundp 'mail-fetch-field)
(require 'mail-utils))
(let* ((lst (hyrolo-name-and-email))
(name (car lst))
(email (car (cdr lst)))
(entry (read-string "Name to add to rolo: "
(or name email))))
(list (if (and email name
(string-match (concat "\\`" (regexp-quote entry)) name))
(format hyrolo-email-format entry email) entry)
current-prefix-arg))))
(when (or (not (stringp name)) (string-equal name ""))
(error "(hyrolo-add): Invalid name: `%s'" name))
(when (and (called-interactively-p 'interactive) file)
(setq file (completing-read "File to add to: "
(mapcar #'list (hyrolo-get-file-list)))))
(unless file
(setq file (car (hyrolo-get-file-list))))
(cond ((and file (or (not (stringp file)) (string-equal file "")))
(error "(hyrolo-add): Invalid file: `%s'" file))
((and (file-exists-p file) (not (file-readable-p file)))
(error "(hyrolo-add): File not readable: `%s'" file))
((not (file-writable-p file))
(error "(hyrolo-add): File not writable: `%s'" file)))
(set-buffer (or (get-file-buffer file)
(hyrolo-find-file-noselect file)))
(when (called-interactively-p 'interactive)
(message "Locating insertion point for `%s'..." name))
(let ((parent "")
(level "")
(entry-regexp hyrolo-entry-regexp)
end)
(hyrolo-widen)
(goto-char (point-min))
;; If name includes slash level separator character, walk down
;; existing matching tree of entries to find insertion point.
(while (string-match "\\`[^\]\[/<>{}\"]*/" name)
(setq end (1- (match-end 0))
parent (substring name 0 end)
name (substring name (min (1+ end) (length name))))
(if (re-search-forward
(concat entry-regexp (regexp-quote parent) "\\s-") nil t)
(progn (setq level (match-string-no-properties hyrolo-entry-group-number))
(goto-char (match-beginning 0)))
(error "(hyrolo-add): Insertion failed, `%s' parent entry not found in \"%s\""
parent file)))
(when (looking-at hyrolo-entry-regexp)
(narrow-to-region (point) (progn (hyrolo-to-entry-end t) (point))))
(let* ((name-level (concat level "*"))
(level-len (length name-level))
(first-char (aref name 0))
(entry "")
(entry-spc "")
(entry-level-len)
(match)
(again t))
;; Speed up entry insertion point location if this is a first-level
;; entry by moving to an entry with the same (or nearest) first character
;; to that of `name'.
(if (and (= level-len 1)
(equal entry-regexp hyrolo-entry-regexp))
(let ((case-fold-search))
(goto-char (point-min))
(if (re-search-forward (concat entry-regexp
(regexp-quote (char-to-string first-char)))
nil t)
(goto-char (match-beginning 0))
(goto-char (point-max))
(when (and (> first-char ?0)
(re-search-backward
(concat "^\\*[ \t]+["
(substring
"0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz"
0 (min (- first-char ?0) 62))
"])")
nil t))
(goto-char (match-end 0))
(hyrolo-to-entry-end t)
;; Now at the insertion point, immediately after
;; the last existing entry whose first character
;; is less than that of `name'. Setting `again'
;; to nil prevents further searching for an
;; insertion point.
(setq again nil))))
(goto-char (point-min)))
(when again
(goto-char (point-min)))
(while (and again (re-search-forward entry-regexp nil 'end))
(setq entry-level-len (length (match-string-no-properties hyrolo-entry-group-number)))
(if (/= entry-level-len level-len)
(hyrolo-to-entry-end t)
(setq entry-spc (match-string-no-properties hyrolo-entry-trailing-space-group-number)
entry (buffer-substring-no-properties (point)
(save-excursion
(re-search-forward hyrolo-entry-name-regexp nil t)
(point))))
(when (and (derived-mode-p 'markdown-mode)
(string-match "\\`[^#]*#+" entry-spc))
(setq entry-spc (substring entry-spc (length (match-string 0 entry-spc)))))
(cond ((string-lessp entry name)
(hyrolo-to-entry-end t))
((string-lessp name entry)
(setq again nil) (beginning-of-line))
(t ;; found existing entry matching name
(setq again nil match t)))))
(setq buffer-read-only nil)
(unless match
(unless (zerop (current-column))
(insert "\n"))
(insert (concat level "*")
(if (string-equal entry-spc "") " " entry-spc)
name "\n")
(backward-char 1))
;; hyrolo-to-buffer may move point from its desired location, so
;; restore it.
(let ((opoint (point)))
(hyrolo-widen)
(hyrolo-to-buffer (current-buffer))
(goto-char opoint))
(when (derived-mode-p 'kotl-mode)
(kotl-mode:to-valid-position))
(set-auto-mode t)
(run-hooks 'hyrolo-add-hook)
(when (called-interactively-p 'interactive)
(message "Edit entry at point.")))))
(defun hyrolo-at-tags-p (&optional at-tag-flag)
"Return non-nil if point is in a HyRolo buffer and at Org tags."
(and (or at-tag-flag (hsys-org-at-tags-p)
;; Non-highlighted Org tags in `hyrolo-display-buffer'
(and (save-excursion
(beginning-of-line)
(looking-at org-tag-line-re))
(>= (point) (match-beginning 1))
(< (point) (match-end 1))))
(or (string-prefix-p "*HyRolo" (buffer-name))
(and (hypb:buffer-file-name)
(apply #'derived-mode-p '(org-mode org-agenda-mode))
(member (hypb:buffer-file-name) (hyrolo-get-file-list))
t))))
;;;###autoload
(defun hyrolo-consult-grep (&optional regexp max-matches path-list)
"Interactively search paths with a consult package grep command.
Search for optional REGEXP up to MAX-MATCHES in PATH-LIST or `hyrolo-file-list'.
Use ripgrep (rg) if found, otherwise, plain grep. Initialize search with
optional REGEXP and interactively prompt for changes. Limit matches
per file to the absolute value of MAX-MATCHES, if given and not 0. If
0, match to headlines only (lines that start with a '^[*#]+[ \t]+' regexp)."
(interactive "i\nP")
(let* ((grep-includes (concat "--include *.kot --include *.kotl"
" --include *.md --include *.markdown --include *.mkd --include *.mdown --include *.mkdn --include *.mdwn"
" --include *.org --include *.otl --include *.outl"))
(ripgrep-globs "--glob *.{kot,kotl,md,markdown,mkd,mdown,mkdn,mdwn,org,otl,outl}"))
(hsys-consult-grep grep-includes ripgrep-globs
regexp max-matches (or path-list hyrolo-file-list))))
;;;###autoload
(defun hyrolo-display-matches (&optional display-buf return-to-buffer)
"Display optional DISPLAY-BUF buffer of previously found rolo matches.
If DISPLAY-BUF is nil, use the value in `hyrolo-display-buffer'.
Second arg RETURN-TO-BUFFER is the buffer to leave point within
after the display."
(interactive)
(unless display-buf
(setq display-buf (get-buffer hyrolo-display-buffer)))
(unless display-buf
(error "(hyrolo-display-matches): Search the rolo first"))
;; Save current window configuration if rolo match buffer is not
;; displayed in one of the windows already.
(or
;; Handle both Emacs V18 and V19 versions of get-buffer-window.
(condition-case ()
(get-buffer-window display-buf (selected-frame))
(error (get-buffer-window display-buf)))
(setq hyrolo--wconfig (current-window-configuration)))
(hyrolo-to-buffer display-buf)
(when (fboundp 'hproperty:but-create)
(hproperty:but-create))
(hyrolo-shrink-window)
(goto-char (point-min))
(set-buffer-modified-p nil)
(setq buffer-read-only t)
(run-hooks 'hyrolo-display-hook)
;; Leave point in match buffer unless a specific RETURN-TO-BUFFER has
;; been specified. Use {q} to quit and restore display.
(when return-to-buffer
(hyrolo-to-buffer return-to-buffer)))
;;;###autoload
(defun hyrolo-edit (&optional name file-or-buf)
"Edit a hyrolo entry matching NAME from FILE-OR-BUF.
With prefix argument, prompt for optional FILE-OR-BUF from `hyrolo-file-list',
within which to locate entry. With no NAME arg, simply display
FILE-OR-BUF or the first entry in `hyrolo-file-list' in an editable
mode. NAME may be of the form: parent/child to edit child below
a parent entry which begins with the parent string."
(interactive "sEdit rolo entry named: \nP")
(when (string-empty-p name)
(setq name nil))
(when (and name (not (stringp name)))
(error "(hyrolo-edit): Invalid name: `%s'" name))
(let* ((found-point)
(all-files-or-bufs (hyrolo-get-file-list))
(file-or-buf-list (if file-or-buf (list file-or-buf) all-files-or-bufs)))
(when (and (called-interactively-p 'interactive) current-prefix-arg)
(setq file-or-buf
(if (cadr all-files-or-bufs) ;; length > 1
(car all-files-or-bufs)
(completing-read "File of entry to edit: "
(mapcar #'list all-files-or-bufs)))))
(unless file-or-buf
(setq file-or-buf (car file-or-buf-list)))
(if (or (null name)
(setq found-point (hyrolo-to name (list file-or-buf))))
(cond ((stringp file-or-buf)
(unless (file-writable-p file-or-buf)
(error "(hyrolo-edit): File not writable: `%s'" file-or-buf))
(hpath:find file-or-buf)
(setq buffer-read-only nil)
(set-auto-mode t))
((bufferp file-or-buf)
(unless (buffer-live-p file-or-buf)
(error "(hyrolo-edit): Buffer is not live: `%s'" file-or-buf))
(when (hyrolo-to-buffer file-or-buf)
(barf-if-buffer-read-only)))
(t (error "(hyrolo-edit): Second argument must be a file or buffer, not: `%s'" file-or-buf)))
(message "(hyrolo-edit): `%s' not found." name)
(beep)
(hyrolo-to-buffer (or (get-buffer file-or-buf)
(hyrolo-find-file-noselect file-or-buf)))
(setq buffer-read-only nil))
(when name
(hyrolo-widen)
;; hyrolo-to-buffer may have moved point from its desired location, so
;; restore it.
(when found-point
(goto-char found-point)
(set-auto-mode t)
(hmouse-pulse-line))
(when (derived-mode-p 'kotl-mode)
(kotl-mode:to-valid-position))
(unless (get-text-property 0 'hyrolo-line-entry name)
;; Run hooks like adding a date only when handling a
;; delimited (rather than single-line) entry.
(run-hooks 'hyrolo-edit-hook)))))
(defun hyrolo-edit-entry ()
"Edit the source entry of the hyrolo match buffer entry at point.
Return entry name, if any, otherwise, trigger an error."
(interactive)
(hyrolo-funcall-match
(lambda ()
(let* ((name-and-src (hyrolo-name-at-p))
(name (car name-and-src))
(src (cdr name-and-src)))
(if name
(progn (cond ((and (boundp 'bbdb-file) (stringp bbdb-file) (equal src (expand-file-name bbdb-file)))
;; For now, can't edit an entry from the bbdb database, signal an error.
(error "(hyrolo-edit-entry): BBDB entries are not editable"))
((and (hyrolo-google-contacts-p) (equal src (get-buffer google-contacts-buffer-name)))
;; For now, can't edit an entry from Google Contacts, signal an error.
(error "(hyrolo-edit-entry): Google Contacts entries are not editable"))
(src
(hyrolo-edit name src)
name)
(t
(error "(hyrolo-edit-entry): Move to an entry to edit it"))))
(error "(hyrolo-edit-entry): Move to an entry to edit it"))))
t))
(defun hyrolo-expand-path-list (paths)
"Expand and return non-nil PATHS's dirs, file variables and file wildcards.
Return only existing, readable files. If PATHS is nil, return a
default set of hyrolo files to use.
Single ${env-or-lisp-variable} references are resolved within
each path using `hpath:expand'; this also expands paths to
absolute paths. Then directories are expanded into the files
they contain that match `hyrolo-file-suffix-regexp'. Then, if
`find-file-wildcards' is non-nil (the default), any files
containing [char-matches] or * wildcards are expanded to their
matches."
(if paths
(hpath:expand-list paths hyrolo-file-suffix-regexp #'file-readable-p)
(delq nil
(list "~/.rolo.otl"
(if (and (boundp 'bbdb-file) (stringp bbdb-file)) bbdb-file)
(when (hyrolo-google-contacts-p) google-contacts-buffer-name)))))
;;;###autoload
(defun hyrolo-fgrep (string &optional max-matches hyrolo-file count-only headline-only no-display)
"Display rolo entries matching STRING or a logical match expression.
Return count of matches.
To a maximum of optional prefix arg MAX-MATCHES, in file(s) from optional
HYROLO-FILE or `hyrolo-file-list'. Default is to find all matching entries.
Each entry is displayed with all of its sub-entries. Optional COUNT-ONLY
non-nil skips retrieval of matching entries. Optional HEADLINE-ONLY searches
only the first line of entries, not the full text. Optional NO-DISPLAY non-nil
retrieves entries but does not display them.
Nil value of MAX-MATCHES means find all entries that match, t value means find
all matching entries but omit file headers, negative values mean find up to the
inverse of that number of matching entries and omit file headers.
Return number of entries matched. See also documentation for the variable
`hyrolo-file-list' and the function `hyrolo-fgrep-logical' for documentation
on the logical sexpression matching."
(interactive "sFind rolo string (or logical sexpression): \nP")
(setq string (string-trim string "\"" "\""))
(let ((total-matches 0))
(if (string-match-p "\(\\(r-\\)?\\(and\\|or\\|xor\\|not\\)\\>" string)
(progn
;; Search string contains embedded logic operators.
;; First try to match logical sexpression within a single
;; subentry to minimize entries displayed. If no match,
;; then match across ancestors and descendants.
(when (zerop (setq total-matches (hyrolo-fgrep-logical string count-only nil t)))
(hyrolo-fgrep-logical string count-only t t)))
(setq total-matches (hyrolo-grep (regexp-quote string)
max-matches hyrolo-file count-only headline-only no-display)))
(if (called-interactively-p 'interactive)
(message "%s matching entr%s found in HyRolo."
(if (= total-matches 0) "No" total-matches)
(if (= total-matches 1) "y" "ies")))
total-matches))
;;;###autoload
(defun hyrolo-find-file (&optional file find-function &rest args)
"Find an optional FILE with FIND-FUNCTION and rest of ARGS.
When called interactively, select from the list of files referenced
by `hyrolo-file-list' unless given a prefix argument, in which case
use the first file generated by the list.
FIND-FUNCTION must return the buffer of the file found but need not
select it."
(interactive "P")
(let ((all-files (hyrolo-get-file-list)))
(when (or (called-interactively-p 'interactive)
(null file))
(if (or (not (cadr all-files)) ;; length <= 1
current-prefix-arg)
(setq file (car all-files))
(setq file (completing-read "Edit HyRolo file: "
(mapcar #'list all-files)))))
(when (stringp file)
(let (buf)
(prog1 (setq buf (apply (or find-function hyrolo-find-file-function) file args))
(when buf
(with-current-buffer buf
(when (equal outline-regexp (default-value 'outline-regexp))
;; Prevent matching to *word* at the beginning of
;; lines and hanging hyrolo search functions. Note this
;; change adds one to the default `outline-level' function,
;; so `hyrolo-outline-level' overrides that as well
;; to get the correct calculation. -- rsw, 2023-11-17
(setq-local outline-regexp "\\([*\^L]+\\)\\([ \t\n\r]\\)"
outline-level #'hyrolo-outline-level))
(setq buffer-read-only nil))))))))
;;;###autoload
(defun hyrolo-find-file-noselect (&optional file)
"HyRolo function to read a FILE in without selecting it.
It uses the setting of `hyrolo-find-file-noselect-function' and
overrides file major modes with any settings in `hyrolo-auto-mode-alist'."
;; In a Lisp program, if you want to be sure of accessing a file’s
;; contents literally, you should create a temporary buffer and then read
;; the file contents into it using ‘insert-file-contents-literally’.
(let ((auto-mode-alist (append hyrolo-auto-mode-alist auto-mode-alist))
(enable-local-variables))
(hyrolo-find-file file hyrolo-find-file-noselect-function)))
;; This wraps forward-visible-line, making its ARG optional, making
;; its calling convention match that of forward-line and making it
;; usable as an argument to `sort-subr' in `hyrolo-sort-lines' to fix a
;; sorting issue visible in Emacs `sort-lines'.
(defun hyrolo-forward-visible-line (&optional arg)
"Move forward by optional ARG lines (default = 1).
Ignore currently invisible newlines only.
If ARG is negative, move backward -ARG lines.
If ARG is zero, move to the beginning of the current line."
(unless arg
(setq arg 1))
(forward-visible-line arg))
(defun hyrolo-get-file-list ()
"Return the current expanded list of HyRolo search files."
(if (equal hyrolo-file-list (symbol-value 'hyrolo-file-list))
(or hyrolo--expanded-file-list hyrolo-file-list)
;; lexical-binding is enabled and there is a local binding of
;; `hyrolo-file-list', so expand it.
(hyrolo-expand-path-list hyrolo-file-list)))
;;;###autoload
(defun hyrolo-grep (regexp &optional max-matches hyrolo-file-or-bufs count-only headline-only no-display)
"Display HyRolo entries matching REGEXP and return count of matches.
To a maximum of prefix arg MAX-MATCHES, in buffer(s) from
optional HYROLO-FILE-OR-BUFS or `hyrolo-get-file-list'. Default
is to find all matching entries. Each entry is displayed with
all of its sub-entries. Optional COUNT-ONLY non-nil means don't
retrieve and don't display matching entries. Optional
HEADLINE-ONLY searches only the first line of entries, not the
full text. Optional NO-DISPLAY non-nil retrieves entries but
does not display.
Nil value of MAX-MATCHES means find all entries that match, t value means find
all matching entries but omit file headers, negative values mean find up to the
inverse of that number of matching entries and omit file headers.
Return number of entries matched. See also documentation for the variable
\`hyrolo-file-list'."
(interactive "sFind rolo regular expression: \nP")
(unless (or (integerp max-matches) (memq max-matches '(nil t)))
(setq max-matches (prefix-numeric-value max-matches)))
(let ((files-or-bufs
(cond ((null hyrolo-file-or-bufs) (hyrolo-get-file-list))
((listp hyrolo-file-or-bufs) hyrolo-file-or-bufs)
((list hyrolo-file-or-bufs))))
(case-fold-search t)
(display-buf (unless count-only
(hyrolo-set-display-buffer)))
(total-matches 0)
(num-matched 0)
(inserting (or (eq max-matches t)
(and (integerp max-matches) (< max-matches 0))))
(file-or-buf))
(unless count-only
(setq buffer-read-only nil)
(unless inserting
(erase-buffer))
(hyrolo--cache-initialize))
(while (and (setq file-or-buf (car files-or-bufs))
(or (not (integerp max-matches))
(< total-matches (max max-matches (- max-matches)))))
(setq files-or-bufs (cdr files-or-bufs)
num-matched (cond ((and (featurep 'bbdb) (equal file-or-buf bbdb-file))
(hyrolo-bbdb-grep-file file-or-buf regexp max-matches count-only))
((and (hyrolo-google-contacts-p) (equal file-or-buf google-contacts-buffer-name))
(hyrolo-retrieve-google-contacts (regexp-quote regexp))
(hyrolo-google-contacts-grep-file file-or-buf regexp max-matches count-only))
(t (hyrolo-grep-file file-or-buf regexp max-matches count-only headline-only)))
total-matches (+ total-matches num-matched))
(when (integerp max-matches)
(setq max-matches
(if (>= max-matches 0)
(- max-matches num-matched)
(+ max-matches num-matched)))))
(unless (or count-only (= total-matches 0))
(hyrolo--cache-post-display-buffer)
(unless (or no-display inserting)
(hyrolo-display-matches display-buf)))
(when (called-interactively-p 'interactive)
(message "%s matching entr%s found in HyRolo."
(if (= total-matches 0) "No" total-matches)
(if (= total-matches 1) "y" "ies")))
total-matches))
;;;###autoload
(defun hyrolo-grep-or-fgrep (&optional arg)
"Grep over `hyrolo-file-list' and display the results as rolo entries.
With optional prefix ARG, do an fgrep string match instead of a regexp match."
(interactive "P")
(call-interactively (if arg 'hyrolo-fgrep 'hyrolo-grep)))
(defun hyrolo-isearch (&optional arg)
"Interactively search forward for the next occurrence of current match string.
Then add characters to further narrow the search. With optional prefix ARG
non-nil, search for the current match regular expression rather than string."
(interactive "P")
(if arg
(hyrolo-isearch-for-regexp hyrolo-match-regexp t)
(hyrolo-verify)
(if hyrolo-match-regexp
(progn (setq unread-command-events
(append unread-command-events (string-to-list (regexp-quote hyrolo-match-regexp))))
(let ((case-fold-search t))
(isearch-forward)))
(error (substitute-command-keys "(hyrolo-isearch): Use {\\[hyrolo-grep-or-fgrep]} to do an initial search")))))
(defun hyrolo-isearch-regexp (&optional arg)
"Interactively search forward for the next occurrence of current match string.
Then add characters to further narrow the search. With optional prefix ARG
non-nil, search for the current match regular expression rather than string."
(interactive "P")
(if arg
(hyrolo-isearch-for-regexp hyrolo-match-regexp t)
(hyrolo-isearch)))
;;;###autoload
(defun hyrolo-kill (name &optional file)
"Kill a rolo entry given by NAME within `hyrolo-file-list'.
With prefix argument, prompts for optional FILE to locate entry within.
NAME may be of the form: parent/child to kill child below a parent entry
which begins with the parent string.
Return t if entry is killed, nil otherwise."
(interactive "sKill rolo entry named: \nP")
(if (or (not (stringp name)) (string-equal name "") (string-match "\\*" name))
(error "(hyrolo-kill): Invalid name: `%s'" name))
(if (and (called-interactively-p 'interactive) current-prefix-arg)
(setq file (completing-read "Entry's File: "
(mapcar #'list (hyrolo-get-file-list)))))
(let ((file-list (if file (list file) (hyrolo-get-file-list)))
(killed))
(unless file
(setq file (car file-list)))
(save-excursion
(if (hyrolo-to name file-list)
(progn
(setq file (hypb:buffer-file-name))
(if (file-writable-p file)
(let ((kill-op
(lambda (start _level-len)
(kill-region
start (hyrolo-to-entry-end t))
(setq killed t)
(hyrolo-save-buffer)
(hyrolo-kill-buffer)))
(case-fold-search)
start end level-len)
(setq buffer-read-only nil)
(re-search-backward hyrolo-hdr-and-entry-regexp nil t)
(setq end (match-end 0))
(setq start (line-beginning-position)
level-len (length (buffer-substring-no-properties start end)))
(goto-char end)
(skip-chars-forward " \t")
(if (called-interactively-p 'interactive)
(let ((entry-line (buffer-substring-no-properties
(point)
(min (+ (point) 60)
(progn (end-of-line) (point))))))
(if (y-or-n-p (format "Kill `%s...'? " entry-line))
(progn
(funcall kill-op start level-len)
(message "Killed"))
(message "Aborted")))
(funcall kill-op start level-len)))
(message
"(hyrolo-kill): Entry found but file not writable: `%s'" file)
(beep)))
(message "(hyrolo-kill): `%s' not found." name)
(beep)))
killed))
(defun hyrolo-locate ()
"Interactively search for an entry beginning with a set of search characters."
(interactive)
(hyrolo-funcall-match
(lambda () (hyrolo-isearch-for-regexp hyrolo-hdr-and-entry-regexp nil))))
;; Adapted from `set-auto-mode' in "files.el" but greatly simplified.
(defun hyrolo-major-mode-from-file-name (name)
"Return `major-mode' function for file NAME from file name alone.
If no matching rule in `hyrolo-auto-mode-alist' or `auto-mode-alist'
or NAME is invalid, return nil."
(when (stringp name)
(let ((remote-id (file-remote-p name))
(case-insensitive-p (file-name-case-insensitive-p
name))
mode)
;; Remove backup-suffixes from file name.
(setq name (file-name-sans-versions name))
;; Remove remote file name identification.
(when (and (stringp remote-id)
(string-match (regexp-quote remote-id) name))
(setq name (substring name (match-end 0))))
(let ((auto-mode-alist (append hyrolo-auto-mode-alist auto-mode-alist)))
(while name
;; Find first matching alist entry.
(setq mode
(if case-insensitive-p
;; Filesystem is case-insensitive.
(let ((case-fold-search t))
(assoc-default name auto-mode-alist
'string-match))
;; Filesystem is case-sensitive.
(or
;; First match case-sensitively.
(let ((case-fold-search nil))
(assoc-default name auto-mode-alist
'string-match))
;; Fallback to case-insensitive match.
(and auto-mode-case-fold
(let ((case-fold-search t))
(assoc-default name auto-mode-alist
'string-match))))))
(if (and mode
(consp mode)
(cadr mode))
(setq mode (car mode)
name (substring name 0 (match-beginning 0)))
(setq name nil))))
mode)))
(defun hyrolo-mail-to ()
"Start composing mail addressed to the first e-mail address at or after point."
(interactive)
(let ((opoint (point)) ibut)
(skip-chars-backward "^ \t\n\r<>")
(if (and (re-search-forward hypb:mail-address-regexp nil t)
(goto-char (match-beginning 1))
(setq ibut (ibut:at-p)))
(hui:hbut-act ibut)