yatex

view yatexpkg.el @ 303:13a62b7c8354

(yatexpkg)Add default option for package completion.
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 06 May 2013 17:24:56 +0900
parents 8d3156073892
children a57f4c74179a
line source
1 ;;; yatexpkg.el --- YaTeX package manager
2 ;;;
3 ;;; (c)2003-2013 by HIROSE, Yuuji [yuuji@yatex.org]
4 ;;; Last modified Mon May 6 17:20:43 2013 on firestorm
5 ;;; $Id$
7 ;;; Code:
8 (defvar YaTeX-package-ams-envs
9 (mapcar 'car YaTeX-ams-env-table))
11 (defvar YaTeX-package-alist-default
12 '(("version" (env "comment") ;by tsuchiya<at>pine.kuee.kyoto-u.ac.jp
13 (section "includeversion" "excludeversion"))
15 ("plext" (section "bou")) ;by yas.axis<at>ma.mni.ne.jp
17 ("url" (section "url")) ;by fujieda<at>jaist.ac.jp
19 ("fancybox" (section "shadowbox" "doublebox" "ovalbox" "Ovalbox"))
20 ("slashbox" (section "slashbox" "backslashbox"))
21 ("pifont" (section "ding"))
22 ("longtable" (env "longtable"))
23 ("ascmac" (env "screen" "boxnote" "shadebox" "itembox")
24 (maketitle "return" "Return" "yen")
25 (section "keytop") ("mask") ("maskbox"))
26 ("bm" (section "bm")) ;by aoyama<at>le.chiba-u.ac.jp
28 ("alltt" (env "alltt"))
29 ("misc" (section "verbfile" "listing"))
30 ("eclbkbox" (env "breakbox"))
31 ("supertabular" (env "supertabular"))
32 ("amsmath" (env . YaTeX-package-ams-envs)
33 (section "tag" "tag*"))
34 ("amssymb" (maketitle "leqq" "geqq" "mathbb" "mathfrak"
35 "fallingdotseq"
36 "lll" "ggg")) ;very few. Please tell us!
37 ("graphicx" (section "includegraphics"
38 "rotatebox" "scalebox" "resizebox" "reflectbox")
39 (option . YaTeX-package-graphics-driver-alist))
40 ("color" (section "textcolor" "colorbox" "pagecolor" "color")
41 (option . YaTeX-package-graphics-driver-alist)
42 (default-option . "usenames,dvipsnames"))
43 ("xcolor" (same-as . "color"))
44 ("ulem" (section "uline" "uuline" "uwave")
45 (option ("normalem")))
46 ("multicol" (env "multicols")))
47 "Default package vs. macro list.
48 Alists contains '(PACKAGENAME . MACROLIST)
49 PACKAGENAME Basename of package(String).
50 MACROLIST List of '(TYPE . MACROS)
51 TYPE One of 'env, 'section or 'maketitle according to completion-type
52 MACROS List of macros
54 If TYPE is 'option, its cdr is alist of completion candidates for that
55 package. Its cdr can be a symbol whose value is alist.
57 An good example is the value of YaTeX-package-alist-default.")
59 (defvar YaTeX-package-graphics-driver-alist
60 '(("dvips") ("dvipsnames") ("usenames")
61 ("xdvi") ("dvipdf") ("pdftex") ("dvipsone") ("dviwindo")
62 ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin")
63 ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex"))
64 "Drivers alist of graphics/color stylefile's supporting deveces.
65 This list is taken from
66 %% graphics.dtx Copyright (C) 1994 David Carlisle Sebastian Rahtz
67 %% Copyright (C) 1995 1996 1997 1998 David Carlisle
68 as of 2004/1/19. Thanks.")
70 (defvar YaTeX-package-alist-private nil
71 "*User defined package vs. macro list. See also YaTeX-package-alist-default")
73 (defun YaTeX-package-lookup (macro &optional type)
74 "Look up a package which contains a definition of MACRO.
75 Optional second argument TYPE limits the macro type.
76 TYPE is a symbol, one of 'env, 'section, 'maketitle."
77 (let ((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
78 element x val pkg pkglist r)
79 (while list
80 (setq element (car list)
81 pkg (car element)
82 element (cdr element))
83 (if (setq r (catch 'found
84 (while element
85 (setq x (car element)
86 val (cdr x))
87 (if (symbolp val) (setq val (symbol-value val)))
88 (and (or (null type)
89 (eq type (car x)))
90 (YaTeX-member macro val)
91 (throw 'found (car x))) ;car x is type
92 (setq element (cdr element)))))
93 (setq pkglist (cons (cons pkg r) pkglist)))
94 (setq list (cdr list)))
95 pkglist))
97 (defun YaTeX-package-option-lookup (pkg &optional key)
98 "Look up options for specified pkg and returne them in alist form.
99 Just only associng against the alist of YaTeX-package-alist-*"
100 (let*((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
101 (l (cdr (assq (or key 'option) (assoc pkg list))))
102 (recur (cdr (assq 'same-as (assoc pkg list)))))
103 (cond
104 (recur (YaTeX-package-option-lookup recur key))
105 ((symbolp l) (symbol-value l))
106 (t l))))
108 (defvar YaTeX-package-resolved-list nil
109 "List of macros whose package is confirmed to be loaded.")
111 (defun YaTeX-package-auto-usepackage (macro type)
112 "(Semi)Automatically add the \\usepackage line to main-file.
113 Search the usepackage for MACRO of the TYPE."
114 (let ((cb (current-buffer))
115 (wc (current-window-configuration))
116 (usepackage (concat YaTeX-ec "usepackage"))
117 (pkglist (YaTeX-package-lookup macro type))
118 (usepkgrx (concat
119 YaTeX-ec-regexp
120 "\\(usepackage\\|include\\)\\b"))
121 (register '(lambda () (set-buffer cb)
122 (set (make-local-variable 'YaTeX-package-resolved-list)
123 (cons macro YaTeX-package-resolved-list))))
124 (begdoc (concat YaTeX-ec "begin{document}"))
125 pb pkg optlist (option "") mb0 uspkgargs)
126 (if (or (YaTeX-member macro YaTeX-package-resolved-list)
127 (null pkglist))
128 nil ;nothing to do
129 ;; Search `usepackage' into main-file
130 (YaTeX-visit-main t) ;set buffer to parent file
131 (setq pb (current-buffer))
132 (save-excursion
133 (save-restriction
134 (if (catch 'found
135 (goto-char (point-min))
136 (YaTeX-search-active-forward ;if search fails, goto eob
137 begdoc YaTeX-comment-prefix nil 1)
138 (while ;(YaTeX-re-search-active-backward
139 ;usepkgrx YaTeX-comment-prefix nil t)
140 ;;allow commented out \usepackages 2004/3/16
141 (re-search-backward usepkgrx nil t)
142 (setq mb0 (match-beginning 0))
143 (skip-chars-forward "^{")
144 (setq uspkgargs (YaTeX-buffer-substring
145 (point)
146 (progn
147 ;;(forward-list 1) is more precise,
148 ;; but higher risk.
149 (skip-chars-forward "^}\n")(point))))
150 (let ((pl pkglist))
151 (while pl ;(car pl)'s car is package, cdr is type
152 (if (string-match
153 (concat "[{,]\\s *"
154 (regexp-quote (car (car pl)))
155 "\\>")
156 uspkgargs)
157 (throw 'found t))
158 (setq pl (cdr pl)))
159 (goto-char mb0))))
160 ;;corresponding \usepackage found
161 (funcall register)
162 ;; not found, insert it.
163 (if (y-or-n-p
164 (format "`%s' requires package. Put \\usepackage now?" macro))
165 (progn
166 (require 'yatexadd)
167 (setq pkg
168 (completing-read
169 "Load which package?(TAB for list): "
170 pkglist nil nil
171 ;;initial input
172 (if (= (length pkglist) 1)
173 (let ((w (car (car pkglist))))
174 (if YaTeX-emacs-19 (cons w 0) w))))
175 optlist
176 (YaTeX-package-option-lookup pkg))
177 (if optlist
178 (let ((minibuffer-completion-table optlist)
179 (delim ",") (w (car (car optlist)))
180 (dflt (YaTeX-package-option-lookup
181 pkg 'default-option)))
182 (setq option
183 (read-from-minibuffer
184 (format "Any option for {%s}?: " pkg)
185 (let ((v (or dflt
186 (and (= (length optlist) 1) w))))
187 (and v (if YaTeX-emacs-19 (cons v 0) v)))
188 YaTeX-minibuffer-completion-map)
189 option (if (string< "" option)
190 (concat "[" option "]")
191 ""))))
192 (set-buffer pb)
193 (goto-char (point-min))
194 (if (YaTeX-re-search-active-forward
195 (concat YaTeX-ec-regexp
196 "document\\(style\\|class\\){")
197 YaTeX-comment-prefix nil t)
198 (forward-line 1))
199 (if (YaTeX-search-active-forward
200 begdoc YaTeX-comment-prefix nil t)
201 (goto-char (match-beginning 0)))
202 (insert
203 usepackage
204 (format "%s{%s}\t%% required for `\\%s' (yatex added)\n"
205 option pkg macro))
206 (funcall register))
207 (funcall register)
208 (message "Don't forget to put \\usepackage{%s} yourself later"
209 (car (car pkglist)))) ;doing car car is negligence...
210 ))))))