yatex

view yatexpkg.el @ 456:7bf780961390

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