yatex

annotate yatexgen.el @ 287:5921f28ef77c

Change the first lines.
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 01 Sep 2012 08:14:41 +0900
parents 0734be649cb8
children 2a72779d9c50
rev   line source
yuuji@287 1 ;;; yatexgen.el --- YaTeX add-in function generator(rev.5)
yuuji@287 2
yuuji@79 3 ;;; (c)1991-1995,1999,2000 by HIROSE Yuuji.[yuuji@yatex.org]
yuuji@287 4 ;;; Last modified Sat Sep 1 08:10:36 2012 on firestorm
yuuji@9 5 ;;; $Id$
yuuji@9 6
yuuji@287 7 ;;; Code:
yuuji@9 8 (require 'yatex)
yuuji@9 9
yuuji@9 10 (defmacro YaTeX-setq (var japanese english)
yuuji@9 11 (list 'setq var
yuuji@287 12 (if YaTeX-japan japanese english)))
yuuji@9 13
yuuji@9 14 (put 'YaTeX-setq 'lisp-indent-hook 1)
yuuji@9 15
yuuji@9 16 (YaTeX-setq YaTeX-generate-initial-message
yuuji@9 17 " 自動生成モードへようこそ!!
yuuji@9 18
yuuji@9 19 初めてやる人はこのバッファの例にしたがって指示通りにやって練習してね。
yuuji@9 20 本番の時もこのバッファに出るメッセージを *よく読んで* 操作しないとう
yuuji@9 21 まく関数が作れないよ!!
yuuji@9 22
yuuji@9 23 ではリターンキーを押して下さい。"
yuuji@9 24 " Welcome to auto-generation mode!!
yuuji@9 25
yuuji@9 26 If this is your first trial, exercise this according to example and
yuuji@9 27 following my messages. Then, at making actual function, operate
yuuji@9 28 reading my messages *carefully*, or you'll fail to generate appropriate
yuuji@9 29 function.
yuuji@9 30
yuuji@287 31 Hit return key!")
yuuji@9 32
yuuji@9 33 (YaTeX-setq YaTeX-generate-start-message
yuuji@9 34 "さぁはじめるよ.\n1.登録したい補完をやってみて.
yuuji@9 35 たとえば section 型補完の \\documentstyle だったら \\documentstyle{}
yuuji@9 36 だけをいれてみてね. ちゃんと『〜型補完』を使わないとダメよ!。
yuuji@9 37 で、おわったらりたーん!!"
yuuji@9 38 "Let's begin completion for which you want to make add-in function.
yuuji@9 39 If you want to make add-in function for \\documentstyle input only
yuuji@9 40 `\\documentstyle{}' *with* completion of yatex-mode.
yuuji@287 41 If you finish this, please press RET.")
yuuji@9 42
yuuji@9 43 (YaTeX-setq YaTeX-generate-abort-message
yuuji@9 44 "やめた、やめた〜いめんどくせ〜"
yuuji@287 45 "Aborted.")
yuuji@9 46
yuuji@9 47 (YaTeX-setq YaTeX-generate-same-message
yuuji@9 48 "それじゃ、なにも変わってねぇだろーが! やめた。"
yuuji@287 49 "I found no difference between them. So I'm quitting.")
yuuji@9 50
yuuji@9 51 (YaTeX-setq YaTeX-generate-invalid-message
yuuji@9 52 "それは、ちと無理というものじゃ."
yuuji@287 53 "It's impossible.")
yuuji@9 54
yuuji@9 55 (YaTeX-setq YaTeX-generate-idontknow-message
yuuji@9 56 "う〜ん、難しくてよくわからないなぁ。ばかでごめんねェ〜"
yuuji@287 57 "Sorry I can't tell your adding method.")
yuuji@9 58
yuuji@9 59 (YaTeX-setq YaTeX-generate-confirm-message
yuuji@9 60 "ということは、付け足したい部分はこれでいいのね"
yuuji@287 61 "Is it additional string of add-in function?")
yuuji@9 62
yuuji@9 63 (YaTeX-setq YaTeX-generate-output-message
yuuji@9 64 "2.じゃ、それにくっつけたいものを *カーソルの位置に* 足してみて.
yuuji@9 65 さっきの \\documentstyle{} の例だと \\documentstyle[12pt]{} とかにするの。
yuuji@9 66 しつこいようだけど、今の位置からカーソル動かしちゃダメよ!!
yuuji@9 67 で、またおわったらりたーん!!"
yuuji@9 68 "2.Then input additional string *at CURSOR POSITION*
yuuji@9 69 According to last example \\documentstyle{},
yuuji@287 70 modify it \\documentstyle[12pt]{}. RET to finish.")
yuuji@9 71
yuuji@9 72 (YaTeX-setq YaTeX-generate-put-arg-message
yuuji@9 73 "3.このうち、キーボードから読み込んで欲しい文字列を順に入れて。
yuuji@9 74 さっきの \\documentstyle[12pt]{} だったら、付加する文字は[12pt]だけど
yuuji@9 75 手で入れたいのは 12pt の部分だけですね。
yuuji@9 76 で、全部入れ終ったら、りたーんだけうってね!!"
yuuji@9 77 "3.In this string, extract string which you want to input from key
yuuji@9 78 board with quiry afterwards. For example, though additional string is
yuuji@9 79 \\documentstyle[12pt]{}, but you want enter only `12pt' by hand.
yuuji@287 80 RET to finish!")
yuuji@9 81
yuuji@9 82 (YaTeX-setq YaTeX-generate-read-prompt-message
yuuji@9 83 "4.では、あとでこれらの文字列を読み込む時に、どういうプロンプトを
yuuji@9 84 出したいですか? 順に入れて下さい。面倒なら単にリターンを打ってね。
yuuji@9 85 さっきの 12pt の部分だったら、『サイズは』とかがおすすめ。"
yuuji@9 86 "4.When you use this add-in function afterwards, what message
yuuji@9 87 do you like to be prompted with to enter these values. In last
yuuji@287 88 example `12pt', typical prompt string may be `Size?: '.")
yuuji@9 89
yuuji@9 90 (YaTeX-setq YaTeX-generate-done-message
yuuji@9 91 "よし! これが、君の作りたかった関数だ。~/.emacs にでも入れてせいぜい
yuuji@9 92 楽してくれ。このバッファ(*ご案内*)を yatex-mode にしておくから
yuuji@9 93 できた関数が本当にお望みの動作をするか確かめてみるといいかもね。
yuuji@9 94 ところで、この関数こんなに簡単だろう? そろそろ自分で書いたらどう?
yuuji@9 95 "
yuuji@9 96 "OK! This is the definition of function you want to make! Add
yuuji@9 97 this description to your ~/.emacs or so. Use this buffer(*Guide*)
yuuji@9 98 for testing of this function please.
yuuji@9 99 But you can see this function quite easy, can't you? You had better
yuuji@9 100 write your most favorite add-in function yourself!
yuuji@287 101 ")
yuuji@9 102
yuuji@9 103 (YaTeX-setq YaTeX-generate-nomatch-message
yuuji@9 104 "こらこら、そんな文字列どこにもねーぞ!!"
yuuji@287 105 "No such string in additional string.")
yuuji@287 106
yuuji@9 107 (YaTeX-setq YaTeX-generate-buffer
yuuji@9 108 "*付加関数生成バッファ*"
yuuji@287 109 "*Generate-add-in-function*")
yuuji@9 110
yuuji@9 111 (YaTeX-setq YaTeX-generate-message-buffer
yuuji@9 112 "*ご案内*"
yuuji@287 113 "*Guide*")
yuuji@9 114
yuuji@9 115 (YaTeX-setq YaTeX-generate-bug-message
yuuji@9 116 "ごめ〜ん!! ちょっと、このアドイン関数つくるの失敗しちゃったみたい!!
yuuji@9 117 作者まで連絡してくださ〜〜〜い!"
yuuji@9 118 "Sorry I failed to make add-in function for you...
yuuji@287 119 Send bug report to me.")
yuuji@9 120
yuuji@9 121 (YaTeX-setq YaTeX-generate-narrow-message
yuuji@9 122 "画面がせますぎるような気がします。"
yuuji@287 123 "Too narrow screen height.")
yuuji@9 124
yuuji@9 125 (defvar YaTeX-generate-message-height
yuuji@9 126 10 "Window height of YaTeX-generate-message-buffer")
yuuji@9 127
yuuji@52 128 ;; Do you need learning for generated function?
yuuji@70 129 ;; If you need, please tell me (yuuji@yatex.org)
yuuji@52 130 ;;(defvar YaTeX-generate-variables-for-learning nil)
yuuji@52 131 ;;(defvar YaTeX-generate-current-completion-table nil)
yuuji@52 132
yuuji@9 133 ;;;
yuuji@9 134 ;Generate mode.
yuuji@9 135 ;;;
yuuji@9 136 (defun YaTeX-generate ()
yuuji@9 137 "Genarate YaTeX add-in function with enquiry."
yuuji@9 138 (interactive)
yuuji@70 139 (if (< (YaTeX-screen-height) (+ YaTeX-generate-message-height 10))
yuuji@9 140 (error YaTeX-generate-narrow-message))
yuuji@9 141 (put 'YaTeX-generate 'disabled t)
yuuji@9 142 (save-window-excursion
yuuji@9 143 (unwind-protect
yuuji@9 144 (let (input output (i 0) (beg 0) end add-in map map1 si str slist
yuuji@9 145 (from (make-marker)) (to (make-marker)))
yuuji@9 146 (delete-other-windows)
yuuji@9 147 (switch-to-buffer YaTeX-generate-message-buffer)
yuuji@9 148 (yatex-mode)
yuuji@9 149 (erase-buffer)
yuuji@9 150 (insert YaTeX-generate-initial-message)
yuuji@9 151 (read-string
yuuji@9 152 (if YaTeX-japan "リターンキーを押して下さい." "Press RETURN."))
yuuji@9 153 (erase-buffer)
yuuji@9 154 (insert YaTeX-generate-start-message)
yuuji@9 155 (pop-to-buffer (get-buffer-create YaTeX-generate-buffer))
yuuji@9 156 (enlarge-window (- (window-height) YaTeX-generate-message-height 1))
yuuji@9 157 (erase-buffer)
yuuji@9 158 (yatex-mode)
yuuji@9 159 (use-local-map (setq map (copy-keymap YaTeX-mode-map)))
yuuji@9 160 (define-key (current-local-map) "\n" 'exit-recursive-edit)
yuuji@9 161 (define-key (current-local-map) "\r" 'exit-recursive-edit)
yuuji@9 162 (define-key (current-local-map) "\C-g" 'abort-recursive-edit)
yuuji@9 163 (setq map1 (copy-keymap map))
yuuji@9 164 (YaTeX-suppress-sparse-keymap map)
yuuji@9 165 ;;First get input form.
yuuji@9 166 (recursive-edit)
yuuji@72 167 (setq input (YaTeX-minibuffer-string)
yuuji@9 168 end (1- (length input)))
yuuji@9 169 (if (string= "" input) (error YaTeX-generate-abort-message))
yuuji@9 170 (YaTeX-generate-move-to-add-in-position)
yuuji@9 171 (set-marker from (1- (point))) ;;Can't write before `from'
yuuji@9 172 (set-marker to (1+ (point))) ;;Can't write after `to'
yuuji@9 173 ;;Second get output form.
yuuji@9 174 (setq beg (1- (point)));;Cheat begin point!
yuuji@9 175 (YaTeX-generate-display-message YaTeX-generate-output-message)
yuuji@9 176 (use-local-map map1)
yuuji@9 177 (fset 'si (symbol-function 'self-insert-command))
yuuji@9 178 (defun self-insert-command (arg)
yuuji@9 179 (interactive "p")
yuuji@9 180 (if (or (not (equal (buffer-name) YaTeX-generate-buffer))
yuuji@9 181 (and (> (point) (marker-position from))
yuuji@9 182 (< (point) (marker-position to))))
yuuji@9 183 (insert (this-command-keys)) (ding)))
yuuji@9 184 (unwind-protect
yuuji@9 185 (recursive-edit)
yuuji@9 186 (fset 'self-insert-command (symbol-function 'si)))
yuuji@72 187 (setq output (YaTeX-minibuffer-string))
yuuji@9 188 (cond ((string= "" output) (error YaTeX-generate-abort-message))
yuuji@9 189 ((string= input output) (error YaTeX-generate-same-message))
yuuji@9 190 ((< (length output) (length input))
yuuji@9 191 (error YaTeX-generate-invalid-message)))
yuuji@9 192 ;;(while (and (< beg end) (= (aref input beg) (aref output i)))
yuuji@9 193 ;; (setq beg (1+ beg) i (1+ i))) ;;for universal use.
yuuji@9 194 (setq i (1- (length output)))
yuuji@9 195 (while (and (>= end beg) (= (aref output i) (aref input end)))
yuuji@9 196 (setq end (1- end) i (1- i)))
yuuji@9 197 (setq add-in (substring output beg
yuuji@9 198 (if (= i (1- (length output))) nil (1+ i))))
yuuji@9 199 (erase-buffer)
yuuji@9 200 (insert add-in)
yuuji@9 201 (if (not (y-or-n-p YaTeX-generate-confirm-message))
yuuji@9 202 (error YaTeX-generate-idontknow-message))
yuuji@9 203 ;;Extract arguments.
yuuji@9 204 (YaTeX-generate-display-message YaTeX-generate-put-arg-message)
yuuji@9 205 (setq i 1)
yuuji@9 206 (while (not (string=
yuuji@9 207 "" (setq str (read-string (format "Arg %d: " i)))))
yuuji@9 208 (if (not (string-match (regexp-quote str) add-in))
yuuji@9 209 (progn
yuuji@9 210 (ding)
yuuji@9 211 (YaTeX-generate-display-message
yuuji@9 212 YaTeX-generate-nomatch-message -1))
yuuji@9 213 (setq slist (append slist (list (list str))) i (1+ i)))
yuuji@9 214 );input all of arguments.
yuuji@9 215 ;;Compare with output string.
yuuji@9 216 (set-buffer YaTeX-generate-buffer) ;;for safety
yuuji@9 217 (emacs-lisp-mode)
yuuji@9 218 (if (> i 1)
yuuji@9 219 (YaTeX-generate-parse-add-in slist add-in)
yuuji@9 220 (erase-buffer)
yuuji@9 221 (insert "(defun " (YaTeX-generate-function-name) " ()\n")
yuuji@9 222 (insert "\"" (YaTeX-generate-lisp-quote add-in) "\")\n")
yuuji@9 223 (indent-region (point-min) (point-max) nil)
yuuji@9 224 (message (if YaTeX-japan
yuuji@9 225 "このくらいの関数手で書け!!"
yuuji@9 226 "You don't need me to make such easy function.")))
yuuji@9 227 );let
yuuji@9 228 (put 'YaTeX-generate 'disabled nil)
yuuji@9 229 (put 'YaTeX-addin 'disabled nil)
yuuji@9 230 ))
yuuji@9 231 (YaTeX-generate-display-message YaTeX-generate-done-message)
yuuji@9 232 (switch-to-buffer YaTeX-generate-buffer)
yuuji@9 233 (condition-case error
yuuji@9 234 (eval-current-buffer)
yuuji@9 235 (error (insert YaTeX-generate-bug-message)))
yuuji@287 236 (pop-to-buffer YaTeX-generate-message-buffer))
yuuji@9 237
yuuji@9 238 (defun YaTeX-generate-parse-add-in (args add-in)
yuuji@9 239 "Parse add-in string and extract argument for it.
yuuji@9 240 Variable add-in is referred in parent function."
yuuji@9 241 (let ((i 1) j (case-fold-search nil) ;i holds argument number
yuuji@9 242 (prompt (make-vector (length args) ""))
yuuji@9 243 (used (make-vector (length add-in) nil))
yuuji@9 244 func-name (string ""))
yuuji@9 245 ;;Phase 1. extract argument from add-in string.
yuuji@9 246 (mapcar
yuuji@9 247 '(lambda (arg)
yuuji@9 248 (let ((index 0) (match 0) beg end (carg (car arg)))
yuuji@9 249 (YaTeX-generate-display-message
yuuji@9 250 YaTeX-generate-read-prompt-message)
yuuji@9 251 (aset prompt (1- i)
yuuji@9 252 (read-string
yuuji@9 253 (format
yuuji@9 254 (if YaTeX-japan "%d番目(%s)を読む時?: "
yuuji@9 255 "When reading argument #%d(%s)?: ") i (car arg))))
yuuji@9 256 (while (string-match (regexp-quote carg) (substring add-in index))
yuuji@9 257 (setq beg (+ index (match-beginning 0))
yuuji@9 258 end (+ index (match-end 0)))
yuuji@9 259 (if (aref used beg) nil
yuuji@9 260 (setq match (1+ match))
yuuji@9 261 (cond
yuuji@9 262 ((= match 1)
yuuji@9 263 ;;(setq arg (append arg (list (list beg end))))
yuuji@9 264 (YaTeX-generate-register-match))
yuuji@9 265 ((YaTeX-generate-ask-match-position)
yuuji@9 266 (YaTeX-generate-register-match))))
yuuji@9 267 (setq index end))
yuuji@9 268 (setq i (1+ i))))
yuuji@9 269 args)
yuuji@9 270 ;;Phase 2. Generate function!!
yuuji@9 271 (setq i 0)
yuuji@9 272 (setq func-name (YaTeX-generate-function-name))
yuuji@9 273 (while (< i (length add-in))
yuuji@9 274 (setq beg i j (aref used i))
yuuji@9 275 (while (and (< i (length add-in)) (equal j (aref used i)))
yuuji@9 276 (setq i (1+ i)))
yuuji@9 277 (if j ;If it is argument.
yuuji@9 278 (setq string (concat string (format " arg%d" j)))
yuuji@9 279 (setq string
yuuji@9 280 (concat string " \""
yuuji@9 281 (YaTeX-generate-quote-quote (substring add-in beg i))
yuuji@9 282 "\""))
yuuji@9 283 ))
yuuji@9 284 (erase-buffer)
yuuji@9 285 (setq i 1)
yuuji@9 286 (insert
yuuji@9 287 "(defun " func-name " ()\n"
yuuji@9 288 " (let (")
yuuji@9 289 (mapcar
yuuji@9 290 '(lambda (arg)
yuuji@9 291 (insert (format "(arg%d (read-string \"%s: \"))\n"
yuuji@9 292 i (aref prompt (1- i))))
yuuji@9 293 (setq i (1+ i)))
yuuji@9 294 args)
yuuji@9 295 (delete-region (point) (progn (forward-line -1) (end-of-line) (point)))
yuuji@9 296 (insert ")\n(concat " (YaTeX-generate-lisp-quote string)
yuuji@9 297 ")))\n")
yuuji@9 298 (indent-region (point-min) (point) nil)
yuuji@287 299 used))
yuuji@9 300
yuuji@9 301 (defun YaTeX-generate-ask-match-position ()
yuuji@9 302 "Ask user whether match-position is in his expectation,
yuuji@9 303 Referencing variables in parent function YaTeX-generate-parse-add-in."
yuuji@9 304 (pop-to-buffer YaTeX-generate-message-buffer)
yuuji@9 305 (goto-char (point-max))
yuuji@9 306 (insert "\n\n"
yuuji@9 307 (format (if YaTeX-japan "%d 番目の引数 %s って"
yuuji@9 308 "Is argument #%d's value `%s' also corresponding to")
yuuji@9 309 i carg) "\n" add-in "\n")
yuuji@9 310 (indent-to-column beg)
yuuji@9 311 (let ((c beg))
yuuji@9 312 (while (< c end) (insert "^") (setq c (1+ c))))
yuuji@9 313 (insert "\n" (if YaTeX-japan "ここにも対応してるの?"
yuuji@9 314 "this underlined part too?"))
yuuji@9 315 (other-window -1)
yuuji@287 316 (y-or-n-p (if YaTeX-japan "下線部はあってますか" "Is underline right")))
yuuji@9 317
yuuji@9 318 (defun YaTeX-generate-register-match ()
yuuji@9 319 (nconc arg (list (list beg end)))
yuuji@9 320 (let ((x beg))
yuuji@287 321 (while (< x end) (aset used x i)(setq x (1+ x)))))
yuuji@9 322
yuuji@9 323 (defun YaTeX-generate-display-message (mes &optional bottom)
yuuji@9 324 "Display message to generation buffer."
yuuji@9 325 (pop-to-buffer YaTeX-generate-message-buffer)
yuuji@9 326 (goto-char (point-max))
yuuji@9 327 (insert "\n\n")
yuuji@9 328 (if bottom (recenter (1- bottom)) (recenter 0))
yuuji@9 329 (insert mes)
yuuji@287 330 (other-window -1))
yuuji@9 331
yuuji@9 332 (defun YaTeX-generate-move-to-add-in-position ()
yuuji@9 333 "Move cursor where add-in function should insert string."
yuuji@9 334 (cond
yuuji@9 335 ((eq YaTeX-current-completion-type 'begin)
yuuji@9 336 (goto-char (point-min))
yuuji@9 337 (skip-chars-forward "^{")
yuuji@72 338 (setq YaTeX-env-name
yuuji@9 339 (buffer-substring (1+ (point))
yuuji@9 340 (progn (skip-chars-forward "^}") (point))))
yuuji@9 341 (forward-char 1))
yuuji@9 342 ((eq YaTeX-current-completion-type 'section)
yuuji@9 343 (goto-char (point-min))
yuuji@9 344 (skip-chars-forward "^{"))
yuuji@9 345 ((eq YaTeX-current-completion-type 'maketitle)
yuuji@9 346 (goto-char (point-max))
yuuji@9 347 (if (= (preceding-char) ? )
yuuji@287 348 (forward-char -1)))))
yuuji@9 349
yuuji@9 350 (defun YaTeX-generate-function-name ()
yuuji@9 351 (concat
yuuji@9 352 "YaTeX:"
yuuji@9 353 (cond
yuuji@72 354 ((eq YaTeX-current-completion-type 'begin) YaTeX-env-name)
yuuji@72 355 ((eq YaTeX-current-completion-type 'section) YaTeX-section-name)
yuuji@287 356 ((eq YaTeX-current-completion-type 'maketitle) YaTeX-single-command))))
yuuji@9 357
yuuji@9 358 (defun YaTeX-generate-lisp-quote (str)
yuuji@9 359 (let ((len (length str))(i 0) (quote ""))
yuuji@9 360 (while (< i len)
yuuji@9 361 (if (= (aref str i) ?\\)
yuuji@9 362 (setq quote (concat quote "\\")))
yuuji@9 363 (if (= (aref str i) 127)
yuuji@9 364 (setq quote (concat quote "\""))
yuuji@9 365 (setq quote (concat quote (substring str i (1+ i)))))
yuuji@9 366 (setq i (1+ i)))
yuuji@287 367 quote))
yuuji@9 368
yuuji@9 369 (defun YaTeX-generate-quote-quote (str)
yuuji@9 370 (let ((len (length str))(i 0) (quote ""))
yuuji@9 371 (while (< i len)
yuuji@9 372 (if (= (aref str i) ?\")
yuuji@9 373 (setq quote (concat quote (char-to-string 127))))
yuuji@9 374 (setq quote (concat quote (substring str i (1+ i))))
yuuji@9 375 (setq i (1+ i)))
yuuji@287 376 quote))
yuuji@9 377
yuuji@9 378 (defun YaTeX-suppress-sparse-keymap (map)
yuuji@9 379 (let ((i ? ))
yuuji@9 380 (while (< i 127)
yuuji@9 381 (define-key map (char-to-string i) 'undefined)
yuuji@287 382 (setq i (1+ i)))))
yuuji@23 383
yuuji@23 384 ;;;
yuuji@23 385 ;; Auto-generate Function for Lispers.
yuuji@23 386 ;;;
yuuji@23 387 (defun YaTeX-generate-read-completion-type (nth)
yuuji@23 388 (message
yuuji@23 389 "Read type(%d): (S)tring (C)omplete (F)ile ([)option (P)osition co(O)rd. (q)uit" nth)
yuuji@23 390 (let ((c (read-char)))
yuuji@23 391 (cond
yuuji@23 392 ((= c ?s) 'string)
yuuji@23 393 ((= c ?c) 'completion)
yuuji@23 394 ((= c ?f) 'file)
yuuji@23 395 ((= c ?\[) 'option)
yuuji@23 396 ((= c ?p) 'oneof)
yuuji@23 397 ((= c ?o) 'coord)
yuuji@23 398 ;;((= c ?m) 'macro)
yuuji@287 399 (t 'quit))))
yuuji@287 400
yuuji@23 401 (defun YaTeX-generate-read-completion-table ()
yuuji@23 402 (let ((i 1) cand (cands "(") (cb (current-buffer))
yuuji@23 403 (buf (get-buffer-create " *Candidates*")))
yuuji@23 404 (save-window-excursion
yuuji@23 405 (save-excursion
yuuji@23 406 (YaTeX-showup-buffer buf nil)
yuuji@23 407 (set-buffer buf)
yuuji@23 408 (erase-buffer)
yuuji@23 409 (while (string<
yuuji@23 410 ""
yuuji@23 411 (setq cand (read-string (format "Item[%d](RET to exit): " i))))
yuuji@46 412 (setq cands (concat cands (format "(\"%s\")\n" cand))
yuuji@23 413 i (1+ i))
yuuji@23 414 (insert cand "\n"))
yuuji@23 415 (kill-buffer buf)))
yuuji@23 416 ;;(set-buffer cb)
yuuji@287 417 (setq YaTeX-generate-current-completion-table (concat cands ")"))))
yuuji@287 418
yuuji@23 419 (defun YaTeX-generate-corresponding-paren (left)
yuuji@23 420 (cond
yuuji@23 421 ((equal left "{") "}")
yuuji@23 422 ((equal left "[") "]")
yuuji@23 423 ((equal left "(") ")")
yuuji@23 424 ((equal left "<") ">")
yuuji@59 425 ((equal left "\\begin{") "}")
yuuji@287 426 (t left)))
yuuji@287 427
yuuji@23 428 (defun YaTeX-generate-create-read-string (&optional nth)
yuuji@23 429 (concat
yuuji@46 430 "(read-string \""
yuuji@23 431 (read-string (if nth (format "Prompt for argument#%d: " nth) "Prompt: "))
yuuji@46 432 ": \"\n"
yuuji@46 433 "\"" (read-string "Default: ") "\""
yuuji@287 434 ")\n"))
yuuji@287 435
yuuji@23 436 (defun YaTeX-generate-create-completing-read (&optional nth)
yuuji@52 437 (prog1
yuuji@52 438 (concat
yuuji@52 439 "(completing-read \""
yuuji@52 440 (read-string
yuuji@52 441 (if nth (format "Prompt for argument#%d: " nth) "Prompt: "))
yuuji@52 442 ": \"\n"
yuuji@52 443 (format "'%s\n" (YaTeX-generate-read-completion-table))
yuuji@52 444 "nil "
yuuji@52 445 (format "%s)" (y-or-n-p "Require match? ")))
yuuji@52 446 (if nil ;;;(y-or-n-p "Do you need learning for this completion?")
yuuji@52 447 (setq YaTeX-generate-variables-for-learning
yuuji@52 448 (cons
yuuji@52 449 (cons (format "YaTeX-%s-%d" command (or nth 0))
yuuji@52 450 YaTeX-generate-current-completion-table)
yuuji@287 451 YaTeX-generate-variables-for-learning)))))
yuuji@287 452
yuuji@23 453 (defun YaTeX-generate-create-read-file-name (&optional nth)
yuuji@23 454 (concat
yuuji@46 455 "(read-file-name \""
yuuji@23 456 (read-string (if nth (format "Prompt for argument#%d: " nth) "Prompt: "))
yuuji@287 457 ": \" "" \"\" t \"\")\n"))
yuuji@287 458
yuuji@23 459 (defun YaTeX-generate-create-read-oneof (&optional nth readpos)
yuuji@23 460 (concat
yuuji@23 461 (if readpos
yuuji@46 462 "(YaTeX:read-position \""
yuuji@46 463 "(YaTeX:read-oneof \"")
yuuji@287 464 (read-string "Acceptable characters: " "lcr") "\")\n"))
yuuji@287 465
yuuji@23 466 (defun YaTeX-generate-option-type (command)
yuuji@23 467 (let ((func (format "YaTeX:%s" command)) leftp
yuuji@23 468 (buf (get-buffer-create YaTeX-generate-buffer)) type (n 1))
yuuji@23 469 (set-buffer buf)
yuuji@23 470 (erase-buffer)
yuuji@23 471 (insert "(defun " func " ()\n (concat\n")
yuuji@23 472 (catch 'done
yuuji@23 473 (while t
yuuji@23 474 (setq type (YaTeX-generate-read-completion-type n))
yuuji@23 475 (insert
yuuji@23 476 (cond
yuuji@23 477 ;;Read string
yuuji@23 478 ((eq type 'string)
yuuji@52 479 (concat "\"" (setq leftp (read-string "Left parenthesis: " "("))
yuuji@52 480 "\"\n"
yuuji@23 481 (YaTeX-generate-create-read-string)
yuuji@52 482 "\"" (YaTeX-generate-corresponding-paren leftp) "\"")
yuuji@52 483 )
yuuji@23 484 ;;Completing-read
yuuji@23 485 ((eq type 'completion)
yuuji@52 486 (concat "\"" (setq leftp (read-string "Left parenthesis: " "{"))
yuuji@52 487 "\"\n"
yuuji@23 488 (YaTeX-generate-create-completing-read)
yuuji@52 489 "\"" (YaTeX-generate-corresponding-paren leftp) "\"")
yuuji@23 490 )
yuuji@23 491 ((eq type 'file)
yuuji@52 492 (concat "\"" (setq leftp (read-string "Left parenthesis: " "("))
yuuji@52 493 "\"\n"
yuuji@23 494 (YaTeX-generate-create-read-file-name)
yuuji@52 495 "\"" (YaTeX-generate-corresponding-paren leftp) "\"")
yuuji@23 496 )
yuuji@23 497 ((eq type 'oneof)
yuuji@23 498 (YaTeX-generate-create-read-oneof nil t)
yuuji@23 499 )
yuuji@23 500 ((eq type 'option)
yuuji@46 501 (concat "(let ((op (read-string \""
yuuji@23 502 (read-string "Prompt: ")
yuuji@46 503 ": \")))\n"
yuuji@46 504 "(if (string< \"\" op)\n"
yuuji@46 505 " (concat \"[\" op \"]\")\n"
yuuji@46 506 " \"\"))\n")
yuuji@23 507 )
yuuji@23 508 ((eq type 'coord)
yuuji@46 509 (concat "(YaTeX:read-coordinates \""
yuuji@23 510 (read-string "Prompt for coordinates: ")
yuuji@46 511 ": \")\n")
yuuji@23 512 )
yuuji@23 513 ((eq type 'macro)
yuuji@23 514 (error "not yet supported")
yuuji@23 515 )
yuuji@23 516 (t (throw 'done t))))
yuuji@23 517 (setq n (1+ n))))
yuuji@23 518 (insert "))\n") ;close defun
yuuji@23 519 (goto-char (point-min))
yuuji@23 520 (while (not (eobp)) (lisp-indent-line) (forward-line 1))
yuuji@23 521 (eval-current-buffer)
yuuji@287 522 buf))
yuuji@287 523
yuuji@23 524 (defun YaTeX-generate-argument-type (command argc)
yuuji@23 525 "Create an argument-type add-in function."
yuuji@23 526 (interactive)
yuuji@23 527 (let ((func (format "YaTeX::%s" command)) (argp 1)
yuuji@23 528 (cb (current-buffer))
yuuji@23 529 (buf (get-buffer-create YaTeX-generate-buffer)))
yuuji@23 530 (set-buffer buf)
yuuji@23 531 (erase-buffer)
yuuji@23 532 (insert "(defun " func " (&optional argp)\n(cond\n")
yuuji@23 533 (while (<= argp argc)
yuuji@23 534 (insert (format "((equal argp %d)\n" argp))
yuuji@23 535 (setq type (YaTeX-generate-read-completion-type argp))
yuuji@23 536 (insert
yuuji@23 537 (cond
yuuji@23 538 ((eq type 'string)
yuuji@23 539 (concat (YaTeX-generate-create-read-string argp)))
yuuji@23 540 ((eq type 'completion)
yuuji@23 541 (concat (YaTeX-generate-create-completing-read argp)))
yuuji@23 542 ((eq type 'oneof)
yuuji@23 543 (YaTeX-generate-create-read-oneof))
yuuji@23 544 ((eq type 'file)
yuuji@23 545 (concat (YaTeX-generate-create-read-file-name argp)))
yuuji@23 546 (t ""))
yuuji@23 547 ")\n")
yuuji@23 548 (setq argp (1+ argp)))
yuuji@23 549 (insert "))\n")
yuuji@23 550 (goto-char (point-min))
yuuji@23 551 (while (not (eobp)) (lisp-indent-line) (forward-line 1))
yuuji@23 552 (eval-current-buffer)
yuuji@23 553 (set-buffer cb)
yuuji@23 554 (YaTeX-update-table
yuuji@23 555 (if (> argc 1) (list command argc) (list command))
yuuji@23 556 'section-table 'user-section-table 'tmp-section-table)
yuuji@287 557 buf))
yuuji@287 558
yuuji@23 559 (defun YaTeX-generate-simple (&optional command)
yuuji@23 560 "Simple but requiring some elisp knowledge add-in generator."
yuuji@23 561 (interactive)
yuuji@52 562 (setq YaTeX-generate-variables-for-learning nil)
yuuji@23 563 (or command
yuuji@23 564 (setq command
yuuji@23 565 (completing-read
yuuji@72 566 (format
yuuji@72 567 "Making add-in function for (default %s): " YaTeX-section-name)
yuuji@23 568 (append
yuuji@23 569 section-table user-section-table tmp-section-table
yuuji@23 570 env-table user-env-table tmp-env-table
yuuji@23 571 singlecmd-table user-singlecmd-table tmp-singlecmd-table)
yuuji@23 572 nil nil)
yuuji@72 573 command (if (string= "" command) YaTeX-section-name command)))
yuuji@49 574 (message
yuuji@49 575 (cond
yuuji@49 576 (YaTeX-japan "(o)追加型? (a)引数型? (yatexadd.docを参照のこと) :")
yuuji@49 577 (t "(O)ption? (A)rgument?")))
yuuji@23 578 (YaTeX-showup-buffer
yuuji@23 579 (if (= (read-char) ?o)
yuuji@23 580 (YaTeX-generate-option-type command)
yuuji@23 581 (YaTeX-generate-argument-type
yuuji@23 582 command
yuuji@287 583 (string-to-int (read-string "How many arguments?: ")))) nil))
yuuji@287 584
yuuji@70 585 (provide 'yatexgen)