yatex

annotate yatexpkg.el @ 541:7595fc6462ff

Add links
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 31 May 2018 09:53:11 +0900
parents 7a64b7761d6b
children e7356faf50df
rev   line source
yuuji@395 1 ;;; yatexpkg.el --- YaTeX package manager -*- coding: sjis -*-
yuuji@287 2 ;;;
yuuji@510 3 ;;; (c)2003-2018 by HIROSE, Yuuji [yuuji@yatex.org]
yuuji@516 4 ;;; Last modified Fri Jan 5 00:22:46 2018 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@80 31 ("eclbkbox" (env "breakbox"))
yuuji@80 32 ("supertabular" (env "supertabular"))
yuuji@80 33 ("amsmath" (env . YaTeX-package-ams-envs)
yuuji@80 34 (section "tag" "tag*"))
yuuji@134 35 ("amssymb" (maketitle "leqq" "geqq" "mathbb" "mathfrak"
yuuji@305 36 "fallingdotseq" "therefore" "because"
yuuji@460 37 "varDelta" "varTheta" "varLambda" "varXi" "varPi"
yuuji@460 38 "varSigma" "varUpsilon" "varPhi" "varPsi" "varOmega"
yuuji@134 39 "lll" "ggg")) ;very few. Please tell us!
yuuji@456 40 ("mathrsfs" (section "mathscr"))
yuuji@80 41 ("graphicx" (section "includegraphics"
yuuji@80 42 "rotatebox" "scalebox" "resizebox" "reflectbox")
yuuji@80 43 (option . YaTeX-package-graphics-driver-alist))
yuuji@516 44 ("svg" (section "includesvg"))
yuuji@80 45 ("color" (section "textcolor" "colorbox" "pagecolor" "color")
yuuji@303 46 (option . YaTeX-package-graphics-driver-alist)
yuuji@303 47 (default-option . "usenames,dvipsnames"))
yuuji@303 48 ("xcolor" (same-as . "color"))
yuuji@80 49 ("ulem" (section "uline" "uuline" "uwave")
yuuji@80 50 (option ("normalem")))
yuuji@312 51 ("multicol" (env "multicols"))
yuuji@314 52 ("cref" (section "cleveref"))
yuuji@314 53 ("crefrange" (same-as . "cref"))
yuuji@314 54 ("cpageref" (same-as . "cref"))
yuuji@314 55 ("labelcref" (same-as . "cref"))
yuuji@314 56 ("labelcpageref" (same-as . "cref"))
yuuji@346 57 ("wrapfig" (env "wrapfigure" "wraptable"))
yuuji@418 58 ("setspace" (env "spacing") (section "setstretch"))
yuuji@449 59 ("cases" (env "numcases" "subnumcases"))
yuuji@314 60 )
yuuji@79 61 "Default package vs. macro list.
yuuji@79 62 Alists contains '(PACKAGENAME . MACROLIST)
yuuji@79 63 PACKAGENAME Basename of package(String).
yuuji@79 64 MACROLIST List of '(TYPE . MACROS)
yuuji@79 65 TYPE One of 'env, 'section or 'maketitle according to completion-type
yuuji@79 66 MACROS List of macros
yuuji@79 67
yuuji@80 68 If TYPE is 'option, its cdr is alist of completion candidates for that
yuuji@80 69 package. Its cdr can be a symbol whose value is alist.
yuuji@80 70
yuuji@79 71 An good example is the value of YaTeX-package-alist-default.")
yuuji@78 72
yuuji@80 73 (defvar YaTeX-package-graphics-driver-alist
yuuji@303 74 '(("dvips") ("dvipsnames") ("usenames")
yuuji@333 75 ("xdvi") ("dvipdfmx") ("pdftex") ("dvipsone") ("dviwindo")
yuuji@80 76 ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin")
yuuji@80 77 ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex"))
yuuji@80 78 "Drivers alist of graphics/color stylefile's supporting deveces.
yuuji@80 79 This list is taken from
yuuji@80 80 %% graphics.dtx Copyright (C) 1994 David Carlisle Sebastian Rahtz
yuuji@80 81 %% Copyright (C) 1995 1996 1997 1998 David Carlisle
yuuji@80 82 as of 2004/1/19. Thanks.")
yuuji@80 83
yuuji@78 84 (defvar YaTeX-package-alist-private nil
yuuji@78 85 "*User defined package vs. macro list. See also YaTeX-package-alist-default")
yuuji@78 86
yuuji@78 87 (defun YaTeX-package-lookup (macro &optional type)
yuuji@78 88 "Look up a package which contains a definition of MACRO.
yuuji@78 89 Optional second argument TYPE limits the macro type.
yuuji@78 90 TYPE is a symbol, one of 'env, 'section, 'maketitle."
yuuji@78 91 (let ((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
yuuji@304 92 origlist element x sameas val pkg pkglist r)
yuuji@304 93 (setq origlist list)
yuuji@78 94 (while list
yuuji@78 95 (setq element (car list)
yuuji@78 96 pkg (car element)
yuuji@78 97 element (cdr element))
yuuji@304 98 (if (setq sameas (assq 'same-as element)) ;non-recursive retrieval
yuuji@304 99 (setq element (cdr (assoc (cdr sameas) origlist))))
yuuji@78 100 (if (setq r (catch 'found
yuuji@78 101 (while element
yuuji@80 102 (setq x (car element)
yuuji@80 103 val (cdr x))
yuuji@80 104 (if (symbolp val) (setq val (symbol-value val)))
yuuji@80 105 (and (or (null type)
yuuji@78 106 (eq type (car x)))
yuuji@80 107 (YaTeX-member macro val)
yuuji@78 108 (throw 'found (car x))) ;car x is type
yuuji@78 109 (setq element (cdr element)))))
yuuji@78 110 (setq pkglist (cons (cons pkg r) pkglist)))
yuuji@78 111 (setq list (cdr list)))
yuuji@78 112 pkglist))
yuuji@78 113
yuuji@303 114 (defun YaTeX-package-option-lookup (pkg &optional key)
yuuji@80 115 "Look up options for specified pkg and returne them in alist form.
yuuji@303 116 Just only associng against the alist of YaTeX-package-alist-*"
yuuji@303 117 (let*((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
yuuji@303 118 (l (cdr (assq (or key 'option) (assoc pkg list))))
yuuji@303 119 (recur (cdr (assq 'same-as (assoc pkg list)))))
yuuji@303 120 (cond
yuuji@303 121 (recur (YaTeX-package-option-lookup recur key))
yuuji@303 122 ((symbolp l) (symbol-value l))
yuuji@303 123 (t l))))
yuuji@80 124
yuuji@78 125 (defvar YaTeX-package-resolved-list nil
yuuji@78 126 "List of macros whose package is confirmed to be loaded.")
yuuji@78 127
yuuji@356 128 (defun YaTeX-package-auto-usepackage (macro type &optional autopkg autoopt)
yuuji@78 129 "(Semi)Automatically add the \\usepackage line to main-file.
yuuji@356 130 Search the usepackage for MACRO of the TYPE.
yuuji@356 131 Optional second and third argument AUTOPKG, AUTOOPT are selected
yuuji@356 132 without query. Thus those two argument (Full)automatically add
yuuji@356 133 a \\usepackage line."
yuuji@78 134 (let ((cb (current-buffer))
yuuji@78 135 (wc (current-window-configuration))
yuuji@78 136 (usepackage (concat YaTeX-ec "usepackage"))
yuuji@80 137 (pkglist (YaTeX-package-lookup macro type))
yuuji@78 138 (usepkgrx (concat
yuuji@78 139 YaTeX-ec-regexp
yuuji@78 140 "\\(usepackage\\|include\\)\\b"))
yuuji@353 141 (register (function
yuuji@353 142 (lambda () (set-buffer cb)
yuuji@78 143 (set (make-local-variable 'YaTeX-package-resolved-list)
yuuji@353 144 (cons macro YaTeX-package-resolved-list)))))
yuuji@78 145 (begdoc (concat YaTeX-ec "begin{document}"))
yuuji@80 146 pb pkg optlist (option "") mb0 uspkgargs)
yuuji@78 147 (if (or (YaTeX-member macro YaTeX-package-resolved-list)
yuuji@78 148 (null pkglist))
yuuji@78 149 nil ;nothing to do
yuuji@78 150 ;; Search `usepackage' into main-file
yuuji@78 151 (YaTeX-visit-main t) ;set buffer to parent file
yuuji@78 152 (setq pb (current-buffer))
yuuji@78 153 (save-excursion
yuuji@78 154 (save-restriction
yuuji@78 155 (if (catch 'found
yuuji@78 156 (goto-char (point-min))
yuuji@78 157 (YaTeX-search-active-forward ;if search fails, goto eob
yuuji@78 158 begdoc YaTeX-comment-prefix nil 1)
yuuji@80 159 (while ;(YaTeX-re-search-active-backward
yuuji@80 160 ;usepkgrx YaTeX-comment-prefix nil t)
yuuji@80 161 ;;allow commented out \usepackages 2004/3/16
yuuji@80 162 (re-search-backward usepkgrx nil t)
yuuji@78 163 (setq mb0 (match-beginning 0))
yuuji@78 164 (skip-chars-forward "^{")
yuuji@80 165 (setq uspkgargs (YaTeX-buffer-substring
yuuji@80 166 (point)
yuuji@80 167 (progn
yuuji@80 168 ;;(forward-list 1) is more precise,
yuuji@80 169 ;; but higher risk.
yuuji@80 170 (skip-chars-forward "^}\n")(point))))
yuuji@78 171 (let ((pl pkglist))
yuuji@78 172 (while pl ;(car pl)'s car is package, cdr is type
yuuji@80 173 (if (string-match
yuuji@80 174 (concat "[{,]\\s *"
yuuji@80 175 (regexp-quote (car (car pl)))
yuuji@80 176 "\\>")
yuuji@80 177 uspkgargs)
yuuji@78 178 (throw 'found t))
yuuji@78 179 (setq pl (cdr pl)))
yuuji@78 180 (goto-char mb0))))
yuuji@78 181 ;;corresponding \usepackage found
yuuji@78 182 (funcall register)
yuuji@78 183 ;; not found, insert it.
yuuji@356 184 (if (or
yuuji@356 185 autopkg
yuuji@356 186 (y-or-n-p
yuuji@356 187 (format "`%s' requires package. Put \\usepackage now?"
yuuji@356 188 macro)))
yuuji@78 189 (progn
yuuji@80 190 (require 'yatexadd)
yuuji@78 191 (setq pkg
yuuji@356 192 (or autopkg
yuuji@356 193 (completing-read
yuuji@356 194 "Load which package?(TAB for list): "
yuuji@356 195 pkglist nil nil
yuuji@356 196 ;;initial input
yuuji@356 197 (if (= (length pkglist) 1)
yuuji@356 198 (let ((w (car (car pkglist))))
yuuji@356 199 (if YaTeX-emacs-19 (cons w 0) w)))))
yuuji@80 200 optlist
yuuji@80 201 (YaTeX-package-option-lookup pkg))
yuuji@80 202 (if optlist
yuuji@80 203 (let ((minibuffer-completion-table optlist)
yuuji@303 204 (delim ",") (w (car (car optlist)))
yuuji@303 205 (dflt (YaTeX-package-option-lookup
yuuji@303 206 pkg 'default-option)))
yuuji@80 207 (setq option
yuuji@356 208 (or
yuuji@356 209 autoopt
yuuji@356 210 (read-from-minibuffer
yuuji@356 211 (format "Any option for {%s}?: " pkg)
yuuji@356 212 (let ((v (or dflt
yuuji@356 213 (and (= (length optlist) 1) w))))
yuuji@356 214 (and v (if YaTeX-emacs-19 (cons v 0) v)))
yuuji@356 215 YaTeX-minibuffer-completion-map))
yuuji@80 216 option (if (string< "" option)
yuuji@80 217 (concat "[" option "]")
yuuji@80 218 ""))))
yuuji@78 219 (set-buffer pb)
yuuji@78 220 (goto-char (point-min))
yuuji@78 221 (if (YaTeX-re-search-active-forward
yuuji@78 222 (concat YaTeX-ec-regexp
yuuji@78 223 "document\\(style\\|class\\){")
yuuji@78 224 YaTeX-comment-prefix nil t)
yuuji@78 225 (forward-line 1))
yuuji@78 226 (if (YaTeX-search-active-forward
yuuji@78 227 begdoc YaTeX-comment-prefix nil t)
yuuji@78 228 (goto-char (match-beginning 0)))
yuuji@78 229 (insert
yuuji@78 230 usepackage
yuuji@80 231 (format "%s{%s}\t%% required for `\\%s' (yatex added)\n"
yuuji@80 232 option pkg macro))
yuuji@78 233 (funcall register))
yuuji@120 234 (funcall register)
yuuji@78 235 (message "Don't forget to put \\usepackage{%s} yourself later"
yuuji@78 236 (car (car pkglist)))) ;doing car car is negligence...
yuuji@78 237 ))))))