yatex

annotate yatexprc.el @ 68:0eb6997bee16

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