Mercurial > emacs
annotate lisp/textmodes/css-mode.el @ 79665:37f5e5ae84d8
*** empty log message ***
author | Dan Nicolaescu <dann@ics.uci.edu> |
---|---|
date | Thu, 03 Jan 2008 06:46:43 +0000 |
parents | e3d75658c018 |
children | dc100f64b2b7 b210bba3f477 |
rev | line source |
---|---|
77923 | 1 ;;; css-mode.el --- Major mode to edit CSS files |
2 | |
3 ;; Copyright (C) 2006, 2007 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> | |
6 ;; Keywords: hypermedia | |
7 | |
8 ;; This file is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
78225
b6d25790aab2
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
77923
diff
changeset
|
10 ;; the Free Software Foundation; either version 3, or (at your option) |
77923 | 11 ;; any later version. |
12 | |
13 ;; This file is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
21 ;; Boston, MA 02110-1301, USA. | |
22 | |
23 ;;; Commentary: | |
24 | |
25 ;; Yet another CSS mode. | |
26 | |
27 ;;; Todo: | |
28 | |
29 ;; - electric ; and } | |
30 ;; - filling code with auto-fill-mode | |
31 ;; - completion | |
32 ;; - fix font-lock errors with multi-line selectors | |
33 | |
34 ;;; Code: | |
35 | |
79187 | 36 (eval-when-compile (require 'cl)) |
37 | |
77923 | 38 (defun css-extract-keyword-list (res) |
39 (with-temp-buffer | |
40 (url-insert-file-contents "http://www.w3.org/TR/REC-CSS2/css2.txt") | |
41 (goto-char (point-max)) | |
42 (search-backward "Appendix H. Index") | |
43 (forward-line) | |
44 (delete-region (point-min) (point)) | |
45 (let ((result nil) | |
46 keys) | |
47 (dolist (re res) | |
48 (goto-char (point-min)) | |
49 (setq keys nil) | |
50 (while (re-search-forward (cdr re) nil t) | |
51 (push (match-string 1) keys)) | |
52 (push (cons (car re) (sort keys 'string-lessp)) result)) | |
53 (nreverse result)))) | |
54 | |
55 (defun css-extract-parse-val-grammar (string env) | |
56 (let ((start 0) | |
57 (elems ()) | |
58 name) | |
59 (while (string-match | |
60 (concat "\\(?:" | |
61 (concat "<a [^>]+><span [^>]+>\\(?:" | |
62 "<\\([^&]+\\)>\\|'\\([^']+\\)'" | |
63 "\\)</span></a>") | |
64 "\\|" "\\(\\[\\)" | |
65 "\\|" "\\(]\\)" | |
66 "\\|" "\\(||\\)" | |
67 "\\|" "\\(|\\)" | |
68 "\\|" "\\([*+?]\\)" | |
69 "\\|" "\\({[^}]+}\\)" | |
70 "\\|" "\\(\\w+\\(?:-\\w+\\)*\\)" | |
71 "\\)[ \t\n]*") | |
72 string start) | |
73 ;; (assert (eq start (match-beginning 0))) | |
74 (setq start (match-end 0)) | |
75 (cond | |
76 ;; Reference to a type of value. | |
77 ((setq name (match-string-no-properties 1 string)) | |
78 (push (intern name) elems)) | |
79 ;; Reference to another property's values. | |
80 ((setq name (match-string-no-properties 2 string)) | |
81 (setq elems (delete-dups (append (cdr (assoc name env)) elems)))) | |
82 ;; A literal | |
83 ((setq name (match-string-no-properties 9 string)) | |
84 (push name elems)) | |
85 ;; We just ignore the rest. I.e. we ignore the structure because | |
86 ;; it's too difficult to exploit anyway (it would allow us to only | |
87 ;; complete top/center/bottom after one of left/center/right and | |
88 ;; vice-versa). | |
89 (t nil))) | |
90 elems)) | |
91 | |
92 | |
93 (defun css-extract-props-and-vals () | |
94 (with-temp-buffer | |
95 (url-insert-file-contents "http://www.w3.org/TR/CSS21/propidx.html") | |
96 (goto-char (point-min)) | |
97 (let ((props ())) | |
98 (while (re-search-forward "#propdef-\\([^\"]+\\)\"><span class=\"propinst-\\1 xref\">'\\1'</span></a>" nil t) | |
99 (let ((prop (match-string-no-properties 1))) | |
100 (save-excursion | |
101 (goto-char (match-end 0)) | |
102 (search-forward "<td>") | |
103 (let ((vals-string (buffer-substring (point) | |
104 (progn | |
105 (re-search-forward "[ \t\n]+|[ \t\n]+<a href=\"cascade.html#value-def-inherit\" class=\"noxref\"><span class=\"value-inst-inherit\">inherit</span></a>") | |
106 (match-beginning 0))))) | |
107 ;; | |
108 (push (cons prop (css-extract-parse-val-grammar vals-string props)) | |
109 props))))) | |
110 props))) | |
111 | |
112 ;; Extraction was done with: | |
113 ;; (css-extract-keyword-list | |
114 ;; '((pseudo . "^ +\\* :\\([^ \n,]+\\)") | |
115 ;; (at . "^ +\\* @\\([^ \n,]+\\)") | |
116 ;; (descriptor . "^ +\\* '\\([^ '\n]+\\)' (descriptor)") | |
117 ;; (media . "^ +\\* '\\([^ '\n]+\\)' media group") | |
118 ;; (property . "^ +\\* '\\([^ '\n]+\\)',"))) | |
119 | |
120 (defconst css-pseudo-ids | |
121 '("active" "after" "before" "first" "first-child" "first-letter" "first-line" | |
122 "focus" "hover" "lang" "left" "link" "right" "visited") | |
123 "Identifiers for pseudo-elements and pseudo-classes.") | |
124 | |
125 (defconst css-at-ids | |
126 '("charset" "font-face" "import" "media" "page") | |
127 "Identifiers that appear in the form @foo.") | |
128 | |
129 (defconst css-descriptor-ids | |
130 '("ascent" "baseline" "bbox" "cap-height" "centerline" "definition-src" | |
131 "descent" "font-family" "font-size" "font-stretch" "font-style" | |
132 "font-variant" "font-weight" "mathline" "panose-1" "slope" "src" "stemh" | |
133 "stemv" "topline" "unicode-range" "units-per-em" "widths" "x-height") | |
134 "Identifiers for font descriptors.") | |
135 | |
136 (defconst css-media-ids | |
137 '("all" "aural" "bitmap" "continuous" "grid" "paged" "static" "tactile" | |
138 "visual") | |
139 "Identifiers for types of media.") | |
140 | |
141 (defconst css-property-ids | |
142 '("azimuth" "background" "background-attachment" "background-color" | |
143 "background-image" "background-position" "background-repeat" "block" | |
144 "border" "border-bottom" "border-bottom-color" "border-bottom-style" | |
145 "border-bottom-width" "border-collapse" "border-color" "border-left" | |
146 "border-left-color" "border-left-style" "border-left-width" "border-right" | |
147 "border-right-color" "border-right-style" "border-right-width" | |
148 "border-spacing" "border-style" "border-top" "border-top-color" | |
149 "border-top-style" "border-top-width" "border-width" "bottom" | |
150 "caption-side" "clear" "clip" "color" "compact" "content" | |
151 "counter-increment" "counter-reset" "cue" "cue-after" "cue-before" | |
152 "cursor" "dashed" "direction" "display" "dotted" "double" "elevation" | |
153 "empty-cells" "float" "font" "font-family" "font-size" "font-size-adjust" | |
154 "font-stretch" "font-style" "font-variant" "font-weight" "groove" "height" | |
155 "hidden" "inline" "inline-table" "inset" "left" "letter-spacing" | |
156 "line-height" "list-item" "list-style" "list-style-image" | |
157 "list-style-position" "list-style-type" "margin" "margin-bottom" | |
158 "margin-left" "margin-right" "margin-top" "marker-offset" "marks" | |
159 "max-height" "max-width" "min-height" "min-width" "orphans" "outline" | |
160 "outline-color" "outline-style" "outline-width" "outset" "overflow" | |
161 "padding" "padding-bottom" "padding-left" "padding-right" "padding-top" | |
162 "page" "page-break-after" "page-break-before" "page-break-inside" "pause" | |
163 "pause-after" "pause-before" "pitch" "pitch-range" "play-during" "position" | |
164 "quotes" "richness" "ridge" "right" "run-in" "size" "solid" "speak" | |
165 "speak-header" "speak-numeral" "speak-punctuation" "speech-rate" "stress" | |
166 "table" "table-caption" "table-cell" "table-column" "table-column-group" | |
167 "table-footer-group" "table-header-group" "table-layout" "table-row" | |
168 "table-row-group" "text-align" "text-decoration" "text-indent" | |
169 "text-shadow" "text-transform" "top" "unicode-bidi" "vertical-align" | |
170 "visibility" "voice-family" "volume" "white-space" "widows" "width" | |
171 "word-spacing" "z-index") | |
172 "Identifiers for properties.") | |
173 | |
78825
6ba709e4adda
(css-electric-keys): electrick->electric.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78225
diff
changeset
|
174 (defcustom css-electric-keys '(?\} ?\;) ;; '() |
77923 | 175 "Self inserting keys which should trigger re-indentation." |
176 :type '(repeat character) | |
177 :options '((?\} ?\;))) | |
178 | |
179 (defvar css-mode-syntax-table | |
180 (let ((st (make-syntax-table))) | |
181 ;; C-style comments. | |
182 (modify-syntax-entry ?/ ". 14" st) | |
183 (modify-syntax-entry ?* ". 23" st) | |
184 ;; Strings. | |
185 (modify-syntax-entry ?\" "\"" st) | |
186 (modify-syntax-entry ?\' "\"" st) | |
187 ;; Blocks. | |
188 (modify-syntax-entry ?\{ "(}" st) | |
189 (modify-syntax-entry ?\} "){" st) | |
190 ;; Args in url(...) thingies and other "function calls". | |
191 (modify-syntax-entry ?\( "()" st) | |
192 (modify-syntax-entry ?\) ")(" st) | |
193 ;; To match attributes in selectors. | |
194 (modify-syntax-entry ?\[ "(]" st) | |
195 (modify-syntax-entry ?\] ")[" st) | |
196 ;; Special chars that sometimes come at the beginning of words. | |
197 (modify-syntax-entry ?@ "'" st) | |
198 ;; (modify-syntax-entry ?: "'" st) | |
199 (modify-syntax-entry ?# "'" st) | |
200 ;; Distinction between words and symbols. | |
201 (modify-syntax-entry ?- "_" st) | |
202 st)) | |
203 | |
204 (defconst css-escapes-re | |
205 "\\\\\\(?:[^\000-\037\177]\\|[0-9a-fA-F]+[ \n\t\r\f]?\\)") | |
206 (defconst css-nmchar-re (concat "\\(?:[-[:alnum:]]\\|" css-escapes-re "\\)")) | |
207 (defconst css-nmstart-re (concat "\\(?:[[:alpha:]]\\|" css-escapes-re "\\)")) | |
208 (defconst css-ident-re (concat css-nmstart-re css-nmchar-re "*")) | |
209 (defconst css-name-re (concat css-nmchar-re "+")) | |
210 | |
211 (defface css-selector '((t :inherit font-lock-function-name-face)) | |
212 "Face to use for selectors.") | |
213 (defface css-property '((t :inherit font-lock-variable-name-face)) | |
214 "Face to use for properties.") | |
215 | |
216 (defvar css-font-lock-keywords | |
217 `(("!\\s-*important" . font-lock-builtin-face) | |
218 ;; Atrules keywords. IDs not in css-at-ids are valid (ignored). | |
219 ;; In fact the regexp should probably be | |
220 ;; (,(concat "\\(@" css-ident-re "\\)\\([ \t\n][^;{]*\\)[;{]") | |
221 ;; (1 font-lock-builtin-face)) | |
222 ;; Since "An at-rule consists of everything up to and including the next | |
223 ;; semicolon (;) or the next block, whichever comes first." | |
224 (,(concat "@" css-ident-re) . font-lock-builtin-face) | |
225 ;; Selectors. | |
226 ;; FIXME: attribute selectors don't work well because they may contain | |
227 ;; strings which have already been highlighted as f-l-string-face and | |
228 ;; thus prevent this highlighting from being applied (actually now that | |
229 ;; I use `append' this should work better). But really the part of hte | |
230 ;; selector between [...] should simply not be highlighted. | |
231 (,(concat "^\\([ \t]*[^@:{\n][^:{\n]+\\(?::" (regexp-opt css-pseudo-ids t) | |
232 "\\(?:([^)]+)\\)?[^:{\n]*\\)*\\)\\(?:\n[ \t]*\\)*{") | |
233 (1 'css-selector append)) | |
234 ;; In the above rule, we allow the open-brace to be on some subsequent | |
235 ;; line. This will only work if we properly mark the intervening text | |
236 ;; as being part of a multiline element (and even then, this only | |
237 ;; ensures proper refontification, but not proper discovery). | |
238 ("^[ \t]*{" (0 (save-excursion | |
239 (goto-char (match-beginning 0)) | |
240 (skip-chars-backward " \n\t") | |
241 (put-text-property (point) (match-end 0) | |
242 'font-lock-multiline t) | |
243 ;; No face. | |
244 nil))) | |
245 ;; Properties. Again, we don't limit ourselves to css-property-ids. | |
246 (,(concat "\\(?:[{;]\\|^\\)[ \t]*\\(" css-ident-re "\\)\\s-*:") | |
247 (1 'css-property)))) | |
248 | |
249 (defvar css-font-lock-defaults | |
250 '(css-font-lock-keywords nil t)) | |
251 | |
252 (unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode)) | |
253 | |
254 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.css\\'" . css-mode)) | |
255 ;;;###autoload | |
256 (define-derived-mode css-mode prog-mode "CSS" | |
257 "Major mode to edit Cascading Style Sheets." | |
258 (set (make-local-variable 'font-lock-defaults) css-font-lock-defaults) | |
259 (set (make-local-variable 'comment-start) "/*") | |
260 (set (make-local-variable 'comment-start-skip) "/\\*+[ \t]*") | |
261 (set (make-local-variable 'comment-end) "*/") | |
262 (set (make-local-variable 'comment-end-skip) "[ \t]*\\*+/") | |
263 (set (make-local-variable 'forward-sexp-function) 'css-forward-sexp) | |
264 (set (make-local-variable 'parse-sexp-ignore-comments) t) | |
265 (set (make-local-variable 'indent-line-function) 'css-indent-line) | |
266 (set (make-local-variable 'fill-paragraph-function) | |
267 'css-fill-paragraph) | |
78825
6ba709e4adda
(css-electric-keys): electrick->electric.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78225
diff
changeset
|
268 (when css-electric-keys |
77923 | 269 (let ((fc (make-char-table 'auto-fill-chars))) |
270 (set-char-table-parent fc auto-fill-chars) | |
78825
6ba709e4adda
(css-electric-keys): electrick->electric.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78225
diff
changeset
|
271 (dolist (c css-electric-keys) |
77923 | 272 (aset fc c 'indent-according-to-mode)) |
273 (set (make-local-variable 'auto-fill-chars) fc)))) | |
274 | |
79187 | 275 (defvar comment-continue) |
276 | |
77923 | 277 (defun css-fill-paragraph (&optional justify) |
278 (save-excursion | |
279 (let ((ppss (syntax-ppss)) | |
280 (eol (line-end-position))) | |
281 (cond | |
282 ((and (nth 4 ppss) | |
283 (save-excursion | |
284 (goto-char (nth 8 ppss)) | |
285 (forward-comment 1) | |
286 (prog1 (not (bolp)) | |
287 (setq eol (point))))) | |
288 ;; Filling inside a comment whose comment-end marker is not \n. | |
289 ;; This code is meant to be generic, so that it works not only for | |
290 ;; css-mode but for all modes. | |
291 (save-restriction | |
292 (narrow-to-region (nth 8 ppss) eol) | |
79187 | 293 (comment-normalize-vars) ;Will define comment-continue. |
77923 | 294 (let ((fill-paragraph-function nil) |
295 (paragraph-separate | |
296 (if (and comment-continue | |
297 (string-match "[^ \t]" comment-continue)) | |
298 (concat "\\(?:[ \t]*" (regexp-quote comment-continue) | |
299 "\\)?\\(?:" paragraph-separate "\\)") | |
300 paragraph-separate)) | |
301 (paragraph-start | |
302 (if (and comment-continue | |
303 (string-match "[^ \t]" comment-continue)) | |
304 (concat "\\(?:[ \t]*" (regexp-quote comment-continue) | |
305 "\\)?\\(?:" paragraph-start "\\)") | |
306 paragraph-start))) | |
307 (fill-paragraph justify) | |
308 ;; Don't try filling again. | |
309 t))) | |
310 | |
311 ((and (null (nth 8 ppss)) | |
312 (or (nth 1 ppss) | |
313 (and (ignore-errors | |
314 (down-list 1) | |
315 (when (<= (point) eol) | |
316 (setq ppss (syntax-ppss))))))) | |
317 (goto-char (nth 1 ppss)) | |
318 (let ((end (save-excursion | |
319 (ignore-errors (forward-sexp 1) (copy-marker (point) t))))) | |
320 (when end | |
321 (while (re-search-forward "[{;}]" end t) | |
322 (cond | |
323 ;; This is a false positive inside a string or comment. | |
324 ((nth 8 (syntax-ppss)) nil) | |
325 ((eq (char-before) ?\}) | |
326 (save-excursion | |
327 (forward-char -1) | |
328 (skip-chars-backward " \t") | |
329 (unless (bolp) (newline)))) | |
330 (t | |
331 (while | |
332 (progn | |
333 (setq eol (line-end-position)) | |
334 (and (forward-comment 1) | |
335 (> (point) eol) | |
336 ;; A multi-line comment should be on its own line. | |
337 (save-excursion (forward-comment -1) | |
338 (when (< (point) eol) | |
339 (newline) | |
340 t))))) | |
341 (if (< (point) eol) (newline))))) | |
342 (goto-char (nth 1 ppss)) | |
343 (indent-region (line-beginning-position 2) end) | |
344 ;; Don't use the default filling code. | |
345 t))))))) | |
346 | |
347 ;;; Navigation and indentation. | |
348 | |
349 (defconst css-navigation-syntax-table | |
350 (let ((st (make-syntax-table css-mode-syntax-table))) | |
351 (map-char-table (lambda (c v) | |
352 ;; Turn punctuation (code = 1) into symbol (code = 1). | |
353 (if (eq (car-safe v) 1) | |
354 (aset st c (cons 3 (cdr v))))) | |
355 st) | |
356 st)) | |
357 | |
358 (defun css-backward-sexp (n) | |
359 (let ((forward-sexp-function nil)) | |
360 (if (< n 0) (css-forward-sexp (- n)) | |
361 (while (> n 0) | |
362 (setq n (1- n)) | |
363 (forward-comment (- (point-max))) | |
364 (if (not (eq (char-before) ?\;)) | |
365 (backward-sexp 1) | |
366 (while (progn (backward-sexp 1) | |
367 (save-excursion | |
368 (forward-comment (- (point-max))) | |
369 ;; FIXME: We should also skip punctuation. | |
370 (not (memq (char-before) '(?\; ?\{))))))))))) | |
371 | |
372 (defun css-forward-sexp (n) | |
373 (let ((forward-sexp-function nil)) | |
374 (if (< n 0) (css-backward-sexp (- n)) | |
375 (while (> n 0) | |
376 (setq n (1- n)) | |
377 (forward-comment (point-max)) | |
378 (if (not (eq (char-after) ?\;)) | |
379 (forward-sexp 1) | |
380 (while (progn (forward-sexp 1) | |
381 (save-excursion | |
382 (forward-comment (point-max)) | |
383 ;; FIXME: We should also skip punctuation. | |
384 (not (memq (char-after) '(?\; ?\}))))))))))) | |
385 | |
386 (defun css-indent-calculate-virtual () | |
387 (if (or (save-excursion (skip-chars-backward " \t") (bolp)) | |
388 (if (looking-at "\\s(") | |
389 (save-excursion | |
390 (forward-char 1) (skip-chars-forward " \t") | |
391 (not (or (eolp) (looking-at comment-start-skip)))))) | |
392 (current-column) | |
393 (css-indent-calculate))) | |
394 | |
395 (defcustom css-indent-offset 4 | |
396 "Basic size of one indentation step." | |
397 :type 'integer) | |
398 | |
399 (defun css-indent-calculate () | |
400 (let ((ppss (syntax-ppss)) | |
401 pos) | |
402 (with-syntax-table css-navigation-syntax-table | |
403 (save-excursion | |
404 (cond | |
405 ;; Inside a string. | |
406 ((nth 3 ppss) 'noindent) | |
407 ;; Inside a comment. | |
408 ((nth 4 ppss) | |
409 (setq pos (point)) | |
410 (forward-line -1) | |
411 (skip-chars-forward " \t") | |
412 (if (>= (nth 8 ppss) (point)) | |
413 (progn | |
414 (goto-char (nth 8 ppss)) | |
415 (if (eq (char-after pos) ?*) | |
416 (forward-char 1) | |
417 (if (not (looking-at comment-start-skip)) | |
418 (error "Internal css-mode error") | |
419 (goto-char (match-end 0)))) | |
420 (current-column)) | |
421 (if (and (eq (char-after pos) ?*) (eq (char-after) ?*)) | |
422 (current-column) | |
423 ;; 'noindent | |
424 (current-column) | |
425 ))) | |
426 ;; In normal code. | |
427 (t | |
428 (or | |
429 (when (looking-at "\\s)") | |
430 (forward-char 1) | |
431 (backward-sexp 1) | |
432 (css-indent-calculate-virtual)) | |
433 (when (looking-at comment-start-skip) | |
434 (forward-comment (point-max)) | |
435 (css-indent-calculate)) | |
436 (when (save-excursion (forward-comment (- (point-max))) | |
437 (setq pos (point)) | |
438 (eq (char-syntax (preceding-char)) ?\()) | |
439 (goto-char (1- pos)) | |
440 (if (not (looking-at "\\s([ \t]*")) | |
441 (error "Internal css-mode error") | |
442 (if (or (memq (char-after (match-end 0)) '(?\n nil)) | |
443 (save-excursion (goto-char (match-end 0)) | |
444 (looking-at comment-start-skip))) | |
445 (+ (css-indent-calculate-virtual) css-indent-offset) | |
446 (progn (goto-char (match-end 0)) (current-column))))) | |
447 (progn | |
448 (css-backward-sexp 1) | |
449 (if (looking-at "\\s(") | |
450 (css-indent-calculate) | |
451 (css-indent-calculate-virtual)))))))))) | |
452 | |
453 | |
454 (defun css-indent-line () | |
455 "Indent current line according to CSS indentation rules." | |
456 (interactive) | |
457 (let* ((savep (point)) | |
458 (forward-sexp-function nil) | |
459 (indent (condition-case nil | |
460 (save-excursion | |
461 (forward-line 0) | |
462 (skip-chars-forward " \t") | |
463 (if (>= (point) savep) (setq savep nil)) | |
464 (css-indent-calculate)) | |
465 (error nil)))) | |
466 (if (not (numberp indent)) 'noindent | |
467 (if savep | |
468 (save-excursion (indent-line-to indent)) | |
469 (indent-line-to indent))))) | |
470 | |
471 (provide 'css-mode) | |
472 ;; arch-tag: b4d8b8e2-b130-4e74-b3aa-cd8f1ab659d0 | |
473 ;;; css-mode.el ends here |