yatex

annotate yatexprc.el @ 261:8c555014f933

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