aboutsummaryrefslogtreecommitdiffhomepage
path: root/isa/thy-mode.el
blob: 2bd643450b01ff2a8e913c04e6470638039a54e9 (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
;; thy-mode.el - Mode for Isabelle theory files.
;;
;; Copyright (C) 1998 LFCS Edinburgh. 
;; Author:    David Aspinall <da@dcs.ed.ac.uk>
;; License:   GPL (GNU GENERAL PUBLIC LICENSE)
;;
;; Taken from Isamode, version: 3.6 1998/09/02 11:40:45
;;
;; $Id$
;;
;; NAMESPACE management: all functions and variables declared
;; in this file begin with isa-thy-

(require 'proof-site)
(require 'proof-syntax)
(require 'isa)

;; In case thy mode was invoked directly or by -*- thy -*- at
;; the start of the file, ensure that Isar mode is used from now
;; on for .thy files.
;; FIXME: be less messy with auto-mode-alist here (remove dups)
(setq auto-mode-alist
      (cons '("\\.thy$" . thy-mode) auto-mode-alist))

;;; ========== Theory File Mode User Options ==========

(defcustom thy-heading-indent 0
  "Indentation for section headings."
  :type  'integer
  :group 'thy)

(defcustom thy-indent-level 2
  "*Indentation level for Isabelle theory files.  An integer."
  :type  'integer
  :group 'thy)

(defcustom thy-indent-strings t
  "If non-nil, indent inside strings.
You may wish to disable indenting inside strings if your logic uses
any of the usual bracket characters in unusual ways."
  :type 'boolean
  :group 'thy)

(defcustom thy-use-sml-mode nil
  "*If non-nil, invoke sml-mode inside \"ML\" section of theory files.
This option is left-over from Isamode.  Really, it would be more
useful if the script editing mode of Proof General itself could be based 
on sml-mode, but at the moment there is no way to do this."
  :type 'boolean
  :group 'thy)


;;; ======  Theory and ML file templates =========================    

(defcustom thy-sections
  ;; NB: preceding white space in templates deleted by indentation alg.
  ;;     top must come first.
  '(("top" .     thy-insert-header)
    ("classes" . thy-insert-class)
    ("default" . thy-insert-default-sort) ; is "default" obsolete?
    ("defaultsort" . thy-insert-default-sort)
    ("types"   . thy-insert-type) 
    ("typedecl")
    ("arities" . thy-insert-arity)
    ;; =================================
    ;; These only make sense for HOL.
    ;; Ideally we should parameterise these parts on the theory.
    ("datatype") ("typedef")
    ("inductive") ("coninductive")
    ("intrs") ("monos")
    ("primrec") ("recdef")
    ("rep_datatype") ("distinct") ("induct")
    ;; ==============================
    ("consts"  . thy-insert-const)
    ("translations" . "\"\"\t==\t\"\"")
    ("axclass")
    ("syntax")
    ("instance")
    ("rules"   . thy-insert-rule)
    ("defs"    . thy-insert-rule)
    ("axioms"    . thy-insert-rule)
    ("use")
    ("theory")
    ("files")
    ("constdefs")
    ("oracle")
    ("local")
    ("locale")
    ("nonterminals")
    ("setup")
    ("global")
    ("end")
    ("ML"))
  "Names of theory file sections and their templates.
Each item in the list is a pair of a section name and a template.
A template is either a string to insert or a function. Useful functions are:
  thy-insert-header, thy-insert-class, thy-insert-default-sort,
  thy-insert-const, thy-insert-rule.
The nil template does nothing.
You can add extra sections to theory files by extending this variable."
  :type '(repeat
	  (cons string
	       (choice function
		       string
		       (const :tag "no template" nil))))
  :group 'thy)

(defcustom thy-id-header
  "(* 
    File:	 %f
    Theory Name: %t
    Logic Image: %l
*)\n\n"
  "*Identification header for .thy and .ML files.
Format characters: %f replaced by filename, %t by theory name,
and %l by the logic image name this file should be read in."
  :group 'thy
  :type 'string)

(defcustom thy-template
"%t  =  %p +\n
classes\n
default\n
types\n
arities\n
consts\n
translations\n
rules\n
end\n
ML\n"
"Template for theory files.
Contains a default selection of sections in a traditional order.
You can use the following format characters:

`%t' --- replaced by theory name.

`%p' --- replaced by names of parents, separated by `+' characters."
 :group 'thy
 :type 'string)



;;; ========== Code ==========

(defvar thy-mode-map nil)

(defvar thy-mode-syntax-table nil)		; Shared below.

(if thy-mode-syntax-table	
    nil		
  ;; This is like sml-mode, except:
  ;;   .   is a word constituent (not punctuation).  (bad for comments?)
  ;;   "   is a paired delimiter
  (setq thy-mode-syntax-table (make-syntax-table))
  (modify-syntax-entry ?\( "()1 " thy-mode-syntax-table)
  (modify-syntax-entry ?\) ")(4 " thy-mode-syntax-table)
  (modify-syntax-entry ?\\ "\\   " thy-mode-syntax-table)
  (modify-syntax-entry ?*  ". 23" thy-mode-syntax-table)
  (modify-syntax-entry ?_  "w   " thy-mode-syntax-table)
  (modify-syntax-entry ?\' "w   " thy-mode-syntax-table)
;  it's annoying to match with quotes from previous strings,
;  so this has been removed. 
;  (modify-syntax-entry ?\" "$   " thy-mode-syntax-table)
  (modify-syntax-entry ?.  "w   " thy-mode-syntax-table))

(or thy-mode-map
    (let ((map (make-sparse-keymap)))
      (define-key map [(control up)] 'thy-goto-prev-section)
      (define-key map [(control down)] 'thy-goto-next-section)
      (define-key map "\C-c\C-n" 'thy-goto-next-section)
      (define-key map "\C-c\C-p" 'thy-goto-prev-section)
      (define-key map "\C-c\C-m" 'thy-minor-sml-mode)
      (define-key map "\C-c\C-t" 'thy-insert-template)
      ;; Disabled for Proof General
      ;;(define-key map "\C-c\C-u" 'thy-use-file)
      ;;(define-key map "\C-c\C-l" 'thy-raise-windows)
      (define-key map "\C-c\C-o" 'thy-find-other-file)
      (define-key map "\C-M" 'newline-and-indent)
      (define-key map "\C-k" 'thy-kill-line)
      (setq thy-mode-map map)))

(defun thy-add-menus (&optional file)
  "Add Proof General and Isabelle menu to current menu bar."
  (require 'proof-script)		; Later: proof-menu, autoloaded
  (easy-menu-define thy-mode-pg-menu
		    thy-mode-map
		    "PG Menu for Isabelle Proof General"
		    (cons proof-general-name
			  (append
			   (list
			   ;; A couple from the toolbar that make sense here
			   ;; (also in proof-universal-keys)
			    ["Issue command" proof-minibuffer-cmd t]
			    ["Interrupt prover" proof-interrupt-process t])
			   (list proof-buffer-menu)
			   (list proof-help-menu))))
  (easy-menu-define thy-mode-isa-menu
		    thy-mode-map
		    "Menu for Isabelle Proof General, theory file mode."
		    (cons "Theory"
			  (list
			   ["Next Section" thy-goto-next-section t]
			   ["Prev Section" thy-goto-prev-section t]
			   ["Insert Template" thy-insert-template t]
			   ["Process Theory" isa-process-thy-file
			    :active (proof-locked-region-empty-p)]
			   ["Retract Theory" isa-retract-thy-file
			    :active (proof-locked-region-full-p)]
			   ["Next Error" proof-next-error t]
			   ["Switch to Script" thy-find-other-file t])))
  (easy-menu-add thy-mode-pg-menu thy-mode-map)
  (easy-menu-add thy-mode-isa-menu thy-mode-map)
  
  (if file
     (progn (easy-menu-remove thy-mode-deps-menu)
	    (proof-thy-menu-define-deps file)
	    (easy-menu-add thy-mode-deps-menu thy-mode-map))))


(defun thy-mode (&optional nomessage)
  "Major mode for editing Isabelle theory files.
\\<thy-mode-map>
\\[thy-goto-next-section]\t Skips to the next section.
\\[thy-goto-prev-section]\t Skips to the previous section.

\\[indent-for-tab-command]\t Indents the current line.

\\[thy-insert-template]\t Inserts a template for the file or current section.

If thy-use-sml-mode is non-nil, \\<thy-mode-map>\\[thy-minor-sml-mode] \
invokes sml-mode as a minor mode 
in the ML section.  This is done automatically by \
\\[indent-for-tab-command].

The style of indentation for theory files is controlled by these variables:
  thy-heading-indent 
  thy-indent-level
  thy-indent-strings
- see individual variable documentation for details.

Here is the full list of theory mode key bindings:
\\{thy-mode-map}"
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'thy-mode)
  (setq mode-name "Theory")
  (use-local-map thy-mode-map)
  (thy-add-menus)

  (set-syntax-table thy-mode-syntax-table)
  (make-local-variable 'indent-line-function)
  (setq indent-line-function 'thy-indent-line)
  (make-local-variable 'comment-start)			; Following lines as in sml-mode
  (setq comment-start "(* ")				; .
  (make-local-variable 'comment-end)			; .
  (setq comment-end " *)")				; .
  (setq comment-start-skip "(\\*+[ \t]?")		; .
  (setq font-lock-keywords
	thy-mode-font-lock-keywords)

  ;; Toolbar: needs alteration for non-scripting mode!
  ;;  (if (featurep 'proof-toolbar)
  ;; (proof-toolbar-setup))
  ;;
			  

  (run-hooks 'thy-mode-hook)
  (force-mode-line-update)
  (if (null nomessage)
      (message
       (substitute-command-keys 
	"Isabelle theory-file mode.   Use \\[thy-insert-template] to insert templates; \\[describe-mode] for help.")))
  )

(defun thy-mode-quiet ()
  (interactive)
  (thy-mode t))




;;; "use" and "use_thy" with theory files ========================

;;; FIXME: Isabelle has been improved, so the following code could
;;; be cleaned up.  Also set variable to allow automatic starting
;;; of process by reading logic image.

;;; NB: this is a mess at the moment because of the theory file
;;; naming conventions.  Really we need to parse the theory/ML
;;; file - yuk!!
;;; The next version of Isabelle will be more consistent.

;(defun thy-use-file (&optional force-use_thy)
;  "Send the file of the current buffer to an Isabelle buffer with use_thy or use."
;  (interactive "P")
;  (let ((fname (buffer-file-name)))
;    (if fname
;	(isa-query-save (current-buffer))
;      (setq fname
;	    (or (buffer-file-name)
;		(read-file-name "Use file: " nil nil t))))
;    (let*
;	((has-thy-extn   (string-match "\\.thy$" fname))   ; o/w assume ML.
;	 (tname          (if has-thy-extn 
;			     (substring fname 0 -4); cos use_thy is daft!
;			   fname))
;	 (use            (if (or has-thy-extn force-use_thy)
;			     "use_thy"
;			   "use"))
;	 (use-thy-string (concat use " \"" tname "\";"))
;	 (logic (isa-guess-root)))
;      (thy-send-string logic use-thy-string))))

;(defun thy-use-region (beg end)
;  "Send the region to an Isabelle buffer, with use"
;  (interactive "r")
;  (write-region beg end thy-use-tempname nil 'nomessage)
;  (let* ((use-thy-string (concat "use \"" thy-use-tempname "\";"))
;	 (logic (isa-guess-root)))
;    (thy-send-string logic use-thy-string)))

;(defun thy-copy-region (beg end &optional isa-buffer)
;  "Copy the region to an Isabelle buffer."
;  (interactive "r")
;  (let ((text (buffer-substring beg end))
;	(logic (isa-guess-root)))
;    (save-excursion
;      (thy-send-string logic text))))

;(defun thy-use-line (&optional isabuffer)
;  "Send the current interactive ML line to an Isabelle buffer.
;Advance to the next line."
;  (interactive)
;  (isa-apply-to-interactive-line 'thy-copy-region))

;(defun thy-send-string (logic text &optional hide)
;  "Send TEXT to a buffer running LOGIC.
;If LOGIC is nil, pick the first Isabelle buffer."
;  (require 'isa-mode)
;  (setq logic nil) ;;; #### HACK! This all needs changing for single-session.
;  (let ((cur-frm (selected-frame)))	; Preserve selected frame.
;    (if logic				; switch to Isabelle buffer, without
;	(isabelle-session logic)	; raising the frame.
;					; (NB: this fails if was renamed).
;      (set-buffer
;       (or (car-safe (isa-find-buffers-in-mode 'isa-mode))
;	   (error "Can't find an Isabelle buffer"))))
;    (if hide
;	(isa-send-string
;	 (get-buffer-process (current-buffer))
;	 text)
;      (isa-insert-ret text))     ; send use command
;    (select-frame cur-frm)))

(defun thy-proofgeneral-send-string (logic text &optional hide)
  ;; FIXME -- new function!
  )

;(defun thy-raise-windows ()
;  "Raise windows/frames associated with Isabelle session."
;  (interactive)
;  (isa-select-buffer isa-session-buffer t)
;  (let ((raise t))
;    (mapcar 'isa-display-if-active   
;	    isa-associated-buffers)))


;(defun thy-guess-logic-in-use ()
;  (if (featurep 'isa-mode)
;      (let* ((buf (car-safe (isa-find-buffers-in-mode 'isa-mode)))
;	     (log (and buf
;		       (save-excursion
;			 (set-buffer buf)
;			 isa-logic-name))))
;	log)
;    nil))


;(defvar thy-use-tempname ".region.ML"
;  "*Name of temporary file to hold region dring thy-use-region.")


;(defconst thy-logic-image-regexp
;  "[lL][oO][gG][iI][cC] [iI][mM][aA][gG][eE]:[ \t]*\"?\\([^ \t\n\"]+\\)\"?[ \t]*$"
;  "Regexp for locating name of logic image file in a .thy or .ML file.")

;(defvar isa-logic-parents
;  ;; I can't be bothered to write all of them in here,
;  ;; and anyway they're ambiguous.  Use "Logic image:"
;  ;; instead.  (Or find a way of getting emacs to track
;  ;; theory structure...)
;  '(("List" . "HOL") ("Prod" . "HOL") ("Nat" . "HOL")
;    ("Ord" . "HOL") ("Set" ."HOL") ("Sexp" . "HOL")
;    ("Univ" . "HOL") ("WF" . "HOL") ("Sum" . "HOL")
;    ("IFOL" . "FOL"))
;  "*An alist of parents of theories that live in logic files.")

;(defun isa-guess-root ()
;  "Guess the root logic of the .thy or .ML file in current buffer.
;Choice based on first name found by:
;  (i) special text: \"Logic Image: <name>\" toward start of file
; (ii) guess work based on parent in THY = <parent> if a .thy file."
;  (save-excursion
;    (goto-char (point-min))
;    (cond
;     ((re-search-forward thy-logic-image-regexp 500 t)
;      (buffer-substring (match-beginning 1) (match-end 1)))
;     ((and (string-match "\\.thy$" (or (buffer-file-name) ""))
;	   (re-search-forward 
;	    "\\w+[ \t\n]*=[ \t\n]*\\(\\w+\\)[ \t\n]*\\+") 500 t)
;      ;; Something looks like a parent theory:
;      ;; 		MyThy = HOL + ...
;      (let ((child
;	     (buffer-substring (match-beginning 1) (match-end 1))))
;	    (or (cdr-safe (assoc child isa-logic-parents))
;		child))))))

;(defun isa-query-save (buffer)
;  (and (buffer-modified-p buffer)
;       (y-or-n-p (concat "Save file "
;			 (buffer-file-name buffer)
;			 "? "))
;       (save-excursion (set-buffer buffer) (save-buffer))))




;;; Interfacing with sml-mode ========================

;; extending sml-mode. This only works if you visit the theory file 
;; (or start Isabelle mode) first.
;; This is probably fairly close to The Right Thing...

(defun isa-sml-hook ()
  "Hook to customize sml-mode for use with Isabelle."
  ;(isa-menus)				; Add Isabelle main menu
  ;; NB: these keydefs will affect other sml-mode buffers too!
  (define-key sml-mode-map "\C-c\C-o" 'thy-find-other-file)
  ; Disabled for proof general 
  ;(define-key sml-mode-map "\C-c\C-u" 'thy-use-file)
  ;(define-key sml-mode-map "\C-c\C-r" 'thy-use-region)
  ;(define-key sml-mode-map "\C-c\C-l" 'thy-use-line)
  ;; listener minor mode removed: \C-c\C-c freed up
  (define-key sml-mode-map "\C-c\C-t" 'thy-insert-id-header))

(add-hook 'sml-mode-hook 'isa-sml-hook)

(defun isa-sml-mode ()
  "Invoke sml-mode after installing Isabelle hook."
  (interactive)
  (and (fboundp 'sml-mode) (sml-mode)))

(defcustom isa-ml-file-extension ".ML"
  "*File name extension to use for ML files."
  :type 'string
  :group 'isabelle)

(defun thy-find-other-file (&optional samewindow)
  "Find associated .ML or .thy file.
Finds and switch to the associated ML file (when editing a theory file)
or theory file (when editing an ML file).  
If SAMEWINDOW is non-nil (interactively, with an optional argument)
the other file replaces the one in the current window."
  (interactive "p")
  (and 
   (buffer-file-name)
   (let* ((fname     (buffer-file-name))
	  (thyfile   (string-match "\\.thy$" fname))
	  (mlfile    (string-match
		      (concat (regexp-quote isa-ml-file-extension) "$") fname))
	  (basename  (file-name-sans-extension fname))
	  (findfn    (if samewindow 'find-file else 'find-file-other-window)))
     (cond (thyfile
	    (funcall findfn (concat basename isa-ml-file-extension)))
	   (mlfile
	    (funcall findfn (concat basename ".thy")))))))
  

;;; "minor" sml-mode inside theory files ==========

(defvar thy-minor-sml-mode-map nil)

(defun thy-install-sml-mode ()
  (progn
    (require 'sml-mode)
    (setq thy-minor-sml-mode-map (copy-keymap sml-mode-map))
    ;; Bind TAB to what it should be in sml-mode.
    (define-key thy-minor-sml-mode-map "\t" 'indent-for-tab-command)
    (define-key thy-minor-sml-mode-map "\C-c\C-m" 'thy-mode-quiet)
    (define-key thy-minor-sml-mode-map "\C-c\C-t" 'thy-insert-template)))

(defun thy-minor-sml-mode ()
  "Invoke sml-mode as if a minor mode inside a theory file.
This has no effect if thy-use-sml-mode is nil."
  (interactive)
  (if thy-use-sml-mode
      (progn
	(if (not (boundp 'sml-mode)) 
	    (thy-install-sml-mode))
	(kill-all-local-variables)
	(sml-mode)					; Switch to sml-mode
	(setq major-mode 'thy-mode)
	(setq mode-name "Theory Sml")			; looks like it's a minor-mode.
	(use-local-map thy-minor-sml-mode-map)	; special map has \C-c\C-c binding.
	(make-local-variable 'indent-line-function)
	(setq indent-line-function 'thy-do-sml-indent)
	(force-mode-line-update)
	(message "Use C-c C-c to exit SML mode."))))

(defun thy-do-sml-indent ()
  "Run sml-indent-line in an Isabelle theory file, provided inside ML section.
If not, will turn off simulated minor mode and run thy-indent-line."
  (interactive)
  (if (string= (thy-current-section) "ML")		; NB: Assumes that TAB key was 
      (sml-indent-line)					; bound to sml-indent-line.
    (thy-mode t)					; (at least, it is now!).
    (thy-indent-line)))


(defun thy-insert-name (name)
  "Insert NAME -- as a string if there are non-alphabetic characters in NAME."
  (if (string-match "[a-zA-Z]+" name)
      (insert name)
    (insert "\"" name "\"")))

(defun thy-insert-class (name supers)
  (interactive 
   (list
    (isa-read-id "Class name: ")
    (isa-read-idlist "Super classes %s: ")))
  (insert name)
  (if supers (insert "\t< " (isa-splice-separator ", " supers)))
  (indent-according-to-mode)
  (forward-line 1))

(defun thy-insert-default-sort (sort)
  (interactive
   (list
    (isa-read-id "Default sort: ")))
  (insert sort)
  (indent-according-to-mode)
  (thy-goto-next-section))

(defun thy-insert-type (name no-of-args)
  (interactive
   (list
    (isa-read-id  "Type name: ")
    (isa-read-num "Number of arguments: ")))
  ;; make type variables for arguments. Daft things for >26!
  (cond 
   ((zerop no-of-args))
   ((= 1 no-of-args)  
    (insert "'a "))
   (t 
    (insert "(")
    (let ((i 0))
      (while (< i no-of-args)
	(if (> i 0) (insert ","))
	(insert "'" (+ ?a i))
	(setq i (1+ i))))
    (insert ") ")))
  (thy-insert-name name)
  (indent-according-to-mode)
  ;; forward line, although use may wish to add infix.
  (forward-line 1))

(defun thy-insert-arity (name param-sorts result-class)
  (interactive
   (list
    (isa-read-id "Type name: ")
    (isa-read-idlist "Parameter sorts %s: ")
    (isa-read-id "Result class: ")))
  (insert name " :: ")
  (if param-sorts 
      (insert "(" (isa-splice-separator ", " param-sorts) ") "))
  (insert result-class)
  (indent-according-to-mode)
  (forward-line 1))

(defun thy-insert-const (name type)
  ;; only does a single constant, no lists.
  (interactive
   (let* ((thename  (isa-read-id "Constant name: "))
	  (thetype  (isa-read-string (format "Type of `%s' : " thename))))
     (list thename thetype)))
  (thy-insert-name name)
  (insert " ::\t \"" type "\"\t\t")
  (indent-according-to-mode)
  ;; no forward line in case user adds mixfix
  )
  
(defun thy-insert-rule (name)
  (interactive 
   (list
    (isa-read-id "Axiom name: ")))
  (end-of-line 1)
  (insert name "\t\"\"")
  (backward-char)
  (indent-according-to-mode))

(defun thy-insert-template ()
  "Insert a syntax template according to the current section"
  (interactive)
  (thy-check-mode)
  (let* ((sect (thy-current-section))
	 (tmpl (cdr-safe (assoc sect thy-sections))))
    ;; Ensure point is at start of an empty line.
    (beginning-of-line)
    (skip-chars-forward "\t ")
    (if (looking-at sect) 
	(progn 
	  (forward-line 1) 
	  (skip-chars-forward "\t ")))
    (if (looking-at "$")
	nil
      (beginning-of-line)
      (newline)
      (forward-line -1))
    (cond ((stringp tmpl)
	   (insert tmpl)
	   (indent-according-to-mode))
	  ((null tmpl))		; nil is a symbol!
	  ((symbolp tmpl)
	   (call-interactively tmpl)))))

;;; === Functions for reading from the minibuffer.
;;;

; These could be improved, e.g. to enforce syntax for identifiers.
; Unfortunately, Xemacs, and now Emacs too, lack a 
; read-no-blanks-input function!

(defun isa-read-idlist (prompt &optional init)
  "Read a list of identifiers from the minibuffer."
  (let ((items init) item)
    (while (not (string= ""
			 (setq item (read-string 
				       (format prompt (or items ""))))))
      (setq items (nconc items (list item))))
    items))

(defun isa-read-id (prompt &optional init)
  "Read an identifier from the minibuffer."
  ;; don't allow empty input
  (let ((result ""))
    (while (string= result "")
      (setq result (read-string prompt init)))
    result))

(defun isa-read-string (prompt &optional init)
  "Read a non-empty string from the minibuffer"
  ;; don't allow empty input
  (let ((result ""))
    (while (string= result "")
      (setq result (read-string prompt init)))
    result))

(defun isa-read-num (prompt)
  "Read a number from the minibuffer."
  (read-number prompt t))

;(defun thy-read-thy-name ()
;  (let* ((default  (car 
;		    (isa-file-name-cons-extension
;		     (file-name-nondirectory 
;		      (abbreviate-file-name (buffer-file-name)))))))
;    default))

(defun thy-read-thy-name ()
  (let* ((default  (car 
		    (isa-file-name-cons-extension
		     (file-name-nondirectory 
		      (abbreviate-file-name (buffer-file-name))))))
	 (name     (read-string 
		    (format "Name of theory [default %s]: " default))))
    (if (string= name "") default name)))

(defun thy-read-logic-image ()
  (let*	((defimage "Pure")
	   ;; (or (thy-guess-logic-in-use)
	   ;; "Pure"))
	   
	 (logic    (read-string
		    (format "Name of logic image to use [default %s]: "
			    defimage))))
    (if (string= logic "") defimage logic)))

(defun thy-insert-header (name logic parents)
  "Insert a theory file header, for LOGIC, theory NAME with PARENTS"
  (interactive 
   (list
    (thy-read-thy-name)
    (thy-read-logic-image)
    (isa-read-idlist "Parent theory %s: ")))
  (let* ((parentplus (isa-splice-separator 
		      " + " 
		      (or parents (list (or logic "Pure")))))
	 (formalist (list
		       (cons "%t" name)
		       (cons "%p" parentplus))))
    (thy-insert-id-header name logic)
    (insert (isa-format formalist thy-template)))
  (goto-char (point-min))
  (thy-goto-next-section))

(defun thy-insert-id-header (name logic)
  "Insert an identification header, for theory NAME logic image LOGIC."
  (interactive
   (list
    (thy-read-thy-name)
    (thy-read-logic-image)))
  (let* ((formalist (list
		     (cons "%f" (buffer-file-name))
		     (cons "%t" name)
		     (cons "%l" logic))))
    (insert (isa-format formalist thy-id-header))))

(defun thy-check-mode ()
  (if (not (eq major-mode 'thy-mode))
      (error "Not in Theory mode.")))
  

(defconst thy-headings-regexp
  (concat
   "^\\s-*\\("
   (substring (apply 'concat (mapcar 
			      '(lambda (pair)
				 (concat "\\|" (car pair)))
			      (cdr thy-sections))) 2)
   "\\)[ \t]*")
  "Regular expression matching headings in theory files.")

(defvar thy-mode-font-lock-keywords
  (list				
   (list (proof-ids-to-regexp
	  (append '("infixl" "infixr" "binder") 
		  (mapcar 'car (cdr thy-sections))))
	 0 'font-lock-keyword-face)
   ;; FIXME: needs more work.  Get symbols in quotes, etc, etc.
   (list "\\s-*\\(\\w+\\)\\s-*\\(::?\\)"
	 2 'font-lock-preprocessor-face)
   (list "\\s-*\\(\\w+\\)\\s-*::?"
	 1 'font-lock-variable-name-face)
   (list "\".*\"\\s-*\\(::?\\)"
	 1 'font-lock-preprocessor-face)
   (list "\"\\(.*\\)\"\\s-*\\(::?\\)"
	 1 'font-lock-variable-name-face))
;   (list "^\\s-*\\(\\w*\\)\\s-*\\(::\\)"
;	 1 'font-lock-function-name-face
;	 2 'font-lock-preprocessor-face))
  "Font lock keywords for thy-mode.")

;;; movement between sections ===================================    

(defun thy-goto-next-section (&optional count noerror)
  "Goto the next (or COUNT'th next) section of a theory file.
Negative value for count means previous sections.
If NOERROR is non-nil, failed search will not be signalled."
  (interactive "p")
  (condition-case nil
      ;; string matching would probably be good enough
      (cond ((and count (< count 0))
	     (let ((oldp (point)))
	       (beginning-of-line)
	       (thy-goto-top-of-section)
	       ;; not quite right here - should go to top
	       ;; of file, like top of section does.
	       (if (equal (point) oldp)
		   (progn
		     (re-search-backward thy-headings-regexp 
					 nil nil (1+ (- count)))
		     (forward-line 1))))
	     t)
	    (t
	     (re-search-forward thy-headings-regexp nil nil count)
	     (forward-line 1)
	     t))
    ;; could just move to top or bottom if this happens, instead
    ;; of giving this error.
    (search-failed (if noerror nil
		     (error "No more headings")))))

(defun thy-goto-prev-section (&optional count noerror)
  "Goto the previous section (or COUNT'th previous) of a theory file.
Negative value for count means following sections.
If NOERROR is non-nil, failed search will not be signalled."
  (interactive)
  (thy-goto-next-section (if count (- count) -1) noerror))

(defun thy-goto-top-of-section ()
  "Goto the top of the current section"
  (interactive)
  (if (re-search-backward thy-headings-regexp nil t)
      (forward-line 1)
    (goto-char (point-min))))

(defun thy-current-section ()
  "Return the current section of the theory file, as a string.
\"top\" indicates no section."
  (save-excursion
    (let* ((gotsect (re-search-backward thy-headings-regexp nil t))
	   (start   (if gotsect
			(progn
			  (skip-chars-forward " \t")
			  (point)))))
      (if (not start)
	  "top"
	(skip-chars-forward "a-zA-z")
	(buffer-substring start (point))))))



;;; kill line ==================================================

(defun thy-kill-line (&optional arg)
  "As kill-line, except in a string will kill continuation backslashes also.
Coalesces multiple lined strings by leaving single spaces."
  (interactive "P")
  (let ((str           (thy-string-start))
	(kill-start    (point))
	following-slash)
    (if (not str)
	;; Usual kill line if not inside a string.
	(kill-line arg)
      (if arg
	  (forward-line (prefix-numeric-value arg))
	(if (eobp)
	    (signal 'end-of-buffer nil)))
      (setq kill-start (point))
      (if (thy-string-start str)		; if still inside a string
	  (cond 
	   ((looking-at "[ \t]*$")	; at end of line bar whitespace
	    (skip-chars-backward 
	     " \t"
	     (save-excursion (beginning-of-line) (1+ (point))))
	    (backward-char)
	    (if (looking-at "\\\\")	; preceding backslash
		(progn
		  (skip-chars-backward " \t")
		  (setq following-slash t)
		  (setq kill-start (min (point) kill-start)))
	      (goto-char kill-start))
	    (forward-line 1))
	   ((looking-at "[ \t]*\\\\[ \t]*$") ; before final backslash
	    (setq following-slash t)
	    (forward-line 1))
	   ((looking-at "\\\\[ \t]*\\\\[ \t]*$") ; an empty line!
	    (forward-line 1))
	   ((looking-at ".*\\(\\\\\\)[ \t]*$")	; want to leave backslash
	    (goto-char (match-beginning 1)))
	   ((and kill-whole-line (bolp))
	    (forward-line 1))
	   (t
	    (end-of-line))))
      (if (and following-slash 
	       (looking-at "[ \t]*\\\\"))	; delete following slash if
	  (goto-char (1+ (match-end 0)))) ; there's one
      (kill-region kill-start (point))	; do kill
      (if following-slash
	  ;; did do just-one-space, but it's not nice to delete backwards
	  ;; too
	  (delete-region (point)
			 (save-excursion
			   (skip-chars-forward " \t")
			   (point)))))))


;;; INDENTATION ==================================================
  
;;; Could do with thy-correct-string function,
;;; which does roughly the same as indent-region.
;;; Then we could have an electric " that did this!

;;; Could perhaps have used fill-prefix to deal with backslash
;;; indenting, rather than lengthy code below?

(defun thy-indent-line ()
  "Indent the current line in an Isabelle theory file.
If in the ML section, this switches into a simulated minor sml-mode."
  (let ((sect (thy-current-section)))
    (cond 
     ((and thy-use-sml-mode (string= sect "ML"))
      (progn				               ; In "ML" section,
	(thy-minor-sml-mode)	               ; switch to sml mode.
	(sml-indent-line)))

     (t   (let ((indent   (thy-calculate-indentation sect)))
	    (save-excursion
	      (beginning-of-line)
	      (let ((beg (point)))
		(skip-chars-forward "\t ")
		(delete-region beg (point)))
	      (indent-to indent))
	    (if (< (current-column) 
		   (current-indentation))	
		(skip-chars-forward "\t ")))))))

;; Better Emacs major modes achieve a kind of "transparency" to
;; the user, where special indentation,etc. happens under your feet, but
;; in a useful way that you soon get accustomed to.  Worse modes
;; cause frustration and repetitive re-editing of automatic indentation.
;; I hope I've achieved something in the first category...

(defun thy-calculate-indentation (sect)
  "Calculate the indentation for the current line.
SECT should be the string name of the current section."
  (save-excursion
    (beginning-of-line)
    (or (thy-long-comment-string-indentation)
	(if (looking-at "\\s-*$")
	    ;; Empty lines use indentation for section.
	    (thy-indentation-for sect)
	  (if (looking-at thy-headings-regexp)
	      thy-heading-indent
	    (progn
	      (skip-chars-forward "\t ")
	      (cond
	       ;; A comment?
	       ((looking-at "(\\*")         
		(thy-indentation-for sect))
	       ;; Anything else, use indentation for section
	       (t (thy-indentation-for sect)))))))))

(defun thy-long-comment-string-indentation ()
  "Calculate the indentation if inside multi-line comment or string.
Also indent string contents."
  (let* ((comstr (thy-comment-or-string-start))
	 (bolpos (save-excursion
		   (beginning-of-line)
		   (point)))
	 (multi  (and comstr 
		      (< comstr bolpos))))
    (if (not multi)
	nil		
      (save-excursion
	(beginning-of-line)
	(cond

	 ;; Inside a comment?
	 ((char-equal (char-after comstr) ?\( )
	  (forward-line -1)
	  (if (looking-at "[\t ]*(\\*")
	      (+ 3 (current-indentation))
	    (current-indentation)))
	 
	 ;; Otherwise, a string.
	 ;; Enforce correct backslashing on continuing
	 ;; line and return cons of backslash indentation
	 ;; and string contents indentation for continued
	 ;; line.
	 (t
	  (let ((quote-col (save-excursion (goto-char comstr) 
					   (current-column))))
	     (if thy-indent-strings
		(thy-string-indentation comstr)
	      ;; just to right of matching " 
	      (+ quote-col 1)))))))))

(defun thy-string-indentation (start)
  ;; Guess indentation for text inside a string
  (let* ((startcol  (save-excursion (goto-char start) (current-column)))
	 (pps-state (parse-partial-sexp (1+ start) (point)))
	 (par-depth (car pps-state)))
	 (cond (;; If not in nested expression, startcol+1
		(zerop par-depth)
		(1+ startcol))
	       (;; If in a nested expression, use position of opening bracket
		(natnump par-depth)
		(save-excursion
		  (goto-char (nth 1 pps-state))
		  (+ (current-column)
		     (cond ((looking-at "\\[|") 3)
			   (t 1)))))
	       (;; Give error for too many closing parens
		t
		(error "Mismatched parentheses")))))

(defun thy-indentation-for (sect)
  "Return the indentation for section SECT"
  (if (string-equal sect "top")
      thy-heading-indent
    thy-indent-level))

(defun thy-string-start (&optional min)
  "Return position of start of string if inside one, nil otherwise."
  (let ((comstr (thy-comment-or-string-start)))
    (if (and comstr
	     (save-excursion
	       (goto-char comstr)
	       (looking-at "\"")))
	comstr)))

;;; Is this parsing still too slow?  (better way? e.g., try setting
;;; variable "char" and examining it, rather than finding current
;;; state first - fewer branches in non-interesting cases, perhaps.
;;; NB: it won't understand escape sequences in strings, such as \"

(defun thy-comment-or-string-start (&optional min)
  "Find if point is in a comment or string, starting parse from MIN.
Returns the position of the comment or string start or nil.
If MIN is nil, starts from top of current section.

Doesn't understand nested comments."
  (or min
      (setq min
	    (save-excursion
	      (thy-goto-top-of-section) (point))))
  (if (<= (point) min)
      nil
    (let ((pos   (point))
	  (incomdepth 0)
	  incom instring)  ; char
      (goto-char min)
      (while (< (point) pos)
	;; When inside a string, only look for its end
	(if instring
	    (if (eq (char-after (point)) ?\") ; looking-at "\""
		(setq instring nil))
	  ;; If inside a comment, look for a comment end
	  (if (> 0 incomdepth)	
	      (if (and			; looking-at "\\*)"
		   (eq (char-after (point)) ?\*)
		   (eq (char-after (1+ (point))) ?\)))
		  (setq incomdepth (1- incomdepth)))
	    ;; If inside neither comment nor string, look for 
	    ;; a string start.
	    (if (eq (char-after (point)) ?\") ; looking-at "\""
		(setq instring (point))))
	  ;; Look for a comment start (unless inside a string)
	  (if (and
	       (eq (char-after (point)) ?\()
	       (eq (char-after (1+ (point))) ?\*))
	      (progn
		(if (= 0 incomdepth)	; record start of main comment
		    (setq incom (point))) ; only
		(setq incomdepth (1+ incomdepth)))))
	(forward-char))
      (if (> 0 incomdepth) incom instring))))




(provide 'thy-mode)

;;; end of thy-mode.el