yatex

view yatexpkg.el @ 441:564510b9caca

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