Initial Commit
[packages] / xemacs-packages / auctex / style / pdfsync.el
1 ;;; pdfsync.el --- AUCTeX style for `pdfsync.sty'
2
3 ;; Copyright (C) 2005, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2005-12-28
8 ;; Keywords: tex
9
10 ;; This file is part of AUCTeX.
11
12 ;; AUCTeX is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; AUCTeX is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
24 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 ;; 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This file adds support for `pdfsync.sty'.
30
31 ;;; Code:
32
33 (defun LaTeX-pdfsync-output-page ()
34   "Return page number in output file corresponding to buffer position."
35   (let* ((line (TeX-line-number-at-pos))
36          (master (TeX-active-master))
37          (file (file-name-sans-extension
38                 (file-relative-name (buffer-file-name)
39                                     (file-name-directory master))))
40          (pdfsync-file (concat master ".pdfsync"))
41          (buf-live-p (get-file-buffer pdfsync-file))
42          (sync-record "0")
43          (sync-line "-1")
44          (sync-page "1")
45          last-match)
46     (when (file-exists-p pdfsync-file)
47       (with-current-buffer (find-file-noselect pdfsync-file)
48         (save-restriction
49           (goto-char (point-min))
50           ;; Narrow region to file in question.
51           (when (not (string= file master))
52             (re-search-forward (concat "^(" file "\\(.tex\\)?$") nil t)
53             (let ((beg (match-beginning 0)))
54               (goto-char beg)
55               (narrow-to-region (line-beginning-position 2)
56                                 (progn (forward-sexp) (point))))
57             (goto-char (point-min)))
58           ;; Look for the record number.
59           (catch 'break
60             (while (re-search-forward "^(\\|^l \\([0-9]+\\) \\([0-9]+\\)" nil t)
61               (cond ((string= (match-string 0) "(")
62                      (goto-char (match-beginning 0))
63                      (forward-sexp))
64                     ((> (string-to-number (match-string 2)) line)
65                      (throw 'break nil))
66                     (t
67                      (setq sync-record (match-string 1)
68                            sync-line (match-string 2)
69                            last-match (match-beginning 0))))))
70           ;; Look for the page number.
71           (goto-char (or last-match (point-min)))
72           ;; There might not be any p or s lines for the current file,
73           ;; so make it possible to search further.
74           (widen)
75           (catch 'break
76             (while (re-search-forward "^p \\([0-9]+\\)" nil t)
77               (when (>= (string-to-number (match-string 1))
78                         (string-to-number sync-record))
79                 (re-search-backward "^s \\([0-9]+\\)" nil t)
80                 (setq sync-page (match-string 1))
81                 (throw 'break nil)))))
82         ;; Kill the buffer if it was loaded by us.
83         (unless buf-live-p (kill-buffer (current-buffer)))))
84     sync-page))
85
86 (TeX-add-style-hook
87  "pdfsync"
88  (lambda ()
89    (setq TeX-source-correlate-output-page-function 'LaTeX-pdfsync-output-page))
90  LaTeX-dialect)
91
92 ;;; pdfsync.el ends here