-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlean4-mode.el
301 lines (262 loc) · 11.4 KB
/
lean4-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
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
;;; lean4-mode.el --- Major mode for Lean language -*- lexical-binding: t; -*-
;; Copyright (c) 2013, 2014 Microsoft Corporation. All rights reserved.
;; Copyright (c) 2014, 2015 Soonho Kong. All rights reserved.
;; Author: Leonardo de Moura <[email protected]>
;; Soonho Kong <[email protected]>
;; Gabriel Ebner <[email protected]>
;; Sebastian Ullrich <[email protected]>
;; Maintainer: Yury G. Kudryashov <[email protected]>
;; Created: Jan 09, 2014
;; Keywords: languages
;; Package-Requires: ((emacs "27.1") (compat "28.1") (dash "2.18.0") (magit-section "2.90.1") (lsp-mode "8.0.0"))
;; URL: https://github.com/leanprover-community/lean4-mode
;; SPDX-License-Identifier: Apache-2.0
;; Version: 1.1.2
;; This file is not part of GNU Emacs.
;; Licensed under the Apache License, Version 2.0 (the "License"); you
;; may not use this file except in compliance with the License. You
;; may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
;; implied. See the License for the specific language governing
;; permissions and limitations under the License.
;;; Commentary:
;; Provides a major mode for the Lean programming language.
;; Provides highlighting, diagnostics, goal visualization,
;; and many other useful features for Lean users.
;; See the README.md for more advanced features and the
;; associated keybindings.
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'pcase)
(require 'lsp-mode)
(require 'lean4-eri)
(require 'lean4-util)
(require 'lean4-settings)
(require 'lean4-syntax)
(require 'lean4-info)
(require 'lean4-dev)
(require 'lean4-fringe)
(require 'lean4-lake)
;; Declare symbols defined in external dependencies. This silences
;; byte-compiler warnings:
(defvar compilation-mode-font-lock-keywords)
(defvar flycheck-after-syntax-check-hook)
(defvar flycheck-disabled-checkers)
(defvar flycheck-mode)
(defvar lsp--cur-version)
(defvar markdown-code-lang-modes)
(declare-function flycheck-list-errors "ext:flycheck")
(declare-function flymake-proc-init-create-temp-buffer-copy "flymake-proc")
(declare-function lean-mode "ext:lean-mode")
(declare-function quail-show-key "quail")
(defun lean4-compile-string (lake-name exe-name args file-name)
"Command to run EXE-NAME with extra ARGS and FILE-NAME.
If LAKE-NAME is nonempty, then prepend \"LAKE-NAME env\" to the command
\"EXE-NAME ARGS FILE-NAME\"."
(if lake-name
(format "%s env %s %s %s" lake-name exe-name args file-name)
(format "%s %s %s" exe-name args file-name)))
(defun lean4-create-temp-in-system-tempdir (file-name prefix)
"Create a temp lean file and return its name.
The new file has prefix PREFIX (defaults to `flymake') and the same extension as
FILE-NAME."
(make-temp-file (or prefix "flymake") nil (file-name-extension file-name)))
(defun lean4-execute (&optional arg)
"Execute Lean in the current buffer with an optional argument ARG."
(interactive)
(when (called-interactively-p 'any)
(setq arg (read-string "arg: " arg)))
(let* ((cc compile-command)
(dd default-directory)
(use-lake (lean4-lake-find-dir))
(default-directory (if use-lake (lean4-lake-find-dir) dd))
(target-file-name
(or
(buffer-file-name)
(flymake-proc-init-create-temp-buffer-copy 'lean4-create-temp-in-system-tempdir))))
(compile (lean4-compile-string
(if use-lake (shell-quote-argument (expand-file-name (lean4-get-executable lean4-lake-name))) nil)
(shell-quote-argument (expand-file-name (lean4-get-executable lean4-executable-name)))
(or arg "")
(shell-quote-argument (expand-file-name target-file-name))))
;; restore old value
(setq compile-command cc)
(setq default-directory dd)))
(defun lean4-std-exe ()
"Execute Lean in the current buffer."
(interactive)
(lean4-execute))
(defun lean4-refresh-file-dependencies ()
"Refresh the file dependencies.
This function restarts the server subprocess for the current
file, recompiling, and reloading all imports."
(interactive)
(lsp-notify
"textDocument/didClose"
`(:textDocument ,(lsp--text-document-identifier)))
(lsp-notify
"textDocument/didOpen"
(list :textDocument
(list :uri (lsp--buffer-uri)
:languageId (lsp-buffer-language)
:version lsp--cur-version
:text (lsp--buffer-content)))))
(defun lean4-tab-indent ()
"Lean 4 function for TAB indent."
(interactive)
(cond ((looking-back (rx line-start (* white)) nil)
(lean4-eri-indent))
(t (indent-for-tab-command))))
(defun lean4-set-keys ()
"Setup Lean 4 keybindings."
(local-set-key lean4-keybinding-std-exe1 #'lean4-std-exe)
(local-set-key lean4-keybinding-std-exe2 #'lean4-std-exe)
(local-set-key lean4-keybinding-show-key #'quail-show-key)
(local-set-key lean4-keybinding-tab-indent #'lean4-tab-indent)
;; (local-set-key lean4-keybinding-hole #'lean4-hole)
(local-set-key lean4-keybinding-lean4-toggle-info #'lean4-toggle-info)
;; (local-set-key lean4-keybinding-lean4-message-boxes-toggle #'lean4-message-boxes-toggle)
(local-set-key lean4-keybinding-lake-build #'lean4-lake-build)
(local-set-key lean4-keybinding-refresh-file-dependencies #'lean4-refresh-file-dependencies)
;; This only works as a mouse binding due to the event, so it is not abstracted
;; to avoid user confusion.
;; (local-set-key (kbd "<mouse-3>") #'lean4-right-click-show-menu)
)
(define-abbrev-table 'lean4-abbrev-table
'())
(defvar lean4-mode-map (make-sparse-keymap)
"Keymap used in Lean mode.")
(easy-menu-define lean4-mode-menu lean4-mode-map
"Menu for the Lean major mode."
`("Lean 4"
["Execute lean" lean4-execute t]
["Toggle info display" lean4-toggle-info t]
;; TODO: Bug#91: We offers a Flycheck-based menu-item when
;; Flycheck is in use. Users who use built-in Flymake should also
;; be offered a working menu-item. Alternatively, the menu-item
;; could also be dropped for both cases.
["List of errors" flycheck-list-errors flycheck-mode]
["Restart lean process" lsp-workspace-restart t]
["Customize lean4-mode" (customize-group 'lean) t]))
(defconst lean4-hooks-alist
'(
;; Handle events that may start automatic syntax checks
(before-save-hook . lean4-whitespace-cleanup)
;; info view
;; update errors immediately, but delay querying goal
(flycheck-after-syntax-check-hook . lean4-info-buffer-redisplay-debounced)
(post-command-hook . lean4-info-buffer-redisplay-debounced)
(lsp-on-idle-hook . lean4-info-buffer-refresh))
"Hooks which lean4-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 lean4-mode-setup ()
"Default lean4-mode setup."
;; Right click menu sources
;;(setq lean4-right-click-item-functions '(lean4-info-right-click-find-definition
;; lean4-hole-right-click))
;; Flycheck
(setq-local flycheck-disabled-checkers '())
;; Lean massively benefits from semantic tokens, so change default to enabled
(setq-local lsp-semantic-tokens-enable t)
(lean4-create-lsp-workspace))
(defun lean4-create-lsp-workspace ()
"Create an LSP workspace.
Starting from `(buffer-file-name)`, repeatedly look up the
directory hierarchy for a directory containing a file
\"lean-toolchain\", and use the last such directory found, if any.
This allows us to edit files in child packages using the settings
of the parent project."
(let (root)
(when-let ((file-name (buffer-file-name)))
(while-let ((dir (locate-dominating-file file-name "lean-toolchain")))
;; We found a toolchain file, but maybe it belongs to a package.
;; Continue looking until there are no more toolchain files.
(setq root dir
file-name (file-name-directory (directory-file-name dir)))))
(when root
(lsp-workspace-folders-add root))))
;;;###autoload
(define-derived-mode lean4-mode prog-mode "Lean 4"
"Major mode for Lean language.
\\{lean4-mode-map}"
:syntax-table lean4-syntax-table
:abbrev-table lean4-abbrev-table
:group 'lean4
(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) lean4-font-lock-defaults)
(set (make-local-variable 'indent-tabs-mode) nil)
(set 'compilation-mode-font-lock-keywords '())
(require 'lean4-input)
(set-input-method "Lean")
(set (make-local-variable 'lisp-indent-function)
'common-lisp-indent-function)
(lean4-set-keys)
(if (fboundp 'electric-indent-local-mode)
(electric-indent-local-mode -1))
;; (abbrev-mode 1)
(pcase-dolist (`(,hook . ,fn) lean4-hooks-alist)
(add-hook hook fn nil 'local))
(lean4-mode-setup))
(defun lean4--version ()
"Return Lean version as a list `(MAJOR MINOR PATCH)'."
(with-temp-buffer
(call-process (lean4-get-executable "lean") nil (list t nil) nil "-v")
(goto-char (point-min))
(re-search-forward (rx bol "Lean (version " (group (+ digit) (+ "." (+ digit)))))
(version-to-list (match-string 1))))
(defun lean4-show-version ()
"Print Lean 4 version."
(interactive)
(message "Lean %s" (mapconcat #'number-to-string (lean4--version) ".")))
;;;###autoload
(defun lean4-select-mode ()
"Automatically select mode (Lean 3 vs Lean 4)."
(if (and lean4-autodetect-lean3
(eq 3 (car (lean4--version))))
(lean-mode)
(lean4-mode)))
;; Automatically use lean4-mode for .lean files.
;;;###autoload
(add-to-list 'auto-mode-alist
'("\\.lean\\'" . lean4-select-mode))
;;;###autoload
(with-eval-after-load 'markdown-mode
(add-to-list 'markdown-code-lang-modes
'("lean" . lean4-select-mode)))
;; Use utf-8 encoding
;;;### autoload
(modify-coding-system-alist 'file "\\.lean\\'" 'utf-8)
;; LSP init
;; Ref: https://emacs-lsp.github.io/lsp-mode/page/adding-new-language/
(add-to-list 'lsp-language-id-configuration
'(lean4-mode . "lean"))
(defun lean4--server-cmd ()
"Return Lean server command.
If found lake version at least 3.1.0, then return '/path/to/lake serve',
otherwise return '/path/to/lean --server'."
(condition-case nil
(if (string-version-lessp (car (process-lines (lean4-get-executable "lake") "--version")) "3.1.0")
`(,(lean4-get-executable lean4-executable-name) "--server")
`(,(lean4-get-executable "lake") "serve"))
(error `(,(lean4-get-executable lean4-executable-name) "--server"))))
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection #'lean4--server-cmd)
:major-modes '(lean4-mode)
:server-id 'lean4-lsp
:notification-handlers (ht ("$/lean/fileProgress" #'lean4-fringe-update))
:semantic-tokens-faces-overrides '(:types (("leanSorryLike" . font-lock-warning-face)))))
(provide 'lean4-mode)
;;; lean4-mode.el ends here