Initial Commit
[packages] / xemacs-packages / auctex / style / graphicx.el
1 ;;; graphicx.el --- AUCTeX style file for graphicx.sty
2
3 ;; Copyright (C) 2000, 2004, 2005 by Free Software Foundation, Inc.
4
5 ;; Author: Ryuichi Arafune <arafune@debian.org>
6 ;; Created: 1999/3/20
7 ;; Keywords: tex
8
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
13
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;;  This package supports the includegraphcics macro in graphicx style.
27
28 ;; Acknowledgements
29 ;;  Dr. Thomas Baumann <thomas.baumann@ch.tum.de>
30 ;;  David Kastrup <David.Kastrup@t-online.de>
31 ;;  Masayuki Akata <ataka@milk.freemail.ne.jp>
32
33 ;;; Code:
34
35 (TeX-add-style-hook
36  "graphicx"
37  (lambda ()
38    (TeX-add-symbols
39     '("reflectbox" "Argument")
40     '("resizebox" "Width" "Height" "Argument")
41     '("resizebox*" "Width" "Total height" "Argument")
42     '("rotatebox" [ "Options" ] "Angle" "Argument")
43     '("scalebox" "Horizontal scale" [ "Vertical scale" ] "Argument")
44     '("includegraphics" LaTeX-arg-includegraphics))
45    ;; Fontification
46    (when (and (featurep 'font-latex)
47               (eq TeX-install-font-lock 'font-latex-setup))
48      (font-latex-add-keywords '(("reflectbox" "{")
49                                 ("resizebox" "*{{{")
50                                 ("rotatebox" "[{{")
51                                 ("scalebox" "{[{"))
52                               'textual)
53      (font-latex-add-keywords '(("includegraphics" "*[[{")) 'reference)))
54  LaTeX-dialect)
55
56 (defun LaTeX-includegraphics-extensions (&optional list)
57   "Return appropriate extensions for input files to \\includegraphics."
58   ;; FIXME: This function may check for latex/pdflatex later.
59   (concat "\\."
60           (mapconcat 'identity
61                      (or list LaTeX-includegraphics-extensions)
62                      "$\\|\\.")
63           "$"))
64
65 (defun LaTeX-includegraphics-read-file-TeX ()
66   "Read image file for \\includegraphics.
67 Offers all graphic files found in the TeX search path.  See
68 `LaTeX-includegraphics-read-file' for more."
69   ;; Drop latex/pdflatex differences for now.  Might be (re-)included later.
70   (completing-read
71    "Image file: "
72    (TeX-delete-dups-by-car
73     (mapcar 'list
74             (TeX-search-files nil LaTeX-includegraphics-extensions t t)))
75    nil nil nil))
76
77 (defun LaTeX-includegraphics-read-file-relative ()
78   "Read image file for \\includegraphics.
79
80 Lists all graphic files in the master directory and its
81 subdirectories and inserts the relative file name.  This option
82 doesn't works with Emacs 21.3 or XEmacs.  See
83 `LaTeX-includegraphics-read-file' for more."
84   (file-relative-name
85    (read-file-name
86     "Image file: " nil nil nil nil
87     ;; FIXME: Emacs 21.3 and XEmacs 21.4.15 dont have PREDICATE as the sixth
88     ;; argument (Emacs 21.3: five args; XEmacs 21.4.15: sixth is HISTORY).
89     (lambda (fname)
90       (or (file-directory-p fname)
91           (string-match (LaTeX-includegraphics-extensions) fname))))
92    (TeX-master-directory)))
93
94 (defun LaTeX-arg-includegraphics (prefix)
95   "Ask for mandantory and optional arguments for the \\includegraphics command.
96
97 The extent of the optional arguments is determined by the prefix argument and
98 `LaTeX-includegraphics-options-alist'."
99   (let* ((maybe-left-brace "[")
100          (maybe-comma "")
101          show-hint
102          (image-file (funcall LaTeX-includegraphics-read-file))
103          (incl-opts
104           (cond
105            ((numberp
106              (if (listp current-prefix-arg)
107                  (setq current-prefix-arg (car current-prefix-arg))
108                current-prefix-arg))
109             (cdr
110              (assq current-prefix-arg LaTeX-includegraphics-options-alist)))
111            ;; If no prefix is given, use `0' and tell the user about the
112            ;; prefix.
113            ((eq current-prefix-arg nil)
114             (setq show-hint t)
115             (cdr (assq 0 LaTeX-includegraphics-options-alist)))
116            (t
117             (cdr (assq 0 LaTeX-includegraphics-options-alist)))))
118          ;; Order the optional aruments like in the tables in epslatex.ps,
119          ;; page 14.  But collect y-or-n options at the end, so that the use
120          ;; can skip some options by typing `RET RET ... RET n n n ... n'
121          ;;
122          ;; Options from Table 1 (epslatex.ps, page 14):
123          (totalheight
124           (TeX-arg-maybe
125            'totalheight incl-opts
126            '(read-string
127              (concat "Total Height (" TeX-default-unit-for-image "): "))))
128          (height
129           (TeX-arg-maybe
130            'height incl-opts
131            ;; Either totalheight or height make sense:
132            '(when (zerop (length totalheight))
133               (read-string
134                (concat "Figure height (" TeX-default-unit-for-image "): ")))))
135          (width
136           (TeX-arg-maybe
137            'width incl-opts
138            '(read-string
139              (concat "Figure width (" TeX-default-unit-for-image "): "))))
140          (scale
141           (TeX-arg-maybe
142            'angle incl-opts
143            ;; If size is already specified, don't ask for scale:
144            '(when (zerop (+ (length totalheight)
145                             (length height)
146                             (length width)))
147               (read-string "Scale: "))))
148          (angle
149           (TeX-arg-maybe
150            'angle incl-opts
151            '(read-string "Rotation angle: ")))
152          (origin
153           (TeX-arg-maybe
154            'origin incl-opts
155            '(read-string
156              (concat
157               "Origin (any combination of `lcr' (horizontal) "
158               "and `tcbB' (vertical)): "))))
159          (page ;; Not in any table; Only for PDF.
160           (TeX-arg-maybe
161            'page incl-opts
162            '(read-string "Page: ")))
163          (bb
164           (TeX-arg-maybe
165            'bb incl-opts
166            '(y-or-n-p "Set Bounding Box? ")))
167          ;; Table 2:
168          (viewport
169           (TeX-arg-maybe
170            'viewport incl-opts
171            '(y-or-n-p "Set viewport? ")))
172          (trim
173           (TeX-arg-maybe
174            'trim incl-opts
175            '(and (not viewport)
176                  (y-or-n-p "Set trim? "))))
177          ;; Table 3:
178          (clip
179           (TeX-arg-maybe
180            'clip incl-opts
181            ;; If viewport, we also use clip.
182            '(or viewport
183                 (y-or-n-p "Clipping figure? "))))
184          (keepaspectratio
185           (TeX-arg-maybe
186            'keepaspectratio incl-opts
187            ;; If we have width and [total]height...
188            '(or (and (not (zerop (length width)))
189                      (or (not (zerop (length totalheight)))
190                          (not (zerop (length height)))))
191                 (y-or-n-p "Keep Aspectratio? "))))
192          ;; Used for bb, trim, viewport, ...:
193          llx lly urx ury)
194     ;; Now insert stuff...
195     (when (not (zerop (length totalheight)))
196       (insert
197        maybe-left-brace maybe-comma "totalheight="
198        (car (TeX-string-divide-number-unit totalheight))
199        (if (zerop
200             (length
201              (car (cdr (TeX-string-divide-number-unit totalheight)))))
202            TeX-default-unit-for-image
203          (car (cdr (TeX-string-divide-number-unit totalheight)))))
204       (setq maybe-comma ",")
205       (setq maybe-left-brace ""))
206     (when (not (zerop (length height)))
207       (insert maybe-left-brace maybe-comma
208               "height=" (car (TeX-string-divide-number-unit height))
209               (if (zerop
210                    (length
211                     (car (cdr (TeX-string-divide-number-unit height)))))
212                   TeX-default-unit-for-image
213                 (car (cdr (TeX-string-divide-number-unit height)))))
214       (setq maybe-comma ",")
215       (setq maybe-left-brace ""))
216     (when (not (zerop (length width)))
217       (insert maybe-left-brace maybe-comma
218               "width=" (car (TeX-string-divide-number-unit width))
219               (if (zerop
220                    (length
221                     (car (cdr (TeX-string-divide-number-unit width)))))
222                   TeX-default-unit-for-image
223                 (car (cdr (TeX-string-divide-number-unit width)))))
224       (setq maybe-comma ",")
225       (setq maybe-left-brace ""))
226     (when (not (zerop (length scale)))
227       (insert maybe-left-brace maybe-comma "scale=" scale)
228       (setq maybe-comma ",")
229       (setq maybe-left-brace ""))
230     (when (not (zerop (length angle)))
231       (insert maybe-left-brace maybe-comma "angle=" angle)
232       (setq maybe-comma ",")
233       (setq maybe-left-brace ""))
234     (when (not (zerop (length origin)))
235       (insert maybe-left-brace maybe-comma "origin=" origin)
236       (setq maybe-comma ",")
237       (setq maybe-left-brace ""))
238     (when bb
239       (setq llx (read-string "Bounding Box lower left x: "))
240       (setq lly (read-string "Bounding Box lower left y: "))
241       (setq urx (read-string "Bounding Box upper right x: "))
242       (setq ury (read-string "Bounding Box upper right y: "))
243       (insert maybe-left-brace maybe-comma
244               "bb=" llx " " lly " " urx " " ury)
245       (setq maybe-comma ",")
246       (setq maybe-left-brace ""))
247     ;;
248     (when viewport
249       (setq llx (read-string "Viewport lower left x: "))
250       (setq lly (read-string "Viewport lower left y: "))
251       (setq urx (read-string "Viewport upper right x: "))
252       (setq ury (read-string "Viewport upper right y: "))
253       (insert maybe-left-brace maybe-comma
254               "viewport=" llx " " lly " " urx " " ury)
255       (setq maybe-comma ",")
256       (setq maybe-left-brace ""))
257     (when trim
258       (setq llx (read-string "Trim lower left x: "))
259       (setq lly (read-string "Trim lower left y: "))
260       (setq urx (read-string "Trim Upper right x: "))
261       (setq ury (read-string "Trim Upper right y: "))
262       (insert maybe-left-brace maybe-comma
263               "trim=" llx " " lly " " urx " " ury)
264       (setq maybe-comma ",")
265       (setq maybe-left-brace ""))
266     ;;
267     (when clip
268       (insert maybe-left-brace maybe-comma "clip")
269       (setq maybe-comma ",")
270       (setq maybe-left-brace ""))
271     (when keepaspectratio
272       (insert maybe-left-brace maybe-comma "keepaspectratio")
273       (setq maybe-comma ",")
274       (setq maybe-left-brace ""))
275     ;;
276     (when (not (zerop (length page)))
277       (insert maybe-left-brace maybe-comma "page=" page)
278       (setq maybe-comma ",")
279       (setq maybe-left-brace ""))
280     ;;
281     (if (zerop (length maybe-left-brace))
282         (insert "]"))
283     (TeX-insert-braces 0)
284     (insert
285      (if LaTeX-includegraphics-strip-extension-flag
286          ;; We don't have `replace-regexp-in-string' in all (X)Emacs versions:
287          (with-temp-buffer
288            (insert image-file)
289            (goto-char (point-max))
290            (when (search-backward-regexp (LaTeX-includegraphics-extensions)
291                                          nil t 1)
292              (replace-match ""))
293            (buffer-string))
294        image-file))
295     (when show-hint
296       (message
297        (concat
298         "Adding `C-u C-u' before the command asks for more optional arguments."
299         "\nSee `LaTeX-includegraphics-options-alist' for details."))
300       (sit-for 3))
301     t))
302
303 ;;; graphicx.el ends here