yatex

view yatexpkg.el @ 462:c940797c19ad

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