yatex

annotate yatexprc.el @ 53:5f4b18da14b3

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