yatex

annotate yatexprc.el @ 224:9705855793ca

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