2014-08-07 15:02:04 +00:00
|
|
|
|
;; Copyright (c) 2013, 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
|
;; Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
;;
|
|
|
|
|
;; Author: Leonardo de Moura
|
|
|
|
|
;; Soonho Kong
|
|
|
|
|
;;
|
2014-08-07 17:40:20 +00:00
|
|
|
|
(require 'cl-lib)
|
2014-01-08 11:25:05 +00:00
|
|
|
|
(require 'generic-x)
|
2014-01-09 19:45:31 +00:00
|
|
|
|
(require 'compile)
|
2014-07-28 00:03:06 +00:00
|
|
|
|
(require 'flymake)
|
2014-08-25 23:51:23 +00:00
|
|
|
|
(require 'dash)
|
|
|
|
|
(require 'dash-functional)
|
2014-08-14 00:02:49 +00:00
|
|
|
|
(require 'lean-variable)
|
2014-08-07 15:02:04 +00:00
|
|
|
|
(require 'lean-util)
|
|
|
|
|
(require 'lean-settings)
|
|
|
|
|
(require 'lean-flycheck)
|
|
|
|
|
(require 'lean-input)
|
2014-08-14 00:02:49 +00:00
|
|
|
|
(require 'lean-type)
|
2014-08-16 03:48:30 +00:00
|
|
|
|
(require 'lean-tags)
|
2014-08-18 20:32:58 +00:00
|
|
|
|
(require 'lean-syntax)
|
2014-08-07 15:02:04 +00:00
|
|
|
|
|
|
|
|
|
(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))
|
2014-01-09 19:45:31 +00:00
|
|
|
|
|
2014-08-14 00:02:49 +00:00
|
|
|
|
(defun lean-create-temp-in-system-tempdir (file-name prefix)
|
2014-08-07 15:02:04 +00:00
|
|
|
|
"Create a temp lean file and return its name"
|
2014-07-28 00:03:06 +00:00
|
|
|
|
(make-temp-file (or prefix "flymake") nil ".lean"))
|
|
|
|
|
|
2014-07-29 17:57:52 +00:00
|
|
|
|
(defun lean-execute (&optional arg)
|
2014-01-09 19:45:31 +00:00
|
|
|
|
"Execute Lean in the current buffer"
|
2014-07-29 17:57:52 +00:00
|
|
|
|
(interactive "sarg: ")
|
2014-08-07 15:02:04 +00:00
|
|
|
|
(let ((target-file-name
|
2014-08-14 00:02:49 +00:00
|
|
|
|
(or
|
|
|
|
|
(buffer-file-name)
|
|
|
|
|
(flymake-init-create-temp-buffer-copy 'lean-create-temp-in-system-tempdir))))
|
2014-08-07 15:02:04 +00:00
|
|
|
|
(compile (lean-compile-string
|
|
|
|
|
(lean-get-executable lean-executable-name)
|
2014-08-13 22:05:46 +00:00
|
|
|
|
(or arg "")
|
2014-08-07 15:02:04 +00:00
|
|
|
|
target-file-name))))
|
2014-07-29 17:57:52 +00:00
|
|
|
|
|
|
|
|
|
(defun lean-std-exe ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(lean-execute))
|
|
|
|
|
|
2014-01-09 19:45:31 +00:00
|
|
|
|
(defun lean-set-keys ()
|
2014-07-29 17:57:52 +00:00
|
|
|
|
(local-set-key "\C-c\C-x" 'lean-std-exe)
|
|
|
|
|
(local-set-key "\C-c\C-l" 'lean-std-exe)
|
2014-08-25 23:51:23 +00:00
|
|
|
|
(local-set-key "\C-c\C-o" 'lean-set-option)
|
|
|
|
|
(local-set-key "\C-c\C-e" 'lean-eval-cmd)
|
2014-08-15 00:10:58 +00:00
|
|
|
|
(local-set-key "\C-c\C-t" 'lean-eldoc-documentation-function)
|
2014-08-16 03:48:30 +00:00
|
|
|
|
(local-set-key "\C-c\C-f" 'lean-fill-placeholder)
|
2014-08-16 04:23:36 +00:00
|
|
|
|
(local-set-key "\M-." 'lean-find-tag)
|
2014-08-18 23:10:07 +00:00
|
|
|
|
(local-set-key [tab] 'completion-at-point))
|
2014-01-08 11:25:05 +00:00
|
|
|
|
|
2014-08-18 20:32:58 +00:00
|
|
|
|
(define-abbrev-table 'lean-abbrev-table
|
|
|
|
|
'(("var" "variable")
|
2014-01-10 03:57:00 +00:00
|
|
|
|
("vars" "variables")
|
|
|
|
|
("def" "definition")
|
2014-08-16 03:48:30 +00:00
|
|
|
|
("th" "theorem")))
|
2014-01-10 03:57:00 +00:00
|
|
|
|
|
2014-08-25 20:31:44 +00:00
|
|
|
|
;; ;; Roll back to generic-mode
|
|
|
|
|
;; (define-generic-mode
|
|
|
|
|
;; 'lean-mode ;; name of the mode to create
|
|
|
|
|
;; '("--") ;; comments start with
|
|
|
|
|
;; '("import" "abbreviation" "opaque_hint" "tactic_hint" "definition" "renaming" "inline" "hiding" "exposing" "parameter" "parameters" "proof" "qed" "conjecture" "hypothesis" "lemma" "corollary" "variable" "variables" "print" "theorem" "axiom" "inductive" "with" "structure" "universe" "alias" "help" "environment" "options" "precedence" "postfix" "prefix" "calc_trans" "calc_subst" "calc_refl" "infix" "infixl" "infixr" "notation" "eval" "check" "exit" "coercion" "end" "private" "using" "namespace" "builtin" "including" "instance" "section" "set_option" "add_rewrite" "extends") ;; some keywords
|
|
|
|
|
;; '(("\\_<\\(bool\\|int\\|nat\\|real\\|Prop\\|Type\\|ℕ\\|ℤ\\)\\_>" . 'font-lock-type-face)
|
|
|
|
|
;; ("\\_<\\(calc\\|have\\|obtains\\|show\\|by\\|in\\|let\\|forall\\|fun\\|exists\\|if\\|then\\|else\\|assume\\|take\\|obtain\\|from\\)\\_>" . font-lock-keyword-face)
|
|
|
|
|
;; ("\"[^\"]*\"" . 'font-lock-string-face)
|
|
|
|
|
;; ("\\(->\\|↔\\|/\\\\\\|==\\|\\\\/\\|[*+/<=>¬∧∨≠≤≥-]\\)" . 'font-lock-constant-face)
|
|
|
|
|
;; ("\\(λ\\|→\\|∃\\|∀\\|:\\|:=\\)" . font-lock-constant-face)
|
|
|
|
|
;; ("\\_<\\(\\b.*_tac\\|Cond\\|or_else\\|t\\(?:hen\\|ry\\)\\|when\\|assumption\\|apply\\|b\\(?:ack\\|eta\\)\\|done\\|exact\\)\\_>" . 'font-lock-constant-face)
|
|
|
|
|
;; ("\\_<\\(universe\\|inductive\\|theorem\\|axiom\\|lemma\\|hypothesis\\|abbreviation\\|definition\\|variable\\|parameter\\)\\_>[ \t\{\[]*\\([^ \t\n]*\\)" (2 'font-lock-function-name-face))
|
|
|
|
|
;; ("\\_<\\(variables\\|parameters\\)\\_>[ \t\(\{\[]*\\([^:]*\\)" (2 'font-lock-function-name-face))
|
|
|
|
|
;; ("\\(set_opaque\\|set_option\\)[ \t]*\\([^ \t\n]*\\)" (2 'font-lock-constant-face))
|
|
|
|
|
;; ("\\_<_\\_>" . 'font-lock-preprocessor-face)
|
|
|
|
|
;; ("\\_<sorry\\_>" . 'font-lock-warning-face)
|
|
|
|
|
;; ;;
|
|
|
|
|
;; )
|
|
|
|
|
;; '("\\.lean$") ;; files for which to activate this mode
|
|
|
|
|
;; '((lambda()
|
|
|
|
|
;; (set-input-method "Lean")
|
|
|
|
|
;; (set (make-local-variable 'lisp-indent-function)
|
|
|
|
|
;; 'common-lisp-indent-function)
|
|
|
|
|
;; (lean-set-keys)
|
|
|
|
|
;; (setq local-abbrev-table lean-abbrev-table)
|
|
|
|
|
;; (abbrev-mode 1)
|
|
|
|
|
;; (add-hook 'before-change-functions '
|
|
|
|
|
;; lean-before-change-function nil t)
|
|
|
|
|
;; (add-hook 'after-change-functions '
|
|
|
|
|
;; lean-after-change-function nil t)
|
|
|
|
|
;; ;; 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)
|
|
|
|
|
;; (add-hook 'lean-mode-hook 'fci-mode nil t))))
|
|
|
|
|
;; ;; Delete Trailing Whitespace
|
|
|
|
|
;; (if lean-delete-trailing-whitespace
|
|
|
|
|
;; (progn (require 'whitespace-cleanup-mode)
|
|
|
|
|
;; (add-hook 'lean-mode-hook 'whitespace-cleanup-mode nil t))
|
|
|
|
|
;; (remove-hook 'lean-mode-hook 'whitespace-cleanup-mode))
|
|
|
|
|
;; ;; eldoc
|
|
|
|
|
;; (set (make-local-variable 'eldoc-documentation-function)
|
|
|
|
|
;; 'lean-eldoc-documentation-function)
|
|
|
|
|
;; (eldoc-mode +1)
|
|
|
|
|
;; ;; flycheck
|
|
|
|
|
;; (when lean-flycheck-use
|
|
|
|
|
;; (lean-flycheck-init))
|
|
|
|
|
;; ;; company-mode
|
|
|
|
|
;; (when lean-company-use
|
|
|
|
|
;; (require 'company)
|
|
|
|
|
;; (company-mode t)
|
|
|
|
|
;; (add-to-list 'company-etags-modes 'lean-mode)
|
|
|
|
|
;; (set (make-local-variable 'company-backends) '(company-etags)))))
|
|
|
|
|
;; "A mode for Lean files" ;; doc string for this mode
|
|
|
|
|
;; )
|
|
|
|
|
|
|
|
|
|
(defconst lean-hooks-alist
|
|
|
|
|
'(
|
|
|
|
|
;; Handle events that may start automatic syntax checks
|
|
|
|
|
;; (after-save-hook . flycheck-handle-save)
|
|
|
|
|
(after-change-functions . lean-after-change-function)
|
|
|
|
|
(before-change-functions . lean-before-change-function)
|
|
|
|
|
;; ;; Handle events that may triggered pending deferred checks
|
|
|
|
|
;; (window-configuration-change-hook . flycheck-perform-deferred-syntax-check)
|
|
|
|
|
;; (post-command-hook . flycheck-perform-deferred-syntax-check)
|
|
|
|
|
;; ;; Teardown Flycheck whenever the buffer state is about to get lost, to
|
|
|
|
|
;; ;; clean up temporary files and directories.
|
|
|
|
|
;; (kill-buffer-hook . flycheck-teardown)
|
|
|
|
|
;; (change-major-mode-hook . flycheck-teardown)
|
|
|
|
|
;; (before-revert-hook . flycheck-teardown)
|
|
|
|
|
;; ;; Update the error list if necessary
|
|
|
|
|
;; (post-command-hook . flycheck-error-list-update-source)
|
|
|
|
|
;; (post-command-hook . flycheck-error-list-highlight-errors)
|
|
|
|
|
;; ;; Show or hide error popups after commands
|
|
|
|
|
;; (post-command-hook . flycheck-display-error-at-point-soon)
|
|
|
|
|
;; (post-command-hook . flycheck-hide-error-buffer)
|
|
|
|
|
;; ;; Immediately show error popups when navigating to an error
|
|
|
|
|
;; (next-error-hook . flycheck-display-error-at-point))
|
2014-08-21 16:09:54 +00:00
|
|
|
|
)
|
2014-08-25 20:31:44 +00:00
|
|
|
|
"Hooks which lean-mode needs to hook in.
|
2014-08-21 16:09:54 +00:00
|
|
|
|
|
2014-08-25 20:31:44 +00:00
|
|
|
|
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.")
|
2014-08-21 16:09:54 +00:00
|
|
|
|
|
2014-08-25 20:31:44 +00:00
|
|
|
|
(defun lean-mode-setup ()
|
|
|
|
|
"Default lean-mode setup"
|
|
|
|
|
;; Flycheck
|
|
|
|
|
(when lean-flycheck-use (lean-flycheck-turn-on))
|
|
|
|
|
;; 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))))
|
|
|
|
|
;; Delete Trailing Whitespace
|
|
|
|
|
(if lean-delete-trailing-whitespace
|
|
|
|
|
(progn (require 'whitespace-cleanup-mode)
|
|
|
|
|
(whitespace-cleanup-mode t))
|
|
|
|
|
(whitespace-cleanup-mode nil))
|
|
|
|
|
;; eldoc
|
|
|
|
|
(when lean-eldoc-use
|
|
|
|
|
(set (make-local-variable 'eldoc-documentation-function)
|
|
|
|
|
'lean-eldoc-documentation-function)
|
2014-08-26 23:02:15 +00:00
|
|
|
|
(eldoc-mode t)
|
|
|
|
|
(lean-eldoc-documentation-function))
|
2014-08-25 20:31:44 +00:00
|
|
|
|
;; company-mode
|
|
|
|
|
(when lean-company-use
|
|
|
|
|
(require 'company)
|
|
|
|
|
(company-mode t)
|
|
|
|
|
(set (make-local-variable 'company-backends) '(company-etags))))
|
|
|
|
|
|
|
|
|
|
;; Automode List
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(define-derived-mode lean-mode prog-mode "Lean"
|
|
|
|
|
"Major mode for Lean"
|
|
|
|
|
:syntax-table lean-syntax-table
|
|
|
|
|
:abbrev-table lean-abbrev-table
|
|
|
|
|
:group 'lean
|
|
|
|
|
(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)
|
2014-08-26 22:07:08 +00:00
|
|
|
|
(set (make-local-variable 'tags-revert-without-query) t)
|
2014-08-25 20:31:44 +00:00
|
|
|
|
(set (make-local-variable 'indent-tabs-mode) nil)
|
|
|
|
|
(set-input-method "Lean")
|
|
|
|
|
(set (make-local-variable 'lisp-indent-function)
|
|
|
|
|
'common-lisp-indent-function)
|
|
|
|
|
(lean-set-keys)
|
|
|
|
|
(abbrev-mode 1)
|
|
|
|
|
(pcase-dolist (`(,hook . ,fn) lean-hooks-alist)
|
|
|
|
|
(add-hook hook fn nil 'local))
|
|
|
|
|
(lean-mode-setup))
|
2014-08-18 20:32:58 +00:00
|
|
|
|
|
|
|
|
|
;; Automatically use lean-mode for .lean files.
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(push '("\\.lean$" . lean-mode) auto-mode-alist)
|
2014-08-25 20:31:44 +00:00
|
|
|
|
|
2014-08-18 20:32:58 +00:00
|
|
|
|
;; Use utf-8 encoding
|
2014-08-18 23:10:07 +00:00
|
|
|
|
;;;### autoload
|
2014-08-18 20:32:58 +00:00
|
|
|
|
(modify-coding-system-alist 'file "\\.lean\\'" 'utf-8)
|
|
|
|
|
|
2014-08-25 20:31:44 +00:00
|
|
|
|
;; Flycheck init
|
|
|
|
|
(when lean-flycheck-use
|
|
|
|
|
(require 'flycheck)
|
|
|
|
|
(eval-after-load 'flycheck
|
|
|
|
|
'(lean-flycheck-init)))
|
|
|
|
|
|
2014-01-09 17:32:47 +00:00
|
|
|
|
(provide 'lean-mode)
|