yuuji@395: ;;; yatexprc.el --- YaTeX process handler -*- coding: sjis -*- yuuji@287: ;;; yuuji@509: ;;; (c)1993-2018 by HIROSE Yuuji.[yuuji@yatex.org] yuuji@517: ;;; Last modified Fri Jan 5 22:48:43 2018 on firestorm yuuji@366: ;;; $Id$ yuuji@51: yuuji@287: ;;; Code: yuuji@64: ;(require 'yatex) yuuji@64: (require 'yatexlib) yuuji@51: yuuji@51: (defvar YaTeX-typeset-process nil yuuji@388: "Process identifier for latex") yuuji@69: yuuji@51: (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*" yuuji@388: "Process buffer for latex") 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@79: ;; (cond yuuji@79: ;; (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist))) yuuji@79: ;; ((and YaTeX-emacs-20 (member 'undecided (coding-system-list)) yuuji@79: ;; 'undecided)) yuuji@79: ;; ((featurep 'mule) yuuji@79: ;; (or (and (boundp '*autoconv*) *autoconv*) yuuji@79: ;; (and (fboundp 'coding-system-list) 'automatic-conversion))) yuuji@79: ;; ((boundp 'NEMACS) yuuji@79: ;; (cdr (assq (if YaTeX-dos 1 2) YaTeX-kanji-code-alist)))) yuuji@64: (cond yuuji@80: (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist))) yuuji@79: (YaTeX-emacs-20 yuuji@79: (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist))) yuuji@79: ((boundp 'MULE) yuuji@79: (symbol-value yuuji@79: (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist)))) yuuji@64: ((boundp 'NEMACS) yuuji@79: latex-message-kanji-code)) 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@212: (defvar YaTeX-typeset-marker nil) yuuji@262: (defvar YaTeX-typeset-consumption nil) yuuji@262: (make-variable-buffer-local 'YaTeX-typeset-consumption) yuuji@213: (defun YaTeX-typeset (command buffer &optional prcname modename ppcmd) yuuji@388: "Execute latex command (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@354: (background (string-match "\\*bg:" buffer)) 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@354: (if background yuuji@354: nil ;do not showup yuuji@354: (YaTeX-showup-buffer yuuji@354: buffer 'YaTeX-showup-buffer-bottom-most)) yuuji@51: (set-buffer (get-buffer-create buffer)) yuuji@51: (setq default-directory execdir) yuuji@51: (cd execdir) yuuji@213: (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@123: (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel) yuuji@212: (put 'YaTeX-typeset-process 'thiscmd command) yuuji@212: (put 'YaTeX-typeset-process 'name prcname) yuuji@262: (if (fboundp 'current-time) yuuji@262: (setq YaTeX-typeset-consumption yuuji@262: (cons (cons 'time (current-time)) yuuji@262: (delq 'time YaTeX-typeset-consumption)))) yuuji@123: (let ((ppprop (get 'YaTeX-typeset-process 'ppcmd))) yuuji@123: (setq ppprop (delq (assq YaTeX-typeset-process ppprop) ppprop)) yuuji@123: (if ppcmd yuuji@123: (setq ppprop (cons (cons YaTeX-typeset-process ppcmd) ppprop))) yuuji@224: (put 'YaTeX-typeset-process 'ppcmd ppprop)) yuuji@224: (if (and (boundp 'bibcmd) bibcmd) yuuji@224: (let ((bcprop (get 'YaTeX-typeset-process 'bibcmd))) yuuji@224: (setq bcprop (cons yuuji@224: (cons YaTeX-typeset-process bibcmd) yuuji@224: (delq (assq YaTeX-typeset-process bcprop) bcprop))) yuuji@224: (put 'YaTeX-typeset-process 'bibcmd bcprop))))) 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@212: (set-marker (or YaTeX-typeset-marker yuuji@212: (setq YaTeX-typeset-marker (make-marker))) yuuji@212: (point)) yuuji@212: (insert (format "Call `%s'\n" command)) yuuji@370: (cond yuuji@370: ((fboundp 'start-process) yuuji@51: (insert " ") yuuji@51: (set-marker (process-mark YaTeX-typeset-process) (1- (point)))) yuuji@370: (t (message "Done."))) yuuji@51: (if (bolp) (forward-line -1)) ;what for? yuuji@354: (cond yuuji@354: (background nil) yuuji@354: ((and YaTeX-emacs-19 window-system) yuuji@354: (let ((win (get-buffer-window buffer t)) owin) yuuji@354: (select-frame (window-frame win)) yuuji@354: (setq owin (selected-window)) yuuji@354: (select-window win) yuuji@354: (goto-char (point-max)) yuuji@354: (recenter -1) yuuji@354: (select-window owin))) yuuji@354: (t (select-window (get-buffer-window buffer)) yuuji@354: (goto-char (point-max)) yuuji@354: (recenter -1))) yuuji@51: (select-window window) yuuji@60: (switch-to-buffer cb) yuuji@69: (YaTeX-remove-nonstopmode)))) yuuji@51: yuuji@262: (defvar YaTeX-typeset-auto-rerun t yuuji@262: "*Non-nil automatically reruns typesetter when cross-refs update found. yuuji@262: This is a toy mechanism. DO NOT RELY ON THIS MECHANISM. yuuji@262: You SHOULD check the integrity of cross-references with your eyes!! yuuji@262: Supplying an integer to this variable inhibit compulsory call of bibtex, yuuji@262: thus, it call bibtex only if warning messages about citation are seen.") yuuji@212: (defvar YaTeX-typeset-rerun-msg "Rerun to get cross-references right.") yuuji@224: (defvar YaTeX-typeset-citation-msg yuuji@261: "Warning: Citation \`") 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@212: (owin (selected-window)) win yuuji@261: tobecalled shortname yuuji@212: (thiscmd (get 'YaTeX-typeset-process 'thiscmd)) yuuji@212: (ppprop (get 'YaTeX-typeset-process 'ppcmd)) yuuji@224: (ppcmd (cdr (assq proc ppprop))) yuuji@224: (bcprop (get 'YaTeX-typeset-process 'bibcmd)) yuuji@224: (bibcmd (cdr (assq proc bcprop)))) yuuji@212: (put 'YaTeX-typeset-process 'ppcmd ;erase ppcmd yuuji@212: (delq (assq proc ppprop) ppprop)) yuuji@224: (put 'YaTeX-typeset-process 'bibcmd ;erase bibcmd yuuji@224: (delq (assq proc bcprop) bcprop)) 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@262: (insert yuuji@262: (format " at %s%s\n" yuuji@262: (substring (current-time-string) 0 -5) yuuji@262: (if (and (fboundp 'current-time) (fboundp 'float) yuuji@262: (assq 'time YaTeX-typeset-consumption)) yuuji@262: (format yuuji@262: " (%.2f secs)" yuuji@262: (YaTeX-elapsed-time yuuji@262: (cdr (assq 'time YaTeX-typeset-consumption)) yuuji@262: (current-time)))))) yuuji@51: (setq mode-line-process yuuji@51: (concat ": " yuuji@51: (symbol-name (process-status proc)))) yuuji@256: (message "%s %s" mode-name 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@224: (if (cond yuuji@263: ((or (not YaTeX-typeset-auto-rerun) yuuji@263: (string-match "latexmk" thiscmd)) yuuji@263: nil) yuuji@262: ((and bibcmd ;Call bibtex if bibcmd defined && yuuji@262: (or ; (1st call || warning found) yuuji@262: (and (not (numberp YaTeX-typeset-auto-rerun)) yuuji@262: ; cancel call at 1st, if value is a number. yuuji@262: (not (string-match "bibtex" mode-name))) yuuji@262: (re-search-backward yuuji@262: YaTeX-typeset-citation-msg yuuji@262: YaTeX-typeset-marker t)) yuuji@262: (save-excursion ; && using .bbl files. yuuji@224: (search-backward yuuji@224: ".bbl" YaTeX-typeset-marker t))) yuuji@262: ;; Always call bibtex after the first typesetting, yuuji@262: ;; because bibtex doesn't warn disappeared \cite. yuuji@262: ;; (Suggested by ryseto. 2012) yuuji@262: ;; It is more efficient to call bibtex directly than yuuji@262: ;; to call it after deep inspection on the balance yuuji@262: ;; of \cite vs. \bib*'s referring all *.aux files. yuuji@224: (insert "\n" YaTeX-typeset-rerun-msg "\n") yuuji@261: (setq tobecalled bibcmd shortname "+bibtex")) yuuji@261: ((or yuuji@261: (save-excursion yuuji@261: (search-backward yuuji@261: YaTeX-typeset-rerun-msg YaTeX-typeset-marker t)) yuuji@261: (save-excursion yuuji@261: (re-search-backward yuuji@261: "natbib.*Rerun to get citations correct" yuuji@261: YaTeX-typeset-marker t))) yuuji@224: (if bibcmd yuuji@224: (put 'YaTeX-typeset-process 'bibcmd yuuji@224: (cons (cons (get-buffer-process pbuf) bibcmd) yuuji@224: bcprop))) yuuji@261: (setq tobecalled thiscmd shortname "+typeset")) yuuji@261: (t yuuji@261: nil)) ;no need to call any process yuuji@354: (progn ;;Something occurs to call next command yuuji@212: (insert yuuji@212: (format yuuji@212: "===!!! %s !!!===\n" yuuji@212: (message "Rerun `%s' to get cross-references right" yuuji@224: tobecalled))) yuuji@224: (if (equal tobecalled thiscmd) yuuji@224: (set-marker YaTeX-typeset-marker (point))) yuuji@212: (set-process-sentinel yuuji@212: (start-process yuuji@261: (setq mode-name (concat mode-name shortname)) yuuji@256: pbuf yuuji@224: shell-file-name YaTeX-shell-command-option tobecalled) yuuji@212: 'YaTeX-typeset-sentinel) yuuji@212: (if ppcmd yuuji@212: (put 'YaTeX-typeset-process 'ppcmd yuuji@212: (cons (cons (get-buffer-process pbuf) ppcmd) yuuji@224: ppprop))) yuuji@224: (if thiscmd yuuji@224: (put 'YaTeX-typeset-process 'thiscmd thiscmd))) yuuji@212: ;; If ppcmd is active, call it. yuuji@123: (cond yuuji@371: ((and ppcmd (symbolp ppcmd) (fboundp ppcmd)) yuuji@371: ;; If ppcmd is set and it is a function symbol, yuuji@371: ;; call it whenever command succeeded or not yuuji@371: (funcall ppcmd)) yuuji@123: ((and ppcmd (string-match "finish" mes)) yuuji@123: (insert (format "=======> Success! Calling %s\n" ppcmd)) yuuji@371: (setq mode-name ; set process name yuuji@371: (concat yuuji@371: mode-name "+" yuuji@371: (substring ppcmd 0 (string-match " " ppcmd)))) yuuji@212: ; to reach here, 'start-process exists on this emacsen yuuji@371: (set-process-sentinel yuuji@371: (start-process yuuji@371: mode-name yuuji@371: pbuf ; Use this buffer twice. yuuji@371: shell-file-name YaTeX-shell-command-option yuuji@371: ppcmd) yuuji@371: 'YaTeX-typeset-sentinel)) yuuji@377: (t yuuji@377: (if (equal 0 (process-exit-status proc)) yuuji@408: ;; Successful typesetting done yuuji@408: (if (save-excursion yuuji@408: (re-search-backward yuuji@408: (concat yuuji@408: "^Output written on .*\\.pdf (.*page," yuuji@408: "\\|\\.dvi -> .*\\.pdf$") yuuji@408: nil t)) yuuji@408: ;; If PDF output log found in buffer, yuuji@408: ;; set next default previewer to 'pdf viewer yuuji@408: (put 'dvi2-command 'format 'pdf)) yuuji@378: ;;Confirm process buffer to be shown when error yuuji@378: (YaTeX-showup-buffer yuuji@378: pbuf 'YaTeX-showup-buffer-bottom-most) yuuji@378: (message "Command FAILED!")) yuuji@377: ;;pull back original mode-name yuuji@261: (setq mode-name "typeset")))) yuuji@123: (forward-char 1)) yuuji@123: (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@354: (defun YaTeX-typeset-region (&optional pp) 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@354: operation to the region. yuuji@354: Optional second argument PP specifies post-processor command which will be yuuji@354: called with one argument of current file name whitout extension." 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@354: (texputroot (substring texput 0 (string-match "\\.tex$" texput))) 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@354: ppcmd yuuji@52: reg-begin reg-end lineinfo) yuuji@354: (setq ppcmd (if (stringp pp) (concat pp " " texputroot) pp)) yuuji@51: (save-excursion yuuji@517: (if (re-search-backward "%#BEGIN\\s *$" 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@73: (concat yuuji@73: (if YaTeX-use-LaTeX2e "\\documentclass{" "\\documentstyle{") yuuji@73: YaTeX-default-document-style "}\n" yuuji@73: "\\begin{document}"))) yuuji@51: (goto-char opoint) yuuji@51: ;;(set-buffer buffer) ;for clarity yuuji@133: (let ((hilit-auto-highlight nil) (auto-mode-alist nil) yuuji@133: (magic-mode-alist nil)) ;Do not activate yatex-mode here yuuji@52: (set-buffer (find-file-noselect texput))) yuuji@51: ;;(find-file YaTeX-texput-file) yuuji@51: (erase-buffer) yuuji@354: (YaTeX-set-file-coding-system YaTeX-kanji-code YaTeX-coding-system) yuuji@64: (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) yuuji@51: (insert "\\nonstopmode{}\n")) yuuji@354: (insert preamble "\n" subpreamble "\n" yuuji@354: "\\pagestyle{empty}\n\\thispagestyle{empty}\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@354: (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd) 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@381: (defvar YaTeX-use-image-preview "png" yuuji@376: "*Nil means not using image preview by [prefix] t e. yuuji@376: Acceptable value is one of \"jpg\" or \"png\", which specifies yuuji@381: format of preview image. NOTE that if your system has /usr/bin/sips yuuji@391: while not having convert(ImageMagick), jpeg format is automatically taken yuuji@381: for conversion.") yuuji@355: (defvar YaTeX-preview-image-mode-map nil yuuji@355: "Keymap used in YaTeX-preview-image-mode") yuuji@355: (defun YaTeX-preview-image-mode () yuuji@355: (interactive) yuuji@355: (if YaTeX-preview-image-mode-map yuuji@355: nil yuuji@355: (let ((map (setq YaTeX-preview-image-mode-map (make-sparse-keymap)))) yuuji@355: (define-key map "q" (lambda()(interactive) yuuji@355: (kill-buffer) yuuji@355: (select-window yuuji@355: (or (get 'YaTeX-typeset-process 'win) yuuji@355: (selected-window))))) yuuji@355: (define-key map "j" (lambda()(interactive) (scroll-up 1))) yuuji@355: (define-key map "k" (lambda()(interactive) (scroll-up -1))))) yuuji@355: (use-local-map YaTeX-preview-image-mode-map)) yuuji@355: yuuji@371: (defvar YaTeX-typeset-pdf2image-chain yuuji@371: (cond yuuji@371: ((YaTeX-executable-find "pdfcrop") ;Mac OS X yuuji@371: (list yuuji@371: "pdfcrop --clip %b.pdf tmp.pdf" yuuji@371: (if (YaTeX-executable-find "convert") yuuji@371: "convert -density %d tmp.pdf %b.%f" yuuji@381: ;; If we use sips, specify jpeg as format yuuji@381: "sips -s format jpeg -s dpiWidth %d -s dpiHeight %d %b.pdf --out %b.jpg") yuuji@513: "rm -f tmp.pdf")) yuuji@513: ((YaTeX-executable-find "convert") yuuji@513: (list "convert -trim -density %d %b.pdf %b.%f")) yuuji@513: ) yuuji@371: "*Pipe line of command as a list to create image file from PDF. yuuji@371: See also doc-string of YaTeX-typeset-dvi2image-chain.") yuuji@371: yuuji@371: (defvar YaTeX-typeset-dvi2image-chain yuuji@371: (cond yuuji@371: ((YaTeX-executable-find YaTeX-cmd-dvips) yuuji@371: (list yuuji@371: (format "%s -E -o %%b.eps %%b.dvi" YaTeX-cmd-dvips) yuuji@381: "convert -alpha off -density %d %b.eps %b.%f")) yuuji@381: ((and (equal YaTeX-use-image-preview "png") yuuji@381: (YaTeX-executable-find "dvipng")) yuuji@381: (list "dvipng %b.dvi"))) yuuji@371: "*Pipe line of command as a list to create png file from DVI or PDF. yuuji@371: %-notation rewritten list: yuuji@371: %b basename of target file(\"texput\") yuuji@371: %f Output format(\"png\") yuuji@371: %d DPI yuuji@371: ") yuuji@371: yuuji@371: (defvar YaTeX-typeset-conv2image-chain-alist yuuji@371: (list (cons 'pdf YaTeX-typeset-pdf2image-chain) yuuji@371: (cons 'dvi YaTeX-typeset-dvi2image-chain)) yuuji@371: "Default alist for creating image files from DVI/PDF. yuuji@371: The value is generated from YaTeX-typeset-pdf2image-chain and yuuji@371: YaTeX-typeset-dvi2image-chain.") yuuji@371: yuuji@517: (defun YaTeX-popup-image (imagesrc buffer &optional func) yuuji@517: (let ((sw (selected-window)) image yuuji@517: (data-p (and (> (length imagesrc) 128) yuuji@517: (not (file-readable-p imagesrc))))) yuuji@517: (save-excursion yuuji@517: (cond yuuji@517: ((featurep 'image) window-system yuuji@517: (YaTeX-showup-buffer ;showup and select yuuji@517: (get-buffer-create buffer) yuuji@517: (or func 'YaTeX-showup-buffer-bottom-most) yuuji@517: t) yuuji@517: (remove-images (point-min) (point-max)) yuuji@517: (erase-buffer) yuuji@517: (insert-image yuuji@517: (setq image (create-image yuuji@517: (if data-p imagesrc (expand-file-name imagesrc)) yuuji@517: nil data-p))) yuuji@517: (YaTeX-preview-image-mode) yuuji@517: (let ((height (1+ (cdr (image-size image))))) yuuji@517: (enlarge-window yuuji@517: (- (ceiling (min height (/ (frame-height) 2))) yuuji@517: (window-height)))) yuuji@517: (select-window sw)) yuuji@517: (t ;; Without direct image, display image with image viewer yuuji@517: (YaTeX-system yuuji@517: (format "%s %s" YaTeX-cmd-view-images target) yuuji@517: "YaTeX-region-image" yuuji@517: 'noask))) yuuji@517: ))) yuuji@517: yuuji@354: (defvar YaTeX-typeset-conv2image-process nil "Process of conv2image chain") yuuji@354: (defun YaTeX-typeset-conv2image-chain () yuuji@354: (let*((proc (or YaTeX-typeset-process YaTeX-typeset-conv2image-process)) yuuji@354: (prevname (process-name proc)) yuuji@376: (texput "texput") yuuji@376: (format YaTeX-use-image-preview) yuuji@374: (target (concat texput "." format)) yuuji@354: (math (get 'YaTeX-typeset-conv2image-chain 'math)) yuuji@391: (dpi (get 'YaTeX-typeset-conv2image-chain 'dpi)) yuuji@371: (srctype (or (get 'YaTeX-typeset-conv2image-chain 'srctype) yuuji@374: (if (file-newer-than-file-p yuuji@374: (concat texput ".pdf") yuuji@374: (concat texput ".dvi")) yuuji@371: 'pdf 'dvi))) yuuji@371: (chain (cdr (assq srctype YaTeX-typeset-conv2image-chain-alist))) yuuji@371: ;; This function is the first evaluation code. yuuji@371: ;; If you find these command line does not work yuuji@371: ;; on your system, please tell the author yuuji@371: ;; which commands should be taken to achieve yuuji@371: ;; one-shot png previewing on your system yuuji@371: ;; before publishing patch on the Web. yuuji@371: ;; Please please please please please. yuuji@354: (curproc (member prevname chain)) yuuji@355: (w (get 'YaTeX-typeset-conv2image-chain 'win)) yuuji@355: (pwd default-directory) yuuji@354: img) yuuji@354: (if (not (= (process-exit-status proc) 0)) yuuji@354: (progn yuuji@362: (YaTeX-showup-buffer ;never comes here(?) yuuji@354: (current-buffer) 'YaTeX-showup-buffer-bottom-most) yuuji@354: (message "Region typesetting FAILED")) yuuji@354: (setq command yuuji@354: (if curproc (car (cdr-safe curproc)) (car chain))) yuuji@354: (if command yuuji@371: (let ((cmdline (YaTeX-replace-formats yuuji@371: command yuuji@371: (list (cons "b" "texput") yuuji@374: (cons "f" format) yuuji@391: (cons "d" dpi))))) yuuji@371: (insert (format "Calling `%s'...\n" cmdline)) yuuji@354: (set-process-sentinel yuuji@354: (setq YaTeX-typeset-conv2image-process yuuji@354: (start-process yuuji@354: command yuuji@354: (current-buffer) yuuji@371: shell-file-name YaTeX-shell-command-option yuuji@371: cmdline)) yuuji@354: 'YaTeX-typeset-sentinel) yuuji@354: (put 'YaTeX-typeset-process 'ppcmd yuuji@354: (cons (cons (get-buffer-process (current-buffer)) yuuji@354: 'YaTeX-typeset-conv2image-chain) yuuji@354: (get 'YaTeX-typeset-process 'ppcmd)))) yuuji@354: ;; After all chain executed, display image in current window yuuji@517: (YaTeX-popup-image target " *YaTeX-popup-image*") yuuji@517: (put 'YaTeX-typeset-conv2image-chain 'elapse yuuji@517: (YaTeX-elapsed-time yuuji@517: (get 'YaTeX-typeset-conv2image-chain 'start) (current-time))) yuuji@517: )))) yuuji@354: yuuji@375: yuuji@375: (defvar YaTeX-typeset-environment-timer nil) yuuji@375: (defun YaTeX-typeset-environment-timer () yuuji@375: "Update preview image in the yuuji@375: Plist: '(buf begPoint endPoint precedingChar 2precedingChar Substring time)" yuuji@375: (let*((plist (get 'YaTeX-typeset-environment-timer 'laststate)) yuuji@375: (b (nth 0 plist)) yuuji@375: (s (nth 1 plist)) yuuji@375: (e (nth 2 plist)) yuuji@375: (p (nth 3 plist)) yuuji@375: (q (nth 4 plist)) yuuji@375: (st (nth 5 plist)) yuuji@375: (tm (nth 6 plist)) yuuji@377: (overlay YaTeX-on-the-fly-overlay) yuuji@375: (thresh (* 2 (or (get 'YaTeX-typeset-conv2image-chain 'elapse) 1)))) yuuji@375: (cond yuuji@375: ;; In minibuffer, do nothing yuuji@375: ((minibuffer-window-active-p (selected-window)) nil) yuuji@375: ;; If point went out from last environment, cancel timer yuuji@375: ;;;((and (message "s=%d, e=%d, p=%d" s e (point)) nil)) yuuji@403: ((null (buffer-file-name)) nil) ;;At momentary-string-display, it's nil. yuuji@375: ((or (not (eq b (window-buffer (selected-window)))) yuuji@375: (< (point) s) yuuji@377: (not (overlayp overlay)) yuuji@377: (not (eq (overlay-buffer overlay) (current-buffer))) yuuji@377: (> (point) (overlay-end overlay))) yuuji@375: (YaTeX-typeset-environment-cancel-auto)) yuuji@375: ;;;((and (message "e=%d, p=%d t=%s" e (point) (current-time)) nil)) yuuji@375: ;; If recently called, hold yuuji@377: ;;; ((< (YaTeX-elapsed-time tm (current-time)) thresh) nil) yuuji@375: ;; If condition changed from last call, do it yuuji@377: ((not (string= st (YaTeX-buffer-substring s (overlay-end overlay)))) yuuji@375: (YaTeX-typeset-environment))))) yuuji@375: yuuji@391: yuuji@391: (defun YaTeX-typeset-environment-by-lmp () yuuji@391: (save-excursion yuuji@391: (let ((sw (selected-window))) yuuji@391: (goto-char opoint) yuuji@391: (latex-math-preview-expression) yuuji@391: (select-window sw)))) yuuji@391: yuuji@391: (defun YaTeX-typeset-environment-by-builtin () yuuji@391: (save-excursion yuuji@391: (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain))) yuuji@391: yuuji@391: (defvar YaTeX-on-the-fly-math-preview-engine yuuji@391: (if (fboundp 'latex-math-preview-expression) yuuji@391: 'YaTeX-typeset-environment-by-lmp yuuji@391: 'YaTeX-typeset-environment-by-builtin) yuuji@391: "Function symbol to use math-preview. yuuji@391: 'YaTeX-typeset-environment-by-lmp for using latex-math-preview, yuuji@391: 'YaTeX-typeset-environment-by-builtin for using yatex-builtin.") yuuji@391: yuuji@380: (defun YaTeX-typeset-environment-1 () yuuji@431: (require 'yatex23) yuuji@391: (let*((math (YaTeX-in-math-mode-p)) yuuji@406: (dpi (or (YaTeX-get-builtin "IMAGEDPI") (if math "300" "200"))) yuuji@403: (opoint (point)) yuuji@403: usetimer) yuuji@380: (cond yuuji@380: ((and YaTeX-on-the-fly-overlay (overlayp YaTeX-on-the-fly-overlay) yuuji@380: (member YaTeX-on-the-fly-overlay (overlays-at (point)))) yuuji@380: ;; If current position is in on-the-fly overlay, yuuji@380: ;; use that region again yuuji@380: (setq math (get 'YaTeX-typeset-conv2image-chain 'math)) yuuji@380: (push-mark (overlay-start YaTeX-on-the-fly-overlay)) yuuji@380: (goto-char (overlay-end YaTeX-on-the-fly-overlay))) yuuji@391: ((YaTeX-region-active-p) yuuji@380: nil) ;if region is active, use it yuuji@519: (math (setq usetimer YaTeX-on-the-fly-preview-interval) yuuji@519: (YaTeX-mark-environment)) yuuji@380: ((equal (or (YaTeX-inner-environment t) "document") "document") yuuji@380: (mark-paragraph)) yuuji@519: (t (setq usetimer YaTeX-on-the-fly-preview-interval) yuuji@519: (YaTeX-mark-environment))) yuuji@380: (if YaTeX-use-image-preview yuuji@380: (let ((YaTeX-typeset-buffer (concat "*bg:" YaTeX-typeset-buffer)) yuuji@380: (b (region-beginning)) (e (region-end))) yuuji@380: (put 'YaTeX-typeset-conv2image-chain 'math math) yuuji@391: (put 'YaTeX-typeset-conv2image-chain 'dpi dpi) yuuji@380: (put 'YaTeX-typeset-conv2image-chain 'srctype nil) yuuji@380: (put 'YaTeX-typeset-conv2image-chain 'win (selected-window)) yuuji@380: (put 'YaTeX-typeset-conv2image-chain 'start (current-time)) yuuji@380: (put 'YaTeX-typeset-environment-timer 'laststate yuuji@380: (list (current-buffer) b e (preceding-char) yuuji@380: (char-after (- (point) 2)) yuuji@380: (YaTeX-buffer-substring b e) yuuji@380: (current-time))) yuuji@391: (if math (funcall YaTeX-on-the-fly-math-preview-engine) yuuji@391: (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain)) yuuji@403: (if usetimer (YaTeX-typeset-environment-auto b e))) yuuji@380: (YaTeX-typeset-region)))) yuuji@380: yuuji@517: (defun YaTeX-filter-BEGEND () yuuji@517: (let ((begend-info (YaTeX-in-BEGEND-p))) yuuji@517: (if begend-info yuuji@517: (progn yuuji@517: (require 'yatexflt) yuuji@517: (YaTeX-filter-pass-to-filter begend-info) yuuji@517: )))) yuuji@517: yuuji@130: (defun YaTeX-typeset-environment () yuuji@371: "Typeset current environment or paragraph. yuuji@371: If region activated, use it." yuuji@130: (interactive) yuuji@380: (let ((md (match-data))) yuuji@380: (unwind-protect yuuji@380: (save-excursion yuuji@517: (or (YaTeX-filter-BEGEND) yuuji@517: (YaTeX-typeset-environment-1))) yuuji@380: (store-match-data md)))) yuuji@380: yuuji@364: yuuji@377: (defvar YaTeX-on-the-fly-preview-interval (string-to-number "0.9") yuuji@375: "*Control the on-the-fly update of preview environment by an image. yuuji@375: Nil disables on-the-fly update. Otherwise on-the-fly update is enabled yuuji@375: with update interval specified by this value.") yuuji@375: yuuji@403: (defun YaTeX-typeset-environment-auto (beg end) yuuji@375: "Turn on on-the-fly preview-image" yuuji@375: (if YaTeX-typeset-environment-timer yuuji@403: (cancel-timer YaTeX-typeset-environment-timer)) yuuji@403: (if YaTeX-on-the-fly-overlay yuuji@403: (move-overlay YaTeX-on-the-fly-overlay beg end) yuuji@403: (overlay-put yuuji@403: (setq YaTeX-on-the-fly-overlay (make-overlay beg end)) yuuji@403: 'face 'YaTeX-on-the-fly-activated-face)) yuuji@403: (setq YaTeX-typeset-environment-timer yuuji@403: (run-with-idle-timer yuuji@403: (max (string-to-number "0.1") yuuji@403: (cond yuuji@403: ((numberp YaTeX-on-the-fly-preview-interval) yuuji@403: YaTeX-on-the-fly-preview-interval) yuuji@403: ((stringp YaTeX-on-the-fly-preview-interval) yuuji@403: (string-to-number YaTeX-on-the-fly-preview-interval)) yuuji@403: (t 1))) yuuji@403: t 'YaTeX-typeset-environment-timer))) yuuji@403: yuuji@403: (defun YaTeX-typeset-environment-activate-onthefly () yuuji@403: (if (get-text-property (point) 'onthefly) yuuji@403: (save-excursion yuuji@403: (if YaTeX-typeset-environment-timer yuuji@403: (progn yuuji@403: (cancel-timer YaTeX-typeset-environment-timer) yuuji@403: (setq YaTeX-typeset-environment-timer nil))) yuuji@403: (if (YaTeX-on-begin-end-p) yuuji@405: (if (or (match-beginning 1) (match-beginning 3)) ;on beginning yuuji@403: (goto-char (match-end 0)) yuuji@403: (goto-char (match-beginning 0)))) yuuji@403: (YaTeX-typeset-environment)))) yuuji@375: yuuji@406: (defun YaTeX-on-the-fly-cancel () yuuji@406: "Reset on-the-fly stickiness" yuuji@406: (interactive) yuuji@406: (YaTeX-typeset-environment-cancel-auto 'stripoff) yuuji@406: t) ;t for kill-* yuuji@406: yuuji@406: (defun YaTeX-typeset-environment-cancel-auto (&optional stripoff) yuuji@375: "Cancel typeset-environment timer." yuuji@375: (interactive) yuuji@403: (if YaTeX-typeset-environment-timer yuuji@403: (cancel-timer YaTeX-typeset-environment-timer)) yuuji@403: (setq YaTeX-typeset-environment-timer yuuji@403: (run-with-idle-timer yuuji@403: (string-to-number "0.1") yuuji@403: t yuuji@403: 'YaTeX-typeset-environment-activate-onthefly)) yuuji@406: (let ((ov YaTeX-on-the-fly-overlay)) yuuji@406: (if stripoff yuuji@406: (remove-text-properties (overlay-start ov) yuuji@406: (1- (overlay-end ov)) yuuji@406: '(onthefly)) yuuji@406: (put-text-property (overlay-start YaTeX-on-the-fly-overlay) yuuji@406: (1- (overlay-end YaTeX-on-the-fly-overlay)) yuuji@406: 'onthefly yuuji@406: t)) yuuji@406: (delete-overlay ov) yuuji@406: (message "On-the-fly preview deactivated"))) yuuji@130: yuuji@123: (defun YaTeX-typeset-buffer (&optional pp) 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@123: action wants to be done, A:Add list, R:Replace list, %:comment-out list. yuuji@123: If optional argument PP given as string, PP is considered as post-process yuuji@123: command and call it with the same command argument as typesetter without yuuji@123: last extension. yuuji@123: eg. if PP is \"dvipdfmx\", called commands as follows. yuuji@123: platex foo.tex yuuji@123: dvipdfmx foo yuuji@123: PP command will be called iff typeset command exit successfully" 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@224: (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd yuuji@123: (cb (current-buffer))) yuuji@224: (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt yuuji@224: pparg (substring pparg (rindex pparg ? )) ;get last arg yuuji@224: pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext yuuji@506: bibcmd (YaTeX-replace-format yuuji@506: (or (YaTeX-get-builtin "BIBTEX") bibtex-command) yuuji@506: "k" (YaTeX-kanji-ptex-mnemonic))) yuuji@506: (or (string-match "\\s [^-]" bibcmd) ;if bibcmd has no argument, yuuji@262: (setq bibcmd (concat bibcmd pparg))) ;append argument(== %#!) yuuji@123: (and pp yuuji@123: (stringp pp) yuuji@224: (setq ppcmd (concat pp pparg))) 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@123: (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd) 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@86: (defun YaTeX-call-command-on-file (base-cmd buffer &optional file) yuuji@171: "Call external command BASE-CMD in the BUFFER. yuuji@86: By default, pass the basename of current file. Optional 3rd argument yuuji@86: FILE changes the default file name." yuuji@51: (YaTeX-save-buffers) yuuji@171: (let ((default (concat base-cmd " " yuuji@171: (let ((me (file-name-nondirectory yuuji@171: (or file buffer-file-name)))) yuuji@171: (if (string-match "\\.tex" me) yuuji@171: (substring me 0 (match-beginning 0)) yuuji@171: me))))) yuuji@171: (or YaTeX-call-command-history yuuji@171: (setq YaTeX-call-command-history (list default))) yuuji@171: (YaTeX-typeset yuuji@171: (read-string-with-history yuuji@171: "Call command: " yuuji@171: (car YaTeX-call-command-history) yuuji@171: 'YaTeX-call-command-history) yuuji@171: buffer))) yuuji@51: yuuji@227: (defvar YaTeX-call-builtin-on-file) yuuji@227: (make-variable-buffer-local 'YaTeX-call-builtin-on-file) yuuji@227: (defun YaTeX-call-builtin-on-file (builtin-type &optional default update) yuuji@224: "Call command on file specified by BUILTIN-TYPE." yuuji@51: (YaTeX-save-buffers) yuuji@224: (let*((main (or YaTeX-parent-file yuuji@227: (save-excursion (YaTeX-visit-main t) buffer-file-name))) yuuji@224: (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.)))) yuuji@227: (alist YaTeX-call-builtin-on-file) yuuji@511: (b-in (or (YaTeX-get-builtin builtin-type) yuuji@511: (cdr (assoc builtin-type alist)))) yuuji@227: (command b-in)) yuuji@227: (if (or update (null b-in)) yuuji@227: (progn yuuji@227: (setq command (read-string-with-history yuuji@227: (format "%s command: " builtin-type) yuuji@227: (or b-in yuuji@227: (format "%s %s" default mainroot)) yuuji@227: 'YaTeX-call-command-history)) yuuji@227: (if (or update (null b-in)) yuuji@362: (if (y-or-n-p "Memorize this command line in this file? ") yuuji@227: (YaTeX-getset-builtin builtin-type command) ;keep in a file yuuji@227: (setq YaTeX-call-builtin-on-file ;keep in memory yuuji@227: (cons (cons builtin-type command) yuuji@362: (delete (assoc builtin-type alist) alist))) yuuji@362: (message "`%s' kept in memory. Type `%s %s' to override." yuuji@362: command yuuji@362: (key-description yuuji@362: (car (where-is-internal 'universal-argument))) yuuji@362: (key-description (this-command-keys))) yuuji@362: (sit-for 2))))) yuuji@224: (YaTeX-typeset yuuji@511: (YaTeX-replace-format command "k" (YaTeX-kanji-ptex-mnemonic)) yuuji@362: (format " *YaTeX-%s*" (downcase builtin-type)) yuuji@362: builtin-type builtin-type))) 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@351: (defun YaTeX-system (command name &optional noask basedir) yuuji@351: "Execute some COMMAND with process name `NAME'. Not a official function. yuuji@514: Optional third argument NOASK skip query when privious process running. yuuji@514: Optional fourth argument BASEDIR changes default-directory there." yuuji@51: (save-excursion yuuji@351: (let ((df default-directory) yuuji@351: (buffer (get-buffer-create (format " *%s*" name))) yuuji@351: proc status) yuuji@351: (set-buffer buffer) yuuji@351: (setq default-directory (cd (or basedir df))) yuuji@351: (erase-buffer) yuuji@362: (insert (format "Calling `%s'...\n" command) yuuji@514: "==Kill this buffer to STOP process==\n") yuuji@354: (YaTeX-showup-buffer buffer 'YaTeX-showup-buffer-bottom-most) yuuji@351: (if (not (fboundp 'start-process)) yuuji@351: (call-process yuuji@351: shell-file-name nil buffer nil YaTeX-shell-command-option command) yuuji@351: (if (and (setq proc (get-buffer-process buffer)) yuuji@351: (setq status (process-status proc)) yuuji@351: (eq status 'run) yuuji@351: (not noask) yuuji@351: (not yuuji@351: (y-or-n-p (format "Process %s is running. Continue?" buffer)))) yuuji@351: nil yuuji@351: (if (eq status 'run) yuuji@351: (progn (interrupt-process proc) (delete-process proc))) yuuji@351: (set-process-buffer yuuji@351: (start-process yuuji@351: name buffer shell-file-name YaTeX-shell-command-option command) yuuji@351: (get-buffer buffer))))))) yuuji@51: yuuji@72: (defvar YaTeX-default-paper-type "a4" yuuji@72: "*Default paper type.") yuuji@72: (defconst YaTeX-paper-type-alist yuuji@72: '(("a4paper" . "a4") yuuji@72: ("a5paper" . "a5") yuuji@72: ("b4paper" . "b4") yuuji@72: ("b5paper" . "b5")) yuuji@72: "Holds map of options and paper types.") yuuji@72: (defconst YaTeX-dvips-paper-option-alist yuuji@72: '(("a4" . "-t a4") yuuji@72: ("a5" . "-t a5") yuuji@72: ("b4" . "-t b4") yuuji@72: ("b5" . "-t b5") yuuji@72: ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time. yuuji@72: ("a5r" . "-t landscape") yuuji@72: ("b4r" . "-t landscape") yuuji@72: ("b5r" . "-t landscape")) yuuji@72: "Holds map of dvips options and paper types.") yuuji@72: (defun YaTeX-get-paper-type () yuuji@72: "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc." yuuji@72: (save-excursion yuuji@77: (YaTeX-visit-main t) yuuji@72: (goto-char (point-min)) yuuji@77: (let ((opts yuuji@77: (if (re-search-forward yuuji@77: "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t) yuuji@77: (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*")))) yuuji@77: (concat yuuji@77: (catch 'found-paper yuuji@77: (mapcar (lambda (pair) yuuji@79: (if (YaTeX-member (car pair) opts) yuuji@77: (throw 'found-paper (cdr pair)))) yuuji@77: YaTeX-paper-type-alist) yuuji@77: YaTeX-default-paper-type) yuuji@79: (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") ""))))) yuuji@72: 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@328: (defun YaTeX-preview-default-previewer () yuuji@328: "Return default previewer for this document" yuuji@328: (YaTeX-replace-format yuuji@410: (if (eq (get 'dvi2-command 'format) 'pdf) yuuji@410: (or (YaTeX-get-builtin "PDFVIEW") yuuji@410: tex-pdfview-command) yuuji@410: (or (YaTeX-get-builtin "PREVIEW") yuuji@410: dvi2-command)) yuuji@410: "p" (format (cond yuuji@410: (YaTeX-dos "-y:%s") yuuji@410: (t "-paper %s")) yuuji@410: (YaTeX-get-paper-type)))) yuuji@328: (defun YaTeX-preview-default-main (command) yuuji@328: "Return default preview target file" yuuji@328: (if (get 'dvi2-command 'region) yuuji@86: (substring YaTeX-texput-file yuuji@86: 0 (rindex YaTeX-texput-file ?.)) yuuji@328: (YaTeX-get-preview-file-name command))) yuuji@328: (defun YaTeX-preview (preview-command preview-file &optional as-default) yuuji@328: "Execute xdvi (or other) to tex-preview." yuuji@328: (interactive yuuji@328: (let* ((previewer (YaTeX-preview-default-previewer)) yuuji@328: (as-default current-prefix-arg) yuuji@328: (command (if as-default yuuji@328: previewer yuuji@328: (read-string-with-history yuuji@328: "Preview command: " yuuji@328: previewer yuuji@328: 'YaTeX-preview-command-history))) yuuji@328: (target (YaTeX-preview-default-main command)) yuuji@328: (file (if as-default yuuji@328: target yuuji@328: (read-string-with-history yuuji@328: "Preview file: " yuuji@328: target yuuji@328: 'YaTeX-preview-file-history)))) yuuji@86: (list command file))) yuuji@328: (setq dvi2-command preview-command) ;`dvi2-command' is buffer local yuuji@51: (save-excursion yuuji@51: (YaTeX-visit-main t) yuuji@86: (if YaTeX-dos (setq preview-file (expand-file-name preview-file))) yuuji@51: (let ((pbuffer "*dvi-preview*") (dir default-directory)) yuuji@51: (YaTeX-showup-buffer yuuji@354: pbuffer 'YaTeX-showup-buffer-bottom-most) 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@86: (let ((process-connection-type nil)) yuuji@86: (start-process "preview" "*dvi-preview*" shell-file-name yuuji@86: YaTeX-shell-command-option yuuji@86: (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@350: (defun YaTeX-preview-jlfmt-xdvi () yuuji@350: "Call xdvi -sourceposition to DVI corresponding to current main file" yuuji@351: (interactive)) yuuji@350: yuuji@350: (defun YaTeX-preview-jump-line () yuuji@350: "Call jump-line function of various previewer on current main file" yuuji@350: (interactive) yuuji@350: (save-excursion yuuji@350: (save-restriction yuuji@350: (widen) yuuji@350: (let*((pf (or YaTeX-parent-file yuuji@350: (save-excursion (YaTeX-visit-main t) (buffer-file-name)))) yuuji@351: (pdir (file-name-directory pf)) yuuji@350: (bnr (substring pf 0 (string-match "\\....$" pf))) yuuji@436: ;(cf (file-relative-name (buffer-file-name) pdir)) yuuji@436: (cf (buffer-file-name)) ;2016-01-08 yuuji@350: (buffer (get-buffer-create " *preview-jump-line*")) yuuji@350: (line (count-lines (point-min) (point-end-of-line))) yuuji@350: (previewer (YaTeX-preview-default-previewer)) yuuji@350: (cmd (cond yuuji@350: ((string-match "xdvi" previewer) yuuji@350: (format "%s -nofork -sourceposition '%d %s' %s.dvi" yuuji@350: YaTeX-xdvi-remote-program yuuji@350: line cf bnr)) yuuji@350: ((string-match "Skim" previewer) yuuji@352: (format "%s %d '%s.pdf' '%s'" yuuji@350: YaTeX-cmd-displayline line bnr cf)) yuuji@350: ((string-match "evince" previewer) yuuji@352: (format "%s '%s.pdf' %d '%s'" yuuji@352: "fwdevince" bnr line cf)) yuuji@362: ;; yuuji@362: ;; These lines below for other PDF viewer is not confirmed yuuji@362: ;; yet. If you find correct command line, PLEASE TELL yuuji@362: ;; IT TO THE AUTHOR before publishing patch on the web. yuuji@362: ;; ..PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE.. yuuji@369: ((string-match "sumatra" previewer) ;;?? yuuji@369: (format "%s \"%s.pdf\" -forward-search \"%s\" %d" yuuji@369: ;;Send patch to the author, please yuuji@369: previewer bnr cf line)) yuuji@369: ((string-match "qpdfview" previewer) ;;?? yuuji@369: (format "%s '%s.pdf#src:%s:%d:0'" yuuji@369: ;;Send patch to the author, please yuuji@369: previewer bnr cf line)) yuuji@369: ((string-match "okular" previewer) ;;?? yuuji@507: (format "%s '%s.pdf#src:%d %s'" yuuji@369: ;;Send patch to the author, please yuuji@369: previewer bnr line cf)) yuuji@362: ))) yuuji@351: (YaTeX-system cmd "jump-line" 'noask pdir))))) yuuji@350: yuuji@350: (defun YaTeX-goto-corresponding-viewer () yuuji@350: (let ((cmd (or (YaTeX-get-builtin "!") tex-command))) yuuji@350: (if (string-match "-src\\|synctex=" cmd) yuuji@350: (progn yuuji@350: (YaTeX-preview-jump-line) yuuji@350: t) ;for YaTeX-goto-corresponding-* yuuji@350: nil))) yuuji@350: yuuji@455: (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@230: "Visit position of previous typeset error or warning. 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@230: (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name))) yuuji@230: (cur-win (selected-window)) yuuji@455: tsb-frame-selwin yuuji@230: b0 bound errorp 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@455: (and (fboundp 'selected-frame) yuuji@455: (setq typeset-win (get-buffer-window YaTeX-typeset-buffer t)) yuuji@455: (setq tsb-frame-selwin (frame-selected-window typeset-win))) yuuji@455: yuuji@51: (YaTeX-showup-buffer YaTeX-typeset-buffer nil t) yuuji@230: (if (and (markerp YaTeX-typeset-marker) yuuji@230: (eq (marker-buffer YaTeX-typeset-marker) (current-buffer))) yuuji@230: (setq bound YaTeX-typeset-marker)) 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@230: bound t) yuuji@80: (setq errorp (match-beginning 1)) yuuji@51: (select-window cur-win) yuuji@68: (error "No more errors on %s" cur-buf)) yuuji@80: (goto-char (setq b0 (match-beginning 0))) yuuji@51: (skip-chars-forward "^0-9" (match-end 0)) yuuji@51: (setq error-line yuuji@452: (YaTeX-str2int 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@455: yuuji@455: (select-window typeset-win) yuuji@455: (skip-chars-backward "0-9") yuuji@455: (recenter (/ (window-height) 2)) yuuji@455: (sit-for 1) yuuji@455: (goto-char b0) yuuji@455: (and tsb-frame-selwin (select-window tsb-frame-selwin)) ;restore selwin yuuji@455: yuuji@52: (setq error-win (get-buffer-window error-buffer)) yuuji@51: (select-window cur-win) yuuji@51: (cond yuuji@455: (t (goto-buffer-window error-buffer) yuuji@455: (if (fboundp 'raise-frame) yuuji@455: (let ((edit-frame (window-frame (selected-window)))) yuuji@455: (raise-frame edit-frame) yuuji@455: (select-frame edit-frame))) yuuji@455: ) 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@80: (if errorp "error" "warning") yuuji@51: error-buffer error-line) 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@452: (setq error-line (YaTeX-str2int 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@453: (if (re-search-forward yuuji@453: "\\.\\(tex\\|sty\\|ltx\\)\\>" nil (point-end-of-line)) yuuji@453: (match-end 0) yuuji@453: (skip-chars-forward "^ \n" (point-end-of-line)) yuuji@453: (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@80: (defvar YaTeX-dvi2-command-ext-alist yuuji@388: '(("[agxk]dvi\\|dviout" . ".dvi") yuuji@80: ("ghostview\\|gv" . ".ps") yuuji@505: ("acroread\\|[xk]pdf\\|pdfopen\\|Preview\\|TeXShop\\|Skim\\|evince\\|atril\\|xreader\\|mupdf\\|zathura\\|okular" . ".pdf"))) yuuji@80: yuuji@86: (defun YaTeX-get-preview-file-name (&optional preview-command) yuuji@51: "Get file name to preview by inquiring YaTeX-get-latex-command" yuuji@86: (if (null preview-command) (setq preview-command dvi2-command)) yuuji@51: (let* ((latex-cmd (YaTeX-get-latex-command t)) yuuji@51: (rin (rindex latex-cmd ? )) yuuji@172: (fname (if rin (substring latex-cmd (1+ rin)) "")) yuuji@86: (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist)) yuuji@172: (ext (if r (cdr r) ""))) yuuji@325: (and (null r) yuuji@325: (eq (get 'dvi2-command 'format) 'pdf) yuuji@499: (setq ext ".pdf")) yuuji@80: (concat yuuji@80: (if (string= fname "") yuuji@172: (setq fname (substring (file-name-nondirectory yuuji@172: (buffer-file-name)) yuuji@172: 0 -4)) yuuji@172: (setq fname (substring fname 0 (rindex fname ?.)))) yuuji@80: ext))) 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@388: and if you write \"%#!platex\" in the beginning of certain line. yuuji@388: \"platex \" FileName yuuji@51: will be the latex-command, yuuji@388: and you write \"%#!platex main.tex\" on some line and argument SWITCH yuuji@51: is non-nil, then yuuji@388: \"platex main.tex\" yuuji@51: yuuji@51: will be given to the shell." yuuji@80: (let (parent tparent magic) yuuji@51: (setq parent yuuji@51: (cond yuuji@80: (YaTeX-parent-file yuuji@80: (if YaTeX-dos (expand-file-name YaTeX-parent-file) yuuji@80: YaTeX-parent-file)) yuuji@51: (t (save-excursion yuuji@51: (YaTeX-visit-main t) yuuji@51: (file-name-nondirectory (buffer-file-name))))) yuuji@80: magic (YaTeX-get-builtin "!") yuuji@80: tparent (file-name-nondirectory parent)) yuuji@80: (YaTeX-replace-formats yuuji@80: (cond yuuji@80: (magic yuuji@80: (cond yuuji@350: (switch (if (string-match "\\s [^-]\\S *$" magic) magic yuuji@80: (concat magic " " parent))) yuuji@350: (t (concat (substring magic 0 (string-match "\\s [^-]\\S *$" magic)) " ")))) yuuji@80: (t (concat tex-command " " (if switch parent)))) yuuji@80: (list (cons "f" tparent) yuuji@503: (cons "r" (substring tparent 0 (rindex tparent ?.))) yuuji@503: (cons "k" (YaTeX-kanji-ptex-mnemonic)))))) 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@79: (defvar YaTeX-lpr-ask-page-range-default t) yuuji@51: (defun YaTeX-lpr (arg) yuuji@79: "Print out. yuuji@79: If prefix arg ARG is non nil, call print driver without yuuji@51: page range description." yuuji@51: (interactive "P") yuuji@79: (or YaTeX-lpr-ask-page-range-default (setq arg (not arg))) 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@72: (setq yuuji@72: cmd yuuji@72: (YaTeX-replace-format yuuji@72: cmd "p" yuuji@72: (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist)))) 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@354: lbuffer 'YaTeX-showup-buffer-bottom-most) 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@86: (let ((process-connection-type nil)) yuuji@86: (start-process "print" "*dvi-printing*" shell-file-name yuuji@86: 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@122: Use set-buffer instead of switch-to-buffer if the optional 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@325: b-in main-file mfa 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@80: (or b-in yuuji@325: (y-or-n-p (concat (setq mfa (expand-file-name main-file)) yuuji@80: " is main file?:")))) yuuji@325: (setq YaTeX-parent-file mfa) 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@326: (setq YaTeX-parent-file (expand-file-name 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@350: (if (string-match "\\s \\([^-]\\S *\\)$" command-line) yuuji@350: (substring command-line (match-beginning 1)) 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-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@351: (mapcar (function yuuji@351: (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@351: (save-buffer buf)))) yuuji@69: (buffer-list))))) yuuji@51: yuuji@51: (provide 'yatexprc)