-
Notifications
You must be signed in to change notification settings - Fork 90
/
coq-db.el
406 lines (337 loc) · 14.9 KB
/
coq-db.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
;;; coq-db.el --- coq keywords database utility functions -*- coding: utf-8; 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: Pierre Courtieu <[email protected]>
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;;
;; We store all information on keywords (tactics or command) in big
;; tables (ex: `coq-tactics-db') From there we get: menus including
;; "smart" commands, completions for command coq-insert-...
;; abbrev tables and font-lock keyword.
;;
;;; Code:
(eval-when-compile (require 'cl-lib)) ;decf
(require 'holes)
(require 'diff-mode)
(defconst coq-syntax-db nil
"Documentation-only variable, for coq keyword databases.
Each element of a keyword database contains the definition of a \"form\", of the
form:
\(MENUNAME ABBREV INSERT STATECH KWREG INSERT-FUN HIDE)
MENUNAME is the name of form (or form variant) as it should appear in menus or
completion lists.
ABBREV is the abbreviation for completion via \\[expand-abbrev].
INSERT is the complete text of the form, which may contain holes denoted by
\"#\" or \"@{xxx}\".
If non-nil the optional STATECH specifies that the command is not state
preserving for coq.
If non-nil the optional KWREG is the regexp to colorize correponding to the
keyword. ex: \"simple\\\\s-+destruct\" (\\\\s-+ meaning \"one or more spaces\").
*WARNING*: A regexp longer than another one should be put FIRST. For example:
(\"Module Type\" ... ... t \"Module\\s-+Type\")
(\"Module\" ... ... t \"Module\")
Is ok because the longer regexp is recognized first.
If non-nil the optional INSERT-FUN is the function to be called when inserting
the form (instead of inserting INSERT, except when using \\[expand-abbrev]). This
allows to write functions asking for more information to assist the user.
If non-nil the optional HIDE specifies that this form should not appear in the
menu but only in interactive completions.
Example of what could be in your emacs init file:
\(defvar coq-user-tactics-db
'(
(\"mytac\" \"mt\" \"mytac # #\" t \"mytac\")
(\"myassert by\" \"massb\" \"myassert ( # : # ) by #\" t \"assert\")
))
Explanation of the first line: the tactic menu entry mytac, abbreviated by mt,
will insert \"mytac # #\" where #s are holes to fill, and \"mytac\" becomes a
new keyword to colorize.")
(defun coq-insert-from-db (db prompt &optional alwaysjump)
"Ask for a keyword, with completion on keyword database DB and insert.
Insert corresponding string with holes at point. If an insertion
function is present for the keyword, call it instead. See
`coq-syntax-db' for DB structure. If ALWAYSJUMP is non nil, jump to
the first hole even if more than one."
(let* ((tac (completing-read (concat prompt " (TAB for completion): ")
db nil nil))
(infos (cddr (assoc tac db)))
(s (car infos)) ; completion to insert
(f (car-safe (cdr-safe (cdr-safe (cdr infos))))) ; insertion function
(pt (point)))
(if f (funcall f) ; call f if present
(insert (or s tac)) ; insert completion and indent otherwise
(holes-replace-string-by-holes-backward-jump pt nil alwaysjump)
(indent-according-to-mode))))
(defun coq-build-command-from-db (db prompt &optional _preformatquery)
"Ask for a keyword, with completion on keyword database DB and send to Coq.
See `coq-syntax-db' for DB structure."
;; Next invocation of minibuffer (read-string below) will first recursively
;; ask for a command in db and expand it with holes. This way the cursor will
;; be at the right place.
(minibuffer-with-setup-hook
(lambda () (coq-insert-from-db db prompt t))
;; I use recursive edition on the minibuffer here, because I want the cursor
;; to be moved inside the initial content
(let ((enable-recursive-minibuffers t)) ; invo
(read-string (concat prompt " : "))
; (read-from-minibuffer (concat prompt " : "))
)))
;
;(defun coq-build-command-from-db (db prompt &optional preformatquery)
; "Ask for a keyword, with completion on keyword database DB and send to coq.
;See `coq-syntax-db' for DB structure."
; (let* ((query (completing-read (concat prompt " (TAB for completion): ")
; db nil nil))
; (infos (cddr (assoc query db)))
; (s (car infos))
; ; default format is add a space at the end of query, for arguments.
; (preformat (or preformatquery '(lambda (s) (concat s " "))))
; (initialvalue (funcall preformat query))
; (whole-query
; (minibuffer-with-setup-hook
; 'coq-move-cursor-at-sharp
; (read-string (concat prompt " : ") initialvalue t nil))))
; whole-query))
;
(defun coq-build-regexp-list-from-db (db &optional filter)
"Take a keyword database DB and return the list of regexps for font-lock.
If non-nil Optional argument FILTER is a function applying to each line of DB.
For each line if FILTER returns nil, then the keyword is not added to the
regexp. See `coq-syntax-db' for DB structure."
(let ((l db) res)
(while l
(let* ((hd (car l)) (tl (cdr l)) ; hd is the first infos list
(color (nth 4 hd))) ; colorization string
; da: do this below, otherwise we get empty words in result!!
; (color (concat "\\_<" (nth 4 hd) "\\_>"))) ; colorization string
;; TODO delete doublons
(when (and color (or (not filter) (funcall filter hd)))
(setq res
(nconc res (list
(concat "\\_<" color "\\_>")))))
(setq l tl)))
res))
(defun coq-build-opt-regexp-from-db (db &optional filter)
"Take a keyword database DB and return a regexps for font-lock.
If non-nil optional argument FILTER is a function applying to each line of DB.
For each line if FILTER returns nil, then the keyword is not added to the
regexp. See `coq-syntax-db' for DB structure."
(let ((l db) res)
(while l
(let* ((hd (car l)) (tl (cdr l)) ; hd is the first infos list
(color (nth 4 hd))) ; colorization string
;; TODO delete doublons
(when (and color (or (not filter) (funcall filter hd)))
(setq res (nconc res (list color)))) ; careful: nconc destructive!
(setq l tl)))
; da: next call is wrong?
; (proof-ids-to-regexp res)))
(concat "\\_<\\(?:" (mapconcat #'identity res "\\|") "\\)\\_>")))
;; Computes the max length of strings in a list
(defun max-length-db (db)
"Return the length of the longest first element (menu label) of DB.
See `coq-syntax-db' for DB structure."
(let ((l db) (res 0))
(while l
(let ((lgth (length (car (car l)))))
(setq res (max lgth res))
(setq l (cdr l))))
res))
(defun coq-build-menu-from-db-internal (db lgth menuwidth)
"Take a keyword database DB and return one insertion submenu.
Argument LGTH is the max size of the submenu. Argument MENUWIDTH is the width
of the largest line in the menu (without abbrev and shortcut specifications).
Used by `coq-build-menu-from-db', which you should probably use instead. See
`coq-syntax-db' for DB structure."
(let ((l db) (res ()) (size lgth)
(keybind-abbrev (substitute-command-keys " \\[expand-abbrev]")))
(while (and l (> size 0))
(let* ((hd (pop l))
(menu (nth 0 hd)) ; e1 = menu entry
(abbrev (nth 1 hd)) ; e2 = abbreviation
(complt (nth 2 hd)) ; e3 = completion
;; (state (nth 3 hd)) ; e4 = state changing
;; (color (nth 4 hd)) ; e5 = colorization string
(insertfn (nth 5 hd)) ; e6 = function for smart insertion
(menuhide (nth 6 hd)) ; e7 = if non-nil : hide in menu
(entry-with (max (- menuwidth (length menu)) 0))
(spaces (make-string entry-with ? ))
;;(restofmenu (coq-build-menu-from-db-internal tl (- size 1) menuwidth))
)
(unless menuhide
(let ((menu-entry
(vector
;; menu entry label
(concat menu
(if (not abbrev) ""
(concat spaces "(" abbrev keybind-abbrev ")")))
;;insertion function if present otherwise insert completion
(if insertfn insertfn `(holes-insert-and-expand ,complt))
t)))
(push menu-entry res)))
(cl-decf size)))
(nreverse res)))
(defun coq-build-title-menu (db size)
"Build a title for the first submenu of DB, of size SIZE.
Return the string made of the first and the SIZE nth first element of DB,
separated by \"...\". Used by `coq-build-menu-from-db'. See `coq-syntax-db'
for DB structure."
(concat (car-safe (car-safe db))
" ... "
(car-safe (car-safe (nthcdr (- size 1) db)))))
(defun coq-sort-menu-entries (menu)
(sort menu
#'(lambda (x y) (string<
(downcase (elt x 0))
(downcase (elt y 0))))))
(defun coq-build-menu-from-db (db &optional size)
"Take a keyword database DB and return a list of insertion menus for them.
Submenus contain SIZE entries (default 30). See `coq-syntax-db' for DB
structure."
;; sort is destructive for the list, so copy list before sorting
(let* ((l (coq-sort-menu-entries (copy-sequence db))) (res ())
(wdth (+ 2 (max-length-db db)))
(sz (or size 30)) (lgth (length l)))
(while l
(if (<= lgth sz)
(setq res ;; careful: nconc destructive!
(nconc res (list (cons
(coq-build-title-menu l lgth)
(coq-build-menu-from-db-internal l lgth wdth)))))
(setq res ; careful: nconc destructive!
(nconc res (list (cons
(coq-build-title-menu l sz)
(coq-build-menu-from-db-internal l sz wdth))))))
(setq l (nthcdr sz l))
(setq lgth (length l)))
res))
(defcustom coq-holes-minor-mode t
"*Whether to apply holes minor mode (see `holes-show-doc') in coq mode."
:type 'boolean
:group 'coq)
(defun coq-build-abbrev-table-from-db (db)
"Take a keyword database DB and return an abbrev table.
See `coq-syntax-db' for DB structure."
(let ((l db) (res ()))
(while l
(let* ((hd (car l))(tl (cdr l)) ; hd is a list of length 3 or 4
(_e1 (car hd)) (tl1 (cdr hd)) ; e1 = menu entry
(e2 (car tl1)) (tl2 (cdr tl1)) ; e2 = abbreviation
(e3 (car tl2)) (_tl3 (cdr tl2)) ; e3 = completion
)
;; careful: nconc destructive!
(when e2
(push `(,e2 ,e3 ,(if coq-holes-minor-mode #'holes-abbrev-complete)
:system t)
res))
(setq l tl)))
(nreverse res)))
(defun filter-state-preserving (l)
; checkdoc-params: (l)
"Not documented."
(not (nth 3 l))) ; fourth argument is nil --> state preserving command
(defun filter-state-changing (l)
; checkdoc-params: (l)
"Not documented."
(nth 3 l)) ; fourth argument is nil --> state preserving command
;;A new face for tactics which fail when they don't kill the current goal
(defface coq-solve-tactics-face
`((((background light)) :foreground "red")
(((background dark)) :foreground "red1")
()) ; pour le noir et blanc
"Face for names of closing tactics in proof scripts."
:group 'proof-faces)
;;A face for cheating tactics
;; We use :box in addition to :background because box remains visible in
;; locked-region. :reverse-video is another solution.
(defface coq-cheat-face
'(;(((class color) (background light)) . (:inverse-video t :foreground "red" :background "black"))
;(((class color) (background dark)) . (:inverse-video t :foreground "red1"))
(((class color) (background light)) . (:box (:line-width -1 :color "red" :style nil) :background "red"))
(((class color) (background dark)) . (:box (:line-width -1 :color "red1" :style nil) :background "red1"))
(t . ())) ; monocolor or greyscale: no highlight
"Face for names of cheating tactics in proof scripts."
:group 'proof-faces)
(defface coq-symbol-binder-face
'((t :inherit font-lock-type-face :bold coq-bold-unicode-binders))
"Face for unicode binders, by default a bold version of `font-lock-type-face'."
:group 'proof-faces)
(defface coq-symbol-face
'((t :inherit font-lock-type-face :bold coq-bold-unicode-binders))
"Face for unicode binders, by default a bold version of `font-lock-type-face'."
:group 'proof-faces)
(defface coq-question-mark-face
'((t :inherit font-lock-variable-name-face))
"Face for Ltac binders and evars."
:group 'proof-faces)
(defface coq-context-qualifier-face
'((t :inherit font-lock-preprocessor-face :weight bold))
"Face used for `ltac:', `constr:', and `uconstr:' headers."
:group 'proof-faces)
;; This is so quite ugly, better have our own face.
;; (defface coq-button-face
;; '((t :inherit custom-button :background "dark gray"
;; ;; -1 avoids messing with line and column sizes
;; :box (:line-width (-1 . -1) :style key-pressed)))
;; ""
;; :group 'proof-faces)
(defface coq-button-face
'(
(((class color) (background light)) . (:background "light gray"))
(((class color) (background dark)) . (:background "gray" :foreground "black")))
"Coq face for the (un)folding hypothesis button"
:group 'proof-faces)
(defface coq-button-face-pressed
'((((class color) (background light)) . (:background "gray"))
(((class color) (background dark)) . (:background "dim gray" :foreground "black")))
"Coq face for the (un)folding hypothesis button"
:group 'proof-faces)
(defface coq-button-face-active
'((((class color) (background light)) . (:background "black" :foreground "white"))
(((class color) (background dark)) . (:background "grey24" :foreground "black")))
"Coq face for the (un)folding hypothesis button"
:group 'proof-faces)
(defconst coq-solve-tactics-face 'coq-solve-tactics-face
"Expression that evaluates to a face.
Required so that 'coq-solve-tactics-face is a proper facename")
(defconst coq-cheat-face 'coq-cheat-face
"Expression that evaluates to a face.
Required so that 'coq-cheat-face is a proper facename")
(defconst coq-symbol-binder-face 'coq-symbol-binder-face
"Expression that evaluates to a face.
Required so that 'coq-symbol-binder-face is a proper facename")
(defconst coq-symbol-face 'coq-symbol-face
"Expression that evaluates to a face.
Required so that 'coq-symbol-binder-face is a proper facename")
(defconst coq-question-mark-face 'coq-question-mark-face)
(defface coq-diffs-added-face
'((t . (:inherit diff-refine-added)))
"Face used to highlight added text in diffs"
:group 'proof-faces)
(defface coq-diffs-removed-face
'((t . (:inherit diff-refine-removed)))
"Face used to highlight removed text in diffs"
:group 'proof-faces)
(defface coq-diffs-added-bg-face
'((t . (:inherit diff-added)))
"Face used to highlight unchanged text in lines showing added text in diffs"
:group 'proof-faces)
(defface coq-diffs-removed-bg-face
'((t . (:inherit diff-removed)))
"Face used to highlight unchanged text in lines showing removed text in diffs"
:group 'proof-faces)
(defvar coq-tag-map
'(("diff.added" . coq-diffs-added-face)
("diff.removed" . coq-diffs-removed-face)
("diff.added.bg" . coq-diffs-added-bg-face)
("diff.removed.bg" . coq-diffs-removed-bg-face)))
(provide 'coq-db)
;;; coq-db.el ends here
;** Local Variables: ***
;** fill-column: 80 ***
;** End: ***