yatex

view yatexpkg.el @ 602:a7ad9158e3ce

Add completions for macros in booktab package
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 01 Dec 2022 14:42:04 +0859
parents e73e41509124
children c3e61ea3ba7f
line source
1 ;;; yatexpkg.el --- YaTeX package manager -*- coding: sjis -*-
2 ;;;
3 ;;; (c)2003-2019 by HIROSE, Yuuji [yuuji@yatex.org]
4 ;;; Last modified Thu Dec 1 14:30:58 2022 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 ("boites" (env "breakbox"))
32 ;;("eclbkbox" (env "breakbox"))
33 ("supertabular" (env "supertabular"))
34 ("tabularx" (env "tabularx"))
35 ("amsmath" (env . YaTeX-package-ams-envs)
36 (section "tag" "tag*"))
37 ("amsart" (same-as . "amsmath"))
38 ("amsbook" (same-as . "amsmath"))
39 ("amsproc" (same-as . "amsmath"))
40 ("amssymb" (maketitle "leqq" "geqq" "mathbb" "mathfrak"
41 "fallingdotseq" "therefore" "because"
42 "varDelta" "varTheta" "varLambda" "varXi" "varPi"
43 "varSigma" "varUpsilon" "varPhi" "varPsi" "varOmega"
44 "lll" "ggg")) ;very few. Please tell us!
45 ("latexsym" (maketitle "mho" "Join" "Box" "Diamond" "leadsto"
46 "sqsubset" "sqsupset" "lhd" "unlhd" "rhd" "unrhd"))
47 ("mathrsfs" (section "mathscr"))
48 ("fleqn" (env "nccmath"))
49 ("graphicx" (section "includegraphics"
50 "rotatebox" "scalebox" "resizebox" "reflectbox")
51 (option . YaTeX-package-graphics-driver-alist))
52 ("xymtex" (section "Ycyclohexaneh")) ;;XXX we need more and more...
53 ("chemist" nil) ;;XXX we need completions...
54 ("a4j" nil)
55 ("array" nil)
56 ("times" nil)
57 ("newtx" nil)
58 ("makeidx" nil)
59 ("geometry" (section "geometry"))
60 ("lscape" (env "landscape"))
61 ("path" (section "path"))
62 ("epsf" (section "epsfbox"))
63 ("epsfig" (section "epsfig"))
64 ("floatflt" (env "floatingfigure"))
65 ("type1cm" (section "fontsize"))
66 ("svg" (section "includesvg"))
67 ("color" (section "textcolor" "colorbox" "pagecolor" "color")
68 (option . YaTeX-package-graphics-driver-alist)
69 (default-option . "usenames,dvipsnames"))
70 ("xcolor" (same-as . "color"))
71 ("ulem" (section "uline" "uuline" "uwave")
72 (option ("normalem")))
73 ("multicol" (env "multicols"))
74 ("cleveref" (section "cref" "crefrange" "cpageref" "labelcref"
75 "labelpageref"))
76 ("wrapfig" (env "wrapfigure" "wraptable"))
77 ("setspace" (env "spacing") (section "setstretch"))
78 ("cases" (env "numcases" "subnumcases"))
79 ("subfigure" (section "subfigure"))
80 ("okumacro" (section "ruby" "kenten"))
81 ("colortbl" (section "columncolor" "rowcolor"))
82 ("booktab" (section "toprule" "midrule" "bottomrule" "cmidrule"
83 "addlinespace" "specialrule"))
84 )
85 "Default package vs. macro list.
86 Alists contains '(PACKAGENAME . MACROLIST)
87 PACKAGENAME Basename of package(String).
88 MACROLIST List of '(TYPE . MACROS)
89 TYPE One of 'env, 'section or 'maketitle according to completion-type
90 MACROS List of macros
92 If TYPE is 'option, its cdr is alist of completion candidates for that
93 package. Its cdr can be a symbol whose value is alist.
95 An good example is the value of YaTeX-package-alist-default.")
97 (defvar YaTeX-package-graphics-driver-alist
98 '(("dvips") ("dvipsnames") ("usenames")
99 ("xdvi") ("dvipdfmx") ("pdftex") ("dvipsone") ("dviwindo")
100 ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin")
101 ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex"))
102 "Drivers alist of graphics/color stylefile's supporting devices.
103 This list is taken from
104 %% graphics.dtx Copyright (C) 1994 David Carlisle Sebastian Rahtz
105 %% Copyright (C) 1995 1996 1997 1998 David Carlisle
106 as of 2004/1/19. Thanks.")
108 (defvar YaTeX-package-alist-private nil
109 "*User defined package vs. macro list. See also YaTeX-package-alist-default")
111 (defun YaTeX-package-lookup (macro &optional type)
112 "Look up a package which contains a definition of MACRO.
113 Optional second argument TYPE limits the macro type.
114 TYPE is a symbol, one of 'env, 'section, 'maketitle."
115 (let ((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
116 origlist element x sameas val pkg pkglist r)
117 (setq origlist list)
118 (while list
119 (setq element (car list)
120 pkg (car element)
121 element (cdr element))
122 (if (setq sameas (assq 'same-as element)) ;non-recursive retrieval
123 (setq element (cdr (assoc (cdr sameas) origlist))))
124 (if (setq r (catch 'found
125 (while element
126 (setq x (car element)
127 val (cdr x))
128 (if (symbolp val) (setq val (symbol-value val)))
129 (and (or (null type)
130 (eq type (car x)))
131 (YaTeX-member macro val)
132 (throw 'found (car x))) ;car x is type
133 (setq element (cdr element)))))
134 (setq pkglist (cons (cons pkg r) pkglist)))
135 (setq list (cdr list)))
136 pkglist))
138 (defun YaTeX-package-option-lookup (pkg &optional key)
139 "Look up options for specified pkg and returne them in alist form.
140 Just only associng against the alist of YaTeX-package-alist-*"
141 (let*((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
142 (l (cdr (assq (or key 'option) (assoc pkg list))))
143 (recur (cdr (assq 'same-as (assoc pkg list)))))
144 (cond
145 (recur (YaTeX-package-option-lookup recur key))
146 ((symbolp l) (symbol-value l))
147 (t l))))
149 (defvar YaTeX-package-resolved-list nil
150 "List of macros whose package is confirmed to be loaded.")
152 (defun YaTeX-package-auto-usepackage (macro type &optional autopkg autoopt)
153 "(Semi)Automatically add the \\usepackage line to main-file.
154 Search the usepackage for MACRO of the TYPE.
155 Optional second and third argument AUTOPKG, AUTOOPT are selected
156 without query. Thus those two argument (Full)automatically add
157 a \\usepackage line."
158 (let ((cb (current-buffer))
159 (wc (current-window-configuration))
160 (usepackage (concat YaTeX-ec "usepackage"))
161 (pkglist (YaTeX-package-lookup macro type))
162 (usepkgrx (concat
163 YaTeX-ec-regexp
164 "\\(usepackage\\|include\\|documentclass\\)\\b"))
165 (register (function
166 (lambda () (set-buffer cb)
167 (set (make-local-variable 'YaTeX-package-resolved-list)
168 (cons macro YaTeX-package-resolved-list)))))
169 (begdoc (concat YaTeX-ec "begin{document}"))
170 pb pkg optlist (option "") mb0 uspkgargs)
171 (if (or (YaTeX-member macro YaTeX-package-resolved-list)
172 (null pkglist))
173 nil ;nothing to do
174 ;; Search `usepackage' into main-file
175 (YaTeX-visit-main t) ;set buffer to parent file
176 (setq pb (current-buffer))
177 (save-excursion
178 (save-restriction
179 (if (catch 'found
180 (goto-char (point-min))
181 (YaTeX-search-active-forward ;if search fails, goto eob
182 begdoc YaTeX-comment-prefix nil 1)
183 (while (re-search-backward usepkgrx nil t)
184 ;;allow commented out \usepackages
185 (setq mb0 (match-beginning 0))
186 (skip-chars-forward "^{")
187 (setq uspkgargs (YaTeX-buffer-substring
188 (point)
189 (progn
190 ;;(forward-list 1) is more precise,
191 ;; but higher risk.
192 (skip-chars-forward "^}\n")(point))))
193 (let ((pl pkglist))
194 (while pl ;(car pl)'s car is package, cdr is type
195 (if (string-match
196 (concat "[{,]\\s *"
197 (regexp-quote (car (car pl)))
198 "\\>")
199 uspkgargs)
200 (throw 'found t))
201 (setq pl (cdr pl)))
202 (goto-char mb0))))
203 ;;corresponding \usepackage found
204 (funcall register)
205 ;; not found, insert it.
206 (if (or
207 autopkg
208 (y-or-n-p
209 (format "`%s' requires package. Put \\usepackage now?"
210 macro)))
211 (progn
212 (require 'yatexadd)
213 (setq pkg
214 (or autopkg
215 (completing-read
216 "Load which package?(TAB for list): "
217 pkglist nil nil
218 ;;initial input
219 (if (= (length pkglist) 1)
220 (let ((w (car (car pkglist))))
221 (if YaTeX-emacs-19 (cons w 0) w)))))
222 optlist
223 (YaTeX-package-option-lookup pkg))
224 (if optlist
225 (let ((minibuffer-completion-table optlist)
226 (delim ",") (w (car (car optlist)))
227 (dflt (YaTeX-package-option-lookup
228 pkg 'default-option)))
229 (setq option
230 (or
231 autoopt
232 (read-from-minibuffer
233 (format "Any option for {%s}?: " pkg)
234 (let ((v (or dflt
235 (and (= (length optlist) 1) w))))
236 (and v (if YaTeX-emacs-19 (cons v 0) v)))
237 YaTeX-minibuffer-completion-map))
238 option (if (string< "" option)
239 (concat "[" option "]")
240 ""))))
241 (set-buffer pb)
242 (goto-char (point-min))
243 (if (YaTeX-re-search-active-forward
244 (concat YaTeX-ec-regexp
245 "document\\(style\\|class\\){")
246 YaTeX-comment-prefix nil t)
247 (forward-line 1))
248 (if (YaTeX-search-active-forward
249 begdoc YaTeX-comment-prefix nil t)
250 (goto-char (match-beginning 0)))
251 (insert
252 usepackage
253 (format "%s{%s}\t%% required for `\\%s' (yatex added)\n"
254 option pkg macro))
255 (funcall register))
256 (funcall register)
257 (message "Don't forget to put \\usepackage{%s} yourself later"
258 (car (car pkglist)))) ;doing car car is negligence...
259 ))))))
261 (defvar YaTeX::usepackage-alist-private nil
262 "*Private completion list of the argument for usepackage")
264 (defvar YaTeX::usepackage-alist-local nil
265 "Directory local completion list of the argument for usepackage")
267 (defun YaTeX::usepackage (&optional argp)
268 (cond
269 ((equal argp 1)
270 (setq YaTeX-env-name "document")
271 (let ((minibuffer-local-completion-map YaTeX-minibuffer-completion-map)
272 (delim ","))
273 (YaTeX-cplread-with-learning
274 (if YaTeX-japan "Use package(カンマで区切ってOK): "
275 "Use package(delimitable by comma): ")
276 ;; 'YaTeX::usepackage-alist-default ;; OBSOLETED at 1.82
277 'YaTeX-package-alist-default
278 'YaTeX::usepackage-alist-private
279 'YaTeX::usepackage-alist-local)))))
282 ;;;
283 ;; Add-ins for auxiliary package handled here
284 ;;;
285 (defun YaTeX:floatingfigure ()
286 (concat (YaTeX:read-position "rlpv")
287 (YaTeX:read-length "Width: ")))
289 (defvar YaTeX:geometry-default "margin=1.5cm,includeheadfoot,includemp"
290 "*Default options for \\geometry{}")
291 (defun YaTeX::geometry (argp)
292 "Add-in for \\geometry's option"
293 ;; cf. https://dayinthelife.at.webry.info/201401/article_2.html
294 (cond
295 ((= argp 1)
296 (YaTeX-help "geometry")
297 (message "Change default by setting YaTeX:geometry-default")
298 (if (string= YaTeX:geometry-default "") ""
299 YaTeX:geometry-default))))
301 (provide 'yatexpkg)