-
Notifications
You must be signed in to change notification settings - Fork 11
/
hypb.el
1219 lines (1100 loc) · 49.7 KB
/
hypb.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
;;; hypb.el --- Miscellaneous GNU Hyperbole support features -*- lexical-binding: t; -*-
;;
;; Author: Bob Weiner
;;
;; Orig-Date: 6-Oct-91 at 03:42:38
;; Last-Mod: 15-Dec-24 at 22:49:02 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:
;;; Code:
;;; ************************************************************************
;;; Other required Elisp libraries
;;; ************************************************************************
;; Load Org here for `org-fold-show-all'.
(eval-and-compile (mapc #'require '(compile hversion hact locate
cl-lib org package seq)))
;;; ************************************************************************
;;; Public declarations
;;; ************************************************************************
(defvar mh-e-RCS-id)
(defvar pm-version)
(declare-function helm-info "ext:helm")
(declare-function helm-apropos "ext:helm")
(declare-function devdocs-lookup "ext:devdocs")
(declare-function native-comp-available-p "comp.c")
;; interaction-log
(defvar ilog-buffer-name)
(defvar ilog-display-state)
(defvar ilog-idle-time)
(defvar ilog-insertion-timer)
(defvar ilog-print-lambdas)
(defvar ilog-self-insert-command-regexps)
(defvar ilog-truncation-timer)
(defvar interaction-log-mode)
(defvar interaction-log-mode-hook)
(declare-function ilog-note-buffer-change "ext:interaction-log")
(declare-function ilog-post-command "ext:interaction-log")
(declare-function ilog-record-this-command "ext:interaction-log")
(declare-function ilog-show-in-other-frame "ext:interaction-log")
(declare-function ilog-timer-function "ext:interaction-log")
(declare-function ilog-toggle-view "ext:interaction-log")
(declare-function ilog-truncate-log-buffer "ext:interaction-log")
(declare-function interaction-log-mode "ext:interaction-log")
(defvar hyperb:user-email) ; "hinit.el"
(declare-function hkey-either "hmouse-drv")
(declare-function hycontrol-frame-to-right-center "hycontrol")
;;; ************************************************************************
;;; Public variables
;;; ************************************************************************
(defconst hypb:help-buf-prefix "*Help: Hyperbole "
"Prefix attached to all native Hyperbole help buffer names.
This should end with a space.")
(defvar hypb:mail-address-mode-list
'(fundamental-mode prog-mode text-mode)
"List of major modes in which mail address implicit buttons are active.
Also active in any Decendent modes of those listed.")
(defconst hypb:mail-address-tld-regexp
(format "\\.%s\\'"
(regexp-opt
'("aero" "arpa" "asia" "biz" "cat" "com" "coop" "edu" "gov" "info"
"int" "jobs" "mil" "mobi" "museum" "name" "net" "org" "pro" "tel"
"travel" "uucp"
"ac" "ad" "ae" "af" "ag" "ai" "al" "am" "an" "ao" "aq"
"ar" "as" "at" "au" "aw" "ax" "az" "ba" "bb" "bd" "be" "bf" "bg" "bh"
"bi" "bj" "bl" "bm" "bn" "bo" "br" "bs" "bt" "bv" "bw" "by" "bz" "ca"
"cc" "cd" "cf" "cg" "ch" "ci" "ck" "cl" "cm" "cn" "co" "cr" "cu" "cv"
"cx" "cy" "cz" "de" "dj" "dk" "dm" "do" "dz" "ec" "ee" "eg" "eh" "er"
"es" "et" "eu" "fi" "fj" "fk" "fm" "fo" "fr" "ga" "gb" "gd" "ge" "gf"
"gg" "gh" "gi" "gl" "gm" "gn" "gp" "gq" "gr" "gs" "gt" "gu" "gw" "gy"
"hk" "hm" "hn" "hr" "ht" "hu" "id" "ie" "il" "im" "in" "io" "iq" "ir"
"is" "it" "je" "jm" "jo" "jp" "ke" "kg" "kh" "ki" "km" "kn" "kp" "kr"
"kw" "ky" "kz" "la" "lb" "lc" "li" "lk" "lr" "ls" "lt" "lu" "lv" "ly"
"ma" "mc" "md" "me" "mf" "mg" "mh" "mk" "ml" "mm" "mn" "mo" "mp" "mq"
"mr" "ms" "mt" "mu" "mv" "mw" "mx" "my" "mz" "na" "nc" "ne" "nf" "ng"
"ni" "nl" "no" "np" "nr" "nu" "nz" "om" "pa" "pe" "pf" "pg" "ph" "pk"
"pl" "pm" "pn" "pr" "ps" "pt" "pw" "py" "qa" "re" "ro" "rs" "ru" "rw"
"sa" "sb" "sc" "sd" "se" "sg" "sh" "si" "sj" "sk" "sl" "sm" "sn" "so"
"sr" "st" "su" "sv" "sy" "sz" "tc" "td" "tf" "tg" "th" "tj" "tk" "tl"
"tm" "tn" "to" "tp" "tr" "tt" "tv" "tw" "tz" "ua" "ug" "uk" "um" "us"
"uy" "uz" "va" "vc" "ve" "vg" "vi" "vn" "vu" "wf" "ws" "ye" "yt" "yu"
"za" "zm" "zw")
t))
"Regular expression of most common Internet top level domain names.")
(defconst hypb:mail-address-regexp
"\\([_a-zA-Z0-9][-_a-zA-Z0-9.!@+%]*@[-_a-zA-Z0-9.!@+%]+\\.[a-zA-Z0-9][-_a-zA-Z0-9]+\\)\\($\\|[^a-zA-Z0-9@%]\\)"
"Regexp with group 1 matching an Internet email address.")
(defcustom hypb:rgrep-command
;; Only the FreeBSD version of zgrep supports all of the grep
;; options that Hyperbole needs: -r, --include, and --exclude
(format "%sgrep -insIHr" (if (and (executable-find "zgrep")
(string-match-p "bsd" (shell-command-to-string "zgrep --version | head -1")))
"z" ""))
"*Grep command string and initial arguments to send to `hypb:rgrep' command.
It must end with a space."
:type 'string
:group 'hyperbole-commands)
;;; ************************************************************************
;;; Public functions
;;; ************************************************************************
;; Adapted from "subr.el" but doesn't add if ELEMENT already exists
;; Used in `kotl-mode', so autoload.
;;;###autoload
(defun hypb:add-to-invisibility-spec (element)
"Add ELEMENT to `buffer-invisibility-spec'.
See documentation for `buffer-invisibility-spec' for the kind of elements
that can be added.
If `buffer-invisibility-spec' isn't a list before calling this
function, `buffer-invisibility-spec' will afterwards be a list
with the value `(t ELEMENT)'. This means that if text exists
that invisibility values that aren't either t or ELEMENT, that
text will become visible."
(if (eq buffer-invisibility-spec t)
(setq buffer-invisibility-spec (list t)))
(unless (member element buffer-invisibility-spec)
(setq buffer-invisibility-spec
(cons element buffer-invisibility-spec))))
;;;###autoload
(defun hypb:activate-interaction-log-mode ()
"Configure and enable the interaction-log package for use with Hyperbole.
This displays a clean log of Emacs keys used and commands executed."
(interactive)
;; Ensure package is installed
(unless (package-installed-p 'interaction-log)
(package-install 'interaction-log))
;; Ensure interaction-log-mode is disabled to removes its command
;; hooks which are replaced below.
(require 'interaction-log)
(interaction-log-mode 0)
;; Optional binding you can enable to display the ilog buffer
;; (global-set-key
;; (kbd "C-h C-l")
;; (lambda () (interactive) (display-buffer ilog-buffer-name)))
;; Display source code lambdas only
(setq ilog-print-lambdas 'not-compiled)
;; Omit display of some lower-level Hyperbole commands for cleaner logs
(mapc (lambda (cmd-str) (cl-pushnew (format "^%s$" cmd-str) ilog-self-insert-command-regexps))
'("hyperbole" "hui:menu-enter"))
;; Redefine the mode to display commands on post-command-hook rather
;; than pre-command-hook since Hyperbole rewrites some command names
;; and key sequences.
(define-minor-mode interaction-log-mode
"Global minor mode logging keys, commands, file loads and messages.
Logged stuff goes to the *Emacs Log* buffer."
:group 'interaction-log
:lighter nil
:global t
:after-hook interaction-log-mode-hook
(if interaction-log-mode
(progn
(add-hook 'after-change-functions #'ilog-note-buffer-change)
(add-hook 'post-command-hook #'ilog-record-this-command)
(add-hook 'post-command-hook #'ilog-post-command)
(setq ilog-truncation-timer (run-at-time 30 30 #'ilog-truncate-log-buffer))
(setq ilog-insertion-timer (run-with-timer ilog-idle-time ilog-idle-time
#'ilog-timer-function))
(message "Interaction Log: started logging in %s" ilog-buffer-name))
(remove-hook 'after-change-functions #'ilog-note-buffer-change)
(remove-hook 'post-command-hook #'ilog-record-this-command)
(remove-hook 'post-command-hook #'ilog-post-command)
(when (timerp ilog-truncation-timer) (cancel-timer ilog-truncation-timer))
(setq ilog-truncation-timer nil)
(when (timerp ilog-insertion-timer) (cancel-timer ilog-insertion-timer))
(setq ilog-insertion-timer nil)))
;; Define this function to display a 41 character wide ilog frame
;; at the right of the screen with other frame parameters that match
;; the frame selected when this is called.
(defun ilog-show-in-other-frame ()
"Display ilog in a separate frame of width 41 with parameters of selected frame.
Raise and reuse any existing single window frame displaying ilog."
(interactive)
(require 'hycontrol)
(with-selected-window (selected-window)
(let* ((win (get-buffer-window ilog-buffer-name t))
(frame (when win (window-frame win))))
(if (and frame (= (with-selected-frame frame (count-windows)) 1))
(raise-frame frame)
(unless interaction-log-mode (interaction-log-mode 1))
(let ((params (frame-parameters)))
(setcdr (assq 'width params) 41)
(setq win (display-buffer-pop-up-frame
(get-buffer ilog-buffer-name)
(list (cons 'pop-up-frame-parameters params))))
(set-window-dedicated-p win t)
(with-selected-frame (window-frame win)
(hycontrol-frame-to-right-center))
win)))))
;; Enable the mode
(interaction-log-mode 1)
;; Limit display to commands executed
(with-current-buffer (get-buffer-create ilog-buffer-name)
(setq ilog-display-state 'messages)
;; Changes to command-only mode
(ilog-toggle-view)
(message ""))
(ilog-show-in-other-frame))
(defmacro hypb:assert-same-start-and-end-buffer (&rest body)
"Assert that current buffer does not change following execution of BODY.
Trigger an error with traceback if the buffer is not live or its
name differs at the start and end of BODY."
(declare (indent 0) (debug t))
`(let ((debug-on-error t)
(start-buffer (current-buffer)))
(unless (buffer-live-p start-buffer)
(error "Start buffer, '%s', is not live" (current-buffer)))
;; `kill-buffer' can change current-buffer in some odd cases.
(unwind-protect
(progn ,@body)
(unless (eq start-buffer (current-buffer))
(error "Start buffer, '%s', differs from end buffer, '%s'" start-buffer (current-buffer)))
(unless (buffer-live-p start-buffer)
(error "End buffer, '%s', is not live" (current-buffer))))))
(defun hypb:call-process-p (program &optional infile predicate &rest args)
"Call an external PROGRAM with INFILE for input.
If PREDICATE is given, it is evaluated in a buffer with the PROGRAM's
output and the result returned. If PREDICATE is nil, returns t iff
program has no output or just a 0-valued output.
Rest of ARGS are passed as arguments to PROGRAM."
(let ((buf (get-buffer-create "*test-output*"))
(found))
(with-current-buffer buf
(setq buffer-read-only nil)
(erase-buffer)
(apply 'call-process program infile buf nil args)
(setq found
(if predicate
(eval predicate)
(or (= (point-max) 1) ;; No output, consider cmd a success.
(and (< (point-max) 4)
(string= (buffer-substring 1 2) "0")))))
(set-buffer-modified-p nil)
(kill-buffer buf))
found))
(defun hypb:char-count (char array)
"Return count of occurrences of CHAR in ARRAY."
(let ((i 0) (c 0) (l (length array)))
(while (< i l)
(when (= char (aref array i)) (setq c (1+ c)))
(setq i (1+ i)))
c))
(defun hypb:chmod (op octal-permissions file)
"Use OP and OCTAL-PERMISSIONS integer to set FILE permissions.
OP may be +, -, xor, or default =."
(let ((func (cond ((eq op '+) #'logior)
((eq op '-) (lambda (p1 p2) (logand (lognot p1) p2)))
((eq op 'xor) #'logxor)
(t (lambda (p1 p2) p2 p1)))))
(set-file-modes file (funcall func (hypb:oct-to-int octal-permissions)
(file-modes file)))))
(defun hypb:cmd-key-series (cmd-sym &optional keymap)
"Return a brace-delimited, human readable key sequence string bound to CMD-SYM.
Global keymap is used unless optional KEYMAP is given.
Trigger an error if CMD-SYM is not bound."
(if (and cmd-sym (symbolp cmd-sym) (fboundp cmd-sym))
(let* ((get-keys (lambda (cmd-sym keymap)
(key-description (where-is-internal
cmd-sym keymap 'first))))
(keys (funcall get-keys cmd-sym keymap)))
(concat "{"
(if (string= keys "")
(concat (funcall get-keys 'execute-extended-command nil)
" " (symbol-name cmd-sym) " RET")
keys)
"}"))
(error "(hypb:cmd-key-series): Invalid cmd-sym arg: %s" cmd-sym)))
(defun hypb:cmd-key-vector (cmd-sym &optional keymap)
"Return as a vector the first key sequence bound to CMD-SYM.
Search global keymap or optional KEYMAP. Return nil if no valid
key binding is found.
The returned value may be compared with `equal' to `this-single-command-keys'.
Use `key-description' to make it human readable."
(where-is-internal cmd-sym keymap t))
;;;###autoload
(defun hypb:count-visible-windows ()
"Return the number of visible, non-minibuffer windows across all frames."
(apply '+ (mapcar (lambda (frm) (length (window-list frm)))
(visible-frame-list))))
;;;###autoload
(defun hypb:configuration (&optional out-buf)
"Insert Emacs configuration information at the end of a buffer.
Use optional OUT-BUF if present, else the current buffer."
(save-excursion
(and out-buf (set-buffer out-buf))
(goto-char (point-min))
(if (re-search-forward mail-header-separator nil t)
(forward-line 1)
(goto-char (point-max)))
(delete-blank-lines) (delete-blank-lines)
(let ((start (point)))
(insert (format "I use:\tEditor: GNU Emacs %s %s\n\tHyperbole: %s\n"
emacs-version
(if (and (fboundp #'native-comp-available-p)
(native-comp-available-p))
"with native comp"
"")
hyperb:version))
(when (and (boundp 'br-version) (stringp br-version))
(insert (format "\tOO-Browser: %s\n" br-version)))
(when (and (boundp 'system-configuration) (stringp system-configuration))
(insert (format "\tSys Type: %s\n" system-configuration)))
(insert (format "\tOS Type: %s\n\tWindow Sys: %s\n"
system-type (or window-system (hyperb:window-system)
"None")))
(when (and (boundp 'hmail:reader) hmail:reader)
(insert (format "\tMail Reader: %s\n"
(cond ((eq hmail:reader 'rmail-mode) "RMAIL")
((and (eq hmail:reader 'mh-show-mode)
(string-match "v ?\\([0-9]+.[0-9]+\\)"
mh-e-RCS-id))
(concat "MH-e "
(substring mh-e-RCS-id
(match-beginning 1)
(match-end 1))))
((eq hmail:reader 'pm-fdr-mode)
(concat "PIEmail " pm-version))))))
(when (and (boundp 'hnews:reader) (boundp 'gnus-version) hnews:reader)
(insert (format "\tNews Reader: %s\n" gnus-version)))
(let ((install-type (hypb:installation-type)))
(when install-type
(insert (format "\tInstall: %s, %s" (car install-type) (cadr install-type)))))
(insert "\n")
;; Insert recent Hyperbole debugging messages if any.
(when (messages-buffer)
(let ((opoint (point)))
(insert-buffer-substring (buffer-name (messages-buffer)))
(keep-lines "^(HyDebug)" opoint (point))))
(untabify start (point)))))
(defun hypb:debug ()
"Load Hyperbole hbut.el source file and set debugging traceback flag."
(interactive)
(or (featurep 'hinit) (load "hyperbole"))
(or (and (featurep 'hbut)
(let ((func (hypb:indirect-function 'ebut:create)))
(not (or (subrp func)
(byte-code-function-p func)
(eq 'byte-code
(car (car (nthcdr 3 (hypb:indirect-function
'ebut:create)))))))))
(load "hbut.el"))
(setq debug-on-error t))
;; Copied from eww.el, eww-decode-url-file-name, so as to not require
;; that package.
(defun hypb:decode-url (url-file-name)
"Decode a URL-FILE-NAME."
(let* ((binary (url-unhex-string url-file-name))
(decoded
(decode-coding-string
binary
;; Possibly set by `universal-coding-system-argument'.
(or coding-system-for-read
;; RFC 3986 says that %AB stuff is utf-8.
(if (equal (decode-coding-string binary 'utf-8)
'(unicode))
'utf-8
;; But perhaps not.
(car (detect-coding-string binary))))))
(encodes (find-coding-systems-string decoded)))
(if (or (equal encodes '(undecided))
(memq (coding-system-base (or file-name-coding-system
default-file-name-coding-system))
encodes))
decoded
;; If we can't encode the decoded file name (due to language
;; environment settings), then we return the original, hexified
;; string.
url-file-name)))
;; Similar keyboard macro to next function, but less flexible: {C-x 1 M-o F M-o a C-x b *scratch* RET M-< M-o s C-@ C-M-h M-o t a C-u C-@ C-u C-@ M-o a C-M-p}
;;;###autoload
(defun hypb:def-to-buffer (&optional arg buffer)
"Copy next optional ARG code definitions to the start of optional BUFFER.
Default ARG is 1 and default BUFFER is \"*scratch*\". Leave
point at the start of the inserted text."
(interactive "p\nbDef insertion buffer (default *scratch*): ")
(let ((def (save-excursion
(mark-defun arg)
(deactivate-mark)
(buffer-substring (region-beginning) (region-end)))))
(pop-to-buffer (or buffer "*scratch*"))
(goto-char (point-min))
(insert def)
(goto-char (point-min))
(forward-line 1)))
;;;###autoload
(defun hypb:devdocs-lookup ()
"Prompt for and display a devdocs.io docset section within Emacs.
This will install the Emacs devdocs package if not yet installed."
(interactive)
(hypb:require-package 'devdocs)
;; (call-interactively #'devdocs-install)
(devdocs-lookup))
(defun hypb:domain-name ()
"Return current Internet domain name with '@' prepended or nil if none."
(let* ((dname-cmd (or (file-exists-p "/usr/bin/domainname")
(file-exists-p "/bin/domainname")))
(dname (or (and (boundp 'message-user-fqdn) (stringp message-user-fqdn)
(string-match "\\." message-user-fqdn)
message-user-fqdn)
(getenv "DOMAINNAME")
(when dname-cmd
(hypb:call-process-p
"domainname" nil
'(substring (buffer-string) 0 -1)))))
host-and-domain)
(when (or (and dname (string-match "\\." dname))
(and (setq host-and-domain (hypb:call-process-p
"hostname" nil '(substring (buffer-string) 0 -1) "-f"))
(setq dname (when (string-match "\\`[^.]+\\." host-and-domain)
(substring host-and-domain (match-end 0)))))
(let* ((src "/etc/resolv.conf")
(src-buf-exists-p (get-file-buffer src)))
(and (file-exists-p src) (file-readable-p src)
(with-temp-buffer
(insert-file-contents-literally src)
(goto-char (point-min))
(when (re-search-forward "^domain[ \t]+\\([^ \t\n\r]+\\)" nil t)
(setq dname (match-string 1)))
(or src-buf-exists-p (kill-buffer nil))
dname))))
(concat "@" dname))))
(defun hypb:empty-file-p ()
"Return non-nil if the current buffer has an attached file of zero size."
(when (and buffer-file-name (file-readable-p buffer-file-name))
(= (file-attribute-size (file-attributes buffer-file-name)) 0)))
(defun hypb:error (&rest args)
"Signal an error typically to be caught by `hyperbole'.
The error message is formatted passing the rest of the ARGS to
the `format' function."
(let ((msg (if (< (length args) 2)
(car args)
(apply 'format (cons (car args)
(mapcar #'hypb:format-quote (cdr args)))))))
(put 'error 'error-message msg)
(error msg)))
(defun hypb:eval-debug (sexp)
"Eval SEXP and on error show a debug backtrace of the problem."
(let ((debug-on-error t)
(debug-on-quit t))
(eval sexp)))
(defun hypb:fgrep-git-log (string)
"List git log entries whose changesets include STRING for selection and display.
Listing is asynchronous. A press of RET, the Action Key or the
Assist Key on any log line will display its committed changes."
(interactive "sFgrep git commits containing: ")
(compile (format "git log -S'%s' --line-prefix='commit ' --oneline" string)
#'hypb:fgrep-git-log-mode))
(defun hypb:fgrep-git-log-activate (_ignore1 &optional _ignore2)
"Display git commit for the current line when `compile-goto-error' {RET} is used.
Does not support use of next and previous error; simply displays
the current one."
(interactive '(nil))
(hkey-either nil))
(define-derived-mode hypb:fgrep-git-log-mode compilation-mode "Fgrep-Git-Log"
"Major mode for listing a matching set of git commits for selection and display.
Mode is derived from `compilation-mode'. Turning on
Fgrep-Git-Log mode runs the normal hook `compilation-mode-hook'."
(setq-local next-error-function #'hypb:fgrep-git-log-activate))
(defun hypb:file-major-mode (file)
"Return the major mode used by FILE.
FILE is temporarily read into a buffer to determine the major mode if necessary."
(let ((existing-flag (get-file-buffer file))
(buf (find-file-noselect file)))
(prog1 (when buf (save-excursion (with-current-buffer buf
major-mode)))
(unless (or existing-flag (null buf))
(kill-buffer buf)))))
(defun hypb:filter-directories (file-regexp &rest dirs)
"Filter files to those matching FILE-REGEXP from rest of DIRS (recursively).
Also filters out any files matching `completion-ignored-extensions' or
ending with # or ~.
Return a flattened list of all matching files."
(setq file-regexp (hypb:glob-to-regexp file-regexp))
(setq dirs (hypb:readable-directories dirs))
(delq nil (mapcar (lambda (f)
(unless (string-match-p
(concat (regexp-opt (append completion-ignored-extensions '("#" "~"))
'paren) "$") f)
f))
(apply #'nconc (mapcar (lambda (dir) (directory-files-recursively dir file-regexp))
dirs)))))
(defun hypb:format-args (args)
"Return a space-separated string of quoted ARGS without surrounding parentheses."
(if args (mapconcat (lambda (a) (format "%S" a)) args " ") ""))
(defun hypb:format-quote (arg)
"Replace all single % with %% in any string ARG.
This is so that a call to `format' or `message' ignores them.
Return either the modified string or the original ARG when not
modified."
(if (stringp arg)
(replace-regexp-in-string
"@@@" "%%" (replace-regexp-in-string
"%" "%%" (replace-regexp-in-string "%%" "@@@" arg nil t)
nil t)
nil t)
arg))
;; Extracted from part of `choose-completion' in "simple.el"
(defun hypb:get-completion (&optional event)
"Return the completion at point.
If EVENT, use EVENT's position to determine the starting position."
(interactive (list last-nonmenu-event))
;; In case this is run via the mouse, give temporary modes such as
;; isearch a chance to turn off.
(run-hooks 'mouse-leave-buffer-hook)
(with-current-buffer (window-buffer (posn-window (event-start event)))
(save-excursion
(goto-char (posn-point (event-start event)))
(let (beg end)
(cond
((and (not (smart-eobp)) (get-text-property (point) 'mouse-face))
(setq end (point) beg (1+ (point))))
((and (not (bobp))
(get-text-property (1- (point)) 'mouse-face))
(setq end (1- (point)) beg (point)))
(t (error "No completion here")))
(setq beg (previous-single-property-change beg 'mouse-face))
(setq end (or (next-single-property-change end 'mouse-face)
(point-max)))
(buffer-substring-no-properties beg end)))))
(defun hypb:get-raw-syntax-descriptor (char &optional syntax-table)
"Return the raw syntax descriptor for CHAR.
Use the current syntax table or optional SYNTAX-TABLE."
(aref (or syntax-table (syntax-table)) char))
(defun hypb:glob-to-regexp (str)
"Convert any file glob syntax in STR to Emacs regexp syntax."
(when (stringp str)
(setq str (replace-regexp-in-string "\\`\\*" ".*" str nil t)
str (replace-regexp-in-string "\\([^\\.]\\)\\*" "\\1.*" str))
(when (and (not (string-match-p "\\(\\$\\|\\\\'\\)\\'" str))
(string-match-p "\\.\\S-+\\'" str))
(setq str (concat str "$"))))
str)
;; Derived from pop-global-mark of "simple.el" in GNU Emacs.
(defun hypb:goto-marker (marker)
"Make MARKER's buffer and position current.
If MARKER is invalid signal an error."
(cond ((not (markerp marker))
(error "Invalid marker: %s" marker))
((not (marker-buffer marker))
(error "Invalid marker buffer: %s" marker))
(t (let* ((buffer (marker-buffer marker))
(position (marker-position marker)))
(set-buffer buffer)
(unless (and (>= position (point-min))
(<= position (point-max)))
(if widen-automatically
(widen)
(error "Marker position is outside accessible part of buffer: %s" marker)))
(goto-char position)
(switch-to-buffer buffer)))))
(defun hypb:grep-git-log (regexp)
"List git log entries whose changesets include REGEXP for selection and display.
Listing is asynchronous. A press of RET, the Action Key or the
Assist Key on any log line will display its committed changes."
(interactive "sGrep git commits containing: ")
(compile (format "git log -G'%s' --line-prefix='commit ' --oneline" regexp)))
(defun hypb:help-buf-name (&optional suffix)
"Return a Hyperbole help buffer name for current buffer.
With optional SUFFIX string, uses it rather than buffer name."
(let ((bn (or suffix (buffer-name))))
(if (string-match (regexp-quote hypb:help-buf-prefix) bn)
(buffer-name (generate-new-buffer bn))
(concat hypb:help-buf-prefix bn "*"))))
;;;###autoload
(defun hypb:helm-apropos (&optional symbol-name)
"Prompt for and display the doc for a command, function, variable or face.
With optional SYMBOL-NAME non-nil, display the doc for that.
This will this install the Emacs helm package when needed."
(interactive "P")
(hypb:require-package 'helm)
(helm-apropos symbol-name))
;;;###autoload
(defun hypb:helm-info (&optional refresh)
"Prompt across all Info manuals and display the node selected.
With optional prefix arg REFRESH non-nil, refresh the cache of Info manuals.
This will this install the Emacs helm package when needed."
(interactive "P")
(hypb:require-package 'helm)
(helm-info refresh))
(defun hypb:hkey-help-file ()
"Return the full path to the Hyperbole mouse key help file."
(cond ((and (fboundp 'locate-data-file)
(locate-data-file "hkey-help.txt")))
(t (let* ((hypb-man (expand-file-name "man/" hyperb:dir))
(help-file (expand-file-name "hkey-help.txt" hypb-man)))
(if (or (file-exists-p help-file)
(file-exists-p
(setq help-file (expand-file-name
"hkey-help.txt" data-directory))))
help-file
(error "(hypb:hkey-help-file): Non-existent file: \"%s\""
help-file))))))
(defun hypb:in-string-p ()
"Return non-nil iff point is in a double quoted string."
(syntax-ppss-flush-cache (line-beginning-position))
(nth 3 (syntax-ppss)))
(defun hypb:indirect-function (obj)
"Return the function at the end of OBJ's function chain.
Resolves autoloadable function symbols properly."
(let ((func (indirect-function obj)))
;; Handle functions with autoload bodies.
(if (and (symbolp obj) (listp func) (eq (car func) 'autoload))
(let ((load-file (car (cdr func))))
(load load-file)
;; Prevent infinite recursion
(if (equal func (symbol-function obj))
(error "(hypb:indirect-function): Autoload of '%s' failed" obj)
(hypb:indirect-function obj)))
func)))
(defun hypb:insert-region (buffer start end invisible-flag)
"Insert into BUFFER the contents of the region from START to END.
Contents come from the current buffer. INVISIBLE-FLAG, if
non-nil, means invisible text in an outline region is copied,
otherwise, it is omitted."
(if invisible-flag
;; Skip hidden blank lines between cells but include hidden outline text.
(while (< start end)
(if (not (get-text-property start 'invisible))
(append-to-buffer buffer start (1+ start)))
(setq start (1+ start)))
;; Skip both hidden blank lines between cells and hidden outline text.
(while (< start end)
(or (kview:char-invisible-p start) (append-to-buffer buffer start (1+ start)))
(setq start (1+ start)))))
(defun hypb:installation-type ()
"Return type of installation and version number.
Is a list of (hyperbole-installation-type-string
hyperbole-install-version-number-string). If no matching
installation type is found, return a list of (\"unknown\"
`hyperb:dir')."
(let ((hypb-dir-name (file-name-nondirectory (directory-file-name hyperb:dir)))
(dir-sep-string (substring (file-name-as-directory ".") -1)))
(cond
;; straight.el package install -- hyperbole gnu-elpa-mirror master 56cd3d8 2022-02-05
((string-match (concat dir-sep-string "straight" dir-sep-string
"build" dir-sep-string "hyperbole") hyperb:dir)
(let* ((plist (hypb:straight-package-plist "hyperbole"))
(pkg-version (plist-get plist :version))
(git-commit (when (string-match " \\([a-f0-9]+\\) " pkg-version)
(match-string 1 pkg-version))))
(list "straight" git-commit)))
;; elpa-devel package install -- hyperbole-7.0.0pre0.20220126.1138
((string-match "hyperbole-\\([.[:digit:]]+pre[.[:digit:]]+\\)" hypb-dir-name)
(list "elpa-devel" (match-string 1 hypb-dir-name)))
;; melpa/quelpa package install -- hyperbole-20220205.1429
((string-match "hyperbole-\\([1-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]\\.[0-9]+\\)"
hypb-dir-name)
(list "melpa" (match-string 1 hypb-dir-name)))
;; git install -- hyperbole d27f4c5197
((file-exists-p (expand-file-name ".git" hyperb:dir))
(ignore-errors
(let ((default-directory hyperb:dir))
(list
"git"
(substring (shell-command-to-string "git rev-parse HEAD") 0 10)))))
;; elpa package install -- /elpa/hyperbole-8.0.0"
((and (string-match-p (concat dir-sep-string "elpa" dir-sep-string) hyperb:dir)
(string-match "hyperbole-\\([.[:digit:]]+\\)" hypb-dir-name))
(list "elpa" (match-string 1 hypb-dir-name)))
;; tarball archive install -- hyperbole-8.0.0
((string-match "hyperbole-\\([.[:digit:]]+\\)" hypb-dir-name)
(list "archive" (match-string 1 hypb-dir-name)))
;; unknown -- hyperbole
(t (list "unknown" hyperb:dir)))))
;;;###autoload
(defun hypb:locate (search-string &optional filter arg)
"Find file name match anywhere and put results in the `*Locate*' buffer.
Pass it SEARCH-STRING as argument. Interactively, prompt for SEARCH-STRING.
With prefix arg ARG, prompt for the exact shell command to run instead.
This program searches for those file names in a database that match
SEARCH-STRING and normally outputs all matching absolute file names,
one per line. The database normally consists of all files on your
system, or of all files that you have access to. Consult the
documentation of the program for the details about how it determines
which file names match SEARCH-STRING. (Those details vary highly with
the version.)
You can specify another program for this command to run by customizing
the variables `locate-command' or `locate-make-command-line'.
The main use of FILTER is to implement `locate-with-filter'. See
the docstring of that function for its meaning.
After preparing the results buffer, this runs `dired-mode-hook' and
then `locate-post-command-hook'."
(interactive (list (let ((default (symbol-at-point)))
(read-string (format "Locate files anywhere with names that match%s: "
(if default
(format " (default %s)" default)
""))
nil nil default))
nil
current-prefix-arg))
(locate search-string filter arg))
;;;###autoload
(defun hypb:map-plist (func plist)
"Apply FUNC of two args, key and value, to key-value pairs in PLIST."
(cl-loop for (k v) on plist by #'cddr
collect (funcall func k v) into result
finally return result))
(defun hypb:map-vector (func object)
"Return list of results of application of FUNC to each element of OBJECT.
OBJECT should be a vector or `byte-code' object."
(unless (or (vectorp object) (byte-code-function-p object))
(error "(hypb:map-vector): Second argument must be a vector or byte-code object"))
(let ((end (length object))
(i 0)
(result))
(while (< i end)
(setq result (cons (funcall func (aref object i)) result)
i (1+ i)))
(nreverse result)))
(defun hypb:mark-object (object)
"Mark OBJECT as a Hyperbole object.
If possible to prevent generic functions from changing it.
OBJECT must be a non-empty string or a symbol or this has no effect."
(cond ((and (stringp object) (not (string-empty-p object)))
(put-text-property 0 1 'hyperbole t object))
((symbolp object)
(put object 'hyperbole t))))
;; Derived from "window.el".
(defun hypb:maximize-window-height (&optional window)
"Maximize WINDOW.
Make WINDOW as large as possible without deleting any windows.
WINDOW must be a valid window and defaults to the selected one.
If the option `window-resize-pixelwise' is non-nil maximize
WINDOW pixelwise."
(interactive)
(setq window (window-normalize-window window))
(window-resize
window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
nil nil window-resize-pixelwise))
(defun hypb:object-p (object)
"Return t if OBJECT is marked as a Hyperbole object, else nil."
(cond ((and (stringp object) (not (string-empty-p object)))
(get-text-property 0 'hyperbole object))
((symbolp object)
(get object 'hyperbole))))
(defun hypb:readable-directories (&rest dirs)
"Flatten rest of DIRS and return or error if any of DIRS are unreadable."
(setq dirs (flatten-list dirs))
(let ((unreadable-dirs (delq nil (mapcar (lambda (dir) (unless (file-readable-p dir) dir)) dirs))))
(when unreadable-dirs
(error "(hypb:readable-directories): These directories are not readable:\n%s"
(string-join unreadable-dirs "\n"))))
dirs)
;;;###autoload
(defun hypb:require-package (package-name)
"Prompt user to install, if necessary, and require the Emacs PACKAGE-NAME.
PACKAGE-NAME may be a symbol or a string."
(when (stringp package-name)
(setq package-name (intern package-name)))
(unless (symbolp package-name)
(error "(hypb:require-package): package-name must be a symbol or string, not '%s'" package-name))
(unless (package-installed-p package-name)
(if (y-or-n-p (format "Install package `%s' required by this command?" package-name))
(package-install package-name)
(keyboard-quit)))
(require package-name))
;; Adapted from cl--do-remf in "cl-extra.el" but uses 'equal' for comparisons.
;;;###autoload
(defun hypb:do-remove-from-plist (plist name)
"Remove from property list PLIST a NAME string."
(let ((p (cdr plist)))
;; Can't use `plist-member' here because it goes to the cons-cell
;; of NAME and we need the one before.
(while (and (cdr p) (not (equal (cadr p) name)))
(setq p (cddr p)))
(and (cdr p) (progn (setcdr p (cdddr p)) t))))
;; Adapted from cl-remf in "cl-macs.el" but uses 'equal' for comparisons.
;;;###autoload
(defmacro hypb:remove-from-plist (place name)
"Remove from property list PLACE a NAME string.
PLACE may be a symbol, or any generalized variable allowed by
`setf'. The form generated by the macro returns true if NAME was
found and removed, nil otherwise."
(declare (debug (place form)))
(gv-letplace (tval setter) place
(macroexp-let2 macroexp-copyable-p tname name
`(if (equal ,tname (car ,tval))
(progn ,(funcall setter `(cddr ,tval))
t)
(hypb:do-remove-from-plist ,tval ,tname)))))
(defun hypb:remove-lines (regexp)
"Remove lines containing match for REGEXP.
Apply within an active region or to the end of buffer."
(interactive "sRemove lines with match for regexp: ")
(flush-lines regexp nil nil t))
(defun hypb:return-process-output (program &optional infile &rest args)
"Return as a string the output from external PROGRAM with INFILE for input.
Rest of ARGS are passed as arguments to PROGRAM.
Removes any trailing newline at the end of the output."
(let ((buf (get-buffer-create "*test-output*"))
(output))
(with-current-buffer buf
(setq buffer-read-only nil)
(erase-buffer)
(apply 'call-process program infile buf nil args)
(setq output (buffer-string))
;; Remove trailing newline from output.
(when (> (length output) 0)
(setq output (substring output 0 -1)))
(set-buffer-modified-p nil)
(kill-buffer buf))
output))
;;;###autoload
(defun hypb:rgrep (pattern &optional prefx-arg)
"Recursively grep with symbol at point or PATTERN.
Grep over all non-backup and non-autosave files in the current
directory tree. If in an Emacs Lisp mode buffer and no optional
PREFX-ARG is given, limit search to only .el and .el.gz files."
(interactive (list (if (and (not current-prefix-arg) (equal (buffer-name) "*Locate*"))
(read-string "Grep files listed here for: ")
(let ((default (symbol-at-point)))
(when default (setq default (symbol-name default)))
(read-string (format "Rgrep below current dir for%s: "
(if default
(format " (default %s)" default)
""))
nil nil default)))
current-prefix-arg))
(let* ((delim (cond ((not (string-match "\'" pattern)) ?\')
((not (string-match "\"" pattern)) ?\")
((not (string-match "=" pattern)) ?=)
(t ?@)))
(grep-cmd
(if (and (not current-prefix-arg) (equal (buffer-name) "*Locate*"))
(format "%s -e \%c%s\%c %s" hypb:rgrep-command delim pattern delim (hypb:locate-pathnames))
(format "%s %s %s -e \%c%s\%c ."
hypb:rgrep-command
(when (and (memq major-mode '(emacs-lisp-mode lisp-interaction-mode))
(not prefx-arg))
(if (string-match "\\`rg " hypb:rgrep-command)
"-g \"*.el\" -g \"*.el.gz\""
"--include=\"*.el\" --include=\"*.el.gz\""))
(if (string-match "\\`rg " hypb:rgrep-command)
"-g \"!*~\" -g \"!#*\" -g \"!TAGS\""
"--exclude=\".git\" --exclude=\"CVS\" --exclude=\"*~\" --exclude=\"#*\" --exclude=\"TAGS\"")
delim pattern delim))))
(setq this-command `(grep ,grep-cmd))
(push this-command command-history)
(grep grep-cmd)))
(defun hypb:save-lines (regexp)
"Save only lines containing match for REGEXP.
Apply within an active region or to the end of buffer."
(interactive "sSave lines with match for regexp: ")
(keep-lines regexp nil nil t))
(defmacro hypb:save-selected-window-and-input-focus (&rest body)
"Execute BODY, restore selected windows in frames and frame with input focus.
The value returned is the value of the last form in BODY."
`(let ((frame (selected-frame)))
(prog1 (save-selected-window ,@body)
(select-frame-set-input-focus frame))))
(defun hypb:select-window-frame (window)
"Select WINDOW and its frame (set input focus there)."
(if (window-live-p window)
(progn (select-window window)
(select-frame-set-input-focus (window-frame window)))
(error "(hypb:select-window-frame): Argument must be a live window, not '%s'" window)))
(defun hypb:set-raw-syntax-descriptor (char raw-descriptor &optional syntax-table)
"Set the syntax of CHAR to RAW-DESCRIPTOR (syntax table value).
Set in the current syntax table or optional SYNTAX-TABLE. Return
the RAW-DESCRIPTOR. Use the `syntax-after' function to retrieve
the raw descriptor for a buffer position.
Similar to `modify-syntax-entry' but uses a raw descriptor
previously extracted from a syntax table to set the value rather
than a string.
Syntax tables are char-tables whose values are encoded as raw
descriptors."
(aset (or syntax-table (syntax-table)) char raw-descriptor))
(defun hypb:split-seq-into-sublists (seq size)
"Split a sequence SEQ into sublists of length SIZE, preserving item order."
(let (result)
(while (> (length seq) 0)
(push (seq-into (seq-take seq size) 'list) result)
(setq seq (seq-drop seq size)))
(nreverse result)))
(defun hypb:straight-package-plist (pkg-string)
"Return package info for a straight.el built package with name PKG-STRING.
The package info is a property list of package-name,
package-download-source and package-version for PKG-STRING, else
return nil. This is for the straight.el package manager."
(when (fboundp 'straight-bug-report-package-info)
(car (delq nil (mapcar (lambda (pkg-plist)
(when (equal (plist-get pkg-plist :package) pkg-string) pkg-plist))
(straight-bug-report-package-info))))))
(defun hypb:string-count-matches (regexp str &optional start end)
"Count occurrences of REGEXP in STR, limited to optional START and END positions.
START is inclusive and indexed from 0; END is exclusive.
This function starts looking for the next match from the end of the
previous match. Hence, it ignores matches that overlap a previously
found match."
(let ((str-len (length str))
(count 0)
substr)
(when (and start (or (>= start str-len) (< start 0)))
(error "(hypb:string-count-matches): 'start' (%d) must be >= 0 and < str length (%d)"
start str-len))
(when (and end (or (> end str-len) (< end 0)))
(error "(hypb:string-count-matches): 'end' (%d) must be >= 0 and <= str length (%d)"
end str-len))
(setq start (or start 0)
end (or end str-len)
substr (substring str start end)
end (- end start)