forked from ecukes/ecukes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecukes-core.el
103 lines (84 loc) · 3.14 KB
/
ecukes-core.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
;;; ecukes-core.el --- Core functionality common to all Ecukes components
(require 'f)
(require 's)
(require 'dash)
(defvar ecukes-message nil
"If true message is internal Ecukes message, otherwise external.")
(defvar ecukes-verbose t
"If true, show all message output, otherwise hide.")
(defvar ecukes-internal-message-log nil
"List with `message' output.")
(defvar ecukes-message-log nil
"List with `message' output (only from external code).")
(defvar ecukes-debug-callbacks nil
"List of functions to callback in debugger.")
(defadvice message (around message-around activate)
(let ((message
(s-concat
(if (car (ad-get-args 0))
(apply 'format (ad-get-args 0))
"")
"\n")))
(unless ecukes-message
(add-to-list 'ecukes-message-log message t 'eq))
(when (or ecukes-message ecukes-verbose)
(add-to-list 'ecukes-internal-message-log `(message . ,message) t 'eq)
ad-do-it)))
(defadvice princ (around princ-around activate)
(let ((message (or (car (ad-get-args 0)) "")))
(unless ecukes-message
(add-to-list 'ecukes-message-log message t 'eq))
(when (or ecukes-message ecukes-verbose)
(add-to-list 'ecukes-internal-message-log `(princ . ,message) t 'eq)
ad-do-it)))
(defadvice print (around print-around activate)
(add-to-list 'ecukes-internal-message-log `(print . ,ad-do-it) t 'eq))
(defun ecukes-quit (&optional exit-code)
"Quit Emacs with EXIT-CODE and write to file if in graphical mode."
(or exit-code (setq exit-code 1))
(let ((outfile (getenv "ECUKES_OUTFILE")))
(when outfile
(let ((output
(-map
(lambda (log)
(let ((message (cdr log)))
(if (eq (car log) 'print)
(prin1-to-string message)
message)))
ecukes-internal-message-log)))
(f-write-text (s-join "" output) 'utf-8 outfile))))
(kill-emacs exit-code))
(defun ecukes-fail (format-string &rest objects)
"Print error message and quit."
(let ((ecukes-message t))
(message (apply 'ansi-red (cons format-string objects)))
(ecukes-quit 1)))
(defun ecukes-on-debug (callback)
"Call CALLBACK with backtrace from debug."
(add-to-list 'ecukes-debug-callbacks callback 'append))
(defun ecukes-debug (&rest debugger-args)
"Ecukes debugger.
This is called when an error occurs. The function creates a
decent backtrace and callbacks all functions in
`ecukes-debug-callbacks' with the backtrace."
(let ((backtrace
(with-temp-buffer
(set-buffer-multibyte t)
(let ((standard-output (current-buffer))
(print-escape-newlines t)
(print-level 8)
(print-length 50))
(backtrace))
(goto-char (point-min))
(delete-region
(point)
(progn
(search-forward "\n ecukes-debug(")
(forward-line 1)
(point)))
(buffer-string))))
(-each ecukes-debug-callbacks
(lambda (callback)
(funcall callback backtrace)))))
(provide 'ecukes-core)
;;; ecukes-core.el ends here