yuuji@287: ;;; yatexgen.el --- YaTeX add-in function generator(rev.5) yuuji@287: yuuji@451: ;;; (c)1991-1995,1999,2000,2017 by HIROSE Yuuji.[yuuji@yatex.org] yuuji@353: ;;; Last modified Sun Dec 21 14:04:49 2014 on firestorm yuuji@366: ;;; $Id$ yuuji@9: yuuji@287: ;;; Code: yuuji@9: (require 'yatex) yuuji@9: yuuji@9: (defmacro YaTeX-setq (var japanese english) yuuji@9: (list 'setq var yuuji@287: (if YaTeX-japan japanese english))) yuuji@9: yuuji@9: (put 'YaTeX-setq 'lisp-indent-hook 1) yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-initial-message yuuji@9: " 自動生成モードへようこそ!! yuuji@9: yuuji@9: 初めてやる人はこのバッファの例にしたがって指示通りにやって練習してね。 yuuji@9: 本番の時もこのバッファに出るメッセージを *よく読んで* 操作しないとう yuuji@9: まく関数が作れないよ!! yuuji@9: yuuji@9: ではリターンキーを押して下さい。" yuuji@9: " Welcome to auto-generation mode!! yuuji@9: yuuji@9: If this is your first trial, exercise this according to example and yuuji@9: following my messages. Then, at making actual function, operate yuuji@9: reading my messages *carefully*, or you'll fail to generate appropriate yuuji@9: function. yuuji@9: yuuji@287: Hit return key!") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-start-message yuuji@9: "さぁはじめるよ.\n1.登録したい補完をやってみて. yuuji@9: たとえば section 型補完の \\documentstyle だったら \\documentstyle{} yuuji@9: だけをいれてみてね. ちゃんと『〜型補完』を使わないとダメよ!。 yuuji@9: で、おわったらりたーん!!" yuuji@9: "Let's begin completion for which you want to make add-in function. yuuji@9: If you want to make add-in function for \\documentstyle input only yuuji@9: `\\documentstyle{}' *with* completion of yatex-mode. yuuji@287: If you finish this, please press RET.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-abort-message yuuji@9: "やめた、やめた〜いめんどくせ〜" yuuji@287: "Aborted.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-same-message yuuji@9: "それじゃ、なにも変わってねぇだろーが! やめた。" yuuji@287: "I found no difference between them. So I'm quitting.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-invalid-message yuuji@9: "それは、ちと無理というものじゃ." yuuji@287: "It's impossible.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-idontknow-message yuuji@9: "う〜ん、難しくてよくわからないなぁ。ばかでごめんねェ〜" yuuji@287: "Sorry I can't tell your adding method.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-confirm-message yuuji@9: "ということは、付け足したい部分はこれでいいのね" yuuji@287: "Is it additional string of add-in function?") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-output-message yuuji@9: "2.じゃ、それにくっつけたいものを *カーソルの位置に* 足してみて. yuuji@9: さっきの \\documentstyle{} の例だと \\documentstyle[12pt]{} とかにするの。 yuuji@9: しつこいようだけど、今の位置からカーソル動かしちゃダメよ!! yuuji@9: で、またおわったらりたーん!!" yuuji@9: "2.Then input additional string *at CURSOR POSITION* yuuji@9: According to last example \\documentstyle{}, yuuji@287: modify it \\documentstyle[12pt]{}. RET to finish.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-put-arg-message yuuji@9: "3.このうち、キーボードから読み込んで欲しい文字列を順に入れて。 yuuji@9: さっきの \\documentstyle[12pt]{} だったら、付加する文字は[12pt]だけど yuuji@9: 手で入れたいのは 12pt の部分だけですね。 yuuji@9: で、全部入れ終ったら、りたーんだけうってね!!" yuuji@9: "3.In this string, extract string which you want to input from key yuuji@9: board with quiry afterwards. For example, though additional string is yuuji@9: \\documentstyle[12pt]{}, but you want enter only `12pt' by hand. yuuji@287: RET to finish!") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-read-prompt-message yuuji@9: "4.では、あとでこれらの文字列を読み込む時に、どういうプロンプトを yuuji@9: 出したいですか? 順に入れて下さい。面倒なら単にリターンを打ってね。 yuuji@9: さっきの 12pt の部分だったら、『サイズは』とかがおすすめ。" yuuji@9: "4.When you use this add-in function afterwards, what message yuuji@9: do you like to be prompted with to enter these values. In last yuuji@287: example `12pt', typical prompt string may be `Size?: '.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-done-message yuuji@9: "よし! これが、君の作りたかった関数だ。~/.emacs にでも入れてせいぜい yuuji@9: 楽してくれ。このバッファ(*ご案内*)を yatex-mode にしておくから yuuji@9: できた関数が本当にお望みの動作をするか確かめてみるといいかもね。 yuuji@9: ところで、この関数こんなに簡単だろう? そろそろ自分で書いたらどう? yuuji@9: " yuuji@9: "OK! This is the definition of function you want to make! Add yuuji@9: this description to your ~/.emacs or so. Use this buffer(*Guide*) yuuji@9: for testing of this function please. yuuji@9: But you can see this function quite easy, can't you? You had better yuuji@9: write your most favorite add-in function yourself! yuuji@287: ") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-nomatch-message yuuji@9: "こらこら、そんな文字列どこにもねーぞ!!" yuuji@287: "No such string in additional string.") yuuji@287: yuuji@9: (YaTeX-setq YaTeX-generate-buffer yuuji@9: "*付加関数生成バッファ*" yuuji@287: "*Generate-add-in-function*") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-message-buffer yuuji@9: "*ご案内*" yuuji@287: "*Guide*") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-bug-message yuuji@9: "ごめ〜ん!! ちょっと、このアドイン関数つくるの失敗しちゃったみたい!! yuuji@9: 作者まで連絡してくださ〜〜〜い!" yuuji@9: "Sorry I failed to make add-in function for you... yuuji@287: Send bug report to me.") yuuji@9: yuuji@9: (YaTeX-setq YaTeX-generate-narrow-message yuuji@9: "画面がせますぎるような気がします。" yuuji@287: "Too narrow screen height.") yuuji@9: yuuji@9: (defvar YaTeX-generate-message-height yuuji@9: 10 "Window height of YaTeX-generate-message-buffer") yuuji@9: yuuji@52: ;; Do you need learning for generated function? yuuji@70: ;; If you need, please tell me (yuuji@yatex.org) yuuji@52: ;;(defvar YaTeX-generate-variables-for-learning nil) yuuji@52: ;;(defvar YaTeX-generate-current-completion-table nil) yuuji@52: yuuji@9: ;;; yuuji@9: ;Generate mode. yuuji@9: ;;; yuuji@9: (defun YaTeX-generate () yuuji@9: "Genarate YaTeX add-in function with enquiry." yuuji@9: (interactive) yuuji@70: (if (< (YaTeX-screen-height) (+ YaTeX-generate-message-height 10)) yuuji@9: (error YaTeX-generate-narrow-message)) yuuji@9: (put 'YaTeX-generate 'disabled t) yuuji@9: (save-window-excursion yuuji@9: (unwind-protect yuuji@9: (let (input output (i 0) (beg 0) end add-in map map1 si str slist yuuji@9: (from (make-marker)) (to (make-marker))) yuuji@9: (delete-other-windows) yuuji@9: (switch-to-buffer YaTeX-generate-message-buffer) yuuji@9: (yatex-mode) yuuji@9: (erase-buffer) yuuji@9: (insert YaTeX-generate-initial-message) yuuji@9: (read-string yuuji@9: (if YaTeX-japan "リターンキーを押して下さい." "Press RETURN.")) yuuji@9: (erase-buffer) yuuji@9: (insert YaTeX-generate-start-message) yuuji@9: (pop-to-buffer (get-buffer-create YaTeX-generate-buffer)) yuuji@9: (enlarge-window (- (window-height) YaTeX-generate-message-height 1)) yuuji@9: (erase-buffer) yuuji@9: (yatex-mode) yuuji@9: (use-local-map (setq map (copy-keymap YaTeX-mode-map))) yuuji@9: (define-key (current-local-map) "\n" 'exit-recursive-edit) yuuji@9: (define-key (current-local-map) "\r" 'exit-recursive-edit) yuuji@9: (define-key (current-local-map) "\C-g" 'abort-recursive-edit) yuuji@9: (setq map1 (copy-keymap map)) yuuji@9: (YaTeX-suppress-sparse-keymap map) yuuji@9: ;;First get input form. yuuji@9: (recursive-edit) yuuji@72: (setq input (YaTeX-minibuffer-string) yuuji@9: end (1- (length input))) yuuji@9: (if (string= "" input) (error YaTeX-generate-abort-message)) yuuji@9: (YaTeX-generate-move-to-add-in-position) yuuji@9: (set-marker from (1- (point))) ;;Can't write before `from' yuuji@9: (set-marker to (1+ (point))) ;;Can't write after `to' yuuji@9: ;;Second get output form. yuuji@9: (setq beg (1- (point)));;Cheat begin point! yuuji@9: (YaTeX-generate-display-message YaTeX-generate-output-message) yuuji@9: (use-local-map map1) yuuji@9: (fset 'si (symbol-function 'self-insert-command)) yuuji@9: (defun self-insert-command (arg) yuuji@9: (interactive "p") yuuji@9: (if (or (not (equal (buffer-name) YaTeX-generate-buffer)) yuuji@9: (and (> (point) (marker-position from)) yuuji@9: (< (point) (marker-position to)))) yuuji@9: (insert (this-command-keys)) (ding))) yuuji@9: (unwind-protect yuuji@9: (recursive-edit) yuuji@9: (fset 'self-insert-command (symbol-function 'si))) yuuji@72: (setq output (YaTeX-minibuffer-string)) yuuji@9: (cond ((string= "" output) (error YaTeX-generate-abort-message)) yuuji@9: ((string= input output) (error YaTeX-generate-same-message)) yuuji@9: ((< (length output) (length input)) yuuji@9: (error YaTeX-generate-invalid-message))) yuuji@9: ;;(while (and (< beg end) (= (aref input beg) (aref output i))) yuuji@9: ;; (setq beg (1+ beg) i (1+ i))) ;;for universal use. yuuji@9: (setq i (1- (length output))) yuuji@9: (while (and (>= end beg) (= (aref output i) (aref input end))) yuuji@9: (setq end (1- end) i (1- i))) yuuji@9: (setq add-in (substring output beg yuuji@9: (if (= i (1- (length output))) nil (1+ i)))) yuuji@9: (erase-buffer) yuuji@9: (insert add-in) yuuji@9: (if (not (y-or-n-p YaTeX-generate-confirm-message)) yuuji@9: (error YaTeX-generate-idontknow-message)) yuuji@9: ;;Extract arguments. yuuji@9: (YaTeX-generate-display-message YaTeX-generate-put-arg-message) yuuji@9: (setq i 1) yuuji@9: (while (not (string= yuuji@9: "" (setq str (read-string (format "Arg %d: " i))))) yuuji@9: (if (not (string-match (regexp-quote str) add-in)) yuuji@9: (progn yuuji@9: (ding) yuuji@9: (YaTeX-generate-display-message yuuji@9: YaTeX-generate-nomatch-message -1)) yuuji@9: (setq slist (append slist (list (list str))) i (1+ i))) yuuji@9: );input all of arguments. yuuji@9: ;;Compare with output string. yuuji@9: (set-buffer YaTeX-generate-buffer) ;;for safety yuuji@9: (emacs-lisp-mode) yuuji@9: (if (> i 1) yuuji@9: (YaTeX-generate-parse-add-in slist add-in) yuuji@9: (erase-buffer) yuuji@9: (insert "(defun " (YaTeX-generate-function-name) " ()\n") yuuji@9: (insert "\"" (YaTeX-generate-lisp-quote add-in) "\")\n") yuuji@9: (indent-region (point-min) (point-max) nil) yuuji@9: (message (if YaTeX-japan yuuji@9: "このくらいの関数手で書け!!" yuuji@9: "You don't need me to make such easy function."))) yuuji@9: );let yuuji@9: (put 'YaTeX-generate 'disabled nil) yuuji@9: (put 'YaTeX-addin 'disabled nil) yuuji@9: )) yuuji@9: (YaTeX-generate-display-message YaTeX-generate-done-message) yuuji@9: (switch-to-buffer YaTeX-generate-buffer) yuuji@9: (condition-case error yuuji@9: (eval-current-buffer) yuuji@9: (error (insert YaTeX-generate-bug-message))) yuuji@287: (pop-to-buffer YaTeX-generate-message-buffer)) yuuji@9: yuuji@9: (defun YaTeX-generate-parse-add-in (args add-in) yuuji@9: "Parse add-in string and extract argument for it. yuuji@9: Variable add-in is referred in parent function." yuuji@9: (let ((i 1) j (case-fold-search nil) ;i holds argument number yuuji@9: (prompt (make-vector (length args) "")) yuuji@9: (used (make-vector (length add-in) nil)) yuuji@9: func-name (string "")) yuuji@9: ;;Phase 1. extract argument from add-in string. yuuji@9: (mapcar yuuji@353: (function yuuji@353: (lambda (arg) yuuji@9: (let ((index 0) (match 0) beg end (carg (car arg))) yuuji@9: (YaTeX-generate-display-message yuuji@9: YaTeX-generate-read-prompt-message) yuuji@9: (aset prompt (1- i) yuuji@9: (read-string yuuji@9: (format yuuji@9: (if YaTeX-japan "%d番目(%s)を読む時?: " yuuji@9: "When reading argument #%d(%s)?: ") i (car arg)))) yuuji@9: (while (string-match (regexp-quote carg) (substring add-in index)) yuuji@9: (setq beg (+ index (match-beginning 0)) yuuji@9: end (+ index (match-end 0))) yuuji@9: (if (aref used beg) nil yuuji@9: (setq match (1+ match)) yuuji@9: (cond yuuji@9: ((= match 1) yuuji@9: ;;(setq arg (append arg (list (list beg end)))) yuuji@9: (YaTeX-generate-register-match)) yuuji@9: ((YaTeX-generate-ask-match-position) yuuji@9: (YaTeX-generate-register-match)))) yuuji@9: (setq index end)) yuuji@353: (setq i (1+ i))))) yuuji@9: args) yuuji@9: ;;Phase 2. Generate function!! yuuji@9: (setq i 0) yuuji@9: (setq func-name (YaTeX-generate-function-name)) yuuji@9: (while (< i (length add-in)) yuuji@9: (setq beg i j (aref used i)) yuuji@9: (while (and (< i (length add-in)) (equal j (aref used i))) yuuji@9: (setq i (1+ i))) yuuji@9: (if j ;If it is argument. yuuji@9: (setq string (concat string (format " arg%d" j))) yuuji@9: (setq string yuuji@9: (concat string " \"" yuuji@9: (YaTeX-generate-quote-quote (substring add-in beg i)) yuuji@9: "\"")) yuuji@9: )) yuuji@9: (erase-buffer) yuuji@9: (setq i 1) yuuji@9: (insert yuuji@9: "(defun " func-name " ()\n" yuuji@9: " (let (") yuuji@9: (mapcar yuuji@353: (function yuuji@353: (lambda (arg) yuuji@9: (insert (format "(arg%d (read-string \"%s: \"))\n" yuuji@9: i (aref prompt (1- i)))) yuuji@353: (setq i (1+ i)))) yuuji@9: args) yuuji@9: (delete-region (point) (progn (forward-line -1) (end-of-line) (point))) yuuji@9: (insert ")\n(concat " (YaTeX-generate-lisp-quote string) yuuji@9: ")))\n") yuuji@9: (indent-region (point-min) (point) nil) yuuji@287: used)) yuuji@9: yuuji@9: (defun YaTeX-generate-ask-match-position () yuuji@9: "Ask user whether match-position is in his expectation, yuuji@9: Referencing variables in parent function YaTeX-generate-parse-add-in." yuuji@9: (pop-to-buffer YaTeX-generate-message-buffer) yuuji@9: (goto-char (point-max)) yuuji@9: (insert "\n\n" yuuji@9: (format (if YaTeX-japan "%d 番目の引数 %s って" yuuji@9: "Is argument #%d's value `%s' also corresponding to") yuuji@9: i carg) "\n" add-in "\n") yuuji@9: (indent-to-column beg) yuuji@9: (let ((c beg)) yuuji@9: (while (< c end) (insert "^") (setq c (1+ c)))) yuuji@9: (insert "\n" (if YaTeX-japan "ここにも対応してるの?" yuuji@9: "this underlined part too?")) yuuji@9: (other-window -1) yuuji@287: (y-or-n-p (if YaTeX-japan "下線部はあってますか" "Is underline right"))) yuuji@9: yuuji@9: (defun YaTeX-generate-register-match () yuuji@9: (nconc arg (list (list beg end))) yuuji@9: (let ((x beg)) yuuji@287: (while (< x end) (aset used x i)(setq x (1+ x))))) yuuji@9: yuuji@9: (defun YaTeX-generate-display-message (mes &optional bottom) yuuji@9: "Display message to generation buffer." yuuji@9: (pop-to-buffer YaTeX-generate-message-buffer) yuuji@9: (goto-char (point-max)) yuuji@9: (insert "\n\n") yuuji@9: (if bottom (recenter (1- bottom)) (recenter 0)) yuuji@9: (insert mes) yuuji@287: (other-window -1)) yuuji@9: yuuji@9: (defun YaTeX-generate-move-to-add-in-position () yuuji@9: "Move cursor where add-in function should insert string." yuuji@9: (cond yuuji@9: ((eq YaTeX-current-completion-type 'begin) yuuji@9: (goto-char (point-min)) yuuji@9: (skip-chars-forward "^{") yuuji@72: (setq YaTeX-env-name yuuji@9: (buffer-substring (1+ (point)) yuuji@9: (progn (skip-chars-forward "^}") (point)))) yuuji@9: (forward-char 1)) yuuji@9: ((eq YaTeX-current-completion-type 'section) yuuji@9: (goto-char (point-min)) yuuji@9: (skip-chars-forward "^{")) yuuji@9: ((eq YaTeX-current-completion-type 'maketitle) yuuji@9: (goto-char (point-max)) yuuji@9: (if (= (preceding-char) ? ) yuuji@287: (forward-char -1))))) yuuji@9: yuuji@9: (defun YaTeX-generate-function-name () yuuji@9: (concat yuuji@9: "YaTeX:" yuuji@9: (cond yuuji@72: ((eq YaTeX-current-completion-type 'begin) YaTeX-env-name) yuuji@72: ((eq YaTeX-current-completion-type 'section) YaTeX-section-name) yuuji@287: ((eq YaTeX-current-completion-type 'maketitle) YaTeX-single-command)))) yuuji@9: yuuji@9: (defun YaTeX-generate-lisp-quote (str) yuuji@9: (let ((len (length str))(i 0) (quote "")) yuuji@9: (while (< i len) yuuji@9: (if (= (aref str i) ?\\) yuuji@9: (setq quote (concat quote "\\"))) yuuji@9: (if (= (aref str i) 127) yuuji@9: (setq quote (concat quote "\"")) yuuji@9: (setq quote (concat quote (substring str i (1+ i))))) yuuji@9: (setq i (1+ i))) yuuji@287: quote)) yuuji@9: yuuji@9: (defun YaTeX-generate-quote-quote (str) yuuji@9: (let ((len (length str))(i 0) (quote "")) yuuji@9: (while (< i len) yuuji@9: (if (= (aref str i) ?\") yuuji@9: (setq quote (concat quote (char-to-string 127)))) yuuji@9: (setq quote (concat quote (substring str i (1+ i)))) yuuji@9: (setq i (1+ i))) yuuji@287: quote)) yuuji@9: yuuji@9: (defun YaTeX-suppress-sparse-keymap (map) yuuji@9: (let ((i ? )) yuuji@9: (while (< i 127) yuuji@9: (define-key map (char-to-string i) 'undefined) yuuji@287: (setq i (1+ i))))) yuuji@23: yuuji@23: ;;; yuuji@23: ;; Auto-generate Function for Lispers. yuuji@23: ;;; yuuji@23: (defun YaTeX-generate-read-completion-type (nth) yuuji@23: (message yuuji@23: "Read type(%d): (S)tring (C)omplete (F)ile ([)option (P)osition co(O)rd. (q)uit" nth) yuuji@23: (let ((c (read-char))) yuuji@23: (cond yuuji@23: ((= c ?s) 'string) yuuji@23: ((= c ?c) 'completion) yuuji@23: ((= c ?f) 'file) yuuji@23: ((= c ?\[) 'option) yuuji@23: ((= c ?p) 'oneof) yuuji@23: ((= c ?o) 'coord) yuuji@23: ;;((= c ?m) 'macro) yuuji@287: (t 'quit)))) yuuji@287: yuuji@23: (defun YaTeX-generate-read-completion-table () yuuji@23: (let ((i 1) cand (cands "(") (cb (current-buffer)) yuuji@23: (buf (get-buffer-create " *Candidates*"))) yuuji@23: (save-window-excursion yuuji@23: (save-excursion yuuji@23: (YaTeX-showup-buffer buf nil) yuuji@23: (set-buffer buf) yuuji@23: (erase-buffer) yuuji@23: (while (string< yuuji@23: "" yuuji@23: (setq cand (read-string (format "Item[%d](RET to exit): " i)))) yuuji@46: (setq cands (concat cands (format "(\"%s\")\n" cand)) yuuji@23: i (1+ i)) yuuji@23: (insert cand "\n")) yuuji@23: (kill-buffer buf))) yuuji@23: ;;(set-buffer cb) yuuji@287: (setq YaTeX-generate-current-completion-table (concat cands ")")))) yuuji@287: yuuji@23: (defun YaTeX-generate-corresponding-paren (left) yuuji@23: (cond yuuji@23: ((equal left "{") "}") yuuji@23: ((equal left "[") "]") yuuji@23: ((equal left "(") ")") yuuji@23: ((equal left "<") ">") yuuji@59: ((equal left "\\begin{") "}") yuuji@287: (t left))) yuuji@287: yuuji@23: (defun YaTeX-generate-create-read-string (&optional nth) yuuji@23: (concat yuuji@46: "(read-string \"" yuuji@23: (read-string (if nth (format "Prompt for argument#%d: " nth) "Prompt: ")) yuuji@46: ": \"\n" yuuji@46: "\"" (read-string "Default: ") "\"" yuuji@287: ")\n")) yuuji@287: yuuji@23: (defun YaTeX-generate-create-completing-read (&optional nth) yuuji@52: (prog1 yuuji@52: (concat yuuji@52: "(completing-read \"" yuuji@52: (read-string yuuji@52: (if nth (format "Prompt for argument#%d: " nth) "Prompt: ")) yuuji@52: ": \"\n" yuuji@52: (format "'%s\n" (YaTeX-generate-read-completion-table)) yuuji@52: "nil " yuuji@52: (format "%s)" (y-or-n-p "Require match? "))) yuuji@52: (if nil ;;;(y-or-n-p "Do you need learning for this completion?") yuuji@52: (setq YaTeX-generate-variables-for-learning yuuji@52: (cons yuuji@52: (cons (format "YaTeX-%s-%d" command (or nth 0)) yuuji@52: YaTeX-generate-current-completion-table) yuuji@287: YaTeX-generate-variables-for-learning))))) yuuji@287: yuuji@23: (defun YaTeX-generate-create-read-file-name (&optional nth) yuuji@23: (concat yuuji@46: "(read-file-name \"" yuuji@23: (read-string (if nth (format "Prompt for argument#%d: " nth) "Prompt: ")) yuuji@287: ": \" "" \"\" t \"\")\n")) yuuji@287: yuuji@23: (defun YaTeX-generate-create-read-oneof (&optional nth readpos) yuuji@23: (concat yuuji@23: (if readpos yuuji@46: "(YaTeX:read-position \"" yuuji@46: "(YaTeX:read-oneof \"") yuuji@287: (read-string "Acceptable characters: " "lcr") "\")\n")) yuuji@287: yuuji@23: (defun YaTeX-generate-option-type (command) yuuji@23: (let ((func (format "YaTeX:%s" command)) leftp yuuji@23: (buf (get-buffer-create YaTeX-generate-buffer)) type (n 1)) yuuji@23: (set-buffer buf) yuuji@23: (erase-buffer) yuuji@23: (insert "(defun " func " ()\n (concat\n") yuuji@23: (catch 'done yuuji@23: (while t yuuji@23: (setq type (YaTeX-generate-read-completion-type n)) yuuji@23: (insert yuuji@23: (cond yuuji@23: ;;Read string yuuji@23: ((eq type 'string) yuuji@52: (concat "\"" (setq leftp (read-string "Left parenthesis: " "(")) yuuji@52: "\"\n" yuuji@23: (YaTeX-generate-create-read-string) yuuji@52: "\"" (YaTeX-generate-corresponding-paren leftp) "\"") yuuji@52: ) yuuji@23: ;;Completing-read yuuji@23: ((eq type 'completion) yuuji@52: (concat "\"" (setq leftp (read-string "Left parenthesis: " "{")) yuuji@52: "\"\n" yuuji@23: (YaTeX-generate-create-completing-read) yuuji@52: "\"" (YaTeX-generate-corresponding-paren leftp) "\"") yuuji@23: ) yuuji@23: ((eq type 'file) yuuji@52: (concat "\"" (setq leftp (read-string "Left parenthesis: " "(")) yuuji@52: "\"\n" yuuji@23: (YaTeX-generate-create-read-file-name) yuuji@52: "\"" (YaTeX-generate-corresponding-paren leftp) "\"") yuuji@23: ) yuuji@23: ((eq type 'oneof) yuuji@23: (YaTeX-generate-create-read-oneof nil t) yuuji@23: ) yuuji@23: ((eq type 'option) yuuji@46: (concat "(let ((op (read-string \"" yuuji@23: (read-string "Prompt: ") yuuji@46: ": \")))\n" yuuji@46: "(if (string< \"\" op)\n" yuuji@46: " (concat \"[\" op \"]\")\n" yuuji@46: " \"\"))\n") yuuji@23: ) yuuji@23: ((eq type 'coord) yuuji@46: (concat "(YaTeX:read-coordinates \"" yuuji@23: (read-string "Prompt for coordinates: ") yuuji@46: ": \")\n") yuuji@23: ) yuuji@23: ((eq type 'macro) yuuji@23: (error "not yet supported") yuuji@23: ) yuuji@23: (t (throw 'done t)))) yuuji@23: (setq n (1+ n)))) yuuji@23: (insert "))\n") ;close defun yuuji@23: (goto-char (point-min)) yuuji@23: (while (not (eobp)) (lisp-indent-line) (forward-line 1)) yuuji@23: (eval-current-buffer) yuuji@287: buf)) yuuji@287: yuuji@23: (defun YaTeX-generate-argument-type (command argc) yuuji@23: "Create an argument-type add-in function." yuuji@23: (interactive) yuuji@23: (let ((func (format "YaTeX::%s" command)) (argp 1) yuuji@23: (cb (current-buffer)) yuuji@23: (buf (get-buffer-create YaTeX-generate-buffer))) yuuji@23: (set-buffer buf) yuuji@23: (erase-buffer) yuuji@23: (insert "(defun " func " (&optional argp)\n(cond\n") yuuji@23: (while (<= argp argc) yuuji@23: (insert (format "((equal argp %d)\n" argp)) yuuji@23: (setq type (YaTeX-generate-read-completion-type argp)) yuuji@23: (insert yuuji@23: (cond yuuji@23: ((eq type 'string) yuuji@23: (concat (YaTeX-generate-create-read-string argp))) yuuji@23: ((eq type 'completion) yuuji@23: (concat (YaTeX-generate-create-completing-read argp))) yuuji@23: ((eq type 'oneof) yuuji@23: (YaTeX-generate-create-read-oneof)) yuuji@23: ((eq type 'file) yuuji@23: (concat (YaTeX-generate-create-read-file-name argp))) yuuji@23: (t "")) yuuji@23: ")\n") yuuji@23: (setq argp (1+ argp))) yuuji@23: (insert "))\n") yuuji@23: (goto-char (point-min)) yuuji@23: (while (not (eobp)) (lisp-indent-line) (forward-line 1)) yuuji@23: (eval-current-buffer) yuuji@23: (set-buffer cb) yuuji@23: (YaTeX-update-table yuuji@23: (if (> argc 1) (list command argc) (list command)) yuuji@23: 'section-table 'user-section-table 'tmp-section-table) yuuji@287: buf)) yuuji@287: yuuji@23: (defun YaTeX-generate-simple (&optional command) yuuji@23: "Simple but requiring some elisp knowledge add-in generator." yuuji@23: (interactive) yuuji@52: (setq YaTeX-generate-variables-for-learning nil) yuuji@23: (or command yuuji@23: (setq command yuuji@23: (completing-read yuuji@72: (format yuuji@72: "Making add-in function for (default %s): " YaTeX-section-name) yuuji@23: (append yuuji@23: section-table user-section-table tmp-section-table yuuji@23: env-table user-env-table tmp-env-table yuuji@23: singlecmd-table user-singlecmd-table tmp-singlecmd-table) yuuji@23: nil nil) yuuji@72: command (if (string= "" command) YaTeX-section-name command))) yuuji@49: (message yuuji@49: (cond yuuji@49: (YaTeX-japan "(o)追加型? (a)引数型? (yatexadd.docを参照のこと) :") yuuji@49: (t "(O)ption? (A)rgument?"))) yuuji@23: (YaTeX-showup-buffer yuuji@23: (if (= (read-char) ?o) yuuji@23: (YaTeX-generate-option-type command) yuuji@23: (YaTeX-generate-argument-type yuuji@23: command yuuji@451: (YaTeX-str2int (read-string "How many arguments?: ")))) nil)) yuuji@287: yuuji@70: (provide 'yatexgen)