yatex

annotate yatexpkg.el @ 627:f294195d0d3f

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