-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefer-macro.el
103 lines (91 loc) · 3.06 KB
/
defer-macro.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
;;; defer-macro.el -*- lexical-binding: t; -*-
;; (defmacro file! ()
;; "Return the file of the file this macro was called."
;; (or (macroexp-file-name) load-file-name buffer-file-name)
;; )
;; (defmacro dir! ()
;; "Return the directory of the file this macro was called.
;; from doom."
;; (file-name-directory (file!)))
(defmacro defvar! (sym &optional initvalue docstring force)
"Define variables, without worrying about overriding them "
`(progn (cond (,force (setq ,sym ,initvalue))
((symbol-file (quote ,sym)) nil)
((boundp (quote ,sym)) (defvar ,sym (if (boundp (quote ,sym)) ,sym ,initvalue) ,docstring))
(t (defvar ,sym ,initvalue ,docstring))
)
,sym)
)
(defmacro dlog! (text &rest args)
" A Simple, doom-less debug message when 'debug-on-error is true"
`(when debug-on-error
(let ((inhibit-message t))
(funcall #'message ,text ,@args)
)
)
)
(defun defer--load-files (dir &rest files)
"Load files with a debug log message"
(cl-loop for file in files
do
(dlog! "Deferred Loading: %s : %s" dir file)
(let ((fname (file-name-concat dir file)))
(condition-case err
(load fname nil (file-exists-p (concat fname ".el")))
(t (message "Error: %s (%s)" err fname))
)
)
)
)
(defmacro defer-load! (&optional afters &rest files)
"set a timer to load the given files after 5 seconds"
(let ((core-timer `(run-with-idle-timer (+ 4 (random 4)) nil
#'defer--load-files
(dir!)
,@(if (stringp afters) (cons afters files) files)
))
temp
)
(cond ((symbolp afters) '(val)
(list #'with-eval-after-load (list 'quote afters)
core-timer))
((consp afters)
(setq temp core-timer)
(while afters
(setq temp (append (list #'with-eval-after-load (list 'quote (pop afters)))
(list temp))))
temp)
(t (list 'progn core-timer))
)
)
)
(defmacro local-load! (filename &optional noerror)
`(load (file-name-concat (dir!) ,filename) ,noerror 'nomessage)
)
(defmacro defer! (time &rest body)
`(run-with-idle-timer ,time nil
(lambda ()
,@body
)
)
)
(defun debug-func--print-return (fn x)
(message "Fn: %s : %s" fn x)
)
(defun debug-func! (fn)
(advice-add fn :filter-return (-partial #'debug-func--print-return fn))
)
(defun undebug-func! (fn)
(advice-mapc (lambda (func props) (advice-remove fn func))
fn
)
)
(defmacro with-state! (state fn)
;; (declare (doc-string 1) (pure t) (side-effect-free t))
`(defun ,(intern (format "%s--with-state-%s" (cadr fn) (cadr state))) (&rest args)
(interactive)
(minibuffer-with-setup-hook (:append (quote ,(intern (format "evil-%s-state" (cadr state)))))
(apply ,fn args)
)
)
)