-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl2-doc.el
1839 lines (1568 loc) · 67.4 KB
/
acl2-doc.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
; ACL2 Version 8.6 -- A Computational Logic for Applicative Common Lisp
; Copyright (C) 2024, Regents of the University of Texas
; This version of ACL2 is a descendent of ACL2 Version 1.9, Copyright
; (C) 1997 Computational Logic, Inc. See the documentation topic NOTE-2-0.
; This program is free software; you can redistribute it and/or modify
; it under the terms of the LICENSE file distributed with ACL2.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; LICENSE file in the main ACL2 source directory for more details.
; Written by: Matt Kaufmann and J Strother Moore
; email: [email protected] and [email protected]
; Department of Computer Science
; University of Texas at Austin
; Austin, TX 78712 U.S.A.
; Enhancements for multiple windows (added August 2021) were
; initially contributed by Mayank Manjrekar.
; This file contains code for browsing ACL2 community book
; system/doc/acl2-doc.lisp, which contains the ACL2 system
; documentation.
; The first two forms are copied from emacs-acl2.el. Keep them in
; sync with the definitions there.
(defvar *acl2-sources-dir*)
(defvar *acl2-doc-show-extra*
; WARNING: Don't use defv (below) for this. We don't want to
; reinitialize it after downloading an updated manual.
"\nNOTE: Type D to download the latest version.")
; Attempt to set *acl2-sources-dir*.
; WARNING: If you change this form, then also change the same form in
; emacs-acl2.el.
(if (and (not (boundp '*acl2-sources-dir*))
(file-name-absolute-p load-file-name))
(let ((pattern ; emacs/ and books/emacs/ differ here
(if (string-match "[\\]" load-file-name)
"\[^\\]+\\*$"
"/[^/]+/*$"))
(dir (file-name-directory load-file-name)))
(let ((posn (string-match pattern dir)))
(if posn
(setq *acl2-sources-dir*
(substring dir 0 (1+ posn)))))))
(require 'etags) ; for "/" and "W" commands, e.g., tags-lazy-completion-table
; Essay on ACL2-doc Data Structures
; Here we briefly summarize some of the main data structures used
; below.
; *acl2-doc-state*: This variable, initialized in acl2-doc-reset, is a
; pair (top-topic-name tuple-list). Top-topic-name is the name of
; the top topic of the manual. Tuple-list is a list of tuples,
; created in acl2-doc-alist-create, read from the rendered.lsp file
; but slightly fixed up. For example, if the manual is the acl2-only
; manual, then (car (car (cdr *acl2-doc-state*))) is:
; (&ALLOW-OTHER-KEYS (POINTERS) "See [macro-args].")
; (acl2-doc-state-alist): Reset if necessary, then return
; (nth 1 *acl2-doc-state*), which is a list of tuples as described
; above.
; *acl2-doc-history*: a buffer-local list of pairs (cons point tuple),
; *where tuple is a member of (acl2-doc-state-alist) and point is the
; *position in the topic of that tuple. This is updated in
; *acl2-doc-display.
; End of Essay on ACL2-doc Data Structures.
(defvar *acl2-doc-manual-alist* nil)
(defvar *acl2-doc-manual-name* ; key into *acl2-doc-manual-alist*
'combined)
(defvar *acl2-doc-manual-name-previous* nil)
(defvar *acl2-doc-short-new-buffer-names* nil)
(defun acl2-doc-manual-alist-entry (pathname top printname url
main-tags-file-name
acl2-tags-file-name)
(list pathname top printname url main-tags-file-name
acl2-tags-file-name
(concat (or (file-name-directory pathname) "") "acl2-doc-search")
(concat (or (and url (file-name-directory url)) "") "acl2-doc-search.gz")))
(defun extend-acl2-doc-manual-alist (key pathname top
&optional
printname
url
main-tags-file-name
acl2-tags-file-name)
(when (and url
(not (and (stringp url)
(let ((len (length url)))
(and (> len 3)
(equal (substring url (- len 3) len)
".gz"))))))
(error "The URL specified for manual name %s does not end in \".gz\"."
key))
(let ((tuple (assoc key *acl2-doc-manual-alist*)))
(cond
(tuple
(setcdr tuple ; avoid setf; see my-cl-position
(acl2-doc-manual-alist-entry pathname top printname url
main-tags-file-name
acl2-tags-file-name))
*acl2-doc-manual-alist*)
(t (push (cons key (acl2-doc-manual-alist-entry pathname top printname url
main-tags-file-name
acl2-tags-file-name))
*acl2-doc-manual-alist*)))))
(extend-acl2-doc-manual-alist
'combined
(concat *acl2-sources-dir*
"books/system/doc/rendered-doc-combined.lsp")
'TOP
"ACL2+Books Manual"
"https://www.cs.utexas.edu/users/moore/acl2/manuals/current/rendered-doc-combined.lsp.gz"
(concat *acl2-sources-dir* "TAGS-acl2-doc")
(concat *acl2-sources-dir* "TAGS"))
(extend-acl2-doc-manual-alist
'acl2-only
(concat *acl2-sources-dir* "doc.lisp")
'ACL2
"ACL2 User's Manual"
nil
nil
(concat *acl2-sources-dir* "TAGS"))
(defmacro defv (var form)
`(progn (defvar ,var)
(setq ,var ,form)))
(defmacro defv-local (var form)
`(progn (defvar-local ,var ,form)
(put ',var 'permanent-local t))) ; required for saving
; buffer-local values when
; major mode is changed
(defmacro acl2-doc-init-vars (first-time)
`(progn
(defv *acl2-doc-buffer-name* ; This is a constant.
"acl2-doc")
(defv *acl2-doc-index-buffer-name*
"acl2-doc-index")
(defv *acl2-doc-search-separator* "###---###---###---###---###")
(defv *acl2-doc-search-buffer-name* "acl2-doc-search")
(defv *acl2-doc-history-buffer-name* "acl2-doc-history")
(defv *acl2-doc-all-topics-rev* nil)
(defv *acl2-doc-state* nil)
(defv *acl2-doc-children-ht* nil)
(defv *acl2-doc-show-help-message* nil)
; Warning: Do not set *acl2-doc-show-extra* here (see comment in its
; introduction using defvar, above).
(defv *acl2-doc-last-tags-file-name* nil)
,@(and first-time
'((defv-local *acl2-doc-index-name* nil)
(defv-local *acl2-doc-index-name-found-p* nil)
(defv-local *acl2-doc-index-position* 1)
(defv-local *acl2-doc-search-string* nil)
(defv-local *acl2-doc-search-regexp-p* nil)
(defv-local *acl2-doc-search-position* 1)
(defv-local *acl2-doc-history* nil)
(defv-local *acl2-doc-return* nil)
(defv-local *acl2-doc-limit-topic* nil)
(defv-local *acl2-doc-topics-ht* nil)))))
(acl2-doc-init-vars t)
; We define the following variable outside acl2-doc-init-vars, so that it is
; not smashed by invoking that macro after running acl2-doc-alist-create.
(defv *acl2-doc-directory* nil)
(defun my-cl-position (ch str)
;;; We include this simple variant of cl-position because it is not
;;; available in some versions of emacs (we have had a report of this
;;; problem for GNU Emacs 24.3.1 on redhat). We could (require 'cl)
;;; or perhaps (require 'cl-lib), but some web pages suggest issues
;;; with using cl functions (as opposed to macros). I don't want to
;;; think about any of that.
(let ((continue t)
(pos 0)
(len (length str))
(ans))
(while (and continue (< pos len))
(when (eql (aref str pos) ch)
(setq ans pos)
(setq continue nil))
(setq pos (1+ pos)))
ans))
(defun acl2-doc-fix-symbol (sym)
;;; Since Emacs Lisp doesn't seem to use |..| for escaping, we simply
;;; remove those vertical bars that seem to have been placed by Common
;;; Lisp. NOTE: If this doesn't do the trick, consider modifying the
;;; definition of function rendered-name-acl2-doc in file
;;; books/system/doc/render-doc-base.lisp.
(let* ((name (symbol-name sym))
(pos (my-cl-position ?| name)))
(cond ((null pos) ; common case
sym)
(t (let ((max (1- (length name))))
(cond ((eql max -1) ; impossible?
sym)
((not (eql (aref name max) ?|))
sym)
((eql pos 0)
(intern (substring name 1 max)))
((and (> pos 1)
(eql (aref name (- pos 1)) ?:)
(eql (aref name (- pos 2)) ?:))
(intern (concat (substring name 0 pos)
(substring name (1+ pos) max))))
(t sym)))))))
(defun acl2-doc-fix-entry (entry)
(cons (acl2-doc-fix-symbol (car entry))
(cons (let ((lst (cadr entry)))
(if (eq lst 'NIL)
()
(let (ans)
(while lst
(push (acl2-doc-fix-symbol (pop lst))
ans))
(nreverse ans))))
(cddr entry))))
(defun acl2-doc-fix-alist (alist)
;;; We delete topics whose names start with a vertical bar. Most of
;;; these are for the tours anyhow. We also lose release notes topics
;;; for ACL2(r) from July 2011 and before, but that seems minor
;;; compared to the pain of dealing with these topics, since Emacs
;;; Lisp doesn't seem to use |..| for escaping.
(let ((ans nil))
(while alist
(let ((entry (pop alist)))
(push (acl2-doc-fix-entry entry) ans)))
(reverse ans)))
(defun acl2-doc-large-file-warning-threshold ()
;;; As of April 2019, file books/system/doc/rendered-doc-combined.lsp is
;;; a bit over 100M bytes. We take a guess here that modern platforms
;;; can handle somewhat more than this size, so we rather arbitrarily
;;; bump up the threshold for a warning, to provide a modest cushion for
;;; avoiding the warning.
(max (or large-file-warning-threshold 0)
120000000))
(defun acl2-doc-alist-create (rendered-pathname)
(when (not (file-exists-p rendered-pathname))
(error "File %s is missing!" rendered-pathname))
(let* ((large-file-warning-threshold (acl2-doc-large-file-warning-threshold))
(buf0 (find-buffer-visiting rendered-pathname))
; We could let buf = buf0 if buf0 is non-nil. But if the file was changed
; after loading it into buf0, then we would be operating on a stale buffer. So
; we ignore buf0 other than to decide whether to delete the buffer just before
; returning from this function.
(buf (progn (when buf0 ; avoid undo warning
(kill-buffer buf0))
(message "Reading %s..."
rendered-pathname)
(find-file-noselect rendered-pathname))))
(with-current-buffer
buf
; Warren Hunt asked that the acl2-doc buffer be put in the same directory as
; the file from which it was derived. One basic advantage of this idea is that
; the directory won't depend on where acl2-doc was invoked.
(setq *acl2-doc-directory* default-directory)
(save-excursion
(lisp-mode)
(goto-char (point-min))
(forward-sexp 1)
(forward-line 3)
(prog1 (acl2-doc-fix-alist (read buf))
(when (not buf0)
(kill-buffer buf))
(message "Refreshed from %s" rendered-pathname))))))
(defun acl2-doc-manual-entry (&optional manual-name)
(let* ((manual-name (or manual-name *acl2-doc-manual-name*))
(tuple (assoc manual-name *acl2-doc-manual-alist*)))
(when (null tuple)
(error "No manual exists in %s with name %s."
'*acl2-doc-manual-alist*
manual-name))
(cdr tuple)))
(defun acl2-doc-manual-name ()
*acl2-doc-manual-name*)
(defun acl2-doc-pathname (&optional must-exist)
(let ((pathname (nth 0 (acl2-doc-manual-entry))))
(cond
((null pathname)
(error "No pathname was specified in %s for manual name %s."
'*acl2-doc-manual-alist*
*acl2-doc-manual-name*))
((and must-exist
(not (file-exists-p pathname)))
(error "The file %s (for manual name %s) does not exist."
pathname
(acl2-doc-manual-name)))
(t pathname))))
(defun acl2-doc-top-name ()
(nth 1 (acl2-doc-manual-entry)))
(defun acl2-doc-manual-printname ()
(or (nth 2 (acl2-doc-manual-entry))
(format "manual named `%s'"
(acl2-doc-manual-name))))
(defun acl2-doc-url (&optional strict)
(or (nth 3 (acl2-doc-manual-entry))
(and strict
(error "No URL was specified for manual name %s."
*acl2-doc-manual-name*))))
(defun acl2-doc-main-tags-file-name ()
(nth 4 (acl2-doc-manual-entry)))
(defun acl2-doc-acl2-tags-file-name ()
(nth 5 (acl2-doc-manual-entry)))
(defun acl2-doc-search-file-name ()
(nth 6 (acl2-doc-manual-entry)))
(defun acl2-doc-search-url ()
(nth 7 (acl2-doc-manual-entry)))
(defun acl2-doc-set-manual (manual-name)
; returns manual-name, except, error if manual-name names no manual
(acl2-doc-manual-entry manual-name)
(setq *acl2-doc-manual-name* manual-name))
(defun acl2-doc-state-initialized-p ()
(not (null *acl2-doc-state*)))
(defun acl2-doc-gzipped-file (filename)
(concat filename ".gz"))
(defun acl2-doc-download-aux-1 (file-url file-pathname)
(let ((file-backup (concat file-pathname ".backup"))
(file-gzipped (acl2-doc-gzipped-file file-pathname)))
(cond ((file-exists-p file-pathname)
(message "Renaming %s to %s"
file-pathname
file-backup)
(rename-file file-pathname file-backup 0)))
(message "Preparing to download URL\n%s\ninto file\n%s"
file-url file-gzipped)
(url-copy-file file-url file-gzipped)
(cond ((file-exists-p file-gzipped)
(cond
((eql 0 (nth 7
(file-attributes ; size
file-gzipped)))
(delete-file file-gzipped)
(error
"Download/install failed (deleted zero-length file, %s)"
file-gzipped))
(t
(shell-command-to-string
(format "gunzip %s"
file-gzipped))
(or (file-exists-p file-pathname)
(error "Gunzip failed")))))
(t (error "Download/install failed")
nil))))
(defvar acl2-doc-download-error "")
(defun acl2-doc-download-aux (url pathname)
(condition-case err
(acl2-doc-download-aux-1 url pathname)
(error
(progn
(setq acl2-doc-download-error
(format "You can copy URL %s
directly to file %s
and then apply gunzip to that file."
url
(acl2-doc-gzipped-file pathname)))
(error "Download failed (message: %s).
The following message is saved in Emacs variable acl2-doc-download-error:
%s"
(error-message-string err)
acl2-doc-download-error)))))
(defun acl2-doc-download ()
"Download the ``bleeding edge'' ACL2+Books Manual from the web;
then restart the ACL2-Doc browser to view that manual."
(interactive)
(acl2-doc-download-aux (acl2-doc-url t) (acl2-doc-pathname))
;;; The following call of acl2-doc-reset may appear to have the
;;; potential to cause a loop: acl2-doc-reset calls acl2-doc-fetch,
;;; which calls the present function. However, acl2-doc-fetch is
;;; essentially a no-op in this case because of the file-exists-p
;;; check made in the first call above of acl2-doc-download-aux.
(acl2-doc-reset (acl2-doc-manual-name))
(acl2-doc-top)
(acl2-doc-download-aux (acl2-doc-search-url) (acl2-doc-search-file-name)))
(defun manual-index-pathname ()
(let* ((rendered (acl2-doc-pathname))
(rendered-length (length rendered))
(suffix "system/doc/rendered-doc-combined.lsp")
(suffix-length (length suffix)))
;;; Sanity check (use (setq debug-on-error t) to see backtrace):
(or (and (<= suffix-length rendered-length)
(equal (substring rendered (- rendered-length suffix-length))
"system/doc/rendered-doc-combined.lsp"))
(error "Implementation error in acl2-doc: unexpected suffix"))
(concat
(substring rendered 0 (- rendered-length suffix-length))
"doc/manual/index.html")))
(defun acl2-doc-fetch ()
(let* ((pathname (acl2-doc-pathname))
(pathname-exists (file-exists-p pathname))
(url (acl2-doc-url))
(pathname-gz (and url (acl2-doc-gzipped-file pathname)))
(manual-index-pathname (and url (manual-index-pathname))))
(cond
((and pathname-exists
(or (null url) ;; no download option
(null manual-index-pathname) ; no writes dates to compare
(file-newer-than-file-p pathname manual-index-pathname)
(yes-or-no-p ; minibuffer display is better than y-or-n-p
"Use outdated manual? (Reply no for download option.) "))))
((and (and pathname-gz
(file-exists-p pathname-gz))
(y-or-n-p
(format
"Run gunzip on %s%s? "
pathname-gz
(if (and manual-index-pathname
(file-newer-than-file-p pathname
manual-index-pathname))
""
", even though you can download a newer version"))))
(shell-command-to-string
(format "gunzip %s"
pathname-gz))
(or (file-exists-p pathname)
(error "Execution of gunzip seems to have failed!")))
((and url
(y-or-n-p
(format "Download %s and install as %s? "
url pathname)))
(acl2-doc-download))
((or pathname-exists url)
(error "Update of manual aborted"))
(t (error
"File %s not found, and\nno URL specified for the manual named %s"
pathname *acl2-doc-manual-name*)))))
(defun acl2-doc-reset (manual-name)
(let ((old-name (acl2-doc-manual-name)))
(cond ((null manual-name)
(setq manual-name old-name))
((not (eq old-name manual-name))
(setq *acl2-doc-manual-name-previous* old-name)
(acl2-doc-set-manual manual-name)))
(acl2-doc-fetch)
(let ((new-state (list (acl2-doc-top-name)
(acl2-doc-alist-create (acl2-doc-pathname t)))))
(when (get-buffer *acl2-doc-index-buffer-name*)
(kill-buffer *acl2-doc-index-buffer-name*))
(when (get-buffer *acl2-doc-search-buffer-name*)
(kill-buffer *acl2-doc-search-buffer-name*))
(acl2-doc-kill-buffers t)
(acl2-doc-init-vars nil)
(setq *acl2-doc-state* new-state)
t)))
(defun acl2-doc-maybe-reset ()
(cond ((not (acl2-doc-state-initialized-p))
(acl2-doc-reset nil)
t)
(t nil)))
(defun acl2-doc-state-top-name ()
(acl2-doc-maybe-reset)
(nth 0 *acl2-doc-state*))
(defun acl2-doc-state-alist ()
(acl2-doc-maybe-reset)
(nth 1 *acl2-doc-state*))
; Based on http://ergoemacs.org/emacs/elisp_syntax_coloring.html:
(defvar acl2-doc-keywords
'(("\\[\\([^ \t]*[^0-9 \t][^ \t]*\\)\\]"
. 1)))
; Can be modified by user; set to the desired link color, or nil if none.
(defv *acl2-doc-link-color* "#0000FF") ; blue
(make-face 'acl2-doc-link-face)
(define-derived-mode acl2-doc-mode
fundamental-mode
"ACL2-Doc"
"Major mode for acl2-doc buffer."
; By using lisp-mode-syntax-table, we arrange that the use of colon
; (:) doesn't break up an s-expression. So for example,
; ACL2-PC::REWRITE is a single s-expression. This matters because we
; define the function acl2-doc-topic-at-point in terms of
; backward-sexp. Of course, we could instead evaluate the form
; (modify-syntax-entry ?: "w" acl2-doc-mode-syntax-table) but we start
; this way, in case other characters are missing and also because this
; approach might conceivably be useful for people who are navigating
; lisp forms in the acl2-doc buffer.
:syntax-table (copy-syntax-table lisp-mode-syntax-table))
(add-hook 'acl2-doc-mode-hook
(lambda ()
(when *acl2-doc-link-color*
(set-face-foreground 'acl2-doc-link-face *acl2-doc-link-color*)
(setq font-lock-defaults '(acl2-doc-keywords t))
(set (make-local-variable 'font-lock-keyword-face)
'acl2-doc-link-face))))
; Arrange that files ending in .acl2-doc come up in acl2-doc mode.
; See also the emacs documentation for auto-mode-alist.
(if (not (assoc "\\.acl2-doc\\'" auto-mode-alist))
(push '("\\.acl2-doc\\'" . acl2-doc-mode) auto-mode-alist))
(defvar *acl2-doc-buffer-name-pattern* ; a constant
(concat "^" *acl2-doc-buffer-name* "\\(<.+>\\|\\)$"))
(defun acl2-doc-buffer-p (buffer)
(and (string-match-p *acl2-doc-buffer-name-pattern*
(buffer-name buffer))
(with-current-buffer buffer
(equal major-mode 'acl2-doc-mode))))
(defun current-acl2-buffer ()
(let ((buf nil)
(lst (buffer-list)))
;;; It is tempting to use (seq-find 'acl2-doc-buffer-p (buffer-list)), but
;;; seq-find is not defined in some older Emacs versions (e.g., 24.5.1).
(while (and lst
(null buf))
(if (acl2-doc-buffer-p (car lst))
(setq buf (car lst))
(pop lst)))
buf))
(defun switch-to-acl2-doc-buffer (&optional new-buffer-name)
(if new-buffer-name
(let ((buf (generate-new-buffer-name
(if *acl2-doc-short-new-buffer-names*
*acl2-doc-buffer-name*
(concat *acl2-doc-buffer-name*
"<"
(symbol-name new-buffer-name)
">")))))
(if pop-up-windows ; t by default; with nil, want single window
(switch-to-buffer-other-window buf)
(switch-to-buffer buf)))
(let ((current-acl2-buffer (current-acl2-buffer)))
(switch-to-buffer (if current-acl2-buffer
current-acl2-buffer
(generate-new-buffer-name *acl2-doc-buffer-name*)))))
;;; The next two forms need only be evaluated when the buffer is first
;;; created, but it is likely harmless to go ahead and evaluate them
;;; every time.
(acl2-doc-mode)
(when *acl2-doc-directory*
(setq default-directory *acl2-doc-directory*))
t)
(defun acl2-doc-kill-buffers (&optional do-not-ask)
"Kill all background ACL2-Doc buffers. If invoked in an
ACl2-Doc buffer, all ACl2-Doc buffers except the current one will
be killed. If invoked in any other buffer, all ACL2-Doc buffers
will be killed. With prefix argument, avoid a query that asks
for confirmation."
(interactive)
(when (or do-not-ask (y-or-n-p "Kill all other acl2-doc-buffers? "))
;;; It may be tempting to use seq-filter below, but that isn't defined
;;; in some older Emacs versions (e.g., 24.5.1).
(let ((cur (current-buffer)))
(dolist (buf (buffer-list))
(when (and (acl2-doc-buffer-p buf)
(not (eq cur buf)))
(kill-buffer buf))))))
(defun acl2-doc-print-topic (tuple)
; Warning: Do not set the buffer to read-only here, because this
; function may be called repeatedly for the same buffer, e.g., by
; function acl2-doc-search-buffer.
(insert (format "Topic: %s\nParent list: %s\n%s\n%s\n"
(nth 0 tuple)
(nth 1 tuple)
(if (equal (length tuple) 4)
(if (eq (nth 0 tuple) 'TOP)
""
(format ":DOC source: %s\n" (nth 3 tuple)))
(if (eq (acl2-doc-state-top-name) 'ACL2)
""
":DOC source: ACL2 Sources\n"))
(nth 2 tuple)))
(set-buffer-modified-p nil)
(force-mode-line-update))
(defun acl2-doc-display-message (entry &optional extra)
(let ((name (car (cdr entry)))
(manual-name (acl2-doc-manual-printname))
(help-msg (if *acl2-doc-show-help-message*
"; type h for help"
""))
(extra (or extra
*acl2-doc-show-extra*
"")))
(setq *acl2-doc-show-help-message* nil)
(setq *acl2-doc-show-extra* nil)
(if (eq (acl2-doc-state-top-name) name)
(message "At the top node of the %s%s%s"
manual-name help-msg extra)
(message "Topic: %s (%s)%s%s" name manual-name help-msg extra))))
(defun acl2-doc-where ()
(interactive)
(cond ((consp *acl2-doc-history*)
(acl2-doc-display-message (car *acl2-doc-history*)))
(t (error "Empty history: No `where' to display!"))))
(defun xdoc-tag-alist-fancy-p (val)
;;; Keep this in sync with function xdoc-tag-alist-fancy-p in
;;; books/system/doc/display.lisp.
(or (null val)
(equal val "")
(equal (upcase val) "FANCY")))
(defvar *acl2-manual-dir*
(concat *acl2-sources-dir*
"books/doc/manual/"))
(defvar *img-prefix*
(byte-to-string 25))
(defvar *img-suffix*
(byte-to-string 26))
(defun acl2-doc-handle-images (display-graphic-p)
(save-excursion
(goto-char (point-min))
(let ((go t))
(while go
(let ((start (search-forward *img-prefix* nil t)))
(cond
(start
(let* ((end (search-forward *img-suffix*))
(src (buffer-substring start (1- end))))
(delete-region (1- start) end)
(cond
(display-graphic-p
(insert-image
(create-image (concat *acl2-manual-dir* src)))
(insert "\n"))
(t (insert "{IMAGE}")))))
(t (setq go nil))))))))
(defun acl2-doc-handle-color ()
;;; This function removes color indicators for Select Graphic Rendition (SGR)
;;; in the current buffer. If we want color, then we should use
;;; ansi-color-apply-on-region to set color properties; if we want plain text
;;; without color then we should use ansi-color-filter-region, which anyhow is
;;; much more efficient than ansi-color-apply-on-region.
(if (xdoc-tag-alist-fancy-p (getenv "ACL2_XDOC_TAGS"))
(ansi-color-apply-on-region (point-min) (point-max))
(ansi-color-filter-region (point-min) (point-max))))
(defun acl2-doc-display-basic (entry &optional extra no-handle-images)
;;; Entry is a history entry, hence of the form (point name parents
;;; string).
;;; The first form below is unnecessary if the caller is
;;; acl2-doc-display, but it's cheap.
(switch-to-acl2-doc-buffer)
(setq buffer-read-only nil)
(erase-buffer)
(acl2-doc-print-topic (cdr entry)) ; entry is (cons position tuple)
(acl2-doc-handle-color)
(when (not no-handle-images)
(acl2-doc-handle-images (display-graphic-p))
(setq buffer-read-only t))
(goto-char (nth 0 entry))
(push (car (cdr entry)) *acl2-doc-all-topics-rev*)
(acl2-doc-display-message entry extra))
(defun acl2-doc-display (name &optional extra new-buffer no-handle-images)
;;; Name should be a symbol. We display the topic and adjust the
;;; history and return history. Do not use this for the "l" or "r"
;;; commands; use acl2-doc-display-basic instead, which does not mess
;;; with those history variables.
(let ((tuple (assoc name (acl2-doc-state-alist))))
(cond (tuple (switch-to-acl2-doc-buffer new-buffer)
(let ((new-entry (cons 0 tuple)))
(if *acl2-doc-history*
(let ((old-entry (pop *acl2-doc-history*)))
(push (cons (point) (cdr old-entry))
*acl2-doc-history*)))
(push new-entry *acl2-doc-history*)
(setq *acl2-doc-return* nil)
(acl2-doc-display-basic new-entry extra no-handle-images)))
(t (error "Not found: %s" name)))))
(defun acl2-doc-topic-at-point ()
;;; This function returns nil if the s-expression at point isn't a
;;; symbol. Otherwise, it returns that symbol in upper case, except
;;; that it removes the leading and trailing bracket for [...].
(let ((sym (sexp-at-point))
(go t)
(arrayp nil))
(while go
(cond ((and (consp sym)
(equal (length sym) 2)
(member (car sym)
'(\` quote)))
(setq sym (car (cdr sym))))
(t (setq go nil))))
;;; Deal with array case.
(cond
((null sym)
;;; We have found that (sexp-at-point) returns nil when standing in text (in
;;; acl2-doc mode) that ends in a square bracket followed by a period, e.g.,
;;; "[loop-stopper]." So we try again.
(setq sym
(save-excursion
(if (< (point) (point-max))
(forward-char 1)) ; in case we are at "["
(let* ((saved-point (point))
(start (if (looking-at "[[]")
(point)
(and (search-backward "[" nil t)
(match-beginning 0)))))
(and start
(let ((end (and (search-forward "]" nil t)
(match-end 0))))
(and end
(<= saved-point end)
(goto-char (1+ start))
(not (search-forward " " end t))
(read (current-buffer))))))))
(when sym
(setq arrayp t)))
((arrayp sym) ;; [...]
(setq sym (and (not (equal sym []))
(aref sym 0)))
(when sym
(setq arrayp t))))
;;; Now we have sym and arrayp.
(cond (arrayp ;; [...]
(let ((tmp (and (symbolp sym)
(intern (upcase (symbol-name sym))))))
(cond ((assoc tmp (acl2-doc-state-alist))
tmp)
((numberp sym) ; e.g. from [1]
nil)
(t 'BROKEN-LINK))))
((not (and sym (symbolp sym)))
nil)
(t
(let* ((name (symbol-name sym))
(max-orig (1- (length name)))
(max max-orig)
(name (cond ((equal (aref name 0) ?:)
(setq max (1- max))
(substring name 1))
(t name)))
(go t))
(while (and go (< 1 max))
(cond ((member (aref name max)
'(?. ?\' ?:))
(setq max (1- max)))
(t (setq go nil))))
(intern (upcase (cond ((equal max max-orig)
name)
(t (substring name 0 (1+ max)))))))))))
(defun acl2-doc-completing-read (prompt silent-error-p)
(let* ((completion-ignore-case t)
(default (acl2-doc-topic-at-point))
(default (and (or (eq default 'BROKEN-LINK) ; optimization
(assoc default (acl2-doc-state-alist)))
default))
(value-read (completing-read
(if default
(format "%s (default %s): " prompt default)
(format "%s: " prompt))
(acl2-doc-state-alist)
nil nil nil nil
default)))
(list (cond ((equal value-read default) ;; i.e., (symbolp value-read)
default)
((and (not silent-error-p)
(equal value-read ""))
(error "No default topic name found at point"))
(t (intern (upcase value-read)))))))
(defun acl2-doc-go (name)
"Go to the specified topic; performs completion."
(interactive (acl2-doc-completing-read "Go to topic" nil))
(acl2-doc-display name))
(defun acl2-doc-go-new-buffer (name)
"Go to the specified topic in a new buffer; performs completion."
(interactive (acl2-doc-completing-read "Go to topic (new buffer)" nil))
(acl2-doc-display name nil name))
(defun acl2-doc-go-from-anywhere (name)
"Go to the specified topic even if not within ACL2-Doc; performs completion."
(interactive (with-syntax-table lisp-mode-syntax-table
(acl2-doc-completing-read "Go to topic" nil)))
(when (not (acl2-doc-buffer-p (current-buffer)))
(setq *acl2-doc-show-help-message* t))
(acl2-doc-display name))
;;; Avoid warning in Emacs 25.
(defvar acl2-doc-find-tag-function
'find-tag)
(defun acl2-doc-go!-aux (new-buffer-p)
(let ((name (acl2-doc-topic-at-point)))
(cond ((not name)
(error "Cursor is not on a name"))
((assoc name (acl2-doc-state-alist))
;;; This code is a bit inefficient: the assoc above is done again by
;;; acl2-doc-display. But I like the simplicity of this code.
(when new-buffer-p (switch-to-acl2-doc-buffer name))
(acl2-doc-display name))
((acl2-doc-tagp (symbol-name name))
(when new-buffer-p
(if pop-up-windows ; t by default; with nil, want single window
(switch-to-buffer-other-window nil t)
(switch-to-buffer nil)))
(acl2-doc-find-tag (acl2-doc-topic-to-tags-name name) nil t))
(t
(error "No topic name: %s" name)))))
(defun acl2-doc-go! ()
"Go to the topic occurring at the cursor position. In the case
of <NAME>, instead go to the source code definition of NAME for
the current manual (as for `/', but without a minibuffer query)."
(interactive)
(acl2-doc-go!-aux nil))
(defun acl2-doc-go!-new-buffer ()
"Go to the topic occurring at the cursor position in a new
buffer. In the case of <NAME>, instead go to the source code
definition of NAME for the current manual (as for `/', but
without a minibuffer query)."
(interactive)
(acl2-doc-go!-aux t))
(defun acl2-doc-top ()
"Go to the top topic."
(interactive)
(setq *acl2-doc-show-help-message* t)
(acl2-doc-go (acl2-doc-state-top-name)))
(defun acl2-doc (&optional clear)
"Go to the ACL2-Doc browser; prefix argument clears state.
See the documentation topic for ACL2-Doc for more
information, either by starting ACL2-Doc and typing `h'
\(help), by viewing the ACL2-DOC topic in a web browser, or by
typing `:doc acl2-doc' in the ACL2 read-eval-print loop.
\\{acl2-doc-mode-map}"
(interactive "P")
(setq *acl2-doc-show-help-message* t)
(cond (clear
(acl2-doc-reset nil)
(acl2-doc-top))
(t
(switch-to-acl2-doc-buffer)
(cond (*acl2-doc-history*
(acl2-doc-display-basic (car *acl2-doc-history*)))
(t
(acl2-doc-maybe-reset)
(acl2-doc-top))))))
(defun acl2-doc-last ()
"Go to the last topic visited in the current buffer. This
command is buffer-local."
(interactive)
(cond ((cdr *acl2-doc-history*)
(push (pop *acl2-doc-history*)
*acl2-doc-return*)
(acl2-doc-display-basic (car *acl2-doc-history*)))
(t (error "No last page visited"))))
(defun acl2-doc-return ()
"Return to the last topic visited in the current buffer, popping
the stack of such topics. This command is buffer-local."
(interactive)
(cond (*acl2-doc-return*
(let ((entry (pop *acl2-doc-return*)))
(push entry *acl2-doc-history*)
(acl2-doc-display-basic entry)))
(t (error "Nothing to return to"))))
(defun acl2-doc-read-line ()
; Return the current line, and go to the end of it.
(forward-line 0)
(let ((beg (point)))
(end-of-line)
(buffer-substring beg (point))))
(defun acl2-doc-up ()
"Go to the parent of the current topic."
(interactive)
(switch-to-acl2-doc-buffer)
(let ((first-parent
(save-excursion
(goto-char (point-min))
(and (search-forward "Parent list: (" nil t)
(acl2-doc-topic-at-point)))))
(cond ((null first-parent)
(cond ((save-excursion
(goto-char (point-min))
(forward-line 1)
(member (acl2-doc-read-line)
'("Parent list: nil"
"Parent list: NIL"
"Parent list: ()")))
(error "Already at the root node of the manual"))
(t (error "Internal ACL2-Doc error in acl2-doc-up.
Please report this error to the ACL2 implementors."))))