yatex

annotate yatexprc.el @ 611:e87c3271b8fd

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