yatex

view yahtml.el @ 147:f9d3c2451d19

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