yatex

annotate yatexprc.el @ 376:1bbd0c2b340f

When on-the-fly preview activated, overlay indicate its busy state. For overlay visibility, modify some foureground/background faces.
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 04 Jan 2015 00:39:44 +0900
parents afb8cb06b1d4
children 81413ee9c32d
rev   line source
yuuji@287 1 ;;; yatexprc.el --- YaTeX process handler
yuuji@287 2 ;;;
yuuji@367 3 ;;; (c)1993-2014 by HIROSE Yuuji.[yuuji@yatex.org]
yuuji@376 4 ;;; Last modified Sat Jan 3 23:37:49 2015 on firestorm
yuuji@367 5 ;;; $Id$
yuuji@51 6
yuuji@287 7 ;;; Code:
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@354 67 (background (string-match "\\*bg:" buffer))
yuuji@64 68 (outcode
yuuji@64 69 (cond ((eq major-mode 'yatex-mode) YaTeX-coding-system)
yuuji@69 70 ((eq major-mode 'yahtml-mode) yahtml-kanji-code))))
yuuji@51 71 (if (and YaTeX-typeset-process
yuuji@51 72 (eq (process-status YaTeX-typeset-process) 'run))
yuuji@51 73 ;; if tex command is halting.
yuuji@51 74 (YaTeX-kill-typeset-process YaTeX-typeset-process))
yuuji@60 75 (YaTeX-put-nonstopmode)
yuuji@60 76 (setq prcname (or prcname "LaTeX")
yuuji@60 77 modename (or modename "typeset"))
yuuji@60 78 (if (eq major-mode 'yatex-mode) (YaTeX-visit-main t)) ;;execution dir
yuuji@51 79 (setq execdir default-directory)
yuuji@51 80 ;;Select lower-most window if there are more than 2 windows and
yuuji@51 81 ;;typeset buffer not seen.
yuuji@354 82 (if background
yuuji@354 83 nil ;do not showup
yuuji@354 84 (YaTeX-showup-buffer
yuuji@354 85 buffer 'YaTeX-showup-buffer-bottom-most))
yuuji@51 86 (set-buffer (get-buffer-create buffer))
yuuji@51 87 (setq default-directory execdir)
yuuji@51 88 (cd execdir)
yuuji@213 89 (erase-buffer)
yuuji@51 90 (cond
yuuji@64 91 ((not (fboundp 'start-process)) ;YaTeX-dos;if MS-DOS
yuuji@51 92 (call-process
yuuji@64 93 shell-file-name nil buffer nil YaTeX-shell-command-option command))
yuuji@51 94 (t ;if UNIX
yuuji@51 95 (set-process-buffer
yuuji@51 96 (setq YaTeX-typeset-process
yuuji@60 97 (start-process prcname buffer shell-file-name
yuuji@51 98 YaTeX-shell-command-option command))
yuuji@51 99 (get-buffer buffer))
yuuji@123 100 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)
yuuji@212 101 (put 'YaTeX-typeset-process 'thiscmd command)
yuuji@212 102 (put 'YaTeX-typeset-process 'name prcname)
yuuji@262 103 (if (fboundp 'current-time)
yuuji@262 104 (setq YaTeX-typeset-consumption
yuuji@262 105 (cons (cons 'time (current-time))
yuuji@262 106 (delq 'time YaTeX-typeset-consumption))))
yuuji@123 107 (let ((ppprop (get 'YaTeX-typeset-process 'ppcmd)))
yuuji@123 108 (setq ppprop (delq (assq YaTeX-typeset-process ppprop) ppprop))
yuuji@123 109 (if ppcmd
yuuji@123 110 (setq ppprop (cons (cons YaTeX-typeset-process ppcmd) ppprop)))
yuuji@224 111 (put 'YaTeX-typeset-process 'ppcmd ppprop))
yuuji@224 112 (if (and (boundp 'bibcmd) bibcmd)
yuuji@224 113 (let ((bcprop (get 'YaTeX-typeset-process 'bibcmd)))
yuuji@224 114 (setq bcprop (cons
yuuji@224 115 (cons YaTeX-typeset-process bibcmd)
yuuji@224 116 (delq (assq YaTeX-typeset-process bcprop) bcprop)))
yuuji@224 117 (put 'YaTeX-typeset-process 'bibcmd bcprop)))))
yuuji@51 118 (message (format "Calling `%s'..." command))
yuuji@51 119 (setq YaTeX-current-TeX-buffer (buffer-name))
yuuji@60 120 (use-local-map map) ;map may be localized
yuuji@51 121 (set-syntax-table YaTeX-typeset-buffer-syntax)
yuuji@60 122 (setq mode-name modename)
yuuji@60 123 (if YaTeX-typeset-process ;if process is running (maybe on UNIX)
yuuji@64 124 (cond ((fboundp 'set-current-process-coding-system)
yuuji@51 125 (set-current-process-coding-system
yuuji@64 126 YaTeX-latex-message-code outcode))
yuuji@64 127 ((fboundp 'set-process-coding-system)
yuuji@64 128 (set-process-coding-system
yuuji@64 129 YaTeX-typeset-process YaTeX-latex-message-code outcode))
yuuji@64 130 (YaTeX-emacs-20
yuuji@64 131 (set-buffer-process-coding-system
yuuji@64 132 YaTeX-latex-message-code outcode))
yuuji@51 133 ((boundp 'NEMACS)
yuuji@51 134 (set-kanji-process-code YaTeX-latex-message-code))))
yuuji@212 135 (set-marker (or YaTeX-typeset-marker
yuuji@212 136 (setq YaTeX-typeset-marker (make-marker)))
yuuji@212 137 (point))
yuuji@212 138 (insert (format "Call `%s'\n" command))
yuuji@370 139 (cond
yuuji@370 140 ((fboundp 'start-process)
yuuji@51 141 (insert " ")
yuuji@51 142 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
yuuji@370 143 (t (message "Done.")))
yuuji@51 144 (if (bolp) (forward-line -1)) ;what for?
yuuji@354 145 (cond
yuuji@354 146 (background nil)
yuuji@354 147 ((and YaTeX-emacs-19 window-system)
yuuji@354 148 (let ((win (get-buffer-window buffer t)) owin)
yuuji@354 149 (select-frame (window-frame win))
yuuji@354 150 (setq owin (selected-window))
yuuji@354 151 (select-window win)
yuuji@354 152 (goto-char (point-max))
yuuji@354 153 (recenter -1)
yuuji@354 154 (select-window owin)))
yuuji@354 155 (t (select-window (get-buffer-window buffer))
yuuji@354 156 (goto-char (point-max))
yuuji@354 157 (recenter -1)))
yuuji@51 158 (select-window window)
yuuji@60 159 (switch-to-buffer cb)
yuuji@69 160 (YaTeX-remove-nonstopmode))))
yuuji@51 161
yuuji@262 162 (defvar YaTeX-typeset-auto-rerun t
yuuji@262 163 "*Non-nil automatically reruns typesetter when cross-refs update found.
yuuji@262 164 This is a toy mechanism. DO NOT RELY ON THIS MECHANISM.
yuuji@262 165 You SHOULD check the integrity of cross-references with your eyes!!
yuuji@262 166 Supplying an integer to this variable inhibit compulsory call of bibtex,
yuuji@262 167 thus, it call bibtex only if warning messages about citation are seen.")
yuuji@212 168 (defvar YaTeX-typeset-rerun-msg "Rerun to get cross-references right.")
yuuji@224 169 (defvar YaTeX-typeset-citation-msg
yuuji@261 170 "Warning: Citation \`")
yuuji@51 171 (defun YaTeX-typeset-sentinel (proc mes)
yuuji@51 172 (cond ((null (buffer-name (process-buffer proc)))
yuuji@51 173 ;; buffer killed
yuuji@51 174 (set-process-buffer proc nil))
yuuji@51 175 ((memq (process-status proc) '(signal exit))
yuuji@51 176 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
yuuji@51 177 (pwin (get-buffer-window pbuf))
yuuji@212 178 (owin (selected-window)) win
yuuji@261 179 tobecalled shortname
yuuji@212 180 (thiscmd (get 'YaTeX-typeset-process 'thiscmd))
yuuji@212 181 (ppprop (get 'YaTeX-typeset-process 'ppcmd))
yuuji@371 182 z (ppcmd (cdr (assq proc ppprop)))
yuuji@224 183 (bcprop (get 'YaTeX-typeset-process 'bibcmd))
yuuji@224 184 (bibcmd (cdr (assq proc bcprop))))
yuuji@212 185 (put 'YaTeX-typeset-process 'ppcmd ;erase ppcmd
yuuji@212 186 (delq (assq proc ppprop) ppprop))
yuuji@224 187 (put 'YaTeX-typeset-process 'bibcmd ;erase bibcmd
yuuji@224 188 (delq (assq proc bcprop) bcprop))
yuuji@51 189 ;; save-excursion isn't the right thing if
yuuji@51 190 ;; process-buffer is current-buffer
yuuji@51 191 (unwind-protect
yuuji@51 192 (progn
yuuji@51 193 ;; Write something in *typesetting* and hack its mode line
yuuji@51 194 (if pwin
yuuji@51 195 (select-window pwin)
yuuji@51 196 (set-buffer pbuf))
yuuji@51 197 ;;(YaTeX-showup-buffer pbuf nil t)
yuuji@51 198 (goto-char (point-max))
yuuji@51 199 (if pwin (recenter -3))
yuuji@60 200 (insert ?\n mode-name " " mes)
yuuji@51 201 (forward-char -1)
yuuji@262 202 (insert
yuuji@262 203 (format " at %s%s\n"
yuuji@262 204 (substring (current-time-string) 0 -5)
yuuji@262 205 (if (and (fboundp 'current-time) (fboundp 'float)
yuuji@262 206 (assq 'time YaTeX-typeset-consumption))
yuuji@262 207 (format
yuuji@262 208 " (%.2f secs)"
yuuji@262 209 (YaTeX-elapsed-time
yuuji@262 210 (cdr (assq 'time YaTeX-typeset-consumption))
yuuji@262 211 (current-time))))))
yuuji@51 212 (setq mode-line-process
yuuji@51 213 (concat ": "
yuuji@51 214 (symbol-name (process-status proc))))
yuuji@256 215 (message "%s %s" mode-name
yuuji@51 216 (if (eq (process-status proc) 'exit)
yuuji@51 217 "done" "ceased"))
yuuji@51 218 ;; If buffer and mode line shows that the process
yuuji@51 219 ;; is dead, we can delete it now. Otherwise it
yuuji@51 220 ;; will stay around until M-x list-processes.
yuuji@51 221 (delete-process proc)
yuuji@224 222 (if (cond
yuuji@263 223 ((or (not YaTeX-typeset-auto-rerun)
yuuji@263 224 (string-match "latexmk" thiscmd))
yuuji@263 225 nil)
yuuji@262 226 ((and bibcmd ;Call bibtex if bibcmd defined &&
yuuji@262 227 (or ; (1st call || warning found)
yuuji@262 228 (and (not (numberp YaTeX-typeset-auto-rerun))
yuuji@262 229 ; cancel call at 1st, if value is a number.
yuuji@262 230 (not (string-match "bibtex" mode-name)))
yuuji@262 231 (re-search-backward
yuuji@262 232 YaTeX-typeset-citation-msg
yuuji@262 233 YaTeX-typeset-marker t))
yuuji@262 234 (save-excursion ; && using .bbl files.
yuuji@224 235 (search-backward
yuuji@224 236 ".bbl" YaTeX-typeset-marker t)))
yuuji@262 237 ;; Always call bibtex after the first typesetting,
yuuji@262 238 ;; because bibtex doesn't warn disappeared \cite.
yuuji@262 239 ;; (Suggested by ryseto. 2012)
yuuji@262 240 ;; It is more efficient to call bibtex directly than
yuuji@262 241 ;; to call it after deep inspection on the balance
yuuji@262 242 ;; of \cite vs. \bib*'s referring all *.aux files.
yuuji@224 243 (insert "\n" YaTeX-typeset-rerun-msg "\n")
yuuji@261 244 (setq tobecalled bibcmd shortname "+bibtex"))
yuuji@261 245 ((or
yuuji@261 246 (save-excursion
yuuji@261 247 (search-backward
yuuji@261 248 YaTeX-typeset-rerun-msg YaTeX-typeset-marker t))
yuuji@261 249 (save-excursion
yuuji@261 250 (re-search-backward
yuuji@261 251 "natbib.*Rerun to get citations correct"
yuuji@261 252 YaTeX-typeset-marker t)))
yuuji@224 253 (if bibcmd
yuuji@224 254 (put 'YaTeX-typeset-process 'bibcmd
yuuji@224 255 (cons (cons (get-buffer-process pbuf) bibcmd)
yuuji@224 256 bcprop)))
yuuji@261 257 (setq tobecalled thiscmd shortname "+typeset"))
yuuji@261 258 (t
yuuji@261 259 nil)) ;no need to call any process
yuuji@354 260 (progn ;;Something occurs to call next command
yuuji@212 261 (insert
yuuji@212 262 (format
yuuji@212 263 "===!!! %s !!!===\n"
yuuji@212 264 (message "Rerun `%s' to get cross-references right"
yuuji@224 265 tobecalled)))
yuuji@224 266 (if (equal tobecalled thiscmd)
yuuji@224 267 (set-marker YaTeX-typeset-marker (point)))
yuuji@212 268 (set-process-sentinel
yuuji@212 269 (start-process
yuuji@261 270 (setq mode-name (concat mode-name shortname))
yuuji@256 271 pbuf
yuuji@224 272 shell-file-name YaTeX-shell-command-option tobecalled)
yuuji@212 273 'YaTeX-typeset-sentinel)
yuuji@212 274 (if ppcmd
yuuji@212 275 (put 'YaTeX-typeset-process 'ppcmd
yuuji@212 276 (cons (cons (get-buffer-process pbuf) ppcmd)
yuuji@224 277 ppprop)))
yuuji@224 278 (if thiscmd
yuuji@224 279 (put 'YaTeX-typeset-process 'thiscmd thiscmd)))
yuuji@212 280 ;; If ppcmd is active, call it.
yuuji@123 281 (cond
yuuji@371 282 ((and ppcmd (symbolp ppcmd) (fboundp ppcmd))
yuuji@371 283 ;; If ppcmd is set and it is a function symbol,
yuuji@371 284 ;; call it whenever command succeeded or not
yuuji@371 285 (funcall ppcmd))
yuuji@123 286 ((and ppcmd (string-match "finish" mes))
yuuji@123 287 (insert (format "=======> Success! Calling %s\n" ppcmd))
yuuji@371 288 (setq mode-name ; set process name
yuuji@371 289 (concat
yuuji@371 290 mode-name "+"
yuuji@371 291 (substring ppcmd 0 (string-match " " ppcmd))))
yuuji@212 292 ; to reach here, 'start-process exists on this emacsen
yuuji@371 293 (set-process-sentinel
yuuji@371 294 (start-process
yuuji@371 295 mode-name
yuuji@371 296 pbuf ; Use this buffer twice.
yuuji@371 297 shell-file-name YaTeX-shell-command-option
yuuji@371 298 ppcmd)
yuuji@371 299 'YaTeX-typeset-sentinel))
yuuji@262 300 (t ;pull back original mode-name
yuuji@371 301 ;;Confirm process buffer to be shown when error occured
yuuji@371 302 (YaTeX-showup-buffer pbuf 'YaTeX-showup-buffer-bottom-most)
yuuji@371 303 (message "Command FAILED!")
yuuji@261 304 (setq mode-name "typeset"))))
yuuji@123 305 (forward-char 1))
yuuji@123 306 (setq YaTeX-typeset-process nil)
yuuji@51 307 ;; Force mode line redisplay soon
yuuji@51 308 (set-buffer-modified-p (buffer-modified-p))
yuuji@51 309 )
yuuji@51 310 (select-window owin)
yuuji@69 311 (set-buffer obuf)))))
yuuji@51 312
yuuji@51 313 (defvar YaTeX-texput-file "texput.tex"
yuuji@69 314 "*File name for temporary file of typeset-region.")
yuuji@51 315
yuuji@354 316 (defun YaTeX-typeset-region (&optional pp)
yuuji@52 317 "Paste the region to the file `texput.tex' and execute typesetter.
yuuji@52 318 The region is specified by the rule:
yuuji@51 319 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
yuuji@51 320 (1-1)if the keyword `%#END' is found after `%#BEGIN',
yuuji@51 321 ->Assume the text between `%#BEGIN' and `%#END' as region.
yuuji@51 322 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
yuuji@51 323 ->Assume the text after `%#BEGIN' as region.
yuuji@51 324 (2)If no `%#BEGIN' usage is found before the (point),
yuuji@51 325 ->Assume the text between current (point) and (mark) as region.
yuuji@51 326 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
yuuji@354 327 operation to the region.
yuuji@354 328 Optional second argument PP specifies post-processor command which will be
yuuji@354 329 called with one argument of current file name whitout extension."
yuuji@51 330 (interactive)
yuuji@51 331 (save-excursion
yuuji@51 332 (let*
yuuji@51 333 ((end "") typeout ;Type out message that tells the method of cutting.
yuuji@51 334 (texput YaTeX-texput-file)
yuuji@354 335 (texputroot (substring texput 0 (string-match "\\.tex$" texput)))
yuuji@51 336 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
yuuji@51 337 (buffer (current-buffer)) opoint preamble (subpreamble "") main
yuuji@51 338 (hilit-auto-highlight nil) ;for Emacs19 with hilit19
yuuji@354 339 ppcmd
yuuji@52 340 reg-begin reg-end lineinfo)
yuuji@354 341 (setq ppcmd (if (stringp pp) (concat pp " " texputroot) pp))
yuuji@51 342 (save-excursion
yuuji@51 343 (if (search-backward "%#BEGIN" nil t)
yuuji@51 344 (progn
yuuji@51 345 (setq typeout "--- Region from BEGIN to "
yuuji@51 346 end "the end of the buffer ---"
yuuji@51 347 reg-begin (match-end 0))
yuuji@51 348 (if (search-forward "%#END" nil t)
yuuji@51 349 (setq reg-end (match-beginning 0)
yuuji@51 350 end "END ---")
yuuji@51 351 (setq reg-end (point-max))))
yuuji@52 352 (setq typeout "=== Region from (point) to (mark) ==="
yuuji@52 353 reg-begin (point) reg-end (mark)))
yuuji@52 354 (goto-char (min reg-begin reg-end))
yuuji@52 355 (setq lineinfo (count-lines (point-min) (point-end-of-line)))
yuuji@51 356 (goto-char (point-min))
yuuji@51 357 (while (search-forward "%#REQUIRE" nil t)
yuuji@51 358 (setq subpreamble
yuuji@51 359 (concat subpreamble
yuuji@51 360 (cond
yuuji@51 361 ((eolp)
yuuji@51 362 (buffer-substring
yuuji@51 363 (match-beginning 0)
yuuji@51 364 (point-beginning-of-line)))
yuuji@51 365 (t (buffer-substring
yuuji@51 366 (match-end 0)
yuuji@51 367 (point-end-of-line))))
yuuji@51 368 "\n"))
yuuji@51 369 (goto-char (match-end 0))))
yuuji@51 370 (YaTeX-visit-main t)
yuuji@51 371 (setq main (current-buffer))
yuuji@51 372 (setq opoint (point))
yuuji@51 373 (goto-char (point-min))
yuuji@51 374 (setq
yuuji@51 375 preamble
yuuji@51 376 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
yuuji@51 377 (buffer-substring (point-min) (match-end 0))
yuuji@73 378 (concat
yuuji@73 379 (if YaTeX-use-LaTeX2e "\\documentclass{" "\\documentstyle{")
yuuji@73 380 YaTeX-default-document-style "}\n"
yuuji@73 381 "\\begin{document}")))
yuuji@51 382 (goto-char opoint)
yuuji@51 383 ;;(set-buffer buffer) ;for clarity
yuuji@133 384 (let ((hilit-auto-highlight nil) (auto-mode-alist nil)
yuuji@133 385 (magic-mode-alist nil)) ;Do not activate yatex-mode here
yuuji@52 386 (set-buffer (find-file-noselect texput)))
yuuji@51 387 ;;(find-file YaTeX-texput-file)
yuuji@51 388 (erase-buffer)
yuuji@354 389 (YaTeX-set-file-coding-system YaTeX-kanji-code YaTeX-coding-system)
yuuji@64 390 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
yuuji@51 391 (insert "\\nonstopmode{}\n"))
yuuji@354 392 (insert preamble "\n" subpreamble "\n"
yuuji@354 393 "\\pagestyle{empty}\n\\thispagestyle{empty}\n")
yuuji@52 394 (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo))
yuuji@51 395 (insert-buffer-substring buffer reg-begin reg-end)
yuuji@51 396 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
yuuji@51 397 (insert "\n\\end{document}\n")
yuuji@51 398 (basic-save-buffer)
yuuji@51 399 (kill-buffer (current-buffer))
yuuji@51 400 (set-buffer main) ;return to parent file or itself.
yuuji@354 401 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
yuuji@51 402 (switch-to-buffer buffer) ;for Emacs-19
yuuji@52 403 (put 'dvi2-command 'region t)
yuuji@52 404 (put 'dvi2-command 'file buffer)
yuuji@69 405 (put 'dvi2-command 'offset lineinfo))))
yuuji@51 406
yuuji@376 407 (defvar YaTeX-use-image-preview "jpg"
yuuji@376 408 "*Nil means not using image preview by [prefix] t e.
yuuji@376 409 Acceptable value is one of \"jpg\" or \"png\", which specifies
yuuji@376 410 format of preview image.")
yuuji@355 411 (defvar YaTeX-preview-image-mode-map nil
yuuji@355 412 "Keymap used in YaTeX-preview-image-mode")
yuuji@355 413 (defun YaTeX-preview-image-mode ()
yuuji@355 414 (interactive)
yuuji@355 415 (if YaTeX-preview-image-mode-map
yuuji@355 416 nil
yuuji@355 417 (let ((map (setq YaTeX-preview-image-mode-map (make-sparse-keymap))))
yuuji@355 418 (define-key map "q" (lambda()(interactive)
yuuji@355 419 (kill-buffer)
yuuji@355 420 (select-window
yuuji@355 421 (or (get 'YaTeX-typeset-process 'win)
yuuji@355 422 (selected-window)))))
yuuji@355 423 (define-key map "j" (lambda()(interactive) (scroll-up 1)))
yuuji@355 424 (define-key map "k" (lambda()(interactive) (scroll-up -1)))))
yuuji@355 425 (use-local-map YaTeX-preview-image-mode-map))
yuuji@355 426
yuuji@371 427 (defvar YaTeX-typeset-pdf2image-chain
yuuji@371 428 (cond
yuuji@371 429 ((YaTeX-executable-find "pdfcrop") ;Mac OS X
yuuji@371 430 (list
yuuji@371 431 "pdfcrop --clip %b.pdf tmp.pdf"
yuuji@371 432 (if (YaTeX-executable-find "convert")
yuuji@371 433 "convert -density %d tmp.pdf %b.%f"
yuuji@371 434 "sips -s format %f -s dpiWidth %d -s dpiHeight %d %b.pdf --out %b.%f")
yuuji@371 435 "rm -f tmp.pdf")))
yuuji@371 436 "*Pipe line of command as a list to create image file from PDF.
yuuji@371 437 See also doc-string of YaTeX-typeset-dvi2image-chain.")
yuuji@371 438
yuuji@371 439 (defvar YaTeX-typeset-dvi2image-chain
yuuji@371 440 (cond
yuuji@376 441 ((and (equal YaTeX-use-image-preview "png")
yuuji@376 442 (YaTeX-executable-find "dvipng"))
yuuji@374 443 (list "dvipng %b.dvi"))
yuuji@371 444 ((YaTeX-executable-find YaTeX-cmd-dvips)
yuuji@371 445 (list
yuuji@371 446 (format "%s -E -o %%b.eps %%b.dvi" YaTeX-cmd-dvips)
yuuji@374 447 "convert -alpha off -density %d %b.eps %b.%f")))
yuuji@371 448 "*Pipe line of command as a list to create png file from DVI or PDF.
yuuji@371 449 %-notation rewritten list:
yuuji@371 450 %b basename of target file(\"texput\")
yuuji@371 451 %f Output format(\"png\")
yuuji@371 452 %d DPI
yuuji@371 453 ")
yuuji@371 454
yuuji@371 455 (defvar YaTeX-typeset-conv2image-chain-alist
yuuji@371 456 (list (cons 'pdf YaTeX-typeset-pdf2image-chain)
yuuji@371 457 (cons 'dvi YaTeX-typeset-dvi2image-chain))
yuuji@371 458 "Default alist for creating image files from DVI/PDF.
yuuji@371 459 The value is generated from YaTeX-typeset-pdf2image-chain and
yuuji@371 460 YaTeX-typeset-dvi2image-chain.")
yuuji@371 461
yuuji@354 462 (defvar YaTeX-typeset-conv2image-process nil "Process of conv2image chain")
yuuji@354 463 (defun YaTeX-typeset-conv2image-chain ()
yuuji@354 464 (let*((proc (or YaTeX-typeset-process YaTeX-typeset-conv2image-process))
yuuji@354 465 (prevname (process-name proc))
yuuji@376 466 (texput "texput")
yuuji@376 467 (format YaTeX-use-image-preview)
yuuji@374 468 (target (concat texput "." format))
yuuji@354 469 (math (get 'YaTeX-typeset-conv2image-chain 'math))
yuuji@371 470 (srctype (or (get 'YaTeX-typeset-conv2image-chain 'srctype)
yuuji@374 471 (if (file-newer-than-file-p
yuuji@374 472 (concat texput ".pdf")
yuuji@374 473 (concat texput ".dvi"))
yuuji@371 474 'pdf 'dvi)))
yuuji@371 475 (chain (cdr (assq srctype YaTeX-typeset-conv2image-chain-alist)))
yuuji@371 476 ;; This function is the first evaluation code.
yuuji@371 477 ;; If you find these command line does not work
yuuji@371 478 ;; on your system, please tell the author
yuuji@371 479 ;; which commands should be taken to achieve
yuuji@371 480 ;; one-shot png previewing on your system
yuuji@371 481 ;; before publishing patch on the Web.
yuuji@371 482 ;; Please please please please please.
yuuji@354 483 (curproc (member prevname chain))
yuuji@355 484 (w (get 'YaTeX-typeset-conv2image-chain 'win))
yuuji@355 485 (pwd default-directory)
yuuji@354 486 img)
yuuji@354 487 (if (not (= (process-exit-status proc) 0))
yuuji@354 488 (progn
yuuji@362 489 (YaTeX-showup-buffer ;never comes here(?)
yuuji@354 490 (current-buffer) 'YaTeX-showup-buffer-bottom-most)
yuuji@354 491 (message "Region typesetting FAILED"))
yuuji@354 492 (setq command
yuuji@354 493 (if curproc (car (cdr-safe curproc)) (car chain)))
yuuji@354 494 (if command
yuuji@371 495 (let ((cmdline (YaTeX-replace-formats
yuuji@371 496 command
yuuji@371 497 (list (cons "b" "texput")
yuuji@374 498 (cons "f" format)
yuuji@371 499 (cons "d" (if math "300" "200"))))))
yuuji@371 500 (insert (format "Calling `%s'...\n" cmdline))
yuuji@354 501 (set-process-sentinel
yuuji@354 502 (setq YaTeX-typeset-conv2image-process
yuuji@354 503 (start-process
yuuji@354 504 command
yuuji@354 505 (current-buffer)
yuuji@371 506 shell-file-name YaTeX-shell-command-option
yuuji@371 507 cmdline))
yuuji@354 508 'YaTeX-typeset-sentinel)
yuuji@354 509 (put 'YaTeX-typeset-process 'ppcmd
yuuji@354 510 (cons (cons (get-buffer-process (current-buffer))
yuuji@354 511 'YaTeX-typeset-conv2image-chain)
yuuji@354 512 (get 'YaTeX-typeset-process 'ppcmd))))
yuuji@354 513 ;; After all chain executed, display image in current window
yuuji@362 514 (cond
yuuji@364 515 ((and (featurep 'image) window-system)
yuuji@362 516 ;; If direct image displaying available in running Emacs,
yuuji@362 517 ;; display target image into the next window in Emacs.
yuuji@362 518 (select-window w)
yuuji@371 519 ;(setq foo (selected-window))
yuuji@362 520 (YaTeX-showup-buffer
yuuji@362 521 (get-buffer-create " *YaTeX-region-image*")
yuuji@362 522 'YaTeX-showup-buffer-bottom-most t)
yuuji@362 523 (remove-images (point-min) (point-max))
yuuji@362 524 (erase-buffer)
yuuji@362 525 (cd pwd) ;when reuse from other source
yuuji@355 526 ;(put-image (create-image (expand-file-name target)) (point))
yuuji@362 527 (insert-image-file target)
yuuji@362 528 (setq img (plist-get (text-properties-at (point)) 'intangible))
yuuji@362 529 (YaTeX-preview-image-mode)
yuuji@362 530 (if img
yuuji@362 531 (let ((height (cdr (image-size img))))
yuuji@362 532 (enlarge-window
yuuji@362 533 (- (ceiling (min height (/ (frame-height) 2)))
yuuji@375 534 (window-height)))))
yuuji@375 535 ;; Remember elapsed time, which will be threshold in auto-preview
yuuji@375 536 (put 'YaTeX-typeset-conv2image-chain 'elapse
yuuji@375 537 (YaTeX-elapsed-time
yuuji@375 538 (get 'YaTeX-typeset-conv2image-chain 'start) (current-time))))
yuuji@362 539 (t
yuuji@362 540 ;; Without direct image, display image with image viewer
yuuji@362 541 (YaTeX-system
yuuji@362 542 (format "%s %s" YaTeX-cmd-view-images target)
yuuji@362 543 "YaTeX-region-image"
yuuji@362 544 'noask)
yuuji@362 545 )
yuuji@362 546 )))))
yuuji@354 547
yuuji@375 548
yuuji@375 549 (defvar YaTeX-typeset-environment-timer nil)
yuuji@375 550 (defun YaTeX-typeset-environment-timer ()
yuuji@375 551 "Update preview image in the
yuuji@375 552 Plist: '(buf begPoint endPoint precedingChar 2precedingChar Substring time)"
yuuji@375 553 (let*((plist (get 'YaTeX-typeset-environment-timer 'laststate))
yuuji@375 554 (b (nth 0 plist))
yuuji@375 555 (s (nth 1 plist))
yuuji@375 556 (e (nth 2 plist))
yuuji@375 557 (p (nth 3 plist))
yuuji@375 558 (q (nth 4 plist))
yuuji@375 559 (st (nth 5 plist))
yuuji@375 560 (tm (nth 6 plist))
yuuji@375 561 (thresh (* 2 (or (get 'YaTeX-typeset-conv2image-chain 'elapse) 1))))
yuuji@375 562 (cond
yuuji@375 563 ;; In minibuffer, do nothing
yuuji@375 564 ((minibuffer-window-active-p (selected-window)) nil)
yuuji@375 565 ;; If point went out from last environment, cancel timer
yuuji@375 566 ;;;((and (message "s=%d, e=%d, p=%d" s e (point)) nil))
yuuji@375 567 ((or (not (eq b (window-buffer (selected-window))))
yuuji@375 568 (< (point) s)
yuuji@375 569 (> (point) e))
yuuji@375 570 (YaTeX-typeset-environment-cancel-auto))
yuuji@375 571 ;;;((and (message "e=%d, p=%d t=%s" e (point) (current-time)) nil))
yuuji@375 572 ;; If recently called, hold
yuuji@375 573 ((< (YaTeX-elapsed-time tm (current-time)) thresh)
yuuji@375 574 nil)
yuuji@375 575 ;; If condition changed from last call, do it
yuuji@375 576 ((and (/= p (preceding-char))
yuuji@375 577 (/= q (char-after (- (point) 1)))
yuuji@376 578 (not (string= st (YaTeX-buffer-substring s (min e (point-max))))))
yuuji@375 579 (YaTeX-typeset-environment)))))
yuuji@375 580
yuuji@130 581 (defun YaTeX-typeset-environment ()
yuuji@371 582 "Typeset current environment or paragraph.
yuuji@371 583 If region activated, use it."
yuuji@130 584 (interactive)
yuuji@130 585 (save-excursion
yuuji@375 586 (let ((math (YaTeX-in-math-mode-p)) usetimer)
yuuji@362 587 (cond
yuuji@362 588 ((and (fboundp 'region-active-p) (region-active-p))
yuuji@362 589 nil) ;if region is active, use it
yuuji@375 590 (math (setq usetimer t) (YaTeX-mark-environment))
yuuji@362 591 ((equal (or (YaTeX-inner-environment t) "document") "document")
yuuji@362 592 (mark-paragraph))
yuuji@375 593 (t (setq usetimer t) (YaTeX-mark-environment)))
yuuji@362 594 (if YaTeX-use-image-preview
yuuji@375 595 (let ((YaTeX-typeset-buffer (concat "*bg:" YaTeX-typeset-buffer))
yuuji@375 596 (b (region-beginning)) (e (region-end)))
yuuji@354 597 (put 'YaTeX-typeset-conv2image-chain 'math math)
yuuji@371 598 (put 'YaTeX-typeset-conv2image-chain 'srctype nil)
yuuji@355 599 (put 'YaTeX-typeset-conv2image-chain 'win (selected-window))
yuuji@375 600 (put 'YaTeX-typeset-conv2image-chain 'start (current-time))
yuuji@375 601 (put 'YaTeX-typeset-environment-timer 'laststate
yuuji@375 602 (list (current-buffer) b e (preceding-char)
yuuji@375 603 (char-after (- (point) 2))
yuuji@375 604 (YaTeX-buffer-substring b e)
yuuji@375 605 (current-time)))
yuuji@375 606 (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain)
yuuji@376 607 (if usetimer
yuuji@376 608 (progn
yuuji@376 609 (if YaTeX-on-the-fly-overlay
yuuji@376 610 (move-overlay YaTeX-on-the-fly-overlay b e)
yuuji@376 611 (overlay-put
yuuji@376 612 (setq YaTeX-on-the-fly-overlay (make-overlay b e))
yuuji@376 613 'face 'YaTeX-on-the-fly-activated-face))
yuuji@376 614 (YaTeX-typeset-environment-auto)
yuuji@376 615 )))
yuuji@354 616 (YaTeX-typeset-region)))))
yuuji@130 617
yuuji@375 618 (defvar YaTeX-on-the-fly-preview-image (string-to-number "0.1")
yuuji@375 619 "*Control the on-the-fly update of preview environment by an image.
yuuji@375 620 Nil disables on-the-fly update. Otherwise on-the-fly update is enabled
yuuji@375 621 with update interval specified by this value.")
yuuji@375 622
yuuji@375 623 (defun YaTeX-typeset-environment-auto ()
yuuji@375 624 "Turn on on-the-fly preview-image"
yuuji@375 625 (if YaTeX-typeset-environment-timer
yuuji@375 626 nil
yuuji@375 627 (setq YaTeX-typeset-environment-timer
yuuji@375 628 (run-with-idle-timer
yuuji@375 629 (max (string-to-number "0.1")
yuuji@375 630 (cond
yuuji@375 631 ((numberp YaTeX-on-the-fly-preview-image)
yuuji@375 632 YaTeX-on-the-fly-preview-image)
yuuji@375 633 ((stringp YaTeX-on-the-fly-preview-image)
yuuji@375 634 (string-to-number YaTeX-on-the-fly-preview-image))
yuuji@375 635 (t 1)))
yuuji@375 636 t 'YaTeX-typeset-environment-timer))))
yuuji@375 637
yuuji@375 638 (defun YaTeX-typeset-environment-cancel-auto ()
yuuji@375 639 "Cancel typeset-environment timer."
yuuji@375 640 (interactive)
yuuji@375 641 (cancel-timer YaTeX-typeset-environment-timer)
yuuji@375 642 (setq YaTeX-typeset-environment-timer nil)
yuuji@376 643 (delete-overlay YaTeX-on-the-fly-overlay)
yuuji@375 644 (message "Auto-preview canceled"))
yuuji@375 645
yuuji@375 646
yuuji@123 647 (defun YaTeX-typeset-buffer (&optional pp)
yuuji@60 648 "Typeset whole buffer.
yuuji@60 649 If %#! usage says other buffer is main text,
yuuji@51 650 visit main buffer to confirm if its includeonly list contains current
yuuji@51 651 buffer's file. And if it doesn't contain editing text, ask user which
yuuji@123 652 action wants to be done, A:Add list, R:Replace list, %:comment-out list.
yuuji@123 653 If optional argument PP given as string, PP is considered as post-process
yuuji@123 654 command and call it with the same command argument as typesetter without
yuuji@123 655 last extension.
yuuji@123 656 eg. if PP is \"dvipdfmx\", called commands as follows.
yuuji@123 657 platex foo.tex
yuuji@123 658 dvipdfmx foo
yuuji@123 659 PP command will be called iff typeset command exit successfully"
yuuji@51 660 (interactive)
yuuji@51 661 (YaTeX-save-buffers)
yuuji@51 662 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
yuuji@51 663 (mydir (file-name-directory (buffer-file-name)))
yuuji@224 664 (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd
yuuji@123 665 (cb (current-buffer)))
yuuji@224 666 (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt
yuuji@224 667 pparg (substring pparg (rindex pparg ? )) ;get last arg
yuuji@224 668 pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext
yuuji@262 669 bibcmd (or (YaTeX-get-builtin "BIBTEX") bibtex-command))
yuuji@262 670 (or (string-match "\\s " bibcmd) ;if bibcmd has no spaces,
yuuji@262 671 (setq bibcmd (concat bibcmd pparg))) ;append argument(== %#!)
yuuji@123 672 (and pp
yuuji@123 673 (stringp pp)
yuuji@224 674 (setq ppcmd (concat pp pparg)))
yuuji@51 675 (if (YaTeX-main-file-p) nil
yuuji@51 676 (save-excursion
yuuji@51 677 (YaTeX-visit-main t) ;search into main buffer
yuuji@51 678 (save-excursion
yuuji@51 679 (push-mark (point) t)
yuuji@51 680 (goto-char (point-min))
yuuji@51 681 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
yuuji@51 682 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
yuuji@51 683 (let*
yuuji@51 684 ((b (progn (skip-chars-forward "^{") (point)))
yuuji@51 685 (e (progn (skip-chars-forward "^}") (1+ (point))))
yuuji@51 686 (s (buffer-substring b e)) c
yuuji@51 687 (pardir (file-name-directory (buffer-file-name))))
yuuji@51 688 (if (string-match (concat "[{,/]" me "[,}]") s)
yuuji@51 689 nil ; Nothing to do when it's already in includeonly.
yuuji@51 690 (ding)
yuuji@51 691 (switch-to-buffer (current-buffer));Display this buffer.
yuuji@51 692 (setq
yuuji@51 693 me ;;Rewrite my name(me) to contain sub directory name.
yuuji@51 694 (concat
yuuji@51 695 (if (string-match pardir mydir) ;if mydir is child of main
yuuji@51 696 (substring mydir (length pardir)) ;cut absolute path
yuuji@51 697 mydir) ;else concat absolute path name.
yuuji@51 698 me))
yuuji@51 699 (message
yuuji@51 700 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
yuuji@51 701 me)
yuuji@51 702 (setq c (read-char))
yuuji@51 703 (cond
yuuji@51 704 ((= c ?a)
yuuji@51 705 (goto-char (1+ b))
yuuji@51 706 (insert me (if (string= s "{}") "" ",")))
yuuji@51 707 ((= c ?r)
yuuji@51 708 (delete-region (1+ b) (1- e)) (insert me))
yuuji@51 709 ((= c ?%)
yuuji@51 710 (beginning-of-line) (insert "%"))
yuuji@51 711 (t nil))
yuuji@51 712 (basic-save-buffer))))
yuuji@51 713 (exchange-point-and-mark)))
yuuji@51 714 (switch-to-buffer cb)) ;for 19
yuuji@123 715 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
yuuji@69 716 (put 'dvi2-command 'region nil)))
yuuji@51 717
yuuji@51 718 (defvar YaTeX-call-command-history nil
yuuji@51 719 "Holds history list of YaTeX-call-command-on-file.")
yuuji@51 720 (put 'YaTeX-call-command-history 'no-default t)
yuuji@86 721 (defun YaTeX-call-command-on-file (base-cmd buffer &optional file)
yuuji@171 722 "Call external command BASE-CMD in the BUFFER.
yuuji@86 723 By default, pass the basename of current file. Optional 3rd argument
yuuji@86 724 FILE changes the default file name."
yuuji@51 725 (YaTeX-save-buffers)
yuuji@171 726 (let ((default (concat base-cmd " "
yuuji@171 727 (let ((me (file-name-nondirectory
yuuji@171 728 (or file buffer-file-name))))
yuuji@171 729 (if (string-match "\\.tex" me)
yuuji@171 730 (substring me 0 (match-beginning 0))
yuuji@171 731 me)))))
yuuji@171 732 (or YaTeX-call-command-history
yuuji@171 733 (setq YaTeX-call-command-history (list default)))
yuuji@171 734 (YaTeX-typeset
yuuji@171 735 (read-string-with-history
yuuji@171 736 "Call command: "
yuuji@171 737 (car YaTeX-call-command-history)
yuuji@171 738 'YaTeX-call-command-history)
yuuji@171 739 buffer)))
yuuji@51 740
yuuji@227 741 (defvar YaTeX-call-builtin-on-file)
yuuji@227 742 (make-variable-buffer-local 'YaTeX-call-builtin-on-file)
yuuji@227 743 (defun YaTeX-call-builtin-on-file (builtin-type &optional default update)
yuuji@224 744 "Call command on file specified by BUILTIN-TYPE."
yuuji@51 745 (YaTeX-save-buffers)
yuuji@224 746 (let*((main (or YaTeX-parent-file
yuuji@227 747 (save-excursion (YaTeX-visit-main t) buffer-file-name)))
yuuji@224 748 (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.))))
yuuji@227 749 (alist YaTeX-call-builtin-on-file)
yuuji@227 750 (b-in (or (YaTeX-get-builtin builtin-type)
yuuji@227 751 (cdr (assoc builtin-type alist))))
yuuji@227 752 (command b-in))
yuuji@227 753 (if (or update (null b-in))
yuuji@227 754 (progn
yuuji@227 755 (setq command (read-string-with-history
yuuji@227 756 (format "%s command: " builtin-type)
yuuji@227 757 (or b-in
yuuji@227 758 (format "%s %s" default mainroot))
yuuji@227 759 'YaTeX-call-command-history))
yuuji@227 760 (if (or update (null b-in))
yuuji@362 761 (if (y-or-n-p "Memorize this command line in this file? ")
yuuji@227 762 (YaTeX-getset-builtin builtin-type command) ;keep in a file
yuuji@227 763 (setq YaTeX-call-builtin-on-file ;keep in memory
yuuji@227 764 (cons (cons builtin-type command)
yuuji@362 765 (delete (assoc builtin-type alist) alist)))
yuuji@362 766 (message "`%s' kept in memory. Type `%s %s' to override."
yuuji@362 767 command
yuuji@362 768 (key-description
yuuji@362 769 (car (where-is-internal 'universal-argument)))
yuuji@362 770 (key-description (this-command-keys)))
yuuji@362 771 (sit-for 2)))))
yuuji@224 772 (YaTeX-typeset
yuuji@227 773 command
yuuji@362 774 (format " *YaTeX-%s*" (downcase builtin-type))
yuuji@362 775 builtin-type builtin-type)))
yuuji@51 776
yuuji@51 777 (defun YaTeX-kill-typeset-process (proc)
yuuji@51 778 "Kill process PROC after sending signal to PROC.
yuuji@51 779 PROC should be process identifier."
yuuji@51 780 (cond
yuuji@68 781 ((not (fboundp 'start-process))
yuuji@68 782 (error "This system can't have concurrent process."))
yuuji@51 783 ((or (null proc) (not (eq (process-status proc) 'run)))
yuuji@51 784 (message "Typesetting process is not running."))
yuuji@51 785 (t
yuuji@51 786 (save-excursion
yuuji@51 787 (set-buffer (process-buffer proc))
yuuji@51 788 (save-excursion
yuuji@51 789 (goto-char (point-max))
yuuji@51 790 (beginning-of-line)
yuuji@51 791 (if (looking-at "\\? +$")
yuuji@51 792 (let ((mp (point-max)))
yuuji@51 793 (process-send-string proc "x\n")
yuuji@51 794 (while (= mp (point-max)) (sit-for 1))))))
yuuji@51 795 (if (eq (process-status proc) 'run)
yuuji@51 796 (progn
yuuji@51 797 (interrupt-process proc)
yuuji@69 798 (delete-process proc))))))
yuuji@51 799
yuuji@351 800 (defun YaTeX-system (command name &optional noask basedir)
yuuji@351 801 "Execute some COMMAND with process name `NAME'. Not a official function.
yuuji@351 802 Optional second argument NOASK skip query when privious process running.
yuuji@351 803 Optional third argument BASEDIR changes default-directory there."
yuuji@51 804 (save-excursion
yuuji@351 805 (let ((df default-directory)
yuuji@351 806 (buffer (get-buffer-create (format " *%s*" name)))
yuuji@351 807 proc status)
yuuji@351 808 (set-buffer buffer)
yuuji@351 809 (setq default-directory (cd (or basedir df)))
yuuji@351 810 (erase-buffer)
yuuji@362 811 (insert (format "Calling `%s'...\n" command)
yuuji@362 812 "==Kill this buffer to STOP process==")
yuuji@354 813 (YaTeX-showup-buffer buffer 'YaTeX-showup-buffer-bottom-most)
yuuji@351 814 (if (not (fboundp 'start-process))
yuuji@351 815 (call-process
yuuji@351 816 shell-file-name nil buffer nil YaTeX-shell-command-option command)
yuuji@351 817 (if (and (setq proc (get-buffer-process buffer))
yuuji@351 818 (setq status (process-status proc))
yuuji@351 819 (eq status 'run)
yuuji@351 820 (not noask)
yuuji@351 821 (not
yuuji@351 822 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
yuuji@351 823 nil
yuuji@351 824 (if (eq status 'run)
yuuji@351 825 (progn (interrupt-process proc) (delete-process proc)))
yuuji@351 826 (set-process-buffer
yuuji@351 827 (start-process
yuuji@351 828 name buffer shell-file-name YaTeX-shell-command-option command)
yuuji@351 829 (get-buffer buffer)))))))
yuuji@51 830
yuuji@72 831 (defvar YaTeX-default-paper-type "a4"
yuuji@72 832 "*Default paper type.")
yuuji@72 833 (defconst YaTeX-paper-type-alist
yuuji@72 834 '(("a4paper" . "a4")
yuuji@72 835 ("a5paper" . "a5")
yuuji@72 836 ("b4paper" . "b4")
yuuji@72 837 ("b5paper" . "b5"))
yuuji@72 838 "Holds map of options and paper types.")
yuuji@72 839 (defconst YaTeX-dvips-paper-option-alist
yuuji@72 840 '(("a4" . "-t a4")
yuuji@72 841 ("a5" . "-t a5")
yuuji@72 842 ("b4" . "-t b4")
yuuji@72 843 ("b5" . "-t b5")
yuuji@72 844 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
yuuji@72 845 ("a5r" . "-t landscape")
yuuji@72 846 ("b4r" . "-t landscape")
yuuji@72 847 ("b5r" . "-t landscape"))
yuuji@72 848 "Holds map of dvips options and paper types.")
yuuji@72 849 (defun YaTeX-get-paper-type ()
yuuji@72 850 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
yuuji@72 851 (save-excursion
yuuji@77 852 (YaTeX-visit-main t)
yuuji@72 853 (goto-char (point-min))
yuuji@77 854 (let ((opts
yuuji@77 855 (if (re-search-forward
yuuji@77 856 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
yuuji@77 857 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
yuuji@77 858 (concat
yuuji@77 859 (catch 'found-paper
yuuji@77 860 (mapcar (lambda (pair)
yuuji@79 861 (if (YaTeX-member (car pair) opts)
yuuji@77 862 (throw 'found-paper (cdr pair))))
yuuji@77 863 YaTeX-paper-type-alist)
yuuji@77 864 YaTeX-default-paper-type)
yuuji@79 865 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
yuuji@72 866
yuuji@51 867 (defvar YaTeX-preview-command-history nil
yuuji@51 868 "Holds minibuffer history of preview command.")
yuuji@51 869 (put 'YaTeX-preview-command-history 'no-default t)
yuuji@51 870 (defvar YaTeX-preview-file-history nil
yuuji@51 871 "Holds minibuffer history of file to preview.")
yuuji@51 872 (put 'YaTeX-preview-file-history 'no-default t)
yuuji@328 873 (defun YaTeX-preview-default-previewer ()
yuuji@328 874 "Return default previewer for this document"
yuuji@328 875 (YaTeX-replace-format
yuuji@325 876 (or (YaTeX-get-builtin "PREVIEW")
yuuji@325 877 (if (eq (get 'dvi2-command 'format) 'pdf)
yuuji@325 878 tex-pdfview-command
yuuji@325 879 dvi2-command))
yuuji@86 880 "p" (format (cond
yuuji@86 881 (YaTeX-dos "-y:%s")
yuuji@86 882 (t "-paper %s"))
yuuji@328 883 (YaTeX-get-paper-type))))
yuuji@328 884 (defun YaTeX-preview-default-main (command)
yuuji@328 885 "Return default preview target file"
yuuji@328 886 (if (get 'dvi2-command 'region)
yuuji@86 887 (substring YaTeX-texput-file
yuuji@86 888 0 (rindex YaTeX-texput-file ?.))
yuuji@328 889 (YaTeX-get-preview-file-name command)))
yuuji@328 890 (defun YaTeX-preview (preview-command preview-file &optional as-default)
yuuji@328 891 "Execute xdvi (or other) to tex-preview."
yuuji@328 892 (interactive
yuuji@328 893 (let* ((previewer (YaTeX-preview-default-previewer))
yuuji@328 894 (as-default current-prefix-arg)
yuuji@328 895 (command (if as-default
yuuji@328 896 previewer
yuuji@328 897 (read-string-with-history
yuuji@328 898 "Preview command: "
yuuji@328 899 previewer
yuuji@328 900 'YaTeX-preview-command-history)))
yuuji@328 901 (target (YaTeX-preview-default-main command))
yuuji@328 902 (file (if as-default
yuuji@328 903 target
yuuji@328 904 (read-string-with-history
yuuji@328 905 "Preview file: "
yuuji@328 906 target
yuuji@328 907 'YaTeX-preview-file-history))))
yuuji@86 908 (list command file)))
yuuji@328 909 (setq dvi2-command preview-command) ;`dvi2-command' is buffer local
yuuji@51 910 (save-excursion
yuuji@51 911 (YaTeX-visit-main t)
yuuji@86 912 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
yuuji@51 913 (let ((pbuffer "*dvi-preview*") (dir default-directory))
yuuji@51 914 (YaTeX-showup-buffer
yuuji@354 915 pbuffer 'YaTeX-showup-buffer-bottom-most)
yuuji@51 916 (set-buffer (get-buffer-create pbuffer))
yuuji@51 917 (erase-buffer)
yuuji@51 918 (setq default-directory dir) ;for 18
yuuji@51 919 (cd dir) ;for 19
yuuji@51 920 (cond
yuuji@64 921 ((not (fboundp 'start-process)) ;if MS-DOS
yuuji@51 922 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
yuuji@51 923 (call-process shell-file-name "con" "*dvi-preview*" nil
yuuji@60 924 YaTeX-shell-command-option
yuuji@61 925 (concat preview-command " " preview-file))
yuuji@51 926 (send-string-to-terminal "\e[>5l") ;show cursor
yuuji@51 927 (redraw-display))
yuuji@69 928 ((and (string-match "dviout" preview-command) ;maybe on `kon'
yuuji@69 929 (stringp (getenv "TERM"))
yuuji@69 930 (string-match "^kon" (getenv "TERM")))
yuuji@69 931 (call-process shell-file-name "con" "*dvi-preview*" nil
yuuji@69 932 YaTeX-shell-command-option
yuuji@69 933 (concat preview-command " " preview-file)))
yuuji@51 934 (t ;if UNIX
yuuji@51 935 (set-process-buffer
yuuji@86 936 (let ((process-connection-type nil))
yuuji@86 937 (start-process "preview" "*dvi-preview*" shell-file-name
yuuji@86 938 YaTeX-shell-command-option
yuuji@86 939 (concat preview-command " " preview-file)))
yuuji@51 940 (get-buffer pbuffer))
yuuji@51 941 (message
yuuji@51 942 (concat "Starting " preview-command
yuuji@69 943 " to preview " preview-file)))))))
yuuji@51 944
yuuji@58 945 (defvar YaTeX-xdvi-remote-program "xdvi")
yuuji@58 946 (defun YaTeX-xdvi-remote-search (&optional region-mode)
yuuji@58 947 "Search string at the point on xdvi -remote window.
yuuji@58 948 Non-nil for optional argument REGION-MODE specifies the search string
yuuji@58 949 by region."
yuuji@58 950 (interactive "P")
yuuji@58 951 (let ((pb " *xdvi*") str proc)
yuuji@58 952 (save-excursion
yuuji@58 953 (if region-mode
yuuji@58 954 (setq str (buffer-substring (region-beginning) (region-end)))
yuuji@58 955 (setq str (buffer-substring
yuuji@58 956 (point)
yuuji@58 957 (progn (skip-chars-forward "^\n\\\\}") (point)))))
yuuji@58 958 (message "Searching `%s'..." str)
yuuji@58 959 (if (boundp 'MULE)
yuuji@58 960 (define-program-coding-system
yuuji@58 961 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
yuuji@58 962 *euc-japan*))
yuuji@58 963 (setq proc
yuuji@58 964 (start-process
yuuji@58 965 "xdvi" pb YaTeX-xdvi-remote-program
yuuji@58 966 "-remote" (format "SloppySearch(%s) " str)
yuuji@58 967 (concat (YaTeX-get-preview-file-name) ".dvi")))
yuuji@69 968 (message "Searching `%s'...Done" str))))
yuuji@58 969
yuuji@350 970 (defun YaTeX-preview-jlfmt-xdvi ()
yuuji@350 971 "Call xdvi -sourceposition to DVI corresponding to current main file"
yuuji@351 972 (interactive))
yuuji@350 973
yuuji@350 974 (defun YaTeX-preview-jump-line ()
yuuji@350 975 "Call jump-line function of various previewer on current main file"
yuuji@350 976 (interactive)
yuuji@350 977 (save-excursion
yuuji@350 978 (save-restriction
yuuji@350 979 (widen)
yuuji@350 980 (let*((pf (or YaTeX-parent-file
yuuji@350 981 (save-excursion (YaTeX-visit-main t) (buffer-file-name))))
yuuji@351 982 (pdir (file-name-directory pf))
yuuji@350 983 (bnr (substring pf 0 (string-match "\\....$" pf)))
yuuji@351 984 (cf (file-relative-name (buffer-file-name) pdir))
yuuji@350 985 (buffer (get-buffer-create " *preview-jump-line*"))
yuuji@350 986 (line (count-lines (point-min) (point-end-of-line)))
yuuji@350 987 (previewer (YaTeX-preview-default-previewer))
yuuji@350 988 (cmd (cond
yuuji@350 989 ((string-match "xdvi" previewer)
yuuji@350 990 (format "%s -nofork -sourceposition '%d %s' %s.dvi"
yuuji@350 991 YaTeX-xdvi-remote-program
yuuji@350 992 line cf bnr))
yuuji@350 993 ((string-match "Skim" previewer)
yuuji@352 994 (format "%s %d '%s.pdf' '%s'"
yuuji@350 995 YaTeX-cmd-displayline line bnr cf))
yuuji@350 996 ((string-match "evince" previewer)
yuuji@352 997 (format "%s '%s.pdf' %d '%s'"
yuuji@352 998 "fwdevince" bnr line cf))
yuuji@362 999 ;;
yuuji@362 1000 ;; These lines below for other PDF viewer is not confirmed
yuuji@362 1001 ;; yet. If you find correct command line, PLEASE TELL
yuuji@362 1002 ;; IT TO THE AUTHOR before publishing patch on the web.
yuuji@362 1003 ;; ..PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE..
yuuji@369 1004 ((string-match "sumatra" previewer) ;;??
yuuji@369 1005 (format "%s \"%s.pdf\" -forward-search \"%s\" %d"
yuuji@369 1006 ;;Send patch to the author, please
yuuji@369 1007 previewer bnr cf line))
yuuji@369 1008 ((string-match "qpdfview" previewer) ;;??
yuuji@369 1009 (format "%s '%s.pdf#src:%s:%d:0'"
yuuji@369 1010 ;;Send patch to the author, please
yuuji@369 1011 previewer bnr cf line))
yuuji@369 1012 ((string-match "okular" previewer) ;;??
yuuji@369 1013 (format "%s '%s.pdf#src:%d' '%s'"
yuuji@369 1014 ;;Send patch to the author, please
yuuji@369 1015 previewer bnr line cf))
yuuji@362 1016 )))
yuuji@351 1017 (YaTeX-system cmd "jump-line" 'noask pdir)))))
yuuji@350 1018
yuuji@350 1019 (defun YaTeX-goto-corresponding-viewer ()
yuuji@350 1020 (let ((cmd (or (YaTeX-get-builtin "!") tex-command)))
yuuji@350 1021 (if (string-match "-src\\|synctex=" cmd)
yuuji@350 1022 (progn
yuuji@350 1023 (YaTeX-preview-jump-line)
yuuji@350 1024 t) ;for YaTeX-goto-corresponding-*
yuuji@350 1025 nil)))
yuuji@350 1026
yuuji@350 1027 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
yuuji@52 1028 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
yuuji@52 1029 (cond
yuuji@52 1030 ((and (get 'dvi2-command 'region)
yuuji@52 1031 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
yuuji@52 1032 (set file-sym (get 'dvi2-command 'file))
yuuji@52 1033 (set line-sym
yuuji@52 1034 (+ (- (apply '- (get 'dvi2-command 'offset)))
yuuji@52 1035 (symbol-value line-sym)
yuuji@52 1036 -1)))))
yuuji@52 1037
yuuji@51 1038 (defun YaTeX-prev-error ()
yuuji@230 1039 "Visit position of previous typeset error or warning.
yuuji@51 1040 To avoid making confliction of line numbers by editing, jump to
yuuji@51 1041 error or warning lines in reverse order."
yuuji@51 1042 (interactive)
yuuji@230 1043 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
yuuji@230 1044 (cur-win (selected-window))
yuuji@230 1045 b0 bound errorp error-line typeset-win error-buffer error-win)
yuuji@51 1046 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@51 1047 (error "There is no typesetting buffer."))
yuuji@51 1048 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@230 1049 (if (and (markerp YaTeX-typeset-marker)
yuuji@230 1050 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
yuuji@230 1051 (setq bound YaTeX-typeset-marker))
yuuji@51 1052 (setq typeset-win (selected-window))
yuuji@51 1053 (if (re-search-backward
yuuji@51 1054 (concat "\\(" latex-error-regexp "\\)\\|\\("
yuuji@51 1055 latex-warning-regexp "\\)")
yuuji@230 1056 bound t)
yuuji@80 1057 (setq errorp (match-beginning 1))
yuuji@51 1058 (select-window cur-win)
yuuji@68 1059 (error "No more errors on %s" cur-buf))
yuuji@80 1060 (goto-char (setq b0 (match-beginning 0)))
yuuji@51 1061 (skip-chars-forward "^0-9" (match-end 0))
yuuji@51 1062 (setq error-line
yuuji@51 1063 (string-to-int
yuuji@51 1064 (buffer-substring
yuuji@51 1065 (point)
yuuji@51 1066 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
yuuji@70 1067 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
yuuji@51 1068 (if (or (null error-line) (equal 0 error-line))
yuuji@51 1069 (error "Can't detect error position."))
yuuji@52 1070 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
yuuji@52 1071 (setq error-win (get-buffer-window error-buffer))
yuuji@51 1072 (select-window cur-win)
yuuji@51 1073 (cond
yuuji@51 1074 (error-win (select-window error-win))
yuuji@51 1075 ((eq (get-lru-window) typeset-win)
yuuji@51 1076 (YaTeX-switch-to-buffer error-buffer))
yuuji@51 1077 (t (select-window (get-lru-window))
yuuji@51 1078 (YaTeX-switch-to-buffer error-buffer)))
yuuji@51 1079 (setq error-win (selected-window))
yuuji@51 1080 (goto-line error-line)
yuuji@51 1081 (message "LaTeX %s in `%s' on line: %d."
yuuji@80 1082 (if errorp "error" "warning")
yuuji@51 1083 error-buffer error-line)
yuuji@51 1084 (select-window typeset-win)
yuuji@51 1085 (skip-chars-backward "0-9")
yuuji@51 1086 (recenter (/ (window-height) 2))
yuuji@60 1087 (sit-for 1)
yuuji@80 1088 (goto-char b0)
yuuji@69 1089 (select-window error-win)))
yuuji@51 1090
yuuji@51 1091 (defun YaTeX-jump-error-line ()
yuuji@51 1092 "Jump to corresponding line on latex command's error message."
yuuji@51 1093 (interactive)
yuuji@51 1094 (let (error-line error-file error-buf)
yuuji@51 1095 (save-excursion
yuuji@51 1096 (beginning-of-line)
yuuji@51 1097 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
yuuji@51 1098 (point-end-of-line) t)))
yuuji@51 1099 (if (null error-line)
yuuji@51 1100 (if (eobp) (insert (this-command-keys))
yuuji@51 1101 (error "No line number expression."))
yuuji@51 1102 (goto-char (match-beginning 0))
yuuji@51 1103 (setq error-line (string-to-int
yuuji@51 1104 (buffer-substring (match-beginning 1) (match-end 1)))
yuuji@70 1105 error-file (expand-file-name
yuuji@70 1106 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
yuuji@52 1107 (YaTeX-set-virtual-error-position 'error-file 'error-line)
yuuji@52 1108 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
yuuji@51 1109 (if (null error-buf)
yuuji@51 1110 (error "`%s' is not found in this directory." error-file))
yuuji@51 1111 (YaTeX-showup-buffer error-buf nil t)
yuuji@69 1112 (goto-line error-line)))
yuuji@51 1113
yuuji@51 1114 (defun YaTeX-send-string ()
yuuji@51 1115 "Send string to current typeset process."
yuuji@51 1116 (interactive)
yuuji@51 1117 (if (and (eq (process-status YaTeX-typeset-process) 'run)
yuuji@51 1118 (>= (point) (process-mark YaTeX-typeset-process)))
yuuji@51 1119 (let ((b (process-mark YaTeX-typeset-process))
yuuji@51 1120 (e (point-end-of-line)))
yuuji@51 1121 (goto-char b)
yuuji@51 1122 (skip-chars-forward " \t" e)
yuuji@51 1123 (setq b (point))
yuuji@51 1124 (process-send-string
yuuji@51 1125 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
yuuji@51 1126 (goto-char e)
yuuji@51 1127 (insert "\n")
yuuji@51 1128 (set-marker (process-mark YaTeX-typeset-process) (point))
yuuji@51 1129 (insert " "))
yuuji@69 1130 (ding)))
yuuji@51 1131
yuuji@51 1132 (defun YaTeX-view-error ()
yuuji@51 1133 (interactive)
yuuji@51 1134 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@51 1135 (message "No typeset buffer found.")
yuuji@51 1136 (let ((win (selected-window)))
yuuji@51 1137 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@51 1138 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
yuuji@51 1139 ;; automatically scrolled up at typesetting.
yuuji@51 1140 ;;(goto-char (point-max))
yuuji@51 1141 ;;(forward-line -1)
yuuji@51 1142 ;;(recenter -1)
yuuji@69 1143 (select-window win))))
yuuji@51 1144
yuuji@51 1145 (defun YaTeX-get-error-file (default)
yuuji@51 1146 "Get current processing file from typesetting log."
yuuji@51 1147 (save-excursion
yuuji@51 1148 (let(s)
yuuji@51 1149 (condition-case () (up-list -1)
yuuji@51 1150 (error
yuuji@51 1151 (let ((list 0) found)
yuuji@51 1152 (while
yuuji@51 1153 (and (<= list 0) (not found)
yuuji@51 1154 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
yuuji@51 1155 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
yuuji@51 1156 (setq list (1- list)) ;open paren
yuuji@51 1157 (setq list (1+ list))
yuuji@51 1158 (if (= list 1)
yuuji@51 1159 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
yuuji@51 1160 (setq found t)
yuuji@51 1161 (setq list (1- list)))))))))
yuuji@51 1162 (setq s
yuuji@51 1163 (buffer-substring
yuuji@51 1164 (progn (forward-char 1) (point))
yuuji@51 1165 (progn (skip-chars-forward "^ \n" (point-end-of-line))
yuuji@51 1166 (point))))
yuuji@69 1167 (if (string= "" s) default s))))
yuuji@51 1168
yuuji@51 1169 (defun YaTeX-put-nonstopmode ()
yuuji@64 1170 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
yuuji@51 1171 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
yuuji@51 1172 nil ;if already written in text then do nothing
yuuji@51 1173 (save-excursion
yuuji@51 1174 (YaTeX-visit-main t)
yuuji@51 1175 (goto-char (point-min))
yuuji@60 1176 (insert "\\nonstopmode{}%_YaTeX_%\n")
yuuji@69 1177 (if (buffer-file-name) (basic-save-buffer))))))
yuuji@51 1178
yuuji@51 1179 (defun YaTeX-remove-nonstopmode ()
yuuji@64 1180 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
yuuji@51 1181 (save-excursion
yuuji@51 1182 (YaTeX-visit-main t)
yuuji@51 1183 (goto-char (point-min))
yuuji@51 1184 (forward-line 1)
yuuji@51 1185 (narrow-to-region (point-min) (point))
yuuji@51 1186 (goto-char (point-min))
yuuji@51 1187 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
yuuji@69 1188 (widen))))
yuuji@51 1189
yuuji@80 1190 (defvar YaTeX-dvi2-command-ext-alist
yuuji@80 1191 '(("[agx]dvi\\|dviout" . ".dvi")
yuuji@80 1192 ("ghostview\\|gv" . ".ps")
yuuji@289 1193 ("acroread\\|xpdf\\|pdfopen\\|Preview\\|TeXShop\\|Skim\\|evince\\|mupdf\\|zathura\\|okular" . ".pdf")))
yuuji@80 1194
yuuji@86 1195 (defun YaTeX-get-preview-file-name (&optional preview-command)
yuuji@51 1196 "Get file name to preview by inquiring YaTeX-get-latex-command"
yuuji@86 1197 (if (null preview-command) (setq preview-command dvi2-command))
yuuji@51 1198 (let* ((latex-cmd (YaTeX-get-latex-command t))
yuuji@51 1199 (rin (rindex latex-cmd ? ))
yuuji@172 1200 (fname (if rin (substring latex-cmd (1+ rin)) ""))
yuuji@86 1201 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
yuuji@172 1202 (ext (if r (cdr r) "")))
yuuji@325 1203 (and (null r)
yuuji@325 1204 (eq (get 'dvi2-command 'format) 'pdf)
yuuji@325 1205 (setq ext "pdf"))
yuuji@80 1206 (concat
yuuji@80 1207 (if (string= fname "")
yuuji@172 1208 (setq fname (substring (file-name-nondirectory
yuuji@172 1209 (buffer-file-name))
yuuji@172 1210 0 -4))
yuuji@172 1211 (setq fname (substring fname 0 (rindex fname ?.))))
yuuji@80 1212 ext)))
yuuji@51 1213
yuuji@51 1214 (defun YaTeX-get-latex-command (&optional switch)
yuuji@51 1215 "Specify the latex-command name and its argument.
yuuji@51 1216 If there is a line which begins with string: \"%#!\", the following
yuuji@51 1217 strings are assumed to be the latex-command and arguments. The
yuuji@51 1218 default value of latex-command is:
yuuji@51 1219 tex-command FileName
yuuji@51 1220 and if you write \"%#!jlatex\" in the beginning of certain line.
yuuji@51 1221 \"jlatex \" FileName
yuuji@51 1222 will be the latex-command,
yuuji@51 1223 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
yuuji@51 1224 is non-nil, then
yuuji@51 1225 \"jlatex main.tex\"
yuuji@51 1226
yuuji@51 1227 will be given to the shell."
yuuji@80 1228 (let (parent tparent magic)
yuuji@51 1229 (setq parent
yuuji@51 1230 (cond
yuuji@80 1231 (YaTeX-parent-file
yuuji@80 1232 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
yuuji@80 1233 YaTeX-parent-file))
yuuji@51 1234 (t (save-excursion
yuuji@51 1235 (YaTeX-visit-main t)
yuuji@51 1236 (file-name-nondirectory (buffer-file-name)))))
yuuji@80 1237 magic (YaTeX-get-builtin "!")
yuuji@80 1238 tparent (file-name-nondirectory parent))
yuuji@80 1239 (YaTeX-replace-formats
yuuji@80 1240 (cond
yuuji@80 1241 (magic
yuuji@80 1242 (cond
yuuji@350 1243 (switch (if (string-match "\\s [^-]\\S *$" magic) magic
yuuji@80 1244 (concat magic " " parent)))
yuuji@350 1245 (t (concat (substring magic 0 (string-match "\\s [^-]\\S *$" magic)) " "))))
yuuji@80 1246 (t (concat tex-command " " (if switch parent))))
yuuji@80 1247 (list (cons "f" tparent)
yuuji@80 1248 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
yuuji@51 1249
yuuji@51 1250 (defvar YaTeX-lpr-command-history nil
yuuji@51 1251 "Holds command line history of YaTeX-lpr.")
yuuji@51 1252 (put 'YaTeX-lpr-command-history 'no-default t)
yuuji@79 1253 (defvar YaTeX-lpr-ask-page-range-default t)
yuuji@51 1254 (defun YaTeX-lpr (arg)
yuuji@79 1255 "Print out.
yuuji@79 1256 If prefix arg ARG is non nil, call print driver without
yuuji@51 1257 page range description."
yuuji@51 1258 (interactive "P")
yuuji@79 1259 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
yuuji@51 1260 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
yuuji@52 1261 from to (lbuffer "*dvi-printing*") dir)
yuuji@51 1262 (setq
yuuji@51 1263 cmd
yuuji@51 1264 (YaTeX-replace-format
yuuji@51 1265 cmd "f"
yuuji@51 1266 (if (or arg (not (string-match "%f" cmd)))
yuuji@51 1267 ""
yuuji@51 1268 (YaTeX-replace-format
yuuji@51 1269 dviprint-from-format
yuuji@51 1270 "b"
yuuji@51 1271 (if (string=
yuuji@51 1272 (setq from (read-string "From page(default 1): ")) "")
yuuji@51 1273 "1" from))))
yuuji@51 1274 )
yuuji@51 1275 (setq
yuuji@51 1276 cmd
yuuji@51 1277 (YaTeX-replace-format
yuuji@51 1278 cmd "t"
yuuji@51 1279 (if (or arg (not (string-match "%t" cmd))
yuuji@51 1280 (string=
yuuji@51 1281 (setq to (read-string "To page(default none): ")) ""))
yuuji@51 1282 ""
yuuji@51 1283 (YaTeX-replace-format dviprint-to-format "e" to)))
yuuji@51 1284 )
yuuji@72 1285 (setq
yuuji@72 1286 cmd
yuuji@72 1287 (YaTeX-replace-format
yuuji@72 1288 cmd "p"
yuuji@72 1289 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
yuuji@51 1290 (setq cmd
yuuji@51 1291 (read-string-with-history
yuuji@51 1292 "Edit command line: "
yuuji@53 1293 (format cmd
yuuji@53 1294 (if (get 'dvi2-command 'region)
yuuji@53 1295 (substring YaTeX-texput-file
yuuji@53 1296 0 (rindex YaTeX-texput-file ?.))
yuuji@53 1297 (YaTeX-get-preview-file-name)))
yuuji@51 1298 'YaTeX-lpr-command-history))
yuuji@51 1299 (save-excursion
yuuji@51 1300 (YaTeX-visit-main t) ;;change execution directory
yuuji@52 1301 (setq dir default-directory)
yuuji@51 1302 (YaTeX-showup-buffer
yuuji@354 1303 lbuffer 'YaTeX-showup-buffer-bottom-most)
yuuji@51 1304 (set-buffer (get-buffer-create lbuffer))
yuuji@51 1305 (erase-buffer)
yuuji@52 1306 (cd dir) ;for 19
yuuji@51 1307 (cond
yuuji@64 1308 ((not (fboundp 'start-process))
yuuji@51 1309 (call-process shell-file-name "con" "*dvi-printing*" nil
yuuji@51 1310 YaTeX-shell-command-option cmd))
yuuji@51 1311 (t
yuuji@51 1312 (set-process-buffer
yuuji@86 1313 (let ((process-connection-type nil))
yuuji@86 1314 (start-process "print" "*dvi-printing*" shell-file-name
yuuji@86 1315 YaTeX-shell-command-option cmd))
yuuji@51 1316 (get-buffer lbuffer))
yuuji@69 1317 (message "Starting printing command: %s..." cmd))))))
yuuji@51 1318
yuuji@51 1319 (defun YaTeX-main-file-p ()
yuuji@51 1320 "Return if current buffer is main LaTeX source."
yuuji@51 1321 (cond
yuuji@52 1322 (YaTeX-parent-file
yuuji@52 1323 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
yuuji@51 1324 ((YaTeX-get-builtin "!")
yuuji@70 1325 (string-match
yuuji@70 1326 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
yuuji@70 1327 (buffer-name)))
yuuji@51 1328 (t
yuuji@51 1329 (save-excursion
yuuji@57 1330 (let ((latex-main-id
yuuji@57 1331 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
yuuji@51 1332 (or (re-search-backward latex-main-id nil t)
yuuji@69 1333 (re-search-forward latex-main-id nil t)))))))
yuuji@51 1334
yuuji@51 1335 (defun YaTeX-visit-main (&optional setbuf)
yuuji@51 1336 "Switch buffer to main LaTeX source.
yuuji@122 1337 Use set-buffer instead of switch-to-buffer if the optional argument
yuuji@51 1338 SETBUF is t(Use it only from Emacs-Lisp program)."
yuuji@51 1339 (interactive "P")
yuuji@51 1340 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
yuuji@70 1341 (let ((ff (function (lambda (f)
yuuji@70 1342 (if setbuf (set-buffer (find-file-noselect f))
yuuji@70 1343 (find-file f)))))
yuuji@325 1344 b-in main-file mfa YaTeX-create-file-prefix-g
yuuji@70 1345 (hilit-auto-highlight (not setbuf)))
yuuji@51 1346 (if (setq b-in (YaTeX-get-builtin "!"))
yuuji@51 1347 (setq main-file (YaTeX-guess-parent b-in)))
yuuji@51 1348 (if YaTeX-parent-file
yuuji@51 1349 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
yuuji@51 1350 YaTeX-parent-file))
yuuji@51 1351 (if (YaTeX-main-file-p)
yuuji@51 1352 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
yuuji@51 1353 (cond
yuuji@70 1354 ((and ;;(interactive-p)
yuuji@70 1355 main-file
yuuji@70 1356 (cond ((get-file-buffer main-file)
yuuji@70 1357 (cond
yuuji@70 1358 (setbuf (set-buffer (get-file-buffer main-file)))
yuuji@70 1359 ((get-buffer-window (get-file-buffer main-file))
yuuji@70 1360 (select-window
yuuji@70 1361 (get-buffer-window (get-file-buffer main-file))))
yuuji@70 1362 (t (switch-to-buffer (get-file-buffer main-file)))))
yuuji@70 1363 ((file-exists-p main-file)
yuuji@70 1364 (funcall ff main-file)))))
yuuji@70 1365 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
yuuji@51 1366 ((and main-file
yuuji@51 1367 (file-exists-p (setq main-file (concat "../" main-file)))
yuuji@80 1368 (or b-in
yuuji@325 1369 (y-or-n-p (concat (setq mfa (expand-file-name main-file))
yuuji@80 1370 " is main file?:"))))
yuuji@325 1371 (setq YaTeX-parent-file mfa)
yuuji@70 1372 ;(YaTeX-switch-to-buffer main-file setbuf)
yuuji@70 1373 (funcall ff main-file)
yuuji@70 1374 )
yuuji@51 1375 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
yuuji@326 1376 (setq YaTeX-parent-file (expand-file-name main-file))
yuuji@70 1377 ; (YaTeX-switch-to-buffer main-file setbuf))
yuuji@70 1378 (funcall ff main-file))
yuuji@51 1379 )))
yuuji@69 1380 nil)
yuuji@51 1381
yuuji@51 1382 (defun YaTeX-guess-parent (command-line)
yuuji@51 1383 (setq command-line
yuuji@350 1384 (if (string-match "\\s \\([^-]\\S *\\)$" command-line)
yuuji@350 1385 (substring command-line (match-beginning 1))
yuuji@51 1386 (file-name-nondirectory (buffer-file-name)))
yuuji@51 1387 command-line
yuuji@51 1388 (concat (if (string-match "\\(.*\\)\\." command-line)
yuuji@51 1389 (substring command-line (match-beginning 1) (match-end 1))
yuuji@51 1390 command-line)
yuuji@69 1391 ".tex")))
yuuji@51 1392
yuuji@51 1393 (defun YaTeX-visit-main-other-window ()
yuuji@51 1394 "Switch to buffer main LaTeX source in other window."
yuuji@51 1395 (interactive)
yuuji@51 1396 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
yuuji@51 1397 (YaTeX-switch-to-buffer-other-window
yuuji@69 1398 (concat (YaTeX-get-preview-file-name) ".tex"))))
yuuji@51 1399
yuuji@51 1400 (defun YaTeX-save-buffers ()
yuuji@60 1401 "Save buffers whose major-mode is equal to current major-mode."
yuuji@51 1402 (basic-save-buffer)
yuuji@60 1403 (let ((cmm major-mode))
yuuji@60 1404 (save-excursion
yuuji@351 1405 (mapcar (function
yuuji@351 1406 (lambda (buf)
yuuji@60 1407 (set-buffer buf)
yuuji@60 1408 (if (and (buffer-file-name buf)
yuuji@60 1409 (eq major-mode cmm)
yuuji@60 1410 (buffer-modified-p buf)
yuuji@60 1411 (y-or-n-p (format "Save %s" (buffer-name buf))))
yuuji@351 1412 (save-buffer buf))))
yuuji@69 1413 (buffer-list)))))
yuuji@51 1414
yuuji@51 1415 (provide 'yatexprc)