summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/related/flymake-css.el
blob: d80abe3afb2d9b6378d7f7e00610b6a5bc834584 (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
;;; flymake-css.el --- Flymake setup for css files
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: 2009-11-21 Sat
;; Version:
;; Last-Updated:
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;  See variable `flymake-css-validator-jar' for instructions for how
;;  to set this up.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; 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:


(require 'flymake)
(require 'xml)


(defcustom flymake-allowed-css-file-name-masks '(("\\.css\\'" flymake-css-init))
  "Filename extensions that switch on js syntax checks."
  :type '(repeat (list (regexp :tag "File name regexp")
                       (function :tag "Init function")
                       (choice (const :tag "No cleanup function" nil)
                               (function :tag "Cleanup function"))))
  :group 'flymake)


(defvar flymake-css-err-line-pattern-re '(("^file:\\([^:]+\\):\\([^:]+\\):\\(.*\\)" 1 2 nil 3))
  "Regexp matching CSS error messages")

(defcustom flymake-css-validator-jar "~/bin/css-validator.jar"
  "Full path to css-validor.jar file.
You need the css-validator.jar and some other files for flymake
for CSS to work. The instructions below tell you how to get and
install it. The instructions are copied from

  http://www.emacswiki.org/emacs/FlymakeCSS

Get http://www.w3.org/QA/Tools/css-validator/css-validator.jar
create a directory named ‘lib’ in the same directory. Copy to the
‘lib’ dir the following jars:

 * commons-collections-3.2.1.jar
 * jigsaw.jar
 * velocity-1.6.1.jar
 * xml-apis.jar
 * commons-lang-2.4.jar
 * tagsoup-1.2.jar
 * xercesImpl.jar

From:

 URL `http://jigsaw.w3.org/Distrib/jigsaw_2.2.6.tar.gz'
 URL `http://www.apache.org/dist/commons/collections/binaries/commons-collections-3.2.1-bin.tar.gz'
 URL `http://www.apache.org/dist/commons/lang/binaries/commons-lang-2.4-bin.tar.gz'
 URL `http://www.apache.org/dist/velocity/engine/1.6.1/velocity-1.6.1.tar.gz'
 URL `http://www.apache.org/dist/xerces/j/Xerces-J-bin.2.9.1.tar.gz'
 URL `http://home.ccil.org/~cowan/XML/tagsoup/tagsoup-1.2.jar'

Test validating some CSS file by running:

  java -jar css-validator.jar file:somecssfile.css"
  :type 'file
  :group 'flymake)
;;(setq flymake-css-validator-jar "c:/dl/programs/css-valid/css-validator.jar")

(defun flymake-css-init ()
  (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
         (local-file (file-relative-name
                      temp-file
                      (file-name-directory buffer-file-name))))
    (unless (file-exists-p flymake-css-validator-jar)
      (error "Can't find css-validator.jar: %s\n\nPlease customize option flymake-css-validator-jar\n"
             flymake-css-validator-jar))
    (list "java"
          (list "-jar" flymake-css-validator-jar
                "-output" "gnu"
                (concat "file:" local-file)))))

;;;###autoload
(defun flymake-css-load ()
  (dolist (rec flymake-allowed-css-file-name-masks)
    (add-to-list 'flymake-allowed-file-name-masks rec))
  (dolist (rec flymake-css-err-line-pattern-re)
    (add-to-list 'flymake-err-line-patterns rec)))


;;(defun flymake-make-overlay (beg end tooltip-text face mouse-face)
(defadvice flymake-make-overlay (before
                                 flymake-css-ad-flymake-make-overlay
                                 activate
                                 compile)
  (ad-set-arg 2 (xml-substitute-numeric-entities (ad-get-arg 2))))

;; Fix-me: remove when this has been giving its proper place in Emacs.
(eval-when-compile
  (unless (fboundp 'xml-substitute-numeric-entities)
    (message "Use Emacs 22 workaround for newsticker--decode-numeric-entities")
    (defun xml-substitute-numeric-entities (string)
    "Decode SGML numeric entities by their respective utf characters.
This is just a copy of the function in newst-backen.el for Emacs
22 users.

This function replaces numeric entities in the input STRING and
returns the modified string.  For example \"*\" gets replaced
by \"*\"."
    (if (and string (stringp string))
        (let ((start 0))
          (while (string-match "&#\\([0-9]+\\);" string start)
            (condition-case nil
                (setq string (replace-match
                              (string (read (substring string
                                                       (match-beginning 1)
                                                       (match-end 1))))
                              nil nil string))
              (error nil))
            (setq start (1+ (match-beginning 0))))
          string)
      nil))
    ))

;;(eval-after-load 'css-mode (flymake-css-load))

(provide 'flymake-css)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; flymake-css.el ends here