yatex

annotate yatexgen.el @ 536:88878af9203d

Modify "sync" target to use hg instead of rsync.
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 31 May 2018 09:31:29 +0900
parents 77a2fd4fd634
children
rev   line source
yuuji@287 1 ;;; yatexgen.el --- YaTeX add-in function generator(rev.5)
yuuji@287 2
yuuji@451 3 ;;; (c)1991-1995,1999,2000,2017 by HIROSE Yuuji.[yuuji@yatex.org]
yuuji@353 4 ;;; Last modified Sun Dec 21 14:04:49 2014 on firestorm
yuuji@366 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@353 247 (function
yuuji@353 248 (lambda (arg)
yuuji@9 249 (let ((index 0) (match 0) beg end (carg (car arg)))
yuuji@9 250 (YaTeX-generate-display-message
yuuji@9 251 YaTeX-generate-read-prompt-message)
yuuji@9 252 (aset prompt (1- i)
yuuji@9 253 (read-string
yuuji@9 254 (format
yuuji@9 255 (if YaTeX-japan "%d番目(%s)を読む時?: "
yuuji@9 256 "When reading argument #%d(%s)?: ") i (car arg))))
yuuji@9 257 (while (string-match (regexp-quote carg) (substring add-in index))
yuuji@9 258 (setq beg (+ index (match-beginning 0))
yuuji@9 259 end (+ index (match-end 0)))
yuuji@9 260 (if (aref used beg) nil
yuuji@9 261 (setq match (1+ match))
yuuji@9 262 (cond
yuuji@9 263 ((= match 1)
yuuji@9 264 ;;(setq arg (append arg (list (list beg end))))
yuuji@9 265 (YaTeX-generate-register-match))
yuuji@9 266 ((YaTeX-generate-ask-match-position)
yuuji@9 267 (YaTeX-generate-register-match))))
yuuji@9 268 (setq index end))
yuuji@353 269 (setq i (1+ i)))))
yuuji@9 270 args)
yuuji@9 271 ;;Phase 2. Generate function!!
yuuji@9 272 (setq i 0)
yuuji@9 273 (setq func-name (YaTeX-generate-function-name))
yuuji@9 274 (while (< i (length add-in))
yuuji@9 275 (setq beg i j (aref used i))
yuuji@9 276 (while (and (< i (length add-in)) (equal j (aref used i)))
yuuji@9 277 (setq i (1+ i)))
yuuji@9 278 (if j ;If it is argument.
yuuji@9 279 (setq string (concat string (format " arg%d" j)))
yuuji@9 280 (setq string
yuuji@9 281 (concat string " \""
yuuji@9 282 (YaTeX-generate-quote-quote (substring add-in beg i))
yuuji@9 283 "\""))
yuuji@9 284 ))
yuuji@9 285 (erase-buffer)
yuuji@9 286 (setq i 1)
yuuji@9 287 (insert
yuuji@9 288 "(defun " func-name " ()\n"
yuuji@9 289 " (let (")
yuuji@9 290 (mapcar
yuuji@353 291 (function
yuuji@353 292 (lambda (arg)
yuuji@9 293 (insert (format "(arg%d (read-string \"%s: \"))\n"
yuuji@9 294 i (aref prompt (1- i))))
yuuji@353 295 (setq i (1+ i))))
yuuji@9 296 args)
yuuji@9 297 (delete-region (point) (progn (forward-line -1) (end-of-line) (point)))
yuuji@9 298 (insert ")\n(concat " (YaTeX-generate-lisp-quote string)
yuuji@9 299 ")))\n")
yuuji@9 300 (indent-region (point-min) (point) nil)
yuuji@287 301 used))
yuuji@9 302
yuuji@9 303 (defun YaTeX-generate-ask-match-position ()
yuuji@9 304 "Ask user whether match-position is in his expectation,
yuuji@9 305 Referencing variables in parent function YaTeX-generate-parse-add-in."
yuuji@9 306 (pop-to-buffer YaTeX-generate-message-buffer)
yuuji@9 307 (goto-char (point-max))
yuuji@9 308 (insert "\n\n"
yuuji@9 309 (format (if YaTeX-japan "%d 番目の引数 %s って"
yuuji@9 310 "Is argument #%d's value `%s' also corresponding to")
yuuji@9 311 i carg) "\n" add-in "\n")
yuuji@9 312 (indent-to-column beg)
yuuji@9 313 (let ((c beg))
yuuji@9 314 (while (< c end) (insert "^") (setq c (1+ c))))
yuuji@9 315 (insert "\n" (if YaTeX-japan "ここにも対応してるの?"
yuuji@9 316 "this underlined part too?"))
yuuji@9 317 (other-window -1)
yuuji@287 318 (y-or-n-p (if YaTeX-japan "下線部はあってますか" "Is underline right")))
yuuji@9 319
yuuji@9 320 (defun YaTeX-generate-register-match ()
yuuji@9 321 (nconc arg (list (list beg end)))
yuuji@9 322 (let ((x beg))
yuuji@287 323 (while (< x end) (aset used x i)(setq x (1+ x)))))
yuuji@9 324
yuuji@9 325 (defun YaTeX-generate-display-message (mes &optional bottom)
yuuji@9 326 "Display message to generation buffer."
yuuji@9 327 (pop-to-buffer YaTeX-generate-message-buffer)
yuuji@9 328 (goto-char (point-max))
yuuji@9 329 (insert "\n\n")
yuuji@9 330 (if bottom (recenter (1- bottom)) (recenter 0))
yuuji@9 331 (insert mes)
yuuji@287 332 (other-window -1))
yuuji@9 333
yuuji@9 334 (defun YaTeX-generate-move-to-add-in-position ()
yuuji@9 335 "Move cursor where add-in function should insert string."
yuuji@9 336 (cond
yuuji@9 337 ((eq YaTeX-current-completion-type 'begin)
yuuji@9 338 (goto-char (point-min))
yuuji@9 339 (skip-chars-forward "^{")
yuuji@72 340 (setq YaTeX-env-name
yuuji@9 341 (buffer-substring (1+ (point))
yuuji@9 342 (progn (skip-chars-forward "^}") (point))))
yuuji@9 343 (forward-char 1))
yuuji@9 344 ((eq YaTeX-current-completion-type 'section)
yuuji@9 345 (goto-char (point-min))
yuuji@9 346 (skip-chars-forward "^{"))
yuuji@9 347 ((eq YaTeX-current-completion-type 'maketitle)
yuuji@9 348 (goto-char (point-max))
yuuji@9 349 (if (= (preceding-char) ? )
yuuji@287 350 (forward-char -1)))))
yuuji@9 351
yuuji@9 352 (defun YaTeX-generate-function-name ()
yuuji@9 353 (concat
yuuji@9 354 "YaTeX:"
yuuji@9 355 (cond
yuuji@72 356 ((eq YaTeX-current-completion-type 'begin) YaTeX-env-name)
yuuji@72 357 ((eq YaTeX-current-completion-type 'section) YaTeX-section-name)
yuuji@287 358 ((eq YaTeX-current-completion-type 'maketitle) YaTeX-single-command))))
yuuji@9 359
yuuji@9 360 (defun YaTeX-generate-lisp-quote (str)
yuuji@9 361 (let ((len (length str))(i 0) (quote ""))
yuuji@9 362 (while (< i len)
yuuji@9 363 (if (= (aref str i) ?\\)
yuuji@9 364 (setq quote (concat quote "\\")))
yuuji@9 365 (if (= (aref str i) 127)
yuuji@9 366 (setq quote (concat quote "\""))
yuuji@9 367 (setq quote (concat quote (substring str i (1+ i)))))
yuuji@9 368 (setq i (1+ i)))
yuuji@287 369 quote))
yuuji@9 370
yuuji@9 371 (defun YaTeX-generate-quote-quote (str)
yuuji@9 372 (let ((len (length str))(i 0) (quote ""))
yuuji@9 373 (while (< i len)
yuuji@9 374 (if (= (aref str i) ?\")
yuuji@9 375 (setq quote (concat quote (char-to-string 127))))
yuuji@9 376 (setq quote (concat quote (substring str i (1+ i))))
yuuji@9 377 (setq i (1+ i)))
yuuji@287 378 quote))
yuuji@9 379
yuuji@9 380 (defun YaTeX-suppress-sparse-keymap (map)
yuuji@9 381 (let ((i ? ))
yuuji@9 382 (while (< i 127)
yuuji@9 383 (define-key map (char-to-string i) 'undefined)
yuuji@287 384 (setq i (1+ i)))))
yuuji@23 385
yuuji@23 386 ;;;
yuuji@23 387 ;; Auto-generate Function for Lispers.
yuuji@23 388 ;;;
yuuji@23 389 (defun YaTeX-generate-read-completion-type (nth)
yuuji@23 390 (message
yuuji@23 391 "Read type(%d): (S)tring (C)omplete (F)ile ([)option (P)osition co(O)rd. (q)uit" nth)
yuuji@23 392 (let ((c (read-char)))
yuuji@23 393 (cond
yuuji@23 394 ((= c ?s) 'string)
yuuji@23 395 ((= c ?c) 'completion)
yuuji@23 396 ((= c ?f) 'file)
yuuji@23 397 ((= c ?\[) 'option)
yuuji@23 398 ((= c ?p) 'oneof)
yuuji@23 399 ((= c ?o) 'coord)
yuuji@23 400 ;;((= c ?m) 'macro)
yuuji@287 401 (t 'quit))))
yuuji@287 402
yuuji@23 403 (defun YaTeX-generate-read-completion-table ()
yuuji@23 404 (let ((i 1) cand (cands "(") (cb (current-buffer))
yuuji@23 405 (buf (get-buffer-create " *Candidates*")))
yuuji@23 406 (save-window-excursion
yuuji@23 407 (save-excursion
yuuji@23 408 (YaTeX-showup-buffer buf nil)
yuuji@23 409 (set-buffer buf)
yuuji@23 410 (erase-buffer)
yuuji@23 411 (while (string<
yuuji@23 412 ""
yuuji@23 413 (setq cand (read-string (format "Item[%d](RET to exit): " i))))
yuuji@46 414 (setq cands (concat cands (format "(\"%s\")\n" cand))
yuuji@23 415 i (1+ i))
yuuji@23 416 (insert cand "\n"))
yuuji@23 417 (kill-buffer buf)))
yuuji@23 418 ;;(set-buffer cb)
yuuji@287 419 (setq YaTeX-generate-current-completion-table (concat cands ")"))))
yuuji@287 420
yuuji@23 421 (defun YaTeX-generate-corresponding-paren (left)
yuuji@23 422 (cond
yuuji@23 423 ((equal left "{") "}")
yuuji@23 424 ((equal left "[") "]")
yuuji@23 425 ((equal left "(") ")")
yuuji@23 426 ((equal left "<") ">")
yuuji@59 427 ((equal left "\\begin{") "}")
yuuji@287 428 (t left)))
yuuji@287 429
yuuji@23 430 (defun YaTeX-generate-create-read-string (&optional nth)
yuuji@23 431 (concat
yuuji@46 432 "(read-string \""
yuuji@23 433 (read-string (if nth (format "Prompt for argument#%d: " nth) "Prompt: "))
yuuji@46 434 ": \"\n"
yuuji@46 435 "\"" (read-string "Default: ") "\""
yuuji@287 436 ")\n"))
yuuji@287 437
yuuji@23 438 (defun YaTeX-generate-create-completing-read (&optional nth)
yuuji@52 439 (prog1
yuuji@52 440 (concat
yuuji@52 441 "(completing-read \""
yuuji@52 442 (read-string
yuuji@52 443 (if nth (format "Prompt for argument#%d: " nth) "Prompt: "))
yuuji@52 444 ": \"\n"
yuuji@52 445 (format "'%s\n" (YaTeX-generate-read-completion-table))
yuuji@52 446 "nil "
yuuji@52 447 (format "%s)" (y-or-n-p "Require match? ")))
yuuji@52 448 (if nil ;;;(y-or-n-p "Do you need learning for this completion?")
yuuji@52 449 (setq YaTeX-generate-variables-for-learning
yuuji@52 450 (cons
yuuji@52 451 (cons (format "YaTeX-%s-%d" command (or nth 0))
yuuji@52 452 YaTeX-generate-current-completion-table)
yuuji@287 453 YaTeX-generate-variables-for-learning)))))
yuuji@287 454
yuuji@23 455 (defun YaTeX-generate-create-read-file-name (&optional nth)
yuuji@23 456 (concat
yuuji@46 457 "(read-file-name \""
yuuji@23 458 (read-string (if nth (format "Prompt for argument#%d: " nth) "Prompt: "))
yuuji@287 459 ": \" "" \"\" t \"\")\n"))
yuuji@287 460
yuuji@23 461 (defun YaTeX-generate-create-read-oneof (&optional nth readpos)
yuuji@23 462 (concat
yuuji@23 463 (if readpos
yuuji@46 464 "(YaTeX:read-position \""
yuuji@46 465 "(YaTeX:read-oneof \"")
yuuji@287 466 (read-string "Acceptable characters: " "lcr") "\")\n"))
yuuji@287 467
yuuji@23 468 (defun YaTeX-generate-option-type (command)
yuuji@23 469 (let ((func (format "YaTeX:%s" command)) leftp
yuuji@23 470 (buf (get-buffer-create YaTeX-generate-buffer)) type (n 1))
yuuji@23 471 (set-buffer buf)
yuuji@23 472 (erase-buffer)
yuuji@23 473 (insert "(defun " func " ()\n (concat\n")
yuuji@23 474 (catch 'done
yuuji@23 475 (while t
yuuji@23 476 (setq type (YaTeX-generate-read-completion-type n))
yuuji@23 477 (insert
yuuji@23 478 (cond
yuuji@23 479 ;;Read string
yuuji@23 480 ((eq type 'string)
yuuji@52 481 (concat "\"" (setq leftp (read-string "Left parenthesis: " "("))
yuuji@52 482 "\"\n"
yuuji@23 483 (YaTeX-generate-create-read-string)
yuuji@52 484 "\"" (YaTeX-generate-corresponding-paren leftp) "\"")
yuuji@52 485 )
yuuji@23 486 ;;Completing-read
yuuji@23 487 ((eq type 'completion)
yuuji@52 488 (concat "\"" (setq leftp (read-string "Left parenthesis: " "{"))
yuuji@52 489 "\"\n"
yuuji@23 490 (YaTeX-generate-create-completing-read)
yuuji@52 491 "\"" (YaTeX-generate-corresponding-paren leftp) "\"")
yuuji@23 492 )
yuuji@23 493 ((eq type 'file)
yuuji@52 494 (concat "\"" (setq leftp (read-string "Left parenthesis: " "("))
yuuji@52 495 "\"\n"
yuuji@23 496 (YaTeX-generate-create-read-file-name)
yuuji@52 497 "\"" (YaTeX-generate-corresponding-paren leftp) "\"")
yuuji@23 498 )
yuuji@23 499 ((eq type 'oneof)
yuuji@23 500 (YaTeX-generate-create-read-oneof nil t)
yuuji@23 501 )
yuuji@23 502 ((eq type 'option)
yuuji@46 503 (concat "(let ((op (read-string \""
yuuji@23 504 (read-string "Prompt: ")
yuuji@46 505 ": \")))\n"
yuuji@46 506 "(if (string< \"\" op)\n"
yuuji@46 507 " (concat \"[\" op \"]\")\n"
yuuji@46 508 " \"\"))\n")
yuuji@23 509 )
yuuji@23 510 ((eq type 'coord)
yuuji@46 511 (concat "(YaTeX:read-coordinates \""
yuuji@23 512 (read-string "Prompt for coordinates: ")
yuuji@46 513 ": \")\n")
yuuji@23 514 )
yuuji@23 515 ((eq type 'macro)
yuuji@23 516 (error "not yet supported")
yuuji@23 517 )
yuuji@23 518 (t (throw 'done t))))
yuuji@23 519 (setq n (1+ n))))
yuuji@23 520 (insert "))\n") ;close defun
yuuji@23 521 (goto-char (point-min))
yuuji@23 522 (while (not (eobp)) (lisp-indent-line) (forward-line 1))
yuuji@23 523 (eval-current-buffer)
yuuji@287 524 buf))
yuuji@287 525
yuuji@23 526 (defun YaTeX-generate-argument-type (command argc)
yuuji@23 527 "Create an argument-type add-in function."
yuuji@23 528 (interactive)
yuuji@23 529 (let ((func (format "YaTeX::%s" command)) (argp 1)
yuuji@23 530 (cb (current-buffer))
yuuji@23 531 (buf (get-buffer-create YaTeX-generate-buffer)))
yuuji@23 532 (set-buffer buf)
yuuji@23 533 (erase-buffer)
yuuji@23 534 (insert "(defun " func " (&optional argp)\n(cond\n")
yuuji@23 535 (while (<= argp argc)
yuuji@23 536 (insert (format "((equal argp %d)\n" argp))
yuuji@23 537 (setq type (YaTeX-generate-read-completion-type argp))
yuuji@23 538 (insert
yuuji@23 539 (cond
yuuji@23 540 ((eq type 'string)
yuuji@23 541 (concat (YaTeX-generate-create-read-string argp)))
yuuji@23 542 ((eq type 'completion)
yuuji@23 543 (concat (YaTeX-generate-create-completing-read argp)))
yuuji@23 544 ((eq type 'oneof)
yuuji@23 545 (YaTeX-generate-create-read-oneof))
yuuji@23 546 ((eq type 'file)
yuuji@23 547 (concat (YaTeX-generate-create-read-file-name argp)))
yuuji@23 548 (t ""))
yuuji@23 549 ")\n")
yuuji@23 550 (setq argp (1+ argp)))
yuuji@23 551 (insert "))\n")
yuuji@23 552 (goto-char (point-min))
yuuji@23 553 (while (not (eobp)) (lisp-indent-line) (forward-line 1))
yuuji@23 554 (eval-current-buffer)
yuuji@23 555 (set-buffer cb)
yuuji@23 556 (YaTeX-update-table
yuuji@23 557 (if (> argc 1) (list command argc) (list command))
yuuji@23 558 'section-table 'user-section-table 'tmp-section-table)
yuuji@287 559 buf))
yuuji@287 560
yuuji@23 561 (defun YaTeX-generate-simple (&optional command)
yuuji@23 562 "Simple but requiring some elisp knowledge add-in generator."
yuuji@23 563 (interactive)
yuuji@52 564 (setq YaTeX-generate-variables-for-learning nil)
yuuji@23 565 (or command
yuuji@23 566 (setq command
yuuji@23 567 (completing-read
yuuji@72 568 (format
yuuji@72 569 "Making add-in function for (default %s): " YaTeX-section-name)
yuuji@23 570 (append
yuuji@23 571 section-table user-section-table tmp-section-table
yuuji@23 572 env-table user-env-table tmp-env-table
yuuji@23 573 singlecmd-table user-singlecmd-table tmp-singlecmd-table)
yuuji@23 574 nil nil)
yuuji@72 575 command (if (string= "" command) YaTeX-section-name command)))
yuuji@49 576 (message
yuuji@49 577 (cond
yuuji@49 578 (YaTeX-japan "(o)追加型? (a)引数型? (yatexadd.docを参照のこと) :")
yuuji@49 579 (t "(O)ption? (A)rgument?")))
yuuji@23 580 (YaTeX-showup-buffer
yuuji@23 581 (if (= (read-char) ?o)
yuuji@23 582 (YaTeX-generate-option-type command)
yuuji@23 583 (YaTeX-generate-argument-type
yuuji@23 584 command
yuuji@451 585 (YaTeX-str2int (read-string "How many arguments?: ")))) nil))
yuuji@287 586
yuuji@70 587 (provide 'yatexgen)