summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/css-simple-completion.el
blob: 95bf27b9411991a4c701b68f7d662c5ecdb9cfba (plain)
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
;;; css-simple-completion.el --- Partly context aware css completion
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: 2009-11-22 Sun
;; Version:
;; Last-Updated: 2009-11-22 Sun
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Simple partly context aware completion. Context is based on
;; guessing mainly.
;;
;; This can be combined with with flymake-css.el that can check the
;; syntax.
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.
;;
;; This program 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 this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

;; Fix-me: bad structure, does not fit completion frameworks
(defun css-simple-completing-w-pred (regexp matnum prompt collection)
  (let (pre start len)
    (when (looking-back regexp (line-beginning-position) t)
      (setq pre (downcase (match-string matnum)))
      (setq len (length pre))
      (setq start (match-beginning matnum))
      (unless (try-completion pre collection)
        (throw 'result nil))
      (throw 'result (list start
                           (completing-read prompt
                                            collection
                                            (lambda (alt)
                                              (and (>= (length alt) len)
                                                   (string= pre
                                                            (substring alt 0 len))))
                                            t
                                            pre))))))

(defun css-simple-complete ()
  "Try to complete at current point.
This tries to complete keywords, but no CSS values.

This is of course a pity since the value syntax is a bit
complicated. However you can at least check the syntax with
flymake-css if you want to."
  (interactive)
  (let ((context (css-simple-guess-context))
        result
        cur
        pre
        start)
    (setq result
          (catch 'result

            (case context

              ( 'css-media-ids
                (css-simple-completing-w-pred "\\<[a-z0-9-]*" 0 "Media type: " css-media-ids))

              ( 'css-at-ids
                (css-simple-completing-w-pred "@\\([a-z0-9-]*\\)" 1 "At rule: @" css-at-ids))

              ( 'css-property-ids
                (css-simple-completing-w-pred "\\<[a-z-]*" 0 "CSS property name: " css-property-ids))

              ( 'css-simple-selectors

                ;; Fix-me: Break out the first two
                (when (looking-back "\\W#\\([a-z0-9-]*\\)")
                  (setq cur (match-string 1))
                  (setq start (match-beginning 1))
                  (throw 'result (list (point)
                                       (read-string (concat "Html tag Id: " cur)))))
                (when (looking-back "\\W\\.\\([a-z0-9-]*\\)")
                  (setq cur (match-string 1))
                  (setq start (match-beginning 1))
                  (throw 'result (list (point)
                                       (read-string (concat "CSS class name: " cur)))))

                (css-simple-completing-w-pred "[a-z0-9]:\\([a-z0-9-]*\\)" 1 "Pseudo id: " css-pseudo-ids)

                (css-simple-completing-w-pred "[a-z0-9-]+" 0 "HTML tag: " (cddr css-simple-selectors))

                (when (looking-back "\\<\\(?:#\\|\\.\\)")
                  (setq pre nil)
                  (while t
                    (setq pre (completing-read "HTML tag, id or CSS class: " css-simple-selectors nil nil pre))
                    (if (string= (substring pre 0 1) "#")
                        (if (or (= 1 (length pre))
                                (and (> (length pre) 2)
                                     (string= (substring pre 0 3) "# (")))
                            (throw 'result (list (point) (concat "#" (read-string "Html tag id: #"))))
                          (throw 'result (list (point) pre)))
                      (if (string= (substring pre 0 1) ".")
                          (if (or (= 1 (length pre))
                                  (and (> (length pre) 2)
                                       (string= (substring pre 0 3) ". (")))
                              (throw 'result (list (point) (concat "." (read-string "CSS class name: ."))))
                            (throw 'result (list (point) pre)))
                        (when (member pre css-simple-selectors)
                          (throw 'result (list (point) pre)))))
                    ))))))
    (message "result=%S" result)
    (if result
        (let ((str (cadr result))
              (len (- (point) (car result))))
          (insert (substring str len)))
      (message "No matching alternatives"))))

(defun css-simple-guess-context ()
  "Try to find a context matching none constant.
Return the symbol corresponding to the context or nil if none
could be found.

The symbols are the names of the defconst holding the possibly
matching ids.

* Note: This function assumes that comments are fontified before
  point."
  ;; Kind of hand-written backward parser ... ;-)
  (let ((ignore-case t) ;; fix-me
        (here (point))
        (after-colon (and (not (bobp)) (eq (char-before) ?:)))
        ret)
    (prog1
        (catch 'return
          ;; No completion in comments.
          (when (eq (get-text-property (point) 'face)
                    'font-lock-comment-face)
            (throw 'return nil))

          ;; If we are not on whitespace then don't complete
          (css-simple-skip-backwards-to-code)
          (unless (or (eobp)
                      (= (char-syntax (char-after)) ?\ )
                      (< (point) here))
            (throw 'return nil))

          ;; Skip backwards to see if after first selector
          (let ((here2 (1+ (point))))
            (while (/= here2 (point))
              (setq here2 (point))
              (css-simple-skip-backwards-to-code)
              (when (and (not (bobp))
                         (eq (char-before) ?,))
                (backward-char))
              (skip-chars-backward "#.:a-z0-9-")))
          ;; Selector
          (when (or (bobp)
                    (eq (char-before) ?}))
            (throw 'return 'css-simple-selectors))

          ;; Property names
          (when (memq (char-before) '( ?{ ?\; ))
            (throw 'return 'css-property-ids))

          ;; If we are in the value we can't complete there yet.
          (when (eq (char-before) ?:)
            (throw 'return nil))


          ;; @
          (goto-char here)
          (skip-chars-backward "a-z0-9-")
          (when (eq (char-before) ?@)
            (throw 'return 'css-at-ids))

          ;; @media ids
          (when (looking-back "@media\\W+")
            (throw 'return 'css-media-ids))

          )
      (goto-char here))))
;;; Fix-me: complete these ...
;;css-descriptor-ids ;; Removed or?

(defun css-simple-skip-backwards-to-code ()
  "Skip backwards until we reach code.
Requires that comments are fontified."
  (let ((here (1+ (point))))
    (while (/= here (point))
      (setq here (point))
      (skip-syntax-backward " ")
      (unless (bobp)
        (when (memq (get-text-property (1- (point)) 'face)
                    '(font-lock-comment-face font-lock-comment-delimiter-face))
          (goto-char (or (previous-single-property-change (1- (point)) 'face)
                         (point-min))))))))

(defconst css-simple-selectors
  '(". (for class)"
    "# (for id)"
    ;; HTML 4.01 tags
    "a" "abbr" "acronym" "address" "applet" "area" "b" "base" "basefont" "bdo" "big"
    "blockquote" "body" "br" "button" "caption" "center" "cite" "code" "col"
    "colgroup" "dd" "del" "dfn" "dir" "div" "dl" "dt" "em" "fieldset" "font" "form"
    "frame" "frameset" "head" "h1" "h2" "h3" "h4" "h5" "h6" "hr" "html" "i" "iframe" "img"
    "input" "ins" "kbd" "label" "legend" "li" "link" "map" "menu" "meta" "noframes"
    "noscript" "object" "ol" "optgroup" "option" "p" "param" "pre" "q" "s" "samp"
    "script" "select" "small" "span" "strike" "strong" "style" "sub" "sup" "table"
    "tbody" "td" "textarea" "tfoot" "th" "thead" "title" "tr" "tt" "u" "ul" "var"
    ))

(provide 'css-simple-completion)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; css-simple-completion.el ends here