This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
lean-util.el
103 lines (92 loc) · 3.37 KB
/
lean-util.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
;; Copyright (c) 2014 Microsoft Corporation. All rights reserved.
;; Released under Apache 2.0 license as described in the file LICENSE.
;;
;; Author: Soonho Kong
;;
(require 'cl-lib)
(require 'f)
(require 's)
(require 'dash)
(defun lean-setup-rootdir ()
(let ((root (executable-find lean-executable-name)))
(when root
(setq lean-rootdir (f-dirname (f-dirname root))))
root))
(defun lean-get-rootdir ()
(if lean-rootdir
(let ((lean-path (f-full (f-join lean-rootdir "bin" lean-executable-name))))
(unless (f-exists? lean-path)
(error "Incorrect 'lean-rootdir' value, path '%s' does not exist." lean-path))
lean-rootdir)
(or
(lean-setup-rootdir)
(error
(concat "Lean was not found in the 'exec-path' and 'lean-rootdir' is not defined. "
"Please set it via M-x customize-variable RET lean-rootdir RET.")))))
(defun lean-get-executable (exe-name)
"Return fullpath of lean executable"
(let ((lean-bin-dir-name "bin"))
(f-full (f-join (lean-get-rootdir) lean-bin-dir-name exe-name))))
(defun lean-line-offset (&optional pos)
"Return the byte-offset of `pos` or current position, counting from the
beginning of the line"
(interactive)
(let* ((pos (or pos (point)))
(bol-pos
(save-excursion
(goto-char pos)
(beginning-of-line)
(point))))
(- pos bol-pos)))
(defun lean-pos-at-line-col (l c)
"Return the point of the given line and column."
;; http://emacs.stackexchange.com/a/8083
(save-excursion
(goto-char (point-min))
(forward-line (- l 1))
(move-to-column c)
(point)))
(defun lean-whitespace-cleanup ()
(when lean-delete-trailing-whitespace
(delete-trailing-whitespace)))
(defun lean-in-comment-p ()
"t if a current point is inside of comment block
nil otherwise"
(nth 4 (syntax-ppss)))
;; The following function is a slightly modified version of
;; f--collect-entries written by Johan Andersson
;; The URL is at https://github.com/rejeep/f.el/blob/master/f.el#L416-L435
(defun lean--collect-entries (path recursive)
(let (result
(entries
(-reject
(lambda (file)
(or
(equal (f-filename file) ".")
(equal (f-filename file) "..")))
(directory-files path t))))
;; The following line is the only modification that I made
;; It waits 0.0001 second for an event. This wait allows
;; wait-timeout function to check the timer and kill the execution
;; of this function.
(sit-for 0.0001)
(cond (recursive
(-map
(lambda (entry)
(if (f-file? entry)
(setq result (cons entry result))
(when (f-directory? entry)
(setq result (cons entry result))
(setq result (append result (lean--collect-entries entry recursive))))))
entries))
(t (setq result entries)))
result))
;; The following function is a slightly modified version of
;; f-files function written by Johan Andersson The URL is at
;; https://github.com/rejeep/f.el/blob/master/f.el#L478-L481
(defun lean-find-files (path &optional fn recursive)
"Find all files in PATH."
;; It calls lean--collect-entries instead of f--collect-entries
(let ((files (-select 'f-file? (lean--collect-entries path recursive))))
(if fn (-select fn files) files)))
(provide 'lean-util)