-
Notifications
You must be signed in to change notification settings - Fork 18
/
lean-mode.el
250 lines (227 loc) · 9.13 KB
/
lean-mode.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
;; -*- lexical-binding: t -*-
;;
;;; lean-mode.el --- Emacs mode for Lean theorem prover
;;
;; Copyright (c) 2013, 2014 Microsoft Corporation. All rights reserved.
;; Copyright (c) 2014, 2015 Soonho Kong. All rights reserved.
;;
;; Author: Leonardo de Moura <leonardo@microsoft.com>
;; Soonho Kong <soonhok@cs.cmu.edu>
;; Maintainer: Soonho Kong <soonhok@cs.cmu.edu>
;; Created: Jan 09, 2014
;; Keywords: languages
;; Version: 0.1
;; URL: https://github.com/leanprover/lean/blob/master/src/emacs
;;
;; Released under Apache 2.0 license as described in the file LICENSE.
;;
(require 'cl-lib)
(require 'pcase)
(require 'lean-require)
(require 'eri)
(require 'lean-util)
(require 'lean-settings)
(require 'lean-flycheck)
(require 'lean-input)
(require 'lean-syntax)
(require 'lean-project)
(require 'lean-company)
(require 'lean-server)
(defun lean-compile-string (exe-name args file-name)
"Concatenate exe-name, args, and file-name"
(format "%s %s %s" exe-name args file-name))
(defun lean-create-temp-in-system-tempdir (file-name prefix)
"Create a temp lean file and return its name"
(make-temp-file (or prefix "flymake") nil (f-ext file-name)))
(defun lean-execute (&optional arg)
"Execute Lean in the current buffer"
(interactive)
(when (called-interactively-p 'any)
(setq arg (read-string "arg: " arg)))
(let ((target-file-name
(or
(buffer-file-name)
(flymake-init-create-temp-buffer-copy 'lean-create-temp-in-system-tempdir))))
(compile (lean-compile-string
(shell-quote-argument (f-full (lean-get-executable lean-executable-name)))
(or arg "")
(shell-quote-argument (f-full target-file-name))))))
(defun lean-show-goal-at-pos ()
"Show goal at the current point."
(interactive)
(lean-server-sync)
(lean-server-send-command
(list :command "show_goal"
:line (line-number-at-pos)
:col (current-column))
(cl-function
(lambda (&key state)
(let* ((temp-buffer-setup-hook #'lean-info-mode))
(with-output-to-temp-buffer "*lean-info*" (princ state)))))))
(defun lean-std-exe ()
(interactive)
(lean-execute))
(defun lean-check-expansion ()
(interactive)
(save-excursion
(if (looking-at (rx symbol-start "_")) t
(if (looking-at "\\_>") t
(backward-char 1)
(if (looking-at "\\.") t
(backward-char 1)
(if (looking-at "->") t nil))))))
(defun lean-tab-indent ()
(cond ((looking-back (rx line-start (* white)) nil)
(eri-indent))
(t (indent-for-tab-command))))
(defun lean-tab-indent-or-complete ()
(interactive)
(if (minibufferp)
(minibuffer-complete)
(cond ((and lean-company-use (company-lean--check-prefix))
(company-complete-common))
(lean-company-use (lean-tab-indent))
((lean-check-expansion)
(completion-at-point-functions))
(t (lean-tab-indent)))))
(defun lean-set-keys ()
(local-set-key lean-keybinding-std-exe1 'lean-std-exe)
(local-set-key lean-keybinding-std-exe2 'lean-std-exe)
(local-set-key lean-keybinding-show-key 'quail-show-key)
(local-set-key lean-keybinding-eval-cmd 'lean-eval-cmd)
(local-set-key lean-keybinding-server-restart 'lean-server-restart)
(local-set-key lean-keybinding-tab-indent-or-complete 'lean-tab-indent-or-complete)
(local-set-key lean-keybinding-lean-show-goal-at-pos 'lean-show-goal-at-pos)
(local-set-key lean-keybinding-lean-show-id-keyword-info 'lean-show-id-keyword-info)
(local-set-key lean-keybinding-lean-next-error-mode 'lean-next-error-mode)
)
(defun lean-define-key-binding (key cmd)
(local-set-key key `(lambda () (interactive) ,cmd)))
(define-abbrev-table 'lean-abbrev-table
'())
(defvar lean-mode-map (make-sparse-keymap)
"Keymap used in Lean mode")
(easy-menu-define lean-mode-menu lean-mode-map
"Menu for the Lean major mode"
`("Lean"
["Execute lean" lean-execute t]
["Create a new project" (call-interactively 'lean-project-create) (not (lean-project-inside-p))]
"-----------------"
["Show goal" lean-show-goal-at-pos t]
["Show id/keyword info" lean-show-id-keyword-info t]
["Global tag search" lean-global-search t]
"-----------------"
["Run flycheck" flycheck-compile (and lean-flycheck-use flycheck-mode)]
["List of errors" flycheck-list-errors (and lean-flycheck-use flycheck-mode)]
"-----------------"
["Clear all cache" lean-clear-cache t]
["Restart lean process" lean-server-restart t]
"-----------------"
("Configuration"
["Use flycheck (on-the-fly syntax check)"
lean-toggle-flycheck-mode :active t :style toggle :selected flycheck-mode]
["Show next error in dedicated buffer"
lean-next-error-mode :active t :style toggle :selected lean-next-error-mode])
"-----------------"
["Customize lean-mode" (customize-group 'lean) t]))
(defconst lean-hooks-alist
'(
;; Handle events that may start automatic syntax checks
(before-save-hook . lean-whitespace-cleanup)
;; ;; Handle events that may triggered pending deferred checks
;; (window-configuration-change-hook . lean-perform-deferred-syntax-check)
;; (post-command-hook . lean-perform-deferred-syntax-check)
;; ;; Teardown Lean whenever the buffer state is about to get lost, to
;; ;; clean up temporary files and directories.
;; (kill-buffer-hook . lean-teardown)
;; (change-major-mode-hook . lean-teardown)
;; (before-revert-hook . lean-before-revert)
;; (after-revert-hook . lean-after-revert)
;; ;; Update the error list if necessary
;; (post-command-hook . lean-error-list-update-source)
;; (post-command-hook . lean-error-list-highlight-errors)
;; ;; Show or hide error popups after commands
;; (post-command-hook . lean-display-error-at-point-soon)
;; (post-command-hook . lean-hide-error-buffer)
)
"Hooks which lean-mode needs to hook in.
The `car' of each pair is a hook variable, the `cdr' a function
to be added or removed from the hook variable if Flycheck mode is
enabled and disabled respectively.")
(defun lean-mode-setup ()
"Default lean-mode setup"
(add-hook 'kill-buffer-hook 'lean-server-stop)
;; Flycheck
(when lean-flycheck-use
(lean-flycheck-turn-on)
(setq-local flycheck-disabled-checkers '()))
;; company-mode
(when lean-company-use
(company-lean-hook))
;; Draw a vertical line for rule-column
(when (and lean-rule-column
lean-show-rule-column-method)
(cl-case lean-show-rule-column-method
('vline (require 'fill-column-indicator)
(setq fci-rule-column lean-rule-column)
(setq fci-rule-color lean-rule-color)
(fci-mode t)))))
;; Automode List
;;;###autoload
(define-derived-mode lean-mode prog-mode "Lean"
"Major mode for Lean
\\{lean-mode-map}
Invokes `lean-mode-hook'.
"
:syntax-table lean-syntax-table
:abbrev-table lean-abbrev-table
:group 'lean
(set (make-local-variable 'comment-start) "--")
(set (make-local-variable 'comment-start-skip) "[-/]-[ \t]*")
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'comment-end-skip) "[ \t]*\\(-/\\|\\s>\\)")
(set (make-local-variable 'comment-padding) 1)
(set (make-local-variable 'comment-use-syntax) t)
(set (make-local-variable 'font-lock-defaults) lean-font-lock-defaults)
(set (make-local-variable 'indent-tabs-mode) nil)
(set 'compilation-mode-font-lock-keywords '())
(set-input-method "Lean")
(set (make-local-variable 'lisp-indent-function)
'common-lisp-indent-function)
(lean-set-keys)
(if (fboundp 'electric-indent-local-mode)
(electric-indent-local-mode -1))
;; (abbrev-mode 1)
(pcase-dolist (`(,hook . ,fn) lean-hooks-alist)
(add-hook hook fn nil 'local))
(lean-mode-setup))
;;; Automatically update TAGS file without asking
(setq tags-revert-without-query t)
;; Automatically use lean-mode for .lean files.
;;;###autoload
(push '("\\.lean$" . lean-mode) auto-mode-alist)
(push '("\\.hlean$" . lean-mode) auto-mode-alist)
;; Use utf-8 encoding
;;;### autoload
(modify-coding-system-alist 'file "\\.lean\\'" 'utf-8)
(modify-coding-system-alist 'file "\\.hlean\\'" 'utf-8)
;; Flycheck init
(when lean-flycheck-use
(require 'flycheck)
(eval-after-load 'flycheck
'(lean-flycheck-init)))
;; Lean Info Mode (for "*lean-info*" buffer)
;; Automode List
;;;###autoload
(define-derived-mode lean-info-mode prog-mode "Lean-Info"
"Major mode for Lean Info Buffer"
:syntax-table lean-syntax-table
:group 'lean
(set (make-local-variable 'font-lock-defaults) lean-info-font-lock-defaults)
(set (make-local-variable 'indent-tabs-mode) nil)
(set 'compilation-mode-font-lock-keywords '())
(set-input-method "Lean")
(set (make-local-variable 'lisp-indent-function)
'common-lisp-indent-function))
(provide 'lean-mode)
;;; lean-mode.el ends here