yatex

annotate yatexprc.el @ 263:808f532d68d3

Inhibit auto-rerun of bibtex when using latexmk.
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 17 Feb 2012 22:36:35 +0900
parents 15ec86ff549c
children 5921f28ef77c
rev   line source
yuuji@51 1 ;;; -*- Emacs-Lisp -*-
yuuji@51 2 ;;; YaTeX process handler.
yuuji@51 3 ;;; yatexprc.el
yuuji@195 4 ;;; (c)1993-2012 by HIROSE Yuuji.[yuuji@yatex.org]
yuuji@263 5 ;;; Last modified Fri Feb 17 22:35:38 2012 on firestorm
yuuji@51 6 ;;; $Id$
yuuji@51 7
yuuji@64 8 ;(require 'yatex)
yuuji@64 9 (require 'yatexlib)
yuuji@51 10
yuuji@51 11 (defvar YaTeX-typeset-process nil
yuuji@69 12 "Process identifier for jlatex")
yuuji@69 13
yuuji@51 14 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
yuuji@51 15 "Process buffer for jlatex")
yuuji@51 16
yuuji@51 17 (defvar YaTeX-typeset-buffer-syntax nil
yuuji@51 18 "*Syntax table for typesetting buffer")
yuuji@51 19
yuuji@51 20 (defvar YaTeX-current-TeX-buffer nil
yuuji@51 21 "Keeps the buffer on which recently typeset run.")
yuuji@51 22
yuuji@51 23 (defvar YaTeX-shell-command-option
yuuji@51 24 (or (and (boundp 'shell-command-option) shell-command-option)
yuuji@68 25 (and (boundp 'shell-command-switch) shell-command-switch)
yuuji@51 26 (if YaTeX-dos "/c" "-c"))
yuuji@51 27 "Shell option for command execution.")
yuuji@51 28
yuuji@64 29 (defvar YaTeX-latex-message-code
yuuji@79 30 ;; (cond
yuuji@79 31 ;; (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
yuuji@79 32 ;; ((and YaTeX-emacs-20 (member 'undecided (coding-system-list))
yuuji@79 33 ;; 'undecided))
yuuji@79 34 ;; ((featurep 'mule)
yuuji@79 35 ;; (or (and (boundp '*autoconv*) *autoconv*)
yuuji@79 36 ;; (and (fboundp 'coding-system-list) 'automatic-conversion)))
yuuji@79 37 ;; ((boundp 'NEMACS)
yuuji@79 38 ;; (cdr (assq (if YaTeX-dos 1 2) YaTeX-kanji-code-alist))))
yuuji@64 39 (cond
yuuji@80 40 (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
yuuji@79 41 (YaTeX-emacs-20
yuuji@79 42 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist)))
yuuji@79 43 ((boundp 'MULE)
yuuji@79 44 (symbol-value
yuuji@79 45 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist))))
yuuji@64 46 ((boundp 'NEMACS)
yuuji@79 47 latex-message-kanji-code))
yuuji@64 48 "Process coding system for LaTeX.")
yuuji@64 49
yuuji@51 50 (if YaTeX-typeset-buffer-syntax nil
yuuji@51 51 (setq YaTeX-typeset-buffer-syntax
yuuji@51 52 (make-syntax-table (standard-syntax-table)))
yuuji@51 53 (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
yuuji@51 54 (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
yuuji@51 55 (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
yuuji@69 56 (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax))
yuuji@51 57
yuuji@212 58 (defvar YaTeX-typeset-marker nil)
yuuji@262 59 (defvar YaTeX-typeset-consumption nil)
yuuji@262 60 (make-variable-buffer-local 'YaTeX-typeset-consumption)
yuuji@213 61 (defun YaTeX-typeset (command buffer &optional prcname modename ppcmd)
yuuji@51 62 "Execute jlatex (or other) to LaTeX typeset."
yuuji@51 63 (interactive)
yuuji@51 64 (save-excursion
yuuji@60 65 (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer))
yuuji@64 66 (map YaTeX-typesetting-mode-map)
yuuji@64 67 (outcode
yuuji@64 68 (cond ((eq major-mode 'yatex-mode) YaTeX-coding-system)
yuuji@69 69 ((eq major-mode 'yahtml-mode) yahtml-kanji-code))))
yuuji@51 70 (if (and YaTeX-typeset-process
yuuji@51 71 (eq (process-status YaTeX-typeset-process) 'run))
yuuji@51 72 ;; if tex command is halting.
yuuji@51 73 (YaTeX-kill-typeset-process YaTeX-typeset-process))
yuuji@60 74 (YaTeX-put-nonstopmode)
yuuji@60 75 (setq prcname (or prcname "LaTeX")
yuuji@60 76 modename (or modename "typeset"))
yuuji@60 77 (if (eq major-mode 'yatex-mode) (YaTeX-visit-main t)) ;;execution dir
yuuji@51 78 (setq execdir default-directory)
yuuji@51 79 ;;Select lower-most window if there are more than 2 windows and
yuuji@51 80 ;;typeset buffer not seen.
yuuji@51 81 (YaTeX-showup-buffer
yuuji@51 82 buffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@51 83 (set-buffer (get-buffer-create buffer))
yuuji@51 84 (setq default-directory execdir)
yuuji@51 85 (cd execdir)
yuuji@213 86 (erase-buffer)
yuuji@51 87 (cond
yuuji@64 88 ((not (fboundp 'start-process)) ;YaTeX-dos;if MS-DOS
yuuji@51 89 (call-process
yuuji@64 90 shell-file-name nil buffer nil YaTeX-shell-command-option command))
yuuji@51 91 (t ;if UNIX
yuuji@51 92 (set-process-buffer
yuuji@51 93 (setq YaTeX-typeset-process
yuuji@60 94 (start-process prcname buffer shell-file-name
yuuji@51 95 YaTeX-shell-command-option command))
yuuji@51 96 (get-buffer buffer))
yuuji@123 97 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)
yuuji@212 98 (put 'YaTeX-typeset-process 'thiscmd command)
yuuji@212 99 (put 'YaTeX-typeset-process 'name prcname)
yuuji@262 100 (if (fboundp 'current-time)
yuuji@262 101 (setq YaTeX-typeset-consumption
yuuji@262 102 (cons (cons 'time (current-time))
yuuji@262 103 (delq 'time YaTeX-typeset-consumption))))
yuuji@123 104 (let ((ppprop (get 'YaTeX-typeset-process 'ppcmd)))
yuuji@123 105 (setq ppprop (delq (assq YaTeX-typeset-process ppprop) ppprop))
yuuji@123 106 (if ppcmd
yuuji@123 107 (setq ppprop (cons (cons YaTeX-typeset-process ppcmd) ppprop)))
yuuji@224 108 (put 'YaTeX-typeset-process 'ppcmd ppprop))
yuuji@224 109 (if (and (boundp 'bibcmd) bibcmd)
yuuji@224 110 (let ((bcprop (get 'YaTeX-typeset-process 'bibcmd)))
yuuji@224 111 (setq bcprop (cons
yuuji@224 112 (cons YaTeX-typeset-process bibcmd)
yuuji@224 113 (delq (assq YaTeX-typeset-process bcprop) bcprop)))
yuuji@224 114 (put 'YaTeX-typeset-process 'bibcmd bcprop)))))
yuuji@51 115 (message (format "Calling `%s'..." command))
yuuji@51 116 (setq YaTeX-current-TeX-buffer (buffer-name))
yuuji@60 117 (use-local-map map) ;map may be localized
yuuji@51 118 (set-syntax-table YaTeX-typeset-buffer-syntax)
yuuji@60 119 (setq mode-name modename)
yuuji@60 120 (if YaTeX-typeset-process ;if process is running (maybe on UNIX)
yuuji@64 121 (cond ((fboundp 'set-current-process-coding-system)
yuuji@51 122 (set-current-process-coding-system
yuuji@64 123 YaTeX-latex-message-code outcode))
yuuji@64 124 ((fboundp 'set-process-coding-system)
yuuji@64 125 (set-process-coding-system
yuuji@64 126 YaTeX-typeset-process YaTeX-latex-message-code outcode))
yuuji@64 127 (YaTeX-emacs-20
yuuji@64 128 (set-buffer-process-coding-system
yuuji@64 129 YaTeX-latex-message-code outcode))
yuuji@51 130 ((boundp 'NEMACS)
yuuji@51 131 (set-kanji-process-code YaTeX-latex-message-code))))
yuuji@212 132 (set-marker (or YaTeX-typeset-marker
yuuji@212 133 (setq YaTeX-typeset-marker (make-marker)))
yuuji@212 134 (point))
yuuji@212 135 (insert (format "Call `%s'\n" command))
yuuji@51 136 (if YaTeX-dos (message "Done.")
yuuji@51 137 (insert " ")
yuuji@51 138 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
yuuji@51 139 (if (bolp) (forward-line -1)) ;what for?
yuuji@51 140 (if (and YaTeX-emacs-19 window-system)
yuuji@51 141 (let ((win (get-buffer-window buffer t)) owin)
yuuji@51 142 (select-frame (window-frame win))
yuuji@51 143 (setq owin (selected-window))
yuuji@51 144 (select-window win)
yuuji@51 145 (goto-char (point-max))
yuuji@51 146 (recenter -1)
yuuji@51 147 (select-window owin))
yuuji@51 148 (select-window (get-buffer-window buffer))
yuuji@51 149 (goto-char (point-max))
yuuji@51 150 (recenter -1))
yuuji@51 151 (select-window window)
yuuji@60 152 (switch-to-buffer cb)
yuuji@69 153 (YaTeX-remove-nonstopmode))))
yuuji@51 154
yuuji@262 155 (defvar YaTeX-typeset-auto-rerun t
yuuji@262 156 "*Non-nil automatically reruns typesetter when cross-refs update found.
yuuji@262 157 This is a toy mechanism. DO NOT RELY ON THIS MECHANISM.
yuuji@262 158 You SHOULD check the integrity of cross-references with your eyes!!
yuuji@262 159 Supplying an integer to this variable inhibit compulsory call of bibtex,
yuuji@262 160 thus, it call bibtex only if warning messages about citation are seen.")
yuuji@212 161 (defvar YaTeX-typeset-rerun-msg "Rerun to get cross-references right.")
yuuji@224 162 (defvar YaTeX-typeset-citation-msg
yuuji@261 163 "Warning: Citation \`")
yuuji@51 164 (defun YaTeX-typeset-sentinel (proc mes)
yuuji@51 165 (cond ((null (buffer-name (process-buffer proc)))
yuuji@51 166 ;; buffer killed
yuuji@51 167 (set-process-buffer proc nil))
yuuji@51 168 ((memq (process-status proc) '(signal exit))
yuuji@51 169 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
yuuji@51 170 (pwin (get-buffer-window pbuf))
yuuji@212 171 (owin (selected-window)) win
yuuji@261 172 tobecalled shortname
yuuji@212 173 (thiscmd (get 'YaTeX-typeset-process 'thiscmd))
yuuji@212 174 (ppprop (get 'YaTeX-typeset-process 'ppcmd))
yuuji@224 175 (ppcmd (cdr (assq proc ppprop)))
yuuji@224 176 (bcprop (get 'YaTeX-typeset-process 'bibcmd))
yuuji@224 177 (bibcmd (cdr (assq proc bcprop))))
yuuji@212 178 (put 'YaTeX-typeset-process 'ppcmd ;erase ppcmd
yuuji@212 179 (delq (assq proc ppprop) ppprop))
yuuji@224 180 (put 'YaTeX-typeset-process 'bibcmd ;erase bibcmd
yuuji@224 181 (delq (assq proc bcprop) bcprop))
yuuji@51 182 ;; save-excursion isn't the right thing if
yuuji@51 183 ;; process-buffer is current-buffer
yuuji@51 184 (unwind-protect
yuuji@51 185 (progn
yuuji@51 186 ;; Write something in *typesetting* and hack its mode line
yuuji@51 187 (if pwin
yuuji@51 188 (select-window pwin)
yuuji@51 189 (set-buffer pbuf))
yuuji@51 190 ;;(YaTeX-showup-buffer pbuf nil t)
yuuji@51 191 (goto-char (point-max))
yuuji@51 192 (if pwin (recenter -3))
yuuji@60 193 (insert ?\n mode-name " " mes)
yuuji@51 194 (forward-char -1)
yuuji@262 195 (insert
yuuji@262 196 (format " at %s%s\n"
yuuji@262 197 (substring (current-time-string) 0 -5)
yuuji@262 198 (if (and (fboundp 'current-time) (fboundp 'float)
yuuji@262 199 (assq 'time YaTeX-typeset-consumption))
yuuji@262 200 (format
yuuji@262 201 " (%.2f secs)"
yuuji@262 202 (YaTeX-elapsed-time
yuuji@262 203 (cdr (assq 'time YaTeX-typeset-consumption))
yuuji@262 204 (current-time))))))
yuuji@51 205 (setq mode-line-process
yuuji@51 206 (concat ": "
yuuji@51 207 (symbol-name (process-status proc))))
yuuji@256 208 (message "%s %s" mode-name
yuuji@51 209 (if (eq (process-status proc) 'exit)
yuuji@51 210 "done" "ceased"))
yuuji@51 211 ;; If buffer and mode line shows that the process
yuuji@51 212 ;; is dead, we can delete it now. Otherwise it
yuuji@51 213 ;; will stay around until M-x list-processes.
yuuji@51 214 (delete-process proc)
yuuji@224 215 (if (cond
yuuji@263 216 ((or (not YaTeX-typeset-auto-rerun)
yuuji@263 217 (string-match "latexmk" thiscmd))
yuuji@263 218 nil)
yuuji@262 219 ((and bibcmd ;Call bibtex if bibcmd defined &&
yuuji@262 220 (or ; (1st call || warning found)
yuuji@262 221 (and (not (numberp YaTeX-typeset-auto-rerun))
yuuji@262 222 ; cancel call at 1st, if value is a number.
yuuji@262 223 (not (string-match "bibtex" mode-name)))
yuuji@262 224 (re-search-backward
yuuji@262 225 YaTeX-typeset-citation-msg
yuuji@262 226 YaTeX-typeset-marker t))
yuuji@262 227 (save-excursion ; && using .bbl files.
yuuji@224 228 (search-backward
yuuji@224 229 ".bbl" YaTeX-typeset-marker t)))
yuuji@262 230 ;; Always call bibtex after the first typesetting,
yuuji@262 231 ;; because bibtex doesn't warn disappeared \cite.
yuuji@262 232 ;; (Suggested by ryseto. 2012)
yuuji@262 233 ;; It is more efficient to call bibtex directly than
yuuji@262 234 ;; to call it after deep inspection on the balance
yuuji@262 235 ;; of \cite vs. \bib*'s referring all *.aux files.
yuuji@224 236 (insert "\n" YaTeX-typeset-rerun-msg "\n")
yuuji@261 237 (setq tobecalled bibcmd shortname "+bibtex"))
yuuji@261 238 ((or
yuuji@261 239 (save-excursion
yuuji@261 240 (search-backward
yuuji@261 241 YaTeX-typeset-rerun-msg YaTeX-typeset-marker t))
yuuji@261 242 (save-excursion
yuuji@261 243 (re-search-backward
yuuji@261 244 "natbib.*Rerun to get citations correct"
yuuji@261 245 YaTeX-typeset-marker t)))
yuuji@224 246 (if bibcmd
yuuji@224 247 (put 'YaTeX-typeset-process 'bibcmd
yuuji@224 248 (cons (cons (get-buffer-process pbuf) bibcmd)
yuuji@224 249 bcprop)))
yuuji@261 250 (setq tobecalled thiscmd shortname "+typeset"))
yuuji@261 251 (t
yuuji@261 252 nil)) ;no need to call any process
yuuji@212 253 (progn
yuuji@212 254 (insert
yuuji@212 255 (format
yuuji@212 256 "===!!! %s !!!===\n"
yuuji@212 257 (message "Rerun `%s' to get cross-references right"
yuuji@224 258 tobecalled)))
yuuji@224 259 (if (equal tobecalled thiscmd)
yuuji@224 260 (set-marker YaTeX-typeset-marker (point)))
yuuji@212 261 (set-process-sentinel
yuuji@212 262 (start-process
yuuji@261 263 (setq mode-name (concat mode-name shortname))
yuuji@256 264 pbuf
yuuji@224 265 shell-file-name YaTeX-shell-command-option tobecalled)
yuuji@212 266 'YaTeX-typeset-sentinel)
yuuji@212 267 (if ppcmd
yuuji@212 268 (put 'YaTeX-typeset-process 'ppcmd
yuuji@212 269 (cons (cons (get-buffer-process pbuf) ppcmd)
yuuji@224 270 ppprop)))
yuuji@224 271 (if thiscmd
yuuji@224 272 (put 'YaTeX-typeset-process 'thiscmd thiscmd)))
yuuji@212 273 ;; If ppcmd is active, call it.
yuuji@123 274 (cond
yuuji@123 275 ((and ppcmd (string-match "finish" mes))
yuuji@123 276 (insert (format "=======> Success! Calling %s\n" ppcmd))
yuuji@123 277 (setq mode-name ; set process name
yuuji@256 278 (concat
yuuji@256 279 mode-name "+"
yuuji@256 280 (substring ppcmd 0 (string-match " " ppcmd))))
yuuji@212 281 ; to reach here, 'start-process exists on this emacsen
yuuji@123 282 (set-process-sentinel
yuuji@123 283 (start-process
yuuji@123 284 mode-name
yuuji@123 285 pbuf ; Use this buffer twice.
yuuji@123 286 shell-file-name YaTeX-shell-command-option
yuuji@123 287 ppcmd)
yuuji@261 288 'YaTeX-typeset-sentinel))
yuuji@262 289 (t ;pull back original mode-name
yuuji@261 290 (setq mode-name "typeset"))))
yuuji@123 291 (forward-char 1))
yuuji@123 292 (setq YaTeX-typeset-process nil)
yuuji@51 293 ;; Force mode line redisplay soon
yuuji@51 294 (set-buffer-modified-p (buffer-modified-p))
yuuji@51 295 )
yuuji@51 296 (select-window owin)
yuuji@69 297 (set-buffer obuf)))))
yuuji@51 298
yuuji@51 299 (defvar YaTeX-texput-file "texput.tex"
yuuji@69 300 "*File name for temporary file of typeset-region.")
yuuji@51 301
yuuji@51 302 (defun YaTeX-typeset-region ()
yuuji@52 303 "Paste the region to the file `texput.tex' and execute typesetter.
yuuji@52 304 The region is specified by the rule:
yuuji@51 305 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
yuuji@51 306 (1-1)if the keyword `%#END' is found after `%#BEGIN',
yuuji@51 307 ->Assume the text between `%#BEGIN' and `%#END' as region.
yuuji@51 308 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
yuuji@51 309 ->Assume the text after `%#BEGIN' as region.
yuuji@51 310 (2)If no `%#BEGIN' usage is found before the (point),
yuuji@51 311 ->Assume the text between current (point) and (mark) as region.
yuuji@51 312 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
yuuji@51 313 operation to the region."
yuuji@51 314 (interactive)
yuuji@51 315 (save-excursion
yuuji@51 316 (let*
yuuji@51 317 ((end "") typeout ;Type out message that tells the method of cutting.
yuuji@51 318 (texput YaTeX-texput-file)
yuuji@51 319 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
yuuji@51 320 (buffer (current-buffer)) opoint preamble (subpreamble "") main
yuuji@51 321 (hilit-auto-highlight nil) ;for Emacs19 with hilit19
yuuji@52 322 reg-begin reg-end lineinfo)
yuuji@51 323
yuuji@51 324 (save-excursion
yuuji@51 325 (if (search-backward "%#BEGIN" nil t)
yuuji@51 326 (progn
yuuji@51 327 (setq typeout "--- Region from BEGIN to "
yuuji@51 328 end "the end of the buffer ---"
yuuji@51 329 reg-begin (match-end 0))
yuuji@51 330 (if (search-forward "%#END" nil t)
yuuji@51 331 (setq reg-end (match-beginning 0)
yuuji@51 332 end "END ---")
yuuji@51 333 (setq reg-end (point-max))))
yuuji@52 334 (setq typeout "=== Region from (point) to (mark) ==="
yuuji@52 335 reg-begin (point) reg-end (mark)))
yuuji@52 336 (goto-char (min reg-begin reg-end))
yuuji@52 337 (setq lineinfo (count-lines (point-min) (point-end-of-line)))
yuuji@51 338 (goto-char (point-min))
yuuji@51 339 (while (search-forward "%#REQUIRE" nil t)
yuuji@51 340 (setq subpreamble
yuuji@51 341 (concat subpreamble
yuuji@51 342 (cond
yuuji@51 343 ((eolp)
yuuji@51 344 (buffer-substring
yuuji@51 345 (match-beginning 0)
yuuji@51 346 (point-beginning-of-line)))
yuuji@51 347 (t (buffer-substring
yuuji@51 348 (match-end 0)
yuuji@51 349 (point-end-of-line))))
yuuji@51 350 "\n"))
yuuji@51 351 (goto-char (match-end 0))))
yuuji@51 352 (YaTeX-visit-main t)
yuuji@51 353 (setq main (current-buffer))
yuuji@51 354 (setq opoint (point))
yuuji@51 355 (goto-char (point-min))
yuuji@51 356 (setq
yuuji@51 357 preamble
yuuji@51 358 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
yuuji@51 359 (buffer-substring (point-min) (match-end 0))
yuuji@73 360 (concat
yuuji@73 361 (if YaTeX-use-LaTeX2e "\\documentclass{" "\\documentstyle{")
yuuji@73 362 YaTeX-default-document-style "}\n"
yuuji@73 363 "\\begin{document}")))
yuuji@51 364 (goto-char opoint)
yuuji@51 365 ;;(set-buffer buffer) ;for clarity
yuuji@133 366 (let ((hilit-auto-highlight nil) (auto-mode-alist nil)
yuuji@133 367 (magic-mode-alist nil)) ;Do not activate yatex-mode here
yuuji@52 368 (set-buffer (find-file-noselect texput)))
yuuji@51 369 ;;(find-file YaTeX-texput-file)
yuuji@51 370 (erase-buffer)
yuuji@64 371 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
yuuji@51 372 (insert "\\nonstopmode{}\n"))
yuuji@51 373 (insert preamble "\n" subpreamble "\n")
yuuji@52 374 (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo))
yuuji@51 375 (insert-buffer-substring buffer reg-begin reg-end)
yuuji@51 376 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
yuuji@51 377 (insert "\n\\end{document}\n")
yuuji@51 378 (basic-save-buffer)
yuuji@51 379 (kill-buffer (current-buffer))
yuuji@51 380 (set-buffer main) ;return to parent file or itself.
yuuji@51 381 (YaTeX-typeset cmd YaTeX-typeset-buffer)
yuuji@51 382 (switch-to-buffer buffer) ;for Emacs-19
yuuji@52 383 (put 'dvi2-command 'region t)
yuuji@52 384 (put 'dvi2-command 'file buffer)
yuuji@69 385 (put 'dvi2-command 'offset lineinfo))))
yuuji@51 386
yuuji@130 387 (defun YaTeX-typeset-environment ()
yuuji@130 388 "Typeset current math environment"
yuuji@130 389 (interactive)
yuuji@130 390 (save-excursion
yuuji@130 391 (YaTeX-mark-environment)
yuuji@130 392 (YaTeX-typeset-region)))
yuuji@130 393
yuuji@123 394 (defun YaTeX-typeset-buffer (&optional pp)
yuuji@60 395 "Typeset whole buffer.
yuuji@60 396 If %#! usage says other buffer is main text,
yuuji@51 397 visit main buffer to confirm if its includeonly list contains current
yuuji@51 398 buffer's file. And if it doesn't contain editing text, ask user which
yuuji@123 399 action wants to be done, A:Add list, R:Replace list, %:comment-out list.
yuuji@123 400 If optional argument PP given as string, PP is considered as post-process
yuuji@123 401 command and call it with the same command argument as typesetter without
yuuji@123 402 last extension.
yuuji@123 403 eg. if PP is \"dvipdfmx\", called commands as follows.
yuuji@123 404 platex foo.tex
yuuji@123 405 dvipdfmx foo
yuuji@123 406 PP command will be called iff typeset command exit successfully"
yuuji@51 407 (interactive)
yuuji@51 408 (YaTeX-save-buffers)
yuuji@51 409 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
yuuji@51 410 (mydir (file-name-directory (buffer-file-name)))
yuuji@224 411 (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd
yuuji@123 412 (cb (current-buffer)))
yuuji@224 413 (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt
yuuji@224 414 pparg (substring pparg (rindex pparg ? )) ;get last arg
yuuji@224 415 pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext
yuuji@262 416 bibcmd (or (YaTeX-get-builtin "BIBTEX") bibtex-command))
yuuji@262 417 (or (string-match "\\s " bibcmd) ;if bibcmd has no spaces,
yuuji@262 418 (setq bibcmd (concat bibcmd pparg))) ;append argument(== %#!)
yuuji@123 419 (and pp
yuuji@123 420 (stringp pp)
yuuji@224 421 (setq ppcmd (concat pp pparg)))
yuuji@51 422 (if (YaTeX-main-file-p) nil
yuuji@51 423 (save-excursion
yuuji@51 424 (YaTeX-visit-main t) ;search into main buffer
yuuji@51 425 (save-excursion
yuuji@51 426 (push-mark (point) t)
yuuji@51 427 (goto-char (point-min))
yuuji@51 428 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
yuuji@51 429 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
yuuji@51 430 (let*
yuuji@51 431 ((b (progn (skip-chars-forward "^{") (point)))
yuuji@51 432 (e (progn (skip-chars-forward "^}") (1+ (point))))
yuuji@51 433 (s (buffer-substring b e)) c
yuuji@51 434 (pardir (file-name-directory (buffer-file-name))))
yuuji@51 435 (if (string-match (concat "[{,/]" me "[,}]") s)
yuuji@51 436 nil ; Nothing to do when it's already in includeonly.
yuuji@51 437 (ding)
yuuji@51 438 (switch-to-buffer (current-buffer));Display this buffer.
yuuji@51 439 (setq
yuuji@51 440 me ;;Rewrite my name(me) to contain sub directory name.
yuuji@51 441 (concat
yuuji@51 442 (if (string-match pardir mydir) ;if mydir is child of main
yuuji@51 443 (substring mydir (length pardir)) ;cut absolute path
yuuji@51 444 mydir) ;else concat absolute path name.
yuuji@51 445 me))
yuuji@51 446 (message
yuuji@51 447 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
yuuji@51 448 me)
yuuji@51 449 (setq c (read-char))
yuuji@51 450 (cond
yuuji@51 451 ((= c ?a)
yuuji@51 452 (goto-char (1+ b))
yuuji@51 453 (insert me (if (string= s "{}") "" ",")))
yuuji@51 454 ((= c ?r)
yuuji@51 455 (delete-region (1+ b) (1- e)) (insert me))
yuuji@51 456 ((= c ?%)
yuuji@51 457 (beginning-of-line) (insert "%"))
yuuji@51 458 (t nil))
yuuji@51 459 (basic-save-buffer))))
yuuji@51 460 (exchange-point-and-mark)))
yuuji@51 461 (switch-to-buffer cb)) ;for 19
yuuji@123 462 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
yuuji@69 463 (put 'dvi2-command 'region nil)))
yuuji@51 464
yuuji@51 465 (defvar YaTeX-call-command-history nil
yuuji@51 466 "Holds history list of YaTeX-call-command-on-file.")
yuuji@51 467 (put 'YaTeX-call-command-history 'no-default t)
yuuji@86 468 (defun YaTeX-call-command-on-file (base-cmd buffer &optional file)
yuuji@171 469 "Call external command BASE-CMD in the BUFFER.
yuuji@86 470 By default, pass the basename of current file. Optional 3rd argument
yuuji@86 471 FILE changes the default file name."
yuuji@51 472 (YaTeX-save-buffers)
yuuji@171 473 (let ((default (concat base-cmd " "
yuuji@171 474 (let ((me (file-name-nondirectory
yuuji@171 475 (or file buffer-file-name))))
yuuji@171 476 (if (string-match "\\.tex" me)
yuuji@171 477 (substring me 0 (match-beginning 0))
yuuji@171 478 me)))))
yuuji@171 479 (or YaTeX-call-command-history
yuuji@171 480 (setq YaTeX-call-command-history (list default)))
yuuji@171 481 (YaTeX-typeset
yuuji@171 482 (read-string-with-history
yuuji@171 483 "Call command: "
yuuji@171 484 (car YaTeX-call-command-history)
yuuji@171 485 'YaTeX-call-command-history)
yuuji@171 486 buffer)))
yuuji@51 487
yuuji@227 488 (defvar YaTeX-call-builtin-on-file)
yuuji@227 489 (make-variable-buffer-local 'YaTeX-call-builtin-on-file)
yuuji@227 490 (defun YaTeX-call-builtin-on-file (builtin-type &optional default update)
yuuji@224 491 "Call command on file specified by BUILTIN-TYPE."
yuuji@51 492 (YaTeX-save-buffers)
yuuji@224 493 (let*((main (or YaTeX-parent-file
yuuji@227 494 (save-excursion (YaTeX-visit-main t) buffer-file-name)))
yuuji@224 495 (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.))))
yuuji@227 496 (alist YaTeX-call-builtin-on-file)
yuuji@227 497 (b-in (or (YaTeX-get-builtin builtin-type)
yuuji@227 498 (cdr (assoc builtin-type alist))))
yuuji@227 499 (command b-in))
yuuji@227 500 (if (or update (null b-in))
yuuji@227 501 (progn
yuuji@227 502 (setq command (read-string-with-history
yuuji@227 503 (format "%s command: " builtin-type)
yuuji@227 504 (or b-in
yuuji@227 505 (format "%s %s" default mainroot))
yuuji@227 506 'YaTeX-call-command-history))
yuuji@227 507 (if (or update (null b-in))
yuuji@227 508 (if (y-or-n-p "Use this command line in the future? ")
yuuji@227 509 (YaTeX-getset-builtin builtin-type command) ;keep in a file
yuuji@227 510 (setq YaTeX-call-builtin-on-file ;keep in memory
yuuji@227 511 (cons (cons builtin-type command)
yuuji@227 512 (delete (assoc builtin-type alist) alist)))))))
yuuji@224 513 (YaTeX-typeset
yuuji@227 514 command
yuuji@224 515 (format " *YaTeX-%s*" (downcase builtin-type)))))
yuuji@51 516
yuuji@51 517 (defun YaTeX-kill-typeset-process (proc)
yuuji@51 518 "Kill process PROC after sending signal to PROC.
yuuji@51 519 PROC should be process identifier."
yuuji@51 520 (cond
yuuji@68 521 ((not (fboundp 'start-process))
yuuji@68 522 (error "This system can't have concurrent process."))
yuuji@51 523 ((or (null proc) (not (eq (process-status proc) 'run)))
yuuji@51 524 (message "Typesetting process is not running."))
yuuji@51 525 (t
yuuji@51 526 (save-excursion
yuuji@51 527 (set-buffer (process-buffer proc))
yuuji@51 528 (save-excursion
yuuji@51 529 (goto-char (point-max))
yuuji@51 530 (beginning-of-line)
yuuji@51 531 (if (looking-at "\\? +$")
yuuji@51 532 (let ((mp (point-max)))
yuuji@51 533 (process-send-string proc "x\n")
yuuji@51 534 (while (= mp (point-max)) (sit-for 1))))))
yuuji@51 535 (if (eq (process-status proc) 'run)
yuuji@51 536 (progn
yuuji@51 537 (interrupt-process proc)
yuuji@69 538 (delete-process proc))))))
yuuji@51 539
yuuji@51 540 (defun YaTeX-system (command buffer)
yuuji@51 541 "Execute some command on buffer. Not a official function."
yuuji@51 542 (save-excursion
yuuji@51 543 (YaTeX-showup-buffer
yuuji@51 544 buffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@59 545 (let ((df default-directory)) ;preserve current buf's pwd
yuuji@59 546 (set-buffer (get-buffer-create buffer)) ;1.61.3
yuuji@59 547 (setq default-directory df)
yuuji@59 548 (cd df))
yuuji@51 549 (erase-buffer)
yuuji@64 550 (if (not (fboundp 'start-process))
yuuji@51 551 (call-process
yuuji@51 552 shell-file-name nil buffer nil YaTeX-shell-command-option command)
yuuji@59 553 (if (and (get-buffer-process buffer)
yuuji@59 554 (eq (process-status (get-buffer-process buffer)) 'run)
yuuji@59 555 (not
yuuji@59 556 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
yuuji@59 557 nil
yuuji@59 558 (set-process-buffer
yuuji@59 559 (start-process
yuuji@59 560 "system" buffer shell-file-name YaTeX-shell-command-option command)
yuuji@69 561 (get-buffer buffer))))))
yuuji@51 562
yuuji@72 563 (defvar YaTeX-default-paper-type "a4"
yuuji@72 564 "*Default paper type.")
yuuji@72 565 (defconst YaTeX-paper-type-alist
yuuji@72 566 '(("a4paper" . "a4")
yuuji@72 567 ("a5paper" . "a5")
yuuji@72 568 ("b4paper" . "b4")
yuuji@72 569 ("b5paper" . "b5"))
yuuji@72 570 "Holds map of options and paper types.")
yuuji@72 571 (defconst YaTeX-dvips-paper-option-alist
yuuji@72 572 '(("a4" . "-t a4")
yuuji@72 573 ("a5" . "-t a5")
yuuji@72 574 ("b4" . "-t b4")
yuuji@72 575 ("b5" . "-t b5")
yuuji@72 576 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
yuuji@72 577 ("a5r" . "-t landscape")
yuuji@72 578 ("b4r" . "-t landscape")
yuuji@72 579 ("b5r" . "-t landscape"))
yuuji@72 580 "Holds map of dvips options and paper types.")
yuuji@72 581 (defun YaTeX-get-paper-type ()
yuuji@72 582 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
yuuji@72 583 (save-excursion
yuuji@77 584 (YaTeX-visit-main t)
yuuji@72 585 (goto-char (point-min))
yuuji@77 586 (let ((opts
yuuji@77 587 (if (re-search-forward
yuuji@77 588 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
yuuji@77 589 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
yuuji@77 590 (concat
yuuji@77 591 (catch 'found-paper
yuuji@77 592 (mapcar (lambda (pair)
yuuji@79 593 (if (YaTeX-member (car pair) opts)
yuuji@77 594 (throw 'found-paper (cdr pair))))
yuuji@77 595 YaTeX-paper-type-alist)
yuuji@77 596 YaTeX-default-paper-type)
yuuji@79 597 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
yuuji@72 598
yuuji@51 599 (defvar YaTeX-preview-command-history nil
yuuji@51 600 "Holds minibuffer history of preview command.")
yuuji@51 601 (put 'YaTeX-preview-command-history 'no-default t)
yuuji@51 602 (defvar YaTeX-preview-file-history nil
yuuji@51 603 "Holds minibuffer history of file to preview.")
yuuji@51 604 (put 'YaTeX-preview-file-history 'no-default t)
yuuji@51 605 (defun YaTeX-preview (preview-command preview-file)
yuuji@51 606 "Execute xdvi (or other) to tex-preview."
yuuji@51 607 (interactive
yuuji@86 608 (let* ((command (read-string-with-history
yuuji@86 609 "Preview command: "
yuuji@86 610 (YaTeX-replace-format
yuuji@86 611 (or (YaTeX-get-builtin "PREVIEW") dvi2-command)
yuuji@86 612 "p" (format (cond
yuuji@86 613 (YaTeX-dos "-y:%s")
yuuji@86 614 (t "-paper %s"))
yuuji@86 615 (YaTeX-get-paper-type)))
yuuji@86 616 'YaTeX-preview-command-history))
yuuji@86 617 (file (read-string-with-history
yuuji@86 618 "Preview file: "
yuuji@86 619 (if (get 'dvi2-command 'region)
yuuji@86 620 (substring YaTeX-texput-file
yuuji@86 621 0 (rindex YaTeX-texput-file ?.))
yuuji@86 622 (YaTeX-get-preview-file-name command))
yuuji@86 623 'YaTeX-preview-file-history)))
yuuji@86 624 (list command file)))
yuuji@51 625 (setq dvi2-command preview-command) ;`dvi2command' is buffer local
yuuji@51 626 (save-excursion
yuuji@51 627 (YaTeX-visit-main t)
yuuji@86 628 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
yuuji@51 629 (let ((pbuffer "*dvi-preview*") (dir default-directory))
yuuji@51 630 (YaTeX-showup-buffer
yuuji@51 631 pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@51 632 (set-buffer (get-buffer-create pbuffer))
yuuji@51 633 (erase-buffer)
yuuji@51 634 (setq default-directory dir) ;for 18
yuuji@51 635 (cd dir) ;for 19
yuuji@51 636 (cond
yuuji@64 637 ((not (fboundp 'start-process)) ;if MS-DOS
yuuji@51 638 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
yuuji@51 639 (call-process shell-file-name "con" "*dvi-preview*" nil
yuuji@60 640 YaTeX-shell-command-option
yuuji@61 641 (concat preview-command " " preview-file))
yuuji@51 642 (send-string-to-terminal "\e[>5l") ;show cursor
yuuji@51 643 (redraw-display))
yuuji@69 644 ((and (string-match "dviout" preview-command) ;maybe on `kon'
yuuji@69 645 (stringp (getenv "TERM"))
yuuji@69 646 (string-match "^kon" (getenv "TERM")))
yuuji@69 647 (call-process shell-file-name "con" "*dvi-preview*" nil
yuuji@69 648 YaTeX-shell-command-option
yuuji@69 649 (concat preview-command " " preview-file)))
yuuji@51 650 (t ;if UNIX
yuuji@51 651 (set-process-buffer
yuuji@86 652 (let ((process-connection-type nil))
yuuji@86 653 (start-process "preview" "*dvi-preview*" shell-file-name
yuuji@86 654 YaTeX-shell-command-option
yuuji@86 655 (concat preview-command " " preview-file)))
yuuji@51 656 (get-buffer pbuffer))
yuuji@51 657 (message
yuuji@51 658 (concat "Starting " preview-command
yuuji@69 659 " to preview " preview-file)))))))
yuuji@51 660
yuuji@58 661 (defvar YaTeX-xdvi-remote-program "xdvi")
yuuji@58 662 (defun YaTeX-xdvi-remote-search (&optional region-mode)
yuuji@58 663 "Search string at the point on xdvi -remote window.
yuuji@58 664 Non-nil for optional argument REGION-MODE specifies the search string
yuuji@58 665 by region."
yuuji@58 666 (interactive "P")
yuuji@58 667 (let ((pb " *xdvi*") str proc)
yuuji@58 668 (save-excursion
yuuji@58 669 (if region-mode
yuuji@58 670 (setq str (buffer-substring (region-beginning) (region-end)))
yuuji@58 671 (setq str (buffer-substring
yuuji@58 672 (point)
yuuji@58 673 (progn (skip-chars-forward "^\n\\\\}") (point)))))
yuuji@58 674 (message "Searching `%s'..." str)
yuuji@58 675 (if (boundp 'MULE)
yuuji@58 676 (define-program-coding-system
yuuji@58 677 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
yuuji@58 678 *euc-japan*))
yuuji@58 679 (setq proc
yuuji@58 680 (start-process
yuuji@58 681 "xdvi" pb YaTeX-xdvi-remote-program
yuuji@58 682 "-remote" (format "SloppySearch(%s) " str)
yuuji@58 683 (concat (YaTeX-get-preview-file-name) ".dvi")))
yuuji@69 684 (message "Searching `%s'...Done" str))))
yuuji@58 685
yuuji@52 686 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
yuuji@52 687 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
yuuji@52 688 (cond
yuuji@52 689 ((and (get 'dvi2-command 'region)
yuuji@52 690 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
yuuji@52 691 (set file-sym (get 'dvi2-command 'file))
yuuji@52 692 (set line-sym
yuuji@52 693 (+ (- (apply '- (get 'dvi2-command 'offset)))
yuuji@52 694 (symbol-value line-sym)
yuuji@52 695 -1)))))
yuuji@52 696
yuuji@51 697 (defun YaTeX-prev-error ()
yuuji@230 698 "Visit position of previous typeset error or warning.
yuuji@51 699 To avoid making confliction of line numbers by editing, jump to
yuuji@51 700 error or warning lines in reverse order."
yuuji@51 701 (interactive)
yuuji@230 702 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
yuuji@230 703 (cur-win (selected-window))
yuuji@230 704 b0 bound errorp error-line typeset-win error-buffer error-win)
yuuji@51 705 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@51 706 (error "There is no typesetting buffer."))
yuuji@51 707 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@230 708 (if (and (markerp YaTeX-typeset-marker)
yuuji@230 709 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
yuuji@230 710 (setq bound YaTeX-typeset-marker))
yuuji@51 711 (setq typeset-win (selected-window))
yuuji@51 712 (if (re-search-backward
yuuji@51 713 (concat "\\(" latex-error-regexp "\\)\\|\\("
yuuji@51 714 latex-warning-regexp "\\)")
yuuji@230 715 bound t)
yuuji@80 716 (setq errorp (match-beginning 1))
yuuji@51 717 (select-window cur-win)
yuuji@68 718 (error "No more errors on %s" cur-buf))
yuuji@80 719 (goto-char (setq b0 (match-beginning 0)))
yuuji@51 720 (skip-chars-forward "^0-9" (match-end 0))
yuuji@51 721 (setq error-line
yuuji@51 722 (string-to-int
yuuji@51 723 (buffer-substring
yuuji@51 724 (point)
yuuji@51 725 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
yuuji@70 726 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
yuuji@51 727 (if (or (null error-line) (equal 0 error-line))
yuuji@51 728 (error "Can't detect error position."))
yuuji@52 729 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
yuuji@52 730 (setq error-win (get-buffer-window error-buffer))
yuuji@51 731 (select-window cur-win)
yuuji@51 732 (cond
yuuji@51 733 (error-win (select-window error-win))
yuuji@51 734 ((eq (get-lru-window) typeset-win)
yuuji@51 735 (YaTeX-switch-to-buffer error-buffer))
yuuji@51 736 (t (select-window (get-lru-window))
yuuji@51 737 (YaTeX-switch-to-buffer error-buffer)))
yuuji@51 738 (setq error-win (selected-window))
yuuji@51 739 (goto-line error-line)
yuuji@51 740 (message "LaTeX %s in `%s' on line: %d."
yuuji@80 741 (if errorp "error" "warning")
yuuji@51 742 error-buffer error-line)
yuuji@51 743 (select-window typeset-win)
yuuji@51 744 (skip-chars-backward "0-9")
yuuji@51 745 (recenter (/ (window-height) 2))
yuuji@60 746 (sit-for 1)
yuuji@80 747 (goto-char b0)
yuuji@69 748 (select-window error-win)))
yuuji@51 749
yuuji@51 750 (defun YaTeX-jump-error-line ()
yuuji@51 751 "Jump to corresponding line on latex command's error message."
yuuji@51 752 (interactive)
yuuji@51 753 (let (error-line error-file error-buf)
yuuji@51 754 (save-excursion
yuuji@51 755 (beginning-of-line)
yuuji@51 756 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
yuuji@51 757 (point-end-of-line) t)))
yuuji@51 758 (if (null error-line)
yuuji@51 759 (if (eobp) (insert (this-command-keys))
yuuji@51 760 (error "No line number expression."))
yuuji@51 761 (goto-char (match-beginning 0))
yuuji@51 762 (setq error-line (string-to-int
yuuji@51 763 (buffer-substring (match-beginning 1) (match-end 1)))
yuuji@70 764 error-file (expand-file-name
yuuji@70 765 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
yuuji@52 766 (YaTeX-set-virtual-error-position 'error-file 'error-line)
yuuji@52 767 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
yuuji@51 768 (if (null error-buf)
yuuji@51 769 (error "`%s' is not found in this directory." error-file))
yuuji@51 770 (YaTeX-showup-buffer error-buf nil t)
yuuji@69 771 (goto-line error-line)))
yuuji@51 772
yuuji@51 773 (defun YaTeX-send-string ()
yuuji@51 774 "Send string to current typeset process."
yuuji@51 775 (interactive)
yuuji@51 776 (if (and (eq (process-status YaTeX-typeset-process) 'run)
yuuji@51 777 (>= (point) (process-mark YaTeX-typeset-process)))
yuuji@51 778 (let ((b (process-mark YaTeX-typeset-process))
yuuji@51 779 (e (point-end-of-line)))
yuuji@51 780 (goto-char b)
yuuji@51 781 (skip-chars-forward " \t" e)
yuuji@51 782 (setq b (point))
yuuji@51 783 (process-send-string
yuuji@51 784 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
yuuji@51 785 (goto-char e)
yuuji@51 786 (insert "\n")
yuuji@51 787 (set-marker (process-mark YaTeX-typeset-process) (point))
yuuji@51 788 (insert " "))
yuuji@69 789 (ding)))
yuuji@51 790
yuuji@51 791 (defun YaTeX-view-error ()
yuuji@51 792 (interactive)
yuuji@51 793 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@51 794 (message "No typeset buffer found.")
yuuji@51 795 (let ((win (selected-window)))
yuuji@51 796 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@51 797 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
yuuji@51 798 ;; automatically scrolled up at typesetting.
yuuji@51 799 ;;(goto-char (point-max))
yuuji@51 800 ;;(forward-line -1)
yuuji@51 801 ;;(recenter -1)
yuuji@69 802 (select-window win))))
yuuji@51 803
yuuji@51 804 (defun YaTeX-get-error-file (default)
yuuji@51 805 "Get current processing file from typesetting log."
yuuji@51 806 (save-excursion
yuuji@51 807 (let(s)
yuuji@51 808 (condition-case () (up-list -1)
yuuji@51 809 (error
yuuji@51 810 (let ((list 0) found)
yuuji@51 811 (while
yuuji@51 812 (and (<= list 0) (not found)
yuuji@51 813 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
yuuji@51 814 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
yuuji@51 815 (setq list (1- list)) ;open paren
yuuji@51 816 (setq list (1+ list))
yuuji@51 817 (if (= list 1)
yuuji@51 818 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
yuuji@51 819 (setq found t)
yuuji@51 820 (setq list (1- list)))))))))
yuuji@51 821 (setq s
yuuji@51 822 (buffer-substring
yuuji@51 823 (progn (forward-char 1) (point))
yuuji@51 824 (progn (skip-chars-forward "^ \n" (point-end-of-line))
yuuji@51 825 (point))))
yuuji@69 826 (if (string= "" s) default s))))
yuuji@51 827
yuuji@51 828 (defun YaTeX-put-nonstopmode ()
yuuji@64 829 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
yuuji@51 830 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
yuuji@51 831 nil ;if already written in text then do nothing
yuuji@51 832 (save-excursion
yuuji@51 833 (YaTeX-visit-main t)
yuuji@51 834 (goto-char (point-min))
yuuji@60 835 (insert "\\nonstopmode{}%_YaTeX_%\n")
yuuji@69 836 (if (buffer-file-name) (basic-save-buffer))))))
yuuji@51 837
yuuji@51 838 (defun YaTeX-remove-nonstopmode ()
yuuji@64 839 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
yuuji@51 840 (save-excursion
yuuji@51 841 (YaTeX-visit-main t)
yuuji@51 842 (goto-char (point-min))
yuuji@51 843 (forward-line 1)
yuuji@51 844 (narrow-to-region (point-min) (point))
yuuji@51 845 (goto-char (point-min))
yuuji@51 846 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
yuuji@69 847 (widen))))
yuuji@51 848
yuuji@80 849 (defvar YaTeX-dvi2-command-ext-alist
yuuji@80 850 '(("[agx]dvi\\|dviout" . ".dvi")
yuuji@80 851 ("ghostview\\|gv" . ".ps")
yuuji@152 852 ("acroread\\|pdf\\|Preview\\|TeXShop\\|Skim\\|evince" . ".pdf")))
yuuji@80 853
yuuji@86 854 (defun YaTeX-get-preview-file-name (&optional preview-command)
yuuji@51 855 "Get file name to preview by inquiring YaTeX-get-latex-command"
yuuji@86 856 (if (null preview-command) (setq preview-command dvi2-command))
yuuji@51 857 (let* ((latex-cmd (YaTeX-get-latex-command t))
yuuji@51 858 (rin (rindex latex-cmd ? ))
yuuji@172 859 (fname (if rin (substring latex-cmd (1+ rin)) ""))
yuuji@86 860 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
yuuji@172 861 (ext (if r (cdr r) "")))
yuuji@80 862 (concat
yuuji@80 863 (if (string= fname "")
yuuji@172 864 (setq fname (substring (file-name-nondirectory
yuuji@172 865 (buffer-file-name))
yuuji@172 866 0 -4))
yuuji@172 867 (setq fname (substring fname 0 (rindex fname ?.))))
yuuji@80 868 ext)))
yuuji@51 869
yuuji@51 870 (defun YaTeX-get-latex-command (&optional switch)
yuuji@51 871 "Specify the latex-command name and its argument.
yuuji@51 872 If there is a line which begins with string: \"%#!\", the following
yuuji@51 873 strings are assumed to be the latex-command and arguments. The
yuuji@51 874 default value of latex-command is:
yuuji@51 875 tex-command FileName
yuuji@51 876 and if you write \"%#!jlatex\" in the beginning of certain line.
yuuji@51 877 \"jlatex \" FileName
yuuji@51 878 will be the latex-command,
yuuji@51 879 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
yuuji@51 880 is non-nil, then
yuuji@51 881 \"jlatex main.tex\"
yuuji@51 882
yuuji@51 883 will be given to the shell."
yuuji@80 884 (let (parent tparent magic)
yuuji@51 885 (setq parent
yuuji@51 886 (cond
yuuji@80 887 (YaTeX-parent-file
yuuji@80 888 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
yuuji@80 889 YaTeX-parent-file))
yuuji@51 890 (t (save-excursion
yuuji@51 891 (YaTeX-visit-main t)
yuuji@51 892 (file-name-nondirectory (buffer-file-name)))))
yuuji@80 893 magic (YaTeX-get-builtin "!")
yuuji@80 894 tparent (file-name-nondirectory parent))
yuuji@80 895 (YaTeX-replace-formats
yuuji@80 896 (cond
yuuji@80 897 (magic
yuuji@80 898 (cond
yuuji@80 899 (switch (if (string-match "\\s " magic) magic
yuuji@80 900 (concat magic " " parent)))
yuuji@80 901 (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
yuuji@80 902 (t (concat tex-command " " (if switch parent))))
yuuji@80 903 (list (cons "f" tparent)
yuuji@80 904 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
yuuji@51 905
yuuji@51 906 (defvar YaTeX-lpr-command-history nil
yuuji@51 907 "Holds command line history of YaTeX-lpr.")
yuuji@51 908 (put 'YaTeX-lpr-command-history 'no-default t)
yuuji@79 909 (defvar YaTeX-lpr-ask-page-range-default t)
yuuji@51 910 (defun YaTeX-lpr (arg)
yuuji@79 911 "Print out.
yuuji@79 912 If prefix arg ARG is non nil, call print driver without
yuuji@51 913 page range description."
yuuji@51 914 (interactive "P")
yuuji@79 915 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
yuuji@51 916 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
yuuji@52 917 from to (lbuffer "*dvi-printing*") dir)
yuuji@51 918 (setq
yuuji@51 919 cmd
yuuji@51 920 (YaTeX-replace-format
yuuji@51 921 cmd "f"
yuuji@51 922 (if (or arg (not (string-match "%f" cmd)))
yuuji@51 923 ""
yuuji@51 924 (YaTeX-replace-format
yuuji@51 925 dviprint-from-format
yuuji@51 926 "b"
yuuji@51 927 (if (string=
yuuji@51 928 (setq from (read-string "From page(default 1): ")) "")
yuuji@51 929 "1" from))))
yuuji@51 930 )
yuuji@51 931 (setq
yuuji@51 932 cmd
yuuji@51 933 (YaTeX-replace-format
yuuji@51 934 cmd "t"
yuuji@51 935 (if (or arg (not (string-match "%t" cmd))
yuuji@51 936 (string=
yuuji@51 937 (setq to (read-string "To page(default none): ")) ""))
yuuji@51 938 ""
yuuji@51 939 (YaTeX-replace-format dviprint-to-format "e" to)))
yuuji@51 940 )
yuuji@72 941 (setq
yuuji@72 942 cmd
yuuji@72 943 (YaTeX-replace-format
yuuji@72 944 cmd "p"
yuuji@72 945 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
yuuji@51 946 (setq cmd
yuuji@51 947 (read-string-with-history
yuuji@51 948 "Edit command line: "
yuuji@53 949 (format cmd
yuuji@53 950 (if (get 'dvi2-command 'region)
yuuji@53 951 (substring YaTeX-texput-file
yuuji@53 952 0 (rindex YaTeX-texput-file ?.))
yuuji@53 953 (YaTeX-get-preview-file-name)))
yuuji@51 954 'YaTeX-lpr-command-history))
yuuji@51 955 (save-excursion
yuuji@51 956 (YaTeX-visit-main t) ;;change execution directory
yuuji@52 957 (setq dir default-directory)
yuuji@51 958 (YaTeX-showup-buffer
yuuji@51 959 lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@51 960 (set-buffer (get-buffer-create lbuffer))
yuuji@51 961 (erase-buffer)
yuuji@52 962 (cd dir) ;for 19
yuuji@51 963 (cond
yuuji@64 964 ((not (fboundp 'start-process))
yuuji@51 965 (call-process shell-file-name "con" "*dvi-printing*" nil
yuuji@51 966 YaTeX-shell-command-option cmd))
yuuji@51 967 (t
yuuji@51 968 (set-process-buffer
yuuji@86 969 (let ((process-connection-type nil))
yuuji@86 970 (start-process "print" "*dvi-printing*" shell-file-name
yuuji@86 971 YaTeX-shell-command-option cmd))
yuuji@51 972 (get-buffer lbuffer))
yuuji@69 973 (message "Starting printing command: %s..." cmd))))))
yuuji@51 974
yuuji@51 975 (defun YaTeX-main-file-p ()
yuuji@51 976 "Return if current buffer is main LaTeX source."
yuuji@51 977 (cond
yuuji@52 978 (YaTeX-parent-file
yuuji@52 979 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
yuuji@51 980 ((YaTeX-get-builtin "!")
yuuji@70 981 (string-match
yuuji@70 982 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
yuuji@70 983 (buffer-name)))
yuuji@51 984 (t
yuuji@51 985 (save-excursion
yuuji@57 986 (let ((latex-main-id
yuuji@57 987 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
yuuji@51 988 (or (re-search-backward latex-main-id nil t)
yuuji@69 989 (re-search-forward latex-main-id nil t)))))))
yuuji@51 990
yuuji@51 991 (defun YaTeX-visit-main (&optional setbuf)
yuuji@51 992 "Switch buffer to main LaTeX source.
yuuji@122 993 Use set-buffer instead of switch-to-buffer if the optional argument
yuuji@51 994 SETBUF is t(Use it only from Emacs-Lisp program)."
yuuji@51 995 (interactive "P")
yuuji@51 996 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
yuuji@70 997 (let ((ff (function (lambda (f)
yuuji@70 998 (if setbuf (set-buffer (find-file-noselect f))
yuuji@70 999 (find-file f)))))
yuuji@70 1000 b-in main-file YaTeX-create-file-prefix-g
yuuji@70 1001 (hilit-auto-highlight (not setbuf)))
yuuji@51 1002 (if (setq b-in (YaTeX-get-builtin "!"))
yuuji@51 1003 (setq main-file (YaTeX-guess-parent b-in)))
yuuji@51 1004 (if YaTeX-parent-file
yuuji@51 1005 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
yuuji@51 1006 YaTeX-parent-file))
yuuji@51 1007 (if (YaTeX-main-file-p)
yuuji@51 1008 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
yuuji@51 1009 (cond
yuuji@70 1010 ((and ;;(interactive-p)
yuuji@70 1011 main-file
yuuji@70 1012 (cond ((get-file-buffer main-file)
yuuji@70 1013 (cond
yuuji@70 1014 (setbuf (set-buffer (get-file-buffer main-file)))
yuuji@70 1015 ((get-buffer-window (get-file-buffer main-file))
yuuji@70 1016 (select-window
yuuji@70 1017 (get-buffer-window (get-file-buffer main-file))))
yuuji@70 1018 (t (switch-to-buffer (get-file-buffer main-file)))))
yuuji@70 1019 ((file-exists-p main-file)
yuuji@70 1020 (funcall ff main-file)))))
yuuji@70 1021 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
yuuji@51 1022 ((and main-file
yuuji@51 1023 (file-exists-p (setq main-file (concat "../" main-file)))
yuuji@80 1024 (or b-in
yuuji@80 1025 (y-or-n-p (concat (expand-file-name main-file)
yuuji@80 1026 " is main file?:"))))
yuuji@51 1027 (setq YaTeX-parent-file main-file)
yuuji@70 1028 ;(YaTeX-switch-to-buffer main-file setbuf)
yuuji@70 1029 (funcall ff main-file)
yuuji@70 1030 )
yuuji@51 1031 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
yuuji@51 1032 (setq YaTeX-parent-file main-file)
yuuji@70 1033 ; (YaTeX-switch-to-buffer main-file setbuf))
yuuji@70 1034 (funcall ff main-file))
yuuji@51 1035 )))
yuuji@69 1036 nil)
yuuji@51 1037
yuuji@51 1038 (defun YaTeX-guess-parent (command-line)
yuuji@51 1039 (setq command-line
yuuji@51 1040 (if (string-match ".*\\s " command-line)
yuuji@51 1041 (substring command-line (match-end 0))
yuuji@51 1042 (file-name-nondirectory (buffer-file-name)))
yuuji@51 1043 command-line
yuuji@51 1044 (concat (if (string-match "\\(.*\\)\\." command-line)
yuuji@51 1045 (substring command-line (match-beginning 1) (match-end 1))
yuuji@51 1046 command-line)
yuuji@69 1047 ".tex")))
yuuji@51 1048
yuuji@51 1049 (defun YaTeX-visit-main-other-window ()
yuuji@51 1050 "Switch to buffer main LaTeX source in other window."
yuuji@51 1051 (interactive)
yuuji@51 1052 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
yuuji@51 1053 (YaTeX-switch-to-buffer-other-window
yuuji@69 1054 (concat (YaTeX-get-preview-file-name) ".tex"))))
yuuji@51 1055
yuuji@51 1056 (defun YaTeX-save-buffers ()
yuuji@60 1057 "Save buffers whose major-mode is equal to current major-mode."
yuuji@51 1058 (basic-save-buffer)
yuuji@60 1059 (let ((cmm major-mode))
yuuji@60 1060 (save-excursion
yuuji@60 1061 (mapcar '(lambda (buf)
yuuji@60 1062 (set-buffer buf)
yuuji@60 1063 (if (and (buffer-file-name buf)
yuuji@60 1064 (eq major-mode cmm)
yuuji@60 1065 (buffer-modified-p buf)
yuuji@60 1066 (y-or-n-p (format "Save %s" (buffer-name buf))))
yuuji@60 1067 (save-buffer buf)))
yuuji@69 1068 (buffer-list)))))
yuuji@51 1069
yuuji@51 1070 (provide 'yatexprc)