yatex

annotate yatexgen.el @ 9:7becca042f80

Initial revision.
author yuuji
date Tue, 04 May 1993 13:01:29 +0000
parents
children 390df0e505da
rev   line source
yuuji@9 1 ;;; -*- Emacs-Lisp -*-
yuuji@9 2 ;;; Generate add-in functions for YaTeX.
yuuji@9 3 ;;; yatexgen.el rev.1(beta2)
yuuji@9 4 ;;; (c)1991-1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
yuuji@9 5 ;;; Last modified Tue May 4 21:51:23 1993 on figaro
yuuji@9 6 ;;; $Id$
yuuji@9 7
yuuji@9 8 (require 'yatex)
yuuji@9 9 (provide 'yatexgen)
yuuji@9 10
yuuji@9 11 (defmacro YaTeX-setq (var japanese english)
yuuji@9 12 (list 'setq var
yuuji@9 13 (if YaTeX-japan japanese english))
yuuji@9 14 )
yuuji@9 15
yuuji@9 16 (put 'YaTeX-setq 'lisp-indent-hook 1)
yuuji@9 17
yuuji@9 18 (YaTeX-setq YaTeX-generate-initial-message
yuuji@9 19 " 自動生成モードへようこそ!!
yuuji@9 20
yuuji@9 21 初めてやる人はこのバッファの例にしたがって指示通りにやって練習してね。
yuuji@9 22 本番の時もこのバッファに出るメッセージを *よく読んで* 操作しないとう
yuuji@9 23 まく関数が作れないよ!!
yuuji@9 24
yuuji@9 25 ではリターンキーを押して下さい。"
yuuji@9 26 " Welcome to auto-generation mode!!
yuuji@9 27
yuuji@9 28 If this is your first trial, exercise this according to example and
yuuji@9 29 following my messages. Then, at making actual function, operate
yuuji@9 30 reading my messages *carefully*, or you'll fail to generate appropriate
yuuji@9 31 function.
yuuji@9 32
yuuji@9 33 Hit return key!"
yuuji@9 34 )
yuuji@9 35
yuuji@9 36 (YaTeX-setq YaTeX-generate-start-message
yuuji@9 37 "さぁはじめるよ.\n1.登録したい補完をやってみて.
yuuji@9 38 たとえば section 型補完の \\documentstyle だったら \\documentstyle{}
yuuji@9 39 だけをいれてみてね. ちゃんと『〜型補完』を使わないとダメよ!。
yuuji@9 40 で、おわったらりたーん!!"
yuuji@9 41 "Let's begin completion for which you want to make add-in function.
yuuji@9 42 If you want to make add-in function for \\documentstyle input only
yuuji@9 43 `\\documentstyle{}' *with* completion of yatex-mode.
yuuji@9 44 If you finish this, please press RET."
yuuji@9 45 )
yuuji@9 46
yuuji@9 47 (YaTeX-setq YaTeX-generate-abort-message
yuuji@9 48 "やめた、やめた〜いめんどくせ〜"
yuuji@9 49 "Aborted."
yuuji@9 50 )
yuuji@9 51
yuuji@9 52 (YaTeX-setq YaTeX-generate-same-message
yuuji@9 53 "それじゃ、なにも変わってねぇだろーが! やめた。"
yuuji@9 54 "I found no difference between them. So I'm quitting."
yuuji@9 55 )
yuuji@9 56
yuuji@9 57 (YaTeX-setq YaTeX-generate-invalid-message
yuuji@9 58 "それは、ちと無理というものじゃ."
yuuji@9 59 "It's impossible."
yuuji@9 60 )
yuuji@9 61
yuuji@9 62 (YaTeX-setq YaTeX-generate-idontknow-message
yuuji@9 63 "う〜ん、難しくてよくわからないなぁ。ばかでごめんねェ〜"
yuuji@9 64 "Sorry I can't tell your adding method."
yuuji@9 65 )
yuuji@9 66
yuuji@9 67 (YaTeX-setq YaTeX-generate-confirm-message
yuuji@9 68 "ということは、付け足したい部分はこれでいいのね"
yuuji@9 69 "Is it additional string of add-in function?"
yuuji@9 70 )
yuuji@9 71
yuuji@9 72 (YaTeX-setq YaTeX-generate-output-message
yuuji@9 73 "2.じゃ、それにくっつけたいものを *カーソルの位置に* 足してみて.
yuuji@9 74 さっきの \\documentstyle{} の例だと \\documentstyle[12pt]{} とかにするの。
yuuji@9 75 しつこいようだけど、今の位置からカーソル動かしちゃダメよ!!
yuuji@9 76 で、またおわったらりたーん!!"
yuuji@9 77 "2.Then input additional string *at CURSOR POSITION*
yuuji@9 78 According to last example \\documentstyle{},
yuuji@9 79 modify it \\documentstyle[12pt]{}. RET to finish."
yuuji@9 80 )
yuuji@9 81
yuuji@9 82 (YaTeX-setq YaTeX-generate-put-arg-message
yuuji@9 83 "3.このうち、キーボードから読み込んで欲しい文字列を順に入れて。
yuuji@9 84 さっきの \\documentstyle[12pt]{} だったら、付加する文字は[12pt]だけど
yuuji@9 85 手で入れたいのは 12pt の部分だけですね。
yuuji@9 86 で、全部入れ終ったら、りたーんだけうってね!!"
yuuji@9 87 "3.In this string, extract string which you want to input from key
yuuji@9 88 board with quiry afterwards. For example, though additional string is
yuuji@9 89 \\documentstyle[12pt]{}, but you want enter only `12pt' by hand.
yuuji@9 90 RET to finish!"
yuuji@9 91 )
yuuji@9 92
yuuji@9 93 (YaTeX-setq YaTeX-generate-read-prompt-message
yuuji@9 94 "4.では、あとでこれらの文字列を読み込む時に、どういうプロンプトを
yuuji@9 95 出したいですか? 順に入れて下さい。面倒なら単にリターンを打ってね。
yuuji@9 96 さっきの 12pt の部分だったら、『サイズは』とかがおすすめ。"
yuuji@9 97 "4.When you use this add-in function afterwards, what message
yuuji@9 98 do you like to be prompted with to enter these values. In last
yuuji@9 99 example `12pt', typical prompt string may be `Size?: '."
yuuji@9 100 )
yuuji@9 101
yuuji@9 102 (YaTeX-setq YaTeX-generate-done-message
yuuji@9 103 "よし! これが、君の作りたかった関数だ。~/.emacs にでも入れてせいぜい
yuuji@9 104 楽してくれ。このバッファ(*ご案内*)を yatex-mode にしておくから
yuuji@9 105 できた関数が本当にお望みの動作をするか確かめてみるといいかもね。
yuuji@9 106 ところで、この関数こんなに簡単だろう? そろそろ自分で書いたらどう?
yuuji@9 107 "
yuuji@9 108 "OK! This is the definition of function you want to make! Add
yuuji@9 109 this description to your ~/.emacs or so. Use this buffer(*Guide*)
yuuji@9 110 for testing of this function please.
yuuji@9 111 But you can see this function quite easy, can't you? You had better
yuuji@9 112 write your most favorite add-in function yourself!
yuuji@9 113 "
yuuji@9 114 )
yuuji@9 115
yuuji@9 116 (YaTeX-setq YaTeX-generate-nomatch-message
yuuji@9 117 "こらこら、そんな文字列どこにもねーぞ!!"
yuuji@9 118 "No such string in additional string."
yuuji@9 119 )
yuuji@9 120 (YaTeX-setq YaTeX-generate-buffer
yuuji@9 121 "*付加関数生成バッファ*"
yuuji@9 122 "*Generate-add-in-function*"
yuuji@9 123 )
yuuji@9 124
yuuji@9 125 (YaTeX-setq YaTeX-generate-message-buffer
yuuji@9 126 "*ご案内*"
yuuji@9 127 "*Guide*"
yuuji@9 128 )
yuuji@9 129
yuuji@9 130 (YaTeX-setq YaTeX-generate-bug-message
yuuji@9 131 "ごめ〜ん!! ちょっと、このアドイン関数つくるの失敗しちゃったみたい!!
yuuji@9 132 作者まで連絡してくださ〜〜〜い!"
yuuji@9 133 "Sorry I failed to make add-in function for you...
yuuji@9 134 Send bug report to me."
yuuji@9 135 )
yuuji@9 136
yuuji@9 137 (YaTeX-setq YaTeX-generate-narrow-message
yuuji@9 138 "画面がせますぎるような気がします。"
yuuji@9 139 "Too narrow screen height."
yuuji@9 140 )
yuuji@9 141
yuuji@9 142 (defvar YaTeX-generate-message-height
yuuji@9 143 10 "Window height of YaTeX-generate-message-buffer")
yuuji@9 144
yuuji@9 145 ;;;
yuuji@9 146 ;Generate mode.
yuuji@9 147 ;;;
yuuji@9 148 (defun YaTeX-generate ()
yuuji@9 149 "Genarate YaTeX add-in function with enquiry."
yuuji@9 150 (interactive)
yuuji@9 151 (if (< (screen-height) (+ YaTeX-generate-message-height 10))
yuuji@9 152 (error YaTeX-generate-narrow-message))
yuuji@9 153 (put 'YaTeX-generate 'disabled t)
yuuji@9 154 (save-window-excursion
yuuji@9 155 (unwind-protect
yuuji@9 156 (let (input output (i 0) (beg 0) end add-in map map1 si str slist
yuuji@9 157 (from (make-marker)) (to (make-marker)))
yuuji@9 158 (delete-other-windows)
yuuji@9 159 (switch-to-buffer YaTeX-generate-message-buffer)
yuuji@9 160 (yatex-mode)
yuuji@9 161 (erase-buffer)
yuuji@9 162 (insert YaTeX-generate-initial-message)
yuuji@9 163 (read-string
yuuji@9 164 (if YaTeX-japan "リターンキーを押して下さい." "Press RETURN."))
yuuji@9 165 (erase-buffer)
yuuji@9 166 (insert YaTeX-generate-start-message)
yuuji@9 167 (pop-to-buffer (get-buffer-create YaTeX-generate-buffer))
yuuji@9 168 (enlarge-window (- (window-height) YaTeX-generate-message-height 1))
yuuji@9 169 (erase-buffer)
yuuji@9 170 (yatex-mode)
yuuji@9 171 (use-local-map (setq map (copy-keymap YaTeX-mode-map)))
yuuji@9 172 (define-key (current-local-map) "\n" 'exit-recursive-edit)
yuuji@9 173 (define-key (current-local-map) "\r" 'exit-recursive-edit)
yuuji@9 174 (define-key (current-local-map) "\C-g" 'abort-recursive-edit)
yuuji@9 175 (setq map1 (copy-keymap map))
yuuji@9 176 (YaTeX-suppress-sparse-keymap map)
yuuji@9 177 ;;First get input form.
yuuji@9 178 (recursive-edit)
yuuji@9 179 (setq input (buffer-string)
yuuji@9 180 end (1- (length input)))
yuuji@9 181 (if (string= "" input) (error YaTeX-generate-abort-message))
yuuji@9 182 (YaTeX-generate-move-to-add-in-position)
yuuji@9 183 (set-marker from (1- (point))) ;;Can't write before `from'
yuuji@9 184 (set-marker to (1+ (point))) ;;Can't write after `to'
yuuji@9 185 ;;Second get output form.
yuuji@9 186 (setq beg (1- (point)));;Cheat begin point!
yuuji@9 187 (YaTeX-generate-display-message YaTeX-generate-output-message)
yuuji@9 188 (use-local-map map1)
yuuji@9 189 (fset 'si (symbol-function 'self-insert-command))
yuuji@9 190 (defun self-insert-command (arg)
yuuji@9 191 (interactive "p")
yuuji@9 192 (if (or (not (equal (buffer-name) YaTeX-generate-buffer))
yuuji@9 193 (and (> (point) (marker-position from))
yuuji@9 194 (< (point) (marker-position to))))
yuuji@9 195 (insert (this-command-keys)) (ding)))
yuuji@9 196 (unwind-protect
yuuji@9 197 (recursive-edit)
yuuji@9 198 (fset 'self-insert-command (symbol-function 'si)))
yuuji@9 199 (setq output (buffer-string))
yuuji@9 200 (cond ((string= "" output) (error YaTeX-generate-abort-message))
yuuji@9 201 ((string= input output) (error YaTeX-generate-same-message))
yuuji@9 202 ((< (length output) (length input))
yuuji@9 203 (error YaTeX-generate-invalid-message)))
yuuji@9 204 ;;(while (and (< beg end) (= (aref input beg) (aref output i)))
yuuji@9 205 ;; (setq beg (1+ beg) i (1+ i))) ;;for universal use.
yuuji@9 206 (setq i (1- (length output)))
yuuji@9 207 (while (and (>= end beg) (= (aref output i) (aref input end)))
yuuji@9 208 (setq end (1- end) i (1- i)))
yuuji@9 209 (setq add-in (substring output beg
yuuji@9 210 (if (= i (1- (length output))) nil (1+ i))))
yuuji@9 211 (erase-buffer)
yuuji@9 212 (insert add-in)
yuuji@9 213 (if (not (y-or-n-p YaTeX-generate-confirm-message))
yuuji@9 214 (error YaTeX-generate-idontknow-message))
yuuji@9 215 ;;Extract arguments.
yuuji@9 216 (YaTeX-generate-display-message YaTeX-generate-put-arg-message)
yuuji@9 217 (setq i 1)
yuuji@9 218 (while (not (string=
yuuji@9 219 "" (setq str (read-string (format "Arg %d: " i)))))
yuuji@9 220 (if (not (string-match (regexp-quote str) add-in))
yuuji@9 221 (progn
yuuji@9 222 (ding)
yuuji@9 223 (YaTeX-generate-display-message
yuuji@9 224 YaTeX-generate-nomatch-message -1))
yuuji@9 225 (setq slist (append slist (list (list str))) i (1+ i)))
yuuji@9 226 );input all of arguments.
yuuji@9 227 ;;Compare with output string.
yuuji@9 228 (set-buffer YaTeX-generate-buffer) ;;for safety
yuuji@9 229 (emacs-lisp-mode)
yuuji@9 230 (if (> i 1)
yuuji@9 231 (YaTeX-generate-parse-add-in slist add-in)
yuuji@9 232 (erase-buffer)
yuuji@9 233 (insert "(defun " (YaTeX-generate-function-name) " ()\n")
yuuji@9 234 (insert "\"" (YaTeX-generate-lisp-quote add-in) "\")\n")
yuuji@9 235 (indent-region (point-min) (point-max) nil)
yuuji@9 236 (message (if YaTeX-japan
yuuji@9 237 "このくらいの関数手で書け!!"
yuuji@9 238 "You don't need me to make such easy function.")))
yuuji@9 239 );let
yuuji@9 240 (put 'YaTeX-generate 'disabled nil)
yuuji@9 241 (put 'YaTeX-addin 'disabled nil)
yuuji@9 242 ))
yuuji@9 243 (YaTeX-generate-display-message YaTeX-generate-done-message)
yuuji@9 244 (switch-to-buffer YaTeX-generate-buffer)
yuuji@9 245 (condition-case error
yuuji@9 246 (eval-current-buffer)
yuuji@9 247 (error (insert YaTeX-generate-bug-message)))
yuuji@9 248 (pop-to-buffer YaTeX-generate-message-buffer)
yuuji@9 249 )
yuuji@9 250
yuuji@9 251 (defun YaTeX-generate-parse-add-in (args add-in)
yuuji@9 252 "Parse add-in string and extract argument for it.
yuuji@9 253 Variable add-in is referred in parent function."
yuuji@9 254 (let ((i 1) j (case-fold-search nil) ;i holds argument number
yuuji@9 255 (prompt (make-vector (length args) ""))
yuuji@9 256 (used (make-vector (length add-in) nil))
yuuji@9 257 func-name (string ""))
yuuji@9 258 ;;Phase 1. extract argument from add-in string.
yuuji@9 259 (mapcar
yuuji@9 260 '(lambda (arg)
yuuji@9 261 (let ((index 0) (match 0) beg end (carg (car arg)))
yuuji@9 262 (YaTeX-generate-display-message
yuuji@9 263 YaTeX-generate-read-prompt-message)
yuuji@9 264 (aset prompt (1- i)
yuuji@9 265 (read-string
yuuji@9 266 (format
yuuji@9 267 (if YaTeX-japan "%d番目(%s)を読む時?: "
yuuji@9 268 "When reading argument #%d(%s)?: ") i (car arg))))
yuuji@9 269 (while (string-match (regexp-quote carg) (substring add-in index))
yuuji@9 270 (setq beg (+ index (match-beginning 0))
yuuji@9 271 end (+ index (match-end 0)))
yuuji@9 272 (if (aref used beg) nil
yuuji@9 273 (setq match (1+ match))
yuuji@9 274 (cond
yuuji@9 275 ((= match 1)
yuuji@9 276 ;;(setq arg (append arg (list (list beg end))))
yuuji@9 277 (YaTeX-generate-register-match))
yuuji@9 278 ((YaTeX-generate-ask-match-position)
yuuji@9 279 (YaTeX-generate-register-match))))
yuuji@9 280 (setq index end))
yuuji@9 281 (setq i (1+ i))))
yuuji@9 282 args)
yuuji@9 283 ;;Phase 2. Generate function!!
yuuji@9 284 (setq i 0)
yuuji@9 285 (setq func-name (YaTeX-generate-function-name))
yuuji@9 286 (while (< i (length add-in))
yuuji@9 287 (setq beg i j (aref used i))
yuuji@9 288 (while (and (< i (length add-in)) (equal j (aref used i)))
yuuji@9 289 (setq i (1+ i)))
yuuji@9 290 (if j ;If it is argument.
yuuji@9 291 (setq string (concat string (format " arg%d" j)))
yuuji@9 292 (setq string
yuuji@9 293 (concat string " \""
yuuji@9 294 (YaTeX-generate-quote-quote (substring add-in beg i))
yuuji@9 295 "\""))
yuuji@9 296 ))
yuuji@9 297 (erase-buffer)
yuuji@9 298 (setq i 1)
yuuji@9 299 (insert
yuuji@9 300 "(defun " func-name " ()\n"
yuuji@9 301 " (let (")
yuuji@9 302 (mapcar
yuuji@9 303 '(lambda (arg)
yuuji@9 304 (insert (format "(arg%d (read-string \"%s: \"))\n"
yuuji@9 305 i (aref prompt (1- i))))
yuuji@9 306 (setq i (1+ i)))
yuuji@9 307 args)
yuuji@9 308 (delete-region (point) (progn (forward-line -1) (end-of-line) (point)))
yuuji@9 309 (insert ")\n(concat " (YaTeX-generate-lisp-quote string)
yuuji@9 310 ")))\n")
yuuji@9 311 (indent-region (point-min) (point) nil)
yuuji@9 312 used)
yuuji@9 313 )
yuuji@9 314
yuuji@9 315 (defun YaTeX-generate-ask-match-position ()
yuuji@9 316 "Ask user whether match-position is in his expectation,
yuuji@9 317 Referencing variables in parent function YaTeX-generate-parse-add-in."
yuuji@9 318 (pop-to-buffer YaTeX-generate-message-buffer)
yuuji@9 319 (goto-char (point-max))
yuuji@9 320 (insert "\n\n"
yuuji@9 321 (format (if YaTeX-japan "%d 番目の引数 %s って"
yuuji@9 322 "Is argument #%d's value `%s' also corresponding to")
yuuji@9 323 i carg) "\n" add-in "\n")
yuuji@9 324 (indent-to-column beg)
yuuji@9 325 (let ((c beg))
yuuji@9 326 (while (< c end) (insert "^") (setq c (1+ c))))
yuuji@9 327 (insert "\n" (if YaTeX-japan "ここにも対応してるの?"
yuuji@9 328 "this underlined part too?"))
yuuji@9 329 (other-window -1)
yuuji@9 330 (y-or-n-p (if YaTeX-japan "下線部はあってますか" "Is underline right"))
yuuji@9 331 )
yuuji@9 332
yuuji@9 333 (defun YaTeX-generate-register-match ()
yuuji@9 334 (nconc arg (list (list beg end)))
yuuji@9 335 (let ((x beg))
yuuji@9 336 (while (< x end) (aset used x i)(setq x (1+ x))))
yuuji@9 337 )
yuuji@9 338
yuuji@9 339 (defun YaTeX-generate-display-message (mes &optional bottom)
yuuji@9 340 "Display message to generation buffer."
yuuji@9 341 (pop-to-buffer YaTeX-generate-message-buffer)
yuuji@9 342 (goto-char (point-max))
yuuji@9 343 (insert "\n\n")
yuuji@9 344 (if bottom (recenter (1- bottom)) (recenter 0))
yuuji@9 345 (insert mes)
yuuji@9 346 (other-window -1)
yuuji@9 347 )
yuuji@9 348
yuuji@9 349 (defun YaTeX-generate-move-to-add-in-position ()
yuuji@9 350 "Move cursor where add-in function should insert string."
yuuji@9 351 (cond
yuuji@9 352 ((eq YaTeX-current-completion-type 'begin)
yuuji@9 353 (goto-char (point-min))
yuuji@9 354 (skip-chars-forward "^{")
yuuji@9 355 (setq env-name
yuuji@9 356 (buffer-substring (1+ (point))
yuuji@9 357 (progn (skip-chars-forward "^}") (point))))
yuuji@9 358 (forward-char 1))
yuuji@9 359 ((eq YaTeX-current-completion-type 'section)
yuuji@9 360 (goto-char (point-min))
yuuji@9 361 (skip-chars-forward "^{"))
yuuji@9 362 ((eq YaTeX-current-completion-type 'maketitle)
yuuji@9 363 (goto-char (point-max))
yuuji@9 364 (if (= (preceding-char) ? )
yuuji@9 365 (forward-char -1)))
yuuji@9 366 )
yuuji@9 367 )
yuuji@9 368
yuuji@9 369 (defun YaTeX-generate-function-name ()
yuuji@9 370 (concat
yuuji@9 371 "YaTeX:"
yuuji@9 372 (cond
yuuji@9 373 ((eq YaTeX-current-completion-type 'begin) env-name)
yuuji@9 374 ((eq YaTeX-current-completion-type 'section) section-name)
yuuji@9 375 ((eq YaTeX-current-completion-type 'maketitle) single-command)))
yuuji@9 376 )
yuuji@9 377
yuuji@9 378 (defun YaTeX-generate-lisp-quote (str)
yuuji@9 379 (let ((len (length str))(i 0) (quote ""))
yuuji@9 380 (while (< i len)
yuuji@9 381 (if (= (aref str i) ?\\)
yuuji@9 382 (setq quote (concat quote "\\")))
yuuji@9 383 (if (= (aref str i) 127)
yuuji@9 384 (setq quote (concat quote "\""))
yuuji@9 385 (setq quote (concat quote (substring str i (1+ i)))))
yuuji@9 386 (setq i (1+ i)))
yuuji@9 387 quote)
yuuji@9 388 )
yuuji@9 389
yuuji@9 390 (defun YaTeX-generate-quote-quote (str)
yuuji@9 391 (let ((len (length str))(i 0) (quote ""))
yuuji@9 392 (while (< i len)
yuuji@9 393 (if (= (aref str i) ?\")
yuuji@9 394 (setq quote (concat quote (char-to-string 127))))
yuuji@9 395 (setq quote (concat quote (substring str i (1+ i))))
yuuji@9 396 (setq i (1+ i)))
yuuji@9 397 quote)
yuuji@9 398 )
yuuji@9 399
yuuji@9 400 (defun YaTeX-suppress-sparse-keymap (map)
yuuji@9 401 (let ((i ? ))
yuuji@9 402 (while (< i 127)
yuuji@9 403 (define-key map (char-to-string i) 'undefined)
yuuji@9 404 (setq i (1+ i))))
yuuji@9 405 )