yatex

view yatexpkg.el @ 270:1b4e0acd0106

Include newpage.rb.
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 10 May 2012 11:10:13 +0900
parents e216f9f347fa
children 5921f28ef77c
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX package manager
3 ;;; yatexpkg.el
4 ;;; (c)2003-2012 by HIROSE, Yuuji [yuuji@yatex.org]
5 ;;; Last modified Mon Jan 9 20:13:53 2012 on firestorm
6 ;;; $Id$
8 (defvar YaTeX-package-ams-envs
9 (mapcar 'car YaTeX-ams-env-table))
11 (defvar YaTeX-package-alist-default
12 '(("version" (env "comment") ;by tsuchiya<at>pine.kuee.kyoto-u.ac.jp
13 (section "includeversion" "excludeversion"))
15 ("plext" (section "bou")) ;by yas.axis<at>ma.mni.ne.jp
17 ("url" (section "url")) ;by fujieda<at>jaist.ac.jp
19 ("fancybox" (section "shadowbox" "doublebox" "ovalbox" "Ovalbox"))
20 ("slashbox" (section "slashbox" "backslashbox"))
21 ("pifont" (section "ding"))
22 ("longtable" (env "longtable"))
23 ("ascmac" (env "screen" "boxnote" "shadebox" "itembox")
24 (maketitle "return" "Return" "yen")
25 (section "keytop") ("mask") ("maskbox"))
26 ("bm" (section "bm")) ;by aoyama<at>le.chiba-u.ac.jp
28 ("alltt" (env "alltt"))
29 ("misc" (section "verbfile" "listing"))
30 ("eclbkbox" (env "breakbox"))
31 ("supertabular" (env "supertabular"))
32 ("amsmath" (env . YaTeX-package-ams-envs)
33 (section "tag" "tag*"))
34 ("amssymb" (maketitle "leqq" "geqq" "mathbb" "mathfrak"
35 "fallingdotseq"
36 "lll" "ggg")) ;very few. Please tell us!
37 ("graphicx" (section "includegraphics"
38 "rotatebox" "scalebox" "resizebox" "reflectbox")
39 (option . YaTeX-package-graphics-driver-alist))
40 ("color" (section "textcolor" "colorbox" "pagecolor" "color")
41 (option . YaTeX-package-graphics-driver-alist))
42 ("ulem" (section "uline" "uuline" "uwave")
43 (option ("normalem")))
44 ("multicol" (env "multicols")))
45 "Default package vs. macro list.
46 Alists contains '(PACKAGENAME . MACROLIST)
47 PACKAGENAME Basename of package(String).
48 MACROLIST List of '(TYPE . MACROS)
49 TYPE One of 'env, 'section or 'maketitle according to completion-type
50 MACROS List of macros
52 If TYPE is 'option, its cdr is alist of completion candidates for that
53 package. Its cdr can be a symbol whose value is alist.
55 An good example is the value of YaTeX-package-alist-default.")
57 (defvar YaTeX-package-graphics-driver-alist
58 '(("dvips") ("xdvi") ("dvipdf") ("pdftex") ("dvipsone") ("dviwindo")
59 ("emtex") ("dviwin") ("oztex") ("textures") ("pctexps") ("pctexwin")
60 ("pctexhp") ("pctex32") ("truetex") ("tcidvi") ("vtex"))
61 "Drivers alist of graphics/color stylefile's supporting deveces.
62 This list is taken from
63 %% graphics.dtx Copyright (C) 1994 David Carlisle Sebastian Rahtz
64 %% Copyright (C) 1995 1996 1997 1998 David Carlisle
65 as of 2004/1/19. Thanks.")
67 (defvar YaTeX-package-alist-private nil
68 "*User defined package vs. macro list. See also YaTeX-package-alist-default")
70 (defun YaTeX-package-lookup (macro &optional type)
71 "Look up a package which contains a definition of MACRO.
72 Optional second argument TYPE limits the macro type.
73 TYPE is a symbol, one of 'env, 'section, 'maketitle."
74 (let ((list (append YaTeX-package-alist-private YaTeX-package-alist-default))
75 element x val pkg pkglist r)
76 (while list
77 (setq element (car list)
78 pkg (car element)
79 element (cdr element))
80 (if (setq r (catch 'found
81 (while element
82 (setq x (car element)
83 val (cdr x))
84 (if (symbolp val) (setq val (symbol-value val)))
85 (and (or (null type)
86 (eq type (car x)))
87 (YaTeX-member macro val)
88 (throw 'found (car x))) ;car x is type
89 (setq element (cdr element)))))
90 (setq pkglist (cons (cons pkg r) pkglist)))
91 (setq list (cdr list)))
92 pkglist))
94 (defun YaTeX-package-option-lookup (pkg)
95 "Look up options for specified pkg and returne them in alist form.
96 Just only accocing against the alist of YaTeX-package-alist-*"
97 (let ((l (cdr (assq 'option
98 (assoc pkg (append YaTeX-package-alist-private
99 YaTeX-package-alist-default))))))
100 (if (symbolp l) (symbol-value l) l)))
102 (defvar YaTeX-package-resolved-list nil
103 "List of macros whose package is confirmed to be loaded.")
105 (defun YaTeX-package-auto-usepackage (macro type)
106 "(Semi)Automatically add the \\usepackage line to main-file.
107 Search the usepackage for MACRO of the TYPE."
108 (let ((cb (current-buffer))
109 (wc (current-window-configuration))
110 (usepackage (concat YaTeX-ec "usepackage"))
111 (pkglist (YaTeX-package-lookup macro type))
112 (usepkgrx (concat
113 YaTeX-ec-regexp
114 "\\(usepackage\\|include\\)\\b"))
115 (register '(lambda () (set-buffer cb)
116 (set (make-local-variable 'YaTeX-package-resolved-list)
117 (cons macro YaTeX-package-resolved-list))))
118 (begdoc (concat YaTeX-ec "begin{document}"))
119 pb pkg optlist (option "") mb0 uspkgargs)
120 (if (or (YaTeX-member macro YaTeX-package-resolved-list)
121 (null pkglist))
122 nil ;nothing to do
123 ;; Search `usepackage' into main-file
124 (YaTeX-visit-main t) ;set buffer to parent file
125 (setq pb (current-buffer))
126 (save-excursion
127 (save-restriction
128 (if (catch 'found
129 (goto-char (point-min))
130 (YaTeX-search-active-forward ;if search fails, goto eob
131 begdoc YaTeX-comment-prefix nil 1)
132 (while ;(YaTeX-re-search-active-backward
133 ;usepkgrx YaTeX-comment-prefix nil t)
134 ;;allow commented out \usepackages 2004/3/16
135 (re-search-backward usepkgrx nil t)
136 (setq mb0 (match-beginning 0))
137 (skip-chars-forward "^{")
138 (setq uspkgargs (YaTeX-buffer-substring
139 (point)
140 (progn
141 ;;(forward-list 1) is more precise,
142 ;; but higher risk.
143 (skip-chars-forward "^}\n")(point))))
144 (let ((pl pkglist))
145 (while pl ;(car pl)'s car is package, cdr is type
146 (if (string-match
147 (concat "[{,]\\s *"
148 (regexp-quote (car (car pl)))
149 "\\>")
150 uspkgargs)
151 (throw 'found t))
152 (setq pl (cdr pl)))
153 (goto-char mb0))))
154 ;;corresponding \usepackage found
155 (funcall register)
156 ;; not found, insert it.
157 (if (y-or-n-p
158 (format "`%s' requires package. Put \\usepackage now?" macro))
159 (progn
160 (require 'yatexadd)
161 (setq pkg
162 (completing-read
163 "Load which package?(TAB for list): "
164 pkglist nil nil
165 ;;initial input
166 (if (= (length pkglist) 1)
167 (let ((w (car (car pkglist))))
168 (if YaTeX-emacs-19 (cons w 0) w))))
169 optlist
170 (YaTeX-package-option-lookup pkg))
171 (if optlist
172 (let ((minibuffer-completion-table optlist)
173 (delim ",") (w (car (car optlist))))
174 (setq option
175 (read-from-minibuffer
176 (format "Any option for {%s}?: " pkg)
177 (if (= (length optlist) 1)
178 (if YaTeX-emacs-19 (cons w 0) w))
179 YaTeX-minibuffer-completion-map)
180 option (if (string< "" option)
181 (concat "[" option "]")
182 ""))))
183 (set-buffer pb)
184 (goto-char (point-min))
185 (if (YaTeX-re-search-active-forward
186 (concat YaTeX-ec-regexp
187 "document\\(style\\|class\\){")
188 YaTeX-comment-prefix nil t)
189 (forward-line 1))
190 (if (YaTeX-search-active-forward
191 begdoc YaTeX-comment-prefix nil t)
192 (goto-char (match-beginning 0)))
193 (insert
194 usepackage
195 (format "%s{%s}\t%% required for `\\%s' (yatex added)\n"
196 option pkg macro))
197 (funcall register))
198 (funcall register)
199 (message "Don't forget to put \\usepackage{%s} yourself later"
200 (car (car pkglist)))) ;doing car car is negligence...
201 ))))))