yuuji@78: ;;; -*- Emacs-Lisp -*- yuuji@78: ;;; YaTeX package manager yuuji@78: ;;; yatexpkg.el yuuji@134: ;;; (c)2003-2010 by HIROSE, Yuuji [yuuji@yatex.org] yuuji@144: ;;; Last modified Sat Sep 11 15:50:40 2010 on firestorm yuuji@78: ;;; $Id$ yuuji@78: 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@79: '(("version" (env "comment") ;by tsuchiya@pine.kuee.kyoto-u.ac.jp yuuji@79: (section "includeversion" "excludeversion")) yuuji@78: yuuji@78: ("plext" (section "bou")) ;by yas.axis@ma.mni.ne.jp yuuji@78: yuuji@78: ("url" (section "url")) ;by fujieda@jaist.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@78: ("bm" (section "bm")) ;by aoyama@le.chiba-u.ac.jp yuuji@78: yuuji@78: ("alltt" (env "alltt")) yuuji@78: ("misc" (section "verbfile" "listing")) yuuji@80: ("eclbkbox" (env "breakbox")) yuuji@80: ("supertabular" (env "supertabular")) yuuji@80: ("amsmath" (env . YaTeX-package-ams-envs) yuuji@80: (section "tag" "tag*")) yuuji@134: ("amssymb" (maketitle "leqq" "geqq" "mathbb" "mathfrak" yuuji@144: "fallingdotseq" yuuji@134: "lll" "ggg")) ;very few. Please tell us! yuuji@80: ("graphicx" (section "includegraphics" yuuji@80: "rotatebox" "scalebox" "resizebox" "reflectbox") yuuji@80: (option . YaTeX-package-graphics-driver-alist)) yuuji@80: ("color" (section "textcolor" "colorbox" "pagecolor" "color") yuuji@80: (option . YaTeX-package-graphics-driver-alist)) yuuji@80: ("ulem" (section "uline" "uuline" "uwave") yuuji@80: (option ("normalem"))) yuuji@80: ("multicol" (env "multicols"))) 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@80: '(("dvips") ("xdvi") ("dvipdf") ("pdftex") ("dvipsone") ("dviwindo") yuuji@80: ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin") yuuji@80: ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex")) yuuji@80: "Drivers alist of graphics/color stylefile's supporting deveces. 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@80: element x val pkg pkglist r) yuuji@78: (while list yuuji@78: (setq element (car list) yuuji@78: pkg (car element) yuuji@78: element (cdr element)) 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@80: (defun YaTeX-package-option-lookup (pkg) yuuji@80: "Look up options for specified pkg and returne them in alist form. yuuji@80: Just only accocing against the alist of YaTeX-package-alist-*" yuuji@80: (let ((l (cdr (assq 'option yuuji@80: (assoc pkg (append YaTeX-package-alist-private yuuji@80: YaTeX-package-alist-default)))))) yuuji@80: (if (symbolp l) (symbol-value l) 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@78: (defun YaTeX-package-auto-usepackage (macro type) yuuji@78: "(Semi)Automatically add the \\usepackage line to main-file. yuuji@78: Search the usepackage for MACRO of the TYPE." 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@78: (register '(lambda () (set-buffer cb) yuuji@78: (set (make-local-variable 'YaTeX-package-resolved-list) yuuji@78: (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@78: (if (y-or-n-p yuuji@78: (format "`%s' requires package. Put \\usepackage now?" macro)) yuuji@78: (progn yuuji@80: (require 'yatexadd) yuuji@78: (setq pkg yuuji@78: (completing-read yuuji@78: "Load which package?(TAB for list): " yuuji@79: pkglist nil nil yuuji@79: ;;initial input yuuji@79: (if (= (length pkglist) 1) yuuji@79: (let ((w (car (car pkglist)))) yuuji@80: (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@80: (delim ",") (w (car (car optlist)))) yuuji@80: (setq option yuuji@80: (read-from-minibuffer yuuji@80: (format "Any option for {%s}?: " pkg) yuuji@80: (if (= (length optlist) 1) yuuji@80: (if YaTeX-emacs-19 (cons w 0) w)) yuuji@80: 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@78: ))))))