initial commit
[emacs-init.git] / nxhtml / nxhtml / xhtml-help.el
1 ;;; xhtml-help.el --- Browse XHTML reference sites
2 ;;
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: 2005-08-16
5 ;; Last-Updated: Wed Aug 01 14:24:07 2007 (7200 +0200)
6 (defconst xhtml-help:version "0.57") ;; Version:
7 ;; Keywords: languages
8
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10 ;;
11 ;;; Commentary:
12 ;;
13 ;; Use when editing XHTML file to get tag references or CSS property
14 ;; name references (like background-color) from web sources.
15 ;;
16 ;; Usage:
17 ;;
18 ;;   (require 'fmode)
19 ;;
20 ;; Then call `xhtml-help-show-tag-ref' or `xhtml-help-show-css-ref'.
21
22
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;;
25 ;;; History:
26 ;;
27 ;; 2005-12-02: Corrected fetching margin-*.
28 ;; 2006-01-08: Prompt for tag and property name before fetching help.
29
30
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;;
33 ;; This file is not part of Emacs
34 ;;
35 ;; This program is free software; you can redistribute it and/or
36 ;; modify it under the terms of the GNU General Public License as
37 ;; published by the Free Software Foundation; either version 2, or (at
38 ;; your option) any later version.
39 ;;
40 ;; This program is distributed in the hope that it will be useful, but
41 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
42 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43 ;; General Public License for more details.
44 ;;
45 ;; You should have received a copy of the GNU General Public License
46 ;; along with this program; see the file COPYING.  If not, write to
47 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
48 ;; Boston, MA 02111-1307, USA.
49
50
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;;
53 ;;; Code:
54
55 (defun xhtml-help-css-prop-at-point ()
56   "Get possible css name property at point."
57   (save-excursion
58     (let ((ch (char-after))
59           (in-word))
60       (when (and (not (bolp))
61                  (or (not ch)
62                      (member ch '(10 9 32 ?\:))))
63         (backward-char)
64         (setq ch (char-after)))
65       (while (string-match "[a-z-]" (char-to-string ch))
66         (setq in-word t)
67         (backward-char)
68         (setq ch (char-after)))
69       (when in-word
70         (forward-char)
71         (when (looking-at "[a-z-]+")
72           (match-string-no-properties 0))))))
73
74 ;;;###autoload
75 (defun xhtml-help-show-css-ref ()
76   "Show CSS reference for CSS property name at point."
77   (interactive)
78   (let ((css-prop (xhtml-help-css-prop-at-point)))
79     (setq css-prop (read-from-minibuffer "Get help for CSS property: " css-prop))
80     (when css-prop
81       (xhtml-help-browse-css css-prop))))
82
83 ;;;###autoload
84 (defun xhtml-help-tag-at-point ()
85   "Get xhtml tag name at or before point."
86   (save-excursion
87     (when (eq (following-char) ?<)
88       (forward-char))
89     (when (and (search-backward "<" nil t)
90                (looking-at "</?\\([[:alnum:]]+\\)"))
91       (match-string-no-properties 1))))
92
93 ;;;###autoload
94 (defun xhtml-help-show-tag-ref ()
95   "Show xhtml reference for tag name at or before point."
96   (interactive)
97   (let ((tag (xhtml-help-tag-at-point)))
98     (setq tag (read-from-minibuffer "Get help for tag name: " tag))
99     (when (< 0 (length tag))
100       (xhtml-help-browse-tag tag))))
101
102 ;;;###autoload
103 (defgroup xhtml-help nil
104   "Customization group for xhtml-help."
105   :group 'nxhtml
106   :group 'hypermedia)
107
108 (defcustom xhtml-help-refurl "http://www.w3.org/"
109   "Web url to get references from."
110   :type '(choice
111           (const "http://www.w3.org/")
112           (const "http://xhtml.com/")
113           (const "http://www.w3schools.com/")
114           ;;(const "http://learningforlife.fsu.edu/")
115           )
116   :group 'xhtml-help)
117
118 (defcustom xhtml-help-query-refurl t
119   "Query for reference url.
120 This is used in `xhtml-help-browse-tag' and `xhtml-help-browse-css'."
121   :type 'boolean
122   :group 'xhtml-help)
123
124 (defun xhtml-help-query-refurl (prompt &optional notvalid)
125   (let ((choices (get 'xhtml-help-refurl 'custom-type))
126         (default xhtml-help-refurl))
127     (unless (eq 'choice (car choices))
128       (error "Custom type of xhtml-help-refurl is not choices"))
129     (setq choices (cdr choices))
130     (setq choices (mapcar (lambda (elt)
131                             (car (cdr elt)))
132                           choices))
133     (mapc (lambda (elt)
134             (setq choices (delete elt choices)))
135           notvalid)
136     (when (member default notvalid)
137       (setq default (car choices)))
138     (completing-read (concat "Fetch " prompt " reference from: ")
139                      choices
140                      nil
141                      t
142                      default
143                      '(choices . 1))))
144
145 (defun xhtml-match (target str)
146   (let ((len (length target)))
147     (when (<= len (length str))
148       (equal target (substring str 0 len)))))
149
150 (defun xhtml-match-member (target str-list)
151   (let (m)
152     (mapc (lambda (elt)
153             (when (xhtml-match elt target)
154               (setq m t)))
155           str-list)
156     m))
157
158 (defun xhtml-help-browse-css (css-prop)
159   (let* ((refurl (if xhtml-help-query-refurl
160                      (xhtml-help-query-refurl (concat "CSS property '" css-prop "'")
161                                               (list "http://xhtml.com/"))
162                    xhtml-help-refurl))
163          (url
164           (cond
165            (  (equal refurl "http://www.w3schools.com/")
166               (concat
167                refurl "css/pr_"
168                (cond
169                 (  (member css-prop '("clear" "cursor" "display" "float" "position" "visibility"))
170                    "class_")
171                 (  (member css-prop '("height" "line-height" "max-width" "min-height" "min-width" "width"))
172                    "dim_")
173                 (  (xhtml-match "font-weight" css-prop)
174                    (setq css-prop "") "font_weight")
175                 (  (xhtml-match "font" css-prop)
176                    "font_")
177                 (  (member css-prop '("content" "counter-increment" "counter-reset" "quotes"))
178                    "gen_")
179                 (  (xhtml-match "list" css-prop)
180                    "list_")
181                 (  (xhtml-match "margin" css-prop)
182                    "")
183                 (  (xhtml-match "outline" css-prop)
184                    "outline_")
185                 (  (equal "padding" css-prop)
186                    "")
187                 (  (xhtml-match "padding" css-prop)
188                    "padding_")
189                 (  (member css-prop '("bottom" "clip" "left" "overflow" "right" "top"
190                                       "vertical-align" "z-index"))
191                    "pos_")
192                 (  (member css-prop '("border-collapse"))
193                    "tab_")
194                 (  (member css-prop '("color" "direction" "letter-spacing" "text-align"
195                                       "text-decoration" "text-indent" "text-transform"
196                                       "white-space" "word-spacing"))
197                    "text_")
198                 (  t ""))
199                css-prop ".asp"))
200            ;; (  (equal refurl "http://learningforlife.fsu.edu/")
201            ;;    (let ((css-prop2 css-prop)
202            ;;          (cc)
203            ;;          (ii 0))
204            ;;      (while (< ii (length css-prop))
205            ;;        (setq cc (substring css-prop2 ii (1+ ii)))
206            ;;        (when (equal cc "-")
207            ;;          (store-substring css-prop2 ii "_"))
208            ;;        (setq ii (1+ ii)))
209            ;;      (concat
210            ;;       refurl "webmaster/references/css/" css-prop2 ".cfm")))
211            (  (equal refurl "http://www.w3.org/")
212               (let ((properties "")
213                     (prop-def ""))
214                 (concat
215                  refurl "TR/REC-CSS2/"
216                  (cond
217                   (  (xhtml-match-member css-prop '("margin" "padding" "border"))
218                      "box.html#propdef-")
219                   (  (member css-prop '("display" "position"
220                                         "top" "right" "bottom" "left"
221                                         "float" "clear"
222                                         "z-index"
223                                         "direction" "unicode-bidi"))
224                      "visuren.html#propdef-")
225                   (  (member css-prop '("width" "min-width" "max-width"
226                                         "height" "min-height" "max-height"
227                                         "line-height" "vertical-align"))
228                      "visudet.html#propdef-")
229                   (  (member css-prop '("overflow" "clip" "visibility"))
230                      "visufx.html#propdef-")
231                   (  (member css-prop '("content" "quotes"))
232                      "generate.html#propdef-")
233                   (  (or (xhtml-match css-prop "counter")
234                          (member css-prop '("marker-offset")))
235                      (setq css-prop "")
236                      "generate.html#counters")
237                   (  (xhtml-match-member css-prop '("list"))
238                      "generate.html#propdef-")
239                   (  (member css-prop '("size" "marks"
240                                         "page-break-before" "page-break-after" "page-break-inside"
241                                         "page"
242                                         "orphans" "widows"
243                                         ))
244                      "page.html#propdef-")
245                   (  (member css-prop '("color"
246                                         "background-color" "background-image" "background-repeat"
247                                         "background-attachment" "background-position" "background"
248                                         ))
249                      "colors.html#propdef-")
250                   (  (xhtml-match "font" css-prop)
251                      "fonts.html#propdef-")
252                   (  (xhtml-match "text" css-prop)
253                      "text.html#")
254                   (  (member css-prop '("letter-spacing" "word-spacing"))
255                      "text.html#")
256                   ;; Fix-me: tables???
257                   (  (member css-prop '("cursor"))
258                      "ui.html#propdef-")
259                   (  (xhtml-match "outline" css-prop)
260                      "ui.html#dynamic-outlines")
261                   (  (or (xhtml-match "speak" css-prop)
262                          (xhtml-match "pause" css-prop)
263                          (xhtml-match "cue" css-prop)
264                          (xhtml-match "pitch" css-prop)
265                          (member css-prop '("volume" "play-during" "azimuth" "elevation"
266                                             "speech-rate" "voice-family" "richness")))
267                      "aural.html#propdef-")
268                   )
269                  css-prop
270                  )))
271            (  t (error "Bad value for xhtml-help-refurl: %s" refurl)))))
272     (browse-url url)))
273
274
275
276
277
278 (defun xhtml-help-browse-tag (tag)
279   (let* ((refurl (if xhtml-help-query-refurl
280                      (xhtml-help-query-refurl (concat "XHTML tag '" tag "'")
281                                               (list "http://www.w3.org/"))
282                    xhtml-help-refurl))
283          (url
284           (cond
285            (  (equal refurl "http://xhtml.com/")
286               (concat
287                refurl "en/xhtml/reference/"
288                tag
289                "/")
290               )
291            (  (equal refurl "http://www.w3schools.com/")
292               (concat
293                refurl "tags/"
294                (cond
295                 (  (member tag '("tt" "i" "b" "big" "small"))
296                    "tag_font_style.asp")
297                 (  (member tag '("em" "strong" "dfn" "code" "samp" "kbd" "var" "cite"))
298                    "tag_phrase_elements.asp")
299                 (  (member tag '("h1" "h2" "h3" "h4" "h5" "h6"))
300                    "tag_hn.asp")
301                 (  (member tag '("sub" "sup"))
302                    "tag_sup.asp")
303                 (  t
304                    (concat "tag_" tag ".asp")
305                    ))))
306            ;; (  (equal refurl "http://learningforlife.fsu.edu/")
307            ;;     (concat
308            ;;      refurl "webmaster/references/xhtml/tags/"
309            ;;      (cond
310            ;;       (   (member tag '("body" "head" "html" "title"))
311            ;;           "structure/")
312            ;;       (   (member tag '("abbr" "acronym" "address" "blockquote" "br" "cite"
313            ;;                         "code" "dfn" "div" "em" "h1" "h2" "h3" "h4" "h5" "h6"
314            ;;                         "kbd" "p" "pre" "q" "samp" "span" "strong" "var"))
315            ;;           "text/")
316            ;;       (   (member tag '("a"))
317            ;;           "hypertext/")
318            ;;       (   (member tag '("dl" "dd" "dt" "ol" "ul" "li"))
319            ;;           "list/")
320            ;;       (   (member tag '("object" "param"))
321            ;;           "object/")
322            ;;       (   (member tag '("b" "big" "hr" "i" "small" "sub" "sup" "tt"))
323            ;;           "presentation/")
324            ;;       (   (member tag '("del" "ins"))
325            ;;           "edit/")
326            ;;       (   (member tag '("bdo"))
327            ;;           "bidirectional/")
328            ;;       (   (member tag '("button" "fieldset" "form" "input" "label" "legend"
329            ;;                         "select" "optgroup" "option" "textarea"))
330            ;;           "forms/")
331            ;;       (   (member tag '("caption" "col" "colgroup" "table" "tbody" "td"
332            ;;                         "tfoot" "th" "thead" "tr"))
333            ;;           "table/")
334            ;;       (   (member tag '("img"))
335            ;;           "image/")
336            ;;       (   (member tag '("area" "map"))
337            ;;           "client/")
338            ;;       (   (member tag '("area" "map"))
339            ;;           "client/")
340            ;;       (   (member tag '("meta"))
341            ;;           "meta/")
342            ;;       (   (member tag '("noscript" "script"))
343            ;;           "scripting/")
344            ;;       (   (member tag '("style"))
345            ;;           "stylesheet/")
346            ;;       (   (member tag '("link"))
347            ;;           "link/")
348            ;;       (   (member tag '("base"))
349            ;;           "base/")
350            ;;       (   (member tag '("base"))
351            ;;           "base/")
352            ;;       (   (member tag '("ruby" "rbc" "rtc" "rb" "rt" "rp"))
353            ;;           "ruby/")
354            ;;       )
355            ;;      tag ".cfm"))
356            (  t (error "Bad value for xhtml-help-refurl: %s" refurl))
357            )))
358     (browse-url url)))
359
360 (defconst xhtml-help-mode-keymap
361   (let ((map (make-sparse-keymap "XHTML Help")))
362     (define-key map [menu-bar xh-help] (cons "XHTML Help" (make-sparse-keymap "second")))
363     (define-key map [menu-bar xh-help css-help] '("CSS Help" . xhtml-help-show-css-ref))
364     (define-key map [menu-bar xh-help tag-help] '("XHTML Tag Help" . xhtml-help-show-tag-ref))
365     map))
366
367 (define-minor-mode xhtml-help-mode
368   "Minor mode that adds keys for accessing xhtml and css help."
369   :keymap xhtml-help-mode-keymap)
370
371 (provide 'xhtml-help)
372
373 ;;; xhtml-help.el ends here