yatex

view yatexpkg.el @ 364:d69fd7b1ac4d

yatex-1.78
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 26 Dec 2014 01:02:52 +0900
parents af0bd0f86b27
children 77a2fd4fd634
line source
1 ;;; yatexpkg.el --- YaTeX package manager
2 ;;;
3 ;;; (c)2003-2014 by HIROSE, Yuuji [yuuji@yatex.org]
4 ;;; Last modified Mon Dec 22 11:12:19 2014 on firestorm
5 ;;; $Id: yatexpkg.el,v 1.78 2014/12/25 15:11:02 yuuji Rel $
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" "therefore" "because"
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 ("cref" (section "cleveref"))
48 ("crefrange" (same-as . "cref"))
49 ("cpageref" (same-as . "cref"))
50 ("labelcref" (same-as . "cref"))
51 ("labelcpageref" (same-as . "cref"))
52 ("wrapfig" (env "wrapfigure" "wraptable"))
53 )
54 "Default package vs. macro list.
55 Alists contains '(PACKAGENAME . MACROLIST)
56 PACKAGENAME Basename of package(String).
57 MACROLIST List of '(TYPE . MACROS)
58 TYPE One of 'env, 'section or 'maketitle according to completion-type
59 MACROS List of macros
61 If TYPE is 'option, its cdr is alist of completion candidates for that
62 package. Its cdr can be a symbol whose value is alist.
64 An good example is the value of YaTeX-package-alist-default.")
66 (defvar YaTeX-package-graphics-driver-alist
67 '(("dvips") ("dvipsnames") ("usenames")
68 ("xdvi") ("dvipdfmx") ("pdftex") ("dvipsone") ("dviwindo")
69 ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin")
70 ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex"))
71 "Drivers alist of graphics/color stylefile's supporting deveces.
72 This list is taken from
73 %% graphics.dtx Copyright (C) 1994 David Carlisle Sebastian Rahtz
74 %% Copyright (C) 1995 1996 1997 1998 David Carlisle
75 as of 2004/1/19. Thanks.")
77 (defvar YaTeX-package-alist-private nil
78 "*User defined package vs. macro list. See also YaTeX-package-alist-default")
80 (defun YaTeX-package-lookup (macro &optional type)
81 "Look up a package which contains a definition of MACRO.
82 Optional second argument TYPE limits the macro type.
83 TYPE is a symbol, one of 'env, 'section, 'maketitle."
84 (let ((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
85 origlist element x sameas val pkg pkglist r)
86 (setq origlist list)
87 (while list
88 (setq element (car list)
89 pkg (car element)
90 element (cdr element))
91 (if (setq sameas (assq 'same-as element)) ;non-recursive retrieval
92 (setq element (cdr (assoc (cdr sameas) origlist))))
93 (if (setq r (catch 'found
94 (while element
95 (setq x (car element)
96 val (cdr x))
97 (if (symbolp val) (setq val (symbol-value val)))
98 (and (or (null type)
99 (eq type (car x)))
100 (YaTeX-member macro val)
101 (throw 'found (car x))) ;car x is type
102 (setq element (cdr element)))))
103 (setq pkglist (cons (cons pkg r) pkglist)))
104 (setq list (cdr list)))
105 pkglist))
107 (defun YaTeX-package-option-lookup (pkg &optional key)
108 "Look up options for specified pkg and returne them in alist form.
109 Just only associng against the alist of YaTeX-package-alist-*"
110 (let*((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
111 (l (cdr (assq (or key 'option) (assoc pkg list))))
112 (recur (cdr (assq 'same-as (assoc pkg list)))))
113 (cond
114 (recur (YaTeX-package-option-lookup recur key))
115 ((symbolp l) (symbol-value l))
116 (t l))))
118 (defvar YaTeX-package-resolved-list nil
119 "List of macros whose package is confirmed to be loaded.")
121 (defun YaTeX-package-auto-usepackage (macro type &optional autopkg autoopt)
122 "(Semi)Automatically add the \\usepackage line to main-file.
123 Search the usepackage for MACRO of the TYPE.
124 Optional second and third argument AUTOPKG, AUTOOPT are selected
125 without query. Thus those two argument (Full)automatically add
126 a \\usepackage line."
127 (let ((cb (current-buffer))
128 (wc (current-window-configuration))
129 (usepackage (concat YaTeX-ec "usepackage"))
130 (pkglist (YaTeX-package-lookup macro type))
131 (usepkgrx (concat
132 YaTeX-ec-regexp
133 "\\(usepackage\\|include\\)\\b"))
134 (register (function
135 (lambda () (set-buffer cb)
136 (set (make-local-variable 'YaTeX-package-resolved-list)
137 (cons macro YaTeX-package-resolved-list)))))
138 (begdoc (concat YaTeX-ec "begin{document}"))
139 pb pkg optlist (option "") mb0 uspkgargs)
140 (if (or (YaTeX-member macro YaTeX-package-resolved-list)
141 (null pkglist))
142 nil ;nothing to do
143 ;; Search `usepackage' into main-file
144 (YaTeX-visit-main t) ;set buffer to parent file
145 (setq pb (current-buffer))
146 (save-excursion
147 (save-restriction
148 (if (catch 'found
149 (goto-char (point-min))
150 (YaTeX-search-active-forward ;if search fails, goto eob
151 begdoc YaTeX-comment-prefix nil 1)
152 (while ;(YaTeX-re-search-active-backward
153 ;usepkgrx YaTeX-comment-prefix nil t)
154 ;;allow commented out \usepackages 2004/3/16
155 (re-search-backward usepkgrx nil t)
156 (setq mb0 (match-beginning 0))
157 (skip-chars-forward "^{")
158 (setq uspkgargs (YaTeX-buffer-substring
159 (point)
160 (progn
161 ;;(forward-list 1) is more precise,
162 ;; but higher risk.
163 (skip-chars-forward "^}\n")(point))))
164 (let ((pl pkglist))
165 (while pl ;(car pl)'s car is package, cdr is type
166 (if (string-match
167 (concat "[{,]\\s *"
168 (regexp-quote (car (car pl)))
169 "\\>")
170 uspkgargs)
171 (throw 'found t))
172 (setq pl (cdr pl)))
173 (goto-char mb0))))
174 ;;corresponding \usepackage found
175 (funcall register)
176 ;; not found, insert it.
177 (if (or
178 autopkg
179 (y-or-n-p
180 (format "`%s' requires package. Put \\usepackage now?"
181 macro)))
182 (progn
183 (require 'yatexadd)
184 (setq pkg
185 (or autopkg
186 (completing-read
187 "Load which package?(TAB for list): "
188 pkglist nil nil
189 ;;initial input
190 (if (= (length pkglist) 1)
191 (let ((w (car (car pkglist))))
192 (if YaTeX-emacs-19 (cons w 0) w)))))
193 optlist
194 (YaTeX-package-option-lookup pkg))
195 (if optlist
196 (let ((minibuffer-completion-table optlist)
197 (delim ",") (w (car (car optlist)))
198 (dflt (YaTeX-package-option-lookup
199 pkg 'default-option)))
200 (setq option
201 (or
202 autoopt
203 (read-from-minibuffer
204 (format "Any option for {%s}?: " pkg)
205 (let ((v (or dflt
206 (and (= (length optlist) 1) w))))
207 (and v (if YaTeX-emacs-19 (cons v 0) v)))
208 YaTeX-minibuffer-completion-map))
209 option (if (string< "" option)
210 (concat "[" option "]")
211 ""))))
212 (set-buffer pb)
213 (goto-char (point-min))
214 (if (YaTeX-re-search-active-forward
215 (concat YaTeX-ec-regexp
216 "document\\(style\\|class\\){")
217 YaTeX-comment-prefix nil t)
218 (forward-line 1))
219 (if (YaTeX-search-active-forward
220 begdoc YaTeX-comment-prefix nil t)
221 (goto-char (match-beginning 0)))
222 (insert
223 usepackage
224 (format "%s{%s}\t%% required for `\\%s' (yatex added)\n"
225 option pkg macro))
226 (funcall register))
227 (funcall register)
228 (message "Don't forget to put \\usepackage{%s} yourself later"
229 (car (car pkglist)))) ;doing car car is negligence...
230 ))))))