yatex

view yatexprc.el @ 79:0734be649cb8

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