yatex

annotate yatexprc.el @ 369:a536acc07a75

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