yatex

view yatexpkg.el @ 552:c3b9e81bd432

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