yatex

view yahtml.el @ 627:f294195d0d3f

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