-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharc.el
461 lines (411 loc) · 17 KB
/
arc.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
;;; arc.el --- Arc editing mode
;; Copyright (C) 1986, 1987, 1988, 1997, 1998, 2001, 2002, 2003, 2004, 2005,
;; 2006, 2007, 2008 Free Software Foundation, Inc.
;; Author: Bill Rozas <[email protected]>
;; Adapted-by: Dave Love <[email protected]>
;; Adapted-by: Andrew MacDonald <[email protected]>
;; Adapted-by: Eric Hanchrow <[email protected]>
;; Keywords: languages, lisp
;; This file is not part of GNU Emacs.
;; GNU Emacs 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, or (at your option)
;; any later version.
;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; The major mode for editing Arc-type Lisp code, very similar to
;; the Lisp mode documented in the Emacs manual.
;;; Code:
(require 'lisp-mode)
(defvar arc-mode-syntax-table
(let ((st (make-syntax-table))
(i 0))
;; Default is atom-constituent.
(while (< i 256)
(modify-syntax-entry i "_ " st)
(setq i (1+ i)))
;; Word components.
(setq i ?0)
(while (<= i ?9)
(modify-syntax-entry i "w " st)
(setq i (1+ i)))
(setq i ?A)
(while (<= i ?Z)
(modify-syntax-entry i "w " st)
(setq i (1+ i)))
(setq i ?a)
(while (<= i ?z)
(modify-syntax-entry i "w " st)
(setq i (1+ i)))
;; Whitespace
(modify-syntax-entry ?\t " " st)
(modify-syntax-entry ?\n "> " st)
(modify-syntax-entry ?\f " " st)
(modify-syntax-entry ?\r " " st)
(modify-syntax-entry ?\s " " st)
;; These characters are delimiters but otherwise undefined.
;; Brackets and braces balance for editing convenience.
(modify-syntax-entry ?\[ "(] " st)
(modify-syntax-entry ?\] ")[ " st)
(modify-syntax-entry ?{ "(} " st)
(modify-syntax-entry ?} "){ " st)
(modify-syntax-entry ?\| "\" 23bn" st)
;; Guile allows #! ... !# comments.
;; But SRFI-22 defines the comment as #!...\n instead.
;; Also Guile says that the !# should be on a line of its own.
;; It's too difficult to get it right, for too little benefit.
;; (modify-syntax-entry ?! "_ 2" st)
;; Other atom delimiters
(modify-syntax-entry ?\( "() " st)
(modify-syntax-entry ?\) ")( " st)
;; It's used for single-line comments as well as for #;(...) sexp-comments.
(modify-syntax-entry ?\; "< 2 " st)
(modify-syntax-entry ?\" "\" " st)
(modify-syntax-entry ?' "' " st)
(modify-syntax-entry ?` "' " st)
;; Special characters
(modify-syntax-entry ?, "' " st)
(modify-syntax-entry ?@ "' " st)
(modify-syntax-entry ?# "' 14b" st)
(modify-syntax-entry ?\\ "\\ " st)
st))
(defvar arc-mode-abbrev-table nil)
(define-abbrev-table 'arc-mode-abbrev-table ())
(defvar arc-imenu-generic-expression
'((nil
"^(define\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)*\\s-+(?\\(\\sw+\\)" 4)
("Types"
"^(define-class\\s-+(?\\(\\sw+\\)" 1)
("Macros"
"^(\\(defmacro\\|define-macro\\|define-syntax\\)\\s-+(?\\(\\sw+\\)" 2))
"Imenu generic expression for Arc mode. See `imenu-generic-expression'.")
(defun arc-mode-variables ()
(set-syntax-table arc-mode-syntax-table)
(setq local-abbrev-table arc-mode-abbrev-table)
(make-local-variable 'paragraph-start)
(setq paragraph-start (concat "$\\|" page-delimiter))
(make-local-variable 'paragraph-separate)
(setq paragraph-separate paragraph-start)
(make-local-variable 'paragraph-ignore-fill-prefix)
(setq paragraph-ignore-fill-prefix t)
(make-local-variable 'fill-paragraph-function)
(setq fill-paragraph-function 'lisp-fill-paragraph)
;; Adaptive fill mode gets in the way of auto-fill,
;; and should make no difference for explicit fill
;; because lisp-fill-paragraph should do the job.
(make-local-variable 'adaptive-fill-mode)
(setq adaptive-fill-mode nil)
(make-local-variable 'normal-auto-fill-function)
(setq normal-auto-fill-function 'lisp-mode-auto-fill)
(make-local-variable 'indent-line-function)
(setq indent-line-function 'arc-indent-line)
(make-local-variable 'parse-sexp-ignore-comments)
(setq parse-sexp-ignore-comments t)
(make-local-variable 'outline-regexp)
(setq outline-regexp ";;; \\|(....")
(make-local-variable 'comment-start)
(setq comment-start ";")
(set (make-local-variable 'comment-add) 1)
(make-local-variable 'comment-start-skip)
;; Look within the line for a ; following an even number of backslashes
;; after either a non-backslash or the line beginning.
(setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+[ \t]*")
(set (make-local-variable 'font-lock-comment-start-skip) ";+ *")
(make-local-variable 'comment-column)
(setq comment-column 40)
(make-local-variable 'parse-sexp-ignore-comments)
(setq parse-sexp-ignore-comments t)
(make-local-variable 'lisp-indent-function)
(setq lisp-indent-function 'arc-indent-function)
(setq mode-line-process '("" arc-mode-line-process))
(set (make-local-variable 'imenu-case-fold-search) t)
(setq imenu-generic-expression arc-imenu-generic-expression)
(set (make-local-variable 'imenu-syntax-alist)
'(("+-*/.<>=?!$%_&~^:" . "w")))
(set (make-local-variable 'font-lock-defaults)
'((arc-font-lock-keywords
arc-font-lock-keywords-1 arc-font-lock-keywords-2)
nil t (("+-*/.<>=!?$%_&~^:" . "w") (?#. "w 14"))
beginning-of-defun
(font-lock-mark-block-function . mark-defun)
(font-lock-syntactic-face-function
. arc-font-lock-syntactic-face-function)
(parse-sexp-lookup-properties . t)
(font-lock-extra-managed-props syntax-table)))
(set (make-local-variable 'lisp-doc-string-elt-property)
'arc-doc-string-elt))
(defvar arc-mode-line-process "")
(defvar arc-mode-map
(let ((map (make-sparse-keymap "Arc")))
(set-keymap-parent map lisp-mode-shared-map)
(define-key map [menu-bar arc] (cons "Arc" map))
(define-key map [run-arc] '("Run Inferior Arc" . run-arc))
(define-key map [uncomment-region]
'("Uncomment Out Region" . (lambda (beg end)
(interactive "r")
(comment-region beg end '(4)))))
(define-key map [comment-region] '("Comment Out Region" . comment-region))
(define-key map [indent-region] '("Indent Region" . indent-region))
(define-key map [indent-line] '("Indent Line" . arc-indent-line))
(define-key map "\t" 'arc-indent-line)
(put 'comment-region 'menu-enable 'mark-active)
(put 'uncomment-region 'menu-enable 'mark-active)
(put 'indent-region 'menu-enable 'mark-active)
map)
"Keymap for Arc mode.
All commands in `lisp-mode-shared-map' are inherited by this map.")
;;;###autoload
(defun arc-mode ()
"Major mode for editing Arc code.
Editing commands are similar to those of `lisp-mode'.
Commands:
Delete converts tabs to spaces as it moves back.
Blank lines separate paragraphs. Semicolons start comments.
\\{arc-mode-map}
Entry to this mode calls the value of `arc-mode-hook'
if that value is non-nil."
(interactive)
(kill-all-local-variables)
(use-local-map arc-mode-map)
(setq major-mode 'arc-mode)
(setq mode-name "Arc")
(arc-mode-variables)
(run-mode-hooks 'arc-mode-hook))
(autoload 'run-arc "inferior-arc"
"Run an inferior Arc process, input and output via buffer `*arc*'.
If there is a process already running in `*arc*', switch to that buffer.
With argument, allows you to edit the command line (default is value
of `arc-program-name').
Runs the hook `inferior-arc-mode-hook' \(after the `comint-mode-hook'
is run).
\(Type \\[describe-mode] in the process buffer for a list of commands.)"
t)
(defgroup arc nil
"Editing Arc code."
:link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
:group 'lisp)
(defcustom arc-mode-hook nil
"Normal hook run when entering `arc-mode'.
See `run-hooks'."
:type 'hook
:group 'arc)
(defconst arc-font-lock-keywords-1
(eval-when-compile
(list
;;
;; Declarations.
(list
(concat "(" (regexp-opt
'("def" "mac" "defop" "defmemo" "defset" "deftem" "defcall" "redef" "set" "=")
t)
"\\>"
;; Any whitespace and declared object.
"[ \t]*(?"
"\\(\\sw+\\)?")
'(1 font-lock-keyword-face)
'(2 (cond ((match-beginning 2) font-lock-function-name-face)
((match-beginning 5) font-lock-variable-name-face)
(t font-lock-type-face))
nil t))
))
"Subdued expressions to highlight in Arc modes.")
(defconst arc-font-lock-keywords-2
(append arc-font-lock-keywords-1
(eval-when-compile
(list
;;
;; Control structures.
(cons
(concat
"(" (regexp-opt
'("fn" "rfn" "afn" "def" "mac" "set"
"defset" "defop" "deftem" "defmemo"
"when" "unless"
"do" "while" "until" "only" "each" "if" "=" "for" "repeat"
"aif" "aand" "awhen"
"case" "zap"
"let" "with" "withs"
"apply" "in"
;; Hannes Haug <[email protected]> wants:
"and" "or"
;; Stefan Monnier <[email protected]> says don't bother:
;;"quasiquote" "quote" "unquote" "unquote-splicing"
"map" "sort") t)
"\\>") 1)
)))
"Gaudy expressions to highlight in Arc modes.")
(defvar arc-font-lock-keywords arc-font-lock-keywords-1
"Default expressions to highlight in Arc modes.")
(defconst arc-sexp-comment-syntax-table
(let ((st (make-syntax-table arc-mode-syntax-table)))
(modify-syntax-entry ?\; "." st)
(modify-syntax-entry ?\n " " st)
(modify-syntax-entry ?# "'" st)
st))
(put 'lambda 'arc-doc-string-elt 2)
;; Docstring's pos in a `define' depends on whether it's a var or fun def.
(put 'define 'arc-doc-string-elt
(lambda ()
;; The function is called with point right after "define".
(forward-comment (point-max))
(if (eq (char-after) ?\() 2 0)))
(defun arc-font-lock-syntactic-face-function (state)
(when (and (null (nth 3 state))
(eq (char-after (nth 8 state)) ?#)
(eq (char-after (1+ (nth 8 state))) ?\;))
;; It's a sexp-comment. Tell parse-partial-sexp where it ends.
(save-excursion
(let ((pos (point))
(end
(condition-case err
(let ((parse-sexp-lookup-properties nil))
(goto-char (+ 2 (nth 8 state)))
;; FIXME: this doesn't handle the case where the sexp
;; itself contains a #; comment.
(forward-sexp 1)
(point))
(scan-error (nth 2 err)))))
(when (< pos (- end 2))
(put-text-property pos (- end 2)
'syntax-table arc-sexp-comment-syntax-table))
(put-text-property (- end 1) end 'syntax-table '(12)))))
;; Choose the face to use.
(lisp-font-lock-syntactic-face-function state))
;; Copied from lisp-indent-line,
;; because Arc doesn't care about how many comment chars you use.
(defun arc-indent-line (&optional whole-exp)
"Indent current line as Arc code.
With argument, indent any additional lines of the same expression
rigidly along with this one."
(interactive "P")
(let ((indent (calculate-lisp-indent)) shift-amt end
(pos (- (point-max) (point)))
(beg (progn (beginning-of-line) (point))))
(skip-chars-forward " \t")
(if (or (null indent) (looking-at "\\s<\\s<\\s<"))
;; Don't alter indentation of a ;;; comment line
;; or a line that starts in a string.
(goto-char (- (point-max) pos))
(if (listp indent) (setq indent (car indent)))
(setq shift-amt (- indent (current-column)))
(if (zerop shift-amt)
nil
(delete-region beg (point))
(indent-to indent)))
;; If initial point was within line's indentation,
;; position after the indentation. Else stay at same point in text.
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos)))
;; If desired, shift remaining lines of expression the same amount.
(and whole-exp (not (zerop shift-amt))
(save-excursion
(goto-char beg)
(forward-sexp 1)
(setq end (point))
(goto-char beg)
(forward-line 1)
(setq beg (point))
(> end beg))
(indent-code-rigidly beg end shift-amt))))
(defvar calculate-lisp-indent-last-sexp)
;; Copied from lisp-indent-function, but with gets of
;; arc-indent-{function,hok}.
(defun arc-indent-function (indent-point state)
(let ((normal-indent (current-column)))
(goto-char (1+ (elt state 1)))
(parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
(if (and (elt state 2)
(not (looking-at "\\sw\\|\\s_")))
;; car of form doesn't seem to be a symbol
(progn
(if (not (> (save-excursion (forward-line 1) (point))
calculate-lisp-indent-last-sexp))
(progn (goto-char calculate-lisp-indent-last-sexp)
(beginning-of-line)
(parse-partial-sexp (point)
calculate-lisp-indent-last-sexp 0 t)))
;; Indent under the list or under the first sexp on the same
;; line as calculate-lisp-indent-last-sexp. Note that first
;; thing on that line has to be complete sexp since we are
;; inside the innermost containing sexp.
(backward-prefix-chars)
(current-column))
(let ((function (buffer-substring (point)
(progn (forward-sexp 1) (point))))
method)
(setq method (or (get (intern-soft function) 'arc-indent-function)
(get (intern-soft function) 'arc-indent-hook)
0))
(cond ((or (eq method 'defun)
(and (null method)
(> (length function) 3)
(string-match "\\`def" function)))
(lisp-indent-defform state indent-point))
((integerp method)
(lisp-indent-specform method state
indent-point normal-indent))
(method
(funcall method state indent-point normal-indent)))))))
;;; Let is different in Arc
(defun would-be-symbol (string)
(not (string-equal (substring string 0 1) "(")))
(defun next-sexp-as-string ()
;; Assumes that it is protected by a save-excursion
(forward-sexp 1)
(let ((the-end (point)))
(backward-sexp 1)
(buffer-substring (point) the-end)))
;; This is correct but too slow.
;; The one below works almost always.
;;(defun arc-let-indent (state indent-point)
;; (if (would-be-symbol (next-sexp-as-string))
;; (arc-indent-specform 2 state indent-point)
;; (arc-indent-specform 1 state indent-point)))
(defun arc-let-indent (state indent-point normal-indent)
(skip-chars-forward " \t")
(if (looking-at "[-a-zA-Z0-9+*/?!@$%^&_:~]")
(lisp-indent-specform 2 state indent-point normal-indent)
(lisp-indent-specform 1 state indent-point normal-indent)))
;; (put 'begin 'arc-indent-function 0), say, causes begin to be indented
;; like def if the first form is placed on the next line, otherwise
;; it is indented like any other form (i.e. forms line up under first).
(put 'unless 'arc-indent-function 1)
(put 'case 'arc-indent-function 1)
(put 'with 'arc-indent-function 1)
(put 'withs 'arc-indent-function 1)
(put 'when 'arc-indent-function 1)
(put 'awhen 'arc-indent-function 1)
(put 'w/uniq 'arc-indent-function 1)
(put 'w/stdout 'arc-indent-function 1)
(put 'w/appendfile 'arc-indent-function 1)
(put 'w/stdin 'arc-indent-function 1)
(put 'w/infile 'arc-indent-function 2)
(put 'whilet 'arc-indent-function 2)
(put 'each 'arc-indent-function 2)
(put 'on 'arc-indent-function 2)
(put 'accum 'arc-indent-function 1)
(put 'def 'arc-indent-function 2)
(put 'mac 'arc-indent-function 2)
(put 'defset 'arc-indent-function 2)
(put 'defcall 'arc-indent-function 2)
(put 'redef 'arc-indent-function 2)
(put 'fn 'arc-indent-function 1)
(put 'afn 'arc-indent-function 1)
(put 'rfn 'arc-indent-function 2)
(put 'let 'arc-indent-function 'arc-let-indent)
;; By default, Emacs thinks .arc is an archive extension.
;; This makes it normal.
(dolist (coding auto-coding-alist)
(when (and (string-match "[Aa][Rr][Cc]\\\\|" (car coding))
(eq (cdr coding) 'no-conversion))
(setcar coding (replace-match "" nil nil (car coding)))))
(provide 'arc)
;; arch-tag: a8f06bc1-ad11-42d2-9e36-ce651df37a90
;;; arc.el ends here