-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdtrace-script-mode.el
370 lines (346 loc) · 9.86 KB
/
dtrace-script-mode.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
;;; dtrace-script-mode.el --- DTrace code editing commands for Emacs
;;; Commentary:
;; dtrace-script-mode: Mode for editing DTrace D language.
;;
;; You can add the following to your .emacs:
;;
;; (autoload 'dtrace-script-mode "dtrace-script-mode" () t)
;; (add-to-list 'auto-mode-alist '("\\.d\\'" . dtrace-script-mode))
;;
;; When loaded, runs all hooks from dtrace-script-mode-hook
;; You may try
;;
;; (add-hook 'dtrace-script-mode-hook 'imenu-add-menubar-index)
;; (add-hook 'dtrace-script-mode-hook 'font-lock-mode)
;;
;; Alexander Kolbasov <akolb at sun dot com>
;;
;;
;; The dtrace-script-mode inherits from C-mode.
;; It supports imenu and syntax highlighting.
;;
;; $Id: dtrace-script-mode.el,v 1.4 2007/07/17 22:10:23 akolb Exp $
;;; This file is NOT part of GNU Emacs
;;
;; Copyright (c) 2007, Alexander Kolbasov
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above
;; copyright notice, this list of conditions and the following
;; disclaimer in the documentation and/or other materials provided
;; with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
;; FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
;; COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
;; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
;; STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
;; OF THE POSSIBILITY OF SUCH DAMAGE.
;;; Code:
(require 'cc-mode)
(defvar dtrace-script-mode-map nil "Keymap used in D mode buffers.")
;;
;; Define dtrace-script-mode map unless it was defined already
;;
;; We just use c-mode-map with a few tweaks:
;;
;; M-C-a is bound to dtrace-script-mode-beginning-of-function
;; M-C-e is bound to dtrace-script-mode-end-of-function
;; M-C-h is bound to dtrace-script-mode-mark-function
;;
;; Ideally I would like to just replace whatever keybindings exist in c-mode-map
;; for the above with the new bindings. But up until recently, C-M-a was bound
;; to beginning-of-defun and not to c-beginning-of-defun and the same goes for
;; C-M-e, so we just define the new bindings.
;;
(unless dtrace-script-mode-map
(setq dtrace-script-mode-map
(let ((map (c-make-inherited-keymap)))
(define-key map "\e\C-a" 'dtrace-script-mode-beginning-of-function)
(define-key map "\e\C-e" 'dtrace-script-mode-end-of-function)
;; Separate M-BS from C-M-h. The former should remain
;; backward-kill-word.
(define-key map [(control meta h)] 'dtrace-script-mode-mark-function)
map)))
(defvar dtrace-script-mode-syntax-table
(let ((st (make-syntax-table (standard-syntax-table))))
(modify-syntax-entry ?/ "$" st)
(modify-syntax-entry ?` "." st)
(modify-syntax-entry ?: "." st)
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?$ "/" st)
(modify-syntax-entry ?& "." st)
(modify-syntax-entry ?* "." st)
(modify-syntax-entry ?+ "." st)
(modify-syntax-entry ?- "." st)
(modify-syntax-entry ?< "." st)
(modify-syntax-entry ?= "." st)
(modify-syntax-entry ?> "." st)
(modify-syntax-entry ?\\ "\\" st)
(modify-syntax-entry ?/ ". 14" st)
(modify-syntax-entry ?* ". 23" st)
st)
"Syntax table in use in `dtrace-script-mode' buffers.")
;;
;; Show probes, pragmas and inlines in imenu
;;
(defvar dtrace-script-mode-imenu-generic-expression
'(
(nil "^\\s-*\\(\\sw+:.+\\)" 1 )
(nil "\\s-*\\(BEGIN\\|END\\)" 1 )
("Pramgas" "^#pragma\\s-+D\\s-+\\(.+\\)" 1)
("Inlines" "\\s-*inline\\s-+\\(.*\\);" 1)
)
"Imenu generic expression for D mode. See `imenu-generic-expression'.")
(defvar dtrace-script-mode-hook nil
"Hooks to run when entering D mode.")
;;
;; Definition of various DTrace keywords for font-lock-mode
;;
(defconst dtrace-script-mode-font-lock-keywords
(eval-when-compile
(list
;;
;; Function names.
;'("^\\(\\sw+\\):\\(\\sw+\\|:\\)?"
; (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
;;
;; Variable names.
(cons (regexp-opt
'(
"egid" "euid" "gid" "pid" "pgid" "ppid" "projid" "sid"
"taskid" "uid") 'words)
'font-lock-variable-name-face)
;;
;; DTrace built-in variables
;;
(cons (regexp-opt
'(
"NULL"
"arg0" "arg1" "arg2" "arg3" "arg4" "arg5" "arg6" "arg7"
"arg8" "arg9"
"args"
"caller"
"chip"
"cpu"
"curcpu"
"curlwpsinfo"
"curpsinfo"
"curthread"
"cwd"
"epid"
"errno"
"execname"
"gid"
"id"
"ipl"
"lgrp"
"pid"
"ppid"
"probefunc"
"probemod"
"probename"
"probeprov"
"pset"
"pwd"
"root"
"self"
"stackdepth"
"target"
"this"
"tid"
"timestamp"
"uid"
"uregs"
"vtimestamp"
"walltimestamp"
) 'words)
'font-lock-constant-face)
;;
;; DTrace functions.
;;
(list (regexp-opt
'(
"alloca"
"avg"
"basename"
"bcopy"
"cleanpath"
"commit"
"copyin"
"copyinstr"
"copyinto"
"copyout"
"copyoutstr"
"count"
"dirname"
"discard"
"exit"
"jstack"
"lquantize"
"max"
"min"
"msgdsize"
"msgsize"
"mutex_owned"
"mutex_owner"
"mutex_type_adaptive"
"mutex_type_spin"
"offsetof"
"printa"
"printf"
"progenyof"
"quantize"
"raise"
"rand"
"rand"
"rw_iswriter"
"rw_read_held"
"rw_write_held"
"speculate"
"speculation"
"stack"
"stop"
"stringof"
"strjoin"
"strlen"
"sum"
"system"
"trace"
"tracemem"
"trunc"
"ustack"
) 'words)
1 'font-lock-builtin-face)
;;
;; Destructive actions
;;
(list (regexp-opt
'(
"breakpoint"
"chill"
"panic"
) 'words)
1 'font-lock-warning-face)
;;
;; DTrace providers
;;
(regexp-opt
'(
"BEGIN"
"END"
"dtrace"
"dtrace"
"entry"
"fasttrap"
"fbt"
"fpuinfo"
"io"
"lockstat"
"mib"
"pid"
"plockstat"
"proc"
"profile"
"return"
"sched"
"sdt"
"syscall"
"sysinfo"
"tick"
"vm"
"vminfo"
"vtrace"
) 'words)))
"Default expressions to highlight in D mode.")
(defun dtrace-script-mode-beginning-of-function (&optional arg)
"Move backward to next beginning-of-function, or as far as possible.
With argument, repeat that many times; negative args move forward.
Returns new value of point in all cases."
(interactive "p")
(or arg (setq arg 1))
(if (< arg 0) (forward-char 1))
(end-of-line)
(and (/= arg 0)
(re-search-backward "^[ \t]*\\([a-z_]+:.*\\|BEGIN\\|END\\)$"
nil 'move arg)
(goto-char (1- (match-end 0))))
(beginning-of-line))
(defun dtrace-script-mode-end-of-current-function ()
"Locate the end of current D function"
(dtrace-script-mode-beginning-of-function 1)
;; Now locate opening curly brace
(search-forward "{")
(backward-char 1)
;; Find closing curly brace
(forward-list 1))
;; note: this routine is adapted directly from emacs perl-mode.el.
;; no bugs have been removed :-)
(defun dtrace-script-mode-end-of-function (&optional arg)
"Move forward to next end-of-function.
The end of a function is found by moving forward from the beginning of one.
With argument, repeat that many times; negative args move backward."
(interactive "p")
(or arg (setq arg 1))
(let ((first t))
(while (and (> arg 0) (< (point) (point-max)))
(let ((pos (point)) npos)
(while (progn
(if (and first
(progn
(forward-char 1)
(dtrace-script-mode-beginning-of-function 1)
(not (bobp))))
nil
(or (bobp) (forward-char -1))
(dtrace-script-mode-beginning-of-function -1))
(setq first nil)
(dtrace-script-mode-end-of-current-function)
(skip-chars-forward " \t")
(if (looking-at "[#\n]")
(forward-line 1))
(<= (point) pos))))
(setq arg (1- arg)))
(while (< arg 0)
(let ((pos (point)))
(dtrace-script-mode-end-of-function)
(forward-line 1)
(if (>= (point) pos)
(if (progn (dtrace-script-mode-beginning-of-function 2) (not (bobp)))
(progn
(forward-list 1)
(skip-chars-forward " \t")
(if (looking-at "[#\n]")
(forward-line 1)))
(goto-char (point-min)))))
(setq arg (1+ arg)))))
(defun dtrace-script-mode-mark-function ()
"Put mark at end of D function, point at beginning."
(interactive)
(push-mark (point))
(dtrace-script-mode-end-of-function)
(push-mark (point))
(dtrace-script-mode-beginning-of-function))
;;;###autoload
(define-derived-mode dtrace-script-mode c-mode "DTrace"
"Major mode for editing DTrace code.
This is much like C mode. Its keymap inherits from C mode's and it has the same
variables for customizing indentation. It has its own abbrev table and its own
syntax table.
\\{dtrace-script-mode-map}
Turning on DTrace mode runs `dtrace-script-mode-hook'."
(setq imenu-generic-expression dtrace-script-mode-imenu-generic-expression)
(setq font-lock-defaults '(dtrace-script-mode-font-lock-keywords nil nil ((?_ . "w")))))
(provide 'dtrace-script-mode)
;;; dtrace-script-mode.el ends here