-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetos.el
355 lines (302 loc) · 12.5 KB
/
etos.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
;;; -*- Mode:Emacs-Lisp -*-
;;; etos.el --- Run ETOS in an [X]Emacs shell buffer
;; Copyright (C) 1998-1999 Universite de Montreal, All Rights Reserved.
;; Authors: Marc Feeley <[email protected]>
;; To use this package, make sure this file is accessible from your
;; load-path and that the following line is in your ".emacs" file:
;;
;; (require 'etos)
;;
;; You can then start a shell with "M-x shell" and run the ETOS compiler
;; from that shell. To pinpoint the source code of an error, place the
;; cursor on the error message an type "\M-;" (escape semicolon).
;------------------------------------------------------------------------------
;; User overridable parameters.
(defvar etos-pinpoint-command "\M-;"
"Emacs keybinding for pinpointing the Erlang source code of an error.")
(defvar etos-highlight-source-color "gold"
"Color of the overlay for highlighting Erlang source code.")
(defvar etos-highlight-error-color "gray"
"Color of the overlay for highlighting error messages.")
(defvar etos-highlight-source-face
(let ((face 'etos-highlight-source-face))
(condition-case nil
(progn
(make-face face)
(if (x-display-color-p)
(set-face-background face etos-highlight-source-color)
(set-face-underline-p face t)))
(error (setq face nil)))
face)
"Face of overlay for highlighting Erlang source code.")
(defvar etos-highlight-error-face
(let ((face 'etos-highlight-error-face))
(condition-case nil
(progn
(make-face face)
(if (x-display-color-p)
(set-face-background face etos-highlight-error-color)
(set-face-underline-p face t)))
(error (setq face nil)))
face)
"Face of overlay for highlighting error messages.")
(defvar etos-new-window-height 8
"Height of a window opened to highlight Erlang source code.")
(defvar etos-move-to-highlighted (not etos-highlight-source-face)
"Flag to move to window opened to highlight Erlang source code.")
(setq etos-location-regexp-alist
'(("^\\([^ :]+\\):\\([0-9]+\\):" -1 2)
("\\(\\\"\\(\\\\\\\\\\|\\\\\"\\|[^\\\"\n]\\)+\\\"\\)@\\([0-9]+\\)\\.\\([0-9]+\\)[^-0-9]" 1 3 4)
("\\(\\\"\\(\\\\\\\\\\|\\\\\"\\|[^\\\"\n]\\)+\\\"\\)@\\([0-9]+\\)\\.\\([0-9]+\\)-\\([0-9]+\\)\\.\\([0-9]+\\):" 1 3 4 5 6))
; "Regular expressions to detect location information in error messages"
)
;------------------------------------------------------------------------------
;; Portable functions for FSF Emacs and Xemacs.
(defun window-top-edge (window)
(if (fboundp 'window-edges)
(car (cdr (window-edges window)))
(car (cdr (window-pixel-edges window)))))
;; Xemacs calls its overlays "extents", so we have to use them to emulate
;; overlays on Xemacs. Some versions of Xemacs have the portability package
;; "overlays.el" for this, so we could simply do:
;;
;; (condition-case nil ; load "overlay.el" if we have it
;; (require 'overlay)
;; (error nil))
;;
;; Unfortunately some versions of Xemacs don't have this package so
;; we explicitly define an interface to extents.
(if (not (fboundp 'make-overlay))
(defun make-overlay (start end)
(make-extent start end)))
(if (not (fboundp 'overlay-put))
(defun overlay-put (overlay prop val)
(set-extent-property overlay prop val)))
(if (not (fboundp 'move-overlay))
(defun move-overlay (overlay start end buffer)
(set-extent-endpoints overlay start end buffer)))
;------------------------------------------------------------------------------
;; Procedures to intercept and process the location information output
;; by Etos.
(defun etos-pinpoint-error ()
(interactive)
(let ((locat
(etos-extract-location (etos-get-current-line))))
(if locat
(etos-highlight-location locat))))
(defun etos-get-current-line ()
(save-excursion
(beginning-of-line)
(let ((b (point)))
(end-of-line)
(buffer-substring b (point)))))
(defun etos-extract-location (str)
(let ((location nil)
(alist etos-location-regexp-alist))
(while (and (not location) (not (null alist)))
(let* ((regexp (car alist))
(x (string-match (car regexp) str)))
(if x
(let* ((pos1 (nth 1 regexp))
(pos2 (nth 2 regexp))
(name (if (< pos1 0)
(substring str
(match-beginning (- pos1))
(match-end (- pos1)))
(read (substring str
(match-beginning pos1)
(match-end pos1)))))
(line1 (read (substring str
(match-beginning pos2)
(match-end pos2)))))
(if (not (null (cdr (cdr (cdr regexp)))))
(let* ((pos3 (nth 3 regexp))
(column1 (read (substring str
(match-beginning pos3)
(match-end pos3)))))
(if (not (null (cdr (cdr (cdr (cdr regexp))))))
(let* ((pos4 (nth 4 regexp))
(pos5 (nth 5 regexp))
(line2 (read (substring str
(match-beginning pos4)
(match-end pos4))))
(column2 (read (substring str
(match-beginning pos5)
(match-end pos5)))))
(setq location
(list name line1 column1 line2 column2)))
(setq location
(list name line1 column1 '() '()))))
(setq location
(list name line1 '() '() '())))))
(setq alist (cdr alist))))
location))
(defun etos-highlight-location (locat)
(let ((name (car locat))
(line1 (car (cdr locat)))
(column1 (car (cdr (cdr locat))))
(line2 (car (cdr (cdr (cdr locat)))))
(column2 (car (cdr (cdr (cdr (cdr locat)))))))
(cond ((stringp name)
(let ((buffer (find-file-noselect name)))
(if buffer
(if column1
(let ((p1 (etos-file-pos-to-point buffer
line1
column1)))
(and p1
(let ((p2 (etos-file-pos-to-point buffer
line2
column2)))
(etos-highlight-source buffer p1 p2))))
(let ((p12 (etos-file-line-to-points buffer line1)))
(and p12
(etos-highlight-source buffer
(car p12)
(cdr p12)))))))))))
(defun etos-file-pos-to-point (buffer line column)
(and line
(save-excursion
(set-buffer buffer)
(goto-line line)
(forward-char (- column 1))
(point))))
(defun etos-file-line-to-points (buffer line)
(and line
(save-excursion
(set-buffer buffer)
(goto-line line)
(beginning-of-line)
(let ((b (point)))
(end-of-line)
(cons b (point))))))
(defun etos-highlight-source (location-buffer pos1 pos2)
"Highlight the source code at a specific location in a buffer.
The location buffer is the one that contains the region to highlight
and \"pos1\" points to the first character of the region and \"pos2\"
just past the region. If the location buffer is not visible then we
must display it in a window. We also have to make sure the
highlighted region is visible, which may require the window to be
scrolled.
Our approach is simple: if the location buffer is not visible then we
split the selected window in 2 and use the bottom window to display
the location buffer. Before we do the split, we enlarge the window if
it is too small."
(let* ((location-windows
(etos-windows-displaying-buffer location-buffer))
(initially-current-buffer
(current-buffer))
(initially-selected-window
(selected-window)))
; "location-windows" is the list of windows containing
; the location buffer.
(if (null location-windows)
(let* ((window-to-split
initially-selected-window)
(height
(window-height window-to-split)))
(select-window window-to-split)
(if (< height (* 2 etos-new-window-height))
(enlarge-window
(- (* 2 etos-new-window-height)
height)))
(let ((bottom-window
(split-window
window-to-split
(- (window-height window-to-split)
etos-new-window-height))))
(select-window bottom-window)
(switch-to-buffer location-buffer)))
(select-window (car (reverse location-windows))))
; Highlight the region in the location buffer.
(save-excursion
(set-buffer (window-buffer (selected-window)))
(goto-char pos1)
(if (not (pos-visible-in-window-p))
(recenter (- (/ (window-height) 2) 1)))
(etos-highlight-source-region
location-buffer
pos1
(or pos2
(progn
(condition-case nil
(forward-sexp)
(error ; if forward-sexp fails with this condition name
(forward-char 1)))
(point))))
(etos-highlight-error-region
initially-current-buffer)
(goto-char pos1))
(if (not (eq initially-selected-window (selected-window)))
(progn
(goto-char pos1)
(if (not etos-move-to-highlighted)
(select-window initially-selected-window))))))
(defun etos-windows-displaying-buffer (buffer)
(let ((windows '()))
(walk-windows (function
(lambda (w)
(if (eq buffer (window-buffer w))
(setq windows (cons w windows)))))
t
'visible)
(sort windows
(function
(lambda (w1 w2)
(< (window-top-edge w1)
(window-top-edge w2)))))))
(defvar etos-highlight-source-overlay
(let ((ovl (make-overlay (point-min) (point-min))))
(overlay-put ovl 'face etos-highlight-source-face)
ovl)
"Overlay for highlighting Erlang source code.")
(defvar etos-highlight-error-overlay
(let ((ovl (make-overlay (point-min) (point-min))))
(overlay-put ovl 'face etos-highlight-error-face)
ovl)
"Overlay for highlighting error message.")
(defun etos-highlight-source-region (buffer start end)
(if etos-highlight-source-overlay
(move-overlay etos-highlight-source-overlay start end buffer)))
(defun etos-highlight-error-region (buffer)
(if etos-highlight-error-overlay
(save-excursion
(set-buffer buffer)
(beginning-of-line)
(let ((b (point)))
(end-of-line)
(let ((e (point)))
(move-overlay etos-highlight-error-overlay b e buffer))))))
;------------------------------------------------------------------------------
(defun etos-extend-mode-map (map)
(define-key map etos-pinpoint-command 'etos-pinpoint-error))
; Redefine next-line and previous-line so that they call etos-pinpoint-error.
(defun next-line (arg)
(interactive "p")
(if (and next-line-add-newlines (= arg 1))
(let ((opoint (point)))
(end-of-line)
(if (eobp)
(newline 1)
(goto-char opoint)
(line-move arg)))
(if (interactive-p)
(condition-case nil
(line-move arg)
((beginning-of-buffer end-of-buffer) (ding)))
(line-move arg)))
(etos-pinpoint-error)
nil)
(defun previous-line (arg)
(interactive "p")
(if (interactive-p)
(condition-case nil
(line-move (- arg))
((beginning-of-buffer end-of-buffer) (ding)))
(line-move (- arg)))
(etos-pinpoint-error)
nil)
(eval-after-load "shell"
'(etos-extend-mode-map shell-mode-map))
(provide 'etos)
;------------------------------------------------------------------------------