yatex

annotate yatexpkg.el @ 366:77a2fd4fd634

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