aboutsummaryrefslogtreecommitdiffhomepage
path: root/generic/pg-user.el
blob: a5ed27a9d0a1c6e678689d3ce1e97cd90fc4938e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
;;; pg-user.el --- User level commands for Proof General  -*- lexical-binding:t -*-

;; 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

;; Author:     David Aspinall and others

;; License:    GPL (GNU GENERAL PUBLIC LICENSE)

;;; Commentary:
;;
;; This file defines some user-level commands.  Most of them
;; are script-based operations.  Exported user-level commands
;; are defined here as autoloads to avoid circular requires.

;;; Code:

(require 'span)
(require 'scomint)

(require 'proof-script)	        ; we build on proof-script

(eval-when-compile (require 'cl-lib))   ;cl-decf
(defvar which-func-modes)               ; Defined by which-func.

(declare-function proof-segment-up-to "proof-script")
(declare-function proof-interrupt-process "proof-shell")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Utilities: moving point in proof script buffer
;;

;; First: two commands for moving forwards in proof scripts.  Moving
;; forward for a "new" command may insert spaces or new lines.  Moving
;; forward for the "next" command does not.

;;;###autoload
(defun proof-script-new-command-advance ()
  "Move point to a nice position for a new command, possibly inserting spaces.
Assumes that point is at the end of a command.
No effect if `proof-next-command-insert-space' is nil."
  (interactive)
  (when proof-next-command-insert-space
    (let (sps)
      (if (and (proof-next-command-new-line)
	       (setq sps (skip-chars-forward " \t"))
	       ;; don't break existing lines
	       (eolp))
	  (progn (newline)
		 (unless proof-next-command-on-new-line
		   (indent-relative))))
      (unless (proof-next-command-new-line)
	;; Multiple commands per line: skip spaces at point, and insert
	;; the 1/0 number of spaces that were skipped in front of point
	;; (at least one).  This has the pleasing effect that the spacing
	;; policy of the current line is copied: e.g.  <command>;
	;; <command>; Tab columns don't work properly, however.
	(let ((newspace (max (or sps 1) (skip-chars-forward " \t")))
	      (p (point)))
	  (insert-char ?\040 newspace)
	  (goto-char p))))))


(defun proof-maybe-follow-locked-end (&optional pos)
  "Move point according to `proof-follow-mode'.
If optional POS is set, use that position, else `proof-queue-or-locked-end'.
Assumes script buffer is current."
  (unless (or proof-autosend-running (eq proof-follow-mode 'ignore))
    (let ((dest (or pos (proof-queue-or-locked-end))))
      (cond
       ((eq proof-follow-mode 'locked)
	(goto-char dest)
	(or pos (proof-script-next-command-advance)))
       ((eq proof-follow-mode 'follow)
	(unless (pos-visible-in-window-p dest)
	  (let ((win (get-buffer-window (current-buffer) t)))
	    (if win
		(set-window-point win dest)))))
       ((and (eq proof-follow-mode 'followdown)
	     (> dest (point)))
	(goto-char dest))))))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Further movement commands
;;

(defun proof-goto-command-start ()
  "Move point to start of current (or final) command of the script."
  (interactive)
  (let* ((cmd (span-at (point) 'type))
	 (start (if cmd (span-start cmd))))
    (if start
	(progn
	  ;; BUG: only works for unclosed proofs.
	  (goto-char start))
      (let ((semis (nth 1 (proof-segment-up-to (point) t))))
	(if (eq 'unclosed-comment (car-safe semis))
	    (setq semis (cdr-safe semis)))
	(if (nth 2 semis) ; fetch end point of previous command
	      (goto-char (nth 2 semis))
	  ;; no previous command: just next to end of locked
	  (goto-char (proof-unprocessed-begin)))))
    ;; Oddities of this function: if we're beyond the last proof
    ;; command, it jumps back to the last command.  Could alter this
    ;; by spotting that command end of last of semis is before
    ;; point.  Also, behaviour with comments is different depending
    ;; on whether locked or not.
    (skip-chars-forward " \t\n")))

(defun proof-goto-command-end ()
  "Set point to end of command at point."
  (interactive)
  (let ((cmd (span-at (point) 'type)))
    (if cmd
	(goto-char (span-end cmd))
      (let ((semis (save-excursion
		     (proof-segment-up-to-using-cache (point)))))
	(if semis
	    (progn
	      (goto-char (nth 2 (car semis)))
	      (skip-chars-backward " \t\n")
	      (unless (eq (point) (point-min))
		(backward-char))))))))

(defun proof-forward-command (&optional num)
  "Move forward to the start of the next proof region.
If called interactively, NUM is given by the prefix argument."
  (interactive "p")
  (skip-chars-forward " \t\n")
  (let* ((span  (or (span-at (point) 'type)
		    (and (skip-chars-backward " \t\n")
			 (> (point) (point-min))
			 (span-at (1- (point)) 'type))))
	 (nextspan (and span (pg-numth-span-higher-or-lower
			      (pg-control-span-of span) num 'noerr))))
    (cond
     ((and nextspan (> num 0))
      (goto-char (span-start nextspan))
      (skip-chars-forward " \t\n"))
     ((and nextspan (< num 0))
      (goto-char (span-end nextspan)))
     ((and span (> num 0))
      (goto-char (span-end span)))
     ((and span (< num 0))
      (goto-char (span-start span))))))

(defun proof-backward-command (&optional num)
  (interactive "p")
  (proof-forward-command (- num)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Processing commands
;;

;;;###autoload
(defun proof-goto-point ()
  "Assert or retract to the command at current position.
Calls `proof-assert-until-point' or `proof-retract-until-point' as
appropriate."
  (interactive)
  (save-excursion
    (if (> (proof-queue-or-locked-end) (point))
	(proof-retract-until-point)
      (if (proof-only-whitespace-to-locked-region-p)
	  (progn
	    (skip-chars-forward " \t\n")
	    (forward-char 1)))
      (proof-assert-until-point))))

(defun proof-assert-next-command-interactive ()
  "Process until the end of the next unprocessed command after point.
If inside a comment, just process until the start of the comment."
  (interactive)
  (proof-with-script-buffer ; for toolbar/other buffers
   (save-excursion
     (goto-char (proof-queue-or-locked-end))
     (skip-chars-forward " \t\n")
     (proof-assert-until-point))
   (proof-maybe-follow-locked-end)))

;; NB: "interactive" variant merely for a simple docstring.
(defun proof-assert-until-point-interactive ()
  "Process the region from the end of the locked-region until point.
If inside a comment, just process until the start of the comment."
  (interactive)
  (proof-assert-until-point))

;;;###autoload
(defun proof-process-buffer ()
  "Process the current (or script) buffer, and maybe move point to the end."
  (interactive)
  (proof-with-script-buffer
   (save-excursion
     (goto-char (point-max))
     (proof-assert-until-point-interactive))
   (proof-maybe-follow-locked-end))
  (when proof-fast-process-buffer
    (message "Processing buffer...")
    (proof-shell-wait)
    (message "Processing buffer...done")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Undoing commands
;;

(defun proof-undo-last-successful-command ()
  "Undo last successful command at end of locked region."
  (interactive)
  (proof-undo-last-successful-command-1))

(defun proof-undo-and-delete-last-successful-command ()
  "Undo and delete last successful command at end of locked region.
Useful if you typed completely the wrong command.
Also handy for proof by pointing, in case the last proof-by-pointing
command took the proof in a direction you don't like.

Notice that the deleted command is put into the Emacs kill ring, so
you can use the usual `yank' and similar commands to retrieve the
deleted text."
  (interactive)
  (proof-undo-last-successful-command-1 'kill-region))

(defun proof-undo-last-successful-command-1 (&optional undo-action)
  "Undo last successful command at end of locked region.
If optional UNDO-ACTION is non-nil, that function is called on
the text region in the proof script after undoing."
  (interactive "P")
  (proof-with-script-buffer
   (let (lastspan)
     (save-excursion
       (unless (proof-locked-region-empty-p)
	 (if (setq lastspan (span-at-before (proof-unprocessed-begin) 'type))
	     (progn
	       (goto-char (span-start lastspan))
	       (proof-retract-until-point undo-action))
	   (error "Nothing to undo!"))))
     (if lastspan (proof-maybe-follow-locked-end
		   (span-start lastspan))))))

(defun proof-retract-buffer (&optional called-interactively)
  "Retract the current buffer, and maybe move point to the start.
Point is only moved according to `proof-follow-mode', if
CALLED-INTERACTIVELY is non-nil, which is the case for all
interactive calls."
  ;; The numeric prefix argument "p" is never nil,
  ;; see Section  "Distinguish Interactive Calls" in the Elisp manual.
  (interactive "p")
  (proof-with-script-buffer
   (save-excursion
    (goto-char (point-min))
    (proof-retract-until-point-interactive))
   (if called-interactively
       (proof-maybe-follow-locked-end (point-min)))))

(defun proof-retract-current-goal ()
  "Retract the current proof, and move point to its start."
  (interactive)
   (let
      ((span (proof-last-goal-or-goalsave)))
     (save-excursion
       (if (and span (not (eq (span-property span 'type) 'goalsave))
		(< (span-end span) (proof-unprocessed-begin)))
	   (progn
	     (goto-char (span-start span))
	     (proof-retract-until-point-interactive))
	 (error "Not proving")))
     (if span (proof-maybe-follow-locked-end (span-start span)))))


;;
;; Mouse functions
;;

(defun proof-mouse-goto-point (event)
  "Call `proof-goto-point' on the click position EVENT."
  (interactive "e")
  (proof-with-script-buffer
   (mouse-set-point event)
   (proof-goto-point)
   (proof-maybe-follow-locked-end)))




;;
;; Minibuffer non-scripting command
;;

(defvar proof-minibuffer-history nil
  "History of proof commands read from the minibuffer.")

(defun proof-minibuffer-cmd (cmd)
  "Send CMD to proof assistant.  Interactively, read from minibuffer.
The command isn't added to the locked region.

If a prefix arg is given and there is a selected region, that is
pasted into the command.  This is handy for copying terms, etc from
the script.

If `proof-strict-state-preserving' is set, and `proof-state-preserving-p'
is configured, then the latter is used as a check that the command
will be safe to execute, in other words, that it won't ruin
synchronization.  If when applied to the command it returns false,
then an error message is given.

WARNING: this command risks spoiling synchronization if the test
`proof-state-preserving-p' is not configured, if it is
only an approximate test, or if `proof-strict-state-preserving'
is off (nil)."
  (interactive
   (list (read-string "Command: "
		      (if (and current-prefix-arg (region-active-p))
			  (replace-regexp-in-string
			   "[ \t\n]+" " "
			   (buffer-substring (region-beginning) (region-end))))
		      'proof-minibuffer-history)))
  (if (and proof-strict-state-preserving
	   proof-state-preserving-p
	   (not (funcall proof-state-preserving-p cmd)))
      (error "Command is not state preserving, I won't execute it!"))
  (proof-shell-invisible-command cmd))


;;
;; Frobbing locked end
;;

;; In fact, it's so risky, we'll disable it by default
(put 'proof-frob-locked-end 'disabled t)

(defun proof-frob-locked-end ()
  "Move the end of the locked region backwards to regain synchronization.
Only for use by consenting adults.

This command can be used to repair synchronization in case something
goes wrong and you want to tell Proof General that the proof assistant
has processed less of your script than Proof General thinks.

You should only use it to move the locked region to the end of
a proof command."
  (interactive)
  (cond
   (proof-shell-busy
    (error "You can't use this command while %s is busy!" proof-assistant))
   ((not (eq (current-buffer) proof-script-buffer))
    (error "Must be in the active scripting buffer"))
   (t (proof-set-locked-end (point))
      (span-delete-spans (proof-unprocessed-begin) (point-max) 'type))))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Non-scripting proof assistant commands.
;;;

;; These are based on defcustom'd settings so that users may
;; re-configure the system to their liking.


;;
;; Helper macros and functions
;;

;; See put expression at end to give this indentation like while form
(defmacro proof-if-setting-configured (var &rest body)
  "Give error if a configuration setting VAR is unset, otherwise eval BODY."
  `(if ,var
       (progn ,@body)
     (error "Proof General not configured for this: set %s"
	    ,(symbol-name var))))

;;;###autoload
(defmacro proof-define-assistant-command (fn doc cmdvar &optional body)
  "Define FN (docstring DOC): check if CMDVAR is set, then send BODY to prover.
BODY defaults to CMDVAR, a variable."
  `(defun ,fn ()
     ,(concat doc
	      (concat "\nIssues a command to the assistant based on "
		      (symbol-name cmdvar) ".")
		"")
     (interactive)
     (proof-if-setting-configured ,cmdvar
       (proof-shell-invisible-command ,(or body cmdvar)))))

;;;###autoload
(defmacro proof-define-assistant-command-witharg (fn doc cmdvar prompt &rest body)
  "Define FN (arg) with DOC: check CMDVAR is set, PROMPT a string and eval BODY.
The BODY can contain occurrences of arg.
CMDVAR is a variable holding a function or string.  Automatically has history."
  `(progn
     (defvar ,(intern (concat (symbol-name fn) "-history")) nil
       ,(concat "History of arguments for " (symbol-name fn) "."))
     (defun ,fn (arg)
     ,(concat doc "\nIssues a command based on ARG to the assistant, using "
	      (symbol-name cmdvar) ".\n"
	      "The user is prompted for an argument.")
      (interactive
       (proof-if-setting-configured ,cmdvar
	   (if (stringp ,cmdvar)
	       (list (format ,cmdvar
			 (read-string
			   ,(concat prompt ": ") ""
			   ,(intern (concat (symbol-name fn) "-history")))))
	     (funcall ,cmdvar))))
       ,@body)))

(defun proof-issue-new-command (cmd)
  "Insert CMD into the script buffer and issue it to the proof assistant.
If point is in the locked region, move to the end of it first.
Start up the proof assistant if necessary."
  (proof-with-script-buffer
   (if (proof-shell-live-buffer)
       (if (proof-in-locked-region-p)
	   (proof-goto-end-of-locked t)))
   (proof-script-new-command-advance)
   (insert cmd)
   (proof-assert-until-point-interactive)
   (proof-script-new-command-advance)))

;;
;; Commands which do not require a prompt and send an invisible
;; command.
;;

(proof-define-assistant-command proof-prf
  "Show the current proof state."
  proof-showproof-command
  (progn
    (pg-goals-buffers-hint)
    proof-showproof-command))

(proof-define-assistant-command proof-ctxt
  "Show the current context."
  proof-context-command)

(proof-define-assistant-command proof-help
  "Show a help or information message from the proof assistant.
Typically, a list of syntax of commands available."
  proof-info-command)

(proof-define-assistant-command proof-cd
  "Change directory to the default directory for the current buffer."
  proof-shell-cd-cmd
  (proof-format-filename proof-shell-cd-cmd
	  default-directory))

(defun proof-cd-sync ()
  "If `proof-shell-cd-cmd' is set, do `proof-cd' and wait for prover ready.
This is intended as a value for `proof-activate-scripting-hook'"
  ;; The hook is set in proof-mode before proof-shell-cd-cmd may be set,
  ;; so we explicitly test it here.
  (when proof-shell-cd-cmd
    (proof-cd)
    (proof-shell-wait)))

;;
;; Commands which require an argument, and maybe affect the script.
;;

(proof-define-assistant-command-witharg proof-find-theorems
 "Search for items containing given constants."
 proof-find-theorems-command
 "Find theorems containing"
 (proof-shell-invisible-command arg))

(proof-define-assistant-command-witharg proof-issue-goal
 "Write a goal command in the script, prompting for the goal."
 proof-goal-command
 "Goal" ; Goals always start at a new line
 (let ((proof-next-command-on-new-line t))
   (proof-issue-new-command arg)))

(proof-define-assistant-command-witharg proof-issue-save
 "Write a save/qed command in the script, prompting for the theorem name."
 proof-save-command
 "Save as" ; Saves always start at a new line
 (let ((proof-next-command-on-new-line t))
   (proof-issue-new-command arg)))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Electric terminator mode
;;

;; Register proof-electric-terminator as a minor mode.
(or (assq 'proof-electric-terminator-enable minor-mode-alist)
    (setq minor-mode-alist
	  (append minor-mode-alist
		  (list '(proof-electric-terminator-enable
			  (:eval
			   (if (eq major-mode proof-mode-for-script)
			       proof-terminal-string)))))))

;;;###autoload
(defun proof-electric-terminator-enable (&optional arg)
  "Ensure modeline update to display new value for electric terminator.
This a function is called by the custom-set property 'proof-set-value.
It can also be used as a minor mode function: with ARG, turn on iff ARG>0"
  (unless (null arg)
    (setq proof-electric-terminator-enable (> (prefix-numeric-value arg) 0)))
  (force-mode-line-update))

(proof-deftoggle proof-electric-terminator-enable
		 proof-electric-terminator-toggle)

(defun proof-electric-terminator (&optional count)
  "Insert terminator char, maybe sending the command to the assistant.
If we are inside a comment or string, insert the terminator.
Otherwise, if the variable `proof-electric-terminator-enable'
is non-nil, the command will be sent to the assistant.

To side-step the electric action and possibly insert multiple characters,
pass a non-nil COUNT arg, e.g. a numeric prefix such as M-3 <terminator>."
  (interactive "P")
  (if (and
       (not count)
       proof-electric-terminator-enable
       (not (proof-inside-comment (point)))
       (not (proof-inside-string (point))))
      (proof-assert-electric-terminator)
    (self-insert-command (prefix-numeric-value count))))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Completion based on <PA>-completion-table
;;
;; Requires completion.el package.  Completion is usually a hand-wavy
;; thing, so we don't attempt to maintain a precise completion table.
;;

(defun proof-add-completions ()
  "Add completions from <PA>-completion-table to completion database.
Uses `add-completion' with a negative number of uses and ancient
last use time, to discourage saving these into the users database."
  (interactive)
  (require 'completion)
  (defvar completion-min-length)
  (declare-function add-completion "completion"
                    (string &optional num-uses last-use-time))
  (mapcar (lambda (cmpl)
	    ;; completion gives error; trapping is tricky so test again
	    (if (>= (length cmpl) completion-min-length)
		(add-completion cmpl -1000 0)))
	  (proof-ass completion-table)))

;; NB: completion table is expected to be set when proof-script
;; is loaded!  Call `proof-script-add-completions' to update.

(unless (bound-and-true-p byte-compile-current-file) ;FIXME: Yuck!
  (eval-after-load "completion"
    '(proof-add-completions)))

(defun proof-script-complete (&optional arg)
  "Like `complete' but case-fold-search set to `proof-case-fold-search'."
  (interactive "*p")
  ;; completion not autoloaded in GNU Emacs
  ;; FIXME: It's probably because we shouldn't use it ;-)
  (require 'completion)
  (declare-function complete "completion" (&optional arg))
  (let ((case-fold-search proof-case-fold-search))
    (complete arg)))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Span manipulation
;;

(defun pg-copy-span-contents (span)
  "Copy contents of SPAN to kill ring, sans surrounding whitespace."
  (copy-region-as-kill
   (save-excursion
     (goto-char (span-start span))
     (skip-chars-forward " \t\n")
     (point))
   (save-excursion
     (goto-char (span-end span))
     (skip-chars-backward " \t\n")
     (point)))
  (if (fboundp 'own-clipboard)		;; XEmacs function
      (own-clipboard (car kill-ring))))

(defun pg-numth-span-higher-or-lower (span num &optional noerr)
  "Find NUM'th span after/before SPAN.  NUM is positive for after."
  (unless (and span (<= (span-end span) (proof-unprocessed-begin)))
    (if noerr
	nil
      (error "No processed region under point")))
  (let ((downflag (> num 0))
	(num      (abs num))
	nextspan)
    (while (and (> num 0)
		(setq nextspan (if downflag
				   (next-span span 'type)
				 (prev-span span 'type)))
		(if downflag
		    ;; If moving down, check we don't go beyond
		    ;; end of processed region
		    (<= (span-end span) (proof-unprocessed-begin))
		  t))
      (setq num (1- num))
      (setq span nextspan))
    (if (= num 0)
	span
      (if noerr
	  nil
	(error "No region to move past")))))

(defun pg-control-span-of (span)
  "Return the controlling span of SPAN, or SPAN itself."
  (or (span-property span 'controlspan)
      span))

;; Really a drag-and-drop interface for this would be nice.
(defun pg-move-span-contents (span num)
  "Move SPAN up/downwards in the buffer, past NUM spans.
If NUM is negative, move upwards.  Return new span."
  (save-excursion
    (let  ((downflag (> num 0)) nextspan)
      ;; Always move a control span instead; it contains
      ;; children span which move together with it.
      (setq span (pg-control-span-of span))
      (setq nextspan (pg-numth-span-higher-or-lower span num))
      ;; We're going to move the span to before/after nextspan.
      ;; First make sure inserting there doesn't extend the span.
      (if downflag
	  (span-set-property nextspan 'end-open t)
	(span-set-property nextspan 'start-open t))
      ;; When we delete the span, we want to duplicate it
      ;; to recreate in the new position.
      (span-set-property span 'duplicable 't)
      ;; TODO: this is faulty: moving span up gives children
      ;; list with single nil element.  Hence liveness test
      (mapc (lambda (s) (if (span-live-p s)
			    (span-set-property s 'duplicable 't)))
	      (span-property span 'children))
      (let* ((start     (span-start span))
	     (end       (span-end span))
	     (contents  (buffer-substring start end))
	     ;; Locked end may move up when we delete
	     ;; region: we'll make sure to reset it
	     ;; again later, it shouldn't change.
	     ;; NB: (rely on singlethreadedness here, so
	     ;; lockedend doesn't move while in this code).
	     (lockedend (span-end proof-locked-span)))
	(let ((inhibit-read-only t))
	  ;; TODO: undo behaviour isn't quite right yet.
	  (undo-boundary)
	  (delete-region start end)
	  (let ((insertpos (if downflag
			       (span-end nextspan)
			     (span-start nextspan))))
	    (goto-char insertpos)
	    ;; Let XEmacs duplicate extents as needed, then repair
	    ;; their associations
	    (insert contents)
	    (let ((new-span
		   (span-at insertpos 'type)));should be one we deleted.
	      (span-set-property new-span 'children
				 (pg-fixup-children-spans
				  new-span insertpos (point)))
	      (span-set-end proof-locked-span lockedend)
	      (undo-boundary)
	      new-span)))))))

(defun pg-fixup-children-spans (new-parent start end)
  (append
   (span-mapcar-spans
    (lambda (span)
      (if (span-property span 'controlspan)
	  (progn
	    (span-set-property span 'controlspan new-parent)
	    (list span))))
    start end 'type)))

(defun pg-move-region-down (&optional num)
  "Move the region under point downwards in the buffer, past NUM spans."
  (interactive "p")
  (let ((span  (span-at (point) 'type)))
    (and span
	 (goto-char (span-start
		     (pg-move-span-contents span num)))
	 (skip-chars-forward " \t\n"))))

(defun pg-move-region-up (&optional num)
  "Move the region under point upwards in the buffer, past NUM spans."
  (interactive "p")
  (pg-move-region-down (- num)))

;; No key-binding is defined for these two functions:
(defun pg-show-all-proofs ()
  "Display all completed proofs in the buffer."
  (interactive)
  (pg-show-all-portions 'proof))

(defun pg-hide-all-proofs ()
  "Hide all completed proofs in the buffer."
  (interactive)
  (pg-show-all-portions 'proof 'hide))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Context menus inside spans
;;

(defun pg-pos-for-event (event)
  "Return position corresponding to position of a mouse click EVENT."
  (with-current-buffer
      (window-buffer (posn-window (event-start event)))
    (posn-point (event-start event))))

(defun pg-span-for-event (event)
  "Return span corresponding to position of a mouse click EVENT."
  (span-at (pg-pos-for-event event) 'type))

(defun pg-span-context-menu (event)
  "Display a context sensitive menu for proof script, around EVENT."
  (interactive "e")
  (let* ((span (pg-span-for-event event))
	 cspan)
    (when span
      ;; Find controlling span
      (while (setq cspan (span-property span 'controlspan))
	(setq span cspan))
      (let*
	  ((idiom (and span (span-property span 'idiom)))
	   (id    (and span (span-property span 'id))))
	(popup-menu (pg-create-in-span-context-menu
		     span idiom
		     (if id (symbol-name id))))))))

(defun pg-toggle-visibility ()
  "Toggle visibility of region under point."
  (interactive)
  (let* ((span (span-at (point) 'type))
	 (idiom (and span (span-property span 'idiom)))
	 (id    (and span (span-property span 'id))))
    (and  idiom id
	 (pg-toggle-element-visibility idiom (symbol-name id)))))

(defun pg-create-in-span-context-menu (span idiom name)
  "Create the dynamic context-sensitive menu for a span."
  (append
   (list (pg-span-name span))
   (list (vector
	  "Show/hide"
	  (if idiom (list 'pg-toggle-element-visibility (quote idiom) name))
	  (not (not idiom))))
   (list (vector
	  "Copy"       (list 'pg-copy-span-contents span) t))
   (list (vector
	  "Undo"
	  (list 'pg-span-undo span) t))
   ;; PG 4.1: these commands are neither very useful nor reliable
   ;; (list (vector
   ;; 	  "Move up"     (list 'pg-move-span-contents span -1)
   ;; 	  (pg-numth-span-higher-or-lower (pg-control-span-of span) -1 'noerr)))
   ;; (list (vector
   ;; 	  "Move down"   (list 'pg-move-span-contents span 1)
   ;; 	  (pg-numth-span-higher-or-lower (pg-control-span-of span) 1 'noerr)))
   (if proof-script-span-context-menu-extensions
       (funcall proof-script-span-context-menu-extensions span idiom name))
   (if proof-shell-theorem-dependency-list-regexp
       (proof-dependency-in-span-context-menu span))))

(defun pg-span-undo (span)
  "Undo to the start of the given SPAN."
  (interactive)
  (goto-char (span-start span))
  (proof-retract-until-point))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Message buffer hints
;;

(defun pg-goals-buffers-hint ()
  (pg-hint "Use \\<proof-mode-map>\\[proof-display-some-buffers] to rotate output buffers; \\<proof-mode-map>\\[pg-response-clear-displays] to clear response & trace."))

;;;###autoload
(defun pg-slow-fontify-tracing-hint ()
  (pg-hint "Large tracing output; refreshing intermittently.  Use \\<proof-mode-map>\\[pg-response-clear-displays] to clear trace."))

;;;###autoload
(defun pg-response-buffers-hint (&optional nextbuf)
  (unless (not (buffer-live-p proof-goals-buffer))
    (pg-hint
     (format
      "\\[proof-prf] for goals;%s \\[proof-layout-windows] refreshes"
      (if (or proof-three-window-enable
	      proof-multiple-frames-enable)
	  ""
	(format " \\[proof-display-some-buffers] rotates output%s;"
		(if nextbuf (concat " (next:" nextbuf ")") "")))))))

;;;###autoload
(defun pg-jump-to-end-hint ()
  (pg-hint "Use \\[proof-goto-end-of-locked] to jump to end of processed region"))

;;;###autoload
(defun pg-processing-complete-hint ()
  "Display hint for showing end of locked region or processing complete."
  (if (buffer-live-p proof-script-buffer)
      (let ((win (get-buffer-window proof-script-buffer)))
	(unless ;; end of locked already displayed
	    (and win (pos-visible-in-window-p (proof-unprocessed-begin)))
	  (with-current-buffer proof-script-buffer
	    (cond
	     ((proof-locked-region-empty-p)) ;; nothing if empty
	     ((proof-locked-region-full-p)
	      (pg-hint
	       (concat "Processing complete in " (buffer-name proof-script-buffer))))
	     ((not proof-autosend-running)
	      ;; partly complete: hint about displaying the locked end
	      (pg-jump-to-end-hint))))))))

;;;###autoload
(defun pg-next-error-hint ()
  "Display hint for locating error."
  (pg-hint "Use \\[proof-next-error] to go to next error location."))

;;;###autoload
(defun pg-hint (hintmsg)
  "Display a hint HINTMSG in the minibuffer, if `pg-show-hints' is non-nil.
The function `substitute-command-keys' is called on the argument."
  (if pg-show-hints (message (substitute-command-keys hintmsg))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Symbol near point/identifier under mouse query
;;

(defun pg-identifier-under-mouse-query (event)
  "Query the prover about the identifier near mouse click EVENT."
  (interactive "e")
  (if proof-query-identifier-command
      (save-selected-window
	(save-excursion
	  (mouse-set-point event)
	  (pg-identifier-near-point-query)))))

;;;###autoload
(defun pg-identifier-near-point-query ()
  "Query the prover about the identifier near point.
If the result is successful, we add a span to the buffer which has
a popup with the information in it."
  (interactive)
  (let* ((stend       (if (region-active-p)
			  (cons (region-beginning) (region-end))
			(pg-current-word-pos)))
	 (start       (car-safe stend))
	 (end         (cdr-safe stend))
	 (identifier  (if start
			  (buffer-substring-no-properties
			   start end)))
	 (ctxt	      (if start
			  (save-excursion
			    (goto-char start)
			    (proof-buffer-syntactic-context)))))
    (if start
	(pg-identifier-query
	 identifier ctxt
	 ;; the callback
	 (lambda (_)
	   (save-excursion
	     (let ((idspan (span-make start end)))
               ;; (span-set-property idspan 'priority 90) ; highest
               (span-set-property idspan 'help-echo
                                  (pg-last-output-displayform)))))))))

(defvar proof-query-identifier-history nil
  "History for `proof-query-identifier'.")

(defun proof-query-identifier (string)
  "Query the prover about the identifier STRING.
If called interactively, STRING defaults to the current word near point."
  (interactive
   (list
    (completing-read "Query identifier: "
		     nil nil nil
		     (current-word)
		     'proof-query-identifier-history)))
  (if string (pg-identifier-query string)))

(defun pg-identifier-query (identifier &optional ctxt callback)
  "Query the proof assisstant about the given IDENTIFIER.
This uses `proof-query-identifier-command'.
Parameter CTXT allows to give a context for the identifier (which
allows for multiple name spaces).
If CALLBACK is set, we invoke that when the command completes."
  (unless (or (null identifier)
	      (string-equal identifier "")) ;; or whitespace?
    (proof-shell-invisible-command
     (cond
      ((stringp proof-query-identifier-command)
       ;; simple customization
       (format proof-query-identifier-command identifier))
      (t
       ;; buffer-syntactic context dependent, as an alist
       ;; (handy for Isabelle: not a true replacement for parsing)
       (format (nth 1 (assq ctxt proof-query-identifier-command))
	       identifier)))
     nil ; no wait
     callback)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Imenu and Speedbar
;;

(declare-function speedbar-add-supported-extension "speedbar")
(eval-after-load "speedbar"
  '(and proof-assistant-symbol ;; *should* be set by now
	(speedbar-add-supported-extension
	 (nth 2 (assoc proof-assistant-symbol proof-assistant-table)))))

;;;###autoload
(defun proof-imenu-enable ()
  "Add or remove index menu."
  ;; NB: next two a bit interferring, but we suppose use-case is PG.
  (which-function-mode (if proof-imenu-enable 1 0))
  (when (listp which-func-modes)
    ;; FIXME: It's not PG's business to decide whether to use
    ;; which-function-mode.
    (add-to-list 'which-func-modes proof-mode-for-script))
  (if proof-imenu-enable
      (imenu-add-to-menubar "Index")
    (progn
      (when (listp which-func-modes)
        (setq which-func-modes
              (remove proof-mode-for-script which-func-modes)))
      (let ((oldkeymap (keymap-parent (current-local-map))))
	(if ;; sanity checks in case someone else set local keymap
	    (and oldkeymap
		 (lookup-key (current-local-map) [menu-bar index])
		 (not
		  (lookup-key oldkeymap [menu-bar index])))
	    (use-local-map oldkeymap)))
      (remove-hook 'menu-bar-update-hook 'imenu-update-menubar))))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Command history
;;
;; This implements a history ring for commands in the locked region.
;; Inspired by (and code heavily copied from) comint.
;;
;; The current behaviour is not ideal: we only extend the input ring as
;; we process (so history does not browse pink text while the
;; prover is busy).  Moreover, instead of using a history, we might
;; simply parse commands backwards (or forwards) in the buffer.
;; (i.e, more like the copying behaviour implemented in Bibtex mode).
;;

(defvar pg-input-ring nil
  "Ring of previous inputs.")

(defvar pg-input-ring-index nil
  "Position of last matched command.")

(defvar pg-stored-incomplete-input nil
  "Stored incomplete input: string between point and locked.")

(defun pg-previous-input (arg)
  "Cycle backwards through input history, saving input.
If called interactively, ARG is given by the prefix argument."
  (interactive "*p")
  (if (and pg-input-ring-index
	   (or			       ; leaving the "end" of the ring
	    (and (< arg 0)	       ; going down
		 (eq pg-input-ring-index 0))
	    (and (> arg 0)		; going up
		 (eq pg-input-ring-index
		     (1- (ring-length pg-input-ring)))))
	   pg-stored-incomplete-input)
      (pg-restore-input)
    (pg-previous-matching-input "." arg)))

(defun pg-next-input (arg)
  "Cycle forwards through input history.
If called interactively, ARG is given by the prefix argument."
  (interactive "*p")
  (pg-previous-input (- arg)))

(defun pg-delete-input ()
  (let* ((unproc (proof-unprocessed-begin))
	 (start  (save-excursion
		   (goto-char unproc)
		   (skip-chars-forward " \t\n")
		   (point)))
	 (end    (point-at-eol)))
    (cond
     ((< start end)
      (delete-region start end))
     ((< start (point-at-eol))
      (delete-region start (point-at-eol))))))

(defun pg-get-old-input ()
  "Return text between end of locked region and point, up to EOL.
If there is no text, return the empty string."
  (let* ((unproc (proof-unprocessed-begin))
	 (start  (save-excursion
		   (goto-char unproc)
		   (skip-chars-forward " \t\n")
		   (point)))
	 (end    (point-at-eol)))
    (if (< start end)
	(buffer-substring-no-properties start end)
      nil)))


(defun pg-restore-input ()
  "Restore unfinished input."
  (interactive)
  (when pg-input-ring-index
    (pg-delete-input)
    (when (> (length pg-stored-incomplete-input) 0)
      (insert pg-stored-incomplete-input)
      (message "Input restored"))
    (setq pg-input-ring-index nil)))


(defun pg-search-start (arg)
  "Index to start a directional search, starting at `pg-input-ring-index'."
  (if pg-input-ring-index
      ;; If a search is running, offset by 1 in direction of arg
      (mod (+ pg-input-ring-index (if (> arg 0) 1 -1))
	   (ring-length pg-input-ring))
    ;; For a new search, start from beginning or end, as appropriate
    (if (>= arg 0)
	0				       ; First elt for forward search
      (1- (ring-length pg-input-ring)))))  ; Last elt for backward search


(defun pg-regexp-arg (prompt)
  "Return list of regexp and prefix arg using PROMPT."
  (let* (;; Don't clobber this.
	 (last-command last-command)
	 (regexp (read-from-minibuffer prompt nil nil nil
				       'minibuffer-history-search-history)))
    (list (if (string-equal regexp "")
	      (setcar minibuffer-history-search-history
		      (nth 1 minibuffer-history-search-history))
	    regexp)
	  (prefix-numeric-value current-prefix-arg))))

(defun pg-search-arg (arg)
  ;; First make sure there is a ring and that we are after the process mark
  (cond ((not (>= (point) (proof-unprocessed-begin)))
	 (error "Not in unlocked region"))
	((or (null pg-input-ring)
	     (ring-empty-p pg-input-ring))
	 (error "Empty input ring"))
	((zerop arg)
	 ;; arg of zero resets search from beginning, and uses arg of 1
	 (setq pg-input-ring-index nil)
	 1)
	(t
	 arg)))

(defun pg-previous-matching-input-string-position (regexp arg &optional start)
  "Return the index matching REGEXP ARG places along the input ring.
Moves relative to START, or `pg-input-ring-index'."
  (if (or (not (ring-p pg-input-ring))
	  (ring-empty-p pg-input-ring))
      (error "No history"))
  (let* ((len (ring-length pg-input-ring))
	 (motion (if (> arg 0) 1 -1))
	 (n (mod (- (or start (pg-search-start arg)) motion) len))
	 (tried-each-ring-item nil)
	 (prev nil))
    ;; Do the whole search as many times as the argument says.
    (while (and (/= arg 0) (not tried-each-ring-item))
      ;; Step once.
      (setq prev n
	    n (mod (+ n motion) len))
      ;; If we haven't reached a match, step some more.
      (while (and (< n len) (not tried-each-ring-item)
		  (not (string-match regexp (ring-ref pg-input-ring n))))
	(setq n (mod (+ n motion) len)
	      ;; If we have gone all the way around in this search.
	      tried-each-ring-item (= n prev)))
      (setq arg (if (> arg 0) (1- arg) (1+ arg))))
    ;; Now that we know which ring element to use, if we found it, return that.
    (if (string-match regexp (ring-ref pg-input-ring n))
	n)))

(defun pg-previous-matching-input (regexp n)
  "Search backwards through input history for match for REGEXP.
\(Previous history elements are earlier commands.)
With prefix argument N, search for Nth previous match.
If N is negative, find the next or Nth next match."
  (interactive (pg-regexp-arg "Previous input matching (regexp): "))
  (setq n (pg-search-arg n))
  (let ((pos (pg-previous-matching-input-string-position regexp n)))
    ;; Has a match been found?
    (if (null pos)
	(error "Match not found for regexp %s" regexp)
      ;; If leaving the edit line, save partial input
      (if (null pg-input-ring-index)	;not yet on ring
	  (setq pg-stored-incomplete-input (pg-get-old-input)))
      (setq pg-input-ring-index pos)
      (message "History item: %d" (1+ pos))
      (pg-delete-input)
      (insert (ring-ref pg-input-ring pos)))))

(defun pg-next-matching-input (regexp n)
  "Search forwards through input history for match for REGEXP.
\(Later history elements are more recent commands.)
With prefix argument N, search for Nth following match.
If N is negative, find the previous or Nth previous match."
  (interactive (pg-regexp-arg "Next input matching (regexp): "))
  (pg-previous-matching-input regexp (- n)))

(defvar pg-matching-input-from-input-string ""
  "Input previously used to match input history.")

;;;###autoload
(defun pg-previous-matching-input-from-input (n)
  "Search backwards through input history for match for current input.
\(Previous history elements are earlier commands.)
With prefix argument N, search for Nth previous match.
If N is negative, search forwards for the -Nth following match."
  (interactive "p")
  (if (not (memq last-command '(pg-previous-matching-input-from-input
				pg-next-matching-input-from-input)))
      ;; Starting a new search
      (setq pg-matching-input-from-input-string (pg-get-old-input)
	    pg-input-ring-index nil))
  (if pg-matching-input-from-input-string
      (pg-previous-matching-input
       (concat "^" (regexp-quote pg-matching-input-from-input-string))
       n)
    (pg-previous-matching-input "." n)))

;;;###autoload
(defun pg-next-matching-input-from-input (n)
  "Search forwards through input history for match for current input.
\(Following history elements are more recent commands.)
With prefix argument N, search for Nth following match.
If N is negative, search backwards for the -Nth previous match."
  (interactive "p")
  (pg-previous-matching-input-from-input (- n)))



;;;###autoload
(defun pg-add-to-input-history (cmd)
   "Maybe add CMD to the input history.
CMD is only added to the input history if it is not a duplicate
of the last item added."
   (when (or (not (ring-p pg-input-ring))
	     (ring-empty-p pg-input-ring)
	     (not (string-equal (ring-ref pg-input-ring 0) cmd)))
     (unless (ring-p pg-input-ring)
       (setq pg-input-ring (make-ring pg-input-ring-size)))
     (ring-insert pg-input-ring cmd)))

;;;###autoload
(defun pg-remove-from-input-history (cmd)
  "Maybe remove CMD from the end of the input history.
This is called when the command is undone.  It's only
removed if it matches the last item in the ring."
  (if (and (ring-p pg-input-ring)
	   (not (ring-empty-p pg-input-ring))
	   (string-equal (ring-ref pg-input-ring 0) cmd))
      (ring-remove pg-input-ring 0)))


;;;###autoload
(defun pg-clear-input-ring ()
  (setq pg-input-ring nil))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Protected undo  (added in PG 4.0)
;;
;; From a suggestion of Stefan Monnier as an improvement over the
;; previous use of `undo-make-selective-list' to hack
;; `buffer-undo-list' in `proof-set-queue-endpoints'.
;;
;; Improved version due to Erik Martin-Dorel.  Uses auxiliary
;; functions `pg-protected-undo-1' and `next-undo-elt'
;;

(define-key proof-mode-map [remap undo] 'pg-protected-undo)
(define-key proof-mode-map [remap advertised-undo] 'pg-protected-undo)

(defun pg-protected-undo (&optional arg)
  "As `undo' but avoids breaking the locked region.

A numeric ARG serves as a repeat count.
If called interactively, ARG is given by the prefix argument.
If ARG is omitted, nil, or not numeric, it takes the value 1.

It performs each of the desired undos checking that these operations will
not affect the locked region, obeying `proof-strict-read-only' if required.
If strict read only behaviour is enforced, the user is queried whether to
retract before the undo is allowed.  If automatic retraction is enabled,
the retract and undo will go ahead without querying the user.

Moreover, undo/redo is always allowed in comments located in \
the locked region."
  (interactive "*P")
  (if (or (not proof-locked-span)
  	  (equal (proof-queue-or-locked-end) (point-min)))
      (undo arg)
    (let ((repeat ; Allow the user to perform successive undos at once
	   (if (numberp arg)
	       (prefix-numeric-value arg) ; arg is a raw prefix argument
	     1))
	  (newarg ; Allow the user to limit the undo to the current region
	   (and
	    ;; this Boolean expression is necessary to match
	    ;; the behavior of GNU Emacs (23.2) undo function
	    (or (region-active-p) (and arg (not (numberp arg))))
	    (> (region-end) (region-beginning)))))
      (while (> repeat 0)
	(pg-protected-undo-1 newarg) ; do some safe undos step by step
	(setq last-command 'undo) ; need for this assignment meanwhile
	(cl-decf repeat)))))

(defun pg-protected-undo-1 (arg)
  "This function is intended to be called by `pg-protected-undo'.

The flag ARG is passed to functions `undo' and `next-undo-elt'.
It should be a non-numeric value saying whether an undo-in-region
behavior is expected."
;; Note that if ARG is non-nil, (> (region-end) (region-beginning)) must hold,
;; at least for the first call from the loop of `pg-protected-undo'.
  (setq arg (and arg (not (numberp arg)))) ; ensure arg is boolean
  (if (or (not proof-locked-span)
	  (equal (proof-queue-or-locked-end) (point-min))) ; required test
      (undo arg)
    (let* ((next (next-undo-elt arg))
	   (delta (undo-delta next))  ; can be '(0 . 0) if next is nil
	   (beg (car delta))
	   (end (max beg (- beg (cdr delta))))) ; Key computation
      (when (and next (> beg 0)		; the "next undo elt" exists
		 (> (proof-queue-or-locked-end) beg)
		 proof-strict-read-only ; edit freely doesn't retract
		 (not (and		; neither does edit in comments
		       (proof-inside-comment beg)
		       (proof-inside-comment end))))
	(if (or (eq proof-strict-read-only 'retract)
		(y-or-n-p "Next undo will modify read-only region, retract? "))
	    (proof-retract-before-change beg end)
	  (when (eq last-command 'undo) (setq this-command 'undo))
	  ;; now we can stop the function without breaking possible undo chains
	  (error
	   "Cannot undo without retracting to the appropriate position")))
      (undo arg))))

(defun next-undo-elt (arg)
  "Return the undo element that will be processed on next undo/redo.
Assume the undo-in-region behavior will apply if ARG is non-nil."
  (let ((undo-list (if arg		; handle "undo in region"
		       (undo-make-selective-list
			(region-beginning) (region-end)) ; can be '(nil)
		     buffer-undo-list)))		 ; can be nil
    (if (or (null undo-list) (equal undo-list (list nil)))
	nil				; there is clearly no undo elt
      (while (and undo-list             ; to ensure it will terminate
                  (let ((elt (car undo-list)))
                    (not (and (consp elt)
                              (or (stringp (car elt))
                                  (integerp (car elt)))))))
	(setq undo-list (cdr undo-list))) ; get the last undo record
      (if (and (eq last-command 'undo)
	       (or (eq pending-undo-list t)
		   (gethash undo-list undo-equiv-table)))
	  ;; then we are within a run of consecutive undo commands
	  (if (eq pending-undo-list t) nil (car pending-undo-list))
	(car undo-list)))))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Automatic processing of buffer ahead of point
;;
;; Added in PG 4.0
;;

(defvar proof-autosend-timer nil "Timer used by autosend.")

(deflocal proof-autosend-modified-tick nil
  "Records 'buffer-chars-modified-tick' since last autosend.")

;;;###autoload
(defun proof-autosend-enable (&optional nomsg)
  "Enable or disable autosend behaviour."
  (if proof-autosend-timer
      (cancel-timer proof-autosend-timer))
  (when proof-autosend-enable
    (setq proof-autosend-timer
	  (run-with-idle-timer proof-autosend-delay
			       t 'proof-autosend-loop))
    (setq proof-autosend-modified-tick nil)
    (unless nomsg (message "Automatic sending turned on.")))
  (when (not proof-autosend-enable)
    (setq proof-autosend-timer nil)
    (unless nomsg (message "Automatic sending turned off."))))

(defun proof-autosend-delay ()
  "Adjust autosend timer when variable `proof-autosend-delay' changes."
  (proof-autosend-enable t))

(defun proof-autosend-loop ()
  (proof-with-current-buffer-if-exists proof-script-buffer
    (unless (or (proof-locked-region-full-p)
		proof-shell-busy
		;; TODO: re-engage autosend after C-c C-n even if not modified.
		(eq (buffer-chars-modified-tick) proof-autosend-modified-tick)
		(and proof-autosend-all
		     (eq proof-shell-last-queuemode 'retracting)))
      (let ((proof-autosend-running t))
	(setq proof-autosend-modified-tick (buffer-chars-modified-tick))
	(if proof-autosend-all
	    (proof-autosend-loop-all)
	  (proof-autosend-loop-next))))))

(defun proof-autosend-loop-all ()
  "Send commands from the script until an error, complete, or input appears."
  (message "Sending commands to prover...")
  (unwind-protect
      (progn
	(save-excursion
	  (goto-char (point-max))
	  (proof-assert-until-point
	   (if proof-multiple-frames-enable
	       'no-minibuffer-messages ; nb: not API
	     '(no-response-display
	       no-error-display
	       no-goals-display))))
	(proof-shell-wait t) ; interruptible
	(cond
	 ((eq proof-shell-last-output-kind 'error)
	  (message "Sending commands to prover...error"))
	 ((and (input-pending-p) proof-shell-busy)
	  (proof-interrupt-process)
	  (message "Sending commands to prover...interrupted")
	  (proof-shell-wait))
	 (t
	  (message "Sending commands to prover...done"))))))

(defun proof-autosend-loop-next ()
  "Send the next command from the script and indicate its success/otherwise."
  (unwind-protect
      (let ((qol   (proof-queue-or-locked-end)))
	(save-excursion
	  ;(goto-char qol)
	  ;(skip-chars-forward " \t\n")
	  (message "Trying next commands in prover...")
	  (proof-assert-until-point
	   (if proof-multiple-frames-enable
	       'no-minibuffer-messages ; nb: not API
	     '(no-response-display
	       no-error-display
	       no-goals-display))))
	(let ((proof-sticky-errors t))
	  (proof-shell-wait t)) ; interruptible
	(cond
	 ((eq proof-shell-last-output-kind 'error)
	  (message "Trying next commands in prover...error"))
	 ((and (input-pending-p) proof-shell-busy)
	  (proof-interrupt-process)
	  (message "Trying next commands in prover...interrupted")
	  (proof-shell-wait))
	 (t
	  (message "Trying next commands in prover...OK")))
	;; success: now undo in prover, highlight undone spans if OK
	(unless (eq qol (proof-queue-or-locked-end)) ; no progress
	  (save-excursion
	    (goto-char qol)
	    (proof-retract-until-point
	     (lambda (beg end)
	       (span-make-self-removing-span
		(save-excursion
		  (goto-char beg)
		  (skip-chars-forward " \t\n")
		  (point))
		end
		'face 'highlight))
	     '(no-response-display
	       no-error-display
	       no-goals-display)))))))
  
(provide 'pg-user)

;;; pg-user.el ends here