yatex

annotate yatexprc.el @ 122:57d3d9abd661

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