yatex

view yahtml.el @ 137:7919fbbb33c0

Add attribute value completion.
author yuuji@gentei.org
date Thu, 24 Jun 2010 16:01:45 +0900
parents ab0c019da44d
children b7b54906ac3b
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; (c) 1994-2010 by HIROSE Yuuji [yuuji(@)yatex.org]
3 ;;; Last modified Thu Jun 24 16:00:42 2010 on firestorm
4 ;;; $Id$
6 (defconst yahtml-revision-number "1.74.2"
7 "Revision number of running yahtml.el")
9 ;;;[Installation]
10 ;;;
11 ;;; First, you have to install YaTeX and make sure it works fine. Then
12 ;;; put these expressions into your ~/.emacs
13 ;;;
14 ;;; (setq auto-mode-alist
15 ;;; (cons (cons "\\.html$" 'yahtml-mode) auto-mode-alist))
16 ;;; (autoload 'yahtml-mode "yahtml" "Yet Another HTML mode" t)
17 ;;; (setq yahtml-www-browser "firefox")
18 ;;; ;Write your favorite browser. But firefox is advantageous.
19 ;;; (setq yahtml-path-url-alist
20 ;;; '(("/home/yuuji/public_html" . "http://www.mynet/~yuuji")
21 ;;; ("/home/staff/yuuji/html" . "http://www.othernet/~yuuji")))
22 ;;; ;Write correspondence alist from ABSOLUTE unix path name to URL path.
23 ;;;
24 ;;;[インストール方法]
25 ;;;
26 ;;; yahtml.el, yatexlib.el, yatexprc.el を load-path の通ったディレクト
27 ;;; リにインストールしてください。その後、以下を参考に ~/.emacs に設定を
28 ;;; 追加して下さい。
29 ;;;
30 ;;; (setq auto-mode-alist
31 ;;; (cons (cons "\\.html$" 'yahtml-mode) auto-mode-alist))
32 ;;; (autoload 'yahtml-mode "yahtml" "Yet Another HTML mode" t)
33 ;;; (setq yahtml-www-browser "firefox")
34 ;;; ;お気に入りのブラウザを書いて下さい。firefoxが便利です。
35 ;;; (setq yahtml-path-url-alist
36 ;;; '(("/home/yuuji/public_html" . "http://www.mynet/~yuuji")
37 ;;; ("/home/staff/yuuji/html" . "http://www.othernet/~yuuji")))
38 ;;; ;UNIXの絶対パスと対応するURLのリストを書いて下さい。
39 ;;;
40 ;;; HTMLファイル漢字コードが正しく判別されるようにホームディレクトリに
41 ;;; .htaccess ファイルを作り以下のどれか1行を選んで書いて下さい。
42 ;;;
43 ;;; AddType "text/html; charset=Shift_JIS" .html (SJISの場合)
44 ;;; AddType "text/html; charset=iso2022-jp" .html (JISの場合)
45 ;;; AddType "text/html; charset=EUC-JP" .html (EUCの場合)
46 ;;; AddType "text/html; charset=utf-8" .html (UTF-8の場合)
47 ;;;
48 ;;; .htaccess が作れない場合は
49 ;;; (setq yahtml-kanji-code 2)
50 ;;; ;HTMLファイルの漢字コードを変更する場合は
51 ;;; ;1=SJIS、2=JIS、3=EUC 4=UTF-8
52 ;;; ;で設定して下さい。デフォルトは 2 です。
53 ;;;
54 ;;; を適切に書き換えて ~/.emacs に足して下さい。
55 ;;;
56 ;;;[Commentary]
57 ;;;
58 ;;; It is assumed you are already familiar with YaTeX. The following
59 ;;; completing featureas are available: ([prefix] means `C-c' by default)
60 ;;;
61 ;;; * [prefix] b X Complete environments such as `H1' which
62 ;;; normally requires closing tag `</H1>
63 ;;; <a href=foo> ... </a> is also classified into
64 ;;; this group
65 ;;; When input `href=...', you can complete file
66 ;;; name or label(href="#foo") by typing TAB.
67 ;;; * [prefix] l Complete typeface-changing commands such as
68 ;;; `<i> ... </i>' or `<samp> ... </samp>'
69 ;;; This completion can be used to make in-line
70 ;;; tags which is normally completed with [prefix] b.
71 ;;; * [prefix] s Complete declarative notations such as
72 ;;; `<img src="foo.gif">'
73 ;;; `<input name="var" ...>'
74 ;;; * [prefix] m Complete single commands such as
75 ;;; `<br>' or `<hr> or <li>...'
76 ;;; * [prefix] p Insert <p></p> on the point
77 ;;; * M-RET Intelligent newline; if current TAG is one of
78 ;;; ul, ol, or dl. insert newline and <li> or
79 ;;; <dt> or <dd> suitable for current condition.
80 ;;; * menu-bar yahtml Complete all by selecting a menu item (Though I
81 ;;; hate menu, this is most useful)
82 ;;; * [prefix] g Goto corresponding Tag or HREF such as
83 ;;; <dl> <-> </dl> or href="xxx".
84 ;;; Or invoke image viewer if point is on <img src=...>.
85 ;;; * [prefix] k Kill html tags on the point. If you provide
86 ;;; universal-argument, kill surrounded contents too.
87 ;;; * [prefix] c Change html tags on the point.
88 ;;; When typeing [prefix] c on `href="xxx"', you can
89 ;;; change the reference link with completion.
90 ;;; * [prefix] t j Call weblint on current file.
91 ;;; * [prefix] t p View current html with WWW browser
92 ;;; (To activate this, never fail to set the lisp
93 ;;; variable yahtml-www-browser. Recommended value
94 ;;; is "firefox")
95 ;;; * [prefix] a YaTeX's accent mark's equivalent of yahtml.
96 ;;; This function can input $lt, $gt or so.
97 ;;; * [prefix] ; Translate chars of `>', `<', `&', and `"' to
98 ;;; `&gt;', `&lt;', `&amp;', `&quot;' respectively
99 ;;; in the region.
100 ;;; * [prefix] : Do translation opposite to above, in the region.
101 ;;; * [prefix] # Translate unsafe-chars and unreserved-chars to
102 ;;; URLencoded string in the region.
103 ;;;
104 ;;;[キーの説明]
105 ;;;
106 ;;; 以下の説明において、特にカスタマイズをしていない限り、[prefix] は
107 ;;; C-c キーを意味します。
108 ;;;
109 ;;; * [prefix] b X `</H1>' といった終了タグが必要となる`H1'のよう
110 ;;; な環境を補完入力します。<a href=foo> ... </a>
111 ;;; もこのグループです。
112 ;;; `href=...' と入力した後、TABキーを押すことで、
113 ;;; ファイル名や (href="#foo") のようなラベルも補完
114 ;;; できます。
115 ;;; * [prefix] s 以下のような宣言の補完を行います。
116 ;;; `<img src="foo.gif">'
117 ;;; `<input name="var" ...>'
118 ;;; * [prefix] l `<i> ... </i>' や `<samp> ... </samp>' のよう
119 ;;; なテキストスタイル指定のタグを補完します。
120 ;;; この補完機能は通常 [prefix] b で補完できるものを
121 ;;; 一行内で書きたいときにも用いることが出来ます。
122 ;;; * [prefix] m `<br>' や `<hr> '、`<li>' 等の単体タグの補完
123 ;;; を行います。
124 ;;; * [prefix] p カーソル位置に<p></p>を挿入します。
125 ;;; * M-RET おまかせ改行; もしul、ol、dl等のタグ(リスト)を
126 ;;; 使っている場合に、環境に合わせて改行と <li>、
127 ;;; <dt>、<dd>を入力します。
128 ;;; * menu-bar yahtml 選択したアイテムをメニューより補完できます。
129 ;;; (私はメニューが嫌いなんですが、htmlに関してはメ
130 ;;; ニューは一番ありがたいかも)
131 ;;; * [prefix] g 対応するタグ、<dl> <-> </dl> や href="xxx" の
132 ;;; ような TAG にジャンプします。
133 ;;; <img src=...> の場合はイメージビューワを呼び出
134 ;;; します。href=hoge.html の場合はhoge.htmlに飛びま
135 ;;; す。
136 ;;; * [prefix] k ポイント上の HTML タグを消去します。
137 ;;; もし universal-argument を付けた場合(C-uを先に押
138 ;;; す)HTMLタグで囲まれた内容も同時に消去します。
139 ;;; * [prefix] c ポイント上のタグを変更します。
140 ;;; `href="xxx"'の上で [prefix] c を利用した場合は、
141 ;;; 参照しているリンクを補完機能を使いながら変更で
142 ;;; きます。
143 ;;; * [prefix] t j カレントファイルに対して jweblint を呼び出しま
144 ;;; す。
145 ;;; * [prefix] t p WWW ブラウザでカレントファイルを表示します。
146 ;;; (lisp変数 yahtml-www-browser の設定をお忘れな
147 ;;; く。お推めは "firefox" です)
148 ;;; * [prefix] a YaTeX のアクセント記号補完と同じです。
149 ;;; &lt; &gt; 等が入力できます。
150 ;;; * [prefix] ; 指定したリジョン中の > < & " をそれぞれ
151 ;;; &gt; &lt; &amp; &quot; に変換します。
152 ;;; * [prefix] : 指定したリジョン中で上と逆の変換をします。
153 ;;; * [prefix] # 指定したリジョン中で%エンコードの必要な文字が
154 ;;; あればそれらをエンコードします。
155 ;;; * [prefix] ESC yahtml-mode を抜け yahtml-mode に入る前に動作し
156 ;;; ていたメジャーモードに戻ります。
157 ;;;
158 ;;; [謝辞]
159 ;;;
160 ;;; fj野鳥の会の皆さんには貴重な助言を頂きました。また、下に示す方々には
161 ;;; 特に大きな協力を頂きました。あわせてここに感謝申し上げます。
162 ;;;
163 ;;; * 横田和也さん(マツダ)
164 ;;; マニュアルの和訳をして頂きました。
165 ;;; * 吉田尚志さん(NTT Data)
166 ;;; Mule for Win32 での動作のさせ方を教えて頂きました。
167 ;;; (というかほとんどやってもらった ^^;)
168 ;;;
171 (require 'yatexlib)
172 ;;; --- customizable variable starts here ---
173 (defvar yahtml-prefix "\C-c"
174 "*Prefix key stroke of yahtml functions.")
175 (defvar yahtml-image-viewer "display" "*Image viewer program")
176 (defvar yahtml-www-browser "firefox" "*WWW Browser command")
177 (defvar yahtml-kanji-code 2
178 "*Kanji coding system number of html file; 1=sjis, 2=jis, 3=euc, 4=UTF-8")
179 ;;(defvar yahtml-coding-system
180 ;; (cdr (assq yahtml-kanji-code YaTeX-kanji-code-alist))
181 ;; "Kanji coding system")
182 (and (featurep 'mule)
183 (integerp yahtml-kanji-code)
184 (setq yahtml-kanji-code
185 (cdr (assq yahtml-kanji-code YaTeX-kanji-code-alist))))
187 (defvar yahtml-fill-column 72 "*fill culumn used for yahtml-mode")
188 (defvar yahtml-fill-prefix nil "*fill prefix for yahtml-mode")
190 ;;(defvar yahtml-www-server "www" "*Host name of your domain's WWW server")
191 (defvar yahtml-path-url-alist nil
192 "*Alist of unix path name vs. URL name of WWW server.
193 Ex.
194 '((\"/usr/home/yuuji/http\" . \"http://www.comp.ae.keio.ac.jp/~yuuji\")
195 (\"/home/yuuji/http\" . \"http://www.gentei.org/~yuuji\"))")
196 (defvar yahtml-directory-index "index.html"
197 "*Directory index file name;
198 Consult your site's WWW administrator.")
200 (defvar yahtml-environment-indent 1
201 "*Indentation depth of HTML's listing environment")
203 ;; YaTeX-japan is defined in yatexlib.el
204 (defvar yahtml-lint-program (if YaTeX-japan "jweblint" "weblint")
205 "*Program name to lint HTML file")
206 (defvar yahtml-hate-too-deep-indentation nil
207 "*Non-nil for this variable suppress deep indentation in listing environments.")
209 (defvar yahtml-always-/p t
210 "*Those who always use <p> with </p> set this to t.")
211 (defvar yahtml-always-/li nil
212 "*Those who always use <li> with </li> set this to t.")
213 (defvar yahtml-always-/dt nil
214 "*Those who always use <dt> with </dt> set this to t.")
215 (defvar yahtml-always-/dd nil
216 "*Those who always use <dd> with </dd> set this to t.")
218 (defvar yahtml-p-prefered-env-regexp "^\\(body\\|dl\\|blockquote\\)"
219 "*Regexp of envs where paragraphed sentences are prefered.")
221 (defvar yahtml-template-file "~/public_html/template.html"
222 "*Template HTML file. It'll be inserted to empty file.")
224 (defvar yahtml-prefer-upcases nil
225 "*Non-nil for preferring upcase TAGs")
227 (defvar yahtml-prefer-upcase-attributes nil
228 "*Non-nil for preferring upcase attributes")
230 (defvar yahtml-server-type 'apache "*WWW server program type")
232 (defvar yahtml-apache-access-file ".htaccess"
233 "*Server access file name for apache")
235 (defvar yahtml-use-css t "*Use stylesheet or not")
237 (defvar yahtml-image-inspection-bytes 50000
238 "*Number of bytes to inspect the image for geometry information")
239 (defvar yahtml:img-default-alt-format "%xx%y(%sbytes)"
240 "*Default format of img entity's ALT attributes.
241 %x: width, %y: height, %s: size in bytes, %c: first comment string,
242 %f: filename")
244 (defvar yahtml-faithful-to-htmllint nil)
245 (defvar yahtml-error-line-regexp
246 "^\\(.*\\)(\\([0-9]+\\)):\\|^line \\([0-9]+\\)"
247 "*Regexp of error position which is produced by lint program.")
249 (defvar yahtml-translate-hyphens-when-comment-region t
250 "*Non-nil for translate hyphens to &#45; when comment-region")
251 (defvar yahtml-escape-chars 'ask
252 "*Escape reserved characters to URL-encoding or not.
253 Nil for never, t for everytime, and 'ask for inquiring
254 at each reserved chars.")
256 (defvar yahtml-use-font-lock (and (featurep 'font-lock)
257 (fboundp 'font-lock-fontify-region))
258 "*Non-nil means to use font-lock to fontify buffer.")
260 (defvar yahtml-use-hilit19 (and (featurep 'hilit19)
261 (not yahtml-use-font-lock))
262 "*Non-nil means to Use hilit19 to highlight buffer")
264 (defvar yahtml-mode-abbrev-table nil
265 "*Abbrev table in use in yahtml-mode buffers.")
266 (define-abbrev-table 'yahtml-mode-abbrev-table ())
268 (defvar yahtml-indentation-boundary "^\\s *<h[1-3]>"
269 "*Boundary regexp for indentation calculation.")
271 (defvar yahtml-html4-strict t
272 "*Non-nil means editing HTML 4.01 Strict.
273 Completing read for obsoleted attributes disabled.")
275 ;;; --- customizable variable ends here ---
276 (defvar yahtml-prefix-map nil)
277 (defvar yahtml-mode-map nil "Keymap used in yahtml-mode.")
278 (defvar yahtml-lint-buffer-map nil "Keymap used in lint buffer.")
279 (defvar yahtml-shell-command-option
280 (or (and (boundp 'shell-command-option) shell-command-option)
281 (if (eq system-type 'ms-dos) "/c" "-c")))
282 (defvar yahtml-use-highlighting (or yahtml-use-font-lock yahtml-use-hilit19))
284 (defun yahtml-define-begend-key-normal (key env &optional map)
285 "Define short cut yahtml-insert-begend key."
286 (YaTeX-define-key
287 key
288 (list 'lambda '(arg) '(interactive "P")
289 (list 'yahtml-insert-begend 'arg env))
290 map))
292 (defun yahtml-define-begend-region-key (key env &optional map)
293 "Define short cut yahtml-insert-begend-region key."
294 (YaTeX-define-key key (list 'lambda nil '(interactive)
295 (list 'yahtml-insert-begend t env)) map))
297 (defun yahtml-define-begend-key (key env &optional map)
298 "Define short cut key for begin type completion both for
299 normal and region mode. To customize yahtml, user should use this function."
300 (yahtml-define-begend-key-normal key env map)
301 (if YaTeX-inhibit-prefix-letter nil
302 (yahtml-define-begend-region-key
303 (concat (upcase (substring key 0 1)) (substring key 1)) env map)))
305 (if yahtml-mode-map nil
306 (setq yahtml-mode-map (make-sparse-keymap)
307 yahtml-prefix-map (make-sparse-keymap))
308 (define-key yahtml-mode-map yahtml-prefix yahtml-prefix-map)
309 (define-key yahtml-mode-map "\M-\C-@" 'yahtml-mark-begend)
310 (if (and (boundp 'window-system) (eq window-system 'x) YaTeX-emacs-19)
311 (define-key yahtml-mode-map [?\M-\C- ] 'yahtml-mark-begend))
312 (define-key yahtml-mode-map "\M-\C-a" 'YaTeX-beginning-of-environment)
313 (define-key yahtml-mode-map "\M-\C-e" 'YaTeX-end-of-environment)
314 (define-key yahtml-mode-map "\M-\C-m" 'yahtml-intelligent-newline)
315 (define-key yahtml-mode-map "\M-\C-j" 'yahtml-intelligent-newline)
316 (define-key yahtml-mode-map "\C-i" 'yahtml-indent-line)
317 (define-key yahtml-mode-map "&" 'yahtml-insert-amps)
318 (let ((map yahtml-prefix-map))
319 (YaTeX-define-key "^" 'yahtml-visit-main map)
320 (YaTeX-define-key "4^" 'yahtml-visit-main-other-window map)
321 (YaTeX-define-key "4g" 'yahtml-goto-corresponding-*-other-window map)
322 (YaTeX-define-key "44" 'YaTeX-switch-to-window map)
323 (and YaTeX-emacs-19 window-system
324 (progn
325 (YaTeX-define-key "5^" 'yahtml-visit-main-other-frame map)
326 (YaTeX-define-key "5g" 'yahtml-goto-corresponding-*-other-frame map)
327 (YaTeX-define-key "55" 'YaTeX-switch-to-window map)))
328 (YaTeX-define-key "v" 'yahtml-version map)
329 (YaTeX-define-key "s" 'yahtml-insert-form map)
330 (YaTeX-define-key "l" 'yahtml-insert-tag map)
331 (YaTeX-define-key "L" 'yahtml-insert-tag-region map)
332 (YaTeX-define-key "m" 'yahtml-insert-single map)
333 (YaTeX-define-key "n" '(lambda () (interactive) (insert (if yahtml-prefer-upcases "<BR>" "<br>"))) map)
334 (YaTeX-define-key "-" '(lambda () (interactive) (insert (if yahtml-prefer-upcases "<HR>" "<hr>") "\n")) map)
335 (YaTeX-define-key "p" 'yahtml-insert-p map)
336 (if YaTeX-no-begend-shortcut
337 (progn
338 (YaTeX-define-key "B" 'yahtml-insert-begend-region map)
339 (YaTeX-define-key "b" 'yahtml-insert-begend map))
340 (yahtml-define-begend-key "bh" "html" map)
341 (yahtml-define-begend-key "bH" "head" map)
342 (yahtml-define-begend-key "bt" "title" map)
343 (yahtml-define-begend-key "bT" "table" map)
344 (yahtml-define-begend-key "bb" "body" map)
345 (yahtml-define-begend-key "bc" "center" map)
346 (yahtml-define-begend-key "bd" "dl" map)
347 (yahtml-define-begend-key "bu" "ul" map)
348 (yahtml-define-begend-key "bo" "ol" map)
349 (yahtml-define-begend-key "b1" "h1" map)
350 (yahtml-define-begend-key "b2" "h2" map)
351 (yahtml-define-begend-key "b3" "h3" map)
352 (yahtml-define-begend-key "ba" "a" map)
353 (yahtml-define-begend-key "bf" "form" map)
354 (yahtml-define-begend-key "bs" "select" map)
355 (yahtml-define-begend-key "bv" "div" map)
356 (yahtml-define-begend-key "bS" "span" map)
357 (yahtml-define-begend-key "bp" "pre" map)
358 (YaTeX-define-key "b " 'yahtml-insert-begend map)
359 (YaTeX-define-key "B " 'yahtml-insert-begend-region map))
360 (YaTeX-define-key "e" 'YaTeX-end-environment map)
361 (YaTeX-define-key ">" 'yahtml-comment-region map)
362 (YaTeX-define-key "<" 'yahtml-uncomment-region map)
363 (YaTeX-define-key "g" 'yahtml-goto-corresponding-* map)
364 (YaTeX-define-key "k" 'yahtml-kill-* map)
365 (YaTeX-define-key "c" 'yahtml-change-* map)
366 (YaTeX-define-key "t" 'yahtml-browse-menu map)
367 (YaTeX-define-key "a" 'yahtml-complete-mark map)
368 (YaTeX-define-key "'" 'yahtml-prev-error map)
369 (YaTeX-define-key ";" 'yahtml-translate-region map)
370 (YaTeX-define-key ":" 'yahtml-translate-reverse-region map)
371 (YaTeX-define-key "#" 'yahtml-escape-chars-region map)
372 ;;;;;(YaTeX-define-key "i" 'yahtml-fill-item map)
373 (YaTeX-define-key "\e" 'yahtml-quit map))
374 (substitute-all-key-definition
375 'fill-paragraph 'yahtml-fill-paragraph yahtml-mode-map)
376 (substitute-all-key-definition
377 'kill-buffer 'YaTeX-kill-buffer yahtml-mode-map))
379 (if yahtml-lint-buffer-map nil
380 (setq yahtml-lint-buffer-map (make-keymap))
381 (define-key yahtml-lint-buffer-map " " 'yahtml-jump-to-error-line))
384 (defvar yahtml-paragraph-start
385 (concat
386 "^$\\|<!--\\|^[ \t]*</?\\(h[1-6]\\|p\\|d[ldt]\\|[bhtd][rdh]\\|li\\|body\\|html\\|head\\|title\\|ul\\|ol\\|dl\\|pre\\|table\\|center\\|blockquote\\)\\b")
387 "*Regexp of html paragraph separater")
388 (defvar yahtml-paragraph-separate
389 (concat
390 "^$\\|<!--\\|^[ \t]*</?\\(h[1-6]\\|p\\|[bhtd][ldt]\\|li\\|body\\|html\\|head\\|title\\|ul\\|ol\\|dl\\|pre\\|table\\|center\\|blockquote\\|!--\\)\\b")
391 "*Regexp of html paragraph separater")
392 (defvar yahtml-syntax-table nil
393 "*Syntax table for yahtml-mode")
395 (if yahtml-syntax-table nil
396 (setq yahtml-syntax-table
397 (make-syntax-table (standard-syntax-table)))
398 (modify-syntax-entry ?\< "(>" yahtml-syntax-table)
399 (modify-syntax-entry ?\> ")<" yahtml-syntax-table)
400 (modify-syntax-entry ?\n " " yahtml-syntax-table))
402 (defvar yahtml-command-regexp "[A-Za-z0-9]+"
403 "Regexp of constituent of html commands.")
405 ;;; Completion tables for `form'
406 (defvar yahtml-form-table
407 '(("img") ("input") ("link") ("meta")))
408 (defvar yahtml-user-form-table nil)
409 (defvar yahtml-tmp-form-table nil)
410 (defvar yahtml-last-form "img")
412 (defvar yahtml-env-table
413 '(("html") ("head") ("title") ("body") ("dl") ("ul") ("ol") ("pre")
414 ("a") ("form") ("select") ("center") ("textarea") ("blockquote")
415 ("OrderedList" . "ol")
416 ("UnorderedList" . "ul")
417 ("DefinitionList" . "dl")
418 ("Preformatted" . "pre")
419 ("table") ("thead") ("tbody") ("tfoot") ("tr") ("th") ("td")
420 ("address") ("button")
421 ("h1") ("h2") ("h3") ("h4") ("h5") ("h6")
422 ;; ("p") ;This makes indentation screwed up!
423 ("style") ("script") ("noscript") ("div") ("object") ("ins") ("del")
424 ))
426 (if yahtml-html4-strict
427 (setq yahtml-env-table
428 (delete (assoc "center" yahtml-env-table) yahtml-env-table)))
430 ;(defvar yahtml-itemizing-regexp
431 ; "\\(ul\\|ol\\|dl\\)"
432 ; "Regexp of itemizing forms")
434 (defvar yahtml-user-env-table nil)
435 (defvar yahtml-tmp-env-table nil)
437 ;;; Completion tables for typeface designator
438 (and yahtml-always-/p
439 (or (assoc "p" yahtml-env-table)
440 (setq yahtml-env-table (cons '("p") yahtml-env-table))))
441 (and yahtml-always-/li
442 (or (assoc "li" yahtml-env-table)
443 (setq yahtml-env-table (cons '("li") yahtml-env-table))))
444 (and yahtml-always-/dt
445 (or (assoc "dt" yahtml-env-table)
446 (setq yahtml-env-table (cons '("dt") yahtml-env-table))))
447 (and yahtml-always-/dd
448 (or (assoc "dd" yahtml-env-table)
449 (setq yahtml-env-table (cons '("dd") yahtml-env-table))))
451 (defvar yahtml-typeface-table
452 (append
453 '(("dfn") ("em") ("cite") ("code") ("kbd") ("samp") ("caption")
454 ("strong") ("var") ("b") ("i") ("tt") ("big") ("small")
455 ("sup") ("sub") ("span") ("abbr"))
456 (if (not yahtml-html4-strict)
457 '(("strike") ("s") ("u") ("font")))
458 yahtml-env-table)
459 "Default completion table of typeface designator")
460 (defvar yahtml-user-typeface-table nil)
461 (defvar yahtml-tmp-typeface-table nil)
462 (defvar yahtml-last-typeface-cmd "a")
464 (defvar yahtml-single-cmd-table
465 '(("hr") ("br") ("option")
466 ("HorizontalRule" . "hr")
467 ("BreakLine" . "br")
468 ("exec" . "!--#exec")
469 ("!--#exec")
470 ("include" . "!--#include")
471 ("!--#include")
472 ;; ("Item" . "li")
473 ;; ("DefineTerm" . "dt")
474 ;; ("Description" . "dd")
475 ;; ("dd") ("dt") ("li")
476 )
477 "Default completion table of HTML single command.")
478 (defvar yahtml-user-single-cmd-table nil)
479 (defvar yahtml-tmp-single-cmd-table nil)
480 (defvar yahtml-last-single-cmd nil)
482 (defvar yahtml-current-completion-type nil
483 "Has current completion type. This may be used in yahtml addin functions.")
485 (defvar yahtml-struct-name-regexp
486 (concat
487 "\\<\\("
488 ;(mapconcat 'car yahtml-typeface-table "\\|")
489 (mapconcat 'car yahtml-env-table "\\|")
490 "\\)\\b")
491 "Regexp of structure beginning.")
493 (defvar yahtml-closable-regexp
494 (concat
495 "\\<\\("
496 (mapconcat 'car yahtml-typeface-table "\\|")
497 (mapconcat 'car yahtml-env-table "\\|")
498 "\\)\\b")
499 "Regexp of any closable elemnts.")
501 (defvar yahtml-indent-listing-constant t
502 "*Nil means indentation for listing obeys the column of `>'.
503 T for static indentation depth")
505 (or (assoc "p" yahtml-env-table)
506 (setq yahtml-env-table (cons '("p") yahtml-env-table)))
509 (defun yahtml-get-user-httpconf-entry (regexp)
510 (cond
511 ((and (eq yahtml-server-type 'apache) ;;check .htaccess
512 buffer-file-name)
513 (let ((dir default-directory)
514 charset af ext (ldir "")
515 line
516 (case-fold-search t)
517 (uid (car (cdr (cdr (file-attributes "."))))))
518 (if (string-match "^[A-Z]:" dir)
519 (setq dir (substring dir 2))) ;remove drive letter
520 (while (and dir
521 (not (string= dir ldir))
522 (equal uid (car (cdr (cdr (file-attributes dir))))))
523 (setq af (expand-file-name yahtml-apache-access-file dir))
524 (if (file-exists-p af)
525 (save-excursion
526 (set-buffer (find-file-noselect af))
527 (save-excursion
528 (goto-char (point-min))
529 (if (re-search-forward regexp nil t)
530 (setq line (buffer-substring
531 (point-beginning-of-line)
532 (point-end-of-line))
533 dir nil)))
534 (kill-buffer (current-buffer))))
535 (if dir
536 (setq ldir dir
537 dir (substring dir 0 (string-match "/$" dir))
538 dir (file-name-directory dir))))
539 line))
540 (t nil)))
542 (defun yahtml-dir-default-charset ()
543 (let*((fn (file-name-nondirectory (or buffer-file-name "")))
544 (ext (substring fn (or (string-match "\\.[a-z0-9]+$" fn) 0)))
545 (ptn (format "^\\s *AddType.*charset=\\(.*\\)\\%s$" ext))
546 (case-fold-search t)
547 line
548 charset)
549 (if (setq line (yahtml-get-user-httpconf-entry ptn))
550 (progn
551 (string-match ptn line)
552 (setq charset
553 (substring line (match-beginning 1) (match-end 1)))
554 (cond
555 ((string-match "iso-2022-jp" charset)
556 (setq charset 2))
557 ((string-match "euc-jp" charset)
558 (setq charset 3))
559 ((string-match "shift_jis" charset)
560 (setq charset 1))
561 ((string-match "utf-8" charset)
562 (setq charset 4))
563 (t (setq charset nil)))
564 (setq dir "")))
565 (if (featurep 'mule)
566 (setq charset (cdr (assq charset YaTeX-kanji-code-alist))))
567 charset))
569 (defun yahtml-get-directory-index ()
570 (let ((line (yahtml-get-user-httpconf-entry "^\\s *DirectoryIndex"))
571 x index-list)
572 ;;s/\\s *$//;
573 (if line
574 (progn
575 (if (string-match "DirectoryIndex\\s +\\(.*\\)\\s *$" line)
576 (setq line (substring line (match-beginning 1) (match-end 1))))
577 (while (string< "" line)
578 (if (setq x (string-match "\\(\\s +\\)" line))
579 (setq index-list (cons (substring line 0 x) index-list)
580 line (substring line (match-end 1)))
581 (setq index-list (cons line index-list)
582 line "")))
583 (or (nreverse index-list)
584 (if (listp yahtml-directory-index)
585 yahtml-directory-index
586 (list yahtml-directory-index)))))))
588 (defvar yahtml-mode-old-mode nil)
589 (defun yahtml-mode ()
590 (interactive)
591 (let ((old-mm major-mode)) ;Emacs21.0.95 resets major-mode
592 (kill-all-local-variables) ;with kill-all-local-variables
593 (if (not (eq 'yahtml-mode old-mm))
594 (set (make-local-variable 'yahtml-mode-old-mode) old-mm)))
595 (let ((coding (or (yahtml-dir-default-charset) yahtml-kanji-code)))
596 (cond
597 ((null coding) nil)
598 ((and YaTeX-emacs-20 (boundp 'buffer-file-coding-system))
599 (setq buffer-file-coding-system
600 (or (and (fboundp 'set-auto-coding) buffer-file-name
601 (save-excursion
602 (goto-char (point-min))
603 (set-auto-coding buffer-file-name (buffer-size))))
604 coding)))
605 ((featurep 'mule)
606 (set-file-coding-system coding))
607 ((boundp 'NEMACS)
608 (make-local-variable 'kanji-fileio-code)
609 (setq kanji-fileio-code coding))))
610 (setq major-mode 'yahtml-mode
611 mode-name "yahtml"
612 YaTeX-current-file-name (file-name-nondirectory
613 (or (buffer-file-name) ""))
614 local-abbrev-table yahtml-mode-abbrev-table)
615 (mapcar
616 (function (lambda (x)
617 (make-local-variable (car x))
618 (set (car x) (if (and (symbolp (cdr x))
619 (boundp (cdr x)))
620 (symbol-value (cdr x))
621 (cdr x)))))
622 '((YaTeX-ec . "")
623 (YaTeX-struct-begin . "<%1%2")
624 (YaTeX-struct-end . "</%1>")
625 (YaTeX-struct-name-regexp . yahtml-closable-regexp)
626 (YaTeX-comment-prefix . "<!--[^#]")
627 (YaTeX-coding-system . yahtml-kanji-code) ;necessary?
628 (YaTeX-typesetting-mode-map . yahtml-lint-buffer-map)
629 (fill-prefix . yahtml-fill-prefix) (fill-column . yahtml-fill-column)
630 (paragraph-start . yahtml-paragraph-start)
631 (paragraph-separate . yahtml-paragraph-separate)
632 (comment-start . "<!-- ") (comment-end . " -->")
633 (comment-start-skip . comment-start)
634 (indent-line-function . yahtml-indent-line)))
636 (if yahtml-use-font-lock
637 (progn
638 (yahtml-font-lock-set-default-keywords)
639 (or (featurep 'xemacs)
640 (progn
641 (set (make-local-variable 'font-lock-defaults)
642 '(yahtml-font-lock-keywords nil t))
643 ;;(font-lock-mode -1)
644 (font-lock-mode 1) ;;Why should I fontify again???
645 ;; in yatex-mode, there's no need to refontify...
646 (font-lock-fontify-buffer)))))
647 (set-syntax-table yahtml-syntax-table)
648 (use-local-map yahtml-mode-map)
649 (YaTeX-read-user-completion-table)
650 (yahtml-css-scan-styles)
651 (turn-on-auto-fill) ;Sorry, this is prerequisite
652 (and (= 0 (buffer-size)) (file-exists-p yahtml-template-file)
653 (y-or-n-p (format "Insert %s?" yahtml-template-file))
654 (insert-file-contents (expand-file-name yahtml-template-file)))
655 (run-hooks 'text-mode-hook 'yahtml-mode-hook)
657 ;; This warning should be removed after a while(2000/12/2)
658 (let ((fld (or (and (local-variable-p 'font-lock-defaults (current-buffer))
659 font-lock-defaults)
660 (get 'yahtml-mode 'font-lock-defaults))))
661 (and fld (not (memq 'yahtml-font-lock-keywords fld))
662 (YaTeX-warning-font-lock "yahtml"))))
664 (defun yahtml-version ()
665 "Return string of the version of running yahtml."
666 (interactive)
667 (message
668 (concat "Yet Another HTML-mode "
669 (if YaTeX-japan "「HTML屋」" "`yahtml'")
670 " Revision "
671 yahtml-revision-number)))
673 (defun yahtml-quit ()
674 (interactive)
675 (and yahtml-mode-old-mode
676 (fboundp yahtml-mode-old-mode)
677 (funcall yahtml-mode-old-mode)))
679 (defun yahtml-define-menu (keymap bindlist)
680 (cond
681 ((featurep 'xemacs)
682 (let ((name (keymap-name (symbol-value keymap))))
683 (set keymap nil)
684 (mapcar
685 (function
686 (lambda (bind)
687 (setq bind (cdr bind))
688 (if (eq (car (cdr bind)) 'lambda)
689 (setcar (cdr bind) 'progn))
690 (if (stringp (car (cdr bind)))
691 (set keymap (cons (cdr bind) (symbol-value keymap)))
692 (set keymap (cons (vector (car bind) (cdr bind) t)
693 (symbol-value keymap))))))
694 bindlist)
695 (set keymap (cons name (symbol-value keymap)))))
696 (t
697 (mapcar
698 (function
699 (lambda (bind)
700 (define-key (symbol-value keymap) (vector (car bind)) (cdr bind))))
701 bindlist))))
703 (defvar yahtml-menu-map nil "Menu map of yahtml")
704 (defvar yahtml-menu-map-sectioning nil "Menu map of yahtml(sectioning)")
705 (defvar yahtml-menu-map-listing nil "Menu map of yahtml(listing)")
706 (defvar yahtml-menu-map-logical nil "Menu map of yahtml(logical tags)")
707 (defvar yahtml-menu-map-typeface nil "Menu map of yahtml(typeface tags)")
709 ;;; Variables for mosaic url history
710 (defvar yahtml-urls nil "Alist of global history")
711 (defvar yahtml-urls-private nil)
712 (defvar yahtml-urls-local nil)
714 (cond
715 ((and YaTeX-emacs-19 (null yahtml-menu-map))
716 (setq yahtml-menu-map (make-sparse-keymap "yahtml"))
717 (setq yahtml-menu-map-sectioning (make-sparse-keymap "sectioning menu"))
718 (YaTeX-define-menu
719 'yahtml-menu-map-sectioning
720 (nreverse
721 '((1 "H1" . (lambda () (interactive) (yahtml-insert-begend nil "H1")))
722 (2 "H2" . (lambda () (interactive) (yahtml-insert-begend nil "H2")))
723 (3 "H3" . (lambda () (interactive) (yahtml-insert-begend nil "H3")))
724 (4 "H4" . (lambda () (interactive) (yahtml-insert-begend nil "H4")))
725 (5 "H5" . (lambda () (interactive) (yahtml-insert-begend nil "H5")))
726 (6 "H6" . (lambda () (interactive) (yahtml-insert-begend nil "H6")))
727 )))
728 (setq yahtml-menu-map-logical (make-sparse-keymap "logical tags"))
729 (YaTeX-define-menu
730 'yahtml-menu-map-logical
731 (nreverse
732 '((em "Embolden" .
733 (lambda () (interactive) (yahtml-insert-tag nil "EM")))
734 (dfn "Define a word" .
735 (lambda () (interactive) (yahtml-insert-tag nil "DFN")))
736 (cite "Citation" .
737 (lambda () (interactive) (yahtml-insert-tag nil "CITE")))
738 (code "Code" .
739 (lambda () (interactive) (yahtml-insert-tag nil "CODE")))
740 (kbd "Keyboard" .
741 (lambda () (interactive) (yahtml-insert-tag nil "KBD")))
742 (samp "Sample display" .
743 (lambda () (interactive) (yahtml-insert-tag nil "SAMP")))
744 (strong "Strong" .
745 (lambda () (interactive) (yahtml-insert-tag nil "STRONG")))
746 (VAR "Variable notation" .
747 (lambda () (interactive) (yahtml-insert-tag nil "VAR"))))))
748 (setq yahtml-menu-map-typeface (make-sparse-keymap "typeface tags"))
749 (YaTeX-define-menu
750 'yahtml-menu-map-typeface
751 (nreverse
752 '((b "Bold" .
753 (lambda () (interactive) (yahtml-insert-tag nil "B")))
754 (i "Italic" .
755 (lambda () (interactive) (yahtml-insert-tag nil "I")))
756 (tt "Typewriter" .
757 (lambda () (interactive) (yahtml-insert-tag nil "TT")))
758 (u "Underlined" .
759 (lambda () (interactive) (yahtml-insert-tag nil "U"))))))
760 (setq yahtml-menu-map-listing (make-sparse-keymap "listing"))
761 (YaTeX-define-menu
762 'yahtml-menu-map-listing
763 (nreverse
764 '((ul "Unordered" .
765 (lambda () (interactive) (yahtml-insert-begend nil "UL")))
766 (ol "Ordered" .
767 (lambda () (interactive) (yahtml-insert-begend nil "OL")))
768 (dl "Definition" .
769 (lambda () (interactive) (yahtml-insert-begend nil "DL"))))))
770 (setq yahtml-menu-map-item (make-sparse-keymap "item"))
771 (YaTeX-define-menu
772 'yahtml-menu-map-item
773 (nreverse
774 '((li "Simple item" .
775 (lambda () (interactive) (yahtml-insert-single "li")))
776 (dt "Define term" .
777 (lambda () (interactive) (yahtml-insert-single "dt")))
778 (dd "Description of term" .
779 (lambda () (interactive) (yahtml-insert-single "dd"))))))
780 (define-key yahtml-mode-map [menu-bar yahtml]
781 (cons "yahtml" yahtml-menu-map))
782 (YaTeX-define-menu
783 'yahtml-menu-map
784 (nreverse
785 (list
786 (cons (list 'sect "Sectioning")
787 (cons "sectioning" yahtml-menu-map-sectioning))
788 (cons (list 'list "Listing")
789 (cons "Listing" yahtml-menu-map-listing))
790 (cons (list 'item "Item")
791 (cons "Itemizing" yahtml-menu-map-item));;;
792 (cons (list 'logi "Logical tags")
793 (cons "logical" yahtml-menu-map-logical))
794 (cons (list 'type "Typeface tags")
795 (cons "typeface" yahtml-menu-map-typeface)))))
796 (if (featurep 'xemacs)
797 (add-hook 'yahtml-mode-hook
798 '(lambda ()
799 (or (assoc "yahtml" current-menubar)
800 (progn
801 (set-buffer-menubar (copy-sequence current-menubar))
802 (add-submenu nil yahtml-menu-map))))))))
804 ;;; ----------- Completion ----------
805 (defvar yahtml-last-begend "html")
806 (defun yahtml-insert-begend (&optional region env)
807 "Insert <cmd> ... </cmd>."
808 (interactive "P")
809 (setq yahtml-current-completion-type 'multiline)
810 (let*((completion-ignore-case t)
811 (cmd
812 (or env
813 (YaTeX-cplread-with-learning
814 (format "Environment(default %s): " yahtml-last-begend)
815 'yahtml-env-table 'yahtml-user-env-table 'yahtml-tmp-env-table)))
816 (bolp (save-excursion
817 (skip-chars-backward " \t" (point-beginning-of-line)) (bolp)))
818 (cc (current-column)))
819 (if (string< "" cmd) (setq yahtml-last-begend cmd))
820 (setq yahtml-last-begend
821 (or (cdr (assoc yahtml-last-begend yahtml-env-table))
822 yahtml-last-begend))
823 (setq cmd yahtml-last-begend)
824 (if yahtml-prefer-upcases (setq cmd (upcase cmd)))
825 (if region
826 ;; We want to keep region effective for new tagged environment
827 ;; to enable continuous regioning by another environment
828 (let ((beg (region-beginning))
829 (end (region-end))
830 (addin (yahtml-addin cmd)))
831 (save-excursion
832 (goto-char end)
833 (insert-before-markers (format "</%s>%s" cmd (if bolp "\n" "")))
834 (goto-char beg)
835 (insert (format "<%s%s>%s" cmd addin (if bolp "\n" "")))))
836 (insert (format "<%s%s>" cmd (yahtml-addin cmd)))
837 (save-excursion
838 (insert "\n")
839 (indent-to-column cc)
840 (insert (format "</%s>" cmd)))
841 (if (string-match "^a\\|p$" cmd) ;aとp決め打ちってのが美しくない…
842 (newline)
843 (yahtml-intelligent-newline nil))
844 (yahtml-indent-line))))
846 (defun yahtml-insert-begend-region ()
847 "Call yahtml-insert-begend in the region mode."
848 (interactive)
849 (yahtml-insert-begend t))
852 (defun yahtml-insert-form (&optional form)
853 "Insert <FORM option=\"argument\">."
854 (interactive)
855 (setq yahtml-current-completion-type 'single)
856 (or form
857 (let ((completion-ignore-case t))
858 (setq form
859 (YaTeX-cplread-with-learning
860 (format "Form(default %s): " yahtml-last-form)
861 'yahtml-form-table 'yahtml-user-form-table
862 'yahtml-tmp-form-table))))
863 (let ((p (point)) q)
864 (if (string= form "") (setq form yahtml-last-form))
865 (setq yahtml-last-form form)
866 (if yahtml-prefer-upcases (setq form (upcase form)))
867 (insert (format "<%s%s>" form (yahtml-addin form)))
868 ;;(indent-relative-maybe)
869 (if (cdr (assoc form yahtml-form-table))
870 (save-excursion (insert (format "</%s>" form))))
871 (if (search-backward "\"\"" p t) (forward-char 1))))
873 (defun yahtml-read-css (alist)
874 (let ((completion-ignore-case t) (delim " ")
875 (minibuffer-completion-table alist))
876 (read-from-minibuffer
877 (substitute-command-keys
878 (if YaTeX-japan
879 "クラス(複数指定は\\[quoted-insert] SPCで区切る): "
880 "class(or class list delimited by \\[quoted-insert] SPC): "))
881 nil YaTeX-minibuffer-completion-map nil)))
883 ;;; ---------- Add-in ----------
884 (defun yahtml-addin (form)
885 "Check add-in function's existence and call it if exists."
886 (let ((addin (concat "yahtml:" (downcase form))) s a)
887 (concat
888 (and (setq a (yahtml-css-get-element-completion-alist form))
889 (not (equal last-command-char ?\C-j))
890 (memq yahtml-current-completion-type '(multiline inline))
891 (yahtml-make-optional-argument ;should be made generic?
892 "class" (yahtml-read-css a)))
893 (if (and (intern-soft addin) (fboundp (intern-soft addin))
894 (stringp (setq s (funcall (intern addin))))
895 (string< "" s))
896 (if (eq (aref s 0) ? ) s (concat " " s))
897 ""))))
899 (defvar yahtml-completing-buffer nil)
900 (defun yahtml-collect-labels (&optional file)
901 "Collect current buffers label (<?? name=...>).
902 If optional argument FILE is specified collect labels in FILE."
903 (let (list end)
904 (save-excursion
905 (set-buffer yahtml-completing-buffer)
906 (if file (let (hilit-auto-highlight)
907 (set-buffer (find-file-noselect file))))
908 (save-excursion
909 (goto-char (point-min))
910 (while ;(re-search-forward "<\\w+\\b" nil t)
911 (re-search-forward "\\(name\\|id\\)\\s *=" nil t)
912 ;(setq bound (match-end 0))
913 ;(search-forward ">" nil t)
914 (setq end (match-end 0))
915 (if (and ;(re-search-backward "\\(name\\|id\\)\\s *=" bound t)
916 (yahtml-on-assignment-p)
917 (progn
918 (goto-char end)
919 (skip-chars-forward " \t\n")
920 (looking-at "\"?#?\\([^\">]+\\)\"?\\b")))
921 (setq list (cons
922 (list (concat "#" (YaTeX-match-string 1)))
923 list))))
924 list))))
926 (defvar yahtml-url-completion-map nil "Key map used in URL completion buffer")
927 (if yahtml-url-completion-map nil
928 (setq yahtml-url-completion-map
929 (copy-keymap minibuffer-local-completion-map))
930 (define-key yahtml-url-completion-map "\t" 'yahtml-complete-url)
931 (define-key yahtml-url-completion-map " " 'yahtml-complete-url))
933 (defun yahtml-complete-url ()
934 "Complete external URL from history or local file name."
935 (interactive)
936 (let ((p (point)) initial i2 cmpl path dir file listfunc beg labels
937 (lim (YaTeX-minibuffer-begin))
938 (min (if (fboundp 'field-beginning) (field-beginning) (point-min))))
939 (setq initial (YaTeX-minibuffer-string))
940 (cond
941 ((string-match "^http:" initial)
942 (setq cmpl (try-completion initial yahtml-urls)
943 listfunc (list 'lambda nil
944 (list 'all-completions initial 'yahtml-urls))
945 beg min))
946 ((setq beg (string-match "#" initial))
947 (or (equal beg 0) ;begin with #
948 (progn
949 (setq path (substring initial 0 beg))
950 (if (string-match "^/" path)
951 (setq path (yahtml-url-to-path path)))))
952 (setq initial (substring initial beg))
953 (setq labels (yahtml-collect-labels path)
954 cmpl (try-completion initial labels)
955 listfunc (list 'lambda ()
956 (list 'all-completions
957 initial (list 'quote labels)))
958 beg (+ min beg)))
959 (t
960 (setq path (if (string-match "^/" initial)
961 (or (yahtml-url-to-path initial) initial)
962 initial))
963 (setq dir (or (file-name-directory path) ".")
964 file (file-name-nondirectory path)
965 initial file
966 cmpl (file-name-completion file dir)
967 listfunc (list 'lambda nil
968 (list 'file-name-all-completions
969 file dir))
970 beg (save-excursion (skip-chars-backward "^/" lim) (point)))))
971 (cond
972 ((stringp cmpl)
973 (if (string= initial cmpl)
974 (with-output-to-temp-buffer "*Completions*"
975 (princ "Possible completinos are:\n")
976 (princ
977 (mapconcat '(lambda (x) x) (funcall listfunc) "\n")))
978 (delete-region (point) beg)
979 (insert cmpl)))
980 ((null cmpl)
981 (ding))
982 ((eq t cmpl)
983 (save-excursion
984 (unwind-protect
985 (progn
986 (goto-char p)
987 (insert " [Sole completion]"))
988 (delete-region p (point-max))))))))
990 ;
991 ; Subject: [yatex:02849] Re: [yahtml] tilda in href tag
992 ; From: Masayasu Ishikawa <mimasa@sfc.keio.ac.jp>
993 ; To: yatex@arcadia.jaist.ac.jp
994 ; Date: Mon, 31 May 1999 21:09:31 +0900
995 ; RFC 2396 の "2.4.3. Excluded US-ASCII Characters" によると、以下の文字
996 ; は必ずエスケープしないといけません。
997 ;
998 ; control = <US-ASCII coded characters 00-1F and 7F hexadecimal>
999 ; space = <US-ASCII coded character 20 hexadecimal>
1000 ; delims = "<" | ">" | "#" | "%" | <">
1001 ; unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
1002 (defvar yahtml-unsafe-chars-regexp
1003 "[][\x0- \x7f <>%\"{}|\\^`]" ;#は除去する
1004 "Characters regexp which must be escaped in URI.")
1006 ; また、以下の文字は予約された用法以外に用いる場合にはエスケープしないと
1007 ; いけないことになっています。
1009 ; reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
1010 ; "$" | ","
1011 (defvar yahtml-unreserved-chars-regexp
1012 "[;/?:@&=+$,]"
1013 "Characters regexp which should be escaped in URI on certain conditions.
1014 Not used yet.")
1016 (defun yahtml-escape-chars-string (str)
1017 "Translate reserved chars to URL encoded string."
1018 (let ((p 0) (target "")
1019 (ask (eq yahtml-escape-chars 'ask)))
1020 (cond
1021 ((null yahtml-escape-chars) str)
1022 (t
1023 (while (and (string< "" str)
1024 (setq p (string-match yahtml-unsafe-chars-regexp str)))
1025 (if (and ask (y-or-n-p (format "Escape char [%c] of `%s'"
1026 (aref str p) (substring str 0 (1+ p)))))
1027 (setq target (concat target
1028 (substring str 0 p)
1029 (format "%%%x" (aref str p))))
1030 (setq target (concat target (substring str 0 (1+ p)))))
1031 (setq str (substring str (1+ p))))
1032 (concat target str)))))
1034 (defun yahtml-escape-chars-region (beg end)
1035 "Translate reserved chars to encoded string in the region."
1036 (interactive "r")
1037 (save-excursion
1038 (let ((e (set-marker (make-marker) end)) c m yes)
1039 (goto-char beg)
1040 (while (and (< (point) e)
1041 (re-search-forward
1042 (concat yahtml-unsafe-chars-regexp "\\|"
1043 yahtml-unreserved-chars-regexp) e t))
1044 (sit-for 0)
1045 ; (setq m (buffer-modified-p)
1046 ; c (char-after (1- (point))))
1047 ; (save-excursion (backward-char 1) (insert " ==>"))
1048 ; (unwind-protect
1049 ; (setq yes (y-or-n-p (format "Replace: [%c]" c)))
1050 ; (save-excursion
1051 ; (backward-char 1)
1052 ; (delete-backward-char 4))
1053 ; (set-buffer-modified-p m))
1054 (message "Replace: [%c] (y or n):" (setq c (char-after (1- (point)))))
1055 (if (memq (read-char) '(?y ?Y))
1056 (progn
1057 (delete-region (match-beginning 0) (match-end 0))
1058 (insert (format "%%%x" c)))))
1059 (set-marker e nil))))
1060 ;; ab%defgls/.|
1062 (defun yahtml:a ()
1063 "Add-in function for <a>"
1064 (let ((href ""))
1065 (setq yahtml-completing-buffer (current-buffer)
1066 yahtml-urls (append yahtml-urls-private yahtml-urls-local)
1067 href (yahtml-escape-chars-string
1068 (read-from-minibuffer "href: " "" yahtml-url-completion-map)))
1069 (prog1
1070 (concat (yahtml-make-optional-argument
1071 "href" href)
1072 (yahtml-make-optional-argument
1073 "name" (read-string "name: ")))
1074 (if (and (string-match "^http://" href)
1075 (null (assoc href yahtml-urls-private))
1076 (null (assoc href yahtml-urls-local)))
1077 (YaTeX-update-table
1078 (list href)
1079 'yahtml-urls-private 'yahtml-urls-private 'yahtml-urls-local)))))
1081 (defvar yahtml-parameters-completion-alist
1082 '(("align" ("top") ("middle") ("bottom") ("left") ("right") ("center"))
1083 ("clear" ("left") ("right") ("center") ("all") ("none"))
1084 ("lang" ("ja") ("en") ("kr") ("ch") ("fr"))
1085 ("src" . file) ("file" . file)
1086 ("background" . file)
1087 ("class file name" . file) ("data" . file)
1088 ("method" ("POST") ("GET"))
1089 ("rev" . yahtml-link-types-alist)
1090 ("rel" . yahtml-link-types-alist)
1091 ("type" . yahtml-content-types-alist)
1092 ("codetype" . yahtml-content-types-alist)
1093 ("http-equiv" ("Refresh"))))
1095 (defvar yahtml-link-types-alist
1096 '(("alternate") ("stylesheet") ("start") ("next") ("prev")
1097 ("contents") ("index") ("glossary") ("chapter") ("section")
1098 ("subsection") ("appendix") ("help") ("bookmark")))
1100 (defvar yahtml-content-types-alist
1101 '(("text/css") ("text/html") ("text/plain") ("text/richtext")
1102 ("text/sgml") ("text/xml")
1103 ("text/javascript") ("text/tcl") ("text/vbscript")
1104 ("application/octet-stream") ("application/postscript") ("application/pdf")
1105 ("application/java")
1106 ("image/jpeg") ("image/gif") ("image/tiff") ("image/png") ("video/mpeg"))
1107 "Alist of content-types")
1109 (defun yahtml-read-parameter (par &optional default alist)
1110 (let* ((alist
1111 (cdr-safe (assoc (downcase par)
1112 (or alist yahtml-parameters-completion-alist))))
1113 (prompt (concat par ": "))
1114 v)
1115 (cond
1116 ((eq alist 'file)
1117 (let ((insert-default-directory))
1118 (read-file-name prompt "" default nil "")))
1119 ((and alist (symbolp alist))
1120 (completing-read prompt (symbol-value alist) nil nil default))
1121 (alist
1122 (completing-read prompt alist nil nil default))
1123 (t
1124 (read-string prompt default)))))
1126 (defun yahtml-make-optional-argument (opt arg)
1127 "Make optional argument string."
1128 (if (or (null arg) (string= "" arg))
1129 ""
1130 (concat " "
1131 (if yahtml-prefer-upcase-attributes (upcase opt) (downcase opt))
1132 "=\"" arg "\"")))
1134 (defun yahtml:html ()
1135 "Add-in for <html>"
1136 (setq yahtml-last-begend "head" yahtml-last-typeface-cmd "head")
1137 (yahtml-make-optional-argument
1138 "lang" (yahtml-read-parameter "lang" (if YaTeX-japan "ja"))))
1140 (defun yahtml:head ()
1141 "Add-in for <head>"
1142 (setq yahtml-last-begend "title" yahtml-last-typeface-cmd "title")
1143 "")
1145 (defun yahtml:body ()
1146 "Add-in function for <body>"
1147 (cond
1148 (yahtml-html4-strict nil)
1149 (t
1150 (let ((b (read-string "bgcolor="))
1151 (bg (yahtml-read-parameter "background" ""))
1152 (x (read-string "text color="))
1153 (l (read-string "link color="))
1154 (v (read-string "vlink color=")))
1155 (concat
1156 (yahtml-make-optional-argument "bgcolor" b)
1157 (yahtml-make-optional-argument "background" bg)
1158 (yahtml-make-optional-argument "text" x)
1159 (yahtml-make-optional-argument "link" l)
1160 (yahtml-make-optional-argument "vlink" v))))))
1162 (defun yahtml-make-style-parameter (proplist)
1163 "Make CSS property definitions in style attribute."
1164 (mapconcat
1165 '(lambda (x) (if (and (cdr x) (string< "" (cdr x)))
1166 (format "%s: %s;" (car x) (cdr x))))
1167 (delq nil proplist)
1168 " "))
1170 (defun yahtml:img ()
1171 "Add-in function for <img>"
1172 (let ((src (yahtml-read-parameter "src"))
1173 (alg (yahtml-read-parameter "align"))
1174 alt
1175 (brd (read-string "border="))
1176 (l yahtml-prefer-upcase-attributes)
1177 info width height bytes comments)
1178 (and (stringp src) (string< "" src) (file-exists-p src)
1179 (setq info (yahtml-get-image-info src))
1180 (car info)
1181 (setq width (int-to-string (car info))
1182 height (int-to-string (car (cdr info)))
1183 bytes (car (cdr (cdr info)))
1184 comments (nth 4 info)))
1185 (if info
1186 (setq alt
1187 (YaTeX-replace-formats
1188 yahtml:img-default-alt-format
1189 (list (cons "x" width)
1190 (cons "y" height)
1191 (cons "s" (int-to-string bytes))
1192 (cons "f" (file-name-nondirectory src))
1193 (cons "c" (car comments))))))
1195 (setq alt (yahtml-read-parameter "alt" alt))
1196 (setq width (yahtml-read-parameter "width" width)
1197 height (yahtml-read-parameter "height" height))
1198 (concat (if l "SRC" "src") "=\"" src "\""
1199 (yahtml-make-optional-argument "alt" alt)
1200 (yahtml-make-optional-argument "width" width)
1201 (yahtml-make-optional-argument "height" height)
1202 (if yahtml-html4-strict
1203 (yahtml-make-optional-argument
1204 "style"
1205 (if (or brd alg)
1206 (yahtml-make-style-parameter
1207 (list
1208 (if (string< "" alg)
1209 (cons "align" alg))
1210 (if (string< "" brd)
1211 (cons "border"
1212 (format "%dpx" (string-to-int brd))))))))
1213 (concat
1214 (yahtml-make-optional-argument "border" brd)
1215 (yahtml-make-optional-argument "align" alg))))))
1217 (defun yahtml-file-truename (file)
1218 (cond
1219 ((fboundp 'file-truename) (file-truename (expand-file-name file)))
1220 (t (let ((new file))
1221 (while (and (stringp (setq new (nth 0 (file-attributes file))))
1222 (not (equal new file)))
1223 (setq file new))
1224 file))))
1226 (defun yahtml-hex-value (point length &optional little-endian)
1227 "Return the hex value the POINT positions LENGTH byte stream represents.
1228 Optional third argument LITTLE-ENDIAN is self extplanatory."
1229 (setq point (1+ point)) ;translate file offset to Emacs's point value
1230 (let ((mlt 1)
1231 (pos (if little-endian point (+ point length -1)))
1232 (direc (if little-endian 1 -1))
1233 (value 0))
1234 (while (> length 0)
1235 (setq value (+ value (* mlt (char-after pos)))
1236 pos (+ pos direc)
1237 mlt (* mlt 256)
1238 length (1- length)))
1239 value))
1241 (defun yahtml-get-image-info (file)
1242 "Return the information on the image file FILE.
1243 Returns list of '(WIDTH HEIGHT BYTES DEPTH COMMENTLIST)."
1244 (save-excursion
1245 (let*((tmpbuf (get-buffer-create " *imgheader*"))
1246 width height bytes depth comment
1247 (file-coding-system-alist (list (cons "." 'no-conversion))) ;20
1248 (file-coding-system-for-read (and (boundp '*noconv*) *noconv*)) ;19
1249 (coding-system-for-read 'no-conversion)
1250 (seekpoint 1)
1251 c1 c2 c3 c4 beg end
1252 (case-fold-search nil))
1253 (setq bytes (nth 7 (file-attributes (yahtml-file-truename file))))
1254 (set-buffer tmpbuf)
1255 (if (boundp 'mc-flag) (set (make-local-variable 'mc-flag) nil))
1256 (erase-buffer)
1257 (if (fboundp 'set-buffer-multibyte) (set-buffer-multibyte nil))
1258 (unwind-protect
1259 (progn
1260 (message "Inspecting image information of %s..." file)
1261 ;; Read 4bytes-more than inspection-bytes in case that
1262 ;; JPEG marker delimiter (4bytes) is on the alignment.
1263 (YaTeX-insert-file-contents
1264 file nil 0 (+ yahtml-image-inspection-bytes 4))
1265 (goto-char (point-min)) ;assertion
1266 (setq c1 (char-after 1) ;cache first 4 bytes
1267 c2 (char-after 2)
1268 c3 (char-after 3)
1269 c4 (char-after 4))
1270 (cond
1271 ((and (eq c1 ?\377) (eq c2 ?\330)) ; 0xff 0xd8
1272 ;;JPEG images need JPEG markers inspection
1273 ;;JPEG markers consist of [ 0xff ID(B) LEN(S) CONTENTS... ]
1274 ;; Warning: here seekpoint is measured by Emacs's point value
1275 ;; while yahtml-hex-vale requires file offset
1276 (setq seekpoint 3) ;where the first JPEG marker exists
1277 (catch 'exit
1278 (while (< seekpoint (- (buffer-size) 4))
1279 (cond
1280 ((not (eq (char-after seekpoint) ?\377))
1281 ;maybe corrupted, exit from loop
1282 (throw 'exit t))
1283 ((memq
1284 (char-after (1+ seekpoint))
1285 '(?\300 ?\301 ?\302 ?\303
1286 ?\305 ?\306 ?\307 ?\311 ?\312 ?\313 ?\315 ?\316 ?\317))
1287 ;;'(192 193 194 195 197 198 199 201 202 203 205 206 207
1288 ;;found!
1289 (setq height (yahtml-hex-value (+ seekpoint 4) 2)
1290 width (yahtml-hex-value (+ seekpoint 6) 2)
1291 depth (yahtml-hex-value (+ seekpoint 3) 1)))
1292 ((eq (char-after (1+ seekpoint)) ?\376) ;0xFE = comment
1293 ;; JPEG comment area
1294 (setq beg (+ seekpoint 2 2)
1295 end (+ seekpoint
1296 (yahtml-hex-value (1+ seekpoint) 2) 2))
1297 (setq comment (cons (buffer-substring beg end) comment)))
1298 (t ;other markers
1299 nil)) ;just skip it
1300 (setq seekpoint (+ seekpoint 2)
1301 seekpoint (+ seekpoint
1302 (yahtml-hex-value (1- seekpoint) 2))))))
1303 ((and (eq c1 ?\211) ;0x89
1304 (eq c2 ?P) (eq c3 ?N) (eq c4 ?G))
1305 ;;PNG Image data X=@0x10(L), Y=@0x14(L), D=@0x18(B)
1306 (setq width (yahtml-hex-value 16 4)
1307 height (yahtml-hex-value 20 4)
1308 depth (yahtml-hex-value 24 1)))
1309 ((looking-at "GIF8")
1310 ;;GIF Image data X=@0x6(leshort), Y=@0x8(leshort)
1311 (setq width (yahtml-hex-value 6 2 t)
1312 height (yahtml-hex-value 8 2 t)))
1313 ((looking-at "BM")
1314 ;;# OS/2, Windoze BMP files
1315 ;;@0x0e = 12 -> OS/2 1.x - X=@0x12(leshort), Y=@0x14(leshort)
1316 ;;@0x0e = 64 -> OS/2 2.x - X=@0x12(leshort), Y=@0x14(leshort)
1317 ;;@0x0e = 40 -> Windows 3.x - X=@0x12(lelong), Y=@0x16(lelong)
1318 (cond
1319 ((eq (yahtml-hex-value 14 2 t) 40)
1320 (setq width (yahtml-hex-value 18 4 t)
1321 height (yahtml-hex-value 22 4 t)))
1322 (t
1323 (setq width (yahtml-hex-value 18 2 t)
1324 height (yahtml-hex-value 20 2 t)))))))
1325 (message "")
1326 (kill-buffer tmpbuf))
1327 (list width height bytes depth (nreverse comment)))))
1329 (defun yahtml:form ()
1330 "Add-in function `form' input format"
1331 (concat
1332 " " (if yahtml-prefer-upcase-attributes "METHOD" "method") "="
1333 (completing-read "Method: " '(("POST") ("GET")) nil t)
1334 " " (if yahtml-prefer-upcase-attributes "ACTION" "action") "=\""
1335 (read-string "Action: ") "\""))
1337 (defun yahtml:select ()
1338 "Add-in function for `select' input format"
1339 (setq yahtml-last-single-cmd "option")
1340 (concat " " (if yahtml-prefer-upcase-attributes "NAME" "name") "=\""
1341 (read-string "name: ") "\""))
1343 (defun yahtml:ol ()
1344 "Add-in function for <ol>"
1345 (setq yahtml-last-single-cmd "li")
1346 (let ((start (read-string "start="))
1347 (type (completing-read
1348 "type=" '(("1") ("a") ("A") ("i") ("I")) nil t)))
1349 (concat
1350 (yahtml-make-optional-argument "start" start)
1351 (yahtml-make-optional-argument "type" type))))
1352 (defun yahtml:ul ()
1353 (setq yahtml-last-single-cmd "li") "")
1354 (defun yahtml:dl ()
1355 (setq yahtml-last-single-cmd "dt") "")
1356 (defun yahtml:dt ()
1357 (setq yahtml-last-single-cmd "dd") "")
1359 (defun yahtml:p ()
1360 (if yahtml-html4-strict nil
1361 (let ((alg (yahtml-read-parameter "align")))
1362 (yahtml-make-optional-argument "align" alg))))
1364 (defvar yahtml-input-types
1365 '(("text") ("password") ("checkbox") ("radio") ("submit")
1366 ("reset") ("image") ("hidden") ("file")))
1368 (defun yahtml:input ()
1369 "Add-in function for `input' form"
1370 (let ((size "") name type value checked (maxlength "")
1371 (l yahtml-prefer-upcase-attributes))
1372 (setq name (read-string "name: ")
1373 type (completing-read "type (default=text): "
1374 yahtml-input-types nil t)
1375 value (read-string "value: "))
1376 (if (string-match "text\\|password\\|^$" type)
1377 (setq size (read-string "size: ")
1378 maxlength (read-string "maxlength: ")))
1379 (concat
1380 (if l "NAME" "name") "=\"" name "\""
1381 (yahtml-make-optional-argument "type" type)
1382 (yahtml-make-optional-argument "value" value)
1383 (yahtml-make-optional-argument "size" size)
1384 (yahtml-make-optional-argument "maxlength" maxlength))))
1386 (defun yahtml:textarea ()
1387 "Add-in function for `textarea'"
1388 (interactive)
1389 (let (name rows cols)
1390 (setq name (read-string "Name: ")
1391 cols (read-string "Columns: ")
1392 rows (read-string "Rows: "))
1393 (concat
1394 (concat (if yahtml-prefer-upcase-attributes "NAME=" "name=")
1395 "\"" name "\"")
1396 (yahtml-make-optional-argument "cols" cols)
1397 (yahtml-make-optional-argument "rows" rows))))
1399 (defun yahtml:table ()
1400 "Add-in function for `table'"
1401 (let ((b (read-string "border="))
1402 (a (yahtml-read-parameter
1403 "align" nil '(("align" ("right")("center"))))))
1404 (if yahtml-html4-strict
1405 (yahtml-make-optional-argument
1406 "style"
1407 (if (or (string< "" b) (string< "" a))
1408 (yahtml-make-style-parameter
1409 (append
1410 (if (string< "" b)
1411 (list
1412 (cons "border" (format "%dpx solid" (string-to-int b)))
1413 (cons "border-collapse" "collapse")))
1414 (if (string< "" a)
1415 (cond
1416 ((string-match "right" a)
1417 (list (cons "margin-left" "auto")
1418 (cons "margin-right" "0")))
1419 ((string-match "center" a)
1420 (list (cons "margin-left" "auto")
1421 (cons "margin-right" "auto")))))))))
1422 (concat
1423 (yahtml-make-optional-argument "border" b)
1424 (yahtml-make-optional-argument "align" a)))))
1426 ;(fset 'yahtml:caption 'yahtml:p)
1427 (defun yahtml:caption ()
1428 "Add-in function for `caption' in table tag"
1429 (let ((par (yahtml-read-parameter "align")))
1430 (if yahtml-html4-strict
1431 (yahtml-make-optional-argument
1432 "style" (if par (yahtml-make-style-parameter
1433 (list (cons "caption-side" par)))))
1434 (yahtml-make-optional-argument "align" par))))
1436 (defun yahtml:font ()
1437 "Add-in function for `font'"
1438 (concat
1439 (yahtml-make-optional-argument "color" (read-string "color="))
1440 (yahtml-make-optional-argument "size" (read-string "size="))))
1442 (defun yahtml:style ()
1443 "Add-in function for `style'"
1444 (yahtml-make-optional-argument
1445 "type" (read-string "type=" "text/css")))
1447 (defun yahtml:script ()
1448 "Add-in function for `script'"
1449 (concat
1450 (yahtml-make-optional-argument
1451 "type" (yahtml-read-parameter "type" "text/javascript"))
1452 (yahtml-make-optional-argument
1453 "src" (yahtml-read-parameter "src" ""))))
1455 (defun yahtml:tr ()
1456 "Add-in function for `tr'"
1457 (setq ;yahtml-last-begend "td" ;; which do you prefer?
1458 yahtml-last-typeface-cmd "td")
1459 "")
1461 (defun yahtml:link ()
1462 "Add-in function for `link' (まだちょっと良く分かってない)"
1463 (let (rel rev type href)
1464 (setq rel (yahtml-read-parameter "rel"))
1465 (cond
1466 ((equal rel "")
1467 (concat (yahtml-make-optional-argument
1468 "rev" (yahtml-read-parameter "rev"))
1469 (yahtml-make-optional-argument
1470 "href" (yahtml-read-parameter "href")
1471 ;;他に良く使うのって何?
1472 )))
1473 ((string-match "stylesheet" rel)
1474 (concat
1475 (yahtml-make-optional-argument "rel" rel)
1476 (yahtml-make-optional-argument
1477 "type" (yahtml-read-parameter "type" "text/css"))
1478 (progn
1479 (setq href
1480 (read-from-minibuffer "href: " "" yahtml-url-completion-map))
1481 (if (string< "" href)
1482 (progn
1483 (if (and (file-exists-p (yahtml-url-to-path href))
1484 (y-or-n-p "Load css symbols now? "))
1485 (setq yahtml-css-class-alist
1486 (yahtml-css-collect-classes-file
1487 (yahtml-url-to-path href) yahtml-css-class-alist)))
1488 (message "")
1489 (yahtml-make-optional-argument "href" href))))))
1490 (t
1491 (concat
1492 (yahtml-make-optional-argument "rel" rel)
1493 (yahtml-make-optional-argument
1494 "type" (yahtml-read-parameter "type" "text/css"))
1495 (yahtml-make-optional-argument
1496 "href"
1497 (read-from-minibuffer "href: " "" yahtml-url-completion-map)))))))
1499 (defvar yahtml:meta-names
1500 '(("name" ("keywords")("author")("copyright")("date")("GENERATOR"))))
1502 (defun yahtml:meta ()
1503 (let ((name (yahtml-make-optional-argument
1504 "name"
1505 (yahtml-read-parameter "name" nil yahtml:meta-names)))
1506 http-equiv content)
1507 (if (string= "" name)
1508 (if (string-match
1509 "Content-type"
1510 (setq http-equiv (yahtml-make-optional-argument
1511 "http-equiv"
1512 (yahtml-read-parameter "http-equiv" nil))))
1513 (error "It's very bad idea to set Content-type in META. %s"
1514 "See docs/qanda")
1515 (concat http-equiv
1516 (yahtml-make-optional-argument
1517 "content" (yahtml-read-parameter "content"))))
1518 (concat
1519 name
1520 (yahtml-make-optional-argument
1521 "content"
1522 (cond
1523 ((string-match "date" name)
1524 (read-string "Date: " (current-time-string)))
1525 ((string-match "author" name)
1526 (read-string "Author: "
1527 (if (and (user-full-name) (string< "" (user-full-name)))
1528 (user-full-name)
1529 (user-login-name))))
1530 ((string-match "GENERATOR" name)
1531 (setq content (read-string "Generator: " "User-agent: "))
1532 (if (string-match "yahtml" content)
1533 (message "Thank you!"))
1534 content)
1535 (t (read-string (concat name ": ")))))))))
1537 (defun yahtml:br ()
1538 (yahtml-make-optional-argument "clear" (yahtml-read-parameter "clear")))
1540 (defun yahtml:object ()
1541 (let ((codetype (yahtml-read-parameter "codetype" "application/java"))
1542 data classid)
1543 (cond
1544 ((string-match "java" codetype)
1545 (let ((completion-ignored-extensions
1546 ;;any extensions except ".class"
1547 '(".java" ".html" ".htm" ".gif" ".jpg" ".jpeg" ".png")))
1548 (setq classid (concat "java:"
1549 (yahtml-read-parameter "class file name"))))
1550 (concat
1551 (yahtml-make-optional-argument "codetype" codetype)
1552 (yahtml-make-optional-argument "classid" classid)
1553 (yahtml-make-optional-argument
1554 "width" (yahtml-read-parameter "width"))
1555 (yahtml-make-optional-argument
1556 "height" (yahtml-read-parameter "height"))
1557 (yahtml-make-optional-argument
1558 "align" (yahtml-read-parameter "align"))))
1559 (t
1560 ""))))
1562 (defun yahtml:abbr ()
1563 "Add-in function for abbr."
1564 (yahtml-make-optional-argument "title" (yahtml-read-parameter "title")))
1566 (defun yahtml:button ()
1567 (concat
1568 (yahtml-make-optional-argument
1569 "name" (yahtml-read-parameter "name"))
1570 (yahtml-make-optional-argument
1571 "type" (yahtml-read-parameter
1572 "type" "button" '(("submit")("reset")("button"))))
1573 (yahtml-make-optional-argument
1574 "value" (yahtml-read-parameter "value"))))
1576 ;;; ---------- Simple tag ----------
1577 (defun yahtml-insert-tag (region-mode &optional tag)
1578 "Insert <TAG> </TAG> and put cursor inside of them."
1579 (interactive "P")
1580 (setq yahtml-current-completion-type 'inline)
1581 (or tag
1582 (let ((completion-ignore-case t))
1583 (setq tag
1584 (YaTeX-cplread-with-learning
1585 (format "Tag %s(default %s): "
1586 (if region-mode "region: " "") yahtml-last-typeface-cmd)
1587 'yahtml-typeface-table 'yahtml-user-typeface-table
1588 'yahtml-tmp-typeface-table))))
1589 (if (string= "" tag) (setq tag yahtml-last-typeface-cmd))
1590 (setq tag (or (cdr (assoc tag yahtml-typeface-table)) tag))
1591 (setq yahtml-last-typeface-cmd tag
1592 tag (funcall (if yahtml-prefer-upcases 'upcase 'downcase) tag))
1593 (if region-mode
1594 (if (if (string< "19" emacs-version) (mark t) (mark))
1595 (save-excursion
1596 (if (> (point) (mark)) (exchange-point-and-mark))
1597 (insert (format "<%s%s>" tag (yahtml-addin tag)))
1598 (exchange-point-and-mark)
1599 (insert "</" tag ">"))
1600 (message "No mark set now"))
1601 (insert (format "<%s%s>" tag (yahtml-addin tag)))
1602 (save-excursion (insert (format "</%s>" tag)))))
1604 (defun yahtml-insert-tag-region (&optional tag)
1605 "Call yahtml-insert-tag with region mode."
1606 (interactive)
1607 (yahtml-insert-tag t tag))
1609 (defvar yahtml-need-single-closer nil) ;for test
1610 (defun yahtml-insert-single (cmd)
1611 "Insert <CMD>."
1612 (interactive
1613 (list
1614 (let ((completion-ignore-case t))
1615 (YaTeX-cplread-with-learning
1616 (format "Command%s: "
1617 (if yahtml-last-single-cmd
1618 (concat "(default " yahtml-last-single-cmd ")") ""))
1619 'yahtml-single-cmd-table 'yahtml-user-single-cmd-table
1620 'yahtml-tmp-single-cmd-table))))
1621 (if (string= "" cmd) (setq cmd yahtml-last-single-cmd))
1622 (setq yahtml-last-single-cmd
1623 (or (cdr (assoc cmd yahtml-single-cmd-table)) cmd))
1624 (setq cmd (funcall (if yahtml-prefer-upcases 'upcase 'downcase)
1625 yahtml-last-single-cmd))
1626 (insert (format "<%s%s%s>"
1627 cmd
1628 (yahtml-addin cmd)
1629 (if (and yahtml-need-single-closer
1630 (assoc cmd '(("br")("hr"))))
1631 " /" "")))
1632 (if (assoc cmd yahtml-env-table)
1633 (save-excursion (insert (format "</%s>" cmd)))))
1635 (defun yahtml-insert-p (&optional arg)
1636 "Convenient function to insert <p></p>"
1637 (interactive "P")
1638 (if (or yahtml-always-/p arg) (yahtml-insert-tag arg "p")
1639 (yahtml-insert-single "p")))
1641 (defun yahtml-insert-amps (arg)
1642 "Insert char-entity references via ampersand"
1643 ;; Thanks; http://www.tsc.co.jp/~asada/html/wdg40_f/entities/
1644 (interactive "P")
1645 (let*((mess "") c
1646 (list (append yahtml-entity-reference-chars-alist-default
1647 yahtml-entity-reference-chars-alist))
1648 (l list))
1649 (while l
1650 (setq mess (format "%s %c" mess (car (car l)) (cdr (car l)))
1651 l (cdr l)))
1652 (message "Char-entity reference: %s SPC=& RET=&; Other=&#..;" mess)
1653 (setq c (read-char))
1654 (cond
1655 ((equal c (car-safe (assoc c list)))
1656 (insert (format "&%s;" (cdr (assoc c list)))))
1657 ((or (equal c ?\n) (equal c ?\r))
1658 (insert "&;")
1659 (forward-char -1))
1660 ((equal c ? )
1661 (insert ?&))
1662 (t (insert (format "&#%d;" c))))))
1664 (defun yahtml:!--\#include ()
1665 (let ((file (yahtml-read-parameter "file" "")))
1666 (format "%s=\"%s\"--" (if (string-match "/" file) "virtual" "file") file)))
1668 (defun yahtml:!--\#exec ()
1669 (format "cmd=\"%s\"--" (yahtml-read-parameter "cmd" "" '(("cmd" . file)))))
1671 ;;; ---------- Jump ----------
1672 (defun yahtml-on-href-p ()
1673 "Check if point is on href clause."
1674 (let ((p (point)) e cmd (case-fold-search t))
1675 (save-excursion
1676 (and ;;(string= (YaTeX-inner-environment t) "a") ;aでなくても許可にした
1677 (save-excursion
1678 ;;(search-forward "</a>" nil t) ;aでなくても許可にした
1679 (search-forward "[\" \t\n]" nil t)
1680 (setq e (point)))
1681 ;(goto-char (get 'YaTeX-inner-environment 'point))
1682 (re-search-backward "<\\(a\\|link\\)\\>" nil t)
1683 (search-forward "href" e t)
1684 (search-forward "=" e t)
1685 (progn
1686 (skip-chars-forward " \t\n")
1687 (looking-at "\"?\\([^\"> \t\n]+\\)\"?"))
1688 (< p (match-end 0))
1689 (YaTeX-match-string 1)))))
1691 (defun yahtml-netscape-sentinel (proc mes)
1692 (cond
1693 ((null (buffer-name (process-buffer proc)))
1694 (set-process-buffer proc nil))
1695 ((eq (process-status proc) 'exit)
1696 (let ((cb (current-buffer)))
1697 (set-buffer (process-buffer proc))
1698 (goto-char (point-min))
1699 (if (search-forward "not running" nil t)
1700 (progn
1701 (message "Starting netscape...")
1702 (start-process
1703 "browser" (process-buffer proc)
1704 shell-file-name yahtml-shell-command-option
1705 (format "%s \"%s\"" yahtml-www-browser
1706 (get 'yahtml-netscape-sentinel 'url)))
1707 (message "Starting netscape...Done")))
1708 (set-buffer cb)))))
1710 (defvar yahtml-browser-process nil)
1712 (defun yahtml-browse-html (href)
1713 "Call WWW Browser to see HREF."
1714 (let ((pb "* WWW Browser *") (cb (current-buffer)))
1715 (cond
1716 ((string-match "^start\\>" yahtml-www-browser)
1717 (if (get-buffer pb)
1718 (progn (set-buffer pb) (erase-buffer) (set-buffer cb)))
1719 (put 'yahtml-netscape-sentinel 'url href)
1720 (set-process-sentinel
1721 (setq yahtml-browser-process
1722 (start-process
1723 "browser" pb shell-file-name yahtml-shell-command-option
1724 (format "%s \"%s\"" yahtml-www-browser href)))
1725 'yahtml-netscape-sentinel))
1726 ((and (string-match
1727 "[Nn]etscape\\|[Ff]irefox\\|[Mm]ozilla" yahtml-www-browser)
1728 (not (eq system-type 'windows-nt)))
1729 (if (get-buffer pb)
1730 (progn (set-buffer pb) (erase-buffer) (set-buffer cb)))
1731 (put 'yahtml-netscape-sentinel 'url href)
1732 (set-process-sentinel
1733 (setq yahtml-browser-process
1734 (start-process
1735 "browser" pb shell-file-name yahtml-shell-command-option ;"-c"
1736 (format "%s -remote \"openURL(%s)\"" yahtml-www-browser href)))
1737 'yahtml-netscape-sentinel))
1738 ((and (string= "w3" yahtml-www-browser) (fboundp 'w3-fetch))
1739 (w3-fetch href))
1740 ((stringp yahtml-www-browser)
1741 (if (and yahtml-browser-process
1742 (eq (process-status yahtml-browser-process) 'run))
1743 (message "%s is already running" yahtml-www-browser)
1744 (setq yahtml-browser-process
1745 (start-process
1746 "browser" "* WWW Browser *"
1747 shell-file-name yahtml-shell-command-option
1748 (format "%s \"%s\"" yahtml-www-browser href)))))
1749 (t
1750 (message "Sorry, jump across http is not supported.")))))
1752 (defun yahtml-goto-corresponding-href (&optional other)
1753 "Go to corresponding name."
1754 (let ((href (yahtml-on-href-p)) file name (parent buffer-file-name))
1755 (if href
1756 (cond
1757 ((string-match "^\\(ht\\|f\\)tps?:" href)
1758 (yahtml-browse-html href))
1759 (t (setq file (substring href 0 (string-match "#" href)))
1760 (if (string-match "#" href)
1761 (setq name (substring href (1+ (string-match "#" href)))))
1762 (if (string< "" file)
1763 (progn
1764 (if (string-match "/$" file)
1765 (or (catch 'dirindex
1766 (mapcar
1767 (function
1768 (lambda (f)
1769 (if (file-exists-p (concat file f))
1770 (throw 'dirindex
1771 (setq file (concat file f))))))
1772 (yahtml-get-directory-index))
1773 nil)
1774 (setq file (concat file yahtml-directory-index))))
1775 (if (string-match "^/" file)
1776 (setq file (yahtml-url-to-path file)))
1777 (if other (YaTeX-switch-to-buffer-other-window file)
1778 (YaTeX-switch-to-buffer file))
1779 (or YaTeX-parent-file (setq YaTeX-parent-file parent))))
1780 (if name
1781 (progn (set-mark-command nil) (yahtml-jump-to-name name)))
1782 t)))))
1784 (defun yahtml-jump-to-name (name)
1785 "Jump to html's named tag."
1786 (setq name (format "\\(name\\|id\\)\\s *=\\s *\"?%s\\>\"?" name))
1787 (or (and (re-search-forward name nil t) (goto-char (match-beginning 0)))
1788 (and (re-search-backward name nil t) (goto-char (match-beginning 0)))
1789 (message "Named tag `%s' not found" (substring href 1))))
1791 (defun yahtml-on-begend-p (&optional p)
1792 "Check if point is on begend clause."
1793 (let ((p (or p (point))) cmd (case-fold-search t))
1794 (save-excursion
1795 (goto-char p)
1796 (if (equal (char-after (point)) ?<) (forward-char 1))
1797 (if (and (re-search-backward "<" nil t)
1798 (looking-at
1799 ;(concat "<\\(/?" yahtml-struct-name-regexp "\\)\\b")
1800 "<\\(/?[A-Z][A-Z0-9]*\\)\\b")
1801 (condition-case nil
1802 (forward-list 1)
1803 (error nil))
1804 (< p (point)))
1805 (YaTeX-match-string 1)))))
1807 (defun yahtml-goto-corresponding-begend (&optional noerr)
1808 "Go to corresponding opening/closing tag.
1809 Optional argument NOERR causes no error for unballanced tag."
1810 (let ((cmd (yahtml-on-begend-p)) m0
1811 (p (point)) (case-fold-search t) func str (nest 0))
1812 (cond
1813 (cmd
1814 (setq m0 (match-beginning 0))
1815 (if (= (aref cmd 0) ?/) ;on </cmd> line
1816 (setq cmd (substring cmd 1)
1817 str (format "\\(<%s\\)\\|\\(</%s\\)" cmd cmd)
1818 func 're-search-backward)
1819 (setq str (format "\\(</%s\\)\\|\\(<%s\\)" cmd cmd)
1820 func 're-search-forward))
1821 (while (and (>= nest 0) (funcall func str nil t))
1822 (if (equal m0 (match-beginning 0))
1823 nil
1824 (setq nest (+ nest (if (match-beginning 1) -1 1)))))
1825 (if (< nest 0)
1826 (goto-char (match-beginning 0))
1827 (funcall
1828 (if noerr 'message 'error)
1829 "Corresponding tag of `%s' not found." cmd)
1830 (goto-char p)
1831 nil))
1832 (t nil))))
1834 (defun yahtml-current-tag ()
1835 "Return the current tag name including #exec and #include."
1836 (save-excursion
1837 (let ((p (point)) b tag)
1838 (or (bobp)
1839 (looking-at "<")
1840 (progn (skip-chars-backward "^<") (forward-char -1)))
1841 (setq b (point))
1842 (skip-chars-forward "<")
1843 (setq tag (YaTeX-buffer-substring
1844 (point) (progn (skip-chars-forward "^ \t\n") (point))))
1845 (goto-char b)
1846 (forward-list 1)
1847 (and (< p (point)) tag))))
1849 (defun yahtml-get-attrvalue (attr)
1850 "Extract current tag's attribute value from buffer."
1851 (let (e (case-fold-search t))
1852 (save-excursion
1853 (or (looking-at "<")
1854 (progn (skip-chars-backward "^<") (backward-char 1)))
1855 (setq e (save-excursion (forward-list 1) (point)))
1856 (if (and
1857 (re-search-forward (concat "\\b" attr "\\b") e t)
1858 (progn (skip-chars-forward " \t\n=")
1859 (looking-at "\"?\\([^\"> \t\n]+\\)\"?")))
1860 (YaTeX-match-string 1)))))
1862 (defun yahtml-goto-corresponding-img ()
1863 "View image on point"
1864 (let ((tag (yahtml-current-tag)) image (p (point)) (case-fold-search t))
1865 (if (and tag
1866 (string-match "img" tag)
1867 (setq image (yahtml-get-attrvalue "src")))
1868 (progn
1869 (message "Invoking %s %s..." yahtml-image-viewer image)
1870 (start-process
1871 "Viewer" " * Image Viewer *"
1872 shell-file-name yahtml-shell-command-option ;"-c"
1873 (concat yahtml-image-viewer " " image))
1874 (message "Invoking %s %s...Done" yahtml-image-viewer image)))))
1876 (defun yahtml-goto-corresponding-source (&optional other)
1877 "Goto applet's or script's source."
1878 (let ((env (yahtml-current-tag)) s (p (point)))
1879 (cond
1880 ((string-match "applet" env)
1881 (if (setq s (yahtml-get-attrvalue "code"))
1882 (progn
1883 (setq s (YaTeX-match-string 1)
1884 s (concat
1885 (substring s 0 (string-match "\\.[A-Za-z]+$" s))
1886 ".java"))
1887 (if other (YaTeX-switch-to-buffer-other-window s)
1888 (YaTeX-switch-to-buffer s))
1889 s) ;return source file name
1890 (message "No applet source specified")
1891 (sit-for 1)
1892 nil))
1893 ((string-match "script" env)
1894 (if (setq s (yahtml-get-attrvalue "src"))
1895 (progn
1896 (funcall (if other 'YaTeX-switch-to-buffer-other-window
1897 'YaTeX-switch-to-buffer)
1898 (yahtml-url-to-path s))
1899 s)))
1900 ((string-match "!--#include" env)
1901 (cond
1902 ((setq s (yahtml-get-attrvalue "file")) ;<!--#include file="foo"-->
1903 (if other (YaTeX-switch-to-buffer-other-window s)
1904 (YaTeX-switch-to-buffer s))
1905 s)
1906 ((setq s (yahtml-get-attrvalue "virtual"));<!--#include virtual="foo"-->
1907 (setq s (yahtml-url-to-path s))
1908 (if other (YaTeX-switch-to-buffer-other-window s)
1909 (YaTeX-switch-to-buffer s))
1910 s)))
1911 ((and (string-match "!--#exec" env)
1912 (setq s (yahtml-get-attrvalue "cmd")))
1913 (setq s (substring s 0 (string-match " \t\\?" s))) ;get argv0
1914 (let ((b " *yahtmltmp*")) ;peek a little
1915 (unwind-protect
1916 (progn
1917 (set-buffer (get-buffer-create b))
1918 (YaTeX-insert-file-contents s nil 0 100)
1919 (if (looking-at "#!")
1920 (if other (YaTeX-switch-to-buffer-other-window s)
1921 (YaTeX-switch-to-buffer s))))
1922 (kill-buffer (get-buffer b)))
1923 (get-file-buffer s))))))
1925 (defun yahtml-goto-corresponding-* (&optional other)
1926 "Go to corresponding object."
1927 (interactive "P")
1928 (cond
1929 ((yahtml-goto-corresponding-href other))
1930 ((yahtml-goto-corresponding-img))
1931 ((yahtml-goto-corresponding-source other))
1932 ((yahtml-goto-corresponding-begend))
1933 (t (message "I don't know where to go."))))
1935 (defun yahtml-goto-corresponding-*-other-window ()
1936 "Go to corresponding object."
1937 (interactive)
1938 (yahtml-goto-corresponding-* t))
1940 (defun yahtml-visit-main ()
1941 "Go to parent file from where you visit current file."
1942 (interactive)
1943 (if YaTeX-parent-file (YaTeX-switch-to-buffer YaTeX-parent-file)))
1945 ;;; ---------- killing ----------
1946 (defun yahtml-kill-begend (&optional whole)
1947 (let ((tag (yahtml-on-begend-p)) p q r bbolp)
1948 (if tag
1949 (save-excursion
1950 (or (looking-at "<")
1951 (progn (skip-chars-backward "^<") (forward-char -1)))
1952 (setq p (point))
1953 (yahtml-goto-corresponding-begend)
1954 (or (looking-at "<")
1955 (progn (skip-chars-backward "^<") (forward-char -1)))
1956 (if (< (point) p) ;if on the opening tag
1957 (progn (setq q p p (point))
1958 (goto-char q))
1959 (setq q (point))) ;now q has end-line's (point)
1960 (if (not whole)
1961 (kill-region
1962 (progn (skip-chars-backward " \t")
1963 (if (setq bbolp (bolp)) (point) q))
1964 (progn (forward-list 1)
1965 (setq r (point))
1966 (skip-chars-forward " \t")
1967 (if (and bbolp (eolp) (not (eobp))) (1+ (point)) r))))
1968 (goto-char p)
1969 (skip-chars-backward " \t")
1970 (if (not whole)
1971 (progn
1972 (kill-append
1973 (buffer-substring
1974 (setq p (if (setq bbolp (bolp)) (point) p))
1975 (setq q (progn
1976 (forward-list 1)
1977 (setq r (point))
1978 (skip-chars-forward " \t")
1979 (if (and bbolp (eolp) (not (eobp)))
1980 (1+ (point))
1981 r))))
1982 t)
1983 (delete-region p q))
1984 (kill-region
1985 (if (bolp) (point) p)
1986 (progn (goto-char q)
1987 (forward-list 1)
1988 (setq r (point))
1989 (skip-chars-forward " \t")
1990 (if (and (eolp) (not (eobp))) (1+ (point)) r))))
1991 tag))))
1993 (defun yahtml-kill-* (whole)
1994 "Kill current position's HTML tag (set)."
1995 (interactive "P")
1996 (cond
1997 ((yahtml-kill-begend whole))))
2000 ;;; ---------- changing ----------
2001 (defun yahtml-on-assignment-p ()
2002 "Return if current point is on parameter assignment.
2003 If so, return parameter name, otherwise nil.
2004 This function should be able to treat white spaces in value, but not yet."
2005 (let ((p (point)))
2006 (save-excursion
2007 (put 'yahtml-on-assignment-p 'region nil)
2008 (skip-chars-backward "^ \t\n")
2009 (and (looking-at "\\([A-Za-z0-9]+\\)\\s *=\\s *\"?\\([^ \t\"]+\\)\"?")
2010 (< p (match-end 0))
2011 (>= p (1- (match-beginning 2)))
2012 (put 'yahtml-on-assignment-p 'region
2013 (cons (match-beginning 2) (match-end 2)))
2014 (YaTeX-match-string 1)))))
2016 (defun yahtml-change-begend ()
2017 (let ((tag (yahtml-on-begend-p))
2018 (completion-ignore-case t)
2019 (case-fold-search t)
2020 (p (point)) (q (make-marker))
2021 (default (append yahtml-env-table yahtml-typeface-table))
2022 (user (append yahtml-user-env-table yahtml-user-typeface-table))
2023 (tmp (append yahtml-tmp-env-table yahtml-tmp-typeface-table))
2024 href b1 e1 attr new css)
2025 (cond
2026 (tag
2027 (cond
2028 ((and (string-match "^a$" tag)
2029 (save-excursion
2030 (and
2031 (re-search-backward "<a\\b" nil t)
2032 (progn
2033 (goto-char (match-end 0))
2034 (skip-chars-forward " \t\n")
2035 (setq b1 (point))
2036 (search-forward ">" nil t))
2037 (setq e1 (match-beginning 0))
2038 (goto-char b1)
2039 (re-search-forward "href\\s *=" e1 t)
2040 (>= p (point))
2041 (progn
2042 (goto-char (match-end 0))
2043 (skip-chars-forward " \t\n")
2044 (looking-at "\"?\\([^\"> \t\n]+\\)\"?"))
2045 (< p (match-end 0)))))
2046 (setq b1 (match-beginning 1) e1 (match-end 1)
2047 yahtml-completing-buffer (current-buffer)
2048 ;; yahtml-urls-local is buffer-local, so we must put
2049 ;; that into yahtml-urls here
2050 yahtml-urls (append yahtml-urls-private yahtml-urls-local)
2051 href (read-from-minibuffer
2052 "Change href to: " "" yahtml-url-completion-map))
2053 (if (string< "" href)
2054 (progn
2055 ;;(setq href ;??
2056 ;; (if yahtml-prefer-upcases (upcase href) (downcase href)))
2057 (delete-region b1 e1)
2058 (goto-char b1)
2059 (insert href))))
2060 ((setq attr (yahtml-on-assignment-p)) ;if on the assignment to attr
2061 (if (and (equal attr "class") ;treat "class" attribute specially
2062 (setq css (yahtml-css-get-element-completion-alist tag)))
2064 (setq new (yahtml-read-css css))
2065 ;;other than "class", read parameter normally
2066 (setq new (yahtml-read-parameter attr)))
2067 (goto-char (car (get 'yahtml-on-assignment-p 'region)))
2068 (delete-region (point) (cdr (get 'yahtml-on-assignment-p 'region)))
2069 (insert new))
2070 (t
2071 (save-excursion
2072 (if (= (aref tag 0) ?/) (setq tag (substring tag 1)))
2073 (or (= (char-after (point)) ?<) (skip-chars-backward "^<"))
2074 (skip-chars-forward "^A-Za-z")
2075 (set-marker q (point))
2076 (setq p (point))
2077 (yahtml-goto-corresponding-begend)
2078 (or (= (char-after (point)) ?<)
2079 (skip-chars-backward "^<"))
2080 (skip-chars-forward "^A-Za-z")
2081 (if (= (char-after (1- (point))) ?/)
2082 (progn
2083 (set-marker q (point))
2084 (goto-char p)))
2085 (setq tag (let ((completion-ignore-case t))
2086 (YaTeX-cplread-with-learning
2087 (format "Change `%s' to(default %s): "
2088 tag yahtml-last-begend)
2089 'default 'user 'tmp)))
2090 (delete-region (point) (progn (skip-chars-forward "^>") (point)))
2091 (if (string= "" tag) (setq tag yahtml-last-begend))
2092 (setq yahtml-last-begend
2093 (or (cdr (assoc tag yahtml-env-table)) tag)
2094 tag yahtml-last-begend)
2095 (setq tag (if yahtml-prefer-upcases (upcase tag) (downcase tag)))
2096 (insert (format "%s%s" tag (yahtml-addin tag)))
2097 (goto-char q)
2098 (set-marker q nil)
2099 (delete-region (point) (progn (skip-chars-forward "^>") (point)))
2100 (insert tag))))
2101 t))))
2103 (defun yahtml-change-command ()
2104 (let ((p (point)) (case-fold-search t) cmd par new
2105 (beg (make-marker)) (end (make-marker)))
2106 (skip-chars-backward "^<")
2107 (if (and
2108 (looking-at yahtml-command-regexp)
2109 (progn
2110 (set-marker beg (match-beginning 0))
2111 (set-marker end (match-end 0))
2112 t) ;for further work
2113 (progn
2114 (forward-char -1)
2115 (condition-case nil
2116 (forward-list 1)
2117 (error nil))
2118 (< p (point))))
2119 (progn
2120 (goto-char p)
2121 (if (setq par (yahtml-on-assignment-p))
2122 (progn
2123 (setq new (yahtml-read-parameter par))
2124 (set-marker beg (car (get 'yahtml-on-assignment-p 'region)))
2125 (set-marker end (cdr (get 'yahtml-on-assignment-p 'region))))
2126 (setq new
2127 (YaTeX-cplread-with-learning
2128 "Change form to: "
2129 'yahtml-form-table 'yahtml-user-form-table
2130 'yahtml-tmp-form-table)))
2131 (delete-region beg end)
2132 (goto-char beg)
2133 (set-marker beg nil)
2134 (set-marker end nil)
2135 (insert new)
2136 t)
2137 (goto-char p)
2138 nil)))
2140 (defun yahtml-change-* ()
2141 "Change current position's HTML tag (set)."
2142 (interactive)
2143 (cond
2144 ((yahtml-change-begend))
2145 ((yahtml-change-command))))
2147 ;;; ---------- commenting ----------
2149 (defun yahtml-comment-region (&optional uncom)
2150 "Comment out region or environment."
2151 (interactive)
2152 (let ((e (make-marker)) be beg p)
2153 (cond
2154 (;(marker-position (set-marker e (yahtml-on-begend-p)))
2155 (setq be (yahtml-on-begend-p))
2156 (save-excursion
2157 (setq p (point))
2158 (if (string-match "^/" be)
2159 (setq beg (progn (forward-line 1) (point)))
2160 (setq beg (progn (beginning-of-line) (point))))
2161 (goto-char p)
2162 (yahtml-goto-corresponding-begend)
2163 (if (string-match "^/" be)
2164 (beginning-of-line)
2165 (forward-line 1))
2166 (set-marker e (point))
2167 ;(comment-region beg (point) (if uncom (list 4)))))
2168 ))
2169 (t ;(comment-region (region-beginning) (region-end) (if uncom (list 4)))
2170 (setq beg (region-beginning))
2171 (set-marker e (region-end))))
2172 (if yahtml-translate-hyphens-when-comment-region
2173 (let ((yahtml-entity-reference-chars-alist-default nil)
2174 (yahtml-entity-reference-chars-alist '((?- . "#45")))
2175 yahtml-entity-reference-chars-regexp
2176 yahtml-entity-reference-chars-reverse-regexp)
2177 (yahtml-entity-reference-chars-setup)
2178 (funcall
2179 (if uncom 'yahtml-translate-reverse-region
2180 'yahtml-translate-region)
2181 beg e)))
2182 (comment-region beg e (if uncom (list 4)))
2183 (set-marker e nil)))
2185 (defun yahtml-uncomment-region ()
2186 (interactive)
2187 (yahtml-comment-region t))
2189 ;;; ---------- translate to entity references ----------
2190 (defvar yahtml-entity-reference-chars-alist-default
2191 ;'((?> . "gt") (?< . "lt") (?& . "amp") (?\" . "quot") (?' . "apos"))
2192 '((?> . "gt") (?< . "lt") (?& . "amp") (?\" . "quot"))
2193 "Default translation table from character to entity reference")
2194 (defvar yahtml-entity-reference-chars-alist nil
2195 "*Translation table from character to entity reference")
2196 (defvar yahtml-entity-reference-chars-regexp nil)
2197 (defvar yahtml-entity-reference-chars-reverse-regexp nil)
2199 (defun yahtml-entity-reference-chars-setup ()
2200 (let ((list (append yahtml-entity-reference-chars-alist-default
2201 yahtml-entity-reference-chars-alist)))
2202 (setq yahtml-entity-reference-chars-regexp "["
2203 yahtml-entity-reference-chars-reverse-regexp "&\\(")
2204 (while list
2205 (setq yahtml-entity-reference-chars-regexp
2206 (concat yahtml-entity-reference-chars-regexp
2207 (char-to-string (car (car list))))
2208 yahtml-entity-reference-chars-reverse-regexp
2209 (concat yahtml-entity-reference-chars-reverse-regexp
2210 (cdr (car list))
2211 (if (cdr list) "\\|")))
2212 (setq list (cdr list)))
2213 (setq yahtml-entity-reference-chars-regexp
2214 (concat yahtml-entity-reference-chars-regexp "]")
2215 yahtml-entity-reference-chars-reverse-regexp
2216 (concat yahtml-entity-reference-chars-reverse-regexp "\\);"))))
2218 (yahtml-entity-reference-chars-setup)
2220 (defun yahtml-translate-region (beg end)
2221 "Translate inhibited literals."
2222 (interactive "r")
2223 (save-excursion
2224 (save-restriction
2225 (narrow-to-region beg end)
2226 (let ((ct (append yahtml-entity-reference-chars-alist
2227 yahtml-entity-reference-chars-alist-default)))
2228 (goto-char beg)
2229 (while (re-search-forward yahtml-entity-reference-chars-regexp nil t)
2230 ;(setq c (preceding-char))
2231 (replace-match
2232 (concat "&" (cdr (assoc (preceding-char) ct)) ";")))))))
2234 (defun yahtml-translate-reverse-region (beg end)
2235 "Translate entity references to literals."
2236 (interactive "r")
2237 (save-excursion
2238 (save-restriction
2239 (narrow-to-region beg end)
2240 (let ((ct (append yahtml-entity-reference-chars-alist
2241 yahtml-entity-reference-chars-alist-default))
2242 ec)
2243 (goto-char beg)
2244 (while (re-search-forward
2245 yahtml-entity-reference-chars-reverse-regexp nil t)
2246 ;(setq c (preceding-char))
2247 (setq ec (YaTeX-match-string 1))
2248 (delete-region (match-end 0) (match-beginning 0))
2249 (insert (car (YaTeX-rassoc ec ct))))))))
2251 (defun yahtml-inner-environment-but (exclude &optional quick)
2252 "Return the inner environment but matches with EXCLUDE tag."
2253 (let (e (case-fold-search t))
2254 (save-excursion
2255 (while (and (setq e (YaTeX-inner-environment quick))
2256 (string-match exclude e))
2257 (goto-char (get 'YaTeX-inner-environment 'point))))
2258 e))
2260 ;;; ---------- filling ----------
2261 (defvar yahtml-saved-move-to-column (symbol-function 'move-to-column))
2262 (defun yahtml-move-to-column (col &optional force)
2263 (beginning-of-line)
2264 (let ((ccol 0))
2265 (while (and (> col ccol) (not (eolp)))
2266 (if (eq (following-char) ?\<)
2267 (progn
2268 (while (and (not (eq (following-char) ?\>))
2269 (not (eolp)))
2270 (forward-char))
2271 (or (eolp) (forward-char)))
2272 (or (eolp) (forward-char))
2273 (if (eq (preceding-char) ?\t)
2274 (let ((wd (- 8 (% (+ ccol 8) 8))))
2275 (if (and force (< col (+ ccol wd)))
2276 (progn
2277 (backward-char 1)
2278 (insert-char ?\ (- col ccol))
2279 (setq ccol col))
2280 (setq ccol (+ ccol wd))))
2281 (setq ccol (1+ ccol)))
2282 (if (and YaTeX-japan
2283 (or
2284 (and (fboundp 'char-category)
2285 (string-match "[chj]" (char-category (preceding-char))))
2286 (and (fboundp 'char-charset)
2287 (not (eq (char-charset (preceding-char)) 'ascii)))))
2288 (setq ccol (1+ ccol)))))
2289 (if (and force (> col ccol))
2290 (progn
2291 (insert-char ?\ (- col ccol))
2292 col)
2293 ccol)))
2295 (defun yahtml-fill-paragraph (arg)
2296 (interactive "P")
2297 (let*((case-fold-search t) (p (point)) fill-prefix
2298 (e (or (yahtml-inner-environment-but "^\\(a\\|p\\)\\b" t) "html"))
2299 indent
2300 (startp (get 'YaTeX-inner-environment 'point))
2301 (prep (string-match "^pre$" e))
2302 (ps1 (if prep (default-value 'paragraph-start)
2303 paragraph-start))
2304 (ps2 (if prep (concat (default-value 'paragraph-start)
2305 "$\\|^\\s *</?pre>")
2306 paragraph-start)))
2307 (save-excursion
2308 (unwind-protect
2309 (progn
2310 (if prep
2311 (fset 'move-to-column 'yahtml-move-to-column))
2312 (save-excursion
2313 (beginning-of-line)
2314 (indent-to-column (yahtml-this-indent))
2315 (setq fill-prefix
2316 (buffer-substring (point) (point-beginning-of-line)))
2317 (delete-region (point) (point-beginning-of-line)))
2318 (fill-region-as-paragraph
2319 (progn (re-search-backward paragraph-start nil t)
2320 (or (save-excursion
2321 (goto-char (match-beginning 0))
2322 (if (looking-at "<")
2323 (forward-list)
2324 (goto-char (match-end 0))
2325 (skip-chars-forward " \t>"))
2326 (if (looking-at "[ \t]*$")
2327 (progn (forward-line 1) (point))))
2328 (point)))
2329 (progn (goto-char p)
2330 (re-search-forward ps2 nil t)
2331 (match-beginning 0))))
2332 (fset 'move-to-column yahtml-saved-move-to-column)))))
2334 ;(defun yahtml-indent-new-commnet-line ()
2335 ; (unwind-protect
2336 ; (progn
2337 ; (fset 'move-to-column 'yahtml-move-to-column)
2338 ; (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft))))
2339 ; (fset 'move-to-column yahtml-saved-move-to-column)))
2341 ;;;
2342 ;;; ---------- indentation ----------
2343 ;;;
2344 (defun yahtml-indent-line ()
2345 "Indent a line (faster wrapper)"
2346 (interactive)
2347 (let (indent)
2348 (if (and (save-excursion
2349 (beginning-of-line) (skip-chars-forward "\t ")
2350 (not (looking-at "<")))
2351 (save-excursion
2352 (forward-line -1)
2353 (while (and (not (bobp)) (looking-at "^\\s *$"))
2354 (forward-line -1))
2355 (skip-chars-forward "\t ")
2356 (setq indent (current-column))
2357 (not (looking-at "<"))))
2358 (progn
2359 (save-excursion
2360 (beginning-of-line)
2361 (skip-chars-forward " \t")
2362 (or (= (current-column) indent)
2363 (YaTeX-reindent indent)))
2364 (and (bolp) (skip-chars-forward " \t")))
2365 (yahtml-indent-line-real))))
2367 (defun yahtml-this-indent ()
2368 (let ((envs "[uod]l\\|table\\|[ht][rhd0-6]\\|select\\|blockquote\\|center\\|menu\\|dir\\|font")
2369 (itemizing-envs "^\\([uod]l\\|menu\\|dir\\)$")
2370 (itms "<\\(dt\\|dd\\|li\\|t[rdh]\\|option\\)\\b")
2371 (excludes
2372 "\\(a\\|p\\|span\\|code\\|tt\\|em\\|u\\|i\\|big\\|small\\|font\\)\\b")
2373 inenv p col peol (case-fold-search t))
2374 (save-excursion
2375 (beginning-of-line)
2376 (setq inenv (or (yahtml-inner-environment-but excludes t)
2377 "html")
2378 col (get 'YaTeX-inner-environment 'indent)
2379 p (get 'YaTeX-inner-environment 'point)
2380 op nil))
2381 (save-excursion
2382 (cond
2383 ((string-match (concat "^\\(" envs "\\)") inenv)
2384 (save-excursion
2385 (beginning-of-line)
2386 (skip-chars-forward " \t")
2387 (cond ;lookup current line's tag
2388 ((looking-at (concat "</\\(" envs "\\)>"))
2389 col)
2390 ((looking-at itms)
2391 (+ col yahtml-environment-indent))
2392 ((and yahtml-hate-too-deep-indentation
2393 (looking-at (concat "<\\(" envs "\\)")))
2394 (+ col (* 2 yahtml-environment-indent)))
2395 ((and (< p (point))
2396 (string-match itemizing-envs inenv)
2397 (save-excursion
2398 (and
2399 (setq op (point))
2400 (goto-char p)
2401 (re-search-forward itms op t)
2402 (progn
2403 (if yahtml-indent-listing-constant
2404 (setq col (+ (current-column)
2405 (if yahtml-faithful-to-htmllint 1 2)))
2406 (skip-chars-forward "^>")
2407 (skip-chars-forward ">")
2408 (skip-chars-forward " \t")
2409 (setq col (if (looking-at "$")
2410 (+ col yahtml-environment-indent)
2411 (current-column))))))))
2412 col)
2413 (t
2414 (+ col yahtml-environment-indent)))))
2415 (t col)))))
2417 (defun yahtml-indent-line-real ()
2418 "Indent current line."
2419 (interactive)
2420 (YaTeX-reindent (yahtml-this-indent))
2421 (if (bolp) (skip-chars-forward " \t"))
2422 (let (peol col inenv)
2423 (if (and (setq inenv (yahtml-on-begend-p))
2424 (string-match
2425 (concat "^\\<\\(" yahtml-struct-name-regexp "\\)") inenv))
2426 (save-excursion
2427 (setq peol (point-end-of-line))
2428 (or (= (char-after (point)) ?<)
2429 (progn (skip-chars-backward "^<") (forward-char -1)))
2430 (setq col (current-column))
2431 (if (and (yahtml-goto-corresponding-begend t)
2432 (> (point) peol)) ;if on the different line
2433 (YaTeX-reindent col))))))
2435 ;(defun yahtml-fill-item ()
2436 ; "Fill item HTML version"
2437 ; (interactive)
2438 ; (let (inenv p fill-prefix peol (case-fold-search t))
2439 ; (setq inenv (or (YaTeX-inner-environment) "html")
2440 ; p (get 'YaTeX-inner-environment 'point))
2441 ; (cond
2442 ; ((string-match "^[uod]l" inenv)
2443 ; (save-excursion
2444 ; (if (re-search-backward "<\\(d[td]\\|li\\)>[ \t\n]*" p t)
2445 ; (progn
2446 ; (goto-char (match-end 0))
2447 ; (setq col (current-column)))
2448 ; (error "No <li>, <dt>, <dd>")))
2449 ; (save-excursion
2450 ; (end-of-line)
2451 ; (setq peol (point))
2452 ; (newline)
2453 ; (indent-to-column col)
2454 ; (setq fill-prefix (buffer-substring (point) (1+ peol)))
2455 ; (delete-region (point) peol)
2456 ; (fill-region-as-paragraph
2457 ; (progn (re-search-backward paragraph-start nil t) (point))
2458 ; (progn (re-search-forward paragraph-start nil t 2)
2459 ; (match-beginning 0)))))
2460 ; (t nil))))
2462 ;;;
2463 ;;; ---------- Lint and Browsing ----------
2464 ;;;
2465 (defun yahtml-browse-menu ()
2466 "Browsing menu"
2467 (interactive)
2468 (message "J)weblint p)Browse R)eload...")
2469 (let ((c (char-to-string (read-char))))
2470 (cond
2471 ((string-match "j" c)
2472 (yahtml-lint-buffer (current-buffer)))
2473 ((string-match "[bp]" c)
2474 (yahtml-browse-current-file))
2475 ((string-match "r" c)
2476 (yahtml-browse-reload)))))
2478 (if (fboundp 'wrap-function-to-control-ime)
2479 (wrap-function-to-control-ime 'yahtml-browse-menu t nil))
2481 (defvar yahtml-lint-buffer "*weblint*")
2483 (defun yahtml-lint-buffer (buf)
2484 "Call lint on buffer BUF."
2485 (require 'yatexprc)
2486 (interactive "bCall lint on buffer: ")
2487 (setq buf (get-buffer buf))
2488 (YaTeX-save-buffers)
2489 (YaTeX-typeset
2490 (concat yahtml-lint-program " "
2491 (file-name-nondirectory (buffer-file-name buf)))
2492 yahtml-lint-buffer "lint" "lint"))
2494 (defun yahtml-file-to-url (file)
2495 "Convert local unix file name to URL.
2496 If no matches found in yahtml-path-url-alist, return raw file name."
2497 (let ((list yahtml-path-url-alist) p url)
2498 (if (file-directory-p file)
2499 (setq file (expand-file-name yahtml-directory-index file))
2500 (setq file (expand-file-name file)))
2501 (if (string-match "^[A-Za-z]:/" file)
2502 (progn
2503 ;; (aset file 1 ?|) ;これは要らないらしい…
2504 (setq file (concat "///" file))))
2505 (while list
2506 (if (string-match (concat "^" (regexp-quote (car (car list)))) file)
2507 (setq url (cdr (car list))
2508 file (substring file (match-end 0))
2509 url (concat url file)
2510 list nil))
2511 (setq list (cdr list)))
2512 (or url (concat "file:" file))))
2514 (defun yahtml-url-to-path (file &optional basedir)
2515 "Convert local URL name to unix file name."
2516 (let ((list yahtml-path-url-alist) url realpath docroot
2517 (dirsufp (string-match "/$" file)))
2518 (setq basedir (or basedir
2519 (file-name-directory
2520 (expand-file-name default-directory))))
2521 (cond
2522 ((string-match "^/" file)
2523 (while list
2524 (if (file-directory-p (car (car list)))
2525 (progn
2526 (setq url (cdr (car list)))
2527 (if (string-match "\\(http://[^/]*\\)/" url)
2528 (setq docroot (substring url (match-end 1)))
2529 (setq docroot url))
2530 (cond
2531 ((string-match (concat "^" (regexp-quote docroot)) file)
2532 (setq realpath
2533 (expand-file-name
2534 (substring
2535 file
2536 (if (= (aref file (1- (match-end 0))) ?/)
2537 (match-end 0) ; "/foo"
2538 (min (1+ (match-end 0)) (length file)))) ; "/~foo"
2539 (car (car list))))))
2540 (if realpath
2541 (progn (setq list nil)
2542 (if (and dirsufp (not (string-match "/$" realpath)))
2543 (setq realpath (concat realpath "/")))))))
2544 (setq list (cdr list)))
2545 realpath)
2546 (t file))))
2548 (defun yahtml-browse-current-file ()
2549 "Call WWW browser on current file."
2550 (interactive)
2551 (basic-save-buffer)
2552 (yahtml-browse-html (yahtml-file-to-url (buffer-file-name))))
2554 (defun yahtml-browse-reload ()
2555 "Send `reload' event to netscape."
2556 (let ((pb "* WWW Browser *") (cb (current-buffer)))
2557 (cond
2558 ((string-match "[Nn]etscape" yahtml-www-browser)
2559 (if (get-buffer pb)
2560 (progn (set-buffer pb) (erase-buffer) (set-buffer cb)))
2561 ;;(or (get 'yahtml-netscape-sentinel 'url)
2562 ;; (error "Reload should be called after Browsing."))
2563 (put 'yahtml-netscape-sentinel 'url
2564 (yahtml-file-to-url (buffer-file-name)))
2565 (basic-save-buffer)
2566 (set-process-sentinel
2567 (setq yahtml-browser-process
2568 (start-process
2569 "browser" pb shell-file-name yahtml-shell-command-option ;"-c"
2570 (format "%s -remote 'reload'" yahtml-www-browser)))
2571 'yahtml-netscape-sentinel))
2572 (t
2573 (message "Sorry, RELOAD is supported only for Netscape.")))))
2575 ;;; ---------- Intelligent newline ----------
2576 (defun yahtml-intelligent-newline (arg)
2577 "Intelligent newline for HTML"
2578 (interactive "P")
2579 (let (env func)
2580 (end-of-line)
2581 (setq env (downcase (or (yahtml-inner-environment-but "^\\(a\\|p\\)\\b" t)
2582 "html")))
2583 (setq func (intern-soft (concat "yahtml-intelligent-newline-" env)))
2584 (newline)
2585 (if (and env func (fboundp func))
2586 ;; if intelligent line function is defined, call that
2587 (funcall func)
2588 ;; else do the default action
2589 (if (string-match yahtml-p-prefered-env-regexp env)
2590 (yahtml-insert-p)))))
2592 (defun yahtml-intelligent-newline-ul ()
2593 (interactive)
2594 (yahtml-insert-single "li")
2595 (or yahtml-always-/li yahtml-faithful-to-htmllint (insert " "))
2596 (yahtml-indent-line))
2598 (fset 'yahtml-intelligent-newline-ol 'yahtml-intelligent-newline-ul)
2600 (defun yahtml-intelligent-newline-dl ()
2601 (interactive)
2602 (let ((case-fold-search t))
2603 (if (save-excursion
2604 (re-search-backward "<\\(\\(dt\\)\\|\\(dd\\)\\)[ \t>]"
2605 (get 'YaTeX-inner-environment 'point) t))
2606 (cond
2607 ((match-beginning 2)
2608 (yahtml-insert-single "dd")
2609 (or yahtml-always-/dd yahtml-faithful-to-htmllint (insert " "))
2610 (setq yahtml-last-single-cmd "dt"))
2611 ((match-beginning 3)
2612 (yahtml-insert-single "dt")
2613 (or yahtml-always-/dt yahtml-faithful-to-htmllint (insert " "))
2614 (setq yahtml-last-single-cmd "dd")))
2615 (yahtml-insert-single "dt")
2616 (or yahtml-always-/li yahtml-faithful-to-htmllint (insert " "))
2617 (setq yahtml-last-single-cmd "dd"))
2618 (yahtml-indent-line)
2619 (and (string-match yahtml-p-prefered-env-regexp "dl")
2620 (string-equal yahtml-last-single-cmd "dt")
2621 (yahtml-insert-p nil))))
2623 (defun yahtml-intelligent-newline-select ()
2624 (interactive)
2625 (insert "<" (if yahtml-prefer-upcases "OPTION" "option") "> ")
2626 (yahtml-indent-line))
2628 (defun yahtml-intelligent-newline-style ()
2629 (interactive)
2630 (if (save-excursion
2631 (and
2632 (re-search-backward "<style\\|<!-- " nil t)
2633 (looking-at "<style")))
2634 (let (c)
2635 (yahtml-indent-line)
2636 (setq c (current-column))
2637 (insert "<!--\n")
2638 (YaTeX-reindent c)
2639 (insert "-->")
2640 (beginning-of-line)
2641 (open-line 1)
2642 (YaTeX-reindent c))))
2644 (defun yahtml-intelligent-newline-head ()
2645 (let ((title (read-string "Document title: "))
2646 (b "<title>") (e "</title>") p)
2647 (yahtml-indent-line)
2648 (insert (format "%s" (if yahtml-prefer-upcases (upcase b) b)))
2649 (setq p (point))
2650 (insert (format "%s%s" title (if yahtml-prefer-upcases (upcase e) e)))
2651 (if (string= "" title) (goto-char p))
2652 (setq yahtml-last-begend "body")))
2654 (defun yahtml-intelligent-newline-script ()
2655 (let ((p (point)) b)
2656 (if (save-excursion
2657 (and
2658 (setq b (re-search-backward "<script\\>" nil t))
2659 (re-search-forward
2660 "\\(javascript\\)\\|\\(tcl\\)\\|\\(vbscript\\)" p t)))
2661 (let ((js (match-end 1)) (tcl (match-end 2)) (vb (match-end 3))
2662 c (srcp (re-search-backward "src=" b t)))
2663 (goto-char p)
2664 (yahtml-indent-line)
2665 (setq c (current-column))
2666 (if srcp
2667 nil
2668 (insert "<!--\n" (cond (js "//") (tcl "#") (vb "'")) " -->")
2669 (beginning-of-line)
2670 (open-line 1)
2671 (YaTeX-reindent c))))))
2673 (defun yahtml-intelligent-newline-table ()
2674 (let ((cp (point)) (p (point)) tb rb (cols 0) th line (i 0) fmt
2675 (ptn "\\(<t[dh]\\>\\)\\|<t\\(r\\|head\\|body\\)\\>"))
2676 (cond
2677 ((save-excursion (setq tb (YaTeX-beginning-of-environment "table")))
2678 (while (and (setq rb (re-search-backward ptn tb t))
2679 (match-beginning 1))
2680 (setq th (looking-at "<th")) ;Remember if first-child is tr or not
2681 (goto-char (match-end 0))
2682 (skip-chars-forward " \t\n")
2683 (if (and (search-forward "colspan\\s *=" p t)
2684 (progn
2685 (skip-chars-forward "\"' \t\n")
2686 (looking-at "[0-9]+")))
2687 (setq cols (+ (string-to-int (YaTeX-match-string 0)) cols))
2688 (setq cols (1+ cols)))
2689 (goto-char rb)
2690 (setq p (point)))
2691 (if (> cols 0)
2692 (message "%s columns found. %s"
2693 cols (if YaTeX-japan "新しいtr(N)? 前のtrの複写?(D)?: "
2694 "New tr?(N) or Duplicate")))
2695 (cond
2696 ((and (> cols 0)
2697 (memq (read-char) '(?d ?D))) ;Duplication mode
2698 (setq line (YaTeX-buffer-substring (point) (1- cp))))
2699 (t ;empty cells
2700 (setq line "<tr>" i 0)
2701 (if (> cols 0)
2702 (while (> cols i)
2703 (setq line (concat line (if (and (= i 0) th) "<th></th>"
2704 "<td></td>"))
2705 th nil i (1+ i)))
2706 (setq fmt (read-string "`th' or `td' format: " "th td td"))
2707 (while (string-match "t\\(h\\)\\|td" fmt i)
2708 (setq line (concat line (if (match-beginning 1) "<th></th>"
2709 "<td></td>"))
2710 i (match-end 0))))
2711 (setq line (concat line "</tr>"))))
2712 (goto-char cp)
2713 (if th
2714 (message
2715 "Type `%s' to change td from/to th."
2716 (key-description (car (where-is-internal 'yahtml-change-*)))))
2717 (if (string< "" line)
2718 (progn
2719 (insert line)
2720 (goto-char (+ 8 cp))
2721 (yahtml-indent-line)))))))
2723 ;;; ---------- Marking ----------
2724 (defun yahtml-mark-begend ()
2725 "Mark current tag"
2726 (interactive)
2727 (YaTeX-beginning-of-environment)
2728 (let ((p (point)))
2729 (save-excursion
2730 (skip-chars-backward " \t" (point-beginning-of-line))
2731 (if (bolp) (setq p (point))))
2732 (push-mark p t))
2733 (yahtml-goto-corresponding-begend)
2734 (forward-list 1)
2735 (if (eolp) (forward-char 1)))
2737 ;;; ---------- complete marks ----------
2738 (defun yahtml-complete-mark ()
2739 "Complete &gt, &lt, &ampersand, and &quote."
2740 (interactive)
2741 (message "1:< 2:> 3:& 4:\" 5:' 6:nbsp")
2742 (let ((c (read-char)))
2743 (setq c (if (or (< c ?0) (> c ?7))
2744 (string-match (regexp-quote (char-to-string c)) "<>&\"")
2745 (- c ?1)))
2746 (if (or (< c 0) (> c 6))
2747 nil
2748 (insert (format "&%s;"
2749 (nth c '("lt" "gt" "amp" "quot" "apos" "nbsp")))))))
2752 ;;; ---------- jump to error line ----------
2753 (defun yahtml-prev-error ()
2754 "Jump to previous error seeing lint buffer."
2755 (interactive)
2756 (or (get-buffer yahtml-lint-buffer)
2757 (error "No lint program ran."))
2758 (YaTeX-showup-buffer yahtml-lint-buffer nil t)
2759 (yahtml-jump-to-error-line t))
2761 (defun yahtml-jump-to-error-line (&optional sit)
2762 (interactive "P")
2763 (let ((p (point)) (e (point-end-of-line)))
2764 (end-of-line)
2765 (if (re-search-backward yahtml-error-line-regexp nil t)
2766 (let ((f (if (string= "" (YaTeX-match-string 1))
2767 YaTeX-current-file-name
2768 (YaTeX-match-string 1)))
2769 (l (string-to-int (or (YaTeX-match-string 2)
2770 (YaTeX-match-string 3)))))
2771 (if sit (sit-for 1))
2772 (forward-line -1)
2773 (YaTeX-showup-buffer (YaTeX-switch-to-buffer f t) nil t)
2774 (goto-line l))
2775 (message "No line number usage"))))
2777 ;;; ---------- Style Sheet Support ----------
2778 (defvar yahtml-css-class-alist nil
2779 "Alist of elements vs. their classes")
2781 (defun yahtml-css-collect-classes-region (beg end &optional initial)
2782 (save-excursion
2783 (save-restriction
2784 (narrow-to-region beg end)
2785 (goto-char (point-min))
2786 (let ((alist initial) b e element class a)
2787 (setq b (point))
2788 (while (re-search-forward "\\({\\)\\|\\(@import\\)" nil t)
2789 (if (match-beginning 2)
2790 (let ((f (YaTeX-buffer-substring
2791 (progn (skip-chars-forward "^\"")(1+ (point)))
2792 (progn (forward-char 1)
2793 (skip-chars-forward "^\"")(point)))))
2794 (if (file-exists-p f)
2795 (setq alist
2796 (append alist (yahtml-css-collect-classes-file f)))))
2797 (setq e (point))
2798 (goto-char b)
2799 (while (re-search-forward ;ちょといい加減なREGEXP
2800 "\\([a-z*][-a-z0-9]*\\)?\\.\\([-a-z0-9][-a-z0-9]*\\)\\>"
2801 e t)
2802 (setq element (YaTeX-match-string 1)
2803 class (YaTeX-match-string 2))
2804 ;;if starts with period (match-string 1 is nil),
2805 ;;this is global class
2806 (setq element (downcase (or element "*")))
2807 (if (setq a (assoc element alist))
2808 (or (assoc class (cdr a))
2809 (setcdr a (cons (list class) (cdr a))))
2810 (setq alist (cons (list element (list class)) alist))))
2811 (goto-char (1- e))
2812 (search-forward "}" nil t)
2813 (setq b (point))))
2814 alist))))
2816 (defun yahtml-css-collect-classes-buffer (&optional initial)
2817 (interactive)
2818 (yahtml-css-collect-classes-region (point-min) (point-max) initial))
2820 (defun yahtml-css-collect-classes-file (file &optional initial)
2821 (let*((hilit-auto-highlight nil)
2822 (buf (get-buffer-create
2823 (format " *css-collection*%s" (file-name-nondirectory file))))
2824 (cb (current-buffer)))
2825 (unwind-protect
2826 (progn
2827 (set-buffer buf)
2828 (insert-file-contents file)
2829 (cd (or (file-name-directory file) "."))
2830 (yahtml-css-collect-classes-buffer initial))
2831 (if (eq buf cb)
2832 nil
2833 (kill-buffer buf)
2834 (set-buffer cb)))))
2836 (defun yahtml-css-scan-styles ()
2837 (save-excursion
2838 (goto-char (point-min))
2839 (set (make-local-variable 'yahtml-css-class-alist) nil)
2840 (let (b tag type e href alist)
2841 (while (re-search-forward "<\\(style\\|link\\)" nil t)
2842 (setq b (match-beginning 0)
2843 tag (YaTeX-match-string 1))
2844 (cond
2845 ((string-match "style" tag)
2846 (goto-char b)
2847 (save-excursion (forward-list 1) (setq e (point)))
2848 (cond
2849 ((search-forward "text/css" e 1) ;css definition starts
2850 (setq alist
2851 (yahtml-css-collect-classes-region
2852 (point) (progn (search-forward "</style>") (point))
2853 alist)))))
2854 ((and (string-match "link" tag)
2855 (stringp (setq type (yahtml-get-attrvalue "type")))
2856 (string-match "text/css" type)
2857 (setq href (yahtml-get-attrvalue "href"))
2858 (file-exists-p (yahtml-url-to-path href)))
2859 (setq alist
2860 (yahtml-css-collect-classes-file
2861 (yahtml-url-to-path href) alist))))
2862 (setq yahtml-css-class-alist alist)))))
2864 (defun yahtml-css-get-element-completion-alist (element)
2865 (let ((alist (cdr-safe (assoc (downcase element) yahtml-css-class-alist)))
2866 (global (cdr-safe (assoc "*" yahtml-css-class-alist))))
2867 (and (or alist global)
2868 (append alist global))))
2870 ;;; ---------- ----------
2872 ;;;
2873 ;;hilit19
2874 ;;;
2875 (defvar yahtml-default-face-table
2876 '(
2877 (form black/ivory white/hex-442233 italic)
2878 ))
2879 (defvar yahtml-hilit-patterns-alist
2880 '(
2881 'case-fold
2882 ;; comments
2883 ("<!--\\s " "-->" comment)
2884 ;; include&exec
2885 ("<!--#\\(include\\|exec\\|config\\|fsize\\|flastmod\\)" "-->" include)
2886 ;; string
2887 (hilit-string-find ?\\ string)
2888 (yahtml-hilit-region-tag "<\\(strong\\|b\\)\\>" bold)
2889 ("</?[uod]l>" 0 decl)
2890 ("<\\(di\\|dt\\|li\\|dd\\)>" 0 label)
2891 (yahtml-hilit-region-tag "<\\(em\\|i\\>\\)" italic)
2892 ;("<a\\s +href" "</a>" crossref) ;good for hilit19, but odd for font-lock..
2893 (yahtml-hilit-region-tag "<\\(a\\)\\s +href" crossref)
2894 (yahtml-hilit-region-tag-itself "</?\\sw+\\>" decl)
2895 ))
2897 (defun yahtml-hilit-region-tag (tag)
2898 "Return list of start/end point of <TAG> form."
2899 (if (re-search-forward tag nil t)
2900 (let ((m0 (match-beginning 0)) (e0 (match-end 0))
2901 (elm (YaTeX-match-string 1)))
2902 (skip-chars-forward "^>")
2903 (prog1
2904 (cons (1+ (point))
2905 (progn (re-search-forward (concat "</" elm ">") nil t)
2906 (match-beginning 0)))
2907 (goto-char e0)))))
2909 (defun yahtml-hilit-region-tag-itself (ptn)
2910 "Return list of start/end point of <tag options...> itself."
2911 (if (re-search-forward ptn nil t)
2912 (let ((m0 (match-beginning 0)) (e0 (match-end 0)))
2913 (skip-chars-forward "^<>")
2914 (if (eq (char-after (point)) ?<) nil
2915 (prog1
2916 (cons m0 (min (point-max) (1+ (point))))
2917 (goto-char e0))))))
2919 ;(setq hilit-patterns-alist (delq (assq 'yahtml-mode hilit-patterns-alist) hilit-patterns-alist))
2920 (and yahtml-use-hilit19
2921 (or (assq 'yahtml-mode hilit-patterns-alist)
2922 (setq hilit-patterns-alist
2923 (cons (cons 'yahtml-mode yahtml-hilit-patterns-alist)
2924 hilit-patterns-alist))))
2925 ;;;
2926 ;; for font-lock
2927 ;;;
2929 ; <<STATIC KEYWORDS BELOW NOT USED>>
2930 ;(defvar yahtml-font-lock-keywords
2931 ; '(
2932 ; ;; comments
2933 ; ("<!--\\s .* -->" . font-lock-comment-face)
2934 ; ;; include&exec
2935 ; ("<!--#\\(include\\|exec\\|config\\|fsize\\|flastmod\\).*-->"
2936 ; 0 font-lock-include-face keep)
2937 ; ;; string
2938 ; ;(hilit-string-find ?\\ string)
2939 ; ;(yahtml-hilit-region-tag "\\(em\\|strong\\)" bold)
2940 ; ("</?[uod]l>" 0 font-lock-keyword-face)
2941 ; ("<\\(di\\|dt\\|li\\|dd\\)>" 0 font-lock-label-face)
2942 ; ("<a\\s +href=.*</a>" (0 font-lock-crossref-face keep))
2943 ; ;(yahtml-hilit-region-tag-itself "</?\\sw+\\>" decl)
2944 ; ("</?\\sw+\\>" (yahtml-fontify-to-tagend nil nil))
2945 ; )
2946 ; "*Defualt font-lock-keywords for yahtml-mode.")
2947 (defvar yahtml-font-lock-keywords
2948 (YaTeX-convert-pattern-hilit2fontlock yahtml-hilit-patterns-alist)
2949 "Default fontifying patterns for yahtml-mode")
2951 (defun yahtml-font-lock-set-default-keywords ()
2952 (put 'yahtml-mode 'font-lock-defaults
2953 '(yahtml-font-lock-keywords nil t)))
2955 (if yahtml-use-font-lock
2956 (progn
2957 (if (and (boundp 'hilit-mode-enable-list) hilit-mode-enable-list)
2958 ;;for those who use both hilit19 and font-lock
2959 (if (eq (car hilit-mode-enable-list) 'not)
2960 (or (member 'yahtml-mode hilit-mode-enable-list)
2961 (nconc hilit-mode-enable-list (list 'yahtml-mode)))
2962 (setq hilit-mode-enable-list
2963 (delq 'yahtml-mode hilit-mode-enable-list))))
2964 (yahtml-font-lock-set-default-keywords)))
2966 (defun yahtml-font-lock-recenter (&optional arg)
2967 (interactive "P")
2968 (font-lock-mode -1) ;is stupid, but sure.
2969 (font-lock-mode 1))
2971 (run-hooks 'yahtml-load-hook)
2972 (provide 'yahtml)
2974 ; Local variables:
2975 ; fill-prefix: ";;; "
2976 ; paragraph-start: "^$\\| \\|;;;$"
2977 ; paragraph-separate: "^$\\| \\|;;;$"
2978 ; coding: sjis
2979 ; End: