yatex

annotate yatexprc.el @ 583:d4831b3672f8

Add some html5 elements
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 01 Jul 2021 07:00:05 +0900
parents 117a846879bc
children 86ceef677ca2
rev   line source
yuuji@395 1 ;;; yatexprc.el --- YaTeX process handler -*- coding: sjis -*-
yuuji@287 2 ;;;
yuuji@578 3 ;;; (c)1993-2019 by HIROSE Yuuji.[yuuji@yatex.org]
yuuji@578 4 ;;; Last modified Thu Dec 26 12:48:12 2019 on firestorm
yuuji@366 5 ;;; $Id$
yuuji@51 6
yuuji@287 7 ;;; Code:
yuuji@64 8 ;(require 'yatex)
yuuji@64 9 (require 'yatexlib)
yuuji@51 10
yuuji@51 11 (defvar YaTeX-typeset-process nil
yuuji@388 12 "Process identifier for latex")
yuuji@69 13
yuuji@51 14 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
yuuji@388 15 "Process buffer for latex")
yuuji@51 16
yuuji@51 17 (defvar YaTeX-typeset-buffer-syntax nil
yuuji@51 18 "*Syntax table for typesetting buffer")
yuuji@51 19
yuuji@51 20 (defvar YaTeX-current-TeX-buffer nil
yuuji@51 21 "Keeps the buffer on which recently typeset run.")
yuuji@51 22
yuuji@51 23 (defvar YaTeX-shell-command-option
yuuji@51 24 (or (and (boundp 'shell-command-option) shell-command-option)
yuuji@68 25 (and (boundp 'shell-command-switch) shell-command-switch)
yuuji@51 26 (if YaTeX-dos "/c" "-c"))
yuuji@51 27 "Shell option for command execution.")
yuuji@51 28
yuuji@64 29 (defvar YaTeX-latex-message-code
yuuji@79 30 ;; (cond
yuuji@79 31 ;; (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
yuuji@79 32 ;; ((and YaTeX-emacs-20 (member 'undecided (coding-system-list))
yuuji@79 33 ;; 'undecided))
yuuji@79 34 ;; ((featurep 'mule)
yuuji@79 35 ;; (or (and (boundp '*autoconv*) *autoconv*)
yuuji@79 36 ;; (and (fboundp 'coding-system-list) 'automatic-conversion)))
yuuji@79 37 ;; ((boundp 'NEMACS)
yuuji@79 38 ;; (cdr (assq (if YaTeX-dos 1 2) YaTeX-kanji-code-alist))))
yuuji@64 39 (cond
yuuji@80 40 (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
yuuji@79 41 (YaTeX-emacs-20
yuuji@79 42 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist)))
yuuji@79 43 ((boundp 'MULE)
yuuji@79 44 (symbol-value
yuuji@79 45 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist))))
yuuji@64 46 ((boundp 'NEMACS)
yuuji@79 47 latex-message-kanji-code))
yuuji@64 48 "Process coding system for LaTeX.")
yuuji@64 49
yuuji@51 50 (if YaTeX-typeset-buffer-syntax nil
yuuji@51 51 (setq YaTeX-typeset-buffer-syntax
yuuji@51 52 (make-syntax-table (standard-syntax-table)))
yuuji@51 53 (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
yuuji@51 54 (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
yuuji@51 55 (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
yuuji@69 56 (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax))
yuuji@51 57
yuuji@212 58 (defvar YaTeX-typeset-marker nil)
yuuji@262 59 (defvar YaTeX-typeset-consumption nil)
yuuji@262 60 (make-variable-buffer-local 'YaTeX-typeset-consumption)
yuuji@213 61 (defun YaTeX-typeset (command buffer &optional prcname modename ppcmd)
yuuji@388 62 "Execute latex command (or other) to LaTeX typeset."
yuuji@51 63 (interactive)
yuuji@51 64 (save-excursion
yuuji@60 65 (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer))
yuuji@64 66 (map YaTeX-typesetting-mode-map)
yuuji@354 67 (background (string-match "\\*bg:" buffer))
yuuji@64 68 (outcode
yuuji@64 69 (cond ((eq major-mode 'yatex-mode) YaTeX-coding-system)
yuuji@69 70 ((eq major-mode 'yahtml-mode) yahtml-kanji-code))))
yuuji@51 71 (if (and YaTeX-typeset-process
yuuji@51 72 (eq (process-status YaTeX-typeset-process) 'run))
yuuji@51 73 ;; if tex command is halting.
yuuji@51 74 (YaTeX-kill-typeset-process YaTeX-typeset-process))
yuuji@60 75 (YaTeX-put-nonstopmode)
yuuji@60 76 (setq prcname (or prcname "LaTeX")
yuuji@60 77 modename (or modename "typeset"))
yuuji@60 78 (if (eq major-mode 'yatex-mode) (YaTeX-visit-main t)) ;;execution dir
yuuji@51 79 (setq execdir default-directory)
yuuji@51 80 ;;Select lower-most window if there are more than 2 windows and
yuuji@51 81 ;;typeset buffer not seen.
yuuji@354 82 (if background
yuuji@354 83 nil ;do not showup
yuuji@354 84 (YaTeX-showup-buffer
yuuji@354 85 buffer 'YaTeX-showup-buffer-bottom-most))
yuuji@51 86 (set-buffer (get-buffer-create buffer))
yuuji@51 87 (setq default-directory execdir)
yuuji@51 88 (cd execdir)
yuuji@213 89 (erase-buffer)
yuuji@51 90 (cond
yuuji@64 91 ((not (fboundp 'start-process)) ;YaTeX-dos;if MS-DOS
yuuji@51 92 (call-process
yuuji@64 93 shell-file-name nil buffer nil YaTeX-shell-command-option command))
yuuji@51 94 (t ;if UNIX
yuuji@51 95 (set-process-buffer
yuuji@51 96 (setq YaTeX-typeset-process
yuuji@60 97 (start-process prcname buffer shell-file-name
yuuji@51 98 YaTeX-shell-command-option command))
yuuji@51 99 (get-buffer buffer))
yuuji@123 100 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)
yuuji@212 101 (put 'YaTeX-typeset-process 'thiscmd command)
yuuji@212 102 (put 'YaTeX-typeset-process 'name prcname)
yuuji@262 103 (if (fboundp 'current-time)
yuuji@262 104 (setq YaTeX-typeset-consumption
yuuji@262 105 (cons (cons 'time (current-time))
yuuji@262 106 (delq 'time YaTeX-typeset-consumption))))
yuuji@123 107 (let ((ppprop (get 'YaTeX-typeset-process 'ppcmd)))
yuuji@123 108 (setq ppprop (delq (assq YaTeX-typeset-process ppprop) ppprop))
yuuji@123 109 (if ppcmd
yuuji@123 110 (setq ppprop (cons (cons YaTeX-typeset-process ppcmd) ppprop)))
yuuji@224 111 (put 'YaTeX-typeset-process 'ppcmd ppprop))
yuuji@224 112 (if (and (boundp 'bibcmd) bibcmd)
yuuji@224 113 (let ((bcprop (get 'YaTeX-typeset-process 'bibcmd)))
yuuji@224 114 (setq bcprop (cons
yuuji@224 115 (cons YaTeX-typeset-process bibcmd)
yuuji@224 116 (delq (assq YaTeX-typeset-process bcprop) bcprop)))
yuuji@224 117 (put 'YaTeX-typeset-process 'bibcmd bcprop)))))
yuuji@527 118 (message "Calling `%s'..." command)
yuuji@51 119 (setq YaTeX-current-TeX-buffer (buffer-name))
yuuji@60 120 (use-local-map map) ;map may be localized
yuuji@51 121 (set-syntax-table YaTeX-typeset-buffer-syntax)
yuuji@60 122 (setq mode-name modename)
yuuji@60 123 (if YaTeX-typeset-process ;if process is running (maybe on UNIX)
yuuji@64 124 (cond ((fboundp 'set-current-process-coding-system)
yuuji@51 125 (set-current-process-coding-system
yuuji@64 126 YaTeX-latex-message-code outcode))
yuuji@64 127 ((fboundp 'set-process-coding-system)
yuuji@64 128 (set-process-coding-system
yuuji@64 129 YaTeX-typeset-process YaTeX-latex-message-code outcode))
yuuji@64 130 (YaTeX-emacs-20
yuuji@64 131 (set-buffer-process-coding-system
yuuji@64 132 YaTeX-latex-message-code outcode))
yuuji@51 133 ((boundp 'NEMACS)
yuuji@51 134 (set-kanji-process-code YaTeX-latex-message-code))))
yuuji@212 135 (set-marker (or YaTeX-typeset-marker
yuuji@212 136 (setq YaTeX-typeset-marker (make-marker)))
yuuji@212 137 (point))
yuuji@212 138 (insert (format "Call `%s'\n" command))
yuuji@370 139 (cond
yuuji@370 140 ((fboundp 'start-process)
yuuji@51 141 (insert " ")
yuuji@51 142 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
yuuji@370 143 (t (message "Done.")))
yuuji@51 144 (if (bolp) (forward-line -1)) ;what for?
yuuji@354 145 (cond
yuuji@354 146 (background nil)
yuuji@354 147 ((and YaTeX-emacs-19 window-system)
yuuji@354 148 (let ((win (get-buffer-window buffer t)) owin)
yuuji@354 149 (select-frame (window-frame win))
yuuji@354 150 (setq owin (selected-window))
yuuji@354 151 (select-window win)
yuuji@354 152 (goto-char (point-max))
yuuji@354 153 (recenter -1)
yuuji@354 154 (select-window owin)))
yuuji@354 155 (t (select-window (get-buffer-window buffer))
yuuji@354 156 (goto-char (point-max))
yuuji@354 157 (recenter -1)))
yuuji@51 158 (select-window window)
yuuji@60 159 (switch-to-buffer cb)
yuuji@69 160 (YaTeX-remove-nonstopmode))))
yuuji@51 161
yuuji@262 162 (defvar YaTeX-typeset-auto-rerun t
yuuji@262 163 "*Non-nil automatically reruns typesetter when cross-refs update found.
yuuji@262 164 This is a toy mechanism. DO NOT RELY ON THIS MECHANISM.
yuuji@262 165 You SHOULD check the integrity of cross-references with your eyes!!
yuuji@262 166 Supplying an integer to this variable inhibit compulsory call of bibtex,
yuuji@262 167 thus, it call bibtex only if warning messages about citation are seen.")
yuuji@212 168 (defvar YaTeX-typeset-rerun-msg "Rerun to get cross-references right.")
yuuji@224 169 (defvar YaTeX-typeset-citation-msg
yuuji@261 170 "Warning: Citation \`")
yuuji@576 171 (defvar YaTeX-typeset-remove-dvi-when-pdf t
yuuji@576 172 "*Non-nil means removing dvi file which has same rootname as PDF output")
yuuji@51 173 (defun YaTeX-typeset-sentinel (proc mes)
yuuji@51 174 (cond ((null (buffer-name (process-buffer proc)))
yuuji@51 175 ;; buffer killed
yuuji@51 176 (set-process-buffer proc nil))
yuuji@51 177 ((memq (process-status proc) '(signal exit))
yuuji@51 178 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
yuuji@51 179 (pwin (get-buffer-window pbuf))
yuuji@212 180 (owin (selected-window)) win
yuuji@261 181 tobecalled shortname
yuuji@212 182 (thiscmd (get 'YaTeX-typeset-process 'thiscmd))
yuuji@212 183 (ppprop (get 'YaTeX-typeset-process 'ppcmd))
yuuji@224 184 (ppcmd (cdr (assq proc ppprop)))
yuuji@224 185 (bcprop (get 'YaTeX-typeset-process 'bibcmd))
yuuji@224 186 (bibcmd (cdr (assq proc bcprop))))
yuuji@212 187 (put 'YaTeX-typeset-process 'ppcmd ;erase ppcmd
yuuji@212 188 (delq (assq proc ppprop) ppprop))
yuuji@224 189 (put 'YaTeX-typeset-process 'bibcmd ;erase bibcmd
yuuji@224 190 (delq (assq proc bcprop) bcprop))
yuuji@51 191 ;; save-excursion isn't the right thing if
yuuji@51 192 ;; process-buffer is current-buffer
yuuji@51 193 (unwind-protect
yuuji@51 194 (progn
yuuji@51 195 ;; Write something in *typesetting* and hack its mode line
yuuji@51 196 (if pwin
yuuji@51 197 (select-window pwin)
yuuji@51 198 (set-buffer pbuf))
yuuji@51 199 ;;(YaTeX-showup-buffer pbuf nil t)
yuuji@51 200 (goto-char (point-max))
yuuji@51 201 (if pwin (recenter -3))
yuuji@60 202 (insert ?\n mode-name " " mes)
yuuji@51 203 (forward-char -1)
yuuji@262 204 (insert
yuuji@262 205 (format " at %s%s\n"
yuuji@262 206 (substring (current-time-string) 0 -5)
yuuji@262 207 (if (and (fboundp 'current-time) (fboundp 'float)
yuuji@262 208 (assq 'time YaTeX-typeset-consumption))
yuuji@262 209 (format
yuuji@262 210 " (%.2f secs)"
yuuji@262 211 (YaTeX-elapsed-time
yuuji@262 212 (cdr (assq 'time YaTeX-typeset-consumption))
yuuji@262 213 (current-time))))))
yuuji@51 214 (setq mode-line-process
yuuji@51 215 (concat ": "
yuuji@51 216 (symbol-name (process-status proc))))
yuuji@256 217 (message "%s %s" mode-name
yuuji@51 218 (if (eq (process-status proc) 'exit)
yuuji@51 219 "done" "ceased"))
yuuji@51 220 ;; If buffer and mode line shows that the process
yuuji@51 221 ;; is dead, we can delete it now. Otherwise it
yuuji@51 222 ;; will stay around until M-x list-processes.
yuuji@51 223 (delete-process proc)
yuuji@224 224 (if (cond
yuuji@263 225 ((or (not YaTeX-typeset-auto-rerun)
yuuji@263 226 (string-match "latexmk" thiscmd))
yuuji@263 227 nil)
yuuji@262 228 ((and bibcmd ;Call bibtex if bibcmd defined &&
yuuji@262 229 (or ; (1st call || warning found)
yuuji@262 230 (and (not (numberp YaTeX-typeset-auto-rerun))
yuuji@262 231 ; cancel call at 1st, if value is a number.
yuuji@262 232 (not (string-match "bibtex" mode-name)))
yuuji@262 233 (re-search-backward
yuuji@262 234 YaTeX-typeset-citation-msg
yuuji@262 235 YaTeX-typeset-marker t))
yuuji@262 236 (save-excursion ; && using .bbl files.
yuuji@224 237 (search-backward
yuuji@224 238 ".bbl" YaTeX-typeset-marker t)))
yuuji@262 239 ;; Always call bibtex after the first typesetting,
yuuji@262 240 ;; because bibtex doesn't warn disappeared \cite.
yuuji@262 241 ;; (Suggested by ryseto. 2012)
yuuji@262 242 ;; It is more efficient to call bibtex directly than
yuuji@262 243 ;; to call it after deep inspection on the balance
yuuji@262 244 ;; of \cite vs. \bib*'s referring all *.aux files.
yuuji@224 245 (insert "\n" YaTeX-typeset-rerun-msg "\n")
yuuji@261 246 (setq tobecalled bibcmd shortname "+bibtex"))
yuuji@261 247 ((or
yuuji@261 248 (save-excursion
yuuji@261 249 (search-backward
yuuji@261 250 YaTeX-typeset-rerun-msg YaTeX-typeset-marker t))
yuuji@261 251 (save-excursion
yuuji@261 252 (re-search-backward
yuuji@261 253 "natbib.*Rerun to get citations correct"
yuuji@261 254 YaTeX-typeset-marker t)))
yuuji@224 255 (if bibcmd
yuuji@224 256 (put 'YaTeX-typeset-process 'bibcmd
yuuji@224 257 (cons (cons (get-buffer-process pbuf) bibcmd)
yuuji@224 258 bcprop)))
yuuji@261 259 (setq tobecalled thiscmd shortname "+typeset"))
yuuji@261 260 (t
yuuji@261 261 nil)) ;no need to call any process
yuuji@354 262 (progn ;;Something occurs to call next command
yuuji@212 263 (insert
yuuji@212 264 (format
yuuji@212 265 "===!!! %s !!!===\n"
yuuji@212 266 (message "Rerun `%s' to get cross-references right"
yuuji@224 267 tobecalled)))
yuuji@224 268 (if (equal tobecalled thiscmd)
yuuji@224 269 (set-marker YaTeX-typeset-marker (point)))
yuuji@212 270 (set-process-sentinel
yuuji@212 271 (start-process
yuuji@261 272 (setq mode-name (concat mode-name shortname))
yuuji@256 273 pbuf
yuuji@224 274 shell-file-name YaTeX-shell-command-option tobecalled)
yuuji@212 275 'YaTeX-typeset-sentinel)
yuuji@212 276 (if ppcmd
yuuji@212 277 (put 'YaTeX-typeset-process 'ppcmd
yuuji@212 278 (cons (cons (get-buffer-process pbuf) ppcmd)
yuuji@224 279 ppprop)))
yuuji@224 280 (if thiscmd
yuuji@224 281 (put 'YaTeX-typeset-process 'thiscmd thiscmd)))
yuuji@212 282 ;; If ppcmd is active, call it.
yuuji@123 283 (cond
yuuji@371 284 ((and ppcmd (symbolp ppcmd) (fboundp ppcmd))
yuuji@371 285 ;; If ppcmd is set and it is a function symbol,
yuuji@371 286 ;; call it whenever command succeeded or not
yuuji@371 287 (funcall ppcmd))
yuuji@123 288 ((and ppcmd (string-match "finish" mes))
yuuji@123 289 (insert (format "=======> Success! Calling %s\n" ppcmd))
yuuji@371 290 (setq mode-name ; set process name
yuuji@371 291 (concat
yuuji@371 292 mode-name "+"
yuuji@371 293 (substring ppcmd 0 (string-match " " ppcmd))))
yuuji@212 294 ; to reach here, 'start-process exists on this emacsen
yuuji@371 295 (set-process-sentinel
yuuji@371 296 (start-process
yuuji@371 297 mode-name
yuuji@371 298 pbuf ; Use this buffer twice.
yuuji@371 299 shell-file-name YaTeX-shell-command-option
yuuji@371 300 ppcmd)
yuuji@371 301 'YaTeX-typeset-sentinel))
yuuji@377 302 (t
yuuji@377 303 (if (equal 0 (process-exit-status proc))
yuuji@408 304 ;; Successful typesetting done
yuuji@408 305 (if (save-excursion
yuuji@408 306 (re-search-backward
yuuji@408 307 (concat
yuuji@576 308 "^Output written on \\(.*\\.pdf\\) (.*page,"
yuuji@576 309 "\\|.* -> \\(.*\\.pdf\\)$")
yuuji@408 310 nil t))
yuuji@576 311 (let*((pdf (or (YaTeX-match-string 1)
yuuji@576 312 (YaTeX-match-string 2)))
yuuji@576 313 (rootname (substring
yuuji@576 314 pdf 0 (rindex pdf ?.)))
yuuji@576 315 (dvi (format "%s.dvi" rootname))
yuuji@576 316 (log (format "%s.log" rootname)))
yuuji@576 317 ;; If PDF output log found in buffer,
yuuji@576 318 ;; set next default previewer to 'pdf viewer
yuuji@576 319 (put 'dvi2-command 'format 'pdf)
yuuji@576 320 (if YaTeX-typeset-remove-dvi-when-pdf
yuuji@576 321 (progn
yuuji@576 322 (delete-file log) ;Ugly, should not do here
yuuji@576 323 (delete-file dvi)))))
yuuji@378 324 ;;Confirm process buffer to be shown when error
yuuji@378 325 (YaTeX-showup-buffer
yuuji@378 326 pbuf 'YaTeX-showup-buffer-bottom-most)
yuuji@378 327 (message "Command FAILED!"))
yuuji@377 328 ;;pull back original mode-name
yuuji@261 329 (setq mode-name "typeset"))))
yuuji@123 330 (forward-char 1))
yuuji@123 331 (setq YaTeX-typeset-process nil)
yuuji@51 332 ;; Force mode line redisplay soon
yuuji@51 333 (set-buffer-modified-p (buffer-modified-p))
yuuji@51 334 )
yuuji@51 335 (select-window owin)
yuuji@69 336 (set-buffer obuf)))))
yuuji@51 337
yuuji@51 338 (defvar YaTeX-texput-file "texput.tex"
yuuji@69 339 "*File name for temporary file of typeset-region.")
yuuji@51 340
yuuji@354 341 (defun YaTeX-typeset-region (&optional pp)
yuuji@52 342 "Paste the region to the file `texput.tex' and execute typesetter.
yuuji@52 343 The region is specified by the rule:
yuuji@51 344 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
yuuji@51 345 (1-1)if the keyword `%#END' is found after `%#BEGIN',
yuuji@51 346 ->Assume the text between `%#BEGIN' and `%#END' as region.
yuuji@51 347 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
yuuji@51 348 ->Assume the text after `%#BEGIN' as region.
yuuji@51 349 (2)If no `%#BEGIN' usage is found before the (point),
yuuji@51 350 ->Assume the text between current (point) and (mark) as region.
yuuji@51 351 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
yuuji@354 352 operation to the region.
yuuji@354 353 Optional second argument PP specifies post-processor command which will be
yuuji@354 354 called with one argument of current file name whitout extension."
yuuji@51 355 (interactive)
yuuji@51 356 (save-excursion
yuuji@51 357 (let*
yuuji@51 358 ((end "") typeout ;Type out message that tells the method of cutting.
yuuji@51 359 (texput YaTeX-texput-file)
yuuji@354 360 (texputroot (substring texput 0 (string-match "\\.tex$" texput)))
yuuji@51 361 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
yuuji@51 362 (buffer (current-buffer)) opoint preamble (subpreamble "") main
yuuji@51 363 (hilit-auto-highlight nil) ;for Emacs19 with hilit19
yuuji@354 364 ppcmd
yuuji@52 365 reg-begin reg-end lineinfo)
yuuji@354 366 (setq ppcmd (if (stringp pp) (concat pp " " texputroot) pp))
yuuji@51 367 (save-excursion
yuuji@517 368 (if (re-search-backward "%#BEGIN\\s *$" nil t)
yuuji@51 369 (progn
yuuji@51 370 (setq typeout "--- Region from BEGIN to "
yuuji@51 371 end "the end of the buffer ---"
yuuji@51 372 reg-begin (match-end 0))
yuuji@51 373 (if (search-forward "%#END" nil t)
yuuji@51 374 (setq reg-end (match-beginning 0)
yuuji@51 375 end "END ---")
yuuji@51 376 (setq reg-end (point-max))))
yuuji@52 377 (setq typeout "=== Region from (point) to (mark) ==="
yuuji@52 378 reg-begin (point) reg-end (mark)))
yuuji@52 379 (goto-char (min reg-begin reg-end))
yuuji@52 380 (setq lineinfo (count-lines (point-min) (point-end-of-line)))
yuuji@51 381 (goto-char (point-min))
yuuji@51 382 (while (search-forward "%#REQUIRE" nil t)
yuuji@51 383 (setq subpreamble
yuuji@51 384 (concat subpreamble
yuuji@51 385 (cond
yuuji@51 386 ((eolp)
yuuji@51 387 (buffer-substring
yuuji@51 388 (match-beginning 0)
yuuji@51 389 (point-beginning-of-line)))
yuuji@51 390 (t (buffer-substring
yuuji@51 391 (match-end 0)
yuuji@51 392 (point-end-of-line))))
yuuji@51 393 "\n"))
yuuji@51 394 (goto-char (match-end 0))))
yuuji@51 395 (YaTeX-visit-main t)
yuuji@51 396 (setq main (current-buffer))
yuuji@51 397 (setq opoint (point))
yuuji@51 398 (goto-char (point-min))
yuuji@51 399 (setq
yuuji@51 400 preamble
yuuji@51 401 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
yuuji@51 402 (buffer-substring (point-min) (match-end 0))
yuuji@73 403 (concat
yuuji@73 404 (if YaTeX-use-LaTeX2e "\\documentclass{" "\\documentstyle{")
yuuji@73 405 YaTeX-default-document-style "}\n"
yuuji@73 406 "\\begin{document}")))
yuuji@51 407 (goto-char opoint)
yuuji@51 408 ;;(set-buffer buffer) ;for clarity
yuuji@133 409 (let ((hilit-auto-highlight nil) (auto-mode-alist nil)
yuuji@133 410 (magic-mode-alist nil)) ;Do not activate yatex-mode here
yuuji@52 411 (set-buffer (find-file-noselect texput)))
yuuji@51 412 ;;(find-file YaTeX-texput-file)
yuuji@51 413 (erase-buffer)
yuuji@354 414 (YaTeX-set-file-coding-system YaTeX-kanji-code YaTeX-coding-system)
yuuji@64 415 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
yuuji@51 416 (insert "\\nonstopmode{}\n"))
yuuji@354 417 (insert preamble "\n" subpreamble "\n"
yuuji@354 418 "\\pagestyle{empty}\n\\thispagestyle{empty}\n")
yuuji@52 419 (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo))
yuuji@51 420 (insert-buffer-substring buffer reg-begin reg-end)
yuuji@51 421 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
yuuji@51 422 (insert "\n\\end{document}\n")
yuuji@51 423 (basic-save-buffer)
yuuji@51 424 (kill-buffer (current-buffer))
yuuji@51 425 (set-buffer main) ;return to parent file or itself.
yuuji@354 426 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
yuuji@51 427 (switch-to-buffer buffer) ;for Emacs-19
yuuji@52 428 (put 'dvi2-command 'region t)
yuuji@52 429 (put 'dvi2-command 'file buffer)
yuuji@69 430 (put 'dvi2-command 'offset lineinfo))))
yuuji@51 431
yuuji@381 432 (defvar YaTeX-use-image-preview "png"
yuuji@376 433 "*Nil means not using image preview by [prefix] t e.
yuuji@376 434 Acceptable value is one of \"jpg\" or \"png\", which specifies
yuuji@381 435 format of preview image. NOTE that if your system has /usr/bin/sips
yuuji@391 436 while not having convert(ImageMagick), jpeg format is automatically taken
yuuji@381 437 for conversion.")
yuuji@355 438 (defvar YaTeX-preview-image-mode-map nil
yuuji@355 439 "Keymap used in YaTeX-preview-image-mode")
yuuji@355 440 (defun YaTeX-preview-image-mode ()
yuuji@355 441 (interactive)
yuuji@355 442 (if YaTeX-preview-image-mode-map
yuuji@355 443 nil
yuuji@355 444 (let ((map (setq YaTeX-preview-image-mode-map (make-sparse-keymap))))
yuuji@355 445 (define-key map "q" (lambda()(interactive)
yuuji@355 446 (kill-buffer)
yuuji@355 447 (select-window
yuuji@355 448 (or (get 'YaTeX-typeset-process 'win)
yuuji@355 449 (selected-window)))))
yuuji@355 450 (define-key map "j" (lambda()(interactive) (scroll-up 1)))
yuuji@355 451 (define-key map "k" (lambda()(interactive) (scroll-up -1)))))
yuuji@355 452 (use-local-map YaTeX-preview-image-mode-map))
yuuji@355 453
yuuji@371 454 (defvar YaTeX-typeset-pdf2image-chain
yuuji@371 455 (cond
yuuji@371 456 ((YaTeX-executable-find "pdfcrop") ;Mac OS X
yuuji@371 457 (list
yuuji@371 458 "pdfcrop --clip %b.pdf tmp.pdf"
yuuji@371 459 (if (YaTeX-executable-find "convert")
yuuji@526 460 "convert -alpha off -density %d tmp.pdf %b.%f"
yuuji@381 461 ;; If we use sips, specify jpeg as format
yuuji@381 462 "sips -s format jpeg -s dpiWidth %d -s dpiHeight %d %b.pdf --out %b.jpg")
yuuji@513 463 "rm -f tmp.pdf"))
yuuji@513 464 ((YaTeX-executable-find "convert")
yuuji@526 465 (list "convert -trim -alpha off -density %d %b.pdf %b.%f"))
yuuji@513 466 )
yuuji@371 467 "*Pipe line of command as a list to create image file from PDF.
yuuji@371 468 See also doc-string of YaTeX-typeset-dvi2image-chain.")
yuuji@371 469
yuuji@371 470 (defvar YaTeX-typeset-dvi2image-chain
yuuji@371 471 (cond
yuuji@371 472 ((YaTeX-executable-find YaTeX-cmd-dvips)
yuuji@371 473 (list
yuuji@371 474 (format "%s -E -o %%b.eps %%b.dvi" YaTeX-cmd-dvips)
yuuji@381 475 "convert -alpha off -density %d %b.eps %b.%f"))
yuuji@526 476 ((YaTeX-executable-find YaTeX-dvipdf-command)
yuuji@526 477 (list
yuuji@526 478 (format "%s %%b.dvi" YaTeX-dvipdf-command)
yuuji@526 479 "convert -trim -alpha off -density %d %b.pdf %b.%f"))
yuuji@381 480 ((and (equal YaTeX-use-image-preview "png")
yuuji@381 481 (YaTeX-executable-find "dvipng"))
yuuji@381 482 (list "dvipng %b.dvi")))
yuuji@371 483 "*Pipe line of command as a list to create png file from DVI or PDF.
yuuji@371 484 %-notation rewritten list:
yuuji@371 485 %b basename of target file(\"texput\")
yuuji@371 486 %f Output format(\"png\")
yuuji@371 487 %d DPI
yuuji@371 488 ")
yuuji@371 489
yuuji@371 490 (defvar YaTeX-typeset-conv2image-chain-alist
yuuji@371 491 (list (cons 'pdf YaTeX-typeset-pdf2image-chain)
yuuji@371 492 (cons 'dvi YaTeX-typeset-dvi2image-chain))
yuuji@371 493 "Default alist for creating image files from DVI/PDF.
yuuji@371 494 The value is generated from YaTeX-typeset-pdf2image-chain and
yuuji@371 495 YaTeX-typeset-dvi2image-chain.")
yuuji@371 496
yuuji@517 497 (defun YaTeX-popup-image (imagesrc buffer &optional func)
yuuji@517 498 (let ((sw (selected-window)) image
yuuji@517 499 (data-p (and (> (length imagesrc) 128)
yuuji@517 500 (not (file-readable-p imagesrc)))))
yuuji@517 501 (save-excursion
yuuji@517 502 (cond
yuuji@517 503 ((featurep 'image) window-system
yuuji@517 504 (YaTeX-showup-buffer ;showup and select
yuuji@517 505 (get-buffer-create buffer)
yuuji@517 506 (or func 'YaTeX-showup-buffer-bottom-most)
yuuji@517 507 t)
yuuji@517 508 (remove-images (point-min) (point-max))
yuuji@517 509 (erase-buffer)
yuuji@520 510 (if data-p
yuuji@520 511 (insert-image
yuuji@520 512 (setq image (create-image imagesrc nil data-p)))
yuuji@520 513 ;; create-image does not re-create img-object for the same file
yuuji@520 514 (insert-image-file (expand-file-name imagesrc))
yuuji@520 515 (setq image (plist-get (text-properties-at (point)) 'intangible)))
yuuji@517 516 (YaTeX-preview-image-mode)
yuuji@517 517 (let ((height (1+ (cdr (image-size image)))))
yuuji@517 518 (enlarge-window
yuuji@517 519 (- (ceiling (min height (/ (frame-height) 2)))
yuuji@517 520 (window-height))))
yuuji@517 521 (select-window sw))
yuuji@517 522 (t ;; Without direct image, display image with image viewer
yuuji@517 523 (YaTeX-system
yuuji@517 524 (format "%s %s" YaTeX-cmd-view-images target)
yuuji@517 525 "YaTeX-region-image"
yuuji@517 526 'noask)))
yuuji@517 527 )))
yuuji@517 528
yuuji@354 529 (defvar YaTeX-typeset-conv2image-process nil "Process of conv2image chain")
yuuji@354 530 (defun YaTeX-typeset-conv2image-chain ()
yuuji@354 531 (let*((proc (or YaTeX-typeset-process YaTeX-typeset-conv2image-process))
yuuji@354 532 (prevname (process-name proc))
yuuji@376 533 (texput "texput")
yuuji@376 534 (format YaTeX-use-image-preview)
yuuji@374 535 (target (concat texput "." format))
yuuji@354 536 (math (get 'YaTeX-typeset-conv2image-chain 'math))
yuuji@391 537 (dpi (get 'YaTeX-typeset-conv2image-chain 'dpi))
yuuji@371 538 (srctype (or (get 'YaTeX-typeset-conv2image-chain 'srctype)
yuuji@374 539 (if (file-newer-than-file-p
yuuji@374 540 (concat texput ".pdf")
yuuji@374 541 (concat texput ".dvi"))
yuuji@371 542 'pdf 'dvi)))
yuuji@371 543 (chain (cdr (assq srctype YaTeX-typeset-conv2image-chain-alist)))
yuuji@371 544 ;; This function is the first evaluation code.
yuuji@371 545 ;; If you find these command line does not work
yuuji@371 546 ;; on your system, please tell the author
yuuji@371 547 ;; which commands should be taken to achieve
yuuji@371 548 ;; one-shot png previewing on your system
yuuji@371 549 ;; before publishing patch on the Web.
yuuji@371 550 ;; Please please please please please.
yuuji@354 551 (curproc (member prevname chain))
yuuji@355 552 (w (get 'YaTeX-typeset-conv2image-chain 'win))
yuuji@355 553 (pwd default-directory)
yuuji@354 554 img)
yuuji@354 555 (if (not (= (process-exit-status proc) 0))
yuuji@354 556 (progn
yuuji@362 557 (YaTeX-showup-buffer ;never comes here(?)
yuuji@354 558 (current-buffer) 'YaTeX-showup-buffer-bottom-most)
yuuji@354 559 (message "Region typesetting FAILED"))
yuuji@354 560 (setq command
yuuji@354 561 (if curproc (car (cdr-safe curproc)) (car chain)))
yuuji@354 562 (if command
yuuji@371 563 (let ((cmdline (YaTeX-replace-formats
yuuji@371 564 command
yuuji@371 565 (list (cons "b" "texput")
yuuji@374 566 (cons "f" format)
yuuji@391 567 (cons "d" dpi)))))
yuuji@371 568 (insert (format "Calling `%s'...\n" cmdline))
yuuji@354 569 (set-process-sentinel
yuuji@354 570 (setq YaTeX-typeset-conv2image-process
yuuji@354 571 (start-process
yuuji@354 572 command
yuuji@354 573 (current-buffer)
yuuji@371 574 shell-file-name YaTeX-shell-command-option
yuuji@371 575 cmdline))
yuuji@354 576 'YaTeX-typeset-sentinel)
yuuji@354 577 (put 'YaTeX-typeset-process 'ppcmd
yuuji@354 578 (cons (cons (get-buffer-process (current-buffer))
yuuji@354 579 'YaTeX-typeset-conv2image-chain)
yuuji@354 580 (get 'YaTeX-typeset-process 'ppcmd))))
yuuji@354 581 ;; After all chain executed, display image in current window
yuuji@517 582 (YaTeX-popup-image target " *YaTeX-popup-image*")
yuuji@517 583 (put 'YaTeX-typeset-conv2image-chain 'elapse
yuuji@517 584 (YaTeX-elapsed-time
yuuji@517 585 (get 'YaTeX-typeset-conv2image-chain 'start) (current-time)))
yuuji@517 586 ))))
yuuji@354 587
yuuji@375 588
yuuji@375 589 (defvar YaTeX-typeset-environment-timer nil)
yuuji@375 590 (defun YaTeX-typeset-environment-timer ()
yuuji@375 591 "Update preview image in the
yuuji@375 592 Plist: '(buf begPoint endPoint precedingChar 2precedingChar Substring time)"
yuuji@375 593 (let*((plist (get 'YaTeX-typeset-environment-timer 'laststate))
yuuji@375 594 (b (nth 0 plist))
yuuji@375 595 (s (nth 1 plist))
yuuji@375 596 (e (nth 2 plist))
yuuji@375 597 (p (nth 3 plist))
yuuji@375 598 (q (nth 4 plist))
yuuji@375 599 (st (nth 5 plist))
yuuji@375 600 (tm (nth 6 plist))
yuuji@377 601 (overlay YaTeX-on-the-fly-overlay)
yuuji@375 602 (thresh (* 2 (or (get 'YaTeX-typeset-conv2image-chain 'elapse) 1))))
yuuji@375 603 (cond
yuuji@375 604 ;; In minibuffer, do nothing
yuuji@375 605 ((minibuffer-window-active-p (selected-window)) nil)
yuuji@375 606 ;; If point went out from last environment, cancel timer
yuuji@375 607 ;;;((and (message "s=%d, e=%d, p=%d" s e (point)) nil))
yuuji@403 608 ((null (buffer-file-name)) nil) ;;At momentary-string-display, it's nil.
yuuji@375 609 ((or (not (eq b (window-buffer (selected-window))))
yuuji@375 610 (< (point) s)
yuuji@377 611 (not (overlayp overlay))
yuuji@377 612 (not (eq (overlay-buffer overlay) (current-buffer)))
yuuji@377 613 (> (point) (overlay-end overlay)))
yuuji@375 614 (YaTeX-typeset-environment-cancel-auto))
yuuji@375 615 ;;;((and (message "e=%d, p=%d t=%s" e (point) (current-time)) nil))
yuuji@375 616 ;; If recently called, hold
yuuji@377 617 ;;; ((< (YaTeX-elapsed-time tm (current-time)) thresh) nil)
yuuji@375 618 ;; If condition changed from last call, do it
yuuji@377 619 ((not (string= st (YaTeX-buffer-substring s (overlay-end overlay))))
yuuji@375 620 (YaTeX-typeset-environment)))))
yuuji@375 621
yuuji@391 622
yuuji@391 623 (defun YaTeX-typeset-environment-by-lmp ()
yuuji@391 624 (save-excursion
yuuji@391 625 (let ((sw (selected-window)))
yuuji@391 626 (goto-char opoint)
yuuji@391 627 (latex-math-preview-expression)
yuuji@391 628 (select-window sw))))
yuuji@391 629
yuuji@391 630 (defun YaTeX-typeset-environment-by-builtin ()
yuuji@391 631 (save-excursion
yuuji@391 632 (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain)))
yuuji@391 633
yuuji@391 634 (defvar YaTeX-on-the-fly-math-preview-engine
yuuji@391 635 (if (fboundp 'latex-math-preview-expression)
yuuji@391 636 'YaTeX-typeset-environment-by-lmp
yuuji@391 637 'YaTeX-typeset-environment-by-builtin)
yuuji@391 638 "Function symbol to use math-preview.
yuuji@391 639 'YaTeX-typeset-environment-by-lmp for using latex-math-preview,
yuuji@391 640 'YaTeX-typeset-environment-by-builtin for using yatex-builtin.")
yuuji@391 641
yuuji@380 642 (defun YaTeX-typeset-environment-1 ()
yuuji@431 643 (require 'yatex23)
yuuji@391 644 (let*((math (YaTeX-in-math-mode-p))
yuuji@406 645 (dpi (or (YaTeX-get-builtin "IMAGEDPI") (if math "300" "200")))
yuuji@403 646 (opoint (point))
yuuji@403 647 usetimer)
yuuji@380 648 (cond
yuuji@380 649 ((and YaTeX-on-the-fly-overlay (overlayp YaTeX-on-the-fly-overlay)
yuuji@380 650 (member YaTeX-on-the-fly-overlay (overlays-at (point))))
yuuji@380 651 ;; If current position is in on-the-fly overlay,
yuuji@380 652 ;; use that region again
yuuji@380 653 (setq math (get 'YaTeX-typeset-conv2image-chain 'math))
yuuji@380 654 (push-mark (overlay-start YaTeX-on-the-fly-overlay))
yuuji@380 655 (goto-char (overlay-end YaTeX-on-the-fly-overlay)))
yuuji@391 656 ((YaTeX-region-active-p)
yuuji@380 657 nil) ;if region is active, use it
yuuji@519 658 (math (setq usetimer YaTeX-on-the-fly-preview-interval)
yuuji@519 659 (YaTeX-mark-environment))
yuuji@380 660 ((equal (or (YaTeX-inner-environment t) "document") "document")
yuuji@380 661 (mark-paragraph))
yuuji@519 662 (t (setq usetimer YaTeX-on-the-fly-preview-interval)
yuuji@519 663 (YaTeX-mark-environment)))
yuuji@380 664 (if YaTeX-use-image-preview
yuuji@380 665 (let ((YaTeX-typeset-buffer (concat "*bg:" YaTeX-typeset-buffer))
yuuji@380 666 (b (region-beginning)) (e (region-end)))
yuuji@380 667 (put 'YaTeX-typeset-conv2image-chain 'math math)
yuuji@391 668 (put 'YaTeX-typeset-conv2image-chain 'dpi dpi)
yuuji@380 669 (put 'YaTeX-typeset-conv2image-chain 'srctype nil)
yuuji@380 670 (put 'YaTeX-typeset-conv2image-chain 'win (selected-window))
yuuji@380 671 (put 'YaTeX-typeset-conv2image-chain 'start (current-time))
yuuji@380 672 (put 'YaTeX-typeset-environment-timer 'laststate
yuuji@380 673 (list (current-buffer) b e (preceding-char)
yuuji@380 674 (char-after (- (point) 2))
yuuji@380 675 (YaTeX-buffer-substring b e)
yuuji@380 676 (current-time)))
yuuji@391 677 (if math (funcall YaTeX-on-the-fly-math-preview-engine)
yuuji@391 678 (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain))
yuuji@403 679 (if usetimer (YaTeX-typeset-environment-auto b e)))
yuuji@380 680 (YaTeX-typeset-region))))
yuuji@380 681
yuuji@517 682 (defun YaTeX-filter-BEGEND ()
yuuji@517 683 (let ((begend-info (YaTeX-in-BEGEND-p)))
yuuji@517 684 (if begend-info
yuuji@517 685 (progn
yuuji@517 686 (require 'yatexflt)
yuuji@517 687 (YaTeX-filter-pass-to-filter begend-info)
yuuji@517 688 ))))
yuuji@517 689
yuuji@130 690 (defun YaTeX-typeset-environment ()
yuuji@371 691 "Typeset current environment or paragraph.
yuuji@371 692 If region activated, use it."
yuuji@130 693 (interactive)
yuuji@380 694 (let ((md (match-data)))
yuuji@380 695 (unwind-protect
yuuji@380 696 (save-excursion
yuuji@517 697 (or (YaTeX-filter-BEGEND)
yuuji@517 698 (YaTeX-typeset-environment-1)))
yuuji@380 699 (store-match-data md))))
yuuji@380 700
yuuji@364 701
yuuji@377 702 (defvar YaTeX-on-the-fly-preview-interval (string-to-number "0.9")
yuuji@375 703 "*Control the on-the-fly update of preview environment by an image.
yuuji@375 704 Nil disables on-the-fly update. Otherwise on-the-fly update is enabled
yuuji@375 705 with update interval specified by this value.")
yuuji@375 706
yuuji@403 707 (defun YaTeX-typeset-environment-auto (beg end)
yuuji@375 708 "Turn on on-the-fly preview-image"
yuuji@375 709 (if YaTeX-typeset-environment-timer
yuuji@403 710 (cancel-timer YaTeX-typeset-environment-timer))
yuuji@403 711 (if YaTeX-on-the-fly-overlay
yuuji@403 712 (move-overlay YaTeX-on-the-fly-overlay beg end)
yuuji@403 713 (overlay-put
yuuji@403 714 (setq YaTeX-on-the-fly-overlay (make-overlay beg end))
yuuji@403 715 'face 'YaTeX-on-the-fly-activated-face))
yuuji@403 716 (setq YaTeX-typeset-environment-timer
yuuji@403 717 (run-with-idle-timer
yuuji@403 718 (max (string-to-number "0.1")
yuuji@403 719 (cond
yuuji@403 720 ((numberp YaTeX-on-the-fly-preview-interval)
yuuji@403 721 YaTeX-on-the-fly-preview-interval)
yuuji@403 722 ((stringp YaTeX-on-the-fly-preview-interval)
yuuji@403 723 (string-to-number YaTeX-on-the-fly-preview-interval))
yuuji@403 724 (t 1)))
yuuji@403 725 t 'YaTeX-typeset-environment-timer)))
yuuji@403 726
yuuji@403 727 (defun YaTeX-typeset-environment-activate-onthefly ()
yuuji@403 728 (if (get-text-property (point) 'onthefly)
yuuji@403 729 (save-excursion
yuuji@403 730 (if YaTeX-typeset-environment-timer
yuuji@403 731 (progn
yuuji@403 732 (cancel-timer YaTeX-typeset-environment-timer)
yuuji@403 733 (setq YaTeX-typeset-environment-timer nil)))
yuuji@403 734 (if (YaTeX-on-begin-end-p)
yuuji@405 735 (if (or (match-beginning 1) (match-beginning 3)) ;on beginning
yuuji@403 736 (goto-char (match-end 0))
yuuji@403 737 (goto-char (match-beginning 0))))
yuuji@403 738 (YaTeX-typeset-environment))))
yuuji@375 739
yuuji@406 740 (defun YaTeX-on-the-fly-cancel ()
yuuji@406 741 "Reset on-the-fly stickiness"
yuuji@406 742 (interactive)
yuuji@406 743 (YaTeX-typeset-environment-cancel-auto 'stripoff)
yuuji@406 744 t) ;t for kill-*
yuuji@406 745
yuuji@406 746 (defun YaTeX-typeset-environment-cancel-auto (&optional stripoff)
yuuji@375 747 "Cancel typeset-environment timer."
yuuji@375 748 (interactive)
yuuji@403 749 (if YaTeX-typeset-environment-timer
yuuji@403 750 (cancel-timer YaTeX-typeset-environment-timer))
yuuji@403 751 (setq YaTeX-typeset-environment-timer
yuuji@403 752 (run-with-idle-timer
yuuji@403 753 (string-to-number "0.1")
yuuji@403 754 t
yuuji@403 755 'YaTeX-typeset-environment-activate-onthefly))
yuuji@406 756 (let ((ov YaTeX-on-the-fly-overlay))
yuuji@406 757 (if stripoff
yuuji@406 758 (remove-text-properties (overlay-start ov)
yuuji@406 759 (1- (overlay-end ov))
yuuji@406 760 '(onthefly))
yuuji@406 761 (put-text-property (overlay-start YaTeX-on-the-fly-overlay)
yuuji@406 762 (1- (overlay-end YaTeX-on-the-fly-overlay))
yuuji@406 763 'onthefly
yuuji@406 764 t))
yuuji@406 765 (delete-overlay ov)
yuuji@406 766 (message "On-the-fly preview deactivated")))
yuuji@130 767
yuuji@123 768 (defun YaTeX-typeset-buffer (&optional pp)
yuuji@60 769 "Typeset whole buffer.
yuuji@60 770 If %#! usage says other buffer is main text,
yuuji@51 771 visit main buffer to confirm if its includeonly list contains current
yuuji@51 772 buffer's file. And if it doesn't contain editing text, ask user which
yuuji@123 773 action wants to be done, A:Add list, R:Replace list, %:comment-out list.
yuuji@123 774 If optional argument PP given as string, PP is considered as post-process
yuuji@123 775 command and call it with the same command argument as typesetter without
yuuji@123 776 last extension.
yuuji@123 777 eg. if PP is \"dvipdfmx\", called commands as follows.
yuuji@123 778 platex foo.tex
yuuji@123 779 dvipdfmx foo
yuuji@123 780 PP command will be called iff typeset command exit successfully"
yuuji@51 781 (interactive)
yuuji@51 782 (YaTeX-save-buffers)
yuuji@51 783 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
yuuji@51 784 (mydir (file-name-directory (buffer-file-name)))
yuuji@224 785 (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd
yuuji@123 786 (cb (current-buffer)))
yuuji@224 787 (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt
yuuji@224 788 pparg (substring pparg (rindex pparg ? )) ;get last arg
yuuji@224 789 pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext
yuuji@506 790 bibcmd (YaTeX-replace-format
yuuji@506 791 (or (YaTeX-get-builtin "BIBTEX") bibtex-command)
yuuji@506 792 "k" (YaTeX-kanji-ptex-mnemonic)))
yuuji@506 793 (or (string-match "\\s [^-]" bibcmd) ;if bibcmd has no argument,
yuuji@262 794 (setq bibcmd (concat bibcmd pparg))) ;append argument(== %#!)
yuuji@123 795 (and pp
yuuji@123 796 (stringp pp)
yuuji@224 797 (setq ppcmd (concat pp pparg)))
yuuji@51 798 (if (YaTeX-main-file-p) nil
yuuji@51 799 (save-excursion
yuuji@51 800 (YaTeX-visit-main t) ;search into main buffer
yuuji@51 801 (save-excursion
yuuji@51 802 (push-mark (point) t)
yuuji@51 803 (goto-char (point-min))
yuuji@51 804 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
yuuji@51 805 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
yuuji@51 806 (let*
yuuji@51 807 ((b (progn (skip-chars-forward "^{") (point)))
yuuji@51 808 (e (progn (skip-chars-forward "^}") (1+ (point))))
yuuji@51 809 (s (buffer-substring b e)) c
yuuji@51 810 (pardir (file-name-directory (buffer-file-name))))
yuuji@51 811 (if (string-match (concat "[{,/]" me "[,}]") s)
yuuji@51 812 nil ; Nothing to do when it's already in includeonly.
yuuji@51 813 (ding)
yuuji@530 814 (set-window-buffer nil (current-buffer));Display this buffer.
yuuji@51 815 (setq
yuuji@51 816 me ;;Rewrite my name(me) to contain sub directory name.
yuuji@51 817 (concat
yuuji@51 818 (if (string-match pardir mydir) ;if mydir is child of main
yuuji@51 819 (substring mydir (length pardir)) ;cut absolute path
yuuji@51 820 mydir) ;else concat absolute path name.
yuuji@51 821 me))
yuuji@51 822 (message
yuuji@51 823 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
yuuji@51 824 me)
yuuji@51 825 (setq c (read-char))
yuuji@51 826 (cond
yuuji@51 827 ((= c ?a)
yuuji@51 828 (goto-char (1+ b))
yuuji@51 829 (insert me (if (string= s "{}") "" ",")))
yuuji@51 830 ((= c ?r)
yuuji@51 831 (delete-region (1+ b) (1- e)) (insert me))
yuuji@51 832 ((= c ?%)
yuuji@51 833 (beginning-of-line) (insert "%"))
yuuji@51 834 (t nil))
yuuji@51 835 (basic-save-buffer))))
yuuji@51 836 (exchange-point-and-mark)))
yuuji@531 837 (set-window-buffer nil cb)) ;for 19 and 26
yuuji@123 838 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
yuuji@69 839 (put 'dvi2-command 'region nil)))
yuuji@51 840
yuuji@51 841 (defvar YaTeX-call-command-history nil
yuuji@51 842 "Holds history list of YaTeX-call-command-on-file.")
yuuji@51 843 (put 'YaTeX-call-command-history 'no-default t)
yuuji@86 844 (defun YaTeX-call-command-on-file (base-cmd buffer &optional file)
yuuji@171 845 "Call external command BASE-CMD in the BUFFER.
yuuji@86 846 By default, pass the basename of current file. Optional 3rd argument
yuuji@86 847 FILE changes the default file name."
yuuji@51 848 (YaTeX-save-buffers)
yuuji@171 849 (let ((default (concat base-cmd " "
yuuji@171 850 (let ((me (file-name-nondirectory
yuuji@171 851 (or file buffer-file-name))))
yuuji@171 852 (if (string-match "\\.tex" me)
yuuji@171 853 (substring me 0 (match-beginning 0))
yuuji@171 854 me)))))
yuuji@171 855 (or YaTeX-call-command-history
yuuji@171 856 (setq YaTeX-call-command-history (list default)))
yuuji@171 857 (YaTeX-typeset
yuuji@171 858 (read-string-with-history
yuuji@171 859 "Call command: "
yuuji@171 860 (car YaTeX-call-command-history)
yuuji@171 861 'YaTeX-call-command-history)
yuuji@171 862 buffer)))
yuuji@51 863
yuuji@227 864 (defvar YaTeX-call-builtin-on-file)
yuuji@227 865 (make-variable-buffer-local 'YaTeX-call-builtin-on-file)
yuuji@227 866 (defun YaTeX-call-builtin-on-file (builtin-type &optional default update)
yuuji@224 867 "Call command on file specified by BUILTIN-TYPE."
yuuji@51 868 (YaTeX-save-buffers)
yuuji@224 869 (let*((main (or YaTeX-parent-file
yuuji@227 870 (save-excursion (YaTeX-visit-main t) buffer-file-name)))
yuuji@224 871 (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.))))
yuuji@227 872 (alist YaTeX-call-builtin-on-file)
yuuji@511 873 (b-in (or (YaTeX-get-builtin builtin-type)
yuuji@511 874 (cdr (assoc builtin-type alist))))
yuuji@227 875 (command b-in))
yuuji@227 876 (if (or update (null b-in))
yuuji@227 877 (progn
yuuji@227 878 (setq command (read-string-with-history
yuuji@227 879 (format "%s command: " builtin-type)
yuuji@227 880 (or b-in
yuuji@227 881 (format "%s %s" default mainroot))
yuuji@227 882 'YaTeX-call-command-history))
yuuji@227 883 (if (or update (null b-in))
yuuji@362 884 (if (y-or-n-p "Memorize this command line in this file? ")
yuuji@227 885 (YaTeX-getset-builtin builtin-type command) ;keep in a file
yuuji@227 886 (setq YaTeX-call-builtin-on-file ;keep in memory
yuuji@227 887 (cons (cons builtin-type command)
yuuji@362 888 (delete (assoc builtin-type alist) alist)))
yuuji@362 889 (message "`%s' kept in memory. Type `%s %s' to override."
yuuji@362 890 command
yuuji@362 891 (key-description
yuuji@362 892 (car (where-is-internal 'universal-argument)))
yuuji@362 893 (key-description (this-command-keys)))
yuuji@362 894 (sit-for 2)))))
yuuji@224 895 (YaTeX-typeset
yuuji@511 896 (YaTeX-replace-format command "k" (YaTeX-kanji-ptex-mnemonic))
yuuji@362 897 (format " *YaTeX-%s*" (downcase builtin-type))
yuuji@362 898 builtin-type builtin-type)))
yuuji@51 899
yuuji@51 900 (defun YaTeX-kill-typeset-process (proc)
yuuji@51 901 "Kill process PROC after sending signal to PROC.
yuuji@51 902 PROC should be process identifier."
yuuji@51 903 (cond
yuuji@68 904 ((not (fboundp 'start-process))
yuuji@68 905 (error "This system can't have concurrent process."))
yuuji@51 906 ((or (null proc) (not (eq (process-status proc) 'run)))
yuuji@51 907 (message "Typesetting process is not running."))
yuuji@51 908 (t
yuuji@51 909 (save-excursion
yuuji@51 910 (set-buffer (process-buffer proc))
yuuji@51 911 (save-excursion
yuuji@51 912 (goto-char (point-max))
yuuji@51 913 (beginning-of-line)
yuuji@51 914 (if (looking-at "\\? +$")
yuuji@51 915 (let ((mp (point-max)))
yuuji@51 916 (process-send-string proc "x\n")
yuuji@51 917 (while (= mp (point-max)) (sit-for 1))))))
yuuji@51 918 (if (eq (process-status proc) 'run)
yuuji@51 919 (progn
yuuji@51 920 (interrupt-process proc)
yuuji@69 921 (delete-process proc))))))
yuuji@51 922
yuuji@351 923 (defun YaTeX-system (command name &optional noask basedir)
yuuji@351 924 "Execute some COMMAND with process name `NAME'. Not a official function.
yuuji@514 925 Optional third argument NOASK skip query when privious process running.
yuuji@514 926 Optional fourth argument BASEDIR changes default-directory there."
yuuji@51 927 (save-excursion
yuuji@351 928 (let ((df default-directory)
yuuji@351 929 (buffer (get-buffer-create (format " *%s*" name)))
yuuji@351 930 proc status)
yuuji@351 931 (set-buffer buffer)
yuuji@351 932 (setq default-directory (cd (or basedir df)))
yuuji@351 933 (erase-buffer)
yuuji@362 934 (insert (format "Calling `%s'...\n" command)
yuuji@514 935 "==Kill this buffer to STOP process==\n")
yuuji@354 936 (YaTeX-showup-buffer buffer 'YaTeX-showup-buffer-bottom-most)
yuuji@351 937 (if (not (fboundp 'start-process))
yuuji@351 938 (call-process
yuuji@351 939 shell-file-name nil buffer nil YaTeX-shell-command-option command)
yuuji@351 940 (if (and (setq proc (get-buffer-process buffer))
yuuji@351 941 (setq status (process-status proc))
yuuji@351 942 (eq status 'run)
yuuji@351 943 (not noask)
yuuji@351 944 (not
yuuji@351 945 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
yuuji@351 946 nil
yuuji@351 947 (if (eq status 'run)
yuuji@351 948 (progn (interrupt-process proc) (delete-process proc)))
yuuji@351 949 (set-process-buffer
yuuji@351 950 (start-process
yuuji@351 951 name buffer shell-file-name YaTeX-shell-command-option command)
yuuji@351 952 (get-buffer buffer)))))))
yuuji@51 953
yuuji@72 954 (defvar YaTeX-default-paper-type "a4"
yuuji@72 955 "*Default paper type.")
yuuji@72 956 (defconst YaTeX-paper-type-alist
yuuji@72 957 '(("a4paper" . "a4")
yuuji@72 958 ("a5paper" . "a5")
yuuji@72 959 ("b4paper" . "b4")
yuuji@72 960 ("b5paper" . "b5"))
yuuji@72 961 "Holds map of options and paper types.")
yuuji@72 962 (defconst YaTeX-dvips-paper-option-alist
yuuji@72 963 '(("a4" . "-t a4")
yuuji@72 964 ("a5" . "-t a5")
yuuji@72 965 ("b4" . "-t b4")
yuuji@72 966 ("b5" . "-t b5")
yuuji@72 967 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
yuuji@72 968 ("a5r" . "-t landscape")
yuuji@72 969 ("b4r" . "-t landscape")
yuuji@72 970 ("b5r" . "-t landscape"))
yuuji@72 971 "Holds map of dvips options and paper types.")
yuuji@72 972 (defun YaTeX-get-paper-type ()
yuuji@72 973 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
yuuji@72 974 (save-excursion
yuuji@77 975 (YaTeX-visit-main t)
yuuji@72 976 (goto-char (point-min))
yuuji@77 977 (let ((opts
yuuji@77 978 (if (re-search-forward
yuuji@77 979 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
yuuji@77 980 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
yuuji@77 981 (concat
yuuji@77 982 (catch 'found-paper
yuuji@77 983 (mapcar (lambda (pair)
yuuji@79 984 (if (YaTeX-member (car pair) opts)
yuuji@77 985 (throw 'found-paper (cdr pair))))
yuuji@77 986 YaTeX-paper-type-alist)
yuuji@77 987 YaTeX-default-paper-type)
yuuji@79 988 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
yuuji@72 989
yuuji@51 990 (defvar YaTeX-preview-command-history nil
yuuji@51 991 "Holds minibuffer history of preview command.")
yuuji@51 992 (put 'YaTeX-preview-command-history 'no-default t)
yuuji@51 993 (defvar YaTeX-preview-file-history nil
yuuji@51 994 "Holds minibuffer history of file to preview.")
yuuji@51 995 (put 'YaTeX-preview-file-history 'no-default t)
yuuji@328 996 (defun YaTeX-preview-default-previewer ()
yuuji@328 997 "Return default previewer for this document"
yuuji@328 998 (YaTeX-replace-format
yuuji@410 999 (if (eq (get 'dvi2-command 'format) 'pdf)
yuuji@410 1000 (or (YaTeX-get-builtin "PDFVIEW")
yuuji@410 1001 tex-pdfview-command)
yuuji@410 1002 (or (YaTeX-get-builtin "PREVIEW")
yuuji@410 1003 dvi2-command))
yuuji@410 1004 "p" (format (cond
yuuji@410 1005 (YaTeX-dos "-y:%s")
yuuji@410 1006 (t "-paper %s"))
yuuji@410 1007 (YaTeX-get-paper-type))))
yuuji@328 1008 (defun YaTeX-preview-default-main (command)
yuuji@328 1009 "Return default preview target file"
yuuji@328 1010 (if (get 'dvi2-command 'region)
yuuji@86 1011 (substring YaTeX-texput-file
yuuji@86 1012 0 (rindex YaTeX-texput-file ?.))
yuuji@328 1013 (YaTeX-get-preview-file-name command)))
yuuji@328 1014 (defun YaTeX-preview (preview-command preview-file &optional as-default)
yuuji@328 1015 "Execute xdvi (or other) to tex-preview."
yuuji@328 1016 (interactive
yuuji@328 1017 (let* ((previewer (YaTeX-preview-default-previewer))
yuuji@328 1018 (as-default current-prefix-arg)
yuuji@328 1019 (command (if as-default
yuuji@328 1020 previewer
yuuji@328 1021 (read-string-with-history
yuuji@328 1022 "Preview command: "
yuuji@328 1023 previewer
yuuji@328 1024 'YaTeX-preview-command-history)))
yuuji@328 1025 (target (YaTeX-preview-default-main command))
yuuji@328 1026 (file (if as-default
yuuji@328 1027 target
yuuji@328 1028 (read-string-with-history
yuuji@328 1029 "Preview file: "
yuuji@328 1030 target
yuuji@328 1031 'YaTeX-preview-file-history))))
yuuji@86 1032 (list command file)))
yuuji@328 1033 (setq dvi2-command preview-command) ;`dvi2-command' is buffer local
yuuji@51 1034 (save-excursion
yuuji@51 1035 (YaTeX-visit-main t)
yuuji@86 1036 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
yuuji@51 1037 (let ((pbuffer "*dvi-preview*") (dir default-directory))
yuuji@51 1038 (YaTeX-showup-buffer
yuuji@354 1039 pbuffer 'YaTeX-showup-buffer-bottom-most)
yuuji@51 1040 (set-buffer (get-buffer-create pbuffer))
yuuji@51 1041 (erase-buffer)
yuuji@51 1042 (setq default-directory dir) ;for 18
yuuji@51 1043 (cd dir) ;for 19
yuuji@51 1044 (cond
yuuji@64 1045 ((not (fboundp 'start-process)) ;if MS-DOS
yuuji@51 1046 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
yuuji@51 1047 (call-process shell-file-name "con" "*dvi-preview*" nil
yuuji@60 1048 YaTeX-shell-command-option
yuuji@61 1049 (concat preview-command " " preview-file))
yuuji@51 1050 (send-string-to-terminal "\e[>5l") ;show cursor
yuuji@51 1051 (redraw-display))
yuuji@69 1052 ((and (string-match "dviout" preview-command) ;maybe on `kon'
yuuji@69 1053 (stringp (getenv "TERM"))
yuuji@69 1054 (string-match "^kon" (getenv "TERM")))
yuuji@69 1055 (call-process shell-file-name "con" "*dvi-preview*" nil
yuuji@69 1056 YaTeX-shell-command-option
yuuji@69 1057 (concat preview-command " " preview-file)))
yuuji@51 1058 (t ;if UNIX
yuuji@51 1059 (set-process-buffer
yuuji@86 1060 (let ((process-connection-type nil))
yuuji@86 1061 (start-process "preview" "*dvi-preview*" shell-file-name
yuuji@86 1062 YaTeX-shell-command-option
yuuji@86 1063 (concat preview-command " " preview-file)))
yuuji@51 1064 (get-buffer pbuffer))
yuuji@51 1065 (message
yuuji@51 1066 (concat "Starting " preview-command
yuuji@69 1067 " to preview " preview-file)))))))
yuuji@51 1068
yuuji@58 1069 (defvar YaTeX-xdvi-remote-program "xdvi")
yuuji@58 1070 (defun YaTeX-xdvi-remote-search (&optional region-mode)
yuuji@58 1071 "Search string at the point on xdvi -remote window.
yuuji@58 1072 Non-nil for optional argument REGION-MODE specifies the search string
yuuji@58 1073 by region."
yuuji@58 1074 (interactive "P")
yuuji@58 1075 (let ((pb " *xdvi*") str proc)
yuuji@58 1076 (save-excursion
yuuji@58 1077 (if region-mode
yuuji@58 1078 (setq str (buffer-substring (region-beginning) (region-end)))
yuuji@58 1079 (setq str (buffer-substring
yuuji@58 1080 (point)
yuuji@58 1081 (progn (skip-chars-forward "^\n\\\\}") (point)))))
yuuji@58 1082 (message "Searching `%s'..." str)
yuuji@58 1083 (if (boundp 'MULE)
yuuji@58 1084 (define-program-coding-system
yuuji@58 1085 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
yuuji@58 1086 *euc-japan*))
yuuji@58 1087 (setq proc
yuuji@58 1088 (start-process
yuuji@58 1089 "xdvi" pb YaTeX-xdvi-remote-program
yuuji@58 1090 "-remote" (format "SloppySearch(%s) " str)
yuuji@58 1091 (concat (YaTeX-get-preview-file-name) ".dvi")))
yuuji@69 1092 (message "Searching `%s'...Done" str))))
yuuji@58 1093
yuuji@350 1094 (defun YaTeX-preview-jlfmt-xdvi ()
yuuji@350 1095 "Call xdvi -sourceposition to DVI corresponding to current main file"
yuuji@351 1096 (interactive))
yuuji@350 1097
yuuji@350 1098 (defun YaTeX-preview-jump-line ()
yuuji@350 1099 "Call jump-line function of various previewer on current main file"
yuuji@350 1100 (interactive)
yuuji@350 1101 (save-excursion
yuuji@350 1102 (save-restriction
yuuji@350 1103 (widen)
yuuji@350 1104 (let*((pf (or YaTeX-parent-file
yuuji@350 1105 (save-excursion (YaTeX-visit-main t) (buffer-file-name))))
yuuji@351 1106 (pdir (file-name-directory pf))
yuuji@350 1107 (bnr (substring pf 0 (string-match "\\....$" pf)))
yuuji@436 1108 ;(cf (file-relative-name (buffer-file-name) pdir))
yuuji@436 1109 (cf (buffer-file-name)) ;2016-01-08
yuuji@350 1110 (buffer (get-buffer-create " *preview-jump-line*"))
yuuji@350 1111 (line (count-lines (point-min) (point-end-of-line)))
yuuji@350 1112 (previewer (YaTeX-preview-default-previewer))
yuuji@350 1113 (cmd (cond
yuuji@350 1114 ((string-match "xdvi" previewer)
yuuji@350 1115 (format "%s -nofork -sourceposition '%d %s' %s.dvi"
yuuji@350 1116 YaTeX-xdvi-remote-program
yuuji@350 1117 line cf bnr))
yuuji@350 1118 ((string-match "Skim" previewer)
yuuji@352 1119 (format "%s %d '%s.pdf' '%s'"
yuuji@350 1120 YaTeX-cmd-displayline line bnr cf))
yuuji@350 1121 ((string-match "evince" previewer)
yuuji@352 1122 (format "%s '%s.pdf' %d '%s'"
yuuji@352 1123 "fwdevince" bnr line cf))
yuuji@362 1124 ;;
yuuji@362 1125 ;; These lines below for other PDF viewer is not confirmed
yuuji@362 1126 ;; yet. If you find correct command line, PLEASE TELL
yuuji@362 1127 ;; IT TO THE AUTHOR before publishing patch on the web.
yuuji@362 1128 ;; ..PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE..
yuuji@369 1129 ((string-match "sumatra" previewer) ;;??
yuuji@369 1130 (format "%s \"%s.pdf\" -forward-search \"%s\" %d"
yuuji@369 1131 ;;Send patch to the author, please
yuuji@369 1132 previewer bnr cf line))
yuuji@369 1133 ((string-match "qpdfview" previewer) ;;??
yuuji@369 1134 (format "%s '%s.pdf#src:%s:%d:0'"
yuuji@369 1135 ;;Send patch to the author, please
yuuji@369 1136 previewer bnr cf line))
yuuji@369 1137 ((string-match "okular" previewer) ;;??
yuuji@507 1138 (format "%s '%s.pdf#src:%d %s'"
yuuji@369 1139 ;;Send patch to the author, please
yuuji@369 1140 previewer bnr line cf))
yuuji@362 1141 )))
yuuji@351 1142 (YaTeX-system cmd "jump-line" 'noask pdir)))))
yuuji@350 1143
yuuji@350 1144 (defun YaTeX-goto-corresponding-viewer ()
yuuji@350 1145 (let ((cmd (or (YaTeX-get-builtin "!") tex-command)))
yuuji@350 1146 (if (string-match "-src\\|synctex=" cmd)
yuuji@350 1147 (progn
yuuji@350 1148 (YaTeX-preview-jump-line)
yuuji@350 1149 t) ;for YaTeX-goto-corresponding-*
yuuji@350 1150 nil)))
yuuji@350 1151
yuuji@455 1152 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
yuuji@52 1153 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
yuuji@52 1154 (cond
yuuji@52 1155 ((and (get 'dvi2-command 'region)
yuuji@52 1156 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
yuuji@52 1157 (set file-sym (get 'dvi2-command 'file))
yuuji@52 1158 (set line-sym
yuuji@52 1159 (+ (- (apply '- (get 'dvi2-command 'offset)))
yuuji@52 1160 (symbol-value line-sym)
yuuji@52 1161 -1)))))
yuuji@52 1162
yuuji@51 1163 (defun YaTeX-prev-error ()
yuuji@230 1164 "Visit position of previous typeset error or warning.
yuuji@51 1165 To avoid making confliction of line numbers by editing, jump to
yuuji@51 1166 error or warning lines in reverse order."
yuuji@51 1167 (interactive)
yuuji@230 1168 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
yuuji@230 1169 (cur-win (selected-window))
yuuji@455 1170 tsb-frame-selwin
yuuji@230 1171 b0 bound errorp error-line typeset-win error-buffer error-win)
yuuji@51 1172 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@51 1173 (error "There is no typesetting buffer."))
yuuji@455 1174 (and (fboundp 'selected-frame)
yuuji@455 1175 (setq typeset-win (get-buffer-window YaTeX-typeset-buffer t))
yuuji@455 1176 (setq tsb-frame-selwin (frame-selected-window typeset-win)))
yuuji@455 1177
yuuji@51 1178 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@230 1179 (if (and (markerp YaTeX-typeset-marker)
yuuji@230 1180 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
yuuji@230 1181 (setq bound YaTeX-typeset-marker))
yuuji@51 1182 (setq typeset-win (selected-window))
yuuji@51 1183 (if (re-search-backward
yuuji@51 1184 (concat "\\(" latex-error-regexp "\\)\\|\\("
yuuji@51 1185 latex-warning-regexp "\\)")
yuuji@230 1186 bound t)
yuuji@80 1187 (setq errorp (match-beginning 1))
yuuji@51 1188 (select-window cur-win)
yuuji@68 1189 (error "No more errors on %s" cur-buf))
yuuji@80 1190 (goto-char (setq b0 (match-beginning 0)))
yuuji@51 1191 (skip-chars-forward "^0-9" (match-end 0))
yuuji@51 1192 (setq error-line
yuuji@452 1193 (YaTeX-str2int
yuuji@51 1194 (buffer-substring
yuuji@51 1195 (point)
yuuji@51 1196 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
yuuji@70 1197 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
yuuji@51 1198 (if (or (null error-line) (equal 0 error-line))
yuuji@51 1199 (error "Can't detect error position."))
yuuji@52 1200 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
yuuji@455 1201
yuuji@455 1202 (select-window typeset-win)
yuuji@455 1203 (skip-chars-backward "0-9")
yuuji@455 1204 (recenter (/ (window-height) 2))
yuuji@455 1205 (sit-for 1)
yuuji@455 1206 (goto-char b0)
yuuji@455 1207 (and tsb-frame-selwin (select-window tsb-frame-selwin)) ;restore selwin
yuuji@455 1208
yuuji@52 1209 (setq error-win (get-buffer-window error-buffer))
yuuji@51 1210 (select-window cur-win)
yuuji@51 1211 (cond
yuuji@455 1212 (t (goto-buffer-window error-buffer)
yuuji@455 1213 (if (fboundp 'raise-frame)
yuuji@455 1214 (let ((edit-frame (window-frame (selected-window))))
yuuji@455 1215 (raise-frame edit-frame)
yuuji@455 1216 (select-frame edit-frame)))
yuuji@455 1217 )
yuuji@51 1218 (error-win (select-window error-win))
yuuji@51 1219 ((eq (get-lru-window) typeset-win)
yuuji@51 1220 (YaTeX-switch-to-buffer error-buffer))
yuuji@51 1221 (t (select-window (get-lru-window))
yuuji@51 1222 (YaTeX-switch-to-buffer error-buffer)))
yuuji@51 1223 (setq error-win (selected-window))
yuuji@51 1224 (goto-line error-line)
yuuji@51 1225 (message "LaTeX %s in `%s' on line: %d."
yuuji@80 1226 (if errorp "error" "warning")
yuuji@51 1227 error-buffer error-line)
yuuji@69 1228 (select-window error-win)))
yuuji@51 1229
yuuji@51 1230 (defun YaTeX-jump-error-line ()
yuuji@51 1231 "Jump to corresponding line on latex command's error message."
yuuji@51 1232 (interactive)
yuuji@51 1233 (let (error-line error-file error-buf)
yuuji@51 1234 (save-excursion
yuuji@51 1235 (beginning-of-line)
yuuji@51 1236 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
yuuji@51 1237 (point-end-of-line) t)))
yuuji@51 1238 (if (null error-line)
yuuji@51 1239 (if (eobp) (insert (this-command-keys))
yuuji@51 1240 (error "No line number expression."))
yuuji@51 1241 (goto-char (match-beginning 0))
yuuji@452 1242 (setq error-line (YaTeX-str2int
yuuji@51 1243 (buffer-substring (match-beginning 1) (match-end 1)))
yuuji@70 1244 error-file (expand-file-name
yuuji@70 1245 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
yuuji@52 1246 (YaTeX-set-virtual-error-position 'error-file 'error-line)
yuuji@52 1247 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
yuuji@51 1248 (if (null error-buf)
yuuji@51 1249 (error "`%s' is not found in this directory." error-file))
yuuji@51 1250 (YaTeX-showup-buffer error-buf nil t)
yuuji@69 1251 (goto-line error-line)))
yuuji@51 1252
yuuji@51 1253 (defun YaTeX-send-string ()
yuuji@51 1254 "Send string to current typeset process."
yuuji@51 1255 (interactive)
yuuji@51 1256 (if (and (eq (process-status YaTeX-typeset-process) 'run)
yuuji@51 1257 (>= (point) (process-mark YaTeX-typeset-process)))
yuuji@51 1258 (let ((b (process-mark YaTeX-typeset-process))
yuuji@51 1259 (e (point-end-of-line)))
yuuji@51 1260 (goto-char b)
yuuji@51 1261 (skip-chars-forward " \t" e)
yuuji@51 1262 (setq b (point))
yuuji@51 1263 (process-send-string
yuuji@51 1264 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
yuuji@51 1265 (goto-char e)
yuuji@51 1266 (insert "\n")
yuuji@51 1267 (set-marker (process-mark YaTeX-typeset-process) (point))
yuuji@51 1268 (insert " "))
yuuji@69 1269 (ding)))
yuuji@51 1270
yuuji@51 1271 (defun YaTeX-view-error ()
yuuji@51 1272 (interactive)
yuuji@51 1273 (if (null (get-buffer YaTeX-typeset-buffer))
yuuji@51 1274 (message "No typeset buffer found.")
yuuji@51 1275 (let ((win (selected-window)))
yuuji@51 1276 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
yuuji@51 1277 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
yuuji@51 1278 ;; automatically scrolled up at typesetting.
yuuji@51 1279 ;;(goto-char (point-max))
yuuji@51 1280 ;;(forward-line -1)
yuuji@51 1281 ;;(recenter -1)
yuuji@69 1282 (select-window win))))
yuuji@51 1283
yuuji@51 1284 (defun YaTeX-get-error-file (default)
yuuji@51 1285 "Get current processing file from typesetting log."
yuuji@51 1286 (save-excursion
yuuji@51 1287 (let(s)
yuuji@51 1288 (condition-case () (up-list -1)
yuuji@51 1289 (error
yuuji@51 1290 (let ((list 0) found)
yuuji@51 1291 (while
yuuji@51 1292 (and (<= list 0) (not found)
yuuji@51 1293 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
yuuji@51 1294 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
yuuji@51 1295 (setq list (1- list)) ;open paren
yuuji@51 1296 (setq list (1+ list))
yuuji@51 1297 (if (= list 1)
yuuji@51 1298 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
yuuji@51 1299 (setq found t)
yuuji@51 1300 (setq list (1- list)))))))))
yuuji@51 1301 (setq s
yuuji@51 1302 (buffer-substring
yuuji@51 1303 (progn (forward-char 1) (point))
yuuji@453 1304 (if (re-search-forward
yuuji@453 1305 "\\.\\(tex\\|sty\\|ltx\\)\\>" nil (point-end-of-line))
yuuji@453 1306 (match-end 0)
yuuji@453 1307 (skip-chars-forward "^ \n" (point-end-of-line))
yuuji@453 1308 (point))))
yuuji@69 1309 (if (string= "" s) default s))))
yuuji@51 1310
yuuji@51 1311 (defun YaTeX-put-nonstopmode ()
yuuji@64 1312 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
yuuji@51 1313 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
yuuji@51 1314 nil ;if already written in text then do nothing
yuuji@51 1315 (save-excursion
yuuji@51 1316 (YaTeX-visit-main t)
yuuji@51 1317 (goto-char (point-min))
yuuji@60 1318 (insert "\\nonstopmode{}%_YaTeX_%\n")
yuuji@69 1319 (if (buffer-file-name) (basic-save-buffer))))))
yuuji@51 1320
yuuji@51 1321 (defun YaTeX-remove-nonstopmode ()
yuuji@64 1322 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
yuuji@51 1323 (save-excursion
yuuji@51 1324 (YaTeX-visit-main t)
yuuji@51 1325 (goto-char (point-min))
yuuji@51 1326 (forward-line 1)
yuuji@51 1327 (narrow-to-region (point-min) (point))
yuuji@51 1328 (goto-char (point-min))
yuuji@51 1329 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
yuuji@69 1330 (widen))))
yuuji@51 1331
yuuji@80 1332 (defvar YaTeX-dvi2-command-ext-alist
yuuji@388 1333 '(("[agxk]dvi\\|dviout" . ".dvi")
yuuji@80 1334 ("ghostview\\|gv" . ".ps")
yuuji@505 1335 ("acroread\\|[xk]pdf\\|pdfopen\\|Preview\\|TeXShop\\|Skim\\|evince\\|atril\\|xreader\\|mupdf\\|zathura\\|okular" . ".pdf")))
yuuji@80 1336
yuuji@86 1337 (defun YaTeX-get-preview-file-name (&optional preview-command)
yuuji@51 1338 "Get file name to preview by inquiring YaTeX-get-latex-command"
yuuji@86 1339 (if (null preview-command) (setq preview-command dvi2-command))
yuuji@51 1340 (let* ((latex-cmd (YaTeX-get-latex-command t))
yuuji@51 1341 (rin (rindex latex-cmd ? ))
yuuji@172 1342 (fname (if rin (substring latex-cmd (1+ rin)) ""))
yuuji@86 1343 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
yuuji@172 1344 (ext (if r (cdr r) "")))
yuuji@325 1345 (and (null r)
yuuji@325 1346 (eq (get 'dvi2-command 'format) 'pdf)
yuuji@499 1347 (setq ext ".pdf"))
yuuji@80 1348 (concat
yuuji@80 1349 (if (string= fname "")
yuuji@172 1350 (setq fname (substring (file-name-nondirectory
yuuji@172 1351 (buffer-file-name))
yuuji@172 1352 0 -4))
yuuji@172 1353 (setq fname (substring fname 0 (rindex fname ?.))))
yuuji@80 1354 ext)))
yuuji@51 1355
yuuji@574 1356 (defvar YaTeX-proc-feed-relative t
yuuji@574 1357 "Non-nil means feed process relative path name of target file.")
yuuji@51 1358 (defun YaTeX-get-latex-command (&optional switch)
yuuji@51 1359 "Specify the latex-command name and its argument.
yuuji@51 1360 If there is a line which begins with string: \"%#!\", the following
yuuji@51 1361 strings are assumed to be the latex-command and arguments. The
yuuji@51 1362 default value of latex-command is:
yuuji@51 1363 tex-command FileName
yuuji@388 1364 and if you write \"%#!platex\" in the beginning of certain line.
yuuji@388 1365 \"platex \" FileName
yuuji@51 1366 will be the latex-command,
yuuji@388 1367 and you write \"%#!platex main.tex\" on some line and argument SWITCH
yuuji@51 1368 is non-nil, then
yuuji@388 1369 \"platex main.tex\"
yuuji@51 1370
yuuji@51 1371 will be given to the shell."
yuuji@80 1372 (let (parent tparent magic)
yuuji@51 1373 (setq parent
yuuji@51 1374 (cond
yuuji@80 1375 (YaTeX-parent-file
yuuji@80 1376 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
yuuji@574 1377 (if YaTeX-proc-feed-relative
yuuji@574 1378 (file-relative-name YaTeX-parent-file)
yuuji@574 1379 YaTeX-parent-file)))
yuuji@51 1380 (t (save-excursion
yuuji@51 1381 (YaTeX-visit-main t)
yuuji@51 1382 (file-name-nondirectory (buffer-file-name)))))
yuuji@80 1383 magic (YaTeX-get-builtin "!")
yuuji@80 1384 tparent (file-name-nondirectory parent))
yuuji@80 1385 (YaTeX-replace-formats
yuuji@80 1386 (cond
yuuji@80 1387 (magic
yuuji@80 1388 (cond
yuuji@350 1389 (switch (if (string-match "\\s [^-]\\S *$" magic) magic
yuuji@80 1390 (concat magic " " parent)))
yuuji@350 1391 (t (concat (substring magic 0 (string-match "\\s [^-]\\S *$" magic)) " "))))
yuuji@80 1392 (t (concat tex-command " " (if switch parent))))
yuuji@80 1393 (list (cons "f" tparent)
yuuji@503 1394 (cons "r" (substring tparent 0 (rindex tparent ?.)))
yuuji@503 1395 (cons "k" (YaTeX-kanji-ptex-mnemonic))))))
yuuji@51 1396
yuuji@51 1397 (defvar YaTeX-lpr-command-history nil
yuuji@51 1398 "Holds command line history of YaTeX-lpr.")
yuuji@51 1399 (put 'YaTeX-lpr-command-history 'no-default t)
yuuji@79 1400 (defvar YaTeX-lpr-ask-page-range-default t)
yuuji@51 1401 (defun YaTeX-lpr (arg)
yuuji@79 1402 "Print out.
yuuji@79 1403 If prefix arg ARG is non nil, call print driver without
yuuji@51 1404 page range description."
yuuji@51 1405 (interactive "P")
yuuji@79 1406 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
yuuji@51 1407 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
yuuji@52 1408 from to (lbuffer "*dvi-printing*") dir)
yuuji@51 1409 (setq
yuuji@51 1410 cmd
yuuji@51 1411 (YaTeX-replace-format
yuuji@51 1412 cmd "f"
yuuji@51 1413 (if (or arg (not (string-match "%f" cmd)))
yuuji@51 1414 ""
yuuji@51 1415 (YaTeX-replace-format
yuuji@51 1416 dviprint-from-format
yuuji@51 1417 "b"
yuuji@51 1418 (if (string=
yuuji@51 1419 (setq from (read-string "From page(default 1): ")) "")
yuuji@51 1420 "1" from))))
yuuji@51 1421 )
yuuji@51 1422 (setq
yuuji@51 1423 cmd
yuuji@51 1424 (YaTeX-replace-format
yuuji@51 1425 cmd "t"
yuuji@51 1426 (if (or arg (not (string-match "%t" cmd))
yuuji@51 1427 (string=
yuuji@51 1428 (setq to (read-string "To page(default none): ")) ""))
yuuji@51 1429 ""
yuuji@51 1430 (YaTeX-replace-format dviprint-to-format "e" to)))
yuuji@51 1431 )
yuuji@72 1432 (setq
yuuji@72 1433 cmd
yuuji@72 1434 (YaTeX-replace-format
yuuji@72 1435 cmd "p"
yuuji@72 1436 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
yuuji@51 1437 (setq cmd
yuuji@51 1438 (read-string-with-history
yuuji@51 1439 "Edit command line: "
yuuji@53 1440 (format cmd
yuuji@53 1441 (if (get 'dvi2-command 'region)
yuuji@53 1442 (substring YaTeX-texput-file
yuuji@53 1443 0 (rindex YaTeX-texput-file ?.))
yuuji@53 1444 (YaTeX-get-preview-file-name)))
yuuji@51 1445 'YaTeX-lpr-command-history))
yuuji@51 1446 (save-excursion
yuuji@51 1447 (YaTeX-visit-main t) ;;change execution directory
yuuji@52 1448 (setq dir default-directory)
yuuji@51 1449 (YaTeX-showup-buffer
yuuji@354 1450 lbuffer 'YaTeX-showup-buffer-bottom-most)
yuuji@51 1451 (set-buffer (get-buffer-create lbuffer))
yuuji@51 1452 (erase-buffer)
yuuji@52 1453 (cd dir) ;for 19
yuuji@51 1454 (cond
yuuji@64 1455 ((not (fboundp 'start-process))
yuuji@51 1456 (call-process shell-file-name "con" "*dvi-printing*" nil
yuuji@51 1457 YaTeX-shell-command-option cmd))
yuuji@51 1458 (t
yuuji@51 1459 (set-process-buffer
yuuji@86 1460 (let ((process-connection-type nil))
yuuji@86 1461 (start-process "print" "*dvi-printing*" shell-file-name
yuuji@86 1462 YaTeX-shell-command-option cmd))
yuuji@51 1463 (get-buffer lbuffer))
yuuji@69 1464 (message "Starting printing command: %s..." cmd))))))
yuuji@51 1465
yuuji@51 1466 (defun YaTeX-main-file-p ()
yuuji@51 1467 "Return if current buffer is main LaTeX source."
yuuji@51 1468 (cond
yuuji@52 1469 (YaTeX-parent-file
yuuji@52 1470 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
yuuji@51 1471 ((YaTeX-get-builtin "!")
yuuji@70 1472 (string-match
yuuji@70 1473 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
yuuji@70 1474 (buffer-name)))
yuuji@51 1475 (t
yuuji@51 1476 (save-excursion
yuuji@57 1477 (let ((latex-main-id
yuuji@57 1478 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
yuuji@51 1479 (or (re-search-backward latex-main-id nil t)
yuuji@69 1480 (re-search-forward latex-main-id nil t)))))))
yuuji@51 1481
yuuji@51 1482 (defun YaTeX-visit-main (&optional setbuf)
yuuji@51 1483 "Switch buffer to main LaTeX source.
yuuji@122 1484 Use set-buffer instead of switch-to-buffer if the optional argument
yuuji@51 1485 SETBUF is t(Use it only from Emacs-Lisp program)."
yuuji@51 1486 (interactive "P")
yuuji@51 1487 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
yuuji@70 1488 (let ((ff (function (lambda (f)
yuuji@70 1489 (if setbuf (set-buffer (find-file-noselect f))
yuuji@70 1490 (find-file f)))))
yuuji@325 1491 b-in main-file mfa YaTeX-create-file-prefix-g
yuuji@70 1492 (hilit-auto-highlight (not setbuf)))
yuuji@51 1493 (if (setq b-in (YaTeX-get-builtin "!"))
yuuji@51 1494 (setq main-file (YaTeX-guess-parent b-in)))
yuuji@51 1495 (if YaTeX-parent-file
yuuji@51 1496 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
yuuji@51 1497 YaTeX-parent-file))
yuuji@51 1498 (if (YaTeX-main-file-p)
yuuji@51 1499 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
yuuji@51 1500 (cond
yuuji@70 1501 ((and ;;(interactive-p)
yuuji@70 1502 main-file
yuuji@70 1503 (cond ((get-file-buffer main-file)
yuuji@70 1504 (cond
yuuji@70 1505 (setbuf (set-buffer (get-file-buffer main-file)))
yuuji@70 1506 ((get-buffer-window (get-file-buffer main-file))
yuuji@70 1507 (select-window
yuuji@70 1508 (get-buffer-window (get-file-buffer main-file))))
yuuji@70 1509 (t (switch-to-buffer (get-file-buffer main-file)))))
yuuji@70 1510 ((file-exists-p main-file)
yuuji@70 1511 (funcall ff main-file)))))
yuuji@70 1512 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
yuuji@51 1513 ((and main-file
yuuji@51 1514 (file-exists-p (setq main-file (concat "../" main-file)))
yuuji@80 1515 (or b-in
yuuji@325 1516 (y-or-n-p (concat (setq mfa (expand-file-name main-file))
yuuji@80 1517 " is main file?:"))))
yuuji@325 1518 (setq YaTeX-parent-file mfa)
yuuji@70 1519 ;(YaTeX-switch-to-buffer main-file setbuf)
yuuji@70 1520 (funcall ff main-file)
yuuji@70 1521 )
yuuji@51 1522 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
yuuji@326 1523 (setq YaTeX-parent-file (expand-file-name main-file))
yuuji@70 1524 ; (YaTeX-switch-to-buffer main-file setbuf))
yuuji@70 1525 (funcall ff main-file))
yuuji@51 1526 )))
yuuji@69 1527 nil)
yuuji@51 1528
yuuji@51 1529 (defun YaTeX-guess-parent (command-line)
yuuji@51 1530 (setq command-line
yuuji@350 1531 (if (string-match "\\s \\([^-]\\S *\\)$" command-line)
yuuji@350 1532 (substring command-line (match-beginning 1))
yuuji@51 1533 (file-name-nondirectory (buffer-file-name)))
yuuji@51 1534 command-line
yuuji@51 1535 (concat (if (string-match "\\(.*\\)\\." command-line)
yuuji@51 1536 (substring command-line (match-beginning 1) (match-end 1))
yuuji@51 1537 command-line)
yuuji@69 1538 ".tex")))
yuuji@51 1539
yuuji@51 1540 (defun YaTeX-visit-main-other-window ()
yuuji@51 1541 "Switch to buffer main LaTeX source in other window."
yuuji@51 1542 (interactive)
yuuji@51 1543 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
yuuji@51 1544 (YaTeX-switch-to-buffer-other-window
yuuji@69 1545 (concat (YaTeX-get-preview-file-name) ".tex"))))
yuuji@51 1546
yuuji@51 1547 (defun YaTeX-save-buffers ()
yuuji@60 1548 "Save buffers whose major-mode is equal to current major-mode."
yuuji@51 1549 (basic-save-buffer)
yuuji@60 1550 (let ((cmm major-mode))
yuuji@60 1551 (save-excursion
yuuji@351 1552 (mapcar (function
yuuji@351 1553 (lambda (buf)
yuuji@60 1554 (set-buffer buf)
yuuji@60 1555 (if (and (buffer-file-name buf)
yuuji@60 1556 (eq major-mode cmm)
yuuji@60 1557 (buffer-modified-p buf)
yuuji@60 1558 (y-or-n-p (format "Save %s" (buffer-name buf))))
yuuji@351 1559 (save-buffer buf))))
yuuji@69 1560 (buffer-list)))))
yuuji@51 1561
yuuji@51 1562 (provide 'yatexprc)