yuuji@51: ;;; -*- Emacs-Lisp -*- yuuji@51: ;;; YaTeX process handler. yuuji@51: ;;; yatexprc.el yuuji@70: ;;; (c )1993-1999 by HIROSE Yuuji.[yuuji@yatex.org] yuuji@70: ;;; Last modified Sat Dec 18 22:16:11 1999 on firestorm yuuji@51: ;;; $Id$ yuuji@51: yuuji@64: ;(require 'yatex) yuuji@64: (require 'yatexlib) yuuji@51: yuuji@51: (defvar YaTeX-typeset-process nil yuuji@69: "Process identifier for jlatex") yuuji@69: yuuji@51: (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*" yuuji@51: "Process buffer for jlatex") yuuji@51: yuuji@51: (defvar YaTeX-typeset-buffer-syntax nil yuuji@51: "*Syntax table for typesetting buffer") yuuji@51: yuuji@51: (defvar YaTeX-current-TeX-buffer nil yuuji@51: "Keeps the buffer on which recently typeset run.") yuuji@51: yuuji@51: (defvar YaTeX-shell-command-option yuuji@51: (or (and (boundp 'shell-command-option) shell-command-option) yuuji@68: (and (boundp 'shell-command-switch) shell-command-switch) yuuji@51: (if YaTeX-dos "/c" "-c")) yuuji@51: "Shell option for command execution.") yuuji@51: yuuji@64: (defvar YaTeX-latex-message-code yuuji@64: (cond yuuji@64: (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist))) yuuji@64: ((and YaTeX-emacs-20 (member 'undecided (coding-system-list)) yuuji@64: 'undecided)) yuuji@64: ((featurep 'mule) yuuji@64: (or (and (boundp '*autoconv*) *autoconv*) yuuji@64: (and (fboundp 'coding-system-list) 'automatic-conversion))) yuuji@64: ((boundp 'NEMACS) yuuji@64: (cdr (assq (if YaTeX-dos 1 2) YaTeX-kanji-code-alist)))) yuuji@64: "Process coding system for LaTeX.") yuuji@64: yuuji@51: (if YaTeX-typeset-buffer-syntax nil yuuji@51: (setq YaTeX-typeset-buffer-syntax yuuji@51: (make-syntax-table (standard-syntax-table))) yuuji@51: (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax) yuuji@51: (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax) yuuji@51: (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax) yuuji@69: (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax)) yuuji@51: yuuji@60: (defun YaTeX-typeset (command buffer &optional prcname modename) yuuji@51: "Execute jlatex (or other) to LaTeX typeset." yuuji@51: (interactive) yuuji@51: (save-excursion yuuji@60: (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer)) yuuji@64: (map YaTeX-typesetting-mode-map) yuuji@64: (outcode yuuji@64: (cond ((eq major-mode 'yatex-mode) YaTeX-coding-system) yuuji@69: ((eq major-mode 'yahtml-mode) yahtml-kanji-code)))) yuuji@51: (if (and YaTeX-typeset-process yuuji@51: (eq (process-status YaTeX-typeset-process) 'run)) yuuji@51: ;; if tex command is halting. yuuji@51: (YaTeX-kill-typeset-process YaTeX-typeset-process)) yuuji@60: (YaTeX-put-nonstopmode) yuuji@60: (setq prcname (or prcname "LaTeX") yuuji@60: modename (or modename "typeset")) yuuji@60: (if (eq major-mode 'yatex-mode) (YaTeX-visit-main t)) ;;execution dir yuuji@51: (setq execdir default-directory) yuuji@51: ;;Select lower-most window if there are more than 2 windows and yuuji@51: ;;typeset buffer not seen. yuuji@51: (YaTeX-showup-buffer yuuji@51: buffer (function (lambda (x) (nth 3 (window-edges x))))) yuuji@51: (set-buffer (get-buffer-create buffer)) yuuji@51: (setq default-directory execdir) yuuji@51: (cd execdir) yuuji@51: (erase-buffer) yuuji@51: (cond yuuji@64: ((not (fboundp 'start-process)) ;YaTeX-dos;if MS-DOS yuuji@51: (call-process yuuji@64: shell-file-name nil buffer nil YaTeX-shell-command-option command)) yuuji@51: (t ;if UNIX yuuji@51: (set-process-buffer yuuji@51: (setq YaTeX-typeset-process yuuji@60: (start-process prcname buffer shell-file-name yuuji@51: YaTeX-shell-command-option command)) yuuji@51: (get-buffer buffer)) yuuji@51: (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel))) yuuji@51: (message (format "Calling `%s'..." command)) yuuji@51: (setq YaTeX-current-TeX-buffer (buffer-name)) yuuji@60: (use-local-map map) ;map may be localized yuuji@51: (set-syntax-table YaTeX-typeset-buffer-syntax) yuuji@60: (setq mode-name modename) yuuji@60: (if YaTeX-typeset-process ;if process is running (maybe on UNIX) yuuji@64: (cond ((fboundp 'set-current-process-coding-system) yuuji@51: (set-current-process-coding-system yuuji@64: YaTeX-latex-message-code outcode)) yuuji@64: ((fboundp 'set-process-coding-system) yuuji@64: (set-process-coding-system yuuji@64: YaTeX-typeset-process YaTeX-latex-message-code outcode)) yuuji@64: (YaTeX-emacs-20 yuuji@64: (set-buffer-process-coding-system yuuji@64: YaTeX-latex-message-code outcode)) yuuji@51: ((boundp 'NEMACS) yuuji@51: (set-kanji-process-code YaTeX-latex-message-code)))) yuuji@51: (if YaTeX-dos (message "Done.") yuuji@51: (insert " ") yuuji@51: (set-marker (process-mark YaTeX-typeset-process) (1- (point)))) yuuji@51: (if (bolp) (forward-line -1)) ;what for? yuuji@51: (if (and YaTeX-emacs-19 window-system) yuuji@51: (let ((win (get-buffer-window buffer t)) owin) yuuji@51: (select-frame (window-frame win)) yuuji@51: (setq owin (selected-window)) yuuji@51: (select-window win) yuuji@51: (goto-char (point-max)) yuuji@51: (recenter -1) yuuji@51: (select-window owin)) yuuji@51: (select-window (get-buffer-window buffer)) yuuji@51: (goto-char (point-max)) yuuji@51: (recenter -1)) yuuji@51: (select-window window) yuuji@60: (switch-to-buffer cb) yuuji@69: (YaTeX-remove-nonstopmode)))) yuuji@51: yuuji@51: (defun YaTeX-typeset-sentinel (proc mes) yuuji@51: (cond ((null (buffer-name (process-buffer proc))) yuuji@51: ;; buffer killed yuuji@51: (set-process-buffer proc nil)) yuuji@51: ((memq (process-status proc) '(signal exit)) yuuji@51: (let* ((obuf (current-buffer)) (pbuf (process-buffer proc)) yuuji@51: (pwin (get-buffer-window pbuf)) yuuji@51: (owin (selected-window)) win) yuuji@51: ;; save-excursion isn't the right thing if yuuji@51: ;; process-buffer is current-buffer yuuji@51: (unwind-protect yuuji@51: (progn yuuji@51: ;; Write something in *typesetting* and hack its mode line yuuji@51: (if pwin yuuji@51: (select-window pwin) yuuji@51: (set-buffer pbuf)) yuuji@51: ;;(YaTeX-showup-buffer pbuf nil t) yuuji@51: (goto-char (point-max)) yuuji@51: (if pwin (recenter -3)) yuuji@60: (insert ?\n mode-name " " mes) yuuji@51: (forward-char -1) yuuji@51: (insert " at " (substring (current-time-string) 0 -5) "\n") yuuji@51: (forward-char 1) yuuji@51: (setq mode-line-process yuuji@51: (concat ": " yuuji@51: (symbol-name (process-status proc)))) yuuji@60: (message mode-name " %s." yuuji@51: (if (eq (process-status proc) 'exit) yuuji@51: "done" "ceased")) yuuji@51: ;; If buffer and mode line shows that the process yuuji@51: ;; is dead, we can delete it now. Otherwise it yuuji@51: ;; will stay around until M-x list-processes. yuuji@51: (delete-process proc) yuuji@51: ) yuuji@51: (setq YaTeX-typeset-process nil) yuuji@51: ;; Force mode line redisplay soon yuuji@51: (set-buffer-modified-p (buffer-modified-p)) yuuji@51: ) yuuji@51: (select-window owin) yuuji@69: (set-buffer obuf))))) yuuji@51: yuuji@51: (defvar YaTeX-texput-file "texput.tex" yuuji@69: "*File name for temporary file of typeset-region.") yuuji@51: yuuji@51: (defun YaTeX-typeset-region () yuuji@52: "Paste the region to the file `texput.tex' and execute typesetter. yuuji@52: The region is specified by the rule: yuuji@51: (1)If keyword `%#BEGIN' is found in the upper direction from (point). yuuji@51: (1-1)if the keyword `%#END' is found after `%#BEGIN', yuuji@51: ->Assume the text between `%#BEGIN' and `%#END' as region. yuuji@51: (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN', yuuji@51: ->Assume the text after `%#BEGIN' as region. yuuji@51: (2)If no `%#BEGIN' usage is found before the (point), yuuji@51: ->Assume the text between current (point) and (mark) as region. yuuji@51: DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing yuuji@51: operation to the region." yuuji@51: (interactive) yuuji@51: (save-excursion yuuji@51: (let* yuuji@51: ((end "") typeout ;Type out message that tells the method of cutting. yuuji@51: (texput YaTeX-texput-file) yuuji@51: (cmd (concat (YaTeX-get-latex-command nil) " " texput)) yuuji@51: (buffer (current-buffer)) opoint preamble (subpreamble "") main yuuji@51: (hilit-auto-highlight nil) ;for Emacs19 with hilit19 yuuji@52: reg-begin reg-end lineinfo) yuuji@51: yuuji@51: (save-excursion yuuji@51: (if (search-backward "%#BEGIN" nil t) yuuji@51: (progn yuuji@51: (setq typeout "--- Region from BEGIN to " yuuji@51: end "the end of the buffer ---" yuuji@51: reg-begin (match-end 0)) yuuji@51: (if (search-forward "%#END" nil t) yuuji@51: (setq reg-end (match-beginning 0) yuuji@51: end "END ---") yuuji@51: (setq reg-end (point-max)))) yuuji@52: (setq typeout "=== Region from (point) to (mark) ===" yuuji@52: reg-begin (point) reg-end (mark))) yuuji@52: (goto-char (min reg-begin reg-end)) yuuji@52: (setq lineinfo (count-lines (point-min) (point-end-of-line))) yuuji@51: (goto-char (point-min)) yuuji@51: (while (search-forward "%#REQUIRE" nil t) yuuji@51: (setq subpreamble yuuji@51: (concat subpreamble yuuji@51: (cond yuuji@51: ((eolp) yuuji@51: (buffer-substring yuuji@51: (match-beginning 0) yuuji@51: (point-beginning-of-line))) yuuji@51: (t (buffer-substring yuuji@51: (match-end 0) yuuji@51: (point-end-of-line)))) yuuji@51: "\n")) yuuji@51: (goto-char (match-end 0)))) yuuji@51: (YaTeX-visit-main t) yuuji@51: (setq main (current-buffer)) yuuji@51: (setq opoint (point)) yuuji@51: (goto-char (point-min)) yuuji@51: (setq yuuji@51: preamble yuuji@51: (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t) yuuji@51: (buffer-substring (point-min) (match-end 0)) yuuji@51: (concat "\\documentstyle{" YaTeX-default-document-style "}\n" yuuji@51: "\\begin{document}"))) yuuji@51: (goto-char opoint) yuuji@51: ;;(set-buffer buffer) ;for clarity yuuji@52: (let ((hilit-auto-highlight nil)) yuuji@52: (set-buffer (find-file-noselect texput))) yuuji@51: ;;(find-file YaTeX-texput-file) yuuji@51: (erase-buffer) yuuji@64: (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) yuuji@51: (insert "\\nonstopmode{}\n")) yuuji@51: (insert preamble "\n" subpreamble "\n") yuuji@52: (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo)) yuuji@51: (insert-buffer-substring buffer reg-begin reg-end) yuuji@51: (insert "\\typeout{" typeout end "}\n") ;Notice the selected method. yuuji@51: (insert "\n\\end{document}\n") yuuji@51: (basic-save-buffer) yuuji@51: (kill-buffer (current-buffer)) yuuji@51: (set-buffer main) ;return to parent file or itself. yuuji@51: (YaTeX-typeset cmd YaTeX-typeset-buffer) yuuji@51: (switch-to-buffer buffer) ;for Emacs-19 yuuji@52: (put 'dvi2-command 'region t) yuuji@52: (put 'dvi2-command 'file buffer) yuuji@69: (put 'dvi2-command 'offset lineinfo)))) yuuji@51: yuuji@51: (defun YaTeX-typeset-buffer () yuuji@60: "Typeset whole buffer. yuuji@60: If %#! usage says other buffer is main text, yuuji@51: visit main buffer to confirm if its includeonly list contains current yuuji@51: buffer's file. And if it doesn't contain editing text, ask user which yuuji@51: action wants to be done, A:Add list, R:Replace list, %:comment-out list." yuuji@51: (interactive) yuuji@51: (YaTeX-save-buffers) yuuji@51: (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.))) yuuji@51: (mydir (file-name-directory (buffer-file-name))) yuuji@51: (cmd (YaTeX-get-latex-command t)) (cb (current-buffer))) yuuji@51: (if (YaTeX-main-file-p) nil yuuji@51: (save-excursion yuuji@51: (YaTeX-visit-main t) ;search into main buffer yuuji@51: (save-excursion yuuji@51: (push-mark (point) t) yuuji@51: (goto-char (point-min)) yuuji@51: (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t) yuuji@51: (re-search-backward "^[ ]*\\\\includeonly{" nil t)) yuuji@51: (let* yuuji@51: ((b (progn (skip-chars-forward "^{") (point))) yuuji@51: (e (progn (skip-chars-forward "^}") (1+ (point)))) yuuji@51: (s (buffer-substring b e)) c yuuji@51: (pardir (file-name-directory (buffer-file-name)))) yuuji@51: (if (string-match (concat "[{,/]" me "[,}]") s) yuuji@51: nil ; Nothing to do when it's already in includeonly. yuuji@51: (ding) yuuji@51: (switch-to-buffer (current-buffer));Display this buffer. yuuji@51: (setq yuuji@51: me ;;Rewrite my name(me) to contain sub directory name. yuuji@51: (concat yuuji@51: (if (string-match pardir mydir) ;if mydir is child of main yuuji@51: (substring mydir (length pardir)) ;cut absolute path yuuji@51: mydir) ;else concat absolute path name. yuuji@51: me)) yuuji@51: (message yuuji@51: "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? " yuuji@51: me) yuuji@51: (setq c (read-char)) yuuji@51: (cond yuuji@51: ((= c ?a) yuuji@51: (goto-char (1+ b)) yuuji@51: (insert me (if (string= s "{}") "" ","))) yuuji@51: ((= c ?r) yuuji@51: (delete-region (1+ b) (1- e)) (insert me)) yuuji@51: ((= c ?%) yuuji@51: (beginning-of-line) (insert "%")) yuuji@51: (t nil)) yuuji@51: (basic-save-buffer)))) yuuji@51: (exchange-point-and-mark))) yuuji@51: (switch-to-buffer cb)) ;for 19 yuuji@51: (YaTeX-typeset cmd YaTeX-typeset-buffer) yuuji@69: (put 'dvi2-command 'region nil))) yuuji@51: yuuji@51: (defvar YaTeX-call-command-history nil yuuji@51: "Holds history list of YaTeX-call-command-on-file.") yuuji@51: (put 'YaTeX-call-command-history 'no-default t) yuuji@51: (defun YaTeX-call-command-on-file (base-cmd buffer) yuuji@51: (YaTeX-save-buffers) yuuji@51: (YaTeX-typeset yuuji@51: (read-string-with-history yuuji@51: "Call command: " yuuji@51: (concat base-cmd " " (YaTeX-get-preview-file-name)) yuuji@51: 'YaTeX-call-command-history) yuuji@69: buffer)) yuuji@51: yuuji@51: (defun YaTeX-bibtex-buffer (cmd) yuuji@51: "Pass the bibliography data of editing file to bibtex." yuuji@51: (interactive) yuuji@51: (YaTeX-save-buffers) yuuji@69: (YaTeX-call-command-on-file cmd "*YaTeX-bibtex*" )) yuuji@51: yuuji@51: (defun YaTeX-kill-typeset-process (proc) yuuji@51: "Kill process PROC after sending signal to PROC. yuuji@51: PROC should be process identifier." yuuji@51: (cond yuuji@68: ((not (fboundp 'start-process)) yuuji@68: (error "This system can't have concurrent process.")) yuuji@51: ((or (null proc) (not (eq (process-status proc) 'run))) yuuji@51: (message "Typesetting process is not running.")) yuuji@51: (t yuuji@51: (save-excursion yuuji@51: (set-buffer (process-buffer proc)) yuuji@51: (save-excursion yuuji@51: (goto-char (point-max)) yuuji@51: (beginning-of-line) yuuji@51: (if (looking-at "\\? +$") yuuji@51: (let ((mp (point-max))) yuuji@51: (process-send-string proc "x\n") yuuji@51: (while (= mp (point-max)) (sit-for 1)))))) yuuji@51: (if (eq (process-status proc) 'run) yuuji@51: (progn yuuji@51: (interrupt-process proc) yuuji@69: (delete-process proc)))))) yuuji@51: yuuji@51: (defun YaTeX-system (command buffer) yuuji@51: "Execute some command on buffer. Not a official function." yuuji@51: (save-excursion yuuji@51: (YaTeX-showup-buffer yuuji@51: buffer (function (lambda (x) (nth 3 (window-edges x))))) yuuji@59: (let ((df default-directory)) ;preserve current buf's pwd yuuji@59: (set-buffer (get-buffer-create buffer)) ;1.61.3 yuuji@59: (setq default-directory df) yuuji@59: (cd df)) yuuji@51: (erase-buffer) yuuji@64: (if (not (fboundp 'start-process)) yuuji@51: (call-process yuuji@51: shell-file-name nil buffer nil YaTeX-shell-command-option command) yuuji@59: (if (and (get-buffer-process buffer) yuuji@59: (eq (process-status (get-buffer-process buffer)) 'run) yuuji@59: (not yuuji@59: (y-or-n-p (format "Process %s is running. Continue?" buffer)))) yuuji@59: nil yuuji@59: (set-process-buffer yuuji@59: (start-process yuuji@59: "system" buffer shell-file-name YaTeX-shell-command-option command) yuuji@69: (get-buffer buffer)))))) yuuji@51: yuuji@51: (defvar YaTeX-preview-command-history nil yuuji@51: "Holds minibuffer history of preview command.") yuuji@51: (put 'YaTeX-preview-command-history 'no-default t) yuuji@51: (defvar YaTeX-preview-file-history nil yuuji@51: "Holds minibuffer history of file to preview.") yuuji@51: (put 'YaTeX-preview-file-history 'no-default t) yuuji@51: (defun YaTeX-preview (preview-command preview-file) yuuji@51: "Execute xdvi (or other) to tex-preview." yuuji@51: (interactive yuuji@51: (list yuuji@51: (read-string-with-history yuuji@58: "Preview command: " yuuji@58: (or (YaTeX-get-builtin "PREVIEW") dvi2-command) yuuji@58: 'YaTeX-preview-command-history) yuuji@51: (read-string-with-history yuuji@51: "Preview file[.dvi]: " yuuji@51: (if (get 'dvi2-command 'region) yuuji@51: (substring YaTeX-texput-file yuuji@51: 0 (rindex YaTeX-texput-file ?.)) yuuji@51: (YaTeX-get-preview-file-name)) yuuji@51: 'YaTeX-preview-file-history))) yuuji@51: (setq dvi2-command preview-command) ;`dvi2command' is buffer local yuuji@51: (save-excursion yuuji@51: (YaTeX-visit-main t) yuuji@51: (let ((pbuffer "*dvi-preview*") (dir default-directory)) yuuji@51: (YaTeX-showup-buffer yuuji@51: pbuffer (function (lambda (x) (nth 3 (window-edges x))))) yuuji@51: (set-buffer (get-buffer-create pbuffer)) yuuji@51: (erase-buffer) yuuji@51: (setq default-directory dir) ;for 18 yuuji@51: (cd dir) ;for 19 yuuji@51: (cond yuuji@64: ((not (fboundp 'start-process)) ;if MS-DOS yuuji@51: (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor yuuji@51: (call-process shell-file-name "con" "*dvi-preview*" nil yuuji@60: YaTeX-shell-command-option yuuji@61: (concat preview-command " " preview-file)) yuuji@51: (send-string-to-terminal "\e[>5l") ;show cursor yuuji@51: (redraw-display)) yuuji@69: ((and (string-match "dviout" preview-command) ;maybe on `kon' yuuji@69: (stringp (getenv "TERM")) yuuji@69: (string-match "^kon" (getenv "TERM"))) yuuji@69: (call-process shell-file-name "con" "*dvi-preview*" nil yuuji@69: YaTeX-shell-command-option yuuji@69: (concat preview-command " " preview-file))) yuuji@51: (t ;if UNIX yuuji@51: (set-process-buffer yuuji@51: (start-process "preview" "*dvi-preview*" shell-file-name yuuji@51: YaTeX-shell-command-option yuuji@51: (concat preview-command " " preview-file)) yuuji@51: (get-buffer pbuffer)) yuuji@51: (message yuuji@51: (concat "Starting " preview-command yuuji@69: " to preview " preview-file))))))) yuuji@51: yuuji@58: (defvar YaTeX-xdvi-remote-program "xdvi") yuuji@58: (defun YaTeX-xdvi-remote-search (&optional region-mode) yuuji@58: "Search string at the point on xdvi -remote window. yuuji@58: Non-nil for optional argument REGION-MODE specifies the search string yuuji@58: by region." yuuji@58: (interactive "P") yuuji@58: (let ((pb " *xdvi*") str proc) yuuji@58: (save-excursion yuuji@58: (if region-mode yuuji@58: (setq str (buffer-substring (region-beginning) (region-end))) yuuji@58: (setq str (buffer-substring yuuji@58: (point) yuuji@58: (progn (skip-chars-forward "^\n\\\\}") (point))))) yuuji@58: (message "Searching `%s'..." str) yuuji@58: (if (boundp 'MULE) yuuji@58: (define-program-coding-system yuuji@58: (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program) yuuji@58: *euc-japan*)) yuuji@58: (setq proc yuuji@58: (start-process yuuji@58: "xdvi" pb YaTeX-xdvi-remote-program yuuji@58: "-remote" (format "SloppySearch(%s) " str) yuuji@58: (concat (YaTeX-get-preview-file-name) ".dvi"))) yuuji@69: (message "Searching `%s'...Done" str)))) yuuji@58: yuuji@52: (defun YaTeX-set-virtual-error-position (file-sym line-sym) yuuji@52: "Replace the value of FILE-SYM, LINE-SYM by virtual error position." yuuji@52: (cond yuuji@52: ((and (get 'dvi2-command 'region) yuuji@52: (> (symbol-value line-sym) (car (get 'dvi2-command 'offset)))) yuuji@52: (set file-sym (get 'dvi2-command 'file)) yuuji@52: (set line-sym yuuji@52: (+ (- (apply '- (get 'dvi2-command 'offset))) yuuji@52: (symbol-value line-sym) yuuji@52: -1))))) yuuji@52: yuuji@51: (defun YaTeX-prev-error () yuuji@51: "Visit previous typeset error. yuuji@51: To avoid making confliction of line numbers by editing, jump to yuuji@51: error or warning lines in reverse order." yuuji@51: (interactive) yuuji@51: (let ((cur-buf (buffer-name)) (cur-win (selected-window)) yuuji@51: error-line typeset-win error-buffer error-win) yuuji@51: (if (null (get-buffer YaTeX-typeset-buffer)) yuuji@51: (error "There is no typesetting buffer.")) yuuji@51: (YaTeX-showup-buffer YaTeX-typeset-buffer nil t) yuuji@51: (setq typeset-win (selected-window)) yuuji@51: (if (re-search-backward yuuji@51: (concat "\\(" latex-error-regexp "\\)\\|\\(" yuuji@51: latex-warning-regexp "\\)") yuuji@51: nil t) yuuji@51: nil yuuji@51: (select-window cur-win) yuuji@68: (error "No more errors on %s" cur-buf)) yuuji@51: (goto-char (match-beginning 0)) yuuji@51: (skip-chars-forward "^0-9" (match-end 0)) yuuji@51: (setq error-line yuuji@51: (string-to-int yuuji@51: (buffer-substring yuuji@51: (point) yuuji@51: (progn (skip-chars-forward "0-9" (match-end 0)) (point)))) yuuji@70: error-buffer (expand-file-name (YaTeX-get-error-file cur-buf))) yuuji@51: (if (or (null error-line) (equal 0 error-line)) yuuji@51: (error "Can't detect error position.")) yuuji@52: (YaTeX-set-virtual-error-position 'error-buffer 'error-line) yuuji@52: (setq error-win (get-buffer-window error-buffer)) yuuji@51: (select-window cur-win) yuuji@51: (cond yuuji@51: (error-win (select-window error-win)) yuuji@51: ((eq (get-lru-window) typeset-win) yuuji@51: (YaTeX-switch-to-buffer error-buffer)) yuuji@51: (t (select-window (get-lru-window)) yuuji@51: (YaTeX-switch-to-buffer error-buffer))) yuuji@51: (setq error-win (selected-window)) yuuji@51: (goto-line error-line) yuuji@51: (message "LaTeX %s in `%s' on line: %d." yuuji@51: (if (match-beginning 1) "error" "warning") yuuji@51: error-buffer error-line) yuuji@51: (select-window typeset-win) yuuji@51: (skip-chars-backward "0-9") yuuji@51: (recenter (/ (window-height) 2)) yuuji@60: (sit-for 1) yuuji@51: (goto-char (match-beginning 0)) yuuji@69: (select-window error-win))) yuuji@51: yuuji@51: (defun YaTeX-jump-error-line () yuuji@51: "Jump to corresponding line on latex command's error message." yuuji@51: (interactive) yuuji@51: (let (error-line error-file error-buf) yuuji@51: (save-excursion yuuji@51: (beginning-of-line) yuuji@51: (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)" yuuji@51: (point-end-of-line) t))) yuuji@51: (if (null error-line) yuuji@51: (if (eobp) (insert (this-command-keys)) yuuji@51: (error "No line number expression.")) yuuji@51: (goto-char (match-beginning 0)) yuuji@51: (setq error-line (string-to-int yuuji@51: (buffer-substring (match-beginning 1) (match-end 1))) yuuji@70: error-file (expand-file-name yuuji@70: (YaTeX-get-error-file YaTeX-current-TeX-buffer))) yuuji@52: (YaTeX-set-virtual-error-position 'error-file 'error-line) yuuji@52: (setq error-buf (YaTeX-switch-to-buffer error-file t))) yuuji@51: (if (null error-buf) yuuji@51: (error "`%s' is not found in this directory." error-file)) yuuji@51: (YaTeX-showup-buffer error-buf nil t) yuuji@69: (goto-line error-line))) yuuji@51: yuuji@51: (defun YaTeX-send-string () yuuji@51: "Send string to current typeset process." yuuji@51: (interactive) yuuji@51: (if (and (eq (process-status YaTeX-typeset-process) 'run) yuuji@51: (>= (point) (process-mark YaTeX-typeset-process))) yuuji@51: (let ((b (process-mark YaTeX-typeset-process)) yuuji@51: (e (point-end-of-line))) yuuji@51: (goto-char b) yuuji@51: (skip-chars-forward " \t" e) yuuji@51: (setq b (point)) yuuji@51: (process-send-string yuuji@51: YaTeX-typeset-process (concat (buffer-substring b e) "\n")) yuuji@51: (goto-char e) yuuji@51: (insert "\n") yuuji@51: (set-marker (process-mark YaTeX-typeset-process) (point)) yuuji@51: (insert " ")) yuuji@69: (ding))) yuuji@51: yuuji@51: (defun YaTeX-view-error () yuuji@51: (interactive) yuuji@51: (if (null (get-buffer YaTeX-typeset-buffer)) yuuji@51: (message "No typeset buffer found.") yuuji@51: (let ((win (selected-window))) yuuji@51: (YaTeX-showup-buffer YaTeX-typeset-buffer nil t) yuuji@51: ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is yuuji@51: ;; automatically scrolled up at typesetting. yuuji@51: ;;(goto-char (point-max)) yuuji@51: ;;(forward-line -1) yuuji@51: ;;(recenter -1) yuuji@69: (select-window win)))) yuuji@51: yuuji@51: (defun YaTeX-get-error-file (default) yuuji@51: "Get current processing file from typesetting log." yuuji@51: (save-excursion yuuji@51: (let(s) yuuji@51: (condition-case () (up-list -1) yuuji@51: (error yuuji@51: (let ((list 0) found) yuuji@51: (while yuuji@51: (and (<= list 0) (not found) yuuji@51: (re-search-backward "\\((\\)\\|\\()\\)" nil t)) yuuji@51: (if (equal (match-beginning 0) (match-beginning 2)) ;close paren. yuuji@51: (setq list (1- list)) ;open paren yuuji@51: (setq list (1+ list)) yuuji@51: (if (= list 1) yuuji@51: (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty") yuuji@51: (setq found t) yuuji@51: (setq list (1- list))))))))) yuuji@51: (setq s yuuji@51: (buffer-substring yuuji@51: (progn (forward-char 1) (point)) yuuji@51: (progn (skip-chars-forward "^ \n" (point-end-of-line)) yuuji@51: (point)))) yuuji@69: (if (string= "" s) default s)))) yuuji@51: yuuji@51: (defun YaTeX-put-nonstopmode () yuuji@64: (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) yuuji@51: (if (re-search-backward "\\\\nonstopmode{}" (point-min) t) yuuji@51: nil ;if already written in text then do nothing yuuji@51: (save-excursion yuuji@51: (YaTeX-visit-main t) yuuji@51: (goto-char (point-min)) yuuji@60: (insert "\\nonstopmode{}%_YaTeX_%\n") yuuji@69: (if (buffer-file-name) (basic-save-buffer)))))) yuuji@51: yuuji@51: (defun YaTeX-remove-nonstopmode () yuuji@64: (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed yuuji@51: (save-excursion yuuji@51: (YaTeX-visit-main t) yuuji@51: (goto-char (point-min)) yuuji@51: (forward-line 1) yuuji@51: (narrow-to-region (point-min) (point)) yuuji@51: (goto-char (point-min)) yuuji@51: (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$") yuuji@69: (widen)))) yuuji@51: yuuji@51: (defun YaTeX-get-preview-file-name () yuuji@51: "Get file name to preview by inquiring YaTeX-get-latex-command" yuuji@51: (let* ((latex-cmd (YaTeX-get-latex-command t)) yuuji@51: (rin (rindex latex-cmd ? )) yuuji@51: (fname (if (> rin -1) (substring latex-cmd (1+ rin)) "")) yuuji@51: (period)) yuuji@51: (if (string= fname "") yuuji@51: (setq fname (substring (file-name-nondirectory yuuji@51: (buffer-file-name)) yuuji@51: 0 -4)) yuuji@51: (setq period (rindex fname ?.)) yuuji@69: (setq fname (substring fname 0 (if (eq -1 period) nil period)))))) yuuji@51: yuuji@51: (defun YaTeX-get-latex-command (&optional switch) yuuji@51: "Specify the latex-command name and its argument. yuuji@51: If there is a line which begins with string: \"%#!\", the following yuuji@51: strings are assumed to be the latex-command and arguments. The yuuji@51: default value of latex-command is: yuuji@51: tex-command FileName yuuji@51: and if you write \"%#!jlatex\" in the beginning of certain line. yuuji@51: \"jlatex \" FileName yuuji@51: will be the latex-command, yuuji@51: and you write \"%#!jlatex main.tex\" on some line and argument SWITCH yuuji@51: is non-nil, then yuuji@51: \"jlatex main.tex\" yuuji@51: yuuji@51: will be given to the shell." yuuji@51: (let (magic command target) yuuji@51: (setq parent yuuji@51: (cond yuuji@51: (YaTeX-parent-file YaTeX-parent-file) yuuji@51: (t (save-excursion yuuji@51: (YaTeX-visit-main t) yuuji@51: (file-name-nondirectory (buffer-file-name))))) yuuji@51: magic (YaTeX-get-builtin "!")) yuuji@51: (cond yuuji@51: (magic yuuji@51: (cond yuuji@51: (switch (if (string-match "\\s " magic) magic yuuji@51: (concat magic " " parent))) yuuji@51: (t (concat (substring magic 0 (string-match "\\s " magic)) " ")))) yuuji@69: (t (concat tex-command " " (if switch parent)))))) yuuji@51: yuuji@51: (defvar YaTeX-lpr-command-history nil yuuji@51: "Holds command line history of YaTeX-lpr.") yuuji@51: (put 'YaTeX-lpr-command-history 'no-default t) yuuji@51: (defun YaTeX-lpr (arg) yuuji@51: "Print out. If prefix arg ARG is non nil, call print driver without yuuji@51: page range description." yuuji@51: (interactive "P") yuuji@51: (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format)) yuuji@52: from to (lbuffer "*dvi-printing*") dir) yuuji@51: (setq yuuji@51: cmd yuuji@51: (YaTeX-replace-format yuuji@51: cmd "f" yuuji@51: (if (or arg (not (string-match "%f" cmd))) yuuji@51: "" yuuji@51: (YaTeX-replace-format yuuji@51: dviprint-from-format yuuji@51: "b" yuuji@51: (if (string= yuuji@51: (setq from (read-string "From page(default 1): ")) "") yuuji@51: "1" from)))) yuuji@51: ) yuuji@51: (setq yuuji@51: cmd yuuji@51: (YaTeX-replace-format yuuji@51: cmd "t" yuuji@51: (if (or arg (not (string-match "%t" cmd)) yuuji@51: (string= yuuji@51: (setq to (read-string "To page(default none): ")) "")) yuuji@51: "" yuuji@51: (YaTeX-replace-format dviprint-to-format "e" to))) yuuji@51: ) yuuji@51: (setq cmd yuuji@51: (read-string-with-history yuuji@51: "Edit command line: " yuuji@53: (format cmd yuuji@53: (if (get 'dvi2-command 'region) yuuji@53: (substring YaTeX-texput-file yuuji@53: 0 (rindex YaTeX-texput-file ?.)) yuuji@53: (YaTeX-get-preview-file-name))) yuuji@51: 'YaTeX-lpr-command-history)) yuuji@51: (save-excursion yuuji@51: (YaTeX-visit-main t) ;;change execution directory yuuji@52: (setq dir default-directory) yuuji@51: (YaTeX-showup-buffer yuuji@51: lbuffer (function (lambda (x) (nth 3 (window-edges x))))) yuuji@51: (set-buffer (get-buffer-create lbuffer)) yuuji@51: (erase-buffer) yuuji@52: (cd dir) ;for 19 yuuji@51: (cond yuuji@64: ((not (fboundp 'start-process)) yuuji@51: (call-process shell-file-name "con" "*dvi-printing*" nil yuuji@51: YaTeX-shell-command-option cmd)) yuuji@51: (t yuuji@51: (set-process-buffer yuuji@51: (start-process "print" "*dvi-printing*" shell-file-name yuuji@51: YaTeX-shell-command-option cmd) yuuji@51: (get-buffer lbuffer)) yuuji@69: (message "Starting printing command: %s..." cmd)))))) yuuji@51: yuuji@51: (defun YaTeX-main-file-p () yuuji@51: "Return if current buffer is main LaTeX source." yuuji@51: (cond yuuji@52: (YaTeX-parent-file yuuji@52: (eq (get-file-buffer YaTeX-parent-file) (current-buffer))) yuuji@51: ((YaTeX-get-builtin "!") yuuji@70: (string-match yuuji@70: (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!"))) yuuji@70: (buffer-name))) yuuji@51: (t yuuji@51: (save-excursion yuuji@57: (let ((latex-main-id yuuji@57: (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)"))) yuuji@51: (or (re-search-backward latex-main-id nil t) yuuji@69: (re-search-forward latex-main-id nil t))))))) yuuji@51: yuuji@51: (defun YaTeX-visit-main (&optional setbuf) yuuji@51: "Switch buffer to main LaTeX source. yuuji@51: Use set-buffer instead of switch-to-buffer if the optional second argument yuuji@51: SETBUF is t(Use it only from Emacs-Lisp program)." yuuji@51: (interactive "P") yuuji@51: (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil)) yuuji@70: (let ((ff (function (lambda (f) yuuji@70: (if setbuf (set-buffer (find-file-noselect f)) yuuji@70: (find-file f))))) yuuji@70: b-in main-file YaTeX-create-file-prefix-g yuuji@70: (hilit-auto-highlight (not setbuf))) yuuji@51: (if (setq b-in (YaTeX-get-builtin "!")) yuuji@51: (setq main-file (YaTeX-guess-parent b-in))) yuuji@51: (if YaTeX-parent-file yuuji@51: (setq main-file ;;(get-file-buffer YaTeX-parent-file) yuuji@51: YaTeX-parent-file)) yuuji@51: (if (YaTeX-main-file-p) yuuji@51: (if (interactive-p) (message "I think this is main LaTeX source.") nil) yuuji@51: (cond yuuji@70: ((and ;;(interactive-p) yuuji@70: main-file yuuji@70: (cond ((get-file-buffer main-file) yuuji@70: (cond yuuji@70: (setbuf (set-buffer (get-file-buffer main-file))) yuuji@70: ((get-buffer-window (get-file-buffer main-file)) yuuji@70: (select-window yuuji@70: (get-buffer-window (get-file-buffer main-file)))) yuuji@70: (t (switch-to-buffer (get-file-buffer main-file))))) yuuji@70: ((file-exists-p main-file) yuuji@70: (funcall ff main-file))))) yuuji@70: ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf))) yuuji@51: ((and main-file yuuji@51: (file-exists-p (setq main-file (concat "../" main-file))) yuuji@51: (y-or-n-p (concat (expand-file-name main-file) yuuji@51: " is main file?:"))) yuuji@51: (setq YaTeX-parent-file main-file) yuuji@70: ;(YaTeX-switch-to-buffer main-file setbuf) yuuji@70: (funcall ff main-file) yuuji@70: ) yuuji@51: (t (setq main-file (read-file-name "Enter your main text: " nil nil 1)) yuuji@51: (setq YaTeX-parent-file main-file) yuuji@70: ; (YaTeX-switch-to-buffer main-file setbuf)) yuuji@70: (funcall ff main-file)) yuuji@51: ))) yuuji@69: nil) yuuji@51: yuuji@51: (defun YaTeX-guess-parent (command-line) yuuji@51: (setq command-line yuuji@51: (if (string-match ".*\\s " command-line) yuuji@51: (substring command-line (match-end 0)) yuuji@51: (file-name-nondirectory (buffer-file-name))) yuuji@51: command-line yuuji@51: (concat (if (string-match "\\(.*\\)\\." command-line) yuuji@51: (substring command-line (match-beginning 1) (match-end 1)) yuuji@51: command-line) yuuji@69: ".tex"))) yuuji@51: yuuji@51: (defun YaTeX-visit-main-other-window () yuuji@51: "Switch to buffer main LaTeX source in other window." yuuji@51: (interactive) yuuji@51: (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.") yuuji@51: (YaTeX-switch-to-buffer-other-window yuuji@69: (concat (YaTeX-get-preview-file-name) ".tex")))) yuuji@51: yuuji@51: (defun YaTeX-get-builtin (key) yuuji@51: "Read source built-in command of %# usage." yuuji@51: (save-excursion yuuji@51: (goto-char (point-min)) yuuji@51: (if (and (re-search-forward yuuji@51: (concat "^" (regexp-quote (concat "%#" key))) nil t) yuuji@51: (not (eolp))) yuuji@51: (buffer-substring yuuji@51: (progn (skip-chars-forward " " (point-end-of-line))(point)) yuuji@51: (point-end-of-line)) yuuji@69: nil))) yuuji@51: yuuji@51: (defun YaTeX-save-buffers () yuuji@60: "Save buffers whose major-mode is equal to current major-mode." yuuji@51: (basic-save-buffer) yuuji@60: (let ((cmm major-mode)) yuuji@60: (save-excursion yuuji@60: (mapcar '(lambda (buf) yuuji@60: (set-buffer buf) yuuji@60: (if (and (buffer-file-name buf) yuuji@60: (eq major-mode cmm) yuuji@60: (buffer-modified-p buf) yuuji@60: (y-or-n-p (format "Save %s" (buffer-name buf)))) yuuji@60: (save-buffer buf))) yuuji@69: (buffer-list))))) yuuji@51: yuuji@51: (provide 'yatexprc)