yuuji@395: ;;; yatexpkg.el --- YaTeX package manager -*- coding: sjis -*- yuuji@287: ;;; yuuji@510: ;;; (c)2003-2018 by HIROSE, Yuuji [yuuji@yatex.org] yuuji@565: ;;; Last modified Thu Jan 24 14:13:01 2019 on firestorm yuuji@366: ;;; $Id$ yuuji@78: yuuji@287: ;;; Code: yuuji@80: (defvar YaTeX-package-ams-envs yuuji@80: (mapcar 'car YaTeX-ams-env-table)) yuuji@80: yuuji@78: (defvar YaTeX-package-alist-default yuuji@193: '(("version" (env "comment") ;by tsuchiyapine.kuee.kyoto-u.ac.jp yuuji@79: (section "includeversion" "excludeversion")) yuuji@78: yuuji@193: ("plext" (section "bou")) ;by yas.axisma.mni.ne.jp yuuji@78: yuuji@193: ("url" (section "url")) ;by fujiedajaist.ac.jp yuuji@78: yuuji@78: ("fancybox" (section "shadowbox" "doublebox" "ovalbox" "Ovalbox")) yuuji@80: ("slashbox" (section "slashbox" "backslashbox")) yuuji@78: ("pifont" (section "ding")) yuuji@78: ("longtable" (env "longtable")) yuuji@78: ("ascmac" (env "screen" "boxnote" "shadebox" "itembox") yuuji@78: (maketitle "return" "Return" "yen") yuuji@79: (section "keytop") ("mask") ("maskbox")) yuuji@193: ("bm" (section "bm")) ;by aoyamale.chiba-u.ac.jp yuuji@78: yuuji@78: ("alltt" (env "alltt")) yuuji@78: ("misc" (section "verbfile" "listing")) yuuji@441: ("verbatim" (section "verbatiminput")) yuuji@80: ("eclbkbox" (env "breakbox")) yuuji@80: ("supertabular" (env "supertabular")) yuuji@80: ("amsmath" (env . YaTeX-package-ams-envs) yuuji@80: (section "tag" "tag*")) yuuji@564: ("amsart" (same-as . "amsmath")) yuuji@564: ("amsbook" (same-as . "amsmath")) yuuji@564: ("amsproc" (same-as . "amsmath")) yuuji@134: ("amssymb" (maketitle "leqq" "geqq" "mathbb" "mathfrak" yuuji@305: "fallingdotseq" "therefore" "because" yuuji@460: "varDelta" "varTheta" "varLambda" "varXi" "varPi" yuuji@460: "varSigma" "varUpsilon" "varPhi" "varPsi" "varOmega" yuuji@134: "lll" "ggg")) ;very few. Please tell us! yuuji@565: ("latexsym" (maketitle "mho" "Join" "Box" "Diamond" "leadsto" yuuji@565: "sqsubset" "sqsupset" "lhd" "unlhd" "rhd" "unrhd")) yuuji@456: ("mathrsfs" (section "mathscr")) yuuji@80: ("graphicx" (section "includegraphics" yuuji@80: "rotatebox" "scalebox" "resizebox" "reflectbox") yuuji@80: (option . YaTeX-package-graphics-driver-alist)) yuuji@565: ("xymtex" (section "Ycyclohexaneh")) ;;XXX we need more and more... yuuji@565: ("chemist" nil) ;;XXX we need completions... yuuji@565: ("a4j" nil) yuuji@565: ("array" nil) yuuji@565: ("times" nil) yuuji@565: ("newtx" nil) yuuji@565: ("makeidx" nil) yuuji@565: ("geometry" (section "geometry")) yuuji@565: ("lscape" (env "landscape")) yuuji@565: ("path" (section "path")) yuuji@565: ("epsf" (section "epsfbox")) yuuji@565: ("epsfig" (section "epsfig")) yuuji@565: ("floatflt" (env "floatingfigure")) yuuji@565: ("type1cm" (section "fontsize")) yuuji@516: ("svg" (section "includesvg")) yuuji@80: ("color" (section "textcolor" "colorbox" "pagecolor" "color") yuuji@303: (option . YaTeX-package-graphics-driver-alist) yuuji@303: (default-option . "usenames,dvipsnames")) yuuji@303: ("xcolor" (same-as . "color")) yuuji@80: ("ulem" (section "uline" "uuline" "uwave") yuuji@80: (option ("normalem"))) yuuji@312: ("multicol" (env "multicols")) yuuji@565: ("cleveref" (section "cref" "crefrange" "cpageref" "labelcref" yuuji@565: "labelpageref")) yuuji@346: ("wrapfig" (env "wrapfigure" "wraptable")) yuuji@418: ("setspace" (env "spacing") (section "setstretch")) yuuji@449: ("cases" (env "numcases" "subnumcases")) yuuji@551: ("subfigure" (section "subfigure")) yuuji@554: ("okumacro" (section "ruby" "kenten")) yuuji@314: ) yuuji@79: "Default package vs. macro list. yuuji@79: Alists contains '(PACKAGENAME . MACROLIST) yuuji@79: PACKAGENAME Basename of package(String). yuuji@79: MACROLIST List of '(TYPE . MACROS) yuuji@79: TYPE One of 'env, 'section or 'maketitle according to completion-type yuuji@79: MACROS List of macros yuuji@79: yuuji@80: If TYPE is 'option, its cdr is alist of completion candidates for that yuuji@80: package. Its cdr can be a symbol whose value is alist. yuuji@80: yuuji@79: An good example is the value of YaTeX-package-alist-default.") yuuji@78: yuuji@80: (defvar YaTeX-package-graphics-driver-alist yuuji@303: '(("dvips") ("dvipsnames") ("usenames") yuuji@333: ("xdvi") ("dvipdfmx") ("pdftex") ("dvipsone") ("dviwindo") yuuji@80: ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin") yuuji@80: ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex")) yuuji@553: "Drivers alist of graphics/color stylefile's supporting devices. yuuji@80: This list is taken from yuuji@80: %% graphics.dtx Copyright (C) 1994 David Carlisle Sebastian Rahtz yuuji@80: %% Copyright (C) 1995 1996 1997 1998 David Carlisle yuuji@80: as of 2004/1/19. Thanks.") yuuji@80: yuuji@78: (defvar YaTeX-package-alist-private nil yuuji@78: "*User defined package vs. macro list. See also YaTeX-package-alist-default") yuuji@78: yuuji@78: (defun YaTeX-package-lookup (macro &optional type) yuuji@78: "Look up a package which contains a definition of MACRO. yuuji@78: Optional second argument TYPE limits the macro type. yuuji@78: TYPE is a symbol, one of 'env, 'section, 'maketitle." yuuji@78: (let ((list (append YaTeX-package-alist-private YaTeX-package-alist-default)) yuuji@304: origlist element x sameas val pkg pkglist r) yuuji@304: (setq origlist list) yuuji@78: (while list yuuji@78: (setq element (car list) yuuji@78: pkg (car element) yuuji@78: element (cdr element)) yuuji@304: (if (setq sameas (assq 'same-as element)) ;non-recursive retrieval yuuji@304: (setq element (cdr (assoc (cdr sameas) origlist)))) yuuji@78: (if (setq r (catch 'found yuuji@78: (while element yuuji@80: (setq x (car element) yuuji@80: val (cdr x)) yuuji@80: (if (symbolp val) (setq val (symbol-value val))) yuuji@80: (and (or (null type) yuuji@78: (eq type (car x))) yuuji@80: (YaTeX-member macro val) yuuji@78: (throw 'found (car x))) ;car x is type yuuji@78: (setq element (cdr element))))) yuuji@78: (setq pkglist (cons (cons pkg r) pkglist))) yuuji@78: (setq list (cdr list))) yuuji@78: pkglist)) yuuji@78: yuuji@303: (defun YaTeX-package-option-lookup (pkg &optional key) yuuji@80: "Look up options for specified pkg and returne them in alist form. yuuji@303: Just only associng against the alist of YaTeX-package-alist-*" yuuji@303: (let*((list (append YaTeX-package-alist-private YaTeX-package-alist-default)) yuuji@303: (l (cdr (assq (or key 'option) (assoc pkg list)))) yuuji@303: (recur (cdr (assq 'same-as (assoc pkg list))))) yuuji@303: (cond yuuji@303: (recur (YaTeX-package-option-lookup recur key)) yuuji@303: ((symbolp l) (symbol-value l)) yuuji@303: (t l)))) yuuji@80: yuuji@78: (defvar YaTeX-package-resolved-list nil yuuji@78: "List of macros whose package is confirmed to be loaded.") yuuji@78: yuuji@356: (defun YaTeX-package-auto-usepackage (macro type &optional autopkg autoopt) yuuji@78: "(Semi)Automatically add the \\usepackage line to main-file. yuuji@356: Search the usepackage for MACRO of the TYPE. yuuji@356: Optional second and third argument AUTOPKG, AUTOOPT are selected yuuji@356: without query. Thus those two argument (Full)automatically add yuuji@356: a \\usepackage line." yuuji@78: (let ((cb (current-buffer)) yuuji@78: (wc (current-window-configuration)) yuuji@78: (usepackage (concat YaTeX-ec "usepackage")) yuuji@80: (pkglist (YaTeX-package-lookup macro type)) yuuji@78: (usepkgrx (concat yuuji@78: YaTeX-ec-regexp yuuji@78: "\\(usepackage\\|include\\)\\b")) yuuji@353: (register (function yuuji@353: (lambda () (set-buffer cb) yuuji@78: (set (make-local-variable 'YaTeX-package-resolved-list) yuuji@353: (cons macro YaTeX-package-resolved-list))))) yuuji@78: (begdoc (concat YaTeX-ec "begin{document}")) yuuji@80: pb pkg optlist (option "") mb0 uspkgargs) yuuji@78: (if (or (YaTeX-member macro YaTeX-package-resolved-list) yuuji@78: (null pkglist)) yuuji@78: nil ;nothing to do yuuji@78: ;; Search `usepackage' into main-file yuuji@78: (YaTeX-visit-main t) ;set buffer to parent file yuuji@78: (setq pb (current-buffer)) yuuji@78: (save-excursion yuuji@78: (save-restriction yuuji@78: (if (catch 'found yuuji@78: (goto-char (point-min)) yuuji@78: (YaTeX-search-active-forward ;if search fails, goto eob yuuji@78: begdoc YaTeX-comment-prefix nil 1) yuuji@80: (while ;(YaTeX-re-search-active-backward yuuji@80: ;usepkgrx YaTeX-comment-prefix nil t) yuuji@80: ;;allow commented out \usepackages 2004/3/16 yuuji@80: (re-search-backward usepkgrx nil t) yuuji@78: (setq mb0 (match-beginning 0)) yuuji@78: (skip-chars-forward "^{") yuuji@80: (setq uspkgargs (YaTeX-buffer-substring yuuji@80: (point) yuuji@80: (progn yuuji@80: ;;(forward-list 1) is more precise, yuuji@80: ;; but higher risk. yuuji@80: (skip-chars-forward "^}\n")(point)))) yuuji@78: (let ((pl pkglist)) yuuji@78: (while pl ;(car pl)'s car is package, cdr is type yuuji@80: (if (string-match yuuji@80: (concat "[{,]\\s *" yuuji@80: (regexp-quote (car (car pl))) yuuji@80: "\\>") yuuji@80: uspkgargs) yuuji@78: (throw 'found t)) yuuji@78: (setq pl (cdr pl))) yuuji@78: (goto-char mb0)))) yuuji@78: ;;corresponding \usepackage found yuuji@78: (funcall register) yuuji@78: ;; not found, insert it. yuuji@356: (if (or yuuji@356: autopkg yuuji@356: (y-or-n-p yuuji@356: (format "`%s' requires package. Put \\usepackage now?" yuuji@356: macro))) yuuji@78: (progn yuuji@80: (require 'yatexadd) yuuji@78: (setq pkg yuuji@356: (or autopkg yuuji@356: (completing-read yuuji@356: "Load which package?(TAB for list): " yuuji@356: pkglist nil nil yuuji@356: ;;initial input yuuji@356: (if (= (length pkglist) 1) yuuji@356: (let ((w (car (car pkglist)))) yuuji@356: (if YaTeX-emacs-19 (cons w 0) w))))) yuuji@80: optlist yuuji@80: (YaTeX-package-option-lookup pkg)) yuuji@80: (if optlist yuuji@80: (let ((minibuffer-completion-table optlist) yuuji@303: (delim ",") (w (car (car optlist))) yuuji@303: (dflt (YaTeX-package-option-lookup yuuji@303: pkg 'default-option))) yuuji@80: (setq option yuuji@356: (or yuuji@356: autoopt yuuji@356: (read-from-minibuffer yuuji@356: (format "Any option for {%s}?: " pkg) yuuji@356: (let ((v (or dflt yuuji@356: (and (= (length optlist) 1) w)))) yuuji@356: (and v (if YaTeX-emacs-19 (cons v 0) v))) yuuji@356: YaTeX-minibuffer-completion-map)) yuuji@80: option (if (string< "" option) yuuji@80: (concat "[" option "]") yuuji@80: "")))) yuuji@78: (set-buffer pb) yuuji@78: (goto-char (point-min)) yuuji@78: (if (YaTeX-re-search-active-forward yuuji@78: (concat YaTeX-ec-regexp yuuji@78: "document\\(style\\|class\\){") yuuji@78: YaTeX-comment-prefix nil t) yuuji@78: (forward-line 1)) yuuji@78: (if (YaTeX-search-active-forward yuuji@78: begdoc YaTeX-comment-prefix nil t) yuuji@78: (goto-char (match-beginning 0))) yuuji@78: (insert yuuji@78: usepackage yuuji@80: (format "%s{%s}\t%% required for `\\%s' (yatex added)\n" yuuji@80: option pkg macro)) yuuji@78: (funcall register)) yuuji@120: (funcall register) yuuji@78: (message "Don't forget to put \\usepackage{%s} yourself later" yuuji@78: (car (car pkglist)))) ;doing car car is negligence... yuuji@565: )))))) yuuji@565: yuuji@565: (defvar YaTeX::usepackage-alist-private nil yuuji@565: "*Private completion list of the argument for usepackage") yuuji@565: yuuji@565: (defvar YaTeX::usepackage-alist-local nil yuuji@565: "Directory local completion list of the argument for usepackage") yuuji@565: yuuji@565: (defun YaTeX::usepackage (&optional argp) yuuji@565: (cond yuuji@565: ((equal argp 1) yuuji@565: (setq YaTeX-env-name "document") yuuji@565: (let ((minibuffer-local-completion-map YaTeX-minibuffer-completion-map) yuuji@565: (delim ",")) yuuji@565: (YaTeX-cplread-with-learning yuuji@565: (if YaTeX-japan "Use package(カンマで区切ってOK): " yuuji@565: "Use package(delimitable by comma): ") yuuji@565: ;; 'YaTeX::usepackage-alist-default ;; OBSOLETED at 1.82 yuuji@565: 'YaTeX-package-alist-default yuuji@565: 'YaTeX::usepackage-alist-private yuuji@565: 'YaTeX::usepackage-alist-local))))) yuuji@565: yuuji@565: yuuji@565: ;;; yuuji@565: ;; Add-ins for auxiliary package handled here yuuji@565: ;;; yuuji@565: (defun YaTeX:floatingfigure () yuuji@565: (concat (YaTeX:read-position "rlpv") yuuji@565: (YaTeX:read-length "Width: "))) yuuji@565: yuuji@565: (defvar YaTeX:geometry-default "margin=1.5cm,includeheadfoot,includemp" yuuji@565: "*Default options for \\geometry{}") yuuji@565: (defun YaTeX::geometry (argp) yuuji@565: "Add-in for \\geometry's option" yuuji@565: ;; cf. https://dayinthelife.at.webry.info/201401/article_2.html yuuji@565: (cond yuuji@565: ((= argp 1) yuuji@565: (YaTeX-help "geometry") yuuji@565: (message "Change default by setting YaTeX:geometry-default") yuuji@565: (if (string= YaTeX:geometry-default "") "" yuuji@565: YaTeX:geometry-default)))) yuuji@565: yuuji@565: (provide 'yatexpkg)