yatex

annotate yahtml.el @ 199:76fe4e01931d

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