yatex

view yatexpkg.el @ 611:e87c3271b8fd

Add "meter" and "progress" completions
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 17 Jul 2023 17:05:45 +0900
parents c3e61ea3ba7f
children
line source
1 ;;; yatexpkg.el --- YaTeX package manager -*- coding: sjis -*-
2 ;;;
3 ;;; (c)2003-2022 by HIROSE, Yuuji [yuuji@yatex.org]
4 ;;; Last modified Fri Dec 2 08:22:41 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 ("pxbase" (section "UI"))
85 )
86 "Default package vs. macro list.
87 Alists contains '(PACKAGENAME . MACROLIST)
88 PACKAGENAME Basename of package(String).
89 MACROLIST List of '(TYPE . MACROS)
90 TYPE One of 'env, 'section or 'maketitle according to completion-type
91 MACROS List of macros
93 If TYPE is 'option, its cdr is alist of completion candidates for that
94 package. Its cdr can be a symbol whose value is alist.
96 An good example is the value of YaTeX-package-alist-default.")
98 (defvar YaTeX-package-graphics-driver-alist
99 '(("dvips") ("dvipsnames") ("usenames")
100 ("xdvi") ("dvipdfmx") ("pdftex") ("dvipsone") ("dviwindo")
101 ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin")
102 ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex"))
103 "Drivers alist of graphics/color stylefile's supporting devices.
104 This list is taken from
105 %% graphics.dtx Copyright (C) 1994 David Carlisle Sebastian Rahtz
106 %% Copyright (C) 1995 1996 1997 1998 David Carlisle
107 as of 2004/1/19. Thanks.")
109 (defvar YaTeX-package-alist-private nil
110 "*User defined package vs. macro list. See also YaTeX-package-alist-default")
112 (defun YaTeX-package-lookup (macro &optional type)
113 "Look up a package which contains a definition of MACRO.
114 Optional second argument TYPE limits the macro type.
115 TYPE is a symbol, one of 'env, 'section, 'maketitle."
116 (let ((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
117 origlist element x sameas val pkg pkglist r)
118 (setq origlist list)
119 (while list
120 (setq element (car list)
121 pkg (car element)
122 element (cdr element))
123 (if (setq sameas (assq 'same-as element)) ;non-recursive retrieval
124 (setq element (cdr (assoc (cdr sameas) origlist))))
125 (if (setq r (catch 'found
126 (while element
127 (setq x (car element)
128 val (cdr x))
129 (if (symbolp val) (setq val (symbol-value val)))
130 (and (or (null type)
131 (eq type (car x)))
132 (YaTeX-member macro val)
133 (throw 'found (car x))) ;car x is type
134 (setq element (cdr element)))))
135 (setq pkglist (cons (cons pkg r) pkglist)))
136 (setq list (cdr list)))
137 pkglist))
139 (defun YaTeX-package-option-lookup (pkg &optional key)
140 "Look up options for specified pkg and returne them in alist form.
141 Just only associng against the alist of YaTeX-package-alist-*"
142 (let*((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
143 (l (cdr (assq (or key 'option) (assoc pkg list))))
144 (recur (cdr (assq 'same-as (assoc pkg list)))))
145 (cond
146 (recur (YaTeX-package-option-lookup recur key))
147 ((symbolp l) (symbol-value l))
148 (t l))))
150 (defvar YaTeX-package-resolved-list nil
151 "List of macros whose package is confirmed to be loaded.")
153 (defun YaTeX-package-auto-usepackage (macro type &optional autopkg autoopt)
154 "(Semi)Automatically add the \\usepackage line to main-file.
155 Search the usepackage for MACRO of the TYPE.
156 Optional second and third argument AUTOPKG, AUTOOPT are selected
157 without query. Thus those two argument (Full)automatically add
158 a \\usepackage line."
159 (let ((cb (current-buffer))
160 (wc (current-window-configuration))
161 (usepackage (concat YaTeX-ec "usepackage"))
162 (pkglist (YaTeX-package-lookup macro type))
163 (usepkgrx (concat
164 YaTeX-ec-regexp
165 "\\(usepackage\\|include\\|documentclass\\)\\b"))
166 (register (function
167 (lambda () (set-buffer cb)
168 (set (make-local-variable 'YaTeX-package-resolved-list)
169 (cons macro YaTeX-package-resolved-list)))))
170 (begdoc (concat YaTeX-ec "begin{document}"))
171 pb pkg optlist (option "") mb0 uspkgargs)
172 (if (or (YaTeX-member macro YaTeX-package-resolved-list)
173 (null pkglist))
174 nil ;nothing to do
175 ;; Search `usepackage' into main-file
176 (YaTeX-visit-main t) ;set buffer to parent file
177 (setq pb (current-buffer))
178 (save-excursion
179 (save-restriction
180 (if (catch 'found
181 (goto-char (point-min))
182 (YaTeX-search-active-forward ;if search fails, goto eob
183 begdoc YaTeX-comment-prefix nil 1)
184 (while (re-search-backward usepkgrx nil t)
185 ;;allow commented out \usepackages
186 (setq mb0 (match-beginning 0))
187 (skip-chars-forward "^{")
188 (setq uspkgargs (YaTeX-buffer-substring
189 (point)
190 (progn
191 ;;(forward-list 1) is more precise,
192 ;; but higher risk.
193 (skip-chars-forward "^}\n")(point))))
194 (let ((pl pkglist))
195 (while pl ;(car pl)'s car is package, cdr is type
196 (if (string-match
197 (concat "[{,]\\s *"
198 (regexp-quote (car (car pl)))
199 "\\>")
200 uspkgargs)
201 (throw 'found t))
202 (setq pl (cdr pl)))
203 (goto-char mb0))))
204 ;;corresponding \usepackage found
205 (funcall register)
206 ;; not found, insert it.
207 (if (or
208 autopkg
209 (y-or-n-p
210 (format "`%s' requires package. Put \\usepackage now?"
211 macro)))
212 (progn
213 (require 'yatexadd)
214 (setq pkg
215 (or autopkg
216 (completing-read
217 "Load which package?(TAB for list): "
218 pkglist nil nil
219 ;;initial input
220 (if (= (length pkglist) 1)
221 (let ((w (car (car pkglist))))
222 (if YaTeX-emacs-19 (cons w 0) w)))))
223 optlist
224 (YaTeX-package-option-lookup pkg))
225 (if optlist
226 (let ((minibuffer-completion-table optlist)
227 (delim ",") (w (car (car optlist)))
228 (dflt (YaTeX-package-option-lookup
229 pkg 'default-option)))
230 (setq option
231 (or
232 autoopt
233 (read-from-minibuffer
234 (format "Any option for {%s}?: " pkg)
235 (let ((v (or dflt
236 (and (= (length optlist) 1) w))))
237 (and v (if YaTeX-emacs-19 (cons v 0) v)))
238 YaTeX-minibuffer-completion-map))
239 option (if (string< "" option)
240 (concat "[" option "]")
241 ""))))
242 (set-buffer pb)
243 (goto-char (point-min))
244 (if (YaTeX-re-search-active-forward
245 (concat YaTeX-ec-regexp
246 "document\\(style\\|class\\){")
247 YaTeX-comment-prefix nil t)
248 (forward-line 1))
249 (if (YaTeX-search-active-forward
250 begdoc YaTeX-comment-prefix nil t)
251 (goto-char (match-beginning 0)))
252 (insert
253 usepackage
254 (format "%s{%s}\t%% required for `\\%s' (yatex added)\n"
255 option pkg macro))
256 (funcall register))
257 (funcall register)
258 (message "Don't forget to put \\usepackage{%s} yourself later"
259 (car (car pkglist)))) ;doing car car is negligence...
260 ))))))
262 (defvar YaTeX::usepackage-alist-private nil
263 "*Private completion list of the argument for usepackage")
265 (defvar YaTeX::usepackage-alist-local nil
266 "Directory local completion list of the argument for usepackage")
268 (defun YaTeX::usepackage (&optional argp)
269 (cond
270 ((equal argp 1)
271 (setq YaTeX-env-name "document")
272 (let ((minibuffer-local-completion-map YaTeX-minibuffer-completion-map)
273 (delim ","))
274 (YaTeX-cplread-with-learning
275 (if YaTeX-japan "Use package(カンマで区切ってOK): "
276 "Use package(delimitable by comma): ")
277 ;; 'YaTeX::usepackage-alist-default ;; OBSOLETED at 1.82
278 'YaTeX-package-alist-default
279 'YaTeX::usepackage-alist-private
280 'YaTeX::usepackage-alist-local)))))
283 ;;;
284 ;; Add-ins for auxiliary package handled here
285 ;;;
286 (defun YaTeX:floatingfigure ()
287 (concat (YaTeX:read-position "rlpv")
288 (YaTeX:read-length "Width: ")))
290 (defvar YaTeX:geometry-default "margin=1.5cm,includeheadfoot,includemp"
291 "*Default options for \\geometry{}")
292 (defun YaTeX::geometry (argp)
293 "Add-in for \\geometry's option"
294 ;; cf. https://dayinthelife.at.webry.info/201401/article_2.html
295 (cond
296 ((= argp 1)
297 (YaTeX-help "geometry")
298 (message "Change default by setting YaTeX:geometry-default")
299 (if (string= YaTeX:geometry-default "") ""
300 YaTeX:geometry-default))))
302 (provide 'yatexpkg)