-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagma-extra.el
275 lines (218 loc) · 8.57 KB
/
magma-extra.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
;;; magma-extra.el --- Extra features for magma-mode. -*- lexical-binding: t; -*-
;; Copyright (C) 2014 Thibaut VERRON
;; Author: Thibaut VERRON <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'magma-vars)
(require 'magma-smie)
;;;;;;;;;;;;;;;;;;;;
;; Support for imenu
;;;;;;;;;;;;;;;;;;;;
(require 'imenu)
(defconst magma-imenu-defun-regexp
"^\\(function\\|procedure\\|intrinsic\\)[[:space:]]+\\<\\(\\sw+\\)\\>[[:space:]]*(")
(defconst magma-imenu-recformat-regexp
"\\<\\(\\sw+\\)\\> := recformat")
(defconst magma-imenu-type-regexp
"declare type \\<\\(\\sw+\\)\\>")
(setq magma-imenu-generic-expression
(list (list "Function" magma-imenu-defun-regexp 2)
(list "Record" magma-imenu-recformat-regexp 1)
(list "Type" magma-imenu-type-regexp 1)))
;;;;;;;;;;;;;;;;;;;;;;;
;; Support for hideshow
;;;;;;;;;;;;;;;;;;;;;;;
(push '(magma-mode "\\(function\\|procedure\\|for\\|while\\|try\\|if\\|case\\)"
"end \\(function\\|procedure\\|for\\|while\\|try\\|if\\|case\\);" "/[/*]" nil nil)
hs-special-modes-alist)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Electric editting facilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Newline
;;;;;;;;;;;;
(defun magma-newline-when-in-string ()
"Insert a newline in a magma string."
(insert "\"cat \"")
(forward-char -5)
(magma-newline-and-indent)
(forward-char 5))
(defun magma-special-newline-when-in-string ()
"Insert a newline in a magma string, both on display and in the string."
(insert "\\n")
(magma-newline-when-in-string))
(defun magma-special-newline-when-in-c-comment ()
"Insert a newline in a C++-like comment."
(magma-newline-and-indent)
(insert "// "))
(defun magma-newline-when-in-c-comment ()
"Insert a newline in a C-like comment.
If we are not at the end of the line, preserve the comment structure."
(if (looking-at "[[:space:]]*[^[:space:]\n].*$")
(magma-special-newline-when-in-c-comment)
(magma-newline-and-indent)))
(defun magma-newline-when-in-cpp-comment ()
"Insert a newline in a C++-like comment."
(let ((col
(save-excursion
(search-backward "/*")
(forward-char 2)
(if (magma-looking-at-end-of-line)
0
(+ (current-column) 1)))))
(newline)
(indent-to-column col)))
(defun magma-newline-and-indent ()
"Like ‘newline-and-indent’, but without deleting the trailing spaces."
(interactive)
(newline)
(magma-indent-line))
(defun magma-insert-newline ()
"Insert a newline depending on where the point is."
(interactive)
(cl-case (car (magma-in-literal))
('string (magma-newline-when-in-string))
('c (magma-newline-when-in-c-comment))
('c++ (magma-newline-when-in-cpp-comment))
(t (magma-newline-and-indent))))
(defun magma-insert-special-newline ()
"Insert a special newline depending on where the point is."
(interactive)
(cl-case (car (magma-in-literal))
('string (magma-special-newline-when-in-string))
('c (magma-special-newline-when-in-c-comment))
('c++ (magma-newline-when-in-cpp-comment))
(t (magma-newline-and-indent))))
;; Smartparens
;;;;;;;;;;;;;;
(defun magma-smartparens-gt-in-an-arrow (_id _beg end)
"Check whether > is part of an arrow.
ID, BEG and END are arguments for the `smartparens' interface and
the first two are ignored."
(save-excursion
(goto-char (- end 2))
(looking-at "-")))
(defun magma-smartparens-escaped-brace-in-intrinsic (_id _beg end)
"Check whether seen } is escaped in intrinsic docstring.
ID, BEG and END are arguments for the `smartparens' interface and
the first two are ignored."
;;(message "%s %s" _beg end)
(save-excursion
(goto-char (- end 2))
(and (eq (magma-in-literal) 'intrinsic)
(looking-at "\\\\"))))
(declare-function sp-with-modes "ext:smartparens.el" t t)
(declare-function sp-local-pair "ext:smartparens.el" t t)
(eval ;; Hide this form from the byte-compiler
'(eval-after-load 'smartparens
'(sp-with-modes '(magma-mode
magma-comint-interactive-mode
magma-term-interactive-mode)
(sp-local-pair "<" ">"
:actions '(insert wrap autoskip navigate)
:skip-match 'magma-smartparens-gt-in-an-arrow)
;; (sp-local-pair "{" "}"
;; :actions '(insert wrap autoskip navigate)
;; :skip-match nil ;; 'magma-smartparens-escaped-brace-in-intrinsic
;; )
(sp-local-pair "`" nil :actions '()))))
;; File header
;;;;;;;;;;;;;;
(defcustom magma-file-header nil
"File header for magma source files.
If non-nil, magma will maintain a file header for the magma
files. This variable should then be either `default', in which case
we use the default header (see the documentation for
`magma-update-header-default'), or the name of a function which
will create and update this header.
This variable can always be overridden as a file-local variable."
:group 'magma
:type '(choice (const :tag "Off" nil)
(const :tag "Default" 'default)
(function :tag "Custom function")))
(defun magma-update-header-default ()
"Update the default header for magma files.
The first line is the date of
creation, the second the date of last modification, and the
third a hash of the rest of the buffer. This function is
intended to be user together with `yasnippet' and
`magma-initial-file' set to t, in order to create the initial content.
`magma-update-header-default' will only operate if the file
begins with \"// Created\", in order not to accidentally
overwrite contents of a file."
(save-excursion
(goto-char (point-min))
(when (looking-at "/[*/] -\\*-") (forward-line 1)) ;; prop-line
(when (looking-at "// Created:")
(forward-line 1)
(when (looking-at "// Last modified:")
(delete-region (point) (progn (forward-line 1) (point))))
(insert (format "// Last modified: %s\n" (current-time-string)))
(when (looking-at "// Hash:")
(delete-region (point) (progn (forward-line 1) (point))))
(let ((start
(save-excursion
(forward-line 2)
(point))))
(insert (format
"// Hash: %s\n"
(secure-hash 'md5 (current-buffer) start (point-max))))))))
(defun magma-update-header ()
"Update the header for magma files."
(when magma-file-header
(if (eq magma-file-header 'default)
(magma-update-header-default)
(funcall magma-file-header))))
(add-hook
'magma-mode-hook
(lambda ()
(add-hook 'write-contents-functions
'magma-update-header)))
(defcustom magma-initial-file nil
"Contents to insert in a new magma file.
This can be either
`default', in which case we insert a skeleton of the header
described in `magma-update-header-default', or a function name which
is then evaluated.
Based on function `auto-insert'"
:group 'magma
:type '(choice (const :tag "None" nil)
(file :tag "File" :must-match t)))
(defun magma-initial-file-contents-default ()
"Insert a skeleton of the header described in `magma-update-header-default'."
(insert (format "// Created: %s\n" (current-time-string)))
(insert "// Last modified:\n")
(insert "// Hash:\n")
(insert (format "// load \"%s\";\n"
(replace-regexp-in-string
"/scpc:.*?:" ""
buffer-file-name)))
(insert "\n")
(insert (format "ChangeDirectory(\"%s\");"
(replace-regexp-in-string
"/scpc:.*?:" ""
default-directory)))
(insert "\n"))
(defun magma-initial-file-contents ()
"Insert the initial file contents if needed."
(when (and magma-initial-file
buffer-file-name
;; Make sure auto-insert has not been already called
(= (point-max) 1))
(if (eq magma-initial-file 'default)
(magma-initial-file-contents-default)
(funcall magma-initial-file))))
(define-auto-insert 'magma-mode 'magma-initial-file-contents)
(provide 'magma-extra)
;;; magma-extra.el ends here