| 1 |
;;; xdvi-search.el --- (X)Emacs frontend for forward search with xdvi(k) |
| 2 |
;;; Requires xdvi(k) >= 22.38. |
| 3 |
;;; |
| 4 |
;;; See http://xdvi.sourceforge.net/inverse-search.html and the section |
| 5 |
;;; `FORWARD SEARCH' in the xdvi man page for more information on forward search. |
| 6 |
;;; |
| 7 |
;;; This file is available from: http://xdvi.sourceforge.net/xdvi-search.el |
| 8 |
;;; |
| 9 |
;;; Copyright (C) 2004 - 2006 Stefan Ulrich |
| 10 |
;;; Copyright (C) 2006, Chris Stucchio (minor modifications) |
| 11 |
;;; $Revision: 1.37 $, tested with Emacs 20.4 to 21.2 and Xemacs 21.1 to 21.5 |
| 12 |
;;; |
| 13 |
;;; This program is free software; you can redistribute it and/or |
| 14 |
;;; modify it under the terms of the GNU General Public License |
| 15 |
;;; as published by the Free Software Foundation; either version 2 |
| 16 |
;;; of the License, or (at your option) any later version. |
| 17 |
;;; |
| 18 |
;;; This program is distributed in the hope that it will be useful, |
| 19 |
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
;;; GNU General Public License for more details. |
| 22 |
;;; |
| 23 |
;;; You should have received a copy of the GNU General Public License |
| 24 |
;;; along with this program; if not, write to the Free Software |
| 25 |
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 26 |
;;; |
| 27 |
;;; |
| 28 |
;;; Commentary: |
| 29 |
;; |
| 30 |
;; Usage: |
| 31 |
;; |
| 32 |
;; - Add this file to some place where Emacs can find it |
| 33 |
;; (e.g. put it into a directory ~/emacs/, which you add to your |
| 34 |
;; load path by putting the following line into your .emacs file: |
| 35 |
;; |
| 36 |
;; (add-to-list 'load-path (expand-file-name "~/emacs/")) |
| 37 |
;; |
| 38 |
;; Then, add the following line to your .emacs file: |
| 39 |
;; |
| 40 |
;; (require 'xdvi-search) |
| 41 |
;; |
| 42 |
;; After compiling your .tex file with source specials activated, you should |
| 43 |
;; be able to use |
| 44 |
;; M-x xdvi-jump-to-line |
| 45 |
;; to make xdvi jump to the current location of the cursor. |
| 46 |
;; |
| 47 |
;; You could also bind this command to a key, e.g. by adding |
| 48 |
;; a binding to tex-mode-hook: |
| 49 |
;; |
| 50 |
;; (add-hook 'tex-mode-hook (lambda () |
| 51 |
;; (local-set-key "\C-c\C-j" 'xdvi-jump-to-line))) |
| 52 |
;; |
| 53 |
;; |
| 54 |
;; (without AucTeX; use LaTeX-mode-hook instead of tex-mode-hook with AucTeX. |
| 55 |
;; Note that with AucTeX, C-c C-j is already bound to LaTeX-insert-item, so |
| 56 |
;; you might want to use a different key combination instead.) |
| 57 |
;; |
| 58 |
;; - Note that with xdvik < 22.74.2, you may need to use one of the |
| 59 |
;; functions xdvi-jump-to-line-fullpath() or xdvi-jump-to-line-relpath() |
| 60 |
;; instead of xdvi-jump-to-line(), depending on the type of paths contained |
| 61 |
;; in your \include{} commands. See the docstring of xdvi-jump-to-line() |
| 62 |
;; for more information. |
| 63 |
;; |
| 64 |
;; Please send bug reports, improvements etc. to |
| 65 |
;; <[email protected]>. |
| 66 |
;; |
| 67 |
|
| 68 |
|
| 69 |
;;; Code: |
| 70 |
|
| 71 |
;;; |
| 72 |
;;; Customizable stuff |
| 73 |
;;; |
| 74 |
|
| 75 |
(defgroup xdvi-search nil |
| 76 |
"Support for inverse search with (La)TeX and DVI previewers." |
| 77 |
:group 'languages) |
| 78 |
|
| 79 |
(defcustom explicit-shell-file-name nil |
| 80 |
"*If non-nil, file name to use for explicitly requested inferior shell." |
| 81 |
:type '(choice (const :tag "nil" nil) file) |
| 82 |
:group 'xdvi-search) |
| 83 |
|
| 84 |
(defcustom xdvi-shell-buffer-name nil |
| 85 |
"*If non-nil, file name to use for explicitly requested inferior shell." |
| 86 |
:type '(choice (const :tag "nil" nil) file) |
| 87 |
:group 'xdvi-search) |
| 88 |
|
| 89 |
|
| 90 |
(defcustom xdvi-bin "xdvi" |
| 91 |
"*Name of the xdvi executable." |
| 92 |
:type '(choice (const "xdvi") file) |
| 93 |
:group 'xdvi-search) |
| 94 |
|
| 95 |
(defcustom xdvi-logfile "~/.xdvi-log" |
| 96 |
"*Write xdvi output to this file, or throw away output if set to nil." |
| 97 |
:type '(choice (const :tag "nil" nil) file) |
| 98 |
:group 'xdvi-search) |
| 99 |
|
| 100 |
|
| 101 |
;;; |
| 102 |
;;; global variables/constants |
| 103 |
;;; |
| 104 |
(defvar xdvi-version-number 0 |
| 105 |
"*Version of xdvi(k) you're using. |
| 106 |
Only set this to override the built-in version recognition if that fails. |
| 107 |
The version number is computed as: major * 1000000 + minor * 1000 + patchlevel, |
| 108 |
where alphabetic characters are ignored; e.g. 22040000 equals version 22.40c; |
| 109 |
22077002 equals version 22.77.2.") |
| 110 |
|
| 111 |
(defvar xdvi-is-xdvik nil |
| 112 |
"Whether we're really using xdvik instead of xdvi.") |
| 113 |
|
| 114 |
;;; |
| 115 |
;;; exported functions |
| 116 |
;;; |
| 117 |
;;;###autoload |
| 118 |
(defun xdvi-jump-to-line-fullpath () |
| 119 |
"See `xdvi-jump-to-line' for documentation." |
| 120 |
(interactive) |
| 121 |
(xdvi-jump-to-line 3)) |
| 122 |
|
| 123 |
;;;###autoload |
| 124 |
(defun xdvi-jump-to-line-relpath () |
| 125 |
"See `xdvi-jump-to-line' for documentation." |
| 126 |
(interactive) |
| 127 |
(xdvi-jump-to-line 2)) |
| 128 |
|
| 129 |
;;;###autoload |
| 130 |
(defun xdvi-jump-to-line (flag) |
| 131 |
"Call xdvi to perform a `forward search' for current file and line number. |
| 132 |
The optional argument FLAG controls the path searching behaviour; see also |
| 133 |
'xdvi-jump-to-line-fullpath' and 'xdvi-jump-to-line-relpath'. |
| 134 |
|
| 135 |
Xdvi needs three pieces of information for forward search: |
| 136 |
|
| 137 |
- the current line number |
| 138 |
- the name of the current input file |
| 139 |
- the name of the master .tex file. |
| 140 |
|
| 141 |
The `master' .tex file (following AucTeX's terminology) is the same |
| 142 |
as `\jobname' and determines the name of the DVI file. It is either |
| 143 |
obtained by using AucTeX's function `TeX-master-file' or, in case |
| 144 |
AucTeX isn't available, by calling `xdvi-master-file-name', a function |
| 145 |
which tries to mimick AucTeX's behaviour to obtain this filename. |
| 146 |
|
| 147 |
For the current input file name, we need to rely on the DVI viewer to |
| 148 |
make a smart matching of path suffixes with the file names that |
| 149 |
actually occur in the specials, since specials inserted by TeX's |
| 150 |
`-src' option may contain absolute or relative paths, depending on |
| 151 |
whether TeX is called with a relative or absolute filename. (Similarly |
| 152 |
for srcltx.sty, which can't expand paths.) |
| 153 |
|
| 154 |
Note to users of older versions of xdvik (< 22.74.2), or non-k xdvi: |
| 155 |
|
| 156 |
There are various ways of specifying an input file path for LaTeX's |
| 157 |
\\input{} commands (which will end up in the source specials): |
| 158 |
|
| 159 |
(1) Filename only: \\input{chapter1} |
| 160 |
(2) Relative path name: \\input{chapters/chapter1} |
| 161 |
(3) Full path name: \\input{/home/user/tex/diss/chapters/chapter1} |
| 162 |
|
| 163 |
Versions of xdvik < 22.74.2 and plain xdvi will not match pathname suffixes. |
| 164 |
With these versions, `xdvi-jump-to-line' will only work with variant (1); |
| 165 |
for (2) or (3) you will need to use one of `xdvi-jump-to-line-relpath' (for 2) |
| 166 |
or `xdvi-jump-to-line-fullpath' (for 3) instead." |
| 167 |
(interactive "P") |
| 168 |
(save-excursion |
| 169 |
(save-restriction |
| 170 |
(widen) |
| 171 |
(beginning-of-line 1) |
| 172 |
(if (not flag) |
| 173 |
(setq flag 1)) |
| 174 |
(let* (;;; Current line in file. |
| 175 |
;;; `count-lines' yields different results at beginning and in the |
| 176 |
;;; middle of a line, so we normalize by going to BOL first and |
| 177 |
;;; then adding 1 |
| 178 |
(curr-line (+ 1 (count-lines (point-min) (point)))) |
| 179 |
;;; name of master .tex file, to be used as DVI basename: |
| 180 |
(abspath (file-name-directory buffer-file-name)) |
| 181 |
(master-file (if (fboundp 'TeX-master-file) |
| 182 |
(TeX-master-file t) |
| 183 |
(xdvi-get-masterfile (xdvi-master-file-name)))) |
| 184 |
(xdvi-version-info (xdvi-get-version-number xdvi-version-number)) |
| 185 |
;;; DVI file name: |
| 186 |
(dvi-file (concat (file-name-sans-extension master-file) ".dvi")) |
| 187 |
;;; Current source filename |
| 188 |
(filename (get-source-filename (buffer-file-name) flag))) |
| 189 |
(if (not (xdvi-has-inverse-search (car xdvi-version-info))) |
| 190 |
(error "Your xdvi version is too old; see http://xdvi.sourceforge.net/inverse-search.html for more information")) |
| 191 |
(if (not (file-exists-p dvi-file)) |
| 192 |
(message "File %s does not exist; maybe you need to run latex first?" dvi-file) |
| 193 |
(save-window-excursion |
| 194 |
(if (xdvi-has-nofork (car xdvi-version-info) (car (cdr xdvi-version-info))) |
| 195 |
(progn |
| 196 |
(setq proc |
| 197 |
(start-process "xdvi" "*xdvi output*" |
| 198 |
xdvi-bin |
| 199 |
"-nofork" |
| 200 |
"-sourceposition" |
| 201 |
(concat (int-to-string curr-line) " " filename) |
| 202 |
dvi-file)) |
| 203 |
(set-process-sentinel proc 'xdvi-process-sentinel)) |
| 204 |
;;; |
| 205 |
;;; More complicated if we don't have nofork: start a |
| 206 |
;;; subshell, since otherwise there's no way of viewing |
| 207 |
;;; the stderr/stdout output of the xdvi child process |
| 208 |
;;; without having either xdvi freezing, Emacs freezing, |
| 209 |
;;; or Emacs killing the xdvi child process (which is |
| 210 |
;;; forked when running with -sourceposition). |
| 211 |
;;; |
| 212 |
;;; Obtain the type of I/O-redirection needed for current shell: |
| 213 |
(let* ((default-shell |
| 214 |
(or (and (boundp 'explicit-shell-file-name) explicit-shell-file-name) |
| 215 |
(getenv "ESHELL") |
| 216 |
(getenv "SHELL") |
| 217 |
"/bin/sh")) |
| 218 |
(shell-redirection |
| 219 |
(cond ((string-match "/bin/t?csh" default-shell) |
| 220 |
(list ">&" "")) |
| 221 |
;; bash/ksh/sh |
| 222 |
((list ">" "2>&1"))))) |
| 223 |
(cond ((string-match "XEmacs" emacs-version) |
| 224 |
(xdvi-shell)) |
| 225 |
((< emacs-major-version 21) |
| 226 |
(xdvi-shell)) |
| 227 |
((shell (or xdvi-shell-buffer-name "*xdvi-shell*")))) |
| 228 |
(comint-send-input) |
| 229 |
(insert (concat "cd " abspath ";") ) |
| 230 |
(insert xdvi-bin |
| 231 |
" -sourceposition '" (int-to-string curr-line) " " filename "' " |
| 232 |
dvi-file |
| 233 |
" " |
| 234 |
;;; we could probably do without the logfile, but it's an easy |
| 235 |
;;; way for toggling between /dev/null and logging. |
| 236 |
(if xdvi-logfile |
| 237 |
(let ((xdvi-logfile-fullname (expand-file-name xdvi-logfile))) |
| 238 |
;;; to avoid problems with `noclobber' |
| 239 |
(if (file-exists-p xdvi-logfile-fullname) |
| 240 |
(delete-file xdvi-logfile-fullname)) |
| 241 |
(car shell-redirection) |
| 242 |
" " |
| 243 |
xdvi-logfile-fullname |
| 244 |
" " |
| 245 |
(car (cdr shell-redirection)) |
| 246 |
) |
| 247 |
) |
| 248 |
" &") |
| 249 |
(if xdvi-logfile |
| 250 |
(message "Writing xdvi output to \"%s\"." xdvi-logfile)) |
| 251 |
(comint-send-input))))))))) |
| 252 |
|
| 253 |
;;; |
| 254 |
;;; internal functions |
| 255 |
;;; |
| 256 |
(defun shell-with-name (name) |
| 257 |
"This function opens a shell in a buffer with name given by the argument, and switches to that buffer. If the buffer already exists, we simply switch to it." |
| 258 |
(let ((buffer-of-name (get-buffer name)) |
| 259 |
(tempname (generate-new-buffer-name "*tempshell*") ) ) |
| 260 |
(cond ((bufferp buffer-of-name) ;If the buffer exists, switch to it (assume it is a shell) |
| 261 |
(switch-to-buffer name)) |
| 262 |
( (bufferp (get-buffer "*shell*")) |
| 263 |
(progn |
| 264 |
(shell) |
| 265 |
(rename-buffer tempname) |
| 266 |
(shell) |
| 267 |
(rename-buffer name) |
| 268 |
(switch-to-buffer tempname) |
| 269 |
(rename-buffer "*shell*") |
| 270 |
(switch-to-buffer name))) |
| 271 |
( t |
| 272 |
(progn |
| 273 |
(shell) |
| 274 |
(rename-buffer name) |
| 275 |
) |
| 276 |
) |
| 277 |
) |
| 278 |
) |
| 279 |
) |
| 280 |
|
| 281 |
(defun xdvi-shell () |
| 282 |
"Behaves just like function shell, but creates the shell in buffer named *xdvi-shell* (unless of course the user chooses to modify the xdvi-shell-buffer-name variable.)" |
| 283 |
(shell-with-name (or xdvi-shell-buffer-name "*xdvi-shell*") ) |
| 284 |
) |
| 285 |
|
| 286 |
(defun xdvi-has-inverse-search (version) |
| 287 |
"Return True if inverse search is available for this VERSION of xdvi(k)." |
| 288 |
(>= version 22038000)) |
| 289 |
|
| 290 |
(defun xdvi-has-nofork (version is-xdvik) |
| 291 |
"Return True if the '-nofork' option is available for this VERSION of xdvi(k). |
| 292 |
This option was implemented in version 22.74.1 of xdvik, and 22.61 of plain xdvi. |
| 293 |
Argument IS-XDVIK controls whether we're running xdvik or plain xdvi." |
| 294 |
(if is-xdvik |
| 295 |
(>= version 22074001) |
| 296 |
(>= version 22061000))) |
| 297 |
|
| 298 |
(defun get-source-filename (fname flag) |
| 299 |
"Helper function to extract the file name from the file name FNAME. |
| 300 |
FLAG controls whether to use the full path or a relative path." |
| 301 |
(if (> flag 1) |
| 302 |
(if (> flag 2) |
| 303 |
;;; full path name, for use with TeX patch and when using the |
| 304 |
;;; full path in \input or \include with srcltx.sty: |
| 305 |
(expand-file-name fname) |
| 306 |
;;; relative path name: if current path and path of master file match partly, |
| 307 |
;;; use the rest of this path; else use buffer name. This can treat both the |
| 308 |
;;; cases when a relative path is used in \input{}, or when just the filename |
| 309 |
;;; is used and the file is located in the current directory (suggested by |
| 310 |
;;; [email protected]). |
| 311 |
(if (string-match (concat "^" (regexp-quote (file-name-directory master-file))) |
| 312 |
fname) |
| 313 |
(substring fname (match-end 0)) |
| 314 |
fname)) |
| 315 |
;;; buffer file name without path: |
| 316 |
(file-name-nondirectory fname))) |
| 317 |
|
| 318 |
(defun xdvi-get-version-number (default) |
| 319 |
"Return a tuple of (version-number xdvi-is-xdvik), or (DEFAULT nil) if DEFAULT != 0." |
| 320 |
(if (not (= default 0)) |
| 321 |
(list default nil) |
| 322 |
(save-window-excursion |
| 323 |
(switch-to-buffer "*xdvi-version*") |
| 324 |
(erase-buffer) |
| 325 |
(call-process xdvi-bin nil "*xdvi-version*" nil "-version") |
| 326 |
(goto-char (point-min)) |
| 327 |
(if (not (re-search-forward "xdvi\\((?k)?\\)? version \\([0-9]+\\)\\.\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" nil t)) |
| 328 |
(error "Unable to get xdvi version number - please check value of `xdvi-bin', or set it manually via `xdvi-version-number'") |
| 329 |
(let* ((major (string-to-int (buffer-substring (match-beginning 2) (match-end 2)))) |
| 330 |
(minor (string-to-int (buffer-substring (match-beginning 3) (match-end 3)))) |
| 331 |
(patchlevel (if (match-beginning 5) |
| 332 |
(string-to-int (buffer-substring (match-beginning 5) (match-end 5))) |
| 333 |
0)) |
| 334 |
(xdvi-is-xdvik (if (match-beginning 1) t nil)) |
| 335 |
(version (+ (* 1000 minor) (* 1000000 major) patchlevel))) |
| 336 |
(kill-buffer "*xdvi-version*") |
| 337 |
;;; (message "Xdvi%s version: major %d, minor %d, version %d" (if xdvi-is-xdvik "k" "") major minor version) |
| 338 |
(list version xdvi-is-xdvik) |
| 339 |
))))) |
| 340 |
|
| 341 |
(defun xdvi-process-sentinel (process signal) |
| 342 |
"Return a readable error message for subprocess PROCESS which has terminated with SIGNAL." |
| 343 |
(let ((retmsg (substring signal 0 -1))) |
| 344 |
(if (string-match "abnormally" retmsg) |
| 345 |
(progn |
| 346 |
(message "XDvi: %s" retmsg) |
| 347 |
(pop-to-buffer (process-buffer process))) |
| 348 |
(message "XDvi: %s" retmsg)))) |
| 349 |
|
| 350 |
(defun xdvi-get-masterfile (file) |
| 351 |
"Small helper function for AucTeX compatibility. |
| 352 |
Converts the special value t that TeX-master might be set to (Argument FILE) |
| 353 |
into a real file name." |
| 354 |
(if (eq file t) |
| 355 |
(buffer-file-name) |
| 356 |
file)) |
| 357 |
|
| 358 |
|
| 359 |
(defun xdvi-master-file-name () |
| 360 |
"Emulate AucTeX's TeX-master-file function. |
| 361 |
Partly copied from tex.el's TeX-master-file and TeX-add-local-master." |
| 362 |
(if (boundp 'TeX-master) |
| 363 |
TeX-master |
| 364 |
(let ((master-file (read-file-name "Master file (default this file): "))) |
| 365 |
(if (y-or-n-p "Save info as local variable? ") |
| 366 |
(progn |
| 367 |
(goto-char (point-max)) |
| 368 |
(if (re-search-backward "^\\([^\n]+\\)Local Variables:" nil t) |
| 369 |
(let* ((prefix (if (match-beginning 1) |
| 370 |
(buffer-substring (match-beginning 1) (match-end 1)) |
| 371 |
"")) |
| 372 |
(start (point))) |
| 373 |
(re-search-forward (regexp-quote (concat prefix "End:")) nil t) |
| 374 |
(if (re-search-backward (regexp-quote (concat prefix "TeX-master")) start t) |
| 375 |
;;; if TeX-master line exists already, replace it |
| 376 |
(progn |
| 377 |
(beginning-of-line 1) |
| 378 |
(kill-line 1)) |
| 379 |
(beginning-of-line 1)) |
| 380 |
(insert prefix "TeX-master: " (prin1-to-string master-file) "\n")) |
| 381 |
(insert "\n%%% Local Variables: " |
| 382 |
;;; mode is of little use without AucTeX ... |
| 383 |
;;; "\n%%% mode: " (substring (symbol-name major-mode) 0 -5) |
| 384 |
"\n%%% TeX-master: " (prin1-to-string master-file) |
| 385 |
"\n%%% End: \n")) |
| 386 |
(save-buffer) |
| 387 |
(message "(local variables written.)")) |
| 388 |
(message "(nothing written.)")) |
| 389 |
(set (make-local-variable 'TeX-master) master-file)))) |
| 390 |
|
| 391 |
|
| 392 |
(provide 'xdvi-search) |
| 393 |
|
| 394 |
;;; page break to avoid "Local variables entry is missing the prefix" error for previous stuff |
| 395 |
|
| 396 |
|
| 397 |
;;; xdvi-search.el ends here |