forked from fxbois/web-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-mode.el
executable file
·3360 lines (2873 loc) · 110 KB
/
web-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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; web-mode.el --- major mode for editing HTML templates
;; Copyright (C) 2011, 2012, 2013 François-Xavier Bois
;; =========================================================================
;; This work is sponsored by KerniX : Digital Agency (Web & Mobile) in Paris
;; =========================================================================
;; Version: 4.0
;; Author: François-Xavier Bois <fxbois AT Google Mail Service>
;; Maintainer: François-Xavier Bois
;; Created: July 2011
;; Keywords: Web Template HTML PHP JavaScript CSS Js
;; JSP ASP ERB Twig Jinja Mustache
;; FreeMarker Django Velocity Cheetah Smarty
;; URL: http://github.com/fxbois/web-mode
;; http://web-mode.org
;; This file is not part of Emacs
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(eval-when-compile (require 'cl))
(defgroup web-mode nil
"Major mode for editing web templates.
`web-mode' is compatible with many template engines: php, jsp, aspx, erb, django/twig/jinja2, CTemplate/Mustache/Hapax.
HTML files can embed various kinds of blocks: javascript / css / code."
:version "4.0"
:group 'languages)
(defgroup web-mode-faces nil
"Faces for syntax highlighting."
:group 'web-mode
:group 'faces)
(defconst web-mode-debug nil
"t if in debug mode.")
(defcustom web-mode-disable-css-colorization (not (display-graphic-p))
"In a style block, do not set background according to the color: #xxx, rgb(x,x,x)."
:type 'boolean
:group 'web-mode)
(defcustom web-mode-indent-with-tabs nil
"Set to t to insert tab characters.")
(defcustom web-mode-indent-level 4
"Indent level global to the whole of web-mode.")
(defcustom web-mode-disable-autocompletion (not (display-graphic-p))
"Disable autocompletion."
:type 'bool
:group 'web-mode)
(defcustom web-mode-tag-autocomplete-style 1
"Tag autocomplete style:
0=no autocomplete
1=autocomplete with </
2=autocomplete with > and </."
:type 'integer
:group 'web-mode)
(defface web-mode-preprocessor-face
'((t :inherit font-lock-preprocessor-face))
"Face for preprocessor."
:group 'web-mode-faces)
(defface web-mode-builtin-face
'((t :inherit font-lock-builtin-face))
"Face for builtins."
:group 'web-mode-faces)
(defface web-mode-doctype-face
'((t :foreground "Grey"))
"Face for HTML doctype."
:group 'web-mode-faces)
(defface web-mode-html-tag-face
'((t :foreground "Snow4"))
"Face for HTML tags."
:group 'web-mode-faces)
(defface web-mode-html-attr-name-face
'((t :foreground "Snow3"))
"Face for HTML attribute names."
:group 'web-mode-faces)
(defface web-mode-html-attr-value-face
'((t :inherit font-lock-string-face))
"Face for HTML attribute values."
:group 'web-mode-faces)
(defface web-mode-css-rule-face
'((t :foreground "orchid3"))
"Face for CSS rules."
:group 'web-mode-faces)
(defface web-mode-css-pseudo-class-face
'((t :foreground "plum2"))
"Face for CSS pseudo-classes."
:group 'web-mode-faces)
(defface web-mode-css-at-rule-face
'((t :inherit font-lock-builtin-face))
"Face for CSS at-rules."
:group 'web-mode-faces)
(defface web-mode-css-prop-face
'((t :foreground "Pink3"))
"Face for CSS props."
:group 'web-mode-faces)
(defface web-mode-variable-name-face
'((t :inherit font-lock-variable-name-face))
"Face for variable names."
:group 'web-mode-faces)
(defface web-mode-function-name-face
'((t :inherit font-lock-function-name-face))
"Face for function names."
:group 'web-mode-faces)
(defface web-mode-string-face
'((t :inherit font-lock-string-face))
"Face for strings."
:group 'web-mode-faces)
(defface web-mode-comment-face
'((t :inherit font-lock-comment-face))
"Face for comments."
:group 'web-mode-faces)
(defface web-mode-constant-face
'((t :inherit font-lock-constant-face))
"Face for language constants."
:group 'web-mode-faces)
(defface web-mode-type-face
'((t :inherit font-lock-type-face))
"Face for language types."
:group 'web-mode-faces)
(defface web-mode-keyword-face
'((t :inherit font-lock-keyword-face))
"Face for language keywords."
:group 'web-mode-faces)
(defface web-mode-param-name-face
'((t :foreground "Snow3"))
"Face for server attribute names."
:group 'web-mode-faces)
(defconst web-mode-void-elements
'("area" "base" "br" "col" "command" "embed" "hr" "img" "input" "keygen"
"link" "meta" "param" "source" "track" "wbr"
"tmpl_var" "h:inputtext" "jsp:usebean"
"#include" "#assign" "#import" "#else")
"Void (self-closing) tags.")
(defconst web-mode-text-properties
'(client-tag-name nil client-tag-type nil server-tag-name nil server-tag-type nil client-language nil server-engine nil client-side nil server-side nil client-type nil server-type nil client-pos nil server-pos nil face nil)
"Text properties used for fontification and indentation.")
(defvar web-mode-expand-first-pos nil
"First mark pos.")
(defvar web-mode-expand-last-type ""
"Last mark type.")
(defface web-mode-folded-face
'((t :underline t))
"Overlay face for folded."
:group 'web-mode-faces)
(defvar web-mode-tag-regexp "<\\(/?[[:alpha:]@#][[:alnum:]:_]*\\)"
"Regular expression for HTML/XML tag.")
(defvar web-mode-server-blocks-regexp nil
"Regular expression for identifying server blocks.")
(defvar web-mode-engine nil
"Template engine")
(defvar web-mode-engine-families
'(("django" . ("twig" "jinja" "jinja2"))
("erb" . ("eruby" "ember" "erubis"))
("velocity" . ("cheetah"))
("ctemplate" . ("mustache" "hapax" "ngtemplate"))
)
"Engine name aliases")
(defvar web-mode-file-type ""
"Buffer file type.")
(defvar web-mode-comments-invisible nil
"Comments visbility.")
(defvar web-mode-is-narrowed nil
"Buffer has been narrowed.")
(defvar web-mode-block-beg nil
"Beg of current block.")
(defvar web-mode-hook nil
"List of functions to be executed with web-mode.")
(defvar web-mode-buffer-highlighted nil
"Is buffer highlighted.")
(defvar web-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?_ "w" table)
table)
"Syntax table in use in web-mode buffers.")
(defvar web-mode-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap (kbd "C-c C-;") 'web-mode-comment-uncomment)
(define-key keymap (kbd "C-;") 'web-mode-comment-uncomment)
(define-key keymap (kbd "C-c C-b") 'web-mode-beginning-of-element)
(define-key keymap (kbd "C-c C-d") 'web-mode-delete-element)
(define-key keymap (kbd "C-c C-e") 'web-mode-select-element-content)
(define-key keymap (kbd "C-c C-f") 'web-mode-toggle-folding)
(define-key keymap (kbd "C-c C-i") 'web-mode-insert)
(define-key keymap (kbd "C-c C-j") 'web-mode-duplicate-element)
(define-key keymap (kbd "C-c C-l") 'web-mode-previous-element)
(define-key keymap (kbd "C-c C-m") 'web-mode-mark-and-expand)
(define-key keymap (kbd "C-c C-n") 'web-mode-match-tag)
(define-key keymap (kbd "C-c C-p") 'web-mode-parent-element)
(define-key keymap (kbd "C-c C-r") 'web-mode-rename-element)
(define-key keymap (kbd "C-c C-s") 'web-mode-select-element)
(define-key keymap (kbd "RET") 'web-mode-newline-and-indent)
(define-key keymap [backspace] 'web-mode-backspace-or-unindent)
keymap)
"Keymap for `web-mode'.")
(eval-and-compile
(defalias 'web-mode-prog-mode (if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
(if (fboundp 'with-silent-modifications)
(defalias 'web-mode-with-silent-modifications 'with-silent-modifications)
(defmacro web-mode-with-silent-modifications (&rest body)
"Compatibility with pre 23.3"
`(let ((old-modified-p (buffer-modified-p))
(inhibit-modification-hooks t)
(buffer-undo-list t))
(unwind-protect
,@body
(set-buffer-modified-p old-modified-p)))))
)
;;;###autoload
(define-derived-mode web-mode web-mode-prog-mode "Web"
"Major mode for editing mixed HTML Templates."
(let ((bfn (buffer-file-name)) elt l i)
;; (make-local-variable 'font-lock-extend-region-functions)
(make-local-variable 'font-lock-keywords)
;; (make-local-variable 'font-lock-keywords-case-fold-search)
;; (make-local-variable 'font-lock-keywords-only)
;; (make-local-variable 'font-lock-lock-defaults)
(make-local-variable 'font-lock-multiline)
(make-local-variable 'indent-line-function)
(make-local-variable 'after-change-functions)
(make-local-variable 'font-lock-fontify-buffer-function)
(make-local-variable 'font-lock-unfontify-buffer-function)
(make-local-variable 'require-final-newline)
(make-local-variable 'web-mode-block-beg)
(make-local-variable 'web-mode-buffer-highlighted)
(make-local-variable 'web-mode-disable-autocompletion)
(make-local-variable 'web-mode-disable-css-colorization)
(make-local-variable 'web-mode-engine)
(make-local-variable 'web-mode-engine-families)
(make-local-variable 'web-mode-expand-first-pos)
(make-local-variable 'web-mode-expand-last-type)
(make-local-variable 'web-mode-file-type)
(make-local-variable 'web-mode-is-narrowed)
(make-local-variable 'web-mode-server-blocks-regexp)
;; (make-local-variable 'font-lock-extend-after-change-region-function)
;; (setq font-lock-extend-after-change-region-function 'web-mode-extend-after-change-region)
(cond
((string-match-p "\\.xml\\'" bfn)
(setq web-mode-file-type "xml")
)
((string-match-p "\\.css\\'" bfn)
(setq web-mode-file-type "css")
)
(t
(setq web-mode-file-type "html"))
)
(when (boundp 'web-mode-engines-alist)
(setq i 0
l (length web-mode-engines-alist))
(while (< i l)
(setq elt (nth i web-mode-engines-alist)
i (1+ i))
(when (string-match-p (car elt) bfn)
(setq web-mode-engine (cdr elt)))
);while
);when
(when (null web-mode-engine)
(cond
((or (string-match-p "\\.erb\\'" bfn)
(string-match-p "\\.rhtml\\'" bfn))
(setq web-mode-engine "erb"))
((string-match-p "\\.tpl\\'" bfn)
(setq web-mode-engine "smarty"))
((string-match-p "\\.jsp\\'" bfn)
(setq web-mode-engine "jsp"))
((or (string-match-p "\\.php\\'" bfn)
(string-match-p "\\.ctp\\'" bfn))
(setq web-mode-engine "php"))
((string-match-p "\\.as[cp]x?\\'" bfn)
(setq web-mode-engine "asp"))
((or (string-match-p "\\.djhtml\\'" bfn)
(string-match-p "\\.tmpl\\'" bfn)
(string-match-p "\\.twig\\'" bfn))
(setq web-mode-engine "django"))
((string-match-p "\\.ftl\\'" bfn)
(setq web-mode-engine "freemarker"))
((string-match-p "\\.mustache\\'" bfn)
(setq web-mode-engine "mustache"))
((or (string-match-p "\\.vsl\\'" bfn)
(string-match-p "\\.vm\\'" bfn))
(setq web-mode-engine "velocity"))
);cond
);when
(when (not (null web-mode-engine))
(setq i 0
l (length web-mode-engine-families))
(while (< i l)
(setq elt (nth i web-mode-engine-families)
i (1+ i))
;; (message "%S %S" web-mode-engine (cdr elt))
(when (member web-mode-engine (cdr elt))
(setq web-mode-engine (car elt)))
);while
);when
(cond
((string= web-mode-engine "php")
(setq web-mode-server-blocks-regexp "<\\?"))
((string= web-mode-engine "velocity")
(setq web-mode-server-blocks-regexp "^[ \t]*#[[:alpha:]#*]\\|$[[:alpha:]!{]"))
((string= web-mode-engine "django")
(setq web-mode-server-blocks-regexp "{[#{%]"))
((string= web-mode-engine "ctemplate")
(setq web-mode-server-blocks-regexp "{{."))
((string= web-mode-engine "freemarker")
(setq web-mode-server-blocks-regexp "[<[]/?[#@][-]?\\|${"))
((string= web-mode-engine "smarty")
;; (setq web-mode-server-blocks-regexp "{[^ ]"))
(setq web-mode-server-blocks-regexp "{[[:alpha:]#$/*\"]"))
((string= web-mode-engine "asp")
(setq web-mode-server-blocks-regexp "<%."))
;; (setq web-mode-server-blocks-regexp "{[[:alpha:]*$#]"))
(t
(setq web-mode-server-blocks-regexp "<\\?\\|<%[#-!@]?\\|[<[]/?[#@][-]?\\|[$#]{\\|{[#{%]\\|^%."))
)
;; (message "engine=%S regexp=%S" web-mode-engine web-mode-server-blocks-regexp)
(setq font-lock-fontify-buffer-function 'web-mode-scan-buffer
indent-line-function 'web-mode-indent-line
;; font-lock-keywords-only t
font-lock-unfontify-buffer-function 'web-mode-scan-buffer
require-final-newline nil)
(remove-hook 'after-change-functions 'font-lock-after-change-function t)
(add-hook 'after-change-functions 'web-mode-on-after-change t t)
(web-mode-scan-buffer)
))
(defun web-mode-previous-usable-line-level ()
"Return the indentation of the previous non-blank line."
(interactive)
(save-excursion
(let ((continue t)
(line "")
(pos (point)))
(beginning-of-line)
(while (and continue
(not (bobp))
(forward-line -1))
(setq line (web-mode-trim (buffer-substring (point) (line-end-position))))
(when (not (string= line "")) (setq continue nil)))
(if (string= line "")
(progn
(goto-char pos)
nil)
(unless (null line)
(current-indentation))))))
(defun web-mode-insert-indent (level)
"Inserts n tabs or spaces to indent the line."
(if web-mode-indent-with-tabs
(insert-char ?\t (/ level tab-width))
(insert-char ?\ level)))
(defun web-mode-do-indent (&optional level)
"Determines how to indent the current line, and does it."
(if (null level)
(web-mode-insert-indent web-mode-indent-level)
(web-mode-insert-indent level)))
(defun web-mode-do-unindent (&optional max-del)
"Unindents"
(let ((max-delete (if max-del max-del 200)))
(if web-mode-indent-with-tabs
(delete-backward-char)
(delete-backward-char (min web-mode-indent-level max-delete)))))
(defun web-mode-indent-line (&optional initial-indent)
"inserts a bunch of spaces in front of a line."
(interactive)
(let ((inhibit-modification-hooks t)
(current-indent (- (point) (point-at-bol)))
(previous-indent (web-mode-previous-usable-line-level)))
(if (and (>= current-indent previous-indent)
(not initial-indent))
(web-mode-do-indent)
(web-mode-do-indent (- previous-indent current-indent)))))
(defun web-mode-newline-and-indent ()
"Inserts a new line and indents the next line."
(interactive)
(progn
(newline)
(web-mode-indent-line t)))
(defun web-mode-backspace-or-unindent ()
"Deletes a character or unindents depending on
cursor's position in the line."
(interactive)
(let ((current-indent (current-indentation))
(point-position (- (point) (point-at-bol))))
(if (and (> point-position 0)
(<= point-position current-indent))
(web-mode-do-unindent point-position)
(delete-backward-char 1))))
(defun web-mode-scan-buffer ()
"Scan entine buffer."
(interactive)
(web-mode-scan-region (point-min) (point-max)))
(defun web-mode-scan-region (beg end &optional verbose)
"Identify code blocks (client/server) and syntactic symbols (strings/comments)."
(interactive)
;; (message "scanning buffer from %d to %d" beg end)
(web-mode-with-silent-modifications
(save-excursion
(save-match-data
(let ((inhibit-modification-hooks t)
(inhibit-point-motion-hooks t)
(inhibit-quit t))
(setq beg (if web-mode-is-narrowed 1 beg))
(remove-text-properties beg end web-mode-text-properties)
(cond
((string= web-mode-file-type "css")
(web-mode-scan-client-block beg end "style"))
(t
(web-mode-mark-server-boundaries beg end)
(web-mode-scan-client beg end)
(web-mode-scan-server beg end)
)
)
)))))
(defun web-mode-mark-server-boundaries (beg end)
"Identifies server blocks."
(save-excursion
(let (open close closing-string continue start sub2 sub3 pos tagopen l tmp)
(goto-char beg)
;; (message "%S: %Sx%S" (point) beg end)
;; (message "regexp=%S" web-mode-server-blocks-regexp)
(while (and (> end (point))
(re-search-forward web-mode-server-blocks-regexp end t))
(setq close nil
tagopen (match-string 0)
open (match-beginning 0)
pos nil)
;; (message "sub2=%S" tagopen)
(when (or (char-equal ?\s (string-to-char tagopen))
(char-equal ?\t (string-to-char tagopen)))
(setq l (length tagopen))
(setq tagopen (replace-regexp-in-string "\\`[ \t]*" "" tagopen))
(setq open (+ open (- l (length tagopen))))
)
(setq sub2 (substring tagopen 0 2))
;; (message "sub2=%S" sub2)
(cond
((string= web-mode-engine "smarty")
(cond
((string= sub2 "{*")
(setq closing-string "*}"))
((char-equal ?{ (string-to-char sub2))
(setq closing-string "}"))
)
);smarty
((string= web-mode-engine "ctemplate")
(cond
((string= sub3 "{{{")
(setq closing-string "}}}"))
(t
(setq closing-string "}}"))
)
);smarty
(t
(cond
((string= "#*" sub2)
(setq closing-string "*#"))
((char-equal ?\# (string-to-char sub2))
(setq closing-string "EOL"))
((char-equal ?$ (string-to-char sub2))
(setq closing-string "EOV"))
((char-equal ?% (string-to-char sub2))
(setq closing-string "EOL"))
((string= "<?" sub2)
(unless (looking-at-p "xml ")
(setq closing-string "?>")
))
((string= "<%-" tagopen)
(setq closing-string "--%>"))
((string= "<%#" tagopen)
(setq closing-string "%>"))
((string= "[#-" tagopen)
(setq closing-string "--]"))
((string= "<#-" tagopen)
(setq closing-string "-->"))
((or (string= "[#" sub2) (string= "[@" sub2) (string= "[/" sub2))
(setq closing-string "]"))
((or (string= "<#" sub2) (string= "<@" sub2) (string= "</" sub2))
(setq closing-string ">"))
((string= "<%@" tagopen)
(setq closing-string "%>"))
((string= "<%" sub2)
(setq closing-string "%>"))
((member sub2 '("${" "#{"))
(setq closing-string "}"))
((string= "{{" sub2)
(setq closing-string "}}"))
((string= "{%" sub2)
(setq closing-string "%}"))
((string= "{#" sub2)
(setq closing-string "#}"))
);cond
);t
);cond
(when closing-string
(cond
((and (string= web-mode-engine "smarty")
(string= closing-string "}"))
(goto-char open)
(setq tmp (web-mode-fetch-closing-paren-pos (point) (line-end-position)))
(if tmp
(setq tmp (1+ tmp))
(setq tmp (line-end-position)))
(goto-char tmp)
(setq close (point)
pos (point))
)
((string= closing-string "EOL")
(end-of-line)
(setq close (point)
pos (point)))
((string= closing-string "EOV")
(goto-char open)
;; (message "pt=%S %c" (point) (char-after))
(when (char-equal ?$ (char-after))
;; (message "pt=%S" (point))
(forward-char))
(when (char-equal ?! (char-after))
;; (message "pt=%S" (point))
(forward-char))
(if (char-equal ?{ (char-after))
(search-forward "}")
(setq continue t)
(while continue
(skip-chars-forward "a-zA-Z0-9_-")
(when (char-equal ?\( (char-after))
(search-forward ")")
)
(if (char-equal ?\. (char-after))
(forward-char)
(setq continue nil))
);while
);if
(setq close (point)
pos (point)))
((search-forward closing-string end t)
;; (message "cs: %S" closing-string)
(setq close (match-end 0)
pos (point)))
((string= "<?" sub2)
(setq close (point-max)
pos (point-max)))
)
(when close
;; (message "open(%S) close(%S)" open close)
;; (add-text-properties open (+ open 1) '(server-pos beg))
(add-text-properties open close '(server-side t))
(add-text-properties (- close 1) close '(server-pos end))
)
(if pos (goto-char pos))
);when
);while
)))
(defun web-mode-scan-server (beg end)
"Identifies server blocks."
(save-excursion
(let ((block-beg beg)
(block-end nil)
(continue t)
(pm (point-max)))
(goto-char beg)
(unless (get-text-property beg 'server-side)
(setq block-beg (next-single-property-change beg 'server-side))
(if (or (not block-beg) (> block-beg end))
(setq continue nil))
)
(while continue
(setq block-end (or (next-single-property-change block-beg 'server-pos) pm))
;; (setq block-end (or (next-single-property-change block-beg 'server-side) (point-max)))
;; (message "block-beg(%S) block-end(%S) end(%S)" block-beg block-end end)
(if (or (not block-end) (> block-end end) (> block-end (- pm 2)))
(setq continue nil)
(web-mode-scan-server-block block-beg (+ block-end 1))
;; (forward-char)
(if (get-text-property (+ block-end 1) 'server-side)
(setq block-beg (+ block-end 1))
(setq block-beg (next-single-property-change (+ block-end 1) 'server-side)))
(if (or (not block-beg) (> block-beg end))
(setq continue nil))
);if
);while
)))
;; todo : move props from server-boundaries to here
(defun web-mode-scan-server-block (beg end)
"Scan server block."
(let (sub1 sub2 sub3 regexp props start ms continue fc keywords tag)
(goto-char beg)
(setq sub1 (buffer-substring-no-properties beg (+ beg 1))
sub2 (buffer-substring-no-properties beg (+ beg 2)))
(setq sub3 sub2)
(if (>= (point-max) (+ beg 3))
(setq sub3 (buffer-substring-no-properties beg (+ beg 3))))
;; (message "beg(%S) end(%S) sub3(%S)" beg end sub3)
(cond
((string= web-mode-engine "ctemplate")
(cond
((string= sub3 "{{!")
(setq props '(server-type comment face web-mode-comment-face)))
((string= sub3 "{{%")
(setq regexp "\"\\|'"
props '(server-engine ctemplate face nil)
keywords web-mode-ctemplate-font-lock-keywords))
(t
(setq props '(server-engine ctemplate face nil)
keywords web-mode-ctemplate-font-lock-keywords))
)
);ctemplate
((string= sub2 "<?")
(setq regexp "//\\|/\\*\\|\"\\|'\\|<<<['\"]?\\([[:alnum:]]+\\)['\"]?"
props '(server-engine php face nil)
keywords web-mode-php-font-lock-keywords))
((member sub3 '("<%-" "<#-" "[#-"))
;; ((or (string= "<%-" sub3) (string= "<#-" sub3) (string= "[#-" sub3))
(setq props '(server-type comment face web-mode-comment-face)))
((member sub2 '("<#" "<@" "</" "[#" "[@" "[/"))
;; ((or (string= "<#" sub2) (string= "<@" sub2) (string= "</" sub2)
;; (string= "[#" sub2) (string= "[@" sub2) (string= "[/" sub2))
(setq regexp "\"\\|'"
keywords web-mode-freemarker-font-lock-keywords
props '(server-engine freemarker face nil))
(looking-at "[<[]/?\\([#@][[:alnum:]._]+\\)")
(setq tag (match-string-no-properties 1))
(setq props (plist-put props 'server-tag-name tag))
(cond
((char-equal (char-after (+ beg 1)) ?/)
(setq props (plist-put props 'server-tag-type 'end))
)
((char-equal (char-after (- end 2)) ?/)
(setq props (plist-put props 'server-tag-type 'void))
)
(t
(if (web-mode-is-void-element (match-string 1))
(setq props (plist-put props 'server-tag-type 'void))
(setq props (plist-put props 'server-tag-type 'start))
)
)
);cond
);or
((string= sub3 "<%@")
(setq regexp "/\\*"
props '(server-engine directive face nil)
keywords web-mode-directive-font-lock-keywords))
((string= sub3 "<%$")
(setq regexp "\"\\|'"
props '(face nil)
keywords web-mode-expression-font-lock-keywords))
((and (string= sub3 "<%#")
(not (string= web-mode-engine "asp")))
(setq props '(server-type comment face web-mode-comment-face))
)
((or (string= sub2 "<%") (string= sub1 "%"))
(setq regexp "//\\|/\\*\\|\"\\|'")
(cond
((or (string= sub1 "%") (string= web-mode-engine "erb"))
(setq props '(server-engine erb face nil)
keywords web-mode-jsp-font-lock-keywords)
)
((string= web-mode-engine "asp")
(setq props '(server-engine asp face nil)
keywords web-mode-asp-font-lock-keywords)
)
(t
(setq props '(server-engine jsp face nil)
keywords web-mode-jsp-font-lock-keywords)
)
)
)
((string= sub2 "{*")
(setq props '(server-type comment face web-mode-comment-face))
)
((and (string= sub1 "{") (string= web-mode-engine "smarty"))
(setq regexp "\"\\|'"
props '(server-engine smarty face nil)
keywords web-mode-smarty-font-lock-keywords)
)
((and (string= sub1 "$") (string= web-mode-engine "velocity"))
(setq regexp "\"\\|'"
props '(server-engine velocity face nil)
keywords web-mode-velocity-font-lock-keywords)
)
((member sub2 '("${" "#{"))
(setq regexp "\"\\|'"
props '(server-engine jsp face nil)
keywords web-mode-uel-font-lock-keywords)
)
((string= sub2 "{{")
(setq regexp "\"\\|'"
props '(server-engine django face nil)
keywords web-mode-uel-font-lock-keywords)
)
((string= sub2 "{%")
(setq regexp "//\\|/\\*\\|\"\\|'"
props '(server-engine django face nil)
keywords web-mode-django-font-lock-keywords)
)
((string= sub2 "{#")
(setq props '(server-type comment face web-mode-comment-face))
)
((member sub2 '("##" "#*"))
(setq props '(server-type comment face web-mode-comment-face))
)
((string= sub1 "#")
(setq regexp "\"\\|'"
props '(server-engine velocity face nil)
keywords web-mode-velocity-font-lock-keywords)
)
);cond
(add-text-properties beg end props)
(when keywords (web-mode-fontify-region beg end keywords))
(when regexp
(goto-char beg)
(while (re-search-forward regexp end t)
(setq start (match-beginning 0)
ms (match-string 0)
continue t)
(setq fc (substring ms 0 1))
(cond
((string= fc "'")
(setq props '(server-type string face web-mode-string-face))
(while (and continue (search-forward "'" end t))
(setq continue (char-equal ?\\ (char-before (- (point) 1))))
)
)
((string= fc "\"")
(setq props '(server-type string face web-mode-string-face))
(while (and continue (search-forward "\"" end t))
(setq continue (char-equal ?\\ (char-before (- (point) 1))))
)
)
((string= ms "//")
(setq props '(server-type comment face web-mode-comment-face))
(goto-char (if (< end (line-end-position)) end (line-end-position)))
)
((string= ms "/*")
(setq props '(server-type comment face web-mode-comment-face))
(search-forward "*/" end t)
)
((string= fc "<")
;; (message "tag=%S" (match-string 1))
(setq props '(server-type string face web-mode-string-face))
(re-search-forward (concat "^" (match-string 1)) end t)
)
);;cond
;; (message "elt=%S" (buffer-substring start (point)))
(add-text-properties start (point) props)
);while
);when
))
;; start-tag, end-tag, tag-name, element (<a>xsx</a>, an element is delimited by tags), void-element
;; http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
;;<#include "toto">
(defun web-mode-scan-client (beg end)
"Scan client side blocks (JS / CSS / HTML Comments) and identifies strings and comments."
(save-excursion
(let (open limit close ms regexp props closing-string start tag-name tag-beg tag-end tag-fc tag-stop attrs-end close-found prop-type prop-name)
(goto-char beg)
(while (web-mode-rsf-client "<\\(!--\\|!doctype\\|/?[[:alnum:]]+[:_]?[[:alnum:]]*\\|\?xml\\)" end t)
(setq tag-name (downcase (match-string 1))
tag-beg (match-beginning 0)
tag-end nil
tag-type nil
tag-stop (point)
tag-fc (substring (match-string 0) 0 1)
prop-name 'client-tag-name
prop-type 'client-tag-type
open nil
limit end
close nil
pos nil
markup-face nil
props nil
regexp nil
closing-string nil
close-found nil)
(cond
((string= tag-name "!--")
(setq regexp "-->"))
((string= tag-name "!doctype")
(setq regexp ">"))
((string= tag-name "?xml")
;; (setq regexp "\?>"))
(setq regexp "?>"))
(t
(cond
((string-match-p "[:_]" tag-name)
(setq props '(face web-mode-preprocessor-face)
prop-name 'server-tag-name
prop-type 'server-tag-type))
(t
(setq props '(face web-mode-html-tag-face)))
)
(cond
((char-equal (string-to-char tag-name) ?/)
(setq props (plist-put props prop-name (substring tag-name 1)))
(setq props (plist-put props prop-type 'end))
(setq regexp ">")
(setq limit (if (> end (line-end-position)) (line-end-position) end))
)
((web-mode-is-void-element tag-name)
(setq props (plist-put props prop-name tag-name))
(setq props (plist-put props prop-type 'void))
(setq regexp "/?>")
;; (setq regexp ">")
)
(t
(setq props (plist-put props prop-name tag-name))
(setq props (plist-put props prop-type 'start))
(setq regexp "/?>")
;; (setq regexp ">")
)
);cond
;; (add-text-properties tag-beg tag-stop props)
);t