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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
;; Copyright (c) 2007,2008 Paulo Cesar Pereira de Andrade
;;
;; Permission is hereby granted, free of charge, to any person obtaining a
;; copy of this software and associated documentation files (the "Software"),
;; to deal in the Software without restriction, including without limitation
;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice (including the next
;; paragraph) shall be included in all copies or substantial portions of the
;; Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;; DEALINGS IN THE SOFTWARE.
;;
;; Author: Paulo Cesar Pereira de Andrade
;;
;; Perl syntax and indentation mode
;; Based on the C/C++ and Lisp modes. Attempting to make simple
;; syntax/indentation rules, that should work correctly with most
;; perl code.
;; *cont-indent* is somewhat buggy, that if pressing C-A,Tab, will
;; not generate the same output as when normally typing the expression.
;; This is because the parser doesn't search for a matching ';', '{',
;; '[' or '(' to know where the expression starts. The C mode has the
;; same problem. Example:
;; a +
;; b; <-- if pressing C-A,Tab will align "b;" with "a +"
;; Maybe most of the code here, and some code in the C mode could be
;; merged to have a single "default mode" parser for languages that
;; basically only depend on { and } for indentation.
(require "syntax")
(require "indent")
(in-package "XEDIT")
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defsynprop *prop-string-escape*
"string-escape"
:font "*lucidatypewriter-bold-r*-12-*"
:foreground "RoyalBlue2"
:underline t)
(defsynprop *prop-string-keyword-bold*
"string-variable-bold"
:font "*lucidatypewriter-bold-r*-12-*"
:foreground "RoyalBlue4")
(defsynprop *prop-string-keyword*
"string-variable"
:font "*lucidatypewriter-medium-r*-12-*"
:foreground "RoyalBlue4")
(defsynprop *prop-constant-escape*
"constant-escape"
:font "*lucidatypewriter-medium-r*-12-*"
:foreground "VioletRed3"
:underline t)
(defsynprop *prop-regex*
"regex"
:font "*courier-medium-o*-12-*"
:foreground "black")
(defsynprop *prop-shell*
"shell"
:font "*lucidatypewriter-medium-r*-12-*"
:foreground "red3")
(defsynprop *prop-shell-escape*
"shell-escape"
:font "*lucidatypewriter-bold-r*-12-*"
:foreground "red3"
:underline t)
(defsynprop *prop-documentation*
"documentation"
:font "fixed"
:foreground "black"
:background "rgb:e/e/e"
)
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defsynoptions *perl-DEFAULT-style*
;; Positive number. Basic indentation
(:indentation . 4)
;; Boolean. Add one indentation level to continuations?
(:cont-indent . t)
;; Boolean. Move cursor to the indent column after pressing <Enter>?
(:newline-indent . t)
;; Boolean. Set to T if tabs shouldn't be used to fill indentation.
(:emulate-tabs . nil)
;; Boolean. Only calculate indentation after pressing <Enter>?
;; This may be useful if the parser does not always
;; do what the user expects...
(:only-newline-indent . nil)
;; Boolean. Remove extra spaces from previous line.
;; This should default to T when newline-indent is not NIL.
(:trim-blank-lines . t)
;; Boolean. If this hash-table entry is set, no indentation is done.
;; Useful to temporarily disable indentation.
(:disable-indent . nil))
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defvar *perl-mode-options* *perl-DEFAULT-style*)
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;; Parenthesis are usually not required, just distinguish as:
;; expression: code without an ending ';'
;; statement: code ending in a ';'
;; block: code enclosed in '{' and '}'
;; In Perl a simpler logic can be used, unlikely the C mode, as in
;; perl braces are mandatory
(defindent *perl-mode-indent* :main
;; this must be the first token
(indtoken "^\\s*" :indent
:code (or *offset* (setq *offset* (+ *ind-offset* *ind-length*))))
;; this may cause some other patterns to fail, due to matching single \'
(indtoken "(&?(\\w+)|&(\\w+)?)'\\w+" :expression)
;; special variables
(indtoken "\\$(\\d|^\\u|[][0-9!#$*()_@<>?/|,\"'])" :expression)
;; ignore comments
(indtoken "#.*$" nil)
;; treat regex as expressions to avoid confusing parser
(indtoken "m?/([^/]|\\\\/)+/\\w*" :expression)
(indtoken "m\\{[^}]+\\}\\w*" :expression)
(indtoken "m<[^>]+>\\w*" :expression)
(indtoken "(s|tr)/[^/]+/([^/]|\\\\/)*/\\w*" :expression)
(indtoken "//" :expression :nospec t)
;; fast resolve deferences to expressions
(indtoken "[$@%&*]?\\{\\$?\\S+\\}" :expression)
(indtoken "($%@*)?\\w+" :expression)
(indtoken ";" :semi :nospec t)
(indinit (braces 0))
(indtoken "{" :obrace :nospec t
:code (decf braces))
(indtoken "}" :cbrace :nospec t
:code (incf braces))
(indinit (parens&bracks 0))
(indtoken ")" :cparen :nospec t :code (incf parens&bracks))
(indtoken "(" :oparen :nospec t :code (decf parens&bracks))
(indtoken "]" :cbrack :nospec t :code (incf parens&bracks))
(indtoken "[" :obrack :nospec t :code (decf parens&bracks))
;; if in the same line, reduce now, this must be done because the
;; delimiters are identical
(indtoken "'([^\\']|\\\\.)*'" :expression)
(indtoken "\"([^\\\"]|\\\\.)*\"" :expression)
(indtoken "\"" :cstring1 :nospec t :begin :string1)
(indtoken "'" :cstring2 :nospec t :begin :string2)
;; This must be the last rule
(indtoken "\\s*$" :eol)
(indtable :string1
;; Ignore escaped characters
(indtoken "\\." nil)
;; Return to the toplevel when the start of the string is found
(indtoken "\"" :ostring1 :nospec t :switch -1))
(indtable :string2
(indtoken "\\." nil)
(indtoken "'" :ostring2 :nospec t :switch -1))
;; This avoids some problems with *cont-indent* adding an indentation
;; level to an expression after an empty line
(indreduce nil
t
((:indent :eol)))
;; Reduce to a single expression token
(indreduce :expression
t
((:indent :expression)
(:expression :eol)
(:expression :parens)
(:expression :bracks)
(:expression :expression)
;; multiline strings
(:ostring1 (not :ostring1) :cstring1)
(:ostring2 (not :ostring2) :cstring2)
;; parenthesis and brackets
(:oparen (not :oparen) :cparen)
(:obrack (not :obrack) :cbrack)))
;; Statements end in a semicollon
(indreduce :statement
t
((:semi)
(:indent :semi)
(:expression :statement)
(:statement :eol)
;; Doesn't necessarily end in a semicollon
(:expression :block)))
(indreduce :block
t
((:obrace (not :obrace) :cbrace)
(:block :eol)))
(indreduce :obrace
(< *ind-offset* *ind-start*)
((:indent :obrace))
(setq *indent* (offset-indentation (+ *ind-offset* *ind-length*) :resolve t))
(indent-macro-reject-left))
;; Try to do an smart indentation on open parenthesis and brackets
(indreduce :parens
t
((:oparen (not :oparen) :cparen))
(when (and
(< *ind-offset* *ind-start*)
(> (+ *ind-offset* *ind-length*) *ind-start*))
(setq *indent* (1+ (offset-indentation *ind-offset* :align t)))
(indent-macro-reject-left)))
(indreduce :bracks
t
((:obrack (not :obrack) :cbrack))
(when (and
(< *ind-offset* *ind-start*)
(> (+ *ind-offset* *ind-length*) *ind-start*))
(setq *indent* (1+ (offset-indentation *ind-offset* :align t)))
(indent-macro-reject-left)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Assuming previous lines have correct indentation, try to
;; fast resolve brace indentation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Line ended with an open brace
(indreduce :obrace
(< *ind-offset* *ind-start*)
((:expression :obrace))
(setq *indent* (offset-indentation *ind-offset* :resolve t))
(indent-macro-reject-left))
;; Line starts with an open brace
(indreduce nil
(< *ind-offset* *ind-start* (+ *ind-offset* *ind-length*))
;; Just set initial indentation
((:indent :obrace))
(setq
*indent* (- (offset-indentation *ind-offset* :resolve t) *base-indent*))
(indent-macro-reject-left))
(indresolve :statement
(when (< *ind-offset* *ind-start*)
(while (> braces 0)
(setq
*indent* (- *indent* *base-indent*)
braces (1- braces)))))
(indresolve :obrace
(and (< *ind-offset* *ind-start*)
(incf *indent* *base-indent*)))
(indresolve :cbrace
(decf *indent* *base-indent*))
(indresolve :expression
(and
*cont-indent*
(> *indent* 0)
(zerop parens&bracks)
(< *ind-offset* *ind-start*)
(> (+ *ind-offset* *ind-length*) *ind-start*)
(incf *indent* *base-indent*)))
(indresolve (:oparen :obrack)
(and (< *ind-offset* *ind-start*)
(setq *indent* (1+ (offset-indentation *ind-offset* :align t)))))
)
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defun perl-offset-indent (&aux char (point (point)))
;; Skip spaces forward
(while (member (setq char (char-after point)) indent-spaces)
(incf point))
(if (member char '(#\})) (1+ point) point))
(compile 'perl-offset-indent)
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defun perl-should-indent (options &aux char point start offset)
(when (hash-table-p options)
;; check if previous line has extra spaces
(and (gethash :trim-blank-lines options)
(indent-clear-empty-line))
;; indentation disabled?
(and (gethash :disable-indent options)
(return-from perl-should-indent))
(setq
point (point)
char (char-before point)
start (scan point :eol :left))
;; if at bol and should indent only when starting a line
(and (gethash :only-newline-indent options)
(return-from perl-should-indent (= point start)))
;; at the start of a line
(and (= point start)
(return-from perl-should-indent (gethash :newline-indent options)))
;; if first character
(and (= point (1+ start))
(return-from perl-should-indent t))
;; check if is the first non-blank character in a new line
(when (and
(gethash :cont-indent options)
(= point (scan point :eol :right))
(alphanumericp char))
(setq offset (1- point))
(while (and
(> offset start)
(member (char-before offset) indent-spaces))
(decf offset))
;; line has only one character with possible spaces before it
(and (<= offset start)
(return-from perl-should-indent t)))
;; if one of these was typed, should check indentation
(if (member char '(#\})) (return-from perl-should-indent t))
)
;; Should not indent
nil)
(compile 'perl-should-indent)
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defun perl-indent (syntax syntable)
(let*
((options (syntax-options syntax))
*base-indent*
*cont-indent*)
(or (perl-should-indent options) (return-from perl-indent))
(setq
*base-indent* (gethash :indentation options 4)
*cont-indent* (gethash :cont-indent options t))
(indent-macro
*perl-mode-indent*
(perl-offset-indent)
(gethash :emulate-tabs options))))
(compile 'perl-indent)
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;; some example macros to easily add new patterns for strings and possibly
;; regex or other patterns
(defmacro perl-q-string-token (token)
`(syntoken (string-concat "\\<q(q|w)?\\s*\\" ,token)
:icase t :contained t :begin
(intern (string-concat "string" ,token) 'keyword)))
(defmacro perl-q-string-table (start end)
`(syntable (intern (string-concat "string" ,start) 'keyword)
*prop-string* #'default-indent
(syntoken ,end :nospec t :switch -1)
(synaugment :inside-string)))
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defsyntax *perl-mode* :main nil #'perl-indent *perl-mode-options*
;; keywords
(syntoken
(string-concat
"\\<("
"and|for|foreach|gt|if|else|elsif|eq|goto|le|lt|last|ne|"
"neg|next|not|or|return|shift|sub|unless|unshift|until|while"
")\\>")
:property *prop-keyword*)
;; pseudo keywords
(syntoken
(string-concat
"\\<("
"BEGIN|END|bless|blessed|defined|delete|eval|local|my|our|"
"package|require|undef|use"
")\\>")
:property *prop-preprocessor*)
;; this may cause some other patterns to fail, due to matching single \'
(syntoken "(&?(\\w+)|&(\\w+)?)'\\w+" :property *prop-preprocessor*)
;; numbers
(syntoken
(string-concat
"\\<("
;; Integers
"(\\d+|0x\\x+)|"
;; Floats
"\\d+\\.?\\d*(e[+-]?\\d+)?"
")\\>")
:icase t
:property *prop-number*)
;; special variables
(syntoken "\\$(\\d|^\\u|[][0-9!#$*()_@<>?/|,\"'])" :property *prop-keyword*)
;; also match variables
(syntable :inside-string nil nil
;; escaped characters
;; XXX This pattern was matching the empty string and entering an
;; infinite loop in code like:
#|
---%<---
" <-- *** if an backslash is added it fails. Inverting
a"; *** the pattern fixed the problem, but was the wrong
---%<--- *** solution. Note that C-G stops the interpreter, and
*** special care must be taken with patterns matching
*** empty strings.
|#
(syntoken "\\\\\\d{3}|\\\\." :property *prop-string-escape*)
(syntoken "(\\{\\$|\\$\\{)" :property *prop-string-keyword-bold* :begin :string-varbrace)
(syntoken "[$@]" :property *prop-string-keyword-bold* :begin :string-variable)
(syntoken "\\$(\\d|^\\u|[][0-9!#$*()_@<>?/|,\"'])" :property *prop-string-keyword-bold*))
;; variables insided strings
(syntable :string-variable *prop-string-keyword* nil
(syntoken "\\w+" :switch -1))
(syntable :string-varbrace *prop-string-keyword* nil
(syntoken "}"
:nospec t
:property *prop-string-keyword-bold*
:switch -1)
(synaugment :inside-string))
;; comments
(syntoken "#.*$" :property *prop-comment*)
;; regex
(syntoken "(\\<m)?/([^/]|\\\\/)+/\\w*" :property *prop-regex*)
(syntoken "\\<m\\{[^}]+\\}\\w*" :property *prop-regex*)
(syntoken "\\<m<[^>]+>\\w*" :property *prop-regex*)
(syntoken "\\<(s|tr)/[^/]+/([^/]|\\\\/)*/\\w*":property *prop-regex*)
;; just to avoid confusing the parser on something like split //, ...
(syntoken "//" :nospec t :property *prop-regex*)
;; strings
(syntoken "\"" :nospec t :contained t :begin :string)
(syntable :string *prop-string* #'default-indent
(syntoken "\"" :nospec t :switch -1)
(synaugment :inside-string))
;; more strings
(perl-q-string-token "{")
(perl-q-string-table "{" "}")
(perl-q-string-token "[")
(perl-q-string-table "[" "]")
(perl-q-string-token "(")
(perl-q-string-table "(" ")")
(perl-q-string-token "/")
(perl-q-string-table "/" "/")
;; yet more strings
(syntoken "'" :nospec t :contained t :begin :constant)
(syntable :constant *prop-constant* #'default-indent
(syntoken "'" :nospec t :switch -1)
(syntoken "\\\\." :property *prop-string-escape*))
;; shell commands
(syntoken "`" :nospec t :contained t :begin :shell)
(syntable :shell *prop-shell* #'default-indent
(syntoken "`" :nospec t :switch -1)
(synaugment :inside-string))
;; punctuation
(syntoken "[][$@%(){}/*+:;=<>,&!|^~\\.?-]" :property *prop-punctuation*)
(syntoken "\\<x\\>" :property *prop-punctuation*)
;; primitive faked heredoc support, doesn't match the proper string, just
;; expects an uppercase identifier in a single line
(syntoken "<<\"[A-Z][A-Z0-9_]+\"" :property *prop-string* :begin :heredoc)
(syntoken "<<'[A-Z][A-Z0-9_]+'" :property *prop-constant* :begin :heredoc)
(syntoken "<<[A-Z][A-Z0-9_]+" :property *prop-preprocessor* :begin :heredoc)
(syntable :heredoc *prop-documentation* #'default-indent
(syntoken "^[A-Z][A-Z0-9_]+$" :switch -1))
(syntoken "^=(pod|item|over|head\\d)\\>.*$" :property *prop-documentation* :begin :info)
(syntable :info *prop-documentation* nil
(syntoken "^=cut\\>.*$" :switch -1)
(syntoken "^.*$"))
(syntoken "^(__END__|__DATA__)$" :property *prop-documentation*
:begin :documentation)
(syntoken "__\\u+__" :property *prop-preprocessor*)
(syntable :documentation *prop-documentation* nil
(syntoken "^.*$"))
)
|