yatex

view yatexprc.el @ 77:1b172d26b55e

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