annotate lisp/textmodes/css-mode.el @ 78636:b1e667d914ef

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