forked from idris-hackers/idris-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidris-metavariable-list.el
162 lines (136 loc) · 7.04 KB
/
idris-metavariable-list.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
;;; idris-metavariable-list.el --- List Idris metavariables in a buffer -*- lexical-binding: t -*-
;; Copyright (C) 2014 David Raymond Christiansen
;; Author: David Raymond Christiansen <[email protected]>
;; License:
;; Inspiration is taken from SLIME/DIME (http://common-lisp.net/project/slime/) (https://github.com/dylan-lang/dylan-mode)
;; Therefore license is GPL
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(require 'cl-lib)
(require 'idris-core)
(require 'idris-warnings-tree)
(require 'idris-settings)
(defvar idris-metavariable-list-buffer-name (idris-buffer-name :metavariables)
"The name of the buffer containing Idris metavariables")
(defun idris-metavariable-list-quit ()
"Quit the Idris metavariable list"
(interactive)
(idris-kill-buffer idris-metavariable-list-buffer-name))
(defvar idris-metavariable-list-mode-map
(let ((map (make-keymap)))
(suppress-keymap map)
(define-key map (kbd "q") 'idris-metavariable-list-quit)
(define-key map (kbd "RET") 'idris-compiler-notes-default-action-or-show-details)
(define-key map (kbd "<mouse-2>") 'idris-compiler-notes-default-action-or-show-details/mouse)
map))
(easy-menu-define idris-metavariable-list-mode-menu idris-metavariable-list-mode-map
"Menu for the Idris metavariable list buffer"
`("Idris Metavars"
["Show term interaction widgets" idris-add-term-widgets t]
["Close metavariable list buffer" idris-metavariable-list-quit t]))
(define-derived-mode idris-metavariable-list-mode fundamental-mode "Idris Metavars"
"Major mode used for transient Idris metavariable list buffers
\\{idris-metavariable-list-mode-map}
Invoces `idris-metavariable-list-mode-hook'.")
(defun idris-metavariable-list-buffer ()
"Return the Idris metavariable buffer, creating one if there is not one"
(get-buffer-create idris-metavariable-list-buffer-name))
(defun idris-metavariable-list-buffer-visible-p ()
(if (get-buffer-window idris-metavariable-list-buffer-name 'visible) t nil))
(defun idris-metavariable-list-show (metavar-info)
(if (null metavar-info)
(progn (message "No metavariables found!")
(idris-metavariable-list-quit))
(with-current-buffer (idris-metavariable-list-buffer)
(setq buffer-read-only nil)
(erase-buffer)
(idris-metavariable-list-mode)
(when idris-show-help-text
(insert "This buffer displays the unsolved metavariables from the currently-loaded code. ")
(insert "Press the [P] buttons to solve the metavariables interactively in the prover.")
(let ((fill-column 80))
(fill-region (point-min) (point-max)))
(insert "\n\n"))
(insert "Metavariables:\n")
(dolist (tree (mapcar #'idris-tree-for-metavariable metavar-info))
(idris-tree-insert tree "")
(insert "\n\n"))
;(idris-tree-insert root "")
(insert "\n")
(message "Press q to close")
(setq buffer-read-only t)
(goto-char (point-min)))
(display-buffer (idris-metavariable-list-buffer))))
(defun idris-metavariable-tree-printer (tree)
"Print TREE, formatted for metavariables."
(idris-propertize-spans (idris-repl-semantic-text-props (idris-tree.highlighting tree))
(insert (idris-tree.item tree)))
(when (idris-tree.button tree)
(insert " ")
(apply #'insert-button (idris-tree.button tree))
(insert (idris-tree.after-button tree))))
;;; Prevent circularity error
(autoload 'idris-prove-metavariable "idris-commands.el")
(defun idris-tree-for-metavariable (metavar)
"Generate a tree for METAVAR.
METAVAR should be a three-element list consisting of the
metavariable name, its premises, and its conclusion."
(cl-destructuring-bind (name premises conclusion) metavar
(make-idris-tree :item name
:button `("[P]"
help-echo "Open in prover"
action ,#'(lambda (_)
(interactive)
(idris-prove-metavariable name)))
:highlighting `((0 ,(length name) ((:decor :metavar))))
:print-fn #'idris-metavariable-tree-printer
:collapsed-p (not idris-metavariable-list-show-expanded) ; from customize
:kids (list (idris-tree-for-metavariable-details name premises conclusion)))))
(defun idris-tree-for-metavariable-details (name premises conclusion)
(let* ((name-width (1+ (apply #'max 0 (length name)
(mapcar #'(lambda (h) (length (car h)))
premises))))
(divider-marker nil)
(contents (with-temp-buffer
(dolist (h premises)
(cl-destructuring-bind (name type formatting) h
(cl-dotimes (_ (- name-width (length name))) (insert " "))
(idris-propertize-spans (idris-repl-semantic-text-props
`((0 ,(length name) ((:decor :bound)))))
(insert name))
(insert " : ")
(idris-propertize-spans (idris-repl-semantic-text-props formatting)
(insert type))
(insert "\n")))
(setq divider-marker (point-marker))
(cl-destructuring-bind (type formatting) conclusion
(when premises
(insert " ")
(idris-propertize-spans (idris-repl-semantic-text-props
`((0 ,(length name) ((:decor :metavar)))))
(insert name))
(insert " : "))
(idris-propertize-spans (idris-repl-semantic-text-props formatting)
(insert type)))
(when premises
(let ((width (apply #'max 0
(mapcar #'length
(split-string (buffer-string) "\n")))))
(goto-char (marker-position divider-marker))
(dotimes (_ (1+ width)) (insert "-"))
(insert "\n")))
(buffer-string))))
(make-idris-tree :item contents
:active-p nil
:highlighting '())))
(provide 'idris-metavariable-list)