-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabmate.el
executable file
·112 lines (89 loc) · 3.13 KB
/
labmate.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
;;(require 'compile)
;; based on: http://ergoemacs.org/emacs/elisp_syntax_coloring.html
;; syntax table
;;(defvar hacky-syntax-table (make-syntax-table))
(defface labmate-directive
'((t :foreground "black"
:background "pale turquoise"
:weight bold
))
"Face for directives."
:group 'labmate )
(defface labmate-response-delimiter
'((t :foreground "black"
:background "pale green"
))
"Face for response delimiters."
:group 'labmate )
(defface labmate-response-error
'((t :foreground "black"
:background "light salmon"
:weight bold
))
"Face for errorneous directive responses."
:group 'labmate )
(defface labmate-response-success
'((t :foreground "black"
:background "pale green"
:weight bold
))
"Face for successful directive responses."
:group 'labmate )
;; define the mode
(define-derived-mode labmate-mode octave-mode
"LabMate mode"
;; handling comments
:syntax-table (make-syntax-table)
;; code for syntax highlighting
(font-lock-add-keywords nil '(("^\s*%>[^%\n]+" 0 'labmate-directive t)))
(font-lock-add-keywords nil '(("^\s*%<.+" . 'labmate-response-error)))
(font-lock-add-keywords nil '(("^\s*%<[{}]$" . 'labmate-response-delimiter)))
(font-lock-add-keywords nil '(("^\s*%<\s*renamed[^%\n]+" . 'labmate-response-success)))
(font-lock-add-keywords nil '(("^\s*%<\s*.*::[^%\n]+" . 'labmate-response-success)))
(font-lock-add-keywords nil '(("^\s*%<\s*LabMate[^%\n]+" . 'labmate-response-success)))
;; Fold generated code
(hs-minor-mode)
(setq mode-name "labmate")
)
;; Customisation options
(defgroup labmate nil
"A Matlab helper."
:group 'languages)
(defcustom labmate-command "labmate"
"The path to the LabMate command to run."
:type 'string
:group 'labmate)
(defcustom labmate-hide-generated-code nil
"Determine if generated code should be hidden after running LabMate."
:type 'boolean
:group 'labmate)
(defface labmate-highlight-error-face
'((t (:underline (:color "red" :style wave))))
"The face used for errors.")
(defun labmate-run-on-file (labmate-file)
"Run LabMate on the current buffer and replace the buffer contents with the LabMate output."
(save-some-buffers compilation-ask-about-save
(when (boundp 'compilation-save-buffers-predicate)
compilation-save-buffers-predicate))
(let* ((res (with-temp-buffer
(list (call-process labmate-command nil
(current-buffer) nil labmate-file)
(buffer-string))))
(exitcode (car res))
(output (cadr res)))
(if (< exitcode 10)
(with-current-buffer (current-buffer)
(let ((old-point (point)))
(erase-buffer)
(insert output)
(goto-char old-point)))
(message "%s" output))))
;;;###autoload
(defun labmate-run (override-options)
"Run LabMate on the current file."
(interactive "P")
(labmate-run-on-file (shell-quote-argument (buffer-file-name)))
(when labmate-hide-generated-code (hs-hide-all))
)
(define-key labmate-mode-map (kbd "C-c C-l") 'labmate-run)
(provide 'labmate-mode)