yatex

annotate yatexprc.el @ 46:cd1b63102eed

Support Mule2
author yuuji
date Mon, 19 Sep 1994 16:54:19 +0000
parents 92c6acff0f37
children d7e7b4654058
rev   line source
yuuji@7 1 ;;; -*- Emacs-Lisp -*-
yuuji@7 2 ;;; YaTeX process handler.
yuuji@13 3 ;;; yatexprc.el
yuuji@14 4 ;;; (c )1993-1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
yuuji@46 5 ;;; Last modified Fri Sep 16 01:50:36 1994 on figaro
yuuji@7 6 ;;; $Id$
yuuji@7 7
yuuji@7 8 (require 'yatex)
yuuji@7 9
yuuji@7 10 (defvar YaTeX-typeset-process nil
yuuji@7 11 "Process identifier for jlatex"
yuuji@7 12 )
yuuji@7 13 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
yuuji@7 14 "Process buffer for jlatex")
yuuji@7 15
yuuji@11 16 (defvar YaTeX-typeset-buffer-syntax nil
yuuji@11 17 "*Syntax table for typesetting buffer")
yuuji@11 18
yuuji@14 19 (defvar YaTeX-current-TeX-buffer nil
yuuji@14 20 "Keeps the buffer on which recently typeset run.")
yuuji@14 21
yuuji@11 22 (if YaTeX-typeset-buffer-syntax nil
yuuji@11 23 (setq YaTeX-typeset-buffer-syntax
yuuji@11 24 (make-syntax-table (standard-syntax-table)))
yuuji@11 25 (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
yuuji@11 26 (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
yuuji@11 27 (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
yuuji@11 28 (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax)
yuuji@11 29 )
yuuji@11 30
yuuji@7 31 (defun YaTeX-typeset (command buffer)
yuuji@7 32 "Execute jlatex (or other) to LaTeX typeset."
yuuji@7 33 (interactive)
yuuji@12 34 (let ((window (selected-window)))
yuuji@12 35 (if (and YaTeX-typeset-process
yuuji@12 36 (eq (process-status YaTeX-typeset-process) 'run))
yuuji@12 37 ;; if tex command is halting.
yuuji@12 38 (YaTeX-kill-typeset-process YaTeX-typeset-process))
yuuji@12 39 (YaTeX-visit-main t);;execution directory
yuuji@14 40 ;;Select lower-most window if there are more than 2 windows and
yuuji@13 41 ;;typeset buffer not seen.
yuuji@12 42 (YaTeX-showup-buffer
yuuji@12 43 buffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@12 44 (with-output-to-temp-buffer buffer
yuuji@13 45 (if YaTeX-dos ;if MS-DOS
yuuji@12 46 (progn
yuuji@23 47 (message (format "Calling `%s'..." command))
yuuji@12 48 (YaTeX-put-nonstopmode)
yuuji@12 49 (call-process shell-file-name
yuuji@12 50 nil buffer nil "/c" command)
yuuji@12 51 (YaTeX-remove-nonstopmode))
yuuji@12 52 (setq YaTeX-typeset-process ;if UNIX
yuuji@7 53 (start-process "LaTeX" buffer shell-file-name "-c"
yuuji@7 54 command))
yuuji@12 55 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
yuuji@14 56 (setq YaTeX-current-TeX-buffer (buffer-name))
yuuji@11 57 (select-window (get-buffer-window buffer))
yuuji@11 58 (use-local-map YaTeX-typesetting-mode-map)
yuuji@11 59 (set-syntax-table YaTeX-typeset-buffer-syntax)
yuuji@11 60 (setq mode-name "typeset")
yuuji@12 61 (if YaTeX-typeset-process ; if process is running (maybe on UNIX)
yuuji@11 62 (cond ((boundp 'MULE)
yuuji@11 63 (set-current-process-coding-system
yuuji@11 64 YaTeX-latex-message-code YaTeX-coding-system))
yuuji@11 65 ((boundp 'NEMACS)
yuuji@11 66 (set-kanji-process-code YaTeX-latex-message-code))))
yuuji@11 67 (goto-char (point-max))
yuuji@13 68 (if YaTeX-dos (message "Done.")
yuuji@23 69 (insert " ")
yuuji@12 70 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
yuuji@11 71 (if (bolp) (forward-line -1))
yuuji@11 72 (recenter -1)
yuuji@12 73 (select-window window))
yuuji@7 74 )
yuuji@7 75
yuuji@7 76 (defun YaTeX-typeset-sentinel (proc mes)
yuuji@7 77 (cond ((null (buffer-name (process-buffer proc)))
yuuji@7 78 ;; buffer killed
yuuji@7 79 (set-process-buffer proc nil))
yuuji@7 80 ((memq (process-status proc) '(signal exit))
yuuji@11 81 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
yuuji@23 82 (pwin (get-buffer-window pbuf))
yuuji@11 83 (owin (selected-window)) win)
yuuji@7 84 ;; save-excursion isn't the right thing if
yuuji@7 85 ;; process-buffer is current-buffer
yuuji@7 86 (unwind-protect
yuuji@7 87 (progn
yuuji@7 88 ;; Write something in *typesetting* and hack its mode line
yuuji@23 89 (if pwin
yuuji@23 90 (select-window pwin)
yuuji@23 91 (set-buffer pbuf))
yuuji@23 92 ;;(YaTeX-showup-buffer pbuf nil t)
yuuji@7 93 (goto-char (point-max))
yuuji@23 94 (if pwin (recenter -3))
yuuji@7 95 (insert ?\n "latex typesetting " mes)
yuuji@7 96 (forward-char -1)
yuuji@7 97 (insert " at " (substring (current-time-string) 0 -5) "\n")
yuuji@7 98 (forward-char 1)
yuuji@7 99 (setq mode-line-process
yuuji@7 100 (concat ": "
yuuji@7 101 (symbol-name (process-status proc))))
yuuji@7 102 (message "latex typesetting done.")
yuuji@13 103 ;; If buffer and mode line shows that the process
yuuji@7 104 ;; is dead, we can delete it now. Otherwise it
yuuji@7 105 ;; will stay around until M-x list-processes.
yuuji@7 106 (delete-process proc)
yuuji@7 107 )
yuuji@7 108 (setq YaTeX-typesetting-process nil)
yuuji@7 109 ;; Force mode line redisplay soon
yuuji@7 110 (set-buffer-modified-p (buffer-modified-p))
yuuji@7 111 )
yuuji@11 112 (select-window owin)
yuuji@7 113 (set-buffer obuf))))
yuuji@7 114 )
yuuji@7 115
yuuji@7 116 (defvar YaTeX-texput-file "texput.tex"
yuuji@7 117 "*File name for temporary file of typeset-region."
yuuji@7 118 )
yuuji@7 119
yuuji@7 120 (defun YaTeX-typeset-region ()
yuuji@7 121 "Paste the region to the file `texput.tex' and execute jlatex (or other)
yuuji@7 122 to LaTeX typeset. The region is specified by the rule:
yuuji@7 123 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
yuuji@7 124 (1-1)if the keyword `%#END' is found after `%#BEGIN',
yuuji@7 125 ->Assume the text between `%#BEGIN' and `%#END' as region.
yuuji@7 126 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
yuuji@7 127 ->Assume the text after `%#BEGIN' as region.
yuuji@7 128 (2)If no `%#BEGIN' usage is found before the (point),
yuuji@7 129 ->Assume the text between current (point) and (mark) as region.
yuuji@7 130 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
yuuji@7 131 operation to the region."
yuuji@7 132 (interactive)
yuuji@7 133 (save-excursion
yuuji@7 134 (let*
yuuji@7 135 ((end "") typeout ;Type out message that tells the method of cutting.
yuuji@7 136 (cmd (concat (YaTeX-get-latex-command nil) " " YaTeX-texput-file))
yuuji@23 137 (buffer (current-buffer)) opoint preamble (subpreamble "") main
yuuji@16 138 reg-begin reg-end)
yuuji@23 139
yuuji@38 140 (save-excursion
yuuji@38 141 (if (search-backward "%#BEGIN" nil t)
yuuji@38 142 (progn
yuuji@38 143 (setq typeout "--- Region from BEGIN to "
yuuji@38 144 end "the end of the buffer ---"
yuuji@38 145 reg-begin (match-end 0))
yuuji@38 146 (if (search-forward "%#END" nil t)
yuuji@38 147 (setq reg-end (match-beginning 0)
yuuji@38 148 end "END ---")
yuuji@38 149 (setq reg-end (point-max))))
yuuji@38 150 (setq typeout "=== Region from (point) to (mark) ===")
yuuji@38 151 (setq reg-begin (point) reg-end (mark)))
yuuji@38 152 (goto-char (point-min))
yuuji@38 153 (while (search-forward "%#REQUIRE" nil t)
yuuji@38 154 (setq subpreamble
yuuji@38 155 (concat subpreamble
yuuji@38 156 (cond
yuuji@38 157 ((eolp)
yuuji@38 158 (buffer-substring
yuuji@38 159 (match-beginning 0)
yuuji@38 160 (point-beginning-of-line)))
yuuji@38 161 (t (buffer-substring
yuuji@38 162 (match-end 0)
yuuji@38 163 (point-end-of-line))))
yuuji@38 164 "\n"))
yuuji@38 165 (goto-char (match-end 0))))
yuuji@7 166 (YaTeX-visit-main t)
yuuji@16 167 (setq main (current-buffer))
yuuji@7 168 (setq opoint (point))
yuuji@7 169 (goto-char (point-min))
yuuji@7 170 (setq
yuuji@7 171 preamble
yuuji@7 172 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
yuuji@7 173 (buffer-substring (point-min) (match-end 0))
yuuji@7 174 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
yuuji@7 175 "\\begin{document}")))
yuuji@7 176 (goto-char opoint)
yuuji@7 177 ;;(set-buffer buffer) ;for clarity
yuuji@16 178 (set-buffer (find-file-noselect YaTeX-texput-file))
yuuji@16 179 ;;(find-file YaTeX-texput-file)
yuuji@7 180 (erase-buffer)
yuuji@7 181 (if YaTeX-need-nonstop
yuuji@7 182 (insert "\\nonstopmode{}\n"))
yuuji@23 183 (insert preamble "\n" subpreamble "\n")
yuuji@16 184 (insert-buffer-substring buffer reg-begin reg-end)
yuuji@7 185 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
yuuji@7 186 (insert "\n\\end{document}\n")
yuuji@7 187 (basic-save-buffer)
yuuji@7 188 (kill-buffer (current-buffer))
yuuji@16 189 (set-buffer main) ;return to parent file or itself.
yuuji@7 190 (YaTeX-typeset cmd YaTeX-typeset-buffer)
yuuji@44 191 (switch-to-buffer buffer) ;for Emacs-19
yuuji@7 192 (put 'dvi2-command 'region t)))
yuuji@7 193 )
yuuji@7 194
yuuji@7 195 (defun YaTeX-typeset-buffer ()
yuuji@7 196 "Typeset whole buffer. If %#! usage says other buffer is main text,
yuuji@7 197 visit main buffer to confirm if its includeonly list contains current
yuuji@7 198 buffer's file. And if it doesn't contain editing text, ask user which
yuuji@13 199 action wants to be done, A:Add list, R:Replace list, %:comment-out list."
yuuji@7 200 (interactive)
yuuji@7 201 (YaTeX-save-buffers)
yuuji@7 202 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
yuuji@7 203 (mydir (file-name-directory (buffer-file-name)))
yuuji@7 204 (cmd (YaTeX-get-latex-command t)))
yuuji@7 205 (if (YaTeX-main-file-p) nil
yuuji@7 206 (save-excursion
yuuji@7 207 (YaTeX-visit-main t) ;search into main buffer
yuuji@7 208 (save-excursion
yuuji@7 209 (push-mark (point) t)
yuuji@7 210 (goto-char (point-min))
yuuji@7 211 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
yuuji@7 212 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
yuuji@7 213 (let*
yuuji@7 214 ((b (progn (skip-chars-forward "^{") (point)))
yuuji@7 215 (e (progn (skip-chars-forward "^}") (1+ (point))))
yuuji@7 216 (s (buffer-substring b e)) c
yuuji@7 217 (pardir (file-name-directory (buffer-file-name))))
yuuji@7 218 (if (string-match (concat "[{,/]" me "[,}]") s)
yuuji@7 219 nil ; Nothing to do when it's already in includeonly.
yuuji@7 220 (ding)
yuuji@7 221 (switch-to-buffer (current-buffer));Display this buffer.
yuuji@7 222 (setq
yuuji@7 223 me ;;Rewrite my name(me) to contain sub directory name.
yuuji@7 224 (concat
yuuji@7 225 (if (string-match pardir mydir) ;if mydir is child of main
yuuji@7 226 (substring mydir (length pardir)) ;cut absolute path
yuuji@7 227 mydir) ;else concat absolute path name.
yuuji@7 228 me))
yuuji@7 229 (message
yuuji@7 230 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
yuuji@7 231 me)
yuuji@7 232 (setq c (read-char))
yuuji@7 233 (cond
yuuji@7 234 ((= c ?a)
yuuji@7 235 (goto-char (1+ b))
yuuji@7 236 (insert me (if (string= s "{}") "" ",")))
yuuji@7 237 ((= c ?r)
yuuji@7 238 (delete-region (1+ b) (1- e)) (insert me))
yuuji@7 239 ((= c ?%)
yuuji@7 240 (beginning-of-line) (insert "%"))
yuuji@7 241 (t nil))
yuuji@7 242 (basic-save-buffer))))
yuuji@7 243 (exchange-point-and-mark))
yuuji@7 244 ))
yuuji@7 245 (YaTeX-typeset cmd YaTeX-typeset-buffer)
yuuji@7 246 (put 'dvi2-command 'region nil))
yuuji@7 247 )
yuuji@7 248
yuuji@16 249 (defvar YaTeX-call-command-history nil
yuuji@16 250 "Holds history list of YaTeX-call-command-on-file.")
yuuji@16 251 (put 'YaTeX-call-command-history 'no-default t)
yuuji@11 252 (defun YaTeX-call-command-on-file (base-cmd buffer)
yuuji@11 253 (YaTeX-save-buffers)
yuuji@11 254 (YaTeX-typeset
yuuji@16 255 (let ((minibufer-history-symbol 'YaTeX-call-command-history))
yuuji@16 256 (read-string "Call command: "
yuuji@16 257 (concat base-cmd " " (YaTeX-get-preview-file-name))))
yuuji@11 258 buffer)
yuuji@11 259 )
yuuji@11 260
yuuji@11 261 (defun YaTeX-bibtex-buffer (cmd)
yuuji@7 262 "Pass the bibliography data of editing file to bibtex."
yuuji@7 263 (interactive)
yuuji@7 264 (YaTeX-save-buffers)
yuuji@11 265 (YaTeX-call-command-on-file cmd "*YaTeX-bibtex*" )
yuuji@7 266 )
yuuji@7 267
yuuji@7 268 (defun YaTeX-kill-typeset-process (proc)
yuuji@7 269 "Kill process PROC after sending signal to PROC.
yuuji@7 270 PROC should be process identifier."
yuuji@7 271 (cond
yuuji@13 272 (YaTeX-dos
yuuji@7 273 (error "MS-DOS can't have concurrent process."))
yuuji@7 274 ((or (null proc) (not (eq (process-status proc) 'run)))
yuuji@7 275 (error "No typesetting process."))
yuuji@7 276 (t (interrupt-process proc)
yuuji@7 277 (delete-process proc)))
yuuji@7 278 )
yuuji@7 279
yuuji@7 280 (defun YaTeX-system (command buffer)
yuuji@7 281 "Execute some command on buffer. Not a official function."
yuuji@7 282 (save-excursion
yuuji@23 283 (YaTeX-showup-buffer
yuuji@23 284 buffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@7 285 (with-output-to-temp-buffer buffer
yuuji@13 286 (if YaTeX-dos
yuuji@7 287 (call-process shell-file-name nil buffer nil "/c " command)
yuuji@13 288 (start-process "system" buffer shell-file-name "-c" command))))
yuuji@7 289 )
yuuji@7 290
yuuji@16 291 (defvar YaTeX-preview-command-history nil
yuuji@16 292 "Holds minibuffer history of preview command.")
yuuji@16 293 (put 'YaTeX-preview-command-history 'no-default t)
yuuji@16 294 (defvar YaTeX-preview-file-history nil
yuuji@16 295 "Holds minibuffer history of file to preview.")
yuuji@16 296 (put 'YaTeX-preview-file-history 'no-default t)
yuuji@7 297 (defun YaTeX-preview (preview-command preview-file)
yuuji@7 298 "Execute xdvi (or other) to tex-preview."
yuuji@7 299 (interactive
yuuji@16 300 (list
yuuji@16 301 (let ((minibuffer-history-symbol 'YaTeX-preview-command-history))
yuuji@16 302 (read-string "Preview command: " dvi2-command))
yuuji@16 303 (let ((minibuffer-history-symbol 'YaTeX-preview-file-history))
yuuji@16 304 (read-string "Preview file[.dvi]: "
yuuji@16 305 (if (get 'dvi2-command 'region)
yuuji@16 306 (substring YaTeX-texput-file
yuuji@16 307 0 (rindex YaTeX-texput-file ?.))
yuuji@16 308 (YaTeX-get-preview-file-name))
yuuji@16 309 ))))
yuuji@23 310 (setq dvi2-command preview-command) ;`dvi2command' is buffer local
yuuji@7 311 (save-excursion
yuuji@7 312 (YaTeX-visit-main t)
yuuji@23 313 (let ((pbuffer "*dvi-preview*"))
yuuji@23 314 (YaTeX-showup-buffer
yuuji@29 315 pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@23 316 (with-output-to-temp-buffer pbuffer
yuuji@23 317 (if YaTeX-dos ;if MS-DOS
yuuji@23 318 (progn (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
yuuji@23 319 (call-process shell-file-name "con" "*dvi-preview*" nil
yuuji@23 320 "/c " preview-command preview-file)
yuuji@23 321 (send-string-to-terminal "\e[>5l") ;show cursor
yuuji@23 322 (redraw-display))
yuuji@23 323 (start-process "preview" "*dvi-preview*" shell-file-name "-c"
yuuji@23 324 (concat preview-command " " preview-file)) ;if UNIX
yuuji@23 325 (message
yuuji@23 326 (concat "Starting " preview-command
yuuji@23 327 " to preview " preview-file))))))
yuuji@7 328 )
yuuji@7 329
yuuji@7 330 (defun YaTeX-prev-error ()
yuuji@14 331 "Visit previous typeset error.
yuuji@14 332 To avoid making confliction of line numbers by editing, jump to
yuuji@14 333 error or warning lines in reverse order."
yuuji@7 334 (interactive)
yuuji@11 335 (let ((cur-buf (buffer-name)) (cur-win (selected-window))
yuuji@14 336 error-line typeset-win error-buffer error-win)
yuuji@7 337 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@14 338 (error "There is no typesetting buffer."))
yuuji@23 339 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@14 340 (setq typeset-win (selected-window))
yuuji@14 341 (if (re-search-backward
yuuji@14 342 (concat "\\(" latex-error-regexp "\\)\\|\\("
yuuji@14 343 latex-warning-regexp "\\)")
yuuji@14 344 nil t)
yuuji@14 345 nil
yuuji@11 346 (select-window cur-win)
yuuji@14 347 (error "No more erros on %s" cur-buf))
yuuji@14 348 (goto-char (match-beginning 0))
yuuji@14 349 (skip-chars-forward "^0-9" (match-end 0))
yuuji@14 350 (setq error-line
yuuji@14 351 (string-to-int
yuuji@14 352 (buffer-substring
yuuji@14 353 (point)
yuuji@14 354 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
yuuji@14 355 error-buffer (YaTeX-get-error-file cur-buf)
yuuji@14 356 error-win (get-buffer-window error-buffer))
yuuji@14 357 (if (or (null error-line) (equal 0 error-line))
yuuji@14 358 (error "Can't detect error position."))
yuuji@14 359 (select-window cur-win)
yuuji@14 360 (cond
yuuji@14 361 (error-win (select-window error-win))
yuuji@14 362 ((eq (get-lru-window) typeset-win)
yuuji@14 363 (YaTeX-switch-to-buffer error-buffer))
yuuji@14 364 (t (select-window (get-lru-window))
yuuji@14 365 (YaTeX-switch-to-buffer error-buffer)))
yuuji@14 366 (setq error-win (selected-window))
yuuji@14 367 (goto-line error-line)
yuuji@14 368 (message "LaTeX %s in `%s' on line: %d."
yuuji@14 369 (if (match-beginning 1) "error" "warning")
yuuji@14 370 error-buffer error-line)
yuuji@14 371 (select-window typeset-win)
yuuji@14 372 (skip-chars-backward "0-9")
yuuji@14 373 (recenter (/ (window-height) 2))
yuuji@14 374 (sit-for 3)
yuuji@14 375 (goto-char (match-beginning 0))
yuuji@14 376 (select-window error-win))
yuuji@7 377 )
yuuji@7 378
yuuji@7 379 (defun YaTeX-jump-error-line ()
yuuji@13 380 "Jump to corresponding line on latex command's error message."
yuuji@7 381 (interactive)
yuuji@14 382 (let (error-line error-file error-buf)
yuuji@14 383 (save-excursion
yuuji@14 384 (beginning-of-line)
yuuji@14 385 (setq error-line (re-search-forward "l[ ines]*\\.\\([1-9][0-9]*\\)"
yuuji@14 386 (point-end-of-line) t)))
yuuji@14 387 (if (null error-line)
yuuji@14 388 (if (eobp) (insert (this-command-keys))
yuuji@14 389 (error "No line number expression."))
yuuji@7 390 (goto-char (match-beginning 0))
yuuji@14 391 (setq error-line (string-to-int
yuuji@14 392 (buffer-substring (match-beginning 1) (match-end 1)))
yuuji@14 393 error-file (YaTeX-get-error-file YaTeX-current-TeX-buffer)
yuuji@14 394 error-buf (YaTeX-switch-to-buffer error-file t))
yuuji@14 395 (if (null error-buf)
yuuji@14 396 (error "`%s' is not found in this directory." error-file))
yuuji@14 397 (YaTeX-showup-buffer error-buf nil t)
yuuji@14 398 (goto-line error-line)))
yuuji@7 399 )
yuuji@7 400
yuuji@7 401 (defun YaTeX-send-string ()
yuuji@7 402 "Send string to current typeset process."
yuuji@7 403 (interactive)
yuuji@7 404 (if (and (eq (process-status YaTeX-typeset-process) 'run)
yuuji@7 405 (>= (point) (process-mark YaTeX-typeset-process)))
yuuji@7 406 (let ((b (process-mark YaTeX-typeset-process))
yuuji@7 407 (e (point-end-of-line)))
yuuji@7 408 (goto-char b)
yuuji@7 409 (skip-chars-forward " \t" e)
yuuji@7 410 (setq b (point))
yuuji@7 411 (process-send-string
yuuji@7 412 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
yuuji@7 413 (goto-char e)
yuuji@7 414 (insert "\n")
yuuji@7 415 (set-marker (process-mark YaTeX-typeset-process) (point))
yuuji@7 416 (insert " "))
yuuji@7 417 (ding))
yuuji@7 418 )
yuuji@7 419
yuuji@7 420 (defun YaTeX-view-error ()
yuuji@7 421 (interactive)
yuuji@7 422 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@7 423 (message "No typeset buffer found.")
yuuji@11 424 (let ((win (selected-window)))
yuuji@23 425 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@11 426 (goto-char (point-max))
yuuji@23 427 (forward-line -1)
yuuji@11 428 (recenter -1)
yuuji@11 429 (select-window win)))
yuuji@7 430 )
yuuji@7 431
yuuji@7 432 (defun YaTeX-get-error-file (default)
yuuji@7 433 "Get current processing file from typesetting log."
yuuji@7 434 (save-excursion
yuuji@7 435 (let(s)
yuuji@7 436 (condition-case () (up-list -1)
yuuji@7 437 (error
yuuji@7 438 (let ((list 0) found)
yuuji@7 439 (while
yuuji@7 440 (and (<= list 0) (not found)
yuuji@7 441 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
yuuji@7 442 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
yuuji@7 443 (setq list (1- list)) ;open paren
yuuji@7 444 (setq list (1+ list))
yuuji@7 445 (if (= list 1)
yuuji@7 446 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
yuuji@7 447 (setq found t)
yuuji@7 448 (setq list (1- list)))))))))
yuuji@7 449 (setq s
yuuji@7 450 (buffer-substring
yuuji@7 451 (progn (forward-char 1) (point))
yuuji@7 452 (progn (skip-chars-forward "-A-Za-z0-9_/\.\\" (point-end-of-line))
yuuji@7 453 (point))))
yuuji@7 454 (if (string= "" s) default s)))
yuuji@7 455 )
yuuji@7 456
yuuji@7 457 (defun YaTeX-put-nonstopmode ()
yuuji@7 458 (if YaTeX-need-nonstop
yuuji@14 459 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
yuuji@7 460 nil ;if already written in text then do nothing
yuuji@7 461 (save-excursion
yuuji@7 462 (YaTeX-visit-main t)
yuuji@7 463 (goto-char (point-min))
yuuji@7 464 (insert "\\nonstopmode{}%_YaTeX_%\n")))
yuuji@7 465 )
yuuji@7 466 )
yuuji@7 467
yuuji@7 468 (defun YaTeX-remove-nonstopmode ()
yuuji@7 469 (if YaTeX-need-nonstop ;for speed
yuuji@7 470 (save-excursion
yuuji@7 471 (YaTeX-visit-main t)
yuuji@7 472 (goto-char (point-min))
yuuji@7 473 (forward-line 1)
yuuji@7 474 (narrow-to-region (point-min) (point))
yuuji@7 475 (goto-char (point-min))
yuuji@7 476 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
yuuji@7 477 (widen)))
yuuji@7 478 )
yuuji@7 479
yuuji@7 480 (defun YaTeX-get-preview-file-name ()
yuuji@7 481 "Get file name to preview by inquiring YaTeX-get-latex-command"
yuuji@7 482 (let* ((latex-cmd (YaTeX-get-latex-command t))
yuuji@7 483 (rin (rindex latex-cmd ? ))
yuuji@7 484 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
yuuji@7 485 (period))
yuuji@7 486 (if (string= fname "")
yuuji@14 487 (setq fname (substring (file-name-nondirectory
yuuji@14 488 (buffer-file-name))
yuuji@14 489 0 -4))
yuuji@7 490 (setq period (rindex fname ?.))
yuuji@7 491 (setq fname (substring fname 0 (if (eq -1 period) nil period)))
yuuji@7 492 ))
yuuji@7 493 )
yuuji@7 494
yuuji@7 495 (defun YaTeX-get-latex-command (&optional switch)
yuuji@7 496 "Specify the latex-command name and its argument.
yuuji@14 497 If there is a line which begins with string: \"%#!\", the following
yuuji@7 498 strings are assumed to be the latex-command and arguments. The
yuuji@7 499 default value of latex-command is:
yuuji@14 500 tex-command FileName
yuuji@7 501 and if you write \"%#!jlatex\" in the beginning of certain line.
yuuji@14 502 \"jlatex \" FileName
yuuji@7 503 will be the latex-command,
yuuji@7 504 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
yuuji@14 505 is non-nil, then
yuuji@7 506 \"jlatex main.tex\"
yuuji@15 507
yuuji@7 508 will be given to the shell."
yuuji@15 509 (let (magic command target)
yuuji@15 510 (setq parent
yuuji@15 511 (cond
yuuji@15 512 (YaTeX-parent-file YaTeX-parent-file)
yuuji@15 513 (t (save-excursion
yuuji@15 514 (YaTeX-visit-main t)
yuuji@15 515 (file-name-nondirectory (buffer-file-name)))))
yuuji@15 516 magic (YaTeX-get-builtin "!"))
yuuji@15 517 (cond
yuuji@15 518 (magic
yuuji@15 519 (cond
yuuji@15 520 (switch (if (string-match "\\s " magic) magic
yuuji@15 521 (concat magic " " parent)))
yuuji@15 522 (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
yuuji@16 523 (t (concat tex-command " " (if switch parent)))))
yuuji@7 524 )
yuuji@7 525
yuuji@16 526 (defvar YaTeX-lpr-command-history nil
yuuji@16 527 "Holds command line history of YaTeX-lpr.")
yuuji@16 528 (put 'YaTeX-lpr-command-history 'no-default t)
yuuji@7 529 (defun YaTeX-lpr (arg)
yuuji@7 530 "Print out. If prefix arg ARG is non nil, call print driver without
yuuji@7 531 page range description."
yuuji@7 532 (interactive "P")
yuuji@23 533 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
yuuji@23 534 from to (lbuffer "*dvi-printing*"))
yuuji@7 535 (setq
yuuji@7 536 cmd
yuuji@7 537 (YaTeX-replace-format
yuuji@7 538 cmd "f"
yuuji@7 539 (if (or arg (not (string-match "%f" cmd)))
yuuji@7 540 ""
yuuji@7 541 (YaTeX-replace-format
yuuji@7 542 dviprint-from-format
yuuji@7 543 "b"
yuuji@7 544 (if (string=
yuuji@7 545 (setq from (read-string "From page(default 1): ")) "")
yuuji@7 546 "1" from))))
yuuji@7 547 )
yuuji@7 548 (setq
yuuji@7 549 cmd
yuuji@7 550 (YaTeX-replace-format
yuuji@7 551 cmd "t"
yuuji@7 552 (if (or arg (not (string-match "%t" cmd))
yuuji@7 553 (string=
yuuji@7 554 (setq to (read-string "To page(default none): ")) ""))
yuuji@7 555 ""
yuuji@7 556 (YaTeX-replace-format dviprint-to-format "e" to)))
yuuji@7 557 )
yuuji@16 558 (setq cmd
yuuji@16 559 (let ((minibuffer-history-symbol 'YaTeX-lpr-command-history))
yuuji@16 560 (read-string "Edit command line: "
yuuji@16 561 (format cmd (YaTeX-get-preview-file-name)))))
yuuji@7 562 (save-excursion
yuuji@7 563 (YaTeX-visit-main t) ;;change execution directory
yuuji@23 564 (YaTeX-showup-buffer
yuuji@23 565 lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@23 566 (with-output-to-temp-buffer lbuffer
yuuji@13 567 (if YaTeX-dos
yuuji@7 568 (call-process shell-file-name "con" "*dvi-printing*" nil
yuuji@7 569 "/c " cmd)
yuuji@7 570 (start-process "print" "*dvi-printing*" shell-file-name "-c" cmd)
yuuji@7 571 (message (concat "Starting " cmd " to printing "
yuuji@7 572 (YaTeX-get-preview-file-name))))
yuuji@7 573 )))
yuuji@7 574 )
yuuji@7 575
yuuji@7 576 (defun YaTeX-main-file-p ()
yuuji@7 577 "Return if current buffer is main LaTeX source."
yuuji@14 578 (cond
yuuji@14 579 ((YaTeX-get-builtin "!")
yuuji@15 580 (string-match (YaTeX-guess-parent (YaTeX-get-builtin "!")) (buffer-name)))
yuuji@14 581 (t
yuuji@14 582 (save-excursion
yuuji@14 583 (let ((latex-main-id (concat "^\\s *" YaTeX-ec-regexp "documentstyle")))
yuuji@14 584 (or (re-search-backward latex-main-id nil t)
yuuji@14 585 (re-search-forward latex-main-id nil t))))))
yuuji@7 586 )
yuuji@7 587
yuuji@7 588 (defun YaTeX-visit-main (&optional setbuf)
yuuji@15 589 "Switch buffer to main LaTeX source.
yuuji@15 590 Use set-buffer instead of switch-to-buffer if the optional second argument
yuuji@15 591 SETBUF is t(Use it only from Emacs-Lisp program)."
yuuji@7 592 (interactive)
yuuji@15 593 (let (b-in main-file)
yuuji@15 594 (if (setq b-in (YaTeX-get-builtin "!"))
yuuji@15 595 (setq main-file (YaTeX-guess-parent b-in)))
yuuji@14 596 (if YaTeX-parent-file
yuuji@23 597 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
yuuji@23 598 YaTeX-parent-file))
yuuji@14 599 (if (YaTeX-main-file-p)
yuuji@7 600 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
yuuji@7 601 (cond
yuuji@14 602 ((and (interactive-p) main-file (get-buffer-window main-file))
yuuji@14 603 (select-window (get-buffer-window main-file)))
yuuji@14 604 ((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
yuuji@14 605 ((and main-file
yuuji@14 606 (file-exists-p (setq main-file (concat "../" main-file)))
yuuji@7 607 (y-or-n-p (concat (expand-file-name main-file)
yuuji@7 608 " is main file?:")))
yuuji@7 609 (YaTeX-switch-to-buffer main-file setbuf))
yuuji@14 610 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
yuuji@14 611 (setq YaTeX-parent-file main-file)
yuuji@14 612 (find-file main-file))
yuuji@14 613 )))
yuuji@7 614 nil
yuuji@7 615 )
yuuji@7 616
yuuji@15 617
yuuji@15 618 (defun YaTeX-guess-parent (command-line)
yuuji@15 619 (setq command-line
yuuji@15 620 (if (string-match ".*\\s " command-line)
yuuji@15 621 (substring command-line (match-end 0))
yuuji@15 622 (file-name-nondirectory (buffer-file-name)))
yuuji@15 623 command-line
yuuji@15 624 (concat (if (string-match "\\(.*\\)\\." command-line)
yuuji@15 625 (substring command-line (match-beginning 1) (match-end 1))
yuuji@15 626 command-line)
yuuji@15 627 ".tex"))
yuuji@15 628 )
yuuji@15 629
yuuji@7 630 (defun YaTeX-visit-main-other-window ()
yuuji@7 631 "Switch to buffer main LaTeX source in other window."
yuuji@7 632 (interactive)
yuuji@7 633 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
yuuji@7 634 (YaTeX-switch-to-buffer-other-window
yuuji@7 635 (concat (YaTeX-get-preview-file-name) ".tex")))
yuuji@7 636 )
yuuji@7 637
yuuji@7 638 (defun YaTeX-get-builtin (key)
yuuji@7 639 "Read source built-in command of %# usage."
yuuji@7 640 (save-excursion
yuuji@7 641 (goto-char (point-min))
yuuji@15 642 (if (and (re-search-forward
yuuji@15 643 (concat "^" (regexp-quote (concat "%#" key))) nil t)
yuuji@7 644 (not (eolp)))
yuuji@7 645 (buffer-substring
yuuji@7 646 (progn (skip-chars-forward " " (point-end-of-line))(point))
yuuji@7 647 (point-end-of-line))
yuuji@7 648 nil))
yuuji@7 649 )
yuuji@7 650
yuuji@7 651 (defun YaTeX-save-buffers ()
yuuji@7 652 "Save buffers which is in yatex-mode."
yuuji@7 653 (basic-save-buffer)
yuuji@7 654 (save-excursion
yuuji@7 655 (mapcar '(lambda (buf)
yuuji@7 656 (set-buffer buf)
yuuji@7 657 (if (and (buffer-file-name buf)
yuuji@7 658 (eq major-mode 'yatex-mode)
yuuji@7 659 (buffer-modified-p buf)
yuuji@7 660 (y-or-n-p (format "Save %s" (buffer-name buf))))
yuuji@7 661 (save-buffer buf)))
yuuji@7 662 (buffer-list)))
yuuji@7 663 )
yuuji@7 664
yuuji@7 665 (provide 'yatexprc)