yatex

view yatexprc.el @ 130:8703f090c628

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