yatex

annotate yatexprc.el @ 262:15ec86ff549c

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