summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/pdfsync.el
blob: 122f0eb95301e3cfceafb612b1ed80a38e23f056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;;; pdfsync.el --- AUCTeX style for `pdfsync.sty'  -*- lexical-binding: t; -*-

;; Copyright (C) 2005-2021  Free Software Foundation, Inc.

;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2005-12-28
;; Keywords: tex

;; This file is part of AUCTeX.

;; AUCTeX is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; AUCTeX is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with AUCTeX; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301, USA.

;;; Commentary:

;; This file adds support for `pdfsync.sty'.

;;; Code:

(require 'tex)

(defun LaTeX-pdfsync-output-page ()
  "Return page number in output file corresponding to buffer position."
  (let* ((line (line-number-at-pos))
         (master (TeX-active-master))
         (file (file-name-sans-extension
                (file-relative-name (buffer-file-name)
                                    (file-name-directory master))))
         (pdfsync-file (concat master ".pdfsync"))
         (buf-live-p (get-file-buffer pdfsync-file))
         (sync-record "0")
         (sync-page "1")
         last-match)
    (when (file-exists-p pdfsync-file)
      (with-current-buffer (find-file-noselect pdfsync-file)
        (save-restriction
          (goto-char (point-min))
          ;; Narrow region to file in question.
          (when (not (string= file master))
            (re-search-forward (concat "^(" file "\\(.tex\\)?$") nil t)
            (let ((beg (match-beginning 0)))
              (goto-char beg)
              (narrow-to-region (line-beginning-position 2)
                                (progn (forward-sexp) (point))))
            (goto-char (point-min)))
          ;; Look for the record number.
          (catch 'break
            (while (re-search-forward "^(\\|^l \\([0-9]+\\) \\([0-9]+\\)" nil t)
              (cond ((string= (match-string 0) "(")
                     (goto-char (match-beginning 0))
                     (forward-sexp))
                    ((> (string-to-number (match-string 2)) line)
                     (throw 'break nil))
                    (t
                     (setq sync-record (match-string 1)
                           last-match (match-beginning 0))))))
          ;; Look for the page number.
          (goto-char (or last-match (point-min)))
          ;; There might not be any p or s lines for the current file,
          ;; so make it possible to search further.
          (widen)
          (catch 'break
            (while (re-search-forward "^p \\([0-9]+\\)" nil t)
              (when (>= (string-to-number (match-string 1))
                        (string-to-number sync-record))
                (re-search-backward "^s \\([0-9]+\\)" nil t)
                (setq sync-page (match-string 1))
                (throw 'break nil)))))
        ;; Kill the buffer if it was loaded by us.
        (unless buf-live-p (kill-buffer (current-buffer)))))
    sync-page))

(TeX-add-style-hook
 "pdfsync"
 (lambda ()
   (setq TeX-source-correlate-output-page-function #'LaTeX-pdfsync-output-page))
 TeX-dialect)

;;; pdfsync.el ends here