yatex

view yahtml.el @ 145:efc3819f749b

Recognize <!-- #lint CommandLine --> as yahtml-lint-program.
author yuuji@gentei.org
date Sun, 12 Sep 2010 10:59:55 +0900
parents b7b54906ac3b
children f9d3c2451d19
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; (c) 1994-2010 by HIROSE Yuuji [yuuji(@)yatex.org]
3 ;;; Last modified Sun Sep 12 10:58:31 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 (yahtml-make-optional-argument ;should be made generic?
911 "class" (yahtml-read-css a)))
912 (if (and (intern-soft addin) (fboundp (intern-soft addin))
913 (stringp (setq s (funcall (intern addin))))
914 (string< "" s))
915 (if (eq (aref s 0) ? ) s (concat " " s))
916 ""))))
918 (defvar yahtml-completing-buffer nil)
919 (defun yahtml-collect-labels (&optional file)
920 "Collect current buffers label (<?? name=...>).
921 If optional argument FILE is specified collect labels in FILE."
922 (let (list end)
923 (save-excursion
924 (set-buffer yahtml-completing-buffer)
925 (if file (let (hilit-auto-highlight)
926 (set-buffer (find-file-noselect file))))
927 (save-excursion
928 (goto-char (point-min))
929 (while ;(re-search-forward "<\\w+\\b" nil t)
930 (re-search-forward "\\(name\\|id\\)\\s *=" nil t)
931 ;(setq bound (match-end 0))
932 ;(search-forward ">" nil t)
933 (setq end (match-end 0))
934 (if (and ;(re-search-backward "\\(name\\|id\\)\\s *=" bound t)
935 (yahtml-on-assignment-p)
936 (progn
937 (goto-char end)
938 (skip-chars-forward " \t\n")
939 (looking-at "\"?#?\\([^\">]+\\)\"?\\b")))
940 (setq list (cons
941 (list (concat "#" (YaTeX-match-string 1)))
942 list))))
943 list))))
945 (defvar yahtml-url-completion-map nil "Key map used in URL completion buffer")
946 (if yahtml-url-completion-map nil
947 (setq yahtml-url-completion-map
948 (copy-keymap minibuffer-local-completion-map))
949 (define-key yahtml-url-completion-map "\t" 'yahtml-complete-url)
950 (define-key yahtml-url-completion-map " " 'yahtml-complete-url))
952 (defun yahtml-complete-url ()
953 "Complete external URL from history or local file name."
954 (interactive)
955 (let ((p (point)) initial i2 cmpl path dir file listfunc beg labels
956 (lim (YaTeX-minibuffer-begin))
957 (min (if (fboundp 'field-beginning) (field-beginning) (point-min))))
958 (setq initial (YaTeX-minibuffer-string))
959 (cond
960 ((string-match "^http:" initial)
961 (setq cmpl (try-completion initial yahtml-urls)
962 listfunc (list 'lambda nil
963 (list 'all-completions initial 'yahtml-urls))
964 beg min))
965 ((setq beg (string-match "#" initial))
966 (or (equal beg 0) ;begin with #
967 (progn
968 (setq path (substring initial 0 beg))
969 (if (string-match "^/" path)
970 (setq path (yahtml-url-to-path path)))))
971 (setq initial (substring initial beg))
972 (setq labels (yahtml-collect-labels path)
973 cmpl (try-completion initial labels)
974 listfunc (list 'lambda ()
975 (list 'all-completions
976 initial (list 'quote labels)))
977 beg (+ min beg)))
978 (t
979 (setq path (if (string-match "^/" initial)
980 (or (yahtml-url-to-path initial) initial)
981 initial))
982 (setq dir (or (file-name-directory path) ".")
983 file (file-name-nondirectory path)
984 initial file
985 cmpl (file-name-completion file dir)
986 listfunc (list 'lambda nil
987 (list 'file-name-all-completions
988 file dir))
989 beg (save-excursion (skip-chars-backward "^/" lim) (point)))))
990 (cond
991 ((stringp cmpl)
992 (if (string= initial cmpl)
993 (with-output-to-temp-buffer "*Completions*"
994 (princ "Possible completinos are:\n")
995 (princ
996 (mapconcat '(lambda (x) x) (funcall listfunc) "\n")))
997 (delete-region (point) beg)
998 (insert cmpl)))
999 ((null cmpl)
1000 (ding))
1001 ((eq t cmpl)
1002 (save-excursion
1003 (unwind-protect
1004 (progn
1005 (goto-char p)
1006 (insert " [Sole completion]"))
1007 (delete-region p (point-max))))))))
1010 ; Subject: [yatex:02849] Re: [yahtml] tilda in href tag
1011 ; From: Masayasu Ishikawa <mimasa@sfc.keio.ac.jp>
1012 ; To: yatex@arcadia.jaist.ac.jp
1013 ; Date: Mon, 31 May 1999 21:09:31 +0900
1014 ; RFC 2396 の "2.4.3. Excluded US-ASCII Characters" によると、以下の文字
1015 ; は必ずエスケープしないといけません。
1017 ; control = <US-ASCII coded characters 00-1F and 7F hexadecimal>
1018 ; space = <US-ASCII coded character 20 hexadecimal>
1019 ; delims = "<" | ">" | "#" | "%" | <">
1020 ; unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
1021 (defvar yahtml-unsafe-chars-regexp
1022 "[][\x0- \x7f <>%\"{}|\\^`]" ;#は除去する
1023 "Characters regexp which must be escaped in URI.")
1025 ; また、以下の文字は予約された用法以外に用いる場合にはエスケープしないと
1026 ; いけないことになっています。
1028 ; reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
1029 ; "$" | ","
1030 (defvar yahtml-unreserved-chars-regexp
1031 "[;/?:@&=+$,]"
1032 "Characters regexp which should be escaped in URI on certain conditions.
1033 Not used yet.")
1035 (defun yahtml-escape-chars-string (str)
1036 "Translate reserved chars to URL encoded string."
1037 (let ((p 0) (target "")
1038 (ask (eq yahtml-escape-chars 'ask)))
1039 (cond
1040 ((null yahtml-escape-chars) str)
1041 (t
1042 (while (and (string< "" str)
1043 (setq p (string-match yahtml-unsafe-chars-regexp str)))
1044 (if (and ask (y-or-n-p (format "Escape char [%c] of `%s'"
1045 (aref str p) (substring str 0 (1+ p)))))
1046 (setq target (concat target
1047 (substring str 0 p)
1048 (format "%%%x" (aref str p))))
1049 (setq target (concat target (substring str 0 (1+ p)))))
1050 (setq str (substring str (1+ p))))
1051 (concat target str)))))
1053 (defun yahtml-escape-chars-region (beg end)
1054 "Translate reserved chars to encoded string in the region."
1055 (interactive "r")
1056 (save-excursion
1057 (let ((e (set-marker (make-marker) end)) c m yes)
1058 (goto-char beg)
1059 (while (and (< (point) e)
1060 (re-search-forward
1061 (concat yahtml-unsafe-chars-regexp "\\|"
1062 yahtml-unreserved-chars-regexp) e t))
1063 (sit-for 0)
1064 ; (setq m (buffer-modified-p)
1065 ; c (char-after (1- (point))))
1066 ; (save-excursion (backward-char 1) (insert " ==>"))
1067 ; (unwind-protect
1068 ; (setq yes (y-or-n-p (format "Replace: [%c]" c)))
1069 ; (save-excursion
1070 ; (backward-char 1)
1071 ; (delete-backward-char 4))
1072 ; (set-buffer-modified-p m))
1073 (message "Replace: [%c] (y or n):" (setq c (char-after (1- (point)))))
1074 (if (memq (read-char) '(?y ?Y))
1075 (progn
1076 (delete-region (match-beginning 0) (match-end 0))
1077 (insert (format "%%%x" c)))))
1078 (set-marker e nil))))
1079 ;; ab%defgls/.|
1081 (defun yahtml:a ()
1082 "Add-in function for <a>"
1083 (let ((href ""))
1084 (setq yahtml-completing-buffer (current-buffer)
1085 yahtml-urls (append yahtml-urls-private yahtml-urls-local)
1086 href (yahtml-escape-chars-string
1087 (read-from-minibuffer "href: " "" yahtml-url-completion-map)))
1088 (prog1
1089 (concat (yahtml-make-optional-argument
1090 "href" href)
1091 (yahtml-make-optional-argument
1092 "name" (read-string "name: ")))
1093 (if (and (string-match "^http://" href)
1094 (null (assoc href yahtml-urls-private))
1095 (null (assoc href yahtml-urls-local)))
1096 (YaTeX-update-table
1097 (list href)
1098 'yahtml-urls-private 'yahtml-urls-private 'yahtml-urls-local)))))
1100 (defvar yahtml-parameters-completion-alist
1101 '(("align" ("top") ("middle") ("bottom") ("left") ("right") ("center"))
1102 ("clear" ("left") ("right") ("center") ("all") ("none"))
1103 ("lang" ("ja") ("en") ("kr") ("ch") ("fr"))
1104 ("src" . file) ("file" . file)
1105 ("background" . file)
1106 ("class file name" . file) ("data" . file)
1107 ("method" ("POST") ("GET"))
1108 ("rev" . yahtml-link-types-alist)
1109 ("rel" . yahtml-link-types-alist)
1110 ("type" . yahtml-content-types-alist)
1111 ("codetype" . yahtml-content-types-alist)
1112 ("http-equiv" ("Refresh"))))
1114 (defvar yahtml-link-types-alist
1115 '(("alternate") ("stylesheet") ("start") ("next") ("prev")
1116 ("contents") ("index") ("glossary") ("chapter") ("section")
1117 ("subsection") ("appendix") ("help") ("bookmark")))
1119 (defvar yahtml-content-types-alist
1120 '(("text/css") ("text/html") ("text/plain") ("text/richtext")
1121 ("text/sgml") ("text/xml")
1122 ("text/javascript") ("text/tcl") ("text/vbscript")
1123 ("application/octet-stream") ("application/postscript") ("application/pdf")
1124 ("application/java")
1125 ("image/jpeg") ("image/gif") ("image/tiff") ("image/png") ("video/mpeg"))
1126 "Alist of content-types")
1128 (defun yahtml-read-parameter (par &optional default alist)
1129 (let* ((alist
1130 (cdr-safe (assoc (downcase par)
1131 (or alist yahtml-parameters-completion-alist))))
1132 (prompt (concat par ": "))
1133 v)
1134 (cond
1135 ((eq alist 'file)
1136 (let ((insert-default-directory))
1137 (read-file-name prompt "" default nil "")))
1138 ((and alist (symbolp alist))
1139 (completing-read prompt (symbol-value alist) nil nil default))
1140 (alist
1141 (completing-read prompt alist nil nil default))
1142 (t
1143 (read-string prompt default)))))
1145 (defun yahtml-make-optional-argument (opt arg)
1146 "Make optional argument string."
1147 (if (or (null arg) (string= "" arg))
1148 ""
1149 (concat " "
1150 (if yahtml-prefer-upcase-attributes (upcase opt) (downcase opt))
1151 "=\"" arg "\"")))
1153 (defun yahtml:html ()
1154 "Add-in for <html>"
1155 (setq yahtml-last-begend "head" yahtml-last-typeface-cmd "head")
1156 (yahtml-make-optional-argument
1157 "lang" (yahtml-read-parameter "lang" (if YaTeX-japan "ja"))))
1159 (defun yahtml:head ()
1160 "Add-in for <head>"
1161 (setq yahtml-last-begend "title" yahtml-last-typeface-cmd "title")
1162 "")
1164 (defun yahtml:body ()
1165 "Add-in function for <body>"
1166 (cond
1167 (yahtml-html4-strict nil)
1168 (t
1169 (let ((b (read-string "bgcolor="))
1170 (bg (yahtml-read-parameter "background" ""))
1171 (x (read-string "text color="))
1172 (l (read-string "link color="))
1173 (v (read-string "vlink color=")))
1174 (concat
1175 (yahtml-make-optional-argument "bgcolor" b)
1176 (yahtml-make-optional-argument "background" bg)
1177 (yahtml-make-optional-argument "text" x)
1178 (yahtml-make-optional-argument "link" l)
1179 (yahtml-make-optional-argument "vlink" v))))))
1181 (defun yahtml-make-style-parameter (proplist)
1182 "Make CSS property definitions in style attribute."
1183 (mapconcat
1184 '(lambda (x) (if (and (cdr x) (string< "" (cdr x)))
1185 (format "%s: %s;" (car x) (cdr x))))
1186 (delq nil proplist)
1187 " "))
1189 (defun yahtml:img ()
1190 "Add-in function for <img>"
1191 (let ((src (yahtml-read-parameter "src"))
1192 (alg (yahtml-read-parameter "align"))
1193 alt
1194 (brd (read-string "border="))
1195 (l yahtml-prefer-upcase-attributes)
1196 info width height bytes comments)
1197 (and (stringp src) (string< "" src) (file-exists-p src)
1198 (setq info (yahtml-get-image-info src))
1199 (car info)
1200 (setq width (int-to-string (car info))
1201 height (int-to-string (car (cdr info)))
1202 bytes (car (cdr (cdr info)))
1203 comments (nth 4 info)))
1204 (if info
1205 (setq alt
1206 (YaTeX-replace-formats
1207 yahtml:img-default-alt-format
1208 (list (cons "x" width)
1209 (cons "y" height)
1210 (cons "s" (int-to-string bytes))
1211 (cons "f" (file-name-nondirectory src))
1212 (cons "c" (car comments))))))
1214 (setq alt (yahtml-read-parameter "alt" alt))
1215 (setq width (yahtml-read-parameter "width" width)
1216 height (yahtml-read-parameter "height" height))
1217 (concat (if l "SRC" "src") "=\"" src "\""
1218 (yahtml-make-optional-argument "alt" alt)
1219 (yahtml-make-optional-argument "width" width)
1220 (yahtml-make-optional-argument "height" height)
1221 (if yahtml-html4-strict
1222 (yahtml-make-optional-argument
1223 "style"
1224 (if (or brd alg)
1225 (yahtml-make-style-parameter
1226 (list
1227 (if (string< "" alg)
1228 (cons "align" alg))
1229 (if (string< "" brd)
1230 (cons "border"
1231 (format "%dpx" (string-to-int brd))))))))
1232 (concat
1233 (yahtml-make-optional-argument "border" brd)
1234 (yahtml-make-optional-argument "align" alg))))))
1236 (defun yahtml-file-truename (file)
1237 (cond
1238 ((fboundp 'file-truename) (file-truename (expand-file-name file)))
1239 (t (let ((new file))
1240 (while (and (stringp (setq new (nth 0 (file-attributes file))))
1241 (not (equal new file)))
1242 (setq file new))
1243 file))))
1245 (defun yahtml-hex-value (point length &optional little-endian)
1246 "Return the hex value the POINT positions LENGTH byte stream represents.
1247 Optional third argument LITTLE-ENDIAN is self extplanatory."
1248 (setq point (1+ point)) ;translate file offset to Emacs's point value
1249 (let ((mlt 1)
1250 (pos (if little-endian point (+ point length -1)))
1251 (direc (if little-endian 1 -1))
1252 (value 0))
1253 (while (> length 0)
1254 (setq value (+ value (* mlt (char-after pos)))
1255 pos (+ pos direc)
1256 mlt (* mlt 256)
1257 length (1- length)))
1258 value))
1260 (defun yahtml-get-image-info (file)
1261 "Return the information on the image file FILE.
1262 Returns list of '(WIDTH HEIGHT BYTES DEPTH COMMENTLIST)."
1263 (save-excursion
1264 (let*((tmpbuf (get-buffer-create " *imgheader*"))
1265 width height bytes depth comment
1266 (file-coding-system-alist (list (cons "." 'no-conversion))) ;20
1267 (file-coding-system-for-read (and (boundp '*noconv*) *noconv*)) ;19
1268 (coding-system-for-read 'no-conversion)
1269 (seekpoint 1)
1270 c1 c2 c3 c4 beg end
1271 (case-fold-search nil))
1272 (setq bytes (nth 7 (file-attributes (yahtml-file-truename file))))
1273 (set-buffer tmpbuf)
1274 (if (boundp 'mc-flag) (set (make-local-variable 'mc-flag) nil))
1275 (erase-buffer)
1276 (if (fboundp 'set-buffer-multibyte) (set-buffer-multibyte nil))
1277 (unwind-protect
1278 (progn
1279 (message "Inspecting image information of %s..." file)
1280 ;; Read 4bytes-more than inspection-bytes in case that
1281 ;; JPEG marker delimiter (4bytes) is on the alignment.
1282 (YaTeX-insert-file-contents
1283 file nil 0 (+ yahtml-image-inspection-bytes 4))
1284 (goto-char (point-min)) ;assertion
1285 (setq c1 (char-after 1) ;cache first 4 bytes
1286 c2 (char-after 2)
1287 c3 (char-after 3)
1288 c4 (char-after 4))
1289 (cond
1290 ((and (eq c1 ?\377) (eq c2 ?\330)) ; 0xff 0xd8
1291 ;;JPEG images need JPEG markers inspection
1292 ;;JPEG markers consist of [ 0xff ID(B) LEN(S) CONTENTS... ]
1293 ;; Warning: here seekpoint is measured by Emacs's point value
1294 ;; while yahtml-hex-vale requires file offset
1295 (setq seekpoint 3) ;where the first JPEG marker exists
1296 (catch 'exit
1297 (while (< seekpoint (- (buffer-size) 4))
1298 (cond
1299 ((not (eq (char-after seekpoint) ?\377))
1300 ;maybe corrupted, exit from loop
1301 (throw 'exit t))
1302 ((memq
1303 (char-after (1+ seekpoint))
1304 '(?\300 ?\301 ?\302 ?\303
1305 ?\305 ?\306 ?\307 ?\311 ?\312 ?\313 ?\315 ?\316 ?\317))
1306 ;;'(192 193 194 195 197 198 199 201 202 203 205 206 207
1307 ;;found!
1308 (setq height (yahtml-hex-value (+ seekpoint 4) 2)
1309 width (yahtml-hex-value (+ seekpoint 6) 2)
1310 depth (yahtml-hex-value (+ seekpoint 3) 1)))
1311 ((eq (char-after (1+ seekpoint)) ?\376) ;0xFE = comment
1312 ;; JPEG comment area
1313 (setq beg (+ seekpoint 2 2)
1314 end (+ seekpoint
1315 (yahtml-hex-value (1+ seekpoint) 2) 2))
1316 (setq comment (cons (buffer-substring beg end) comment)))
1317 (t ;other markers
1318 nil)) ;just skip it
1319 (setq seekpoint (+ seekpoint 2)
1320 seekpoint (+ seekpoint
1321 (yahtml-hex-value (1- seekpoint) 2))))))
1322 ((and (eq c1 ?\211) ;0x89
1323 (eq c2 ?P) (eq c3 ?N) (eq c4 ?G))
1324 ;;PNG Image data X=@0x10(L), Y=@0x14(L), D=@0x18(B)
1325 (setq width (yahtml-hex-value 16 4)
1326 height (yahtml-hex-value 20 4)
1327 depth (yahtml-hex-value 24 1)))
1328 ((looking-at "GIF8")
1329 ;;GIF Image data X=@0x6(leshort), Y=@0x8(leshort)
1330 (setq width (yahtml-hex-value 6 2 t)
1331 height (yahtml-hex-value 8 2 t)))
1332 ((looking-at "BM")
1333 ;;# OS/2, Windoze BMP files
1334 ;;@0x0e = 12 -> OS/2 1.x - X=@0x12(leshort), Y=@0x14(leshort)
1335 ;;@0x0e = 64 -> OS/2 2.x - X=@0x12(leshort), Y=@0x14(leshort)
1336 ;;@0x0e = 40 -> Windows 3.x - X=@0x12(lelong), Y=@0x16(lelong)
1337 (cond
1338 ((eq (yahtml-hex-value 14 2 t) 40)
1339 (setq width (yahtml-hex-value 18 4 t)
1340 height (yahtml-hex-value 22 4 t)))
1341 (t
1342 (setq width (yahtml-hex-value 18 2 t)
1343 height (yahtml-hex-value 20 2 t)))))))
1344 (message "")
1345 (kill-buffer tmpbuf))
1346 (list width height bytes depth (nreverse comment)))))
1348 (defun yahtml:form ()
1349 "Add-in function `form' input format"
1350 (concat
1351 " " (if yahtml-prefer-upcase-attributes "METHOD" "method") "="
1352 (completing-read "Method: " '(("POST") ("GET")) nil t)
1353 " " (if yahtml-prefer-upcase-attributes "ACTION" "action") "=\""
1354 (read-string "Action: ") "\""))
1356 (defun yahtml:select ()
1357 "Add-in function for `select' input format"
1358 (setq yahtml-last-single-cmd "option")
1359 (concat " " (if yahtml-prefer-upcase-attributes "NAME" "name") "=\""
1360 (read-string "name: ") "\""))
1362 (defun yahtml:ol ()
1363 "Add-in function for <ol>"
1364 (setq yahtml-last-single-cmd "li")
1365 (let ((start (read-string "start="))
1366 (type (completing-read
1367 "type=" '(("1") ("a") ("A") ("i") ("I")) nil t)))
1368 (concat
1369 (yahtml-make-optional-argument "start" start)
1370 (yahtml-make-optional-argument "type" type))))
1371 (defun yahtml:ul ()
1372 (setq yahtml-last-single-cmd "li") "")
1373 (defun yahtml:dl ()
1374 (setq yahtml-last-single-cmd "dt") "")
1375 (defun yahtml:dt ()
1376 (setq yahtml-last-single-cmd "dd") "")
1378 (defun yahtml:p ()
1379 (if yahtml-html4-strict nil
1380 (let ((alg (yahtml-read-parameter "align")))
1381 (yahtml-make-optional-argument "align" alg))))
1383 (defvar yahtml-input-types
1384 '(("text") ("password") ("checkbox") ("radio") ("submit")
1385 ("reset") ("image") ("hidden") ("file")))
1387 (defun yahtml:input ()
1388 "Add-in function for `input' form"
1389 (let ((size "") name type value checked (maxlength "")
1390 (l yahtml-prefer-upcase-attributes))
1391 (setq name (read-string "name: ")
1392 type (completing-read "type (default=text): "
1393 yahtml-input-types nil t)
1394 value (read-string "value: "))
1395 (if (string-match "text\\|password\\|^$" type)
1396 (setq size (read-string "size: ")
1397 maxlength (read-string "maxlength: ")))
1398 (concat
1399 (if l "NAME" "name") "=\"" name "\""
1400 (yahtml-make-optional-argument "type" type)
1401 (yahtml-make-optional-argument "value" value)
1402 (yahtml-make-optional-argument "size" size)
1403 (yahtml-make-optional-argument "maxlength" maxlength))))
1405 (defun yahtml:textarea ()
1406 "Add-in function for `textarea'"
1407 (interactive)
1408 (let (name rows cols)
1409 (setq name (read-string "Name: ")
1410 cols (read-string "Columns: ")
1411 rows (read-string "Rows: "))
1412 (concat
1413 (concat (if yahtml-prefer-upcase-attributes "NAME=" "name=")
1414 "\"" name "\"")
1415 (yahtml-make-optional-argument "cols" cols)
1416 (yahtml-make-optional-argument "rows" rows))))
1418 (defun yahtml:table ()
1419 "Add-in function for `table'"
1420 (let ((b (read-string "border="))
1421 (a (yahtml-read-parameter
1422 "align" nil '(("align" ("right")("center"))))))
1423 (if yahtml-html4-strict
1424 (yahtml-make-optional-argument
1425 "style"
1426 (if (or (string< "" b) (string< "" a))
1427 (yahtml-make-style-parameter
1428 (append
1429 (if (string< "" b)
1430 (list
1431 (cons "border" (format "%dpx solid" (string-to-int b)))
1432 (cons "border-collapse" "collapse")))
1433 (if (string< "" a)
1434 (cond
1435 ((string-match "right" a)
1436 (list (cons "margin-left" "auto")
1437 (cons "margin-right" "0")))
1438 ((string-match "center" a)
1439 (list (cons "margin-left" "auto")
1440 (cons "margin-right" "auto")))))))))
1441 (concat
1442 (yahtml-make-optional-argument "border" b)
1443 (yahtml-make-optional-argument "align" a)))))
1445 ;(fset 'yahtml:caption 'yahtml:p)
1446 (defun yahtml:caption ()
1447 "Add-in function for `caption' in table tag"
1448 (let ((par (yahtml-read-parameter "align")))
1449 (if yahtml-html4-strict
1450 (yahtml-make-optional-argument
1451 "style" (if par (yahtml-make-style-parameter
1452 (list (cons "caption-side" par)))))
1453 (yahtml-make-optional-argument "align" par))))
1455 (defun yahtml:font ()
1456 "Add-in function for `font'"
1457 (concat
1458 (yahtml-make-optional-argument "color" (read-string "color="))
1459 (yahtml-make-optional-argument "size" (read-string "size="))))
1461 (defun yahtml:style ()
1462 "Add-in function for `style'"
1463 (yahtml-make-optional-argument
1464 "type" (read-string "type=" "text/css")))
1466 (defun yahtml:script ()
1467 "Add-in function for `script'"
1468 (concat
1469 (yahtml-make-optional-argument
1470 "type" (yahtml-read-parameter "type" "text/javascript"))
1471 (yahtml-make-optional-argument
1472 "src" (yahtml-read-parameter "src" ""))))
1474 (defun yahtml:tr ()
1475 "Add-in function for `tr'"
1476 (setq ;yahtml-last-begend "td" ;; which do you prefer?
1477 yahtml-last-typeface-cmd "td")
1478 "")
1480 (defun yahtml:link ()
1481 "Add-in function for `link' (まだちょっと良く分かってない)"
1482 (let (rel rev type href)
1483 (setq rel (yahtml-read-parameter "rel"))
1484 (cond
1485 ((equal rel "")
1486 (concat (yahtml-make-optional-argument
1487 "rev" (yahtml-read-parameter "rev"))
1488 (yahtml-make-optional-argument
1489 "href" (yahtml-read-parameter "href")
1490 ;;他に良く使うのって何?
1491 )))
1492 ((string-match "stylesheet" rel)
1493 (concat
1494 (yahtml-make-optional-argument "rel" rel)
1495 (yahtml-make-optional-argument
1496 "type" (yahtml-read-parameter "type" "text/css"))
1497 (progn
1498 (setq href
1499 (read-from-minibuffer "href: " "" yahtml-url-completion-map))
1500 (if (string< "" href)
1501 (progn
1502 (if (and (file-exists-p (yahtml-url-to-path href))
1503 (y-or-n-p "Load css symbols now? "))
1504 (setq yahtml-css-class-alist
1505 (yahtml-css-collect-classes-file
1506 (yahtml-url-to-path href) yahtml-css-class-alist)))
1507 (message "")
1508 (yahtml-make-optional-argument "href" href))))))
1509 (t
1510 (concat
1511 (yahtml-make-optional-argument "rel" rel)
1512 (yahtml-make-optional-argument
1513 "type" (yahtml-read-parameter "type" "text/css"))
1514 (yahtml-make-optional-argument
1515 "href"
1516 (read-from-minibuffer "href: " "" yahtml-url-completion-map)))))))
1518 (defvar yahtml:meta-names
1519 '(("name" ("keywords")("author")("copyright")("date")("GENERATOR"))))
1521 (defun yahtml:meta ()
1522 (let ((name (yahtml-make-optional-argument
1523 "name"
1524 (yahtml-read-parameter "name" nil yahtml:meta-names)))
1525 http-equiv content)
1526 (if (string= "" name)
1527 (if (string-match
1528 "Content-type"
1529 (setq http-equiv (yahtml-make-optional-argument
1530 "http-equiv"
1531 (yahtml-read-parameter "http-equiv" nil))))
1532 (error "It's very bad idea to set Content-type in META. %s"
1533 "See docs/qanda")
1534 (concat http-equiv
1535 (yahtml-make-optional-argument
1536 "content" (yahtml-read-parameter "content"))))
1537 (concat
1538 name
1539 (yahtml-make-optional-argument
1540 "content"
1541 (cond
1542 ((string-match "date" name)
1543 (read-string "Date: " (current-time-string)))
1544 ((string-match "author" name)
1545 (read-string "Author: "
1546 (if (and (user-full-name) (string< "" (user-full-name)))
1547 (user-full-name)
1548 (user-login-name))))
1549 ((string-match "GENERATOR" name)
1550 (setq content (read-string "Generator: " "User-agent: "))
1551 (if (string-match "yahtml" content)
1552 (message "Thank you!"))
1553 content)
1554 (t (read-string (concat name ": ")))))))))
1556 (defun yahtml:br ()
1557 (yahtml-make-optional-argument "clear" (yahtml-read-parameter "clear")))
1559 (defun yahtml:object ()
1560 (let ((codetype (yahtml-read-parameter "codetype" "application/java"))
1561 data classid)
1562 (cond
1563 ((string-match "java" codetype)
1564 (let ((completion-ignored-extensions
1565 ;;any extensions except ".class"
1566 '(".java" ".html" ".htm" ".gif" ".jpg" ".jpeg" ".png")))
1567 (setq classid (concat "java:"
1568 (yahtml-read-parameter "class file name"))))
1569 (concat
1570 (yahtml-make-optional-argument "codetype" codetype)
1571 (yahtml-make-optional-argument "classid" classid)
1572 (yahtml-make-optional-argument
1573 "width" (yahtml-read-parameter "width"))
1574 (yahtml-make-optional-argument
1575 "height" (yahtml-read-parameter "height"))
1576 (yahtml-make-optional-argument
1577 "align" (yahtml-read-parameter "align"))))
1578 (t
1579 ""))))
1581 (defun yahtml:abbr ()
1582 "Add-in function for abbr."
1583 (yahtml-make-optional-argument "title" (yahtml-read-parameter "title")))
1585 (defun yahtml:button ()
1586 (concat
1587 (yahtml-make-optional-argument
1588 "name" (yahtml-read-parameter "name"))
1589 (yahtml-make-optional-argument
1590 "type" (yahtml-read-parameter
1591 "type" "button" '(("submit")("reset")("button"))))
1592 (yahtml-make-optional-argument
1593 "value" (yahtml-read-parameter "value"))))
1595 ;;; ---------- Simple tag ----------
1596 (defun yahtml-insert-tag (region-mode &optional tag)
1597 "Insert <TAG> </TAG> and put cursor inside of them."
1598 (interactive "P")
1599 (setq yahtml-current-completion-type 'inline)
1600 (or tag
1601 (let ((completion-ignore-case t))
1602 (setq tag
1603 (YaTeX-cplread-with-learning
1604 (format "Tag %s(default %s): "
1605 (if region-mode "region: " "") yahtml-last-typeface-cmd)
1606 'yahtml-typeface-table 'yahtml-user-typeface-table
1607 'yahtml-tmp-typeface-table))))
1608 (if (string= "" tag) (setq tag yahtml-last-typeface-cmd))
1609 (setq tag (or (cdr (assoc tag yahtml-typeface-table)) tag))
1610 (setq yahtml-last-typeface-cmd tag
1611 tag (funcall (if yahtml-prefer-upcases 'upcase 'downcase) tag))
1612 (if region-mode
1613 (if (if (string< "19" emacs-version) (mark t) (mark))
1614 (save-excursion
1615 (if (> (point) (mark)) (exchange-point-and-mark))
1616 (insert (format "<%s%s>" tag (yahtml-addin tag)))
1617 (exchange-point-and-mark)
1618 (insert "</" tag ">"))
1619 (message "No mark set now"))
1620 (insert (format "<%s%s>" tag (yahtml-addin tag)))
1621 (save-excursion (insert (format "</%s>" tag)))))
1623 (defun yahtml-insert-tag-region (&optional tag)
1624 "Call yahtml-insert-tag with region mode."
1625 (interactive)
1626 (yahtml-insert-tag t tag))
1628 (defvar yahtml-need-single-closer nil) ;for test
1629 (defun yahtml-insert-single (cmd)
1630 "Insert <CMD>."
1631 (interactive
1632 (list
1633 (let ((completion-ignore-case t))
1634 (YaTeX-cplread-with-learning
1635 (format "Command%s: "
1636 (if yahtml-last-single-cmd
1637 (concat "(default " yahtml-last-single-cmd ")") ""))
1638 'yahtml-single-cmd-table 'yahtml-user-single-cmd-table
1639 'yahtml-tmp-single-cmd-table))))
1640 (if (string= "" cmd) (setq cmd yahtml-last-single-cmd))
1641 (setq yahtml-last-single-cmd
1642 (or (cdr (assoc cmd yahtml-single-cmd-table)) cmd))
1643 (setq cmd (funcall (if yahtml-prefer-upcases 'upcase 'downcase)
1644 yahtml-last-single-cmd))
1645 (insert (format "<%s%s%s>"
1646 cmd
1647 (yahtml-addin cmd)
1648 (if (and yahtml-need-single-closer
1649 (assoc cmd '(("br")("hr"))))
1650 " /" "")))
1651 (if (assoc cmd yahtml-env-table)
1652 (save-excursion (insert (format "</%s>" cmd)))))
1654 (defun yahtml-insert-p (&optional arg)
1655 "Convenient function to insert <p></p>"
1656 (interactive "P")
1657 (if (or yahtml-always-/p arg) (yahtml-insert-tag arg "p")
1658 (yahtml-insert-single "p")))
1660 (defun yahtml-insert-amps (arg)
1661 "Insert char-entity references via ampersand"
1662 ;; Thanks; http://www.tsc.co.jp/~asada/html/wdg40_f/entities/
1663 (interactive "P")
1664 (let*((mess "") c
1665 (list (append yahtml-entity-reference-chars-alist-default
1666 yahtml-entity-reference-chars-alist))
1667 (l list))
1668 (while l
1669 (setq mess (format "%s %c" mess (car (car l)) (cdr (car l)))
1670 l (cdr l)))
1671 (message "Char-entity reference: %s SPC=& RET=&; Other=&#..;" mess)
1672 (setq c (read-char))
1673 (cond
1674 ((equal c (car-safe (assoc c list)))
1675 (insert (format "&%s;" (cdr (assoc c list)))))
1676 ((or (equal c ?\n) (equal c ?\r))
1677 (insert "&;")
1678 (forward-char -1))
1679 ((equal c ? )
1680 (insert ?&))
1681 (t (insert (format "&#%d;" c))))))
1683 (defun yahtml:!--\#include ()
1684 (let ((file (yahtml-read-parameter "file" "")))
1685 (format "%s=\"%s\"--" (if (string-match "/" file) "virtual" "file") file)))
1687 (defun yahtml:!--\#exec ()
1688 (format "cmd=\"%s\"--" (yahtml-read-parameter "cmd" "" '(("cmd" . file)))))
1690 ;;; ---------- Jump ----------
1691 (defun yahtml-on-href-p ()
1692 "Check if point is on href clause."
1693 (let ((p (point)) e cmd (case-fold-search t))
1694 (save-excursion
1695 (and ;;(string= (YaTeX-inner-environment t) "a") ;aでなくても許可にした
1696 (save-excursion
1697 ;;(search-forward "</a>" nil t) ;aでなくても許可にした
1698 (search-forward "[\" \t\n]" nil t)
1699 (setq e (point)))
1700 ;(goto-char (get 'YaTeX-inner-environment 'point))
1701 (re-search-backward "<\\(a\\|link\\)\\>" nil t)
1702 (search-forward "href" e t)
1703 (search-forward "=" e t)
1704 (progn
1705 (skip-chars-forward " \t\n")
1706 (looking-at "\"?\\([^\"> \t\n]+\\)\"?"))
1707 (< p (match-end 0))
1708 (YaTeX-match-string 1)))))
1710 (defun yahtml-netscape-sentinel (proc mes)
1711 (cond
1712 ((null (buffer-name (process-buffer proc)))
1713 (set-process-buffer proc nil))
1714 ((eq (process-status proc) 'exit)
1715 (let ((cb (current-buffer)))
1716 (set-buffer (process-buffer proc))
1717 (goto-char (point-min))
1718 (if (search-forward "not running" nil t)
1719 (progn
1720 (message "Starting netscape...")
1721 (start-process
1722 "browser" (process-buffer proc)
1723 shell-file-name yahtml-shell-command-option
1724 (format "%s \"%s\"" yahtml-www-browser
1725 (get 'yahtml-netscape-sentinel 'url)))
1726 (message "Starting netscape...Done")))
1727 (set-buffer cb)))))
1729 (defvar yahtml-browser-process nil)
1731 (defun yahtml-browse-html (href)
1732 "Call WWW Browser to see HREF."
1733 (let ((pb "* WWW Browser *") (cb (current-buffer)))
1734 (cond
1735 ((string-match "^start\\>" yahtml-www-browser)
1736 (if (get-buffer pb)
1737 (progn (set-buffer pb) (erase-buffer) (set-buffer cb)))
1738 (put 'yahtml-netscape-sentinel 'url href)
1739 (set-process-sentinel
1740 (setq yahtml-browser-process
1741 (start-process
1742 "browser" pb shell-file-name yahtml-shell-command-option
1743 (format "%s \"%s\"" yahtml-www-browser href)))
1744 'yahtml-netscape-sentinel))
1745 ((and (string-match
1746 "[Nn]etscape\\|[Ff]irefox\\|[Mm]ozilla" yahtml-www-browser)
1747 (not (eq system-type 'windows-nt)))
1748 (if (get-buffer pb)
1749 (progn (set-buffer pb) (erase-buffer) (set-buffer cb)))
1750 (put 'yahtml-netscape-sentinel 'url href)
1751 (set-process-sentinel
1752 (setq yahtml-browser-process
1753 (start-process
1754 "browser" pb shell-file-name yahtml-shell-command-option ;"-c"
1755 (format "%s -remote \"openURL(%s)\"" yahtml-www-browser href)))
1756 'yahtml-netscape-sentinel))
1757 ((and (string= "w3" yahtml-www-browser) (fboundp 'w3-fetch))
1758 (w3-fetch href))
1759 ((stringp yahtml-www-browser)
1760 (if (and yahtml-browser-process
1761 (eq (process-status yahtml-browser-process) 'run))
1762 (message "%s is already running" yahtml-www-browser)
1763 (setq yahtml-browser-process
1764 (start-process
1765 "browser" "* WWW Browser *"
1766 shell-file-name yahtml-shell-command-option
1767 (format "%s \"%s\"" yahtml-www-browser href)))))
1768 (t
1769 (message "Sorry, jump across http is not supported.")))))
1771 (defun yahtml-goto-corresponding-href (&optional other)
1772 "Go to corresponding name."
1773 (let ((href (yahtml-on-href-p)) file name (parent buffer-file-name))
1774 (if href
1775 (cond
1776 ((string-match "^\\(ht\\|f\\)tps?:" href)
1777 (yahtml-browse-html href))
1778 (t (setq file (substring href 0 (string-match "#" href)))
1779 (if (string-match "#" href)
1780 (setq name (substring href (1+ (string-match "#" href)))))
1781 (if (string< "" file)
1782 (progn
1783 (if (string-match "/$" file)
1784 (or (catch 'dirindex
1785 (mapcar
1786 (function
1787 (lambda (f)
1788 (if (file-exists-p (concat file f))
1789 (throw 'dirindex
1790 (setq file (concat file f))))))
1791 (yahtml-get-directory-index))
1792 nil)
1793 (setq file (concat file yahtml-directory-index))))
1794 (if (string-match "^/" file)
1795 (setq file (yahtml-url-to-path file)))
1796 (if other (YaTeX-switch-to-buffer-other-window file)
1797 (YaTeX-switch-to-buffer file))
1798 (or YaTeX-parent-file (setq YaTeX-parent-file parent))))
1799 (if name
1800 (progn (set-mark-command nil) (yahtml-jump-to-name name)))
1801 t)))))
1803 (defun yahtml-jump-to-name (name)
1804 "Jump to html's named tag."
1805 (setq name (format "\\(name\\|id\\)\\s *=\\s *\"?%s\\>\"?" name))
1806 (or (and (re-search-forward name nil t) (goto-char (match-beginning 0)))
1807 (and (re-search-backward name nil t) (goto-char (match-beginning 0)))
1808 (message "Named tag `%s' not found" (substring href 1))))
1810 (defun yahtml-on-begend-p (&optional p)
1811 "Check if point is on begend clause."
1812 (let ((p (or p (point))) cmd (case-fold-search t))
1813 (save-excursion
1814 (goto-char p)
1815 (if (equal (char-after (point)) ?<) (forward-char 1))
1816 (if (and (re-search-backward "<" nil t)
1817 (looking-at
1818 ;(concat "<\\(/?" yahtml-struct-name-regexp "\\)\\b")
1819 "<\\(/?[A-Z][A-Z0-9]*\\)\\b")
1820 (condition-case nil
1821 (forward-list 1)
1822 (error nil))
1823 (< p (point)))
1824 (YaTeX-match-string 1)))))
1826 (defun yahtml-goto-corresponding-begend (&optional noerr)
1827 "Go to corresponding opening/closing tag.
1828 Optional argument NOERR causes no error for unballanced tag."
1829 (let ((cmd (yahtml-on-begend-p)) m0
1830 (p (point)) (case-fold-search t) func str (nest 0))
1831 (cond
1832 (cmd
1833 (setq m0 (match-beginning 0))
1834 (if (= (aref cmd 0) ?/) ;on </cmd> line
1835 (setq cmd (substring cmd 1)
1836 str (format "\\(<%s\\)\\|\\(</%s\\)" cmd cmd)
1837 func 're-search-backward)
1838 (setq str (format "\\(</%s\\)\\|\\(<%s\\)" cmd cmd)
1839 func 're-search-forward))
1840 (while (and (>= nest 0) (funcall func str nil t))
1841 (if (equal m0 (match-beginning 0))
1842 nil
1843 (setq nest (+ nest (if (match-beginning 1) -1 1)))))
1844 (if (< nest 0)
1845 (goto-char (match-beginning 0))
1846 (funcall
1847 (if noerr 'message 'error)
1848 "Corresponding tag of `%s' not found." cmd)
1849 (goto-char p)
1850 nil))
1851 (t nil))))
1853 (defun yahtml-current-tag ()
1854 "Return the current tag name including #exec and #include."
1855 (save-excursion
1856 (let ((p (point)) b tag)
1857 (or (bobp)
1858 (looking-at "<")
1859 (progn (skip-chars-backward "^<") (forward-char -1)))
1860 (setq b (point))
1861 (skip-chars-forward "<")
1862 (setq tag (YaTeX-buffer-substring
1863 (point) (progn (skip-chars-forward "^ \t\n") (point))))
1864 (goto-char b)
1865 (forward-list 1)
1866 (and (< p (point)) tag))))
1868 (defun yahtml-get-attrvalue (attr)
1869 "Extract current tag's attribute value from buffer."
1870 (let (e (case-fold-search t))
1871 (save-excursion
1872 (or (looking-at "<")
1873 (progn (skip-chars-backward "^<") (backward-char 1)))
1874 (setq e (save-excursion (forward-list 1) (point)))
1875 (if (and
1876 (re-search-forward (concat "\\b" attr "\\b") e t)
1877 (progn (skip-chars-forward " \t\n=")
1878 (looking-at "\"?\\([^\"> \t\n]+\\)\"?")))
1879 (YaTeX-match-string 1)))))
1881 (defun yahtml-goto-corresponding-img ()
1882 "View image on point"
1883 (let ((tag (yahtml-current-tag)) image (p (point)) (case-fold-search t))
1884 (if (and tag
1885 (string-match "img" tag)
1886 (setq image (yahtml-get-attrvalue "src")))
1887 (progn
1888 (message "Invoking %s %s..." yahtml-image-viewer image)
1889 (start-process
1890 "Viewer" " * Image Viewer *"
1891 shell-file-name yahtml-shell-command-option ;"-c"
1892 (concat yahtml-image-viewer " " image))
1893 (message "Invoking %s %s...Done" yahtml-image-viewer image)))))
1895 (defun yahtml-goto-corresponding-source (&optional other)
1896 "Goto applet's or script's source."
1897 (let ((env (yahtml-current-tag)) s (p (point)))
1898 (cond
1899 ((string-match "applet" env)
1900 (if (setq s (yahtml-get-attrvalue "code"))
1901 (progn
1902 (setq s (YaTeX-match-string 1)
1903 s (concat
1904 (substring s 0 (string-match "\\.[A-Za-z]+$" s))
1905 ".java"))
1906 (if other (YaTeX-switch-to-buffer-other-window s)
1907 (YaTeX-switch-to-buffer s))
1908 s) ;return source file name
1909 (message "No applet source specified")
1910 (sit-for 1)
1911 nil))
1912 ((string-match "script" env)
1913 (if (setq s (yahtml-get-attrvalue "src"))
1914 (progn
1915 (funcall (if other 'YaTeX-switch-to-buffer-other-window
1916 'YaTeX-switch-to-buffer)
1917 (yahtml-url-to-path s))
1918 s)))
1919 ((string-match "!--#include" env)
1920 (cond
1921 ((setq s (yahtml-get-attrvalue "file")) ;<!--#include file="foo"-->
1922 (if other (YaTeX-switch-to-buffer-other-window s)
1923 (YaTeX-switch-to-buffer s))
1924 s)
1925 ((setq s (yahtml-get-attrvalue "virtual"));<!--#include virtual="foo"-->
1926 (setq s (yahtml-url-to-path s))
1927 (if other (YaTeX-switch-to-buffer-other-window s)
1928 (YaTeX-switch-to-buffer s))
1929 s)))
1930 ((and (string-match "!--#exec" env)
1931 (setq s (yahtml-get-attrvalue "cmd")))
1932 (setq s (substring s 0 (string-match " \t\\?" s))) ;get argv0
1933 (let ((b " *yahtmltmp*")) ;peek a little
1934 (unwind-protect
1935 (progn
1936 (set-buffer (get-buffer-create b))
1937 (YaTeX-insert-file-contents s nil 0 100)
1938 (if (looking-at "#!")
1939 (if other (YaTeX-switch-to-buffer-other-window s)
1940 (YaTeX-switch-to-buffer s))))
1941 (kill-buffer (get-buffer b)))
1942 (get-file-buffer s))))))
1944 (defun yahtml-goto-corresponding-* (&optional other)
1945 "Go to corresponding object."
1946 (interactive "P")
1947 (cond
1948 ((yahtml-goto-corresponding-href other))
1949 ((yahtml-goto-corresponding-img))
1950 ((yahtml-goto-corresponding-source other))
1951 ((yahtml-goto-corresponding-begend))
1952 (t (message "I don't know where to go."))))
1954 (defun yahtml-goto-corresponding-*-other-window ()
1955 "Go to corresponding object."
1956 (interactive)
1957 (yahtml-goto-corresponding-* t))
1959 (defun yahtml-visit-main ()
1960 "Go to parent file from where you visit current file."
1961 (interactive)
1962 (if YaTeX-parent-file (YaTeX-switch-to-buffer YaTeX-parent-file)))
1964 ;;; ---------- killing ----------
1965 (defun yahtml-kill-begend (&optional whole)
1966 (let ((tag (yahtml-on-begend-p)) p q r bbolp)
1967 (if tag
1968 (save-excursion
1969 (or (looking-at "<")
1970 (progn (skip-chars-backward "^<") (forward-char -1)))
1971 (setq p (point))
1972 (yahtml-goto-corresponding-begend)
1973 (or (looking-at "<")
1974 (progn (skip-chars-backward "^<") (forward-char -1)))
1975 (if (< (point) p) ;if on the opening tag
1976 (progn (setq q p p (point))
1977 (goto-char q))
1978 (setq q (point))) ;now q has end-line's (point)
1979 (if (not whole)
1980 (kill-region
1981 (progn (skip-chars-backward " \t")
1982 (if (setq bbolp (bolp)) (point) q))
1983 (progn (forward-list 1)
1984 (setq r (point))
1985 (skip-chars-forward " \t")
1986 (if (and bbolp (eolp) (not (eobp))) (1+ (point)) r))))
1987 (goto-char p)
1988 (skip-chars-backward " \t")
1989 (if (not whole)
1990 (progn
1991 (kill-append
1992 (buffer-substring
1993 (setq p (if (setq bbolp (bolp)) (point) p))
1994 (setq q (progn
1995 (forward-list 1)
1996 (setq r (point))
1997 (skip-chars-forward " \t")
1998 (if (and bbolp (eolp) (not (eobp)))
1999 (1+ (point))
2000 r))))
2001 t)
2002 (delete-region p q))
2003 (kill-region
2004 (if (bolp) (point) p)
2005 (progn (goto-char q)
2006 (forward-list 1)
2007 (setq r (point))
2008 (skip-chars-forward " \t")
2009 (if (and (eolp) (not (eobp))) (1+ (point)) r))))
2010 tag))))
2012 (defun yahtml-kill-* (whole)
2013 "Kill current position's HTML tag (set)."
2014 (interactive "P")
2015 (cond
2016 ((yahtml-kill-begend whole))))
2019 ;;; ---------- changing ----------
2020 (defun yahtml-on-assignment-p ()
2021 "Return if current point is on parameter assignment.
2022 If so, return parameter name, otherwise nil.
2023 This function should be able to treat white spaces in value, but not yet."
2024 (let ((p (point)))
2025 (save-excursion
2026 (put 'yahtml-on-assignment-p 'region nil)
2027 (skip-chars-backward "^ \t\n")
2028 (and (looking-at "\\([A-Za-z0-9]+\\)\\s *=\\s *\"?\\([^ \t\"]+\\)\"?")
2029 (< p (match-end 0))
2030 (>= p (1- (match-beginning 2)))
2031 (put 'yahtml-on-assignment-p 'region
2032 (cons (match-beginning 2) (match-end 2)))
2033 (YaTeX-match-string 1)))))
2035 (defun yahtml-change-begend ()
2036 (let ((tag (yahtml-on-begend-p))
2037 (completion-ignore-case t)
2038 (case-fold-search t)
2039 (p (point)) (q (make-marker))
2040 (default (append yahtml-env-table yahtml-typeface-table))
2041 (user (append yahtml-user-env-table yahtml-user-typeface-table))
2042 (tmp (append yahtml-tmp-env-table yahtml-tmp-typeface-table))
2043 href b1 e1 attr new css)
2044 (cond
2045 (tag
2046 (cond
2047 ((and (string-match "^a$" tag)
2048 (save-excursion
2049 (and
2050 (re-search-backward "<a\\b" nil t)
2051 (progn
2052 (goto-char (match-end 0))
2053 (skip-chars-forward " \t\n")
2054 (setq b1 (point))
2055 (search-forward ">" nil t))
2056 (setq e1 (match-beginning 0))
2057 (goto-char b1)
2058 (re-search-forward "href\\s *=" e1 t)
2059 (>= p (point))
2060 (progn
2061 (goto-char (match-end 0))
2062 (skip-chars-forward " \t\n")
2063 (looking-at "\"?\\([^\"> \t\n]+\\)\"?"))
2064 (< p (match-end 0)))))
2065 (setq b1 (match-beginning 1) e1 (match-end 1)
2066 yahtml-completing-buffer (current-buffer)
2067 ;; yahtml-urls-local is buffer-local, so we must put
2068 ;; that into yahtml-urls here
2069 yahtml-urls (append yahtml-urls-private yahtml-urls-local)
2070 href (read-from-minibuffer
2071 "Change href to: " "" yahtml-url-completion-map))
2072 (if (string< "" href)
2073 (progn
2074 ;;(setq href ;??
2075 ;; (if yahtml-prefer-upcases (upcase href) (downcase href)))
2076 (delete-region b1 e1)
2077 (goto-char b1)
2078 (insert href))))
2079 ((setq attr (yahtml-on-assignment-p)) ;if on the assignment to attr
2080 (if (and (equal attr "class") ;treat "class" attribute specially
2081 (setq css (yahtml-css-get-element-completion-alist tag)))
2083 (setq new (yahtml-read-css css))
2084 ;;other than "class", read parameter normally
2085 (setq new (yahtml-read-parameter attr)))
2086 (goto-char (car (get 'yahtml-on-assignment-p 'region)))
2087 (delete-region (point) (cdr (get 'yahtml-on-assignment-p 'region)))
2088 (insert new))
2089 (t
2090 (save-excursion
2091 (if (= (aref tag 0) ?/) (setq tag (substring tag 1)))
2092 (or (= (char-after (point)) ?<) (skip-chars-backward "^<"))
2093 (skip-chars-forward "^A-Za-z")
2094 (set-marker q (point))
2095 (setq p (point))
2096 (yahtml-goto-corresponding-begend)
2097 (or (= (char-after (point)) ?<)
2098 (skip-chars-backward "^<"))
2099 (skip-chars-forward "^A-Za-z")
2100 (if (= (char-after (1- (point))) ?/)
2101 (progn
2102 (set-marker q (point))
2103 (goto-char p)))
2104 (setq tag (let ((completion-ignore-case t))
2105 (YaTeX-cplread-with-learning
2106 (format "Change `%s' to(default %s): "
2107 tag yahtml-last-begend)
2108 'default 'user 'tmp)))
2109 (delete-region (point) (progn (skip-chars-forward "^>") (point)))
2110 (if (string= "" tag) (setq tag yahtml-last-begend))
2111 (setq yahtml-last-begend
2112 (or (cdr (assoc tag yahtml-env-table)) tag)
2113 tag yahtml-last-begend)
2114 (setq tag (if yahtml-prefer-upcases (upcase tag) (downcase tag)))
2115 (insert (format "%s%s" tag (yahtml-addin tag)))
2116 (goto-char q)
2117 (set-marker q nil)
2118 (delete-region (point) (progn (skip-chars-forward "^>") (point)))
2119 (insert tag))))
2120 t))))
2122 (defun yahtml-change-command ()
2123 (let ((p (point)) (case-fold-search t) cmd par new
2124 (beg (make-marker)) (end (make-marker)))
2125 (skip-chars-backward "^<")
2126 (if (and
2127 (looking-at yahtml-command-regexp)
2128 (progn
2129 (set-marker beg (match-beginning 0))
2130 (set-marker end (match-end 0))
2131 t) ;for further work
2132 (progn
2133 (forward-char -1)
2134 (condition-case nil
2135 (forward-list 1)
2136 (error nil))
2137 (< p (point))))
2138 (progn
2139 (goto-char p)
2140 (if (setq par (yahtml-on-assignment-p))
2141 (progn
2142 (setq new (yahtml-read-parameter par))
2143 (set-marker beg (car (get 'yahtml-on-assignment-p 'region)))
2144 (set-marker end (cdr (get 'yahtml-on-assignment-p 'region))))
2145 (setq new
2146 (YaTeX-cplread-with-learning
2147 "Change form to: "
2148 'yahtml-form-table 'yahtml-user-form-table
2149 'yahtml-tmp-form-table)))
2150 (delete-region beg end)
2151 (goto-char beg)
2152 (set-marker beg nil)
2153 (set-marker end nil)
2154 (insert new)
2155 t)
2156 (goto-char p)
2157 nil)))
2159 (defun yahtml-change-* ()
2160 "Change current position's HTML tag (set)."
2161 (interactive)
2162 (cond
2163 ((yahtml-change-begend))
2164 ((yahtml-change-command))))
2166 ;;; ---------- commenting ----------
2168 (defun yahtml-comment-region (&optional uncom)
2169 "Comment out region or environment."
2170 (interactive)
2171 (let ((e (make-marker)) be beg p)
2172 (cond
2173 (;(marker-position (set-marker e (yahtml-on-begend-p)))
2174 (setq be (yahtml-on-begend-p))
2175 (save-excursion
2176 (setq p (point))
2177 (if (string-match "^/" be)
2178 (setq beg (progn (forward-line 1) (point)))
2179 (setq beg (progn (beginning-of-line) (point))))
2180 (goto-char p)
2181 (yahtml-goto-corresponding-begend)
2182 (if (string-match "^/" be)
2183 (beginning-of-line)
2184 (forward-line 1))
2185 (set-marker e (point))
2186 ;(comment-region beg (point) (if uncom (list 4)))))
2187 ))
2188 (t ;(comment-region (region-beginning) (region-end) (if uncom (list 4)))
2189 (setq beg (region-beginning))
2190 (set-marker e (region-end))))
2191 (if yahtml-translate-hyphens-when-comment-region
2192 (let ((yahtml-entity-reference-chars-alist-default nil)
2193 (yahtml-entity-reference-chars-alist '((?- . "#45")))
2194 yahtml-entity-reference-chars-regexp
2195 yahtml-entity-reference-chars-reverse-regexp)
2196 (yahtml-entity-reference-chars-setup)
2197 (funcall
2198 (if uncom 'yahtml-translate-reverse-region
2199 'yahtml-translate-region)
2200 beg e)))
2201 (comment-region beg e (if uncom (list 4)))
2202 (set-marker e nil)))
2204 (defun yahtml-uncomment-region ()
2205 (interactive)
2206 (yahtml-comment-region t))
2208 ;;; ---------- translate to entity references ----------
2209 (defvar yahtml-entity-reference-chars-alist-default
2210 ;'((?> . "gt") (?< . "lt") (?& . "amp") (?\" . "quot") (?' . "apos"))
2211 '((?> . "gt") (?< . "lt") (?& . "amp") (?\" . "quot"))
2212 "Default translation table from character to entity reference")
2213 (defvar yahtml-entity-reference-chars-alist nil
2214 "*Translation table from character to entity reference")
2215 (defvar yahtml-entity-reference-chars-regexp nil)
2216 (defvar yahtml-entity-reference-chars-reverse-regexp nil)
2218 (defun yahtml-entity-reference-chars-setup ()
2219 (let ((list (append yahtml-entity-reference-chars-alist-default
2220 yahtml-entity-reference-chars-alist)))
2221 (setq yahtml-entity-reference-chars-regexp "["
2222 yahtml-entity-reference-chars-reverse-regexp "&\\(")
2223 (while list
2224 (setq yahtml-entity-reference-chars-regexp
2225 (concat yahtml-entity-reference-chars-regexp
2226 (char-to-string (car (car list))))
2227 yahtml-entity-reference-chars-reverse-regexp
2228 (concat yahtml-entity-reference-chars-reverse-regexp
2229 (cdr (car list))
2230 (if (cdr list) "\\|")))
2231 (setq list (cdr list)))
2232 (setq yahtml-entity-reference-chars-regexp
2233 (concat yahtml-entity-reference-chars-regexp "]")
2234 yahtml-entity-reference-chars-reverse-regexp
2235 (concat yahtml-entity-reference-chars-reverse-regexp "\\);"))))
2237 (yahtml-entity-reference-chars-setup)
2239 (defun yahtml-translate-region (beg end)
2240 "Translate inhibited literals."
2241 (interactive "r")
2242 (save-excursion
2243 (save-restriction
2244 (narrow-to-region beg end)
2245 (let ((ct (append yahtml-entity-reference-chars-alist
2246 yahtml-entity-reference-chars-alist-default)))
2247 (goto-char beg)
2248 (while (re-search-forward yahtml-entity-reference-chars-regexp nil t)
2249 ;(setq c (preceding-char))
2250 (replace-match
2251 (concat "&" (cdr (assoc (preceding-char) ct)) ";")))))))
2253 (defun yahtml-translate-reverse-region (beg end)
2254 "Translate entity references to literals."
2255 (interactive "r")
2256 (save-excursion
2257 (save-restriction
2258 (narrow-to-region beg end)
2259 (let ((ct (append yahtml-entity-reference-chars-alist
2260 yahtml-entity-reference-chars-alist-default))
2261 ec)
2262 (goto-char beg)
2263 (while (re-search-forward
2264 yahtml-entity-reference-chars-reverse-regexp nil t)
2265 ;(setq c (preceding-char))
2266 (setq ec (YaTeX-match-string 1))
2267 (delete-region (match-end 0) (match-beginning 0))
2268 (insert (car (YaTeX-rassoc ec ct))))))))
2270 (defun yahtml-inner-environment-but (exclude &optional quick)
2271 "Return the inner environment but matches with EXCLUDE tag."
2272 (let (e (case-fold-search t))
2273 (save-excursion
2274 (while (and (setq e (YaTeX-inner-environment quick))
2275 (string-match exclude e))
2276 (goto-char (get 'YaTeX-inner-environment 'point))))
2277 e))
2279 (defun yahtml-untranslate-string (str)
2280 "Untranslate entity reference."
2281 (let ((md (match-data)) (left "") (right str) b0 ch
2282 (ct (append yahtml-entity-reference-chars-alist
2283 yahtml-entity-reference-chars-alist-default))
2284 (revrex yahtml-entity-reference-chars-reverse-regexp))
2285 (unwind-protect
2286 (progn
2287 (while (string< "" right)
2288 (cond
2289 ((string-match revrex right)
2290 (setq ch (YaTeX-rassoc
2291 (substring right (match-beginning 1) (match-end 1)))
2292 b0 (substring right 0 (match-beginning 0))
2293 right (substring right (match-end 0))
2294 left (concat left
2295 (substring right 0 (match-beginning 0))
2296 (char-to-string ch))))
2297 ((string-match "\\&#\\(x\\)?\\([0-9a-f]+\\);" right)
2298 (setq ch (substring right (match-beginning 2) (match-end 2))
2299 b0 (substring right 0 (match-beginning 0))
2300 right (substring right (match-end 0))
2301 left (concat left
2302 b0
2303 (char-to-string
2304 (if (match-beginning 1)
2305 (YaTeX-hex ch)
2306 (string-to-number ch))))))
2307 (t (setq left (concat left right)
2308 right ""))))
2309 left)
2310 (store-match-data md))))
2312 ;;; ---------- filling ----------
2313 (defvar yahtml-saved-move-to-column (symbol-function 'move-to-column))
2314 (defun yahtml-move-to-column (col &optional force)
2315 (beginning-of-line)
2316 (let ((ccol 0))
2317 (while (and (> col ccol) (not (eolp)))
2318 (if (eq (following-char) ?\<)
2319 (progn
2320 (while (and (not (eq (following-char) ?\>))
2321 (not (eolp)))
2322 (forward-char))
2323 (or (eolp) (forward-char)))
2324 (or (eolp) (forward-char))
2325 (if (eq (preceding-char) ?\t)
2326 (let ((wd (- 8 (% (+ ccol 8) 8))))
2327 (if (and force (< col (+ ccol wd)))
2328 (progn
2329 (backward-char 1)
2330 (insert-char ?\ (- col ccol))
2331 (setq ccol col))
2332 (setq ccol (+ ccol wd))))
2333 (setq ccol (1+ ccol)))
2334 (if (and YaTeX-japan
2335 (or
2336 (and (fboundp 'char-category)
2337 (string-match "[chj]" (char-category (preceding-char))))
2338 (and (fboundp 'char-charset)
2339 (not (eq (char-charset (preceding-char)) 'ascii)))))
2340 (setq ccol (1+ ccol)))))
2341 (if (and force (> col ccol))
2342 (progn
2343 (insert-char ?\ (- col ccol))
2344 col)
2345 ccol)))
2347 (defun yahtml-fill-paragraph (arg)
2348 (interactive "P")
2349 (let*((case-fold-search t) (p (point)) fill-prefix
2350 (e (or (yahtml-inner-environment-but "^\\(a\\|p\\)\\b" t) "html"))
2351 indent
2352 (startp (get 'YaTeX-inner-environment 'point))
2353 (prep (string-match "^pre$" e))
2354 (ps1 (if prep (default-value 'paragraph-start)
2355 paragraph-start))
2356 (ps2 (if prep (concat (default-value 'paragraph-start)
2357 "$\\|^\\s *</?pre>")
2358 paragraph-start)))
2359 (save-excursion
2360 (unwind-protect
2361 (progn
2362 (if prep
2363 (fset 'move-to-column 'yahtml-move-to-column))
2364 (save-excursion
2365 (beginning-of-line)
2366 (indent-to-column (yahtml-this-indent))
2367 (setq fill-prefix
2368 (buffer-substring (point) (point-beginning-of-line)))
2369 (delete-region (point) (point-beginning-of-line)))
2370 (fill-region-as-paragraph
2371 (progn (re-search-backward paragraph-start nil t)
2372 (or (save-excursion
2373 (goto-char (match-beginning 0))
2374 (if (looking-at "<")
2375 (forward-list)
2376 (goto-char (match-end 0))
2377 (skip-chars-forward " \t>"))
2378 (if (looking-at "[ \t]*$")
2379 (progn (forward-line 1) (point))))
2380 (point)))
2381 (progn (goto-char p)
2382 (re-search-forward ps2 nil t)
2383 (match-beginning 0))))
2384 (fset 'move-to-column yahtml-saved-move-to-column)))))
2386 ;(defun yahtml-indent-new-commnet-line ()
2387 ; (unwind-protect
2388 ; (progn
2389 ; (fset 'move-to-column 'yahtml-move-to-column)
2390 ; (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft))))
2391 ; (fset 'move-to-column yahtml-saved-move-to-column)))
2393 ;;;
2394 ;;; ---------- indentation ----------
2395 ;;;
2396 (defun yahtml-indent-line ()
2397 "Indent a line (faster wrapper)"
2398 (interactive)
2399 (let (indent)
2400 (if (and (save-excursion
2401 (beginning-of-line) (skip-chars-forward "\t ")
2402 (not (looking-at "<")))
2403 (save-excursion
2404 (forward-line -1)
2405 (while (and (not (bobp)) (looking-at "^\\s *$"))
2406 (forward-line -1))
2407 (skip-chars-forward "\t ")
2408 (setq indent (current-column))
2409 (not (looking-at "<"))))
2410 (progn
2411 (save-excursion
2412 (beginning-of-line)
2413 (skip-chars-forward " \t")
2414 (or (= (current-column) indent)
2415 (YaTeX-reindent indent)))
2416 (and (bolp) (skip-chars-forward " \t")))
2417 (yahtml-indent-line-real))))
2419 (defun yahtml-this-indent ()
2420 (let ((envs "[uod]l\\|table\\|[ht][rhd0-6]\\|select\\|blockquote\\|center\\|menu\\|dir\\|font")
2421 (itemizing-envs "^\\([uod]l\\|menu\\|dir\\)$")
2422 (itms "<\\(dt\\|dd\\|li\\|t[rdh]\\|option\\)\\b")
2423 (excludes
2424 "\\(a\\|p\\|span\\|code\\|tt\\|em\\|u\\|i\\|big\\|small\\|font\\)\\b")
2425 inenv p col peol (case-fold-search t))
2426 (save-excursion
2427 (beginning-of-line)
2428 (setq inenv (or (yahtml-inner-environment-but excludes t)
2429 "html")
2430 col (get 'YaTeX-inner-environment 'indent)
2431 p (get 'YaTeX-inner-environment 'point)
2432 op nil))
2433 (save-excursion
2434 (cond
2435 ((string-match (concat "^\\(" envs "\\)") inenv)
2436 (save-excursion
2437 (beginning-of-line)
2438 (skip-chars-forward " \t")
2439 (cond ;lookup current line's tag
2440 ((looking-at (concat "</\\(" envs "\\)>"))
2441 col)
2442 ((looking-at itms)
2443 (+ col yahtml-environment-indent))
2444 ((and yahtml-hate-too-deep-indentation
2445 (looking-at (concat "<\\(" envs "\\)")))
2446 (+ col (* 2 yahtml-environment-indent)))
2447 ((and (< p (point))
2448 (string-match itemizing-envs inenv)
2449 (save-excursion
2450 (and
2451 (setq op (point))
2452 (goto-char p)
2453 (re-search-forward itms op t)
2454 (progn
2455 (if yahtml-indent-listing-constant
2456 (setq col (+ (current-column)
2457 (if yahtml-faithful-to-htmllint 1 2)))
2458 (skip-chars-forward "^>")
2459 (skip-chars-forward ">")
2460 (skip-chars-forward " \t")
2461 (setq col (if (looking-at "$")
2462 (+ col yahtml-environment-indent)
2463 (current-column))))))))
2464 col)
2465 (t
2466 (+ col yahtml-environment-indent)))))
2467 (t col)))))
2469 (defun yahtml-indent-line-real ()
2470 "Indent current line."
2471 (interactive)
2472 (YaTeX-reindent (yahtml-this-indent))
2473 (if (bolp) (skip-chars-forward " \t"))
2474 (let (peol col inenv)
2475 (if (and (setq inenv (yahtml-on-begend-p))
2476 (string-match
2477 (concat "^\\<\\(" yahtml-struct-name-regexp "\\)") inenv))
2478 (save-excursion
2479 (setq peol (point-end-of-line))
2480 (or (= (char-after (point)) ?<)
2481 (progn (skip-chars-backward "^<") (forward-char -1)))
2482 (setq col (current-column))
2483 (if (and (yahtml-goto-corresponding-begend t)
2484 (> (point) peol)) ;if on the different line
2485 (YaTeX-reindent col))))))
2487 ;(defun yahtml-fill-item ()
2488 ; "Fill item HTML version"
2489 ; (interactive)
2490 ; (let (inenv p fill-prefix peol (case-fold-search t))
2491 ; (setq inenv (or (YaTeX-inner-environment) "html")
2492 ; p (get 'YaTeX-inner-environment 'point))
2493 ; (cond
2494 ; ((string-match "^[uod]l" inenv)
2495 ; (save-excursion
2496 ; (if (re-search-backward "<\\(d[td]\\|li\\)>[ \t\n]*" p t)
2497 ; (progn
2498 ; (goto-char (match-end 0))
2499 ; (setq col (current-column)))
2500 ; (error "No <li>, <dt>, <dd>")))
2501 ; (save-excursion
2502 ; (end-of-line)
2503 ; (setq peol (point))
2504 ; (newline)
2505 ; (indent-to-column col)
2506 ; (setq fill-prefix (buffer-substring (point) (1+ peol)))
2507 ; (delete-region (point) peol)
2508 ; (fill-region-as-paragraph
2509 ; (progn (re-search-backward paragraph-start nil t) (point))
2510 ; (progn (re-search-forward paragraph-start nil t 2)
2511 ; (match-beginning 0)))))
2512 ; (t nil))))
2514 ;;;
2515 ;;; ---------- Lint and Browsing ----------
2516 ;;;
2517 (defun yahtml-browse-menu ()
2518 "Browsing or other external process invokation menu."
2519 (interactive)
2520 (message "J)weblint p)Browse R)eload N)ewpage...")
2521 (let ((c (char-to-string (read-char))))
2522 (cond
2523 ((string-match "j" c)
2524 (yahtml-lint-buffer (current-buffer)))
2525 ((string-match "[bp]" c)
2526 (yahtml-browse-current-file))
2527 ((string-match "r" c)
2528 (yahtml-browse-reload))
2529 ((string-match "n" c)
2530 (call-interactively 'yahtml-newpage)))))
2532 (if (fboundp 'wrap-function-to-control-ime)
2533 (wrap-function-to-control-ime 'yahtml-browse-menu t nil))
2535 (defvar yahtml-lint-buffer "*weblint*")
2537 (defun yahtml-lint-buffer (buf)
2538 "Call lint on buffer BUF."
2539 (require 'yatexprc)
2540 (interactive "bCall lint on buffer: ")
2541 (setq buf (get-buffer buf))
2542 (YaTeX-save-buffers)
2543 (let ((bcmd (YaTeX-get-builtin "lint")))
2544 (and bcmd (setq bcmd (yahtml-untranslate-string bcmd)))
2545 (YaTeX-typeset
2546 (concat (or bcmd yahtml-lint-program)
2547 " " (file-name-nondirectory (buffer-file-name buf)))
2548 yahtml-lint-buffer "lint" "lint")))
2550 (defun yahtml-file-to-url (file)
2551 "Convert local unix file name to URL.
2552 If no matches found in yahtml-path-url-alist, return raw file name."
2553 (let ((list yahtml-path-url-alist) p url)
2554 (if (file-directory-p file)
2555 (setq file (expand-file-name yahtml-directory-index file))
2556 (setq file (expand-file-name file)))
2557 (if (string-match "^[A-Za-z]:/" file)
2558 (progn
2559 ;; (aset file 1 ?|) ;これは要らないらしい…
2560 (setq file (concat "///" file))))
2561 (while list
2562 (if (string-match (concat "^" (regexp-quote (car (car list)))) file)
2563 (setq url (cdr (car list))
2564 file (substring file (match-end 0))
2565 url (concat url file)
2566 list nil))
2567 (setq list (cdr list)))
2568 (or url (concat "file:" file))))
2570 (defun yahtml-url-to-path (file &optional basedir)
2571 "Convert local URL name to unix file name."
2572 (let ((list yahtml-path-url-alist) url realpath docroot
2573 (dirsufp (string-match "/$" file)))
2574 (setq basedir (or basedir
2575 (file-name-directory
2576 (expand-file-name default-directory))))
2577 (cond
2578 ((string-match "^/" file)
2579 (while list
2580 (if (file-directory-p (car (car list)))
2581 (progn
2582 (setq url (cdr (car list)))
2583 (if (string-match "\\(http://[^/]*\\)/" url)
2584 (setq docroot (substring url (match-end 1)))
2585 (setq docroot url))
2586 (cond
2587 ((string-match (concat "^" (regexp-quote docroot)) file)
2588 (setq realpath
2589 (expand-file-name
2590 (substring
2591 file
2592 (if (= (aref file (1- (match-end 0))) ?/)
2593 (match-end 0) ; "/foo"
2594 (min (1+ (match-end 0)) (length file)))) ; "/~foo"
2595 (car (car list))))))
2596 (if realpath
2597 (progn (setq list nil)
2598 (if (and dirsufp (not (string-match "/$" realpath)))
2599 (setq realpath (concat realpath "/")))))))
2600 (setq list (cdr list)))
2601 realpath)
2602 (t file))))
2604 (defun yahtml-browse-current-file ()
2605 "Call WWW browser on current file."
2606 (interactive)
2607 (basic-save-buffer)
2608 (yahtml-browse-html (yahtml-file-to-url (buffer-file-name))))
2610 (defun yahtml-browse-reload ()
2611 "Send `reload' event to netscape."
2612 (let ((pb "* WWW Browser *") (cb (current-buffer)))
2613 (cond
2614 ((string-match "[Nn]etscape" yahtml-www-browser)
2615 (if (get-buffer pb)
2616 (progn (set-buffer pb) (erase-buffer) (set-buffer cb)))
2617 ;;(or (get 'yahtml-netscape-sentinel 'url)
2618 ;; (error "Reload should be called after Browsing."))
2619 (put 'yahtml-netscape-sentinel 'url
2620 (yahtml-file-to-url (buffer-file-name)))
2621 (basic-save-buffer)
2622 (set-process-sentinel
2623 (setq yahtml-browser-process
2624 (start-process
2625 "browser" pb shell-file-name yahtml-shell-command-option ;"-c"
2626 (format "%s -remote 'reload'" yahtml-www-browser)))
2627 'yahtml-netscape-sentinel))
2628 (t
2629 (message "Sorry, RELOAD is supported only for Netscape.")))))
2631 ;;; ---------- Intelligent newline ----------
2632 (defun yahtml-intelligent-newline (arg)
2633 "Intelligent newline for HTML"
2634 (interactive "P")
2635 (let (env func)
2636 (end-of-line)
2637 (setq env (downcase (or (yahtml-inner-environment-but "^\\(a\\|p\\)\\b" t)
2638 "html")))
2639 (setq func (intern-soft (concat "yahtml-intelligent-newline-" env)))
2640 (newline)
2641 (if (and env func (fboundp func))
2642 ;; if intelligent line function is defined, call that
2643 (funcall func)
2644 ;; else do the default action
2645 (if (string-match yahtml-p-prefered-env-regexp env)
2646 (yahtml-insert-p)))))
2648 (defun yahtml-intelligent-newline-ul ()
2649 (interactive)
2650 (yahtml-insert-single "li")
2651 (or yahtml-always-/li yahtml-faithful-to-htmllint (insert " "))
2652 (yahtml-indent-line))
2654 (fset 'yahtml-intelligent-newline-ol 'yahtml-intelligent-newline-ul)
2656 (defun yahtml-intelligent-newline-dl ()
2657 (interactive)
2658 (let ((case-fold-search t))
2659 (if (save-excursion
2660 (re-search-backward "<\\(\\(dt\\)\\|\\(dd\\)\\)[ \t>]"
2661 (get 'YaTeX-inner-environment 'point) t))
2662 (cond
2663 ((match-beginning 2)
2664 (yahtml-insert-single "dd")
2665 (or yahtml-always-/dd yahtml-faithful-to-htmllint (insert " "))
2666 (setq yahtml-last-single-cmd "dt"))
2667 ((match-beginning 3)
2668 (yahtml-insert-single "dt")
2669 (or yahtml-always-/dt yahtml-faithful-to-htmllint (insert " "))
2670 (setq yahtml-last-single-cmd "dd")))
2671 (yahtml-insert-single "dt")
2672 (or yahtml-always-/li yahtml-faithful-to-htmllint (insert " "))
2673 (setq yahtml-last-single-cmd "dd"))
2674 (yahtml-indent-line)
2675 (and (string-match yahtml-p-prefered-env-regexp "dl")
2676 (string-equal yahtml-last-single-cmd "dt")
2677 (yahtml-insert-p nil))))
2679 (defun yahtml-intelligent-newline-select ()
2680 (interactive)
2681 (insert "<" (if yahtml-prefer-upcases "OPTION" "option") "> ")
2682 (yahtml-indent-line))
2684 (defun yahtml-intelligent-newline-style ()
2685 (interactive)
2686 (if (save-excursion
2687 (and
2688 (re-search-backward "<style\\|<!-- " nil t)
2689 (looking-at "<style")))
2690 (let (c)
2691 (yahtml-indent-line)
2692 (setq c (current-column))
2693 (insert "<!--\n")
2694 (YaTeX-reindent c)
2695 (insert "-->")
2696 (beginning-of-line)
2697 (open-line 1)
2698 (YaTeX-reindent c))))
2700 (defun yahtml-intelligent-newline-head ()
2701 (let ((title (read-string "Document title: "))
2702 (b "<title>") (e "</title>") p)
2703 (yahtml-indent-line)
2704 (insert (format "%s" (if yahtml-prefer-upcases (upcase b) b)))
2705 (setq p (point))
2706 (insert (format "%s%s" title (if yahtml-prefer-upcases (upcase e) e)))
2707 (if (string= "" title) (goto-char p))
2708 (setq yahtml-last-begend "body")))
2710 (defun yahtml-intelligent-newline-script ()
2711 (let ((p (point)) b)
2712 (if (save-excursion
2713 (and
2714 (setq b (re-search-backward "<script\\>" nil t))
2715 (re-search-forward
2716 "\\(javascript\\)\\|\\(tcl\\)\\|\\(vbscript\\)" p t)))
2717 (let ((js (match-end 1)) (tcl (match-end 2)) (vb (match-end 3))
2718 c (srcp (re-search-backward "src=" b t)))
2719 (goto-char p)
2720 (yahtml-indent-line)
2721 (setq c (current-column))
2722 (if srcp
2723 nil
2724 (insert "<!--\n" (cond (js "//") (tcl "#") (vb "'")) " -->")
2725 (beginning-of-line)
2726 (open-line 1)
2727 (YaTeX-reindent c))))))
2729 (defun yahtml-intelligent-newline-table ()
2730 (let ((cp (point)) (p (point)) tb rb (cols 0) th line (i 0) fmt
2731 (ptn "\\(<t[dh]\\>\\)\\|<t\\(r\\|head\\|body\\)\\>"))
2732 (cond
2733 ((save-excursion (setq tb (YaTeX-beginning-of-environment "table")))
2734 (while (and (setq rb (re-search-backward ptn tb t))
2735 (match-beginning 1))
2736 (setq th (looking-at "<th")) ;Remember if first-child is tr or not
2737 (goto-char (match-end 0))
2738 (skip-chars-forward " \t\n")
2739 (if (and (search-forward "colspan\\s *=" p t)
2740 (progn
2741 (skip-chars-forward "\"' \t\n")
2742 (looking-at "[0-9]+")))
2743 (setq cols (+ (string-to-int (YaTeX-match-string 0)) cols))
2744 (setq cols (1+ cols)))
2745 (goto-char rb)
2746 (setq p (point)))
2747 (if (> cols 0)
2748 (message "%s columns found. %s"
2749 cols (if YaTeX-japan "新しいtr(N)? 前のtrの複写?(D)?: "
2750 "New tr?(N) or Duplicate")))
2751 (cond
2752 ((and (> cols 0)
2753 (memq (read-char) '(?d ?D))) ;Duplication mode
2754 (setq line (YaTeX-buffer-substring (point) (1- cp))))
2755 (t ;empty cells
2756 (setq line "<tr>" i 0)
2757 (if (> cols 0)
2758 (while (> cols i)
2759 (setq line (concat line (if (and (= i 0) th) "<th></th>"
2760 "<td></td>"))
2761 th nil i (1+ i)))
2762 (setq fmt (read-string "`th' or `td' format: " "th td td"))
2763 (while (string-match "t\\(h\\)\\|td" fmt i)
2764 (setq line (concat line (if (match-beginning 1) "<th></th>"
2765 "<td></td>"))
2766 i (match-end 0))))
2767 (setq line (concat line "</tr>"))))
2768 (goto-char cp)
2769 (if th
2770 (message
2771 "Type `%s' to change td from/to th."
2772 (key-description (car (where-is-internal 'yahtml-change-*)))))
2773 (if (string< "" line)
2774 (progn
2775 (insert line)
2776 (goto-char (+ 8 cp))
2777 (yahtml-indent-line)))))))
2779 ;;; ---------- Marking ----------
2780 (defun yahtml-mark-begend ()
2781 "Mark current tag"
2782 (interactive)
2783 (YaTeX-beginning-of-environment)
2784 (let ((p (point)))
2785 (save-excursion
2786 (skip-chars-backward " \t" (point-beginning-of-line))
2787 (if (bolp) (setq p (point))))
2788 (push-mark p t))
2789 (yahtml-goto-corresponding-begend)
2790 (forward-list 1)
2791 (if (eolp) (forward-char 1)))
2793 ;;; ---------- complete marks ----------
2794 (defun yahtml-complete-mark ()
2795 "Complete &gt, &lt, &ampersand, and &quote."
2796 (interactive)
2797 (message "1:< 2:> 3:& 4:\" 5:' 6:nbsp")
2798 (let ((c (read-char)))
2799 (setq c (if (or (< c ?0) (> c ?7))
2800 (string-match (regexp-quote (char-to-string c)) "<>&\"")
2801 (- c ?1)))
2802 (if (or (< c 0) (> c 6))
2803 nil
2804 (insert (format "&%s;"
2805 (nth c '("lt" "gt" "amp" "quot" "apos" "nbsp")))))))
2808 ;;; ---------- jump to error line ----------
2809 (defun yahtml-prev-error ()
2810 "Jump to previous error seeing lint buffer."
2811 (interactive)
2812 (or (get-buffer yahtml-lint-buffer)
2813 (error "No lint program ran."))
2814 (YaTeX-showup-buffer yahtml-lint-buffer nil t)
2815 (yahtml-jump-to-error-line t))
2817 (defun yahtml-jump-to-error-line (&optional sit)
2818 (interactive "P")
2819 (let ((p (point)) (e (point-end-of-line)))
2820 (end-of-line)
2821 (if (re-search-backward yahtml-error-line-regexp nil t)
2822 (let ((f (if (string= "" (YaTeX-match-string 1))
2823 YaTeX-current-file-name
2824 (YaTeX-match-string 1)))
2825 (l (string-to-int (or (YaTeX-match-string 2)
2826 (YaTeX-match-string 3)))))
2827 (if sit (sit-for 1))
2828 (forward-line -1)
2829 (YaTeX-showup-buffer (YaTeX-switch-to-buffer f t) nil t)
2830 (goto-line l))
2831 (message "No line number usage"))))
2833 ;;; ---------- Style Sheet Support ----------
2834 (defvar yahtml-css-class-alist nil
2835 "Alist of elements vs. their classes")
2837 (defun yahtml-css-collect-classes-region (beg end &optional initial)
2838 (save-excursion
2839 (save-restriction
2840 (narrow-to-region beg end)
2841 (goto-char (point-min))
2842 (let ((alist initial) b e element class a)
2843 (setq b (point))
2844 (while (re-search-forward "\\({\\)\\|\\(@import\\)" nil t)
2845 (if (match-beginning 2)
2846 (let ((f (YaTeX-buffer-substring
2847 (progn (skip-chars-forward "^\"")(1+ (point)))
2848 (progn (forward-char 1)
2849 (skip-chars-forward "^\"")(point)))))
2850 (if (file-exists-p f)
2851 (setq alist
2852 (append alist (yahtml-css-collect-classes-file f)))))
2853 (setq e (point))
2854 (goto-char b)
2855 (while (re-search-forward ;ちょといい加減なREGEXP
2856 "\\([a-z*][-a-z0-9]*\\)?\\.\\([-a-z0-9][-a-z0-9]*\\)\\>"
2857 e t)
2858 (setq element (YaTeX-match-string 1)
2859 class (YaTeX-match-string 2))
2860 ;;if starts with period (match-string 1 is nil),
2861 ;;this is global class
2862 (setq element (downcase (or element "*")))
2863 (if (setq a (assoc element alist))
2864 (or (assoc class (cdr a))
2865 (setcdr a (cons (list class) (cdr a))))
2866 (setq alist (cons (list element (list class)) alist))))
2867 (goto-char (1- e))
2868 (search-forward "}" nil t)
2869 (setq b (point))))
2870 alist))))
2872 (defun yahtml-css-collect-classes-buffer (&optional initial)
2873 (interactive)
2874 (yahtml-css-collect-classes-region (point-min) (point-max) initial))
2876 (defun yahtml-css-collect-classes-file (file &optional initial)
2877 (let*((hilit-auto-highlight nil)
2878 (buf (get-buffer-create
2879 (format " *css-collection*%s" (file-name-nondirectory file))))
2880 (cb (current-buffer)))
2881 (unwind-protect
2882 (progn
2883 (set-buffer buf)
2884 (insert-file-contents file)
2885 (cd (or (file-name-directory file) "."))
2886 (yahtml-css-collect-classes-buffer initial))
2887 (if (eq buf cb)
2888 nil
2889 (kill-buffer buf)
2890 (set-buffer cb)))))
2892 (defun yahtml-css-scan-styles ()
2893 (save-excursion
2894 (goto-char (point-min))
2895 (set (make-local-variable 'yahtml-css-class-alist) nil)
2896 (let (b tag type e href alist)
2897 (while (re-search-forward "<\\(style\\|link\\)" nil t)
2898 (setq b (match-beginning 0)
2899 tag (YaTeX-match-string 1))
2900 (cond
2901 ((string-match "style" tag)
2902 (goto-char b)
2903 (save-excursion (forward-list 1) (setq e (point)))
2904 (cond
2905 ((search-forward "text/css" e 1) ;css definition starts
2906 (setq alist
2907 (yahtml-css-collect-classes-region
2908 (point) (progn (search-forward "</style>") (point))
2909 alist)))))
2910 ((and (string-match "link" tag)
2911 (stringp (setq type (yahtml-get-attrvalue "type")))
2912 (string-match "text/css" type)
2913 (setq href (yahtml-get-attrvalue "href"))
2914 (file-exists-p (yahtml-url-to-path href)))
2915 (setq alist
2916 (yahtml-css-collect-classes-file
2917 (yahtml-url-to-path href) alist))))
2918 (setq yahtml-css-class-alist alist)))))
2920 (defun yahtml-css-get-element-completion-alist (element)
2921 (let ((alist (cdr-safe (assoc (downcase element) yahtml-css-class-alist)))
2922 (global (cdr-safe (assoc "*" yahtml-css-class-alist))))
2923 (and (or alist global)
2924 (append alist global))))
2926 ;;; ---------- ----------
2928 ;;;
2929 ;;hilit19
2930 ;;;
2931 (defvar yahtml-default-face-table
2932 '(
2933 (form black/ivory white/hex-442233 italic)
2934 ))
2935 (defvar yahtml-hilit-patterns-alist
2936 '(
2937 'case-fold
2938 ;; comments
2939 ("<!--\\s " "-->" comment)
2940 ;; include&exec
2941 ("<!--#\\(include\\|exec\\|config\\|fsize\\|flastmod\\)" "-->" include)
2942 ;; string
2943 (hilit-string-find ?\\ string)
2944 (yahtml-hilit-region-tag "<\\(strong\\|b\\)\\>" bold)
2945 ("</?[uod]l>" 0 decl)
2946 ("<\\(di\\|dt\\|li\\|dd\\)>" 0 label)
2947 (yahtml-hilit-region-tag "<\\(em\\|i\\>\\)" italic)
2948 ;("<a\\s +href" "</a>" crossref) ;good for hilit19, but odd for font-lock..
2949 (yahtml-hilit-region-tag "<\\(a\\)\\s +href" crossref)
2950 (yahtml-hilit-region-tag-itself "</?\\sw+\\>" decl)
2951 ))
2953 (defun yahtml-hilit-region-tag (tag)
2954 "Return list of start/end point of <TAG> form."
2955 (if (re-search-forward tag nil t)
2956 (let ((m0 (match-beginning 0)) (e0 (match-end 0))
2957 (elm (YaTeX-match-string 1)))
2958 (skip-chars-forward "^>")
2959 (prog1
2960 (cons (1+ (point))
2961 (progn (re-search-forward (concat "</" elm ">") nil t)
2962 (match-beginning 0)))
2963 (goto-char e0)))))
2965 (defun yahtml-hilit-region-tag-itself (ptn)
2966 "Return list of start/end point of <tag options...> itself."
2967 (if (re-search-forward ptn nil t)
2968 (let ((m0 (match-beginning 0)) (e0 (match-end 0)))
2969 (skip-chars-forward "^<>")
2970 (if (eq (char-after (point)) ?<) nil
2971 (prog1
2972 (cons m0 (min (point-max) (1+ (point))))
2973 (goto-char e0))))))
2975 ;(setq hilit-patterns-alist (delq (assq 'yahtml-mode hilit-patterns-alist) hilit-patterns-alist))
2976 (and yahtml-use-hilit19
2977 (or (assq 'yahtml-mode hilit-patterns-alist)
2978 (setq hilit-patterns-alist
2979 (cons (cons 'yahtml-mode yahtml-hilit-patterns-alist)
2980 hilit-patterns-alist))))
2981 ;;;
2982 ;; for font-lock
2983 ;;;
2985 ; <<STATIC KEYWORDS BELOW NOT USED>>
2986 ;(defvar yahtml-font-lock-keywords
2987 ; '(
2988 ; ;; comments
2989 ; ("<!--\\s .* -->" . font-lock-comment-face)
2990 ; ;; include&exec
2991 ; ("<!--#\\(include\\|exec\\|config\\|fsize\\|flastmod\\).*-->"
2992 ; 0 font-lock-include-face keep)
2993 ; ;; string
2994 ; ;(hilit-string-find ?\\ string)
2995 ; ;(yahtml-hilit-region-tag "\\(em\\|strong\\)" bold)
2996 ; ("</?[uod]l>" 0 font-lock-keyword-face)
2997 ; ("<\\(di\\|dt\\|li\\|dd\\)>" 0 font-lock-label-face)
2998 ; ("<a\\s +href=.*</a>" (0 font-lock-crossref-face keep))
2999 ; ;(yahtml-hilit-region-tag-itself "</?\\sw+\\>" decl)
3000 ; ("</?\\sw+\\>" (yahtml-fontify-to-tagend nil nil))
3001 ; )
3002 ; "*Defualt font-lock-keywords for yahtml-mode.")
3003 (defvar yahtml-font-lock-keywords
3004 (YaTeX-convert-pattern-hilit2fontlock yahtml-hilit-patterns-alist)
3005 "Default fontifying patterns for yahtml-mode")
3007 (defun yahtml-font-lock-set-default-keywords ()
3008 (put 'yahtml-mode 'font-lock-defaults
3009 '(yahtml-font-lock-keywords nil t)))
3011 (if yahtml-use-font-lock
3012 (progn
3013 (if (and (boundp 'hilit-mode-enable-list) hilit-mode-enable-list)
3014 ;;for those who use both hilit19 and font-lock
3015 (if (eq (car hilit-mode-enable-list) 'not)
3016 (or (member 'yahtml-mode hilit-mode-enable-list)
3017 (nconc hilit-mode-enable-list (list 'yahtml-mode)))
3018 (setq hilit-mode-enable-list
3019 (delq 'yahtml-mode hilit-mode-enable-list))))
3020 (yahtml-font-lock-set-default-keywords)))
3022 (defun yahtml-font-lock-recenter (&optional arg)
3023 (interactive "P")
3024 (font-lock-mode -1) ;is stupid, but sure.
3025 (font-lock-mode 1))
3027 (run-hooks 'yahtml-load-hook)
3028 (provide 'yahtml)
3030 ; Local variables:
3031 ; fill-prefix: ";;; "
3032 ; paragraph-start: "^$\\| \\|;;;$"
3033 ; paragraph-separate: "^$\\| \\|;;;$"
3034 ; coding: sjis
3035 ; End: