forked from ProofGeneral/PG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoq.el
More file actions
3349 lines (2839 loc) · 134 KB
/
Copy pathcoq.el
File metadata and controls
3349 lines (2839 loc) · 134 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; coq.el --- Major mode for Coq proof assistant -*- coding: utf-8 -*-
;; This file is part of Proof General.
;; Portions © Copyright 1994-2012 David Aspinall and University of Edinburgh
;; Portions © Copyright 2003-2018 Free Software Foundation, Inc.
;; Portions © Copyright 2001-2017 Pierre Courtieu
;; Portions © Copyright 2010, 2016 Erik Martin-Dorel
;; Portions © Copyright 2011-2013, 2016-2017 Hendrik Tews
;; Portions © Copyright 2015-2017 Clément Pit-Claudel
;; Authors: Healfdene Goguen, Pierre Courtieu
;; Maintainer: Pierre Courtieu <Pierre.Courtieu@cnam.fr>
;; License: GPL (GNU GENERAL PUBLIC LICENSE)
;;; Commentary:
;;
;;; Code:
;; PG doesn't support using several backends at the same time.
;; FIXME: We really should set proof-assistant just because this file is
;; loaded (e.g. we should set it within coq-pg-setup instead), but
;; defpacustom and various others fail if executed before proof-assistant
;; is set.
(if (and (boundp 'proof-assistant)
(member proof-assistant '(nil "" "Coq" "coq")))
(proof-ready-for-assistant 'coq)
(message "Coq-PG error: Proof General already in use for %s" proof-assistant))
(require 'cl-lib)
(require 'span)
(eval-when-compile
(require 'proof-utils)
(require 'span)
(require 'outline)
(require 'newcomment)
(require 'etags))
(defvar proof-info) ; dynamic scope in proof-tree-urgent-action
(defvar action) ; dynamic scope in coq-insert-as stuff
(defvar string) ; dynamic scope in coq-insert-as stuff
(defvar old-proof-marker)
(defvar coq-keymap)
(defvar coq-one-command-per-line)
(defvar coq-auto-insert-as) ; defpacustom
(defvar coq-time-commands) ; defpacustom
(defvar coq-use-project-file) ; defpacustom
(defvar coq-use-editing-holes) ; defpacustom
(defvar coq-hide-additional-subgoals)
(require 'proof)
(require 'coq-system) ; load path, option, project file etc.
(require 'coq-syntax) ; font-lock, syntax categories (tactics, commands etc)
(require 'coq-local-vars) ; setting coq args via file variables
; (prefer _CoqProject file instead)
(require 'coq-abbrev) ; abbrev and coq specific menu
(require 'coq-seq-compile) ; sequential compilation of Requires
(require 'coq-par-compile) ; parallel compilation of Requires
(require 'coq-diffs) ; highlight proof diffs
(defvar prettify-symbols-alist)
;; ----- coq-shell configuration options
;;; Code:
;; debugging functions
;; (defun proofstack () (coq-get-span-proofstack (span-at (point) 'type)))
(defvar coq-debug nil)
;; End debugging
;; Obsolete
;;(defcustom coq-default-undo-limit 500
;; "Maximum number of Undo's possible when doing a proof."
;; :type 'number
;; :group 'coq)
(defcustom coq-user-init-cmd nil
"User defined init commands for Coq.
These are appended at the end of `coq-shell-init-cmd'."
:type '(repeat (cons (string :tag "command")))
:group 'coq)
(defcustom coq-optimise-resp-windows-enable t
"If non-nil (default) resize vertically response windw after each command."
:type 'boolean
:group 'coq)
;; Default coq is only Private_ and _subproof
(defcustom coq-search-blacklist-string ; add this? \"_ind\" \"_rect\" \"_rec\"
"\"Private_\" \"_subproof\""
"String for blacklisting strings from requests to Coq environment."
:type 'string
:group 'coq)
(defcustom coq-prefer-top-of-conclusion nil
"Prefer start of the conclusion over its end when displaying large goals.
Namely, goals that do not fit in the goals window."
:type 'boolean
:group 'coq)
;; this remembers the previous value of coq-search-blacklist-string, so that we
;; can cook a remove+add blacklist command each time the variable is changed.
;; initially we put it at current value of coq-search-blacklist-string.
(defvar coq-search-blacklist-string-prev coq-search-blacklist-string)
;TODO: remove Set Undo xx. It is obsolete since coq-8.5 at least.
;;`(,(format "Set Undo %s . " coq-default-undo-limit) "Set Printing Width 75.")
(defconst coq-shell-init-cmd
(append `(,(format "Add Search Blacklist %s. " coq-search-blacklist-string))
'("Set Suggest Proof Using. ")
coq-user-init-cmd)
"Command to initialize the Coq Proof Assistant.")
(require 'coq-syntax)
;; FIXME: Even if we don't use coq-indent for indentation, we still need it for
;; coq-script-parse-cmdend-forward/backward and coq-find-real-start.
(require 'coq-indent)
;; Command to reset the Coq Proof Assistant
(defconst coq-shell-restart-cmd "Reset Initial.\n ")
(defvar coq-shell-prompt-pattern
"\\(?:\n\\(?:[^\n\371]+\371\\|<prompt>[^\n]+</prompt>\\)\\)"
"*The prompt pattern for the inferior shell running coq.")
;; FIXME da: this was disabled (set to nil) -- why?
;; da: 3.5: add experimental
;; am:answer: because of bad interaction
;; with coq -R option.
(defvar coq-shell-cd nil
;; "Add LoadPath \"%s\"." ;; fixes unadorned Require (if .vo exists).
"*Command of the inferior process to change the directory.")
(defvar coq-shell-proof-completed-regexp "No\\s-+more\\s-+subgoals\\.\\|Subtree\\s-proved!\\|Proof\\s-completed"; \\|This subproof is complete
"*Regular expression indicating that the proof has been completed.")
(defvar coq-goal-regexp
"\\(============================\\)\\|\\(subgoal [0-9]+\\)\n")
(defconst coq-interrupt-regexp "User Interrupt."
"Regexp corresponding to an interrupt.")
(defcustom coq-end-goals-regexp-show-subgoals "\n(dependent evars:"
"Regexp for `proof-shell-end-goals-regexp' when showing all subgoals.
A setting of nil means show all output from Coq. See also option
`coq-hide-additional-subgoals'."
:type '(choice regexp (const nil))
:group 'coq)
(defcustom coq-end-goals-regexp-hide-subgoals
(concat "\\(\nsubgoal 2 \\)\\|\\(" coq-end-goals-regexp-show-subgoals "\\)")
"Regexp for `proof-shell-end-goals-regexp' when hiding additional subgoals.
See also option `coq-hide-additional-subgoals'."
:type '(choice regexp (const nil))
:group 'coq)
;;
;; prooftree customization
;;
(defgroup coq-proof-tree ()
"Coq specific customization for prooftree."
:group 'coq-config
:package-version '(ProofGeneral . "4.2"))
;; Ignore all commands that start a proof. Otherwise "Proof" will appear
;; as superfluous node in the proof tree. Note that we cannot ignore Proof,
;; because, Fixpoint does not display the proof goal, see Coq bug #2776.
(defcustom coq-proof-tree-ignored-commands-regexp
(concat "^\\(\\(Show\\)\\|\\(Locate\\)\\|"
"\\(Theorem\\)\\|\\(Lemma\\)\\|\\(Remark\\)\\|\\(Fact\\)\\|"
"\\(Corollary\\)\\|\\(Proposition\\)\\|\\(Definition\\)\\|"
"\\(Let\\)\\|\\(Fixpoint\\)\\|\\(CoFixpoint\\)\\)")
"Regexp for `proof-tree-ignored-commands-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-navigation-command-regexp
(concat "^\\(\\(Focus\\)\\|\\(Unfocus\\)\\|"
"\\(all\\s-*:\\s-*\\(cycle\\|swap\\|revgoals\\)\\)\\|"
"\\(\\+\\)\\|\\(-\\)\\|\\(\\*\\)\\|\\({\\)\\|\\(}\\)\\)")
"Regexp for `proof-tree-navigation-command-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-cheating-regexp
"\\(?:admit\\)\\|\\(?:give_up\\)"
"Regexp for `proof-tree-cheating-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-new-layer-command-regexp
"^\\(\\(Proof\\)\\|\\(Grab Existential Variables\\)\\)"
"Regexp for `proof-tree-new-layer-command-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-current-goal-regexp
(concat "^[0-9]+ \\(?:focused \\)?subgoal\\(?:s\\)?\\s-*"
"\\(?:(\\(?:unfocused: [-0-9]+\\)?,?"
"\\s-*\\(?:shelved: [-0-9]+\\)?)\\)?\\(?:\\s-*, subgoal 1\\)? "
"(ID \\([0-9]+\\))\n\\s-*\n\\(\\(?: .*\n\\)+\\)\\(?:\n\\|$\\)")
"Regexp for `proof-tree-current-goal-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-update-goal-regexp
(concat "^goal / evar \\([0-9]+\\) is:\n"
"\\s-*\n\\(\\(?:.+\n\\)*\\)\\(?:\n\\|$\\)")
"Regexp for `proof-tree-update-goal-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-additional-subgoal-ID-regexp
"^subgoal [0-9]+ (ID \\([0-9]+\\)) is:"
"Regexp for `proof-tree-additional-subgoal-ID-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-existential-regexp "\\(\\?[0-9]+\\)"
"Regexp for `proof-tree-existential-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-instantiated-existential-regexp
(concat coq-proof-tree-existential-regexp " using")
"Regexp for recognizing an instantiated existential variable."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-existentials-state-start-regexp
"^(dependent evars:"
"Coq instance of `proof-tree-existentials-state-start-regexp'."
:type 'regexp
:group 'coq-proof-tree)
(defcustom coq-proof-tree-existentials-state-end-regexp ")\n"
"Coq instance of `proof-tree-existentials-state-end-regexp'."
:type 'regexp
:group 'coq-proof-tree)
;; 8.4:
;; <infomsg>This subproof is complete, but there are still unfocused goals.</infomsg>
;;
;; 8.5:
;; <infomsg>
;; This subproof is complete, but there are some unfocused goals.
;; Focus next goal with bullet *.
;; </infomsg>
;;
;; <infomsg>No more subgoals, but there are some goals you gave up:</infomsg>
;;
;; <infomsg>All the remaining goals are on the shelf.</infomsg>
(defcustom coq-proof-tree-branch-finished-regexp
(concat "^\\(\\(?:Proof completed\\.\\)\\|"
"\\(?:\\(?:<infomsg>\\)?No more subgoals\\)\\|"
"\\(No more subgoals but non-instantiated "
"existential variables:\\)\\|"
"\\(?:<infomsg>All the remaining goals are on the shelf\\)\\|"
"\\(<infomsg>\\s-*This subproof is complete, but there are "
"\\(?:still\\|some\\) unfocused goals.\\)\\)")
"Regexp for `proof-tree-branch-finished-regexp'."
:type 'regexp
:group 'coq-proof-tree)
;;
;; Outline mode
;;
(defvar coq-shell-outline-regexp coq-goal-regexp)
(defvar coq-shell-outline-heading-end-regexp coq-goal-regexp)
(defconst coq-state-preserving-tactics-regexp
(proof-regexp-alt-list coq-state-preserving-tactics))
(defconst coq-state-changing-commands-regexp
(proof-regexp-alt-list coq-keywords-state-changing-commands))
(defconst coq-state-preserving-commands-regexp
(proof-regexp-alt-list coq-keywords-state-preserving-commands))
(defconst coq-commands-regexp
(proof-regexp-alt-list coq-keywords-commands))
(defvar coq-retractable-instruct-regexp
(proof-regexp-alt-list coq-retractable-instruct))
(defvar coq-non-retractable-instruct-regexp
(proof-regexp-alt-list coq-non-retractable-instruct))
;;
;; Derived modes
;;
(defvar coq-shell-mode-syntax-table coq-mode-syntax-table)
(defvar coq-response-mode-syntax-table coq-mode-syntax-table)
(defvar coq-goals-mode-syntax-table coq-mode-syntax-table)
(define-derived-mode coq-shell-mode proof-shell-mode
"Coq Shell" nil
(coq-shell-mode-config))
(define-derived-mode coq-response-mode proof-response-mode
"Coq Response" nil
(coq-response-config))
(define-derived-mode coq-goals-mode proof-goals-mode
"Coq Goals" nil
(coq-goals-mode-config))
;;
;; Auxiliary code for Coq modes
;;
;; finding the main frame of pg.
(defun coq-find-threeb-frames ()
"Return a list of frames displaying both response and goals buffers."
(let* ((wins-resp (get-buffer-window-list proof-response-buffer nil t))
(wins-gls (get-buffer-window-list proof-goals-buffer nil t))
(frame-resp (mapcar #'window-frame wins-resp))
(frame-gls (mapcar #'window-frame wins-gls)))
(filtered-frame-list (lambda (x) (and (member x frame-resp) (member x frame-gls))))))
(defun coq-remove-trailing-blanks (s)
(let ((pos (string-match "\\s-*\\'" s)))
(substring s 0 pos)))
(defun coq-remove-starting-blanks (s)
(string-match "\\`\\s-*" s)
(substring s (match-end 0) (length s)))
;; Messages dispalyed by "Time" are always following or preceding the real
;; result, if it follows we want it to be non urgent, otherwise the result
;; would not be shown in response buffer. If it is before, then we want it
;; urgent so that it is displayed.
(defvar coq-eager-no-urgent-regex "\\s-*Finished "
"Regexp of commands matching ‘proof-shell-eager-annotation-start’
that should maybe not be classified as urgent messages.")
;; return the end position if found, nil otherwise
(defun coq-non-urgent-eager-annotation ()
(save-excursion
(when (and (looking-at coq-eager-no-urgent-regex)
(re-search-forward proof-shell-eager-annotation-end nil t))
(let ((res (match-end 0))); robustify
;; if there is something else than a prompt here then this eager
;; annotation is left urgent (return nil), otherwise it is not urgent
;; (return position of the end of the annotation)
(when (looking-at (concat "\\s-*" proof-shell-annotated-prompt-regexp))
res)))))
;; Looking for eager annotation which does not match coq-eager-no-urgent-regex
(defun coq-search-urgent-message ()
"Find forward the next really urgent message.
Return the position of the beginning of the message (after the
annotation-start) if found."
(let ((again t) (found nil) (start-start nil) (end-end nil)
(eager proof-shell-eager-annotation-start))
(while again
(setq start-start (and (re-search-forward eager nil 'limit)
(match-beginning 0)))
(setq end-end (and start-start (coq-non-urgent-eager-annotation)))
(unless end-end
(setq again nil) ; exit while
;; display message (this prints the message once now but it will be
;; reprinted as a normal output, bad?)
;;(proof-shell-process-urgent-message start-start end-end)
))
(and start-start (goto-char start-start))))
;; This is a modified version of the same function in generic/proof-shell.el.
;; Using function coq-search-urgent-message instead of regex
;; proof-shell-eager-annotation-start, in order to let non urgent message as
;; such. i.e. "Time" messages.
(defun proof-shell-process-urgent-messages ()
"Scan the shell buffer for urgent messages.
Scanning starts from `proof-shell-urgent-message-scanner' or
`scomint-last-input-end', which ever is later. We deal with strings
between regexps `proof-shell-eager-annotation-start' and
`proof-shell-eager-annotation-end'.
We update `proof-shell-urgent-message-marker' to point to last message found.
This is a subroutine of `proof-shell-filter'."
(let ((pt (point)) (end t)
lastend laststart
(initstart (max (marker-position proof-shell-urgent-message-scanner)
(marker-position scomint-last-input-end))))
(goto-char initstart)
(while (and end (setq laststart (coq-search-urgent-message))
)
; (setq laststart (match-beginning 0))
(if (setq end
(re-search-forward proof-shell-eager-annotation-end nil t))
(save-excursion
(setq lastend end)
;; Process the region including the annotations
(proof-shell-process-urgent-message laststart lastend))))
(set-marker
proof-shell-urgent-message-scanner
(if end ;; couldn't find message start; move forward to avoid rescanning
(max (or lastend 1) ;; goto after last end urgent msg
;; or near the end of current output if that jumps farther.
(- (point)
(1+ proof-shell-eager-annotation-start-length)))
;; incomplete message; leave marker at start of message
laststart))
;; Set position of last urgent message found
(if lastend
(set-marker proof-shell-urgent-message-marker lastend))
(goto-char pt)))
;; Due to the architecture of proofgeneral, informative message put *before*
;; the goal disappear unless marked as "urgent", i.e. being enclosed with
;; "eager-annotation" syntax. Since we don't want the Warning color to be used
;; for simple informative message, we have to redefine this function to use
;; normal face when the "eager annotation" is acutally not a warning. This is a
;; modified version of the same function in generic/proof-shell.el.
(defun proof-shell-process-urgent-message-default (start end)
"A subroutine of `proof-shell-process-urgent-message'."
;; Clear the response buffer this time, but not next, leave window.
(pg-response-maybe-erase nil nil)
(proof-minibuffer-message
(buffer-substring-no-properties
(save-excursion
(re-search-forward proof-shell-eager-annotation-start end nil)
(point))
(min end
(save-excursion (end-of-line) (point))
(+ start 75))))
(let*
((face
(progn (goto-char start)
(if (looking-at "<infomsg>") 'default
'proof-eager-annotation-face)))
(str (proof-shell-strip-eager-annotations start end))
(strnotrailingspace
(coq-remove-starting-blanks (coq-remove-trailing-blanks str))))
(pg-response-display-with-face strnotrailingspace))) ; face
;; Trying to accept { and } as terminator for empty commands. Actually
;; I am experimenting two new commands "{" and "}" (without no
;; trailing ".") which behave like BeginSubProof and EndSubproof. The
;; absence of a trailing "." makes it difficult to distinguish between
;; "{" of normal coq code (implicits, records) and this the new
;; commands. We therefore define a coq-script-parse-function to this
;; purpose.
;; coq-end-command-regexp is ni coq-indent.el
(defconst coq-script-command-end-regexp coq-end-command-regexp)
;; "\\(?:[^.]\\|\\(?:\\.\\.\\)\\)\\.\\(\\s-\\|\\'\\)")
;; slight modification of proof-script-generic-parse-cmdend (one of the
;; candidate for proof-script-parse-function), to allow "{" and "}" to be
;; command terminator when the command is empty. TO PLUG: swith the comment
;; below and rename coq-script-parse-function2 into coq-script-parse-function
(defun coq-script-parse-function ()
"For `proof-script-parse-function' if `proof-script-command-end-regexp' set."
(coq-script-parse-cmdend-forward))
;;;;;;;;;;;;;;;;;;;;;;;;;; End of "{" and "} experiments ;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;; Freeze buffers ;;;;;;;;;;;;
;; For storing output of respnse and goals buffers into a permanent buffer.
(defun coq-clone-buffer-response-mode (s &optional erase)
(let ((already-existing (get-buffer s))
(nb (get-buffer-create s)))
(save-window-excursion
(switch-to-buffer nb)
(unless already-existing
(coq-response-mode)
(read-only-mode 0))
(goto-char (point-min))
(insert "\n************************************\n")
(goto-char (point-min)))
(if erase (copy-to-buffer nb (point-min) (point-max))
(append-to-buffer nb (point-min) (point-max)))
;; (set-window-point window pos)
nb))
;; copy the content of proof-response-buffer into the "response-freeze" buffer,
;; resetting its content if ERASE non nil.
(defun proof-store-buffer-win (buffer &optional erase)
(proof-with-current-buffer-if-exists buffer
(let ((newbuffer nil))
(set-buffer buffer)
(setq newbuffer (coq-clone-buffer-response-mode "*response-freeze*" erase))
(let ((win (display-buffer-other-frame newbuffer))
(win-point-min (save-window-excursion
(switch-to-buffer-other-frame newbuffer)
(point-min))))
(set-window-point win win-point-min)))))
(defun proof-store-response-win (&optional erase)
(interactive "P")
(proof-store-buffer-win proof-response-buffer erase))
(defun proof-store-goals-win (&optional erase)
(interactive "P")
(proof-store-buffer-win proof-goals-buffer erase))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; make this non recursive?
(defun build-list-id-from-string (s)
"Build a list of string from a string S of the form \"id1|id2|...|idn\"."
(if (or (not s) (string= s "")) '()
(let ((x (string-match (concat "\\(" coq-id-shy "\\)\\(?:|\\|\\'\\)\\(.*\\)") s)))
(if (not x) (error "Cannot extract list of ids from string")
(cons (match-string 1 s)
(build-list-id-from-string (match-string 2 s)))))))
;; Use the statenumber inside the coq prompt to backtrack more easily
(defun coq-last-prompt-info (s)
"Extract info from the coq prompt S. See `coq-last-prompt-info-safe'."
(let ((lastprompt (or s (error "No prompt !!?")))
(regex
(concat ">\\(" coq-id-shy "\\) < \\([0-9]+\\) |\\(\\(?:" coq-id-shy
"|?\\)*\\)| \\([0-9]+\\) < ")))
(when (string-match regex lastprompt)
(let ((current-proof-name (match-string 1 lastprompt))
(state-number (string-to-number (match-string 2 lastprompt)))
(proof-state-number (string-to-number (match-string 4 lastprompt)))
;; bind pending-proofs last, because build-list-id-from-string
;; modifies the match data
(pending-proofs
(build-list-id-from-string (match-string 3 lastprompt))))
(list state-number proof-state-number pending-proofs
(if pending-proofs current-proof-name nil))))))
(defun coq-last-prompt-info-safe ()
"Return a list with all informations from the last prompt.
The list contains in the following order the state number, the
proof stack depth, a list with the names of all pending proofs,
and as last element the name of the current proof (or nil if
there is none)."
(coq-last-prompt-info proof-shell-last-prompt))
(defvar coq-last-but-one-statenum 1
"The state number we want to put in a span.
This is the prompt number given *just before* the command was sent.
This variable remembers this number and will be updated when
used see coq-set-state-number.
Initially 1 because Coq initial state has number 1.")
(defvar coq-last-but-one-proofnum 1
"As for `coq-last-but-one-statenum' but for stack depth.")
(defvar coq-last-but-one-proofstack '()
"As for `coq-last-but-one-statenum' but for proof stack symbols.")
(defvar coq--retract-naborts 0
"The number of (possibly nested) goals to abort on retract.
This temporary variable is written by function `coq-find-and-forget'
and read by function `coq-empty-action-list-command'.")
(defsubst coq-get-span-statenum (span)
"Return the state number of the SPAN."
(span-property span 'statenum))
(defsubst coq-get-span-proofnum (span)
"Return the proof number of the SPAN."
(span-property span 'proofnum))
(defsubst coq-get-span-proofstack (span)
"Return the proof stack (names of pending proofs) of the SPAN."
(span-property span 'proofstack))
(defsubst coq-set-span-statenum (span val)
"Set the state number of the SPAN to VAL."
(span-set-property span 'statenum val))
(defsubst coq-get-span-goalcmd (span)
"Return the 'goalcmd flag of the SPAN."
(span-property span 'goalcmd))
(defsubst coq-set-span-goalcmd (span val)
"Set the 'goalcmd flag of the SPAN to VAL."
(span-set-property span 'goalcmd val))
(defsubst coq-set-span-proofnum (span val)
"Set the proof number of the SPAN to VAL."
(span-set-property span 'proofnum val))
(defsubst coq-set-span-proofstack (span val)
"Set the proof stack (names of pending proofs) of the SPAN to VAL."
(span-set-property span 'proofstack val))
(defsubst proof-last-locked-span ()
(with-current-buffer proof-script-buffer
(span-at (- (proof-unprocessed-begin) 1) 'type)))
;; Each time the state changes (hook below), (try to) put the state number in
;; the last locked span (will fail if there is already a number which should
;; happen when going back in the script). The state number we put is not the
;; last one because the last one has been sent by Coq *after* the change. We
;; use `coq-last-but-one-statenum' instead and then update it.
;;TODO update docstring and comment
(defun coq-open-goals-p ()
"Return non nil if we are inside a proof, see `proof-shell-open-goals-p'."
(car (cdr (cdr (coq-last-prompt-info-safe)))))
(defun coq-set-state-infos ()
"Set the last locked span's state number to the number found last time.
This number is in the *last but one* prompt (variable `coq-last-but-one-statenum').
If locked span already has a state number, then do nothing. Also updates
`coq-last-but-one-statenum' to the last state number for next time."
(if proof-shell-last-prompt
;; da: did test proof-script-buffer here, but that seems wrong
;; since restart needs to reset these values.
;; infos = promt infos of the very last prompt
;; sp = last locked span, which we want to fill with prompt infos
(let ((sp (if proof-script-buffer (proof-last-locked-span)))
(infos (or (coq-last-prompt-info-safe)
;; the line above seems to return nil sometimes, let us
;; issue a warning when this happens, so that we
;; understand why.
(and (display-warning
'proof-general
"oops nothing returned by (coq-last-prompt-info-safe)!!!" :debug) nil)
'(0 0 nil nil))))
(unless (or (not sp) (coq-get-span-statenum sp))
(coq-set-span-statenum sp coq-last-but-one-statenum))
(setq coq-last-but-one-statenum (car infos))
;; set goalcmd property if this is a goal start
;; (ie proofstack has changed and not a save cmd)
(unless
(or (not sp) (equal (span-property sp 'type) 'goalsave)
(<= (length (car (cdr (cdr infos))))
(length coq-last-but-one-proofstack)))
(coq-set-span-goalcmd sp t))
;; testing proofstack=nil is not good here because nil is the empty list OR
;; the no value, so we test proofnum as it is always set at the same time.
;; This is why this test is done before the next one (which sets proofnum)
(unless (or (not sp) (coq-get-span-proofnum sp))
(coq-set-span-proofstack sp coq-last-but-one-proofstack))
(setq coq-last-but-one-proofstack (car (cdr (cdr infos))))
(unless (or (not sp) (coq-get-span-proofnum sp))
(coq-set-span-proofnum sp coq-last-but-one-proofnum))
(setq coq-last-but-one-proofnum (car (cdr infos))))))
;; This hook seems the one we want.
;; WARNING! It is applied once after each command PLUS once before a group of
;; commands is started
(add-hook 'proof-state-change-pre-hook #'coq-set-state-infos)
(defun count-not-intersection (l notin)
"Return the number of elts of L that are not in NOTIN."
(let ((l1 l) (l2 notin) (res 0))
(while l1
(if (member (car l1) l2) ()
(setq res (+ res 1))) ; else
(setq l1 (cdr l1)))
res
))
;; Simplified version of backtracking which uses state numbers (Coq >= 8.4).
;; It uses the new coq command
;; "BackTo". The prompt is like this:
;; state proof stack
;; num depth
;; __ _
;; aux < 12 |aux|SmallStepAntiReflexive| 4 < \371
;; ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
;; usual pending proofs usual
;; special char
;; exemple:
;; to go (back) from 12 |lema1|lema2...|leman| xx
;; to 8 |lemb1|lemb2...|lembm| 5
;; we must do "BackTo 8" and set `coq--retract-naborts' to
;; naborts, i.e., the number of lemais that are not lembis
;; Rem: We could deal with suspend and resume with more work. We would need a new coq
;; command, because it is better to backtrack with *one* command (because
;; proof-change-hook used above is not exactly called at right times).
(defun coq-find-and-forget (span)
"Backtrack to SPAN. Using the \"BackTo n\" coq command."
(if (eq (span-property span 'type) 'proverproc)
;; processed externally (i.e. Require, etc), nothing to do
;; (should really be unlocked when we undo the Require).
nil
(let* ((naborts 0)
(proofdepth (coq-get-span-proofnum span))
(proofstack (coq-get-span-proofstack span))
(span-staten (coq-get-span-statenum span))
(naborts (count-not-intersection
coq-last-but-one-proofstack proofstack)))
;; if we move outside of any proof, coq does not print anything, so clean
;; the goals buffer otherwise the old one will still be displayed
(if (= proofdepth 0) (proof-clean-buffer proof-goals-buffer))
(setq coq--retract-naborts naborts)
(list
(format "BackTo %s . "
(int-to-string span-staten))))))
(defvar coq-current-goal 1
"Last goal that Emacs looked at.")
(defun coq-goal-hyp ()
(cond
((looking-at "============================\n")
(goto-char (match-end 0))
(cons 'goal (int-to-string coq-current-goal)))
((looking-at "subgoal \\([0-9]+\\) is:\n")
(goto-char (match-end 0))
(cons 'goal (match-string 1)) ;FIXME: This is dead-code!? --Stef
(setq coq-current-goal (string-to-number (match-string 1))))
((proof-looking-at proof-shell-assumption-regexp)
(cons 'hyp (match-string 1)))
(t nil)))
(defun coq-state-preserving-p (cmd)
;; (or
(proof-string-match coq-non-retractable-instruct-regexp cmd))
;; (and
;; (not (proof-string-match coq-retractable-instruct-regexp cmd))
;; (or
;; (message "Unknown command, hopes this won't desynchronize ProofGeneral")
;; t))))
(defun coq-hide-additional-subgoals-switch ()
"Function invoked when the user switches option `coq-hide-additional-subgoals'."
(if coq-time-commands
(progn
(setq coq-hide-additional-subgoals nil)
(error
"You must disable ``Time Commands'' (var coq-time-commands) first"))
(if coq-hide-additional-subgoals
(setq proof-shell-end-goals-regexp coq-end-goals-regexp-hide-subgoals)
(setq proof-shell-end-goals-regexp coq-end-goals-regexp-show-subgoals))))
(defun coq-time-commands-switch ()
"Function invoked when the user switches option `coq-time-commands'.
Reset option `coq-hide-additional-subgoals' and puts nil into
`proof-shell-end-goals-regexp' to ensure the timing is visible in
the *goals* buffer."
(if coq-time-commands
(progn
(let ((coq-time-commands nil))
(customize-set-variable 'coq-hide-additional-subgoals nil))
(setq proof-shell-end-goals-regexp nil))
(coq-hide-additional-subgoals-switch)))
;;
;; Commands for Coq
;;
(defconst notation-print-kinds-table
'(("Print Scope(s)" 0) ("Print Visibility" 1))
"Enumerates the different kinds of notation information one can ask to coq.")
(defun coq-PrintScope ()
"Show information on notations. Coq specific."
(interactive)
(let*
((mods
(completing-read "Infos on notation (TAB to see list): "
notation-print-kinds-table))
(s (read-string "Name (empty for all): "))
(all (string-equal s "")))
(cond
((and (string-equal mods "Print Scope(s)") (string-equal s ""))
(proof-shell-invisible-command (format "Print Scopes.")))
(t
(proof-shell-invisible-command (format "%s %s ." mods s)))
)
)
)
(defun coq-remove-trailing-dot (s)
"Return the string S without its trailing \".\" if any.
Return nil if S is nil."
(if (and s (string-match "\\.\\'" s))
(substring s 0 (- (length s) 1))
s))
(defun coq-remove-heading-quote (s)
"Return the string S without its heading \"\'\" if any.
Return nil if S is nil."
(if (and s (string-match "\\`'" s))
(substring s 1 (length s))
s))
(defun coq-clean-id-at-point (s)
(coq-remove-heading-quote (coq-remove-trailing-dot s)))
(defun coq-is-symbol-or-punct (c)
"Return non nil if character C is a punctuation or a symbol constituent.
If C is nil, return nil."
(when c
(or (equal (char-syntax c) ?\.) (equal (char-syntax c) ?\_))))
(defun coq-grab-punctuation-left (pos)
"Return a string made of punctuations chars found immediately before position POS."
(let ((res nil)
(currpos pos))
(while (coq-is-symbol-or-punct (char-before currpos))
(setq res (concat (char-to-string (char-before currpos)) res))
(setq currpos (- currpos 1)))
res))
(defun coq-grab-punctuation-right (pos)
"Return a string made of punctuations chars found immediately after position POS."
(let ((res nil)
(currpos pos))
(while (coq-is-symbol-or-punct (char-after currpos))
(setq res (concat res (char-to-string (char-after currpos))))
(setq currpos (+ currpos 1)))
res))
(defun coq-notation-at-position (pos)
"Return the notation at current point.
Support dot.notation.of.modules."
(coq-with-altered-syntax-table
(when (or (coq-grab-punctuation-left pos) (coq-grab-punctuation-right pos))
(concat (coq-grab-punctuation-left pos)
(coq-grab-punctuation-right pos)))))
(defun coq-string-starts-with-symbol (s)
(eq 0 (string-match "\\s_" s)))
;; remove trailing dot if any.
(defun coq-id-at-point ()
"Return the identifier at current point.
Support dot.notation.of.modules."
(coq-with-altered-syntax-table
(let* ((symb (cond
((fboundp 'symbol-near-point) (symbol-near-point))
((fboundp 'symbol-at-point) (symbol-at-point))))
(symbclean (when symb (coq-clean-id-at-point (symbol-name symb)))))
(when (and symb (not (zerop (length symbclean))))
symbclean))))
(defun coq-id-or-notation-at-point ()
(or (coq-id-at-point)
(let ((notation (coq-notation-at-position (point))))
(if notation (concat "\"" notation "\"") ""))))
(defcustom coq-remap-mouse-1 nil
"Whether Coq mode should remap mouse button 1 to Coq queries.
This overrides the default global binding of (control mouse-1) and
\(shift mouse-1) (buffers and faces menus). Hence it is nil by
default."
:type 'boolean
:group 'coq)
;; On a mouse event, try to query the id at point clicked.
(defun coq-id-under-mouse-query (event)
"Query the prover about the identifier or notation near mouse click EVENT.
This is mapped to control/shift mouse-1, unless coq-remap-mouse-1
is nil (t by default)."
(interactive "e")
(save-selected-window
(save-excursion
(mouse-set-point event)
(let* ((id (coq-id-at-point))
(notat (coq-notation-at-position (point)))
(modifs (event-modifiers event))
(shft (member 'shift modifs))
(ctrl (member 'control modifs))
(cmd (when (or id notat)
(if (and ctrl shft) (if id "Check" "Locate")
(if shft (if id "About" "Locate")
(if ctrl (if id "Print" "Locate")))))))
(proof-shell-invisible-command
(format (concat cmd " %s . ")
;; Notation need to be surrounded by ""
(if id id (concat "\"" notat "\""))))))))
(defun coq-guess-or-ask-for-string (s &optional dontguess)
"Asks for a coq identifier with message S.
If DONTGUESS is non nil, propose a default value as follows:
If region is active, propose its containt as default value.
Otherwise propose identifier at point if any."
(let* ((guess
(cond
(dontguess nil)
((use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end)))
(t (coq-id-or-notation-at-point)))))
(read-string
(if guess (concat s " (default " guess "): ") (concat s ": "))
nil 'proof-minibuffer-history guess)))
(defun coq-ask-do (ask do &optional dontguess postformatcmd wait)
"Ask for an ident and print the corresponding term."
(let* ((cmd) (postform (if (eq postformatcmd nil) 'identity postformatcmd)))
(proof-shell-ready-prover)
(setq cmd (coq-guess-or-ask-for-string ask dontguess))
(proof-shell-invisible-command
(format (concat do " %s . ") (funcall postform cmd))
wait)))
(defun coq-flag-is-on-p (testcmd)
(proof-shell-ready-prover)
(proof-shell-invisible-command (format testcmd) 'wait)
(let ((resp proof-shell-last-response-output))
(string-match "is on\\>" resp)))
(defun coq-command-with-set-unset (setcmd cmd unsetcmd &optional postformatcmd testcmd)
"Play commands SETCMD then CMD and then silently UNSETCMD.
The last UNSETCMD is performed with tag 'empty-action-list so that it
does not trigger ‘proof-shell-empty-action’ (which does \"Show\" at
the time of writing this documentation)."
(let* ((postform (if (eq postformatcmd nil) 'identity postformatcmd))
(flag-is-on (and testcmd (coq-flag-is-on-p testcmd))))
;; We put 'empty-action-list tags on all three commands since we don't want
;; to trigger "Show" or anything that we usually insert after a group of
;; commands.
(unless flag-is-on (proof-shell-invisible-command
(format " %s . " (funcall postform setcmd))
nil nil 'no-response-display 'empty-action-list))
(proof-shell-invisible-command
(format " %s . " (funcall postform cmd)) 'wait nil 'empty-action-list)
(unless flag-is-on (proof-shell-invisible-command
(format " %s . " (funcall postform unsetcmd))
'waitforit nil 'no-response-display 'empty-action-list))))
(defun coq-ask-do-set-unset (ask do setcmd unsetcmd
&optional dontguess postformatcmd tescmd)
"Ask for an ident id and execute command DO in SETCMD mode.
More precisely it executes SETCMD, then DO id and finally silently
UNSETCMD. See `coq-command-with-set-unset'."
(let* ((cmd) (postform (if (eq postformatcmd nil) 'identity postformatcmd tescmd)))
(proof-shell-ready-prover)
(setq cmd (coq-guess-or-ask-for-string ask dontguess))
(coq-command-with-set-unset setcmd (concat do " " cmd) unsetcmd postformatcmd)))
(defun coq-ask-do-show-implicits (ask do &optional dontguess postformatcmd)
"Ask for an ident and print the corresponding term."
(coq-ask-do-set-unset ask do
"Set Printing Implicit"
"Unset Printing Implicit"
dontguess postformatcmd
"Test Printing Implicit"))
(defun coq-ask-do-show-all (ask do &optional dontguess postformatcmd)
"Ask for an ident and print the corresponding term."
(coq-ask-do-set-unset ask do
"Set Printing All"
"Unset Printing All"
dontguess postformatcmd
"Test Printing All"))
;; (let* ((cmd) (postform (if (eq postformatcmd nil) 'identity postformatcmd)))
;; (proof-shell-ready-prover)
;; (setq cmd (coq-guess-or-ask-for-string ask dontguess))
;; (coq-command-with-set-unset
;; "Set Printing Implicit"
;; (format (concat do " %s . ") cmd)
;; "Unset Printing Implicit" )
;; ))
(defsubst coq-put-into-brackets (s)
(concat "[ " s " ]"))
(defsubst coq-put-into-double-quote-if-notation (s)
(if (coq-is-symbol-or-punct (string-to-char s))
(concat "\"" s "\"")
s))
(defun coq-build-removed-pattern (s)
(concat " -\"" s "\""))