yatex

view yatexpkg.el @ 353:2a72779d9c50

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