yatex

annotate yatexprc.el @ 47:d7e7b4654058

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