yatex

annotate yatexprc.el @ 14:b7b023a74293

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