yatex

view yatexpkg.el @ 432:79dc66c450df

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