yatex

view yatexprc.el @ 72:0aaebd07dad0

Support font-lock on XEmacs-21, Emacs-20, Emacs-21. Support Emacs-21.
author yuuji
date Mon, 25 Dec 2000 10:19:28 +0000
parents 44e3a5e1e883
children f41b01fef5d6
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX process handler.
3 ;;; yatexprc.el
4 ;;; (c )1993-2000 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Mon Dec 25 19:18:19 2000 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 "\\documentstyle{" YaTeX-default-document-style "}\n"
225 "\\begin{document}")))
226 (goto-char opoint)
227 ;;(set-buffer buffer) ;for clarity
228 (let ((hilit-auto-highlight nil))
229 (set-buffer (find-file-noselect texput)))
230 ;;(find-file YaTeX-texput-file)
231 (erase-buffer)
232 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
233 (insert "\\nonstopmode{}\n"))
234 (insert preamble "\n" subpreamble "\n")
235 (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo))
236 (insert-buffer-substring buffer reg-begin reg-end)
237 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
238 (insert "\n\\end{document}\n")
239 (basic-save-buffer)
240 (kill-buffer (current-buffer))
241 (set-buffer main) ;return to parent file or itself.
242 (YaTeX-typeset cmd YaTeX-typeset-buffer)
243 (switch-to-buffer buffer) ;for Emacs-19
244 (put 'dvi2-command 'region t)
245 (put 'dvi2-command 'file buffer)
246 (put 'dvi2-command 'offset lineinfo))))
248 (defun YaTeX-typeset-buffer ()
249 "Typeset whole buffer.
250 If %#! usage says other buffer is main text,
251 visit main buffer to confirm if its includeonly list contains current
252 buffer's file. And if it doesn't contain editing text, ask user which
253 action wants to be done, A:Add list, R:Replace list, %:comment-out list."
254 (interactive)
255 (YaTeX-save-buffers)
256 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
257 (mydir (file-name-directory (buffer-file-name)))
258 (cmd (YaTeX-get-latex-command t)) (cb (current-buffer)))
259 (if (YaTeX-main-file-p) nil
260 (save-excursion
261 (YaTeX-visit-main t) ;search into main buffer
262 (save-excursion
263 (push-mark (point) t)
264 (goto-char (point-min))
265 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
266 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
267 (let*
268 ((b (progn (skip-chars-forward "^{") (point)))
269 (e (progn (skip-chars-forward "^}") (1+ (point))))
270 (s (buffer-substring b e)) c
271 (pardir (file-name-directory (buffer-file-name))))
272 (if (string-match (concat "[{,/]" me "[,}]") s)
273 nil ; Nothing to do when it's already in includeonly.
274 (ding)
275 (switch-to-buffer (current-buffer));Display this buffer.
276 (setq
277 me ;;Rewrite my name(me) to contain sub directory name.
278 (concat
279 (if (string-match pardir mydir) ;if mydir is child of main
280 (substring mydir (length pardir)) ;cut absolute path
281 mydir) ;else concat absolute path name.
282 me))
283 (message
284 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
285 me)
286 (setq c (read-char))
287 (cond
288 ((= c ?a)
289 (goto-char (1+ b))
290 (insert me (if (string= s "{}") "" ",")))
291 ((= c ?r)
292 (delete-region (1+ b) (1- e)) (insert me))
293 ((= c ?%)
294 (beginning-of-line) (insert "%"))
295 (t nil))
296 (basic-save-buffer))))
297 (exchange-point-and-mark)))
298 (switch-to-buffer cb)) ;for 19
299 (YaTeX-typeset cmd YaTeX-typeset-buffer)
300 (put 'dvi2-command 'region nil)))
302 (defvar YaTeX-call-command-history nil
303 "Holds history list of YaTeX-call-command-on-file.")
304 (put 'YaTeX-call-command-history 'no-default t)
305 (defun YaTeX-call-command-on-file (base-cmd buffer)
306 (YaTeX-save-buffers)
307 (YaTeX-typeset
308 (read-string-with-history
309 "Call command: "
310 (concat base-cmd " " (YaTeX-get-preview-file-name))
311 'YaTeX-call-command-history)
312 buffer))
314 (defun YaTeX-bibtex-buffer (cmd)
315 "Pass the bibliography data of editing file to bibtex."
316 (interactive)
317 (YaTeX-save-buffers)
318 (YaTeX-call-command-on-file cmd "*YaTeX-bibtex*" ))
320 (defun YaTeX-kill-typeset-process (proc)
321 "Kill process PROC after sending signal to PROC.
322 PROC should be process identifier."
323 (cond
324 ((not (fboundp 'start-process))
325 (error "This system can't have concurrent process."))
326 ((or (null proc) (not (eq (process-status proc) 'run)))
327 (message "Typesetting process is not running."))
328 (t
329 (save-excursion
330 (set-buffer (process-buffer proc))
331 (save-excursion
332 (goto-char (point-max))
333 (beginning-of-line)
334 (if (looking-at "\\? +$")
335 (let ((mp (point-max)))
336 (process-send-string proc "x\n")
337 (while (= mp (point-max)) (sit-for 1))))))
338 (if (eq (process-status proc) 'run)
339 (progn
340 (interrupt-process proc)
341 (delete-process proc))))))
343 (defun YaTeX-system (command buffer)
344 "Execute some command on buffer. Not a official function."
345 (save-excursion
346 (YaTeX-showup-buffer
347 buffer (function (lambda (x) (nth 3 (window-edges x)))))
348 (let ((df default-directory)) ;preserve current buf's pwd
349 (set-buffer (get-buffer-create buffer)) ;1.61.3
350 (setq default-directory df)
351 (cd df))
352 (erase-buffer)
353 (if (not (fboundp 'start-process))
354 (call-process
355 shell-file-name nil buffer nil YaTeX-shell-command-option command)
356 (if (and (get-buffer-process buffer)
357 (eq (process-status (get-buffer-process buffer)) 'run)
358 (not
359 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
360 nil
361 (set-process-buffer
362 (start-process
363 "system" buffer shell-file-name YaTeX-shell-command-option command)
364 (get-buffer buffer))))))
366 (defvar YaTeX-default-paper-type "a4"
367 "*Default paper type.")
368 (defconst YaTeX-paper-type-alist
369 '(("a4paper" . "a4")
370 ("a5paper" . "a5")
371 ("b4paper" . "b4")
372 ("b5paper" . "b5"))
373 "Holds map of options and paper types.")
374 (defconst YaTeX-dvips-paper-option-alist
375 '(("a4" . "-t a4")
376 ("a5" . "-t a5")
377 ("b4" . "-t b4")
378 ("b5" . "-t b5")
379 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
380 ("a5r" . "-t landscape")
381 ("b4r" . "-t landscape")
382 ("b5r" . "-t landscape"))
383 "Holds map of dvips options and paper types.")
384 (defun YaTeX-get-paper-type ()
385 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
386 (save-excursion
387 (goto-char (point-min))
388 (if (re-search-forward
389 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
390 (let ((opts (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*")))
391 (concat
392 (catch 'found-paper
393 (mapcar (lambda (pair)
394 (if (member (car pair) opts)
395 (throw 'found-paper (cdr pair))))
396 YaTeX-paper-type-alist)
397 YaTeX-default-paper-type)
398 (if (member "landscape" opts) "r" ""))))))
400 (defvar YaTeX-preview-command-history nil
401 "Holds minibuffer history of preview command.")
402 (put 'YaTeX-preview-command-history 'no-default t)
403 (defvar YaTeX-preview-file-history nil
404 "Holds minibuffer history of file to preview.")
405 (put 'YaTeX-preview-file-history 'no-default t)
406 (defun YaTeX-preview (preview-command preview-file)
407 "Execute xdvi (or other) to tex-preview."
408 (interactive
409 (list
410 (read-string-with-history
411 "Preview command: "
412 (YaTeX-replace-format
413 (or (YaTeX-get-builtin "PREVIEW") dvi2-command)
414 "p" (concat "-paper " (YaTeX-get-paper-type)))
415 'YaTeX-preview-command-history)
416 (read-string-with-history
417 "Preview file[.dvi]: "
418 (if (get 'dvi2-command 'region)
419 (substring YaTeX-texput-file
420 0 (rindex YaTeX-texput-file ?.))
421 (YaTeX-get-preview-file-name))
422 'YaTeX-preview-file-history)))
423 (setq dvi2-command preview-command) ;`dvi2command' is buffer local
424 (save-excursion
425 (YaTeX-visit-main t)
426 (let ((pbuffer "*dvi-preview*") (dir default-directory))
427 (YaTeX-showup-buffer
428 pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
429 (set-buffer (get-buffer-create pbuffer))
430 (erase-buffer)
431 (setq default-directory dir) ;for 18
432 (cd dir) ;for 19
433 (cond
434 ((not (fboundp 'start-process)) ;if MS-DOS
435 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
436 (call-process shell-file-name "con" "*dvi-preview*" nil
437 YaTeX-shell-command-option
438 (concat preview-command " " preview-file))
439 (send-string-to-terminal "\e[>5l") ;show cursor
440 (redraw-display))
441 ((and (string-match "dviout" preview-command) ;maybe on `kon'
442 (stringp (getenv "TERM"))
443 (string-match "^kon" (getenv "TERM")))
444 (call-process shell-file-name "con" "*dvi-preview*" nil
445 YaTeX-shell-command-option
446 (concat preview-command " " preview-file)))
447 (t ;if UNIX
448 (set-process-buffer
449 (start-process "preview" "*dvi-preview*" shell-file-name
450 YaTeX-shell-command-option
451 (concat preview-command " " preview-file))
452 (get-buffer pbuffer))
453 (message
454 (concat "Starting " preview-command
455 " to preview " preview-file)))))))
457 (defvar YaTeX-xdvi-remote-program "xdvi")
458 (defun YaTeX-xdvi-remote-search (&optional region-mode)
459 "Search string at the point on xdvi -remote window.
460 Non-nil for optional argument REGION-MODE specifies the search string
461 by region."
462 (interactive "P")
463 (let ((pb " *xdvi*") str proc)
464 (save-excursion
465 (if region-mode
466 (setq str (buffer-substring (region-beginning) (region-end)))
467 (setq str (buffer-substring
468 (point)
469 (progn (skip-chars-forward "^\n\\\\}") (point)))))
470 (message "Searching `%s'..." str)
471 (if (boundp 'MULE)
472 (define-program-coding-system
473 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
474 *euc-japan*))
475 (setq proc
476 (start-process
477 "xdvi" pb YaTeX-xdvi-remote-program
478 "-remote" (format "SloppySearch(%s) " str)
479 (concat (YaTeX-get-preview-file-name) ".dvi")))
480 (message "Searching `%s'...Done" str))))
482 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
483 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
484 (cond
485 ((and (get 'dvi2-command 'region)
486 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
487 (set file-sym (get 'dvi2-command 'file))
488 (set line-sym
489 (+ (- (apply '- (get 'dvi2-command 'offset)))
490 (symbol-value line-sym)
491 -1)))))
493 (defun YaTeX-prev-error ()
494 "Visit previous typeset error.
495 To avoid making confliction of line numbers by editing, jump to
496 error or warning lines in reverse order."
497 (interactive)
498 (let ((cur-buf (buffer-name)) (cur-win (selected-window))
499 error-line typeset-win error-buffer error-win)
500 (if (null (get-buffer YaTeX-typeset-buffer))
501 (error "There is no typesetting buffer."))
502 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
503 (setq typeset-win (selected-window))
504 (if (re-search-backward
505 (concat "\\(" latex-error-regexp "\\)\\|\\("
506 latex-warning-regexp "\\)")
507 nil t)
508 nil
509 (select-window cur-win)
510 (error "No more errors on %s" cur-buf))
511 (goto-char (match-beginning 0))
512 (skip-chars-forward "^0-9" (match-end 0))
513 (setq error-line
514 (string-to-int
515 (buffer-substring
516 (point)
517 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
518 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
519 (if (or (null error-line) (equal 0 error-line))
520 (error "Can't detect error position."))
521 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
522 (setq error-win (get-buffer-window error-buffer))
523 (select-window cur-win)
524 (cond
525 (error-win (select-window error-win))
526 ((eq (get-lru-window) typeset-win)
527 (YaTeX-switch-to-buffer error-buffer))
528 (t (select-window (get-lru-window))
529 (YaTeX-switch-to-buffer error-buffer)))
530 (setq error-win (selected-window))
531 (goto-line error-line)
532 (message "LaTeX %s in `%s' on line: %d."
533 (if (match-beginning 1) "error" "warning")
534 error-buffer error-line)
535 (select-window typeset-win)
536 (skip-chars-backward "0-9")
537 (recenter (/ (window-height) 2))
538 (sit-for 1)
539 (goto-char (match-beginning 0))
540 (select-window error-win)))
542 (defun YaTeX-jump-error-line ()
543 "Jump to corresponding line on latex command's error message."
544 (interactive)
545 (let (error-line error-file error-buf)
546 (save-excursion
547 (beginning-of-line)
548 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
549 (point-end-of-line) t)))
550 (if (null error-line)
551 (if (eobp) (insert (this-command-keys))
552 (error "No line number expression."))
553 (goto-char (match-beginning 0))
554 (setq error-line (string-to-int
555 (buffer-substring (match-beginning 1) (match-end 1)))
556 error-file (expand-file-name
557 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
558 (YaTeX-set-virtual-error-position 'error-file 'error-line)
559 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
560 (if (null error-buf)
561 (error "`%s' is not found in this directory." error-file))
562 (YaTeX-showup-buffer error-buf nil t)
563 (goto-line error-line)))
565 (defun YaTeX-send-string ()
566 "Send string to current typeset process."
567 (interactive)
568 (if (and (eq (process-status YaTeX-typeset-process) 'run)
569 (>= (point) (process-mark YaTeX-typeset-process)))
570 (let ((b (process-mark YaTeX-typeset-process))
571 (e (point-end-of-line)))
572 (goto-char b)
573 (skip-chars-forward " \t" e)
574 (setq b (point))
575 (process-send-string
576 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
577 (goto-char e)
578 (insert "\n")
579 (set-marker (process-mark YaTeX-typeset-process) (point))
580 (insert " "))
581 (ding)))
583 (defun YaTeX-view-error ()
584 (interactive)
585 (if (null (get-buffer YaTeX-typeset-buffer))
586 (message "No typeset buffer found.")
587 (let ((win (selected-window)))
588 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
589 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
590 ;; automatically scrolled up at typesetting.
591 ;;(goto-char (point-max))
592 ;;(forward-line -1)
593 ;;(recenter -1)
594 (select-window win))))
596 (defun YaTeX-get-error-file (default)
597 "Get current processing file from typesetting log."
598 (save-excursion
599 (let(s)
600 (condition-case () (up-list -1)
601 (error
602 (let ((list 0) found)
603 (while
604 (and (<= list 0) (not found)
605 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
606 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
607 (setq list (1- list)) ;open paren
608 (setq list (1+ list))
609 (if (= list 1)
610 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
611 (setq found t)
612 (setq list (1- list)))))))))
613 (setq s
614 (buffer-substring
615 (progn (forward-char 1) (point))
616 (progn (skip-chars-forward "^ \n" (point-end-of-line))
617 (point))))
618 (if (string= "" s) default s))))
620 (defun YaTeX-put-nonstopmode ()
621 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
622 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
623 nil ;if already written in text then do nothing
624 (save-excursion
625 (YaTeX-visit-main t)
626 (goto-char (point-min))
627 (insert "\\nonstopmode{}%_YaTeX_%\n")
628 (if (buffer-file-name) (basic-save-buffer))))))
630 (defun YaTeX-remove-nonstopmode ()
631 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
632 (save-excursion
633 (YaTeX-visit-main t)
634 (goto-char (point-min))
635 (forward-line 1)
636 (narrow-to-region (point-min) (point))
637 (goto-char (point-min))
638 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
639 (widen))))
641 (defun YaTeX-get-preview-file-name ()
642 "Get file name to preview by inquiring YaTeX-get-latex-command"
643 (let* ((latex-cmd (YaTeX-get-latex-command t))
644 (rin (rindex latex-cmd ? ))
645 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
646 (period))
647 (if (string= fname "")
648 (setq fname (substring (file-name-nondirectory
649 (buffer-file-name))
650 0 -4))
651 (setq period (rindex fname ?.))
652 (setq fname (substring fname 0 (if (eq -1 period) nil period))))))
654 (defun YaTeX-get-latex-command (&optional switch)
655 "Specify the latex-command name and its argument.
656 If there is a line which begins with string: \"%#!\", the following
657 strings are assumed to be the latex-command and arguments. The
658 default value of latex-command is:
659 tex-command FileName
660 and if you write \"%#!jlatex\" in the beginning of certain line.
661 \"jlatex \" FileName
662 will be the latex-command,
663 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
664 is non-nil, then
665 \"jlatex main.tex\"
667 will be given to the shell."
668 (let (magic command target)
669 (setq parent
670 (cond
671 (YaTeX-parent-file YaTeX-parent-file)
672 (t (save-excursion
673 (YaTeX-visit-main t)
674 (file-name-nondirectory (buffer-file-name)))))
675 magic (YaTeX-get-builtin "!"))
676 (cond
677 (magic
678 (cond
679 (switch (if (string-match "\\s " magic) magic
680 (concat magic " " parent)))
681 (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
682 (t (concat tex-command " " (if switch parent))))))
684 (defvar YaTeX-lpr-command-history nil
685 "Holds command line history of YaTeX-lpr.")
686 (put 'YaTeX-lpr-command-history 'no-default t)
687 (defun YaTeX-lpr (arg)
688 "Print out. If prefix arg ARG is non nil, call print driver without
689 page range description."
690 (interactive "P")
691 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
692 from to (lbuffer "*dvi-printing*") dir)
693 (setq
694 cmd
695 (YaTeX-replace-format
696 cmd "f"
697 (if (or arg (not (string-match "%f" cmd)))
698 ""
699 (YaTeX-replace-format
700 dviprint-from-format
701 "b"
702 (if (string=
703 (setq from (read-string "From page(default 1): ")) "")
704 "1" from))))
705 )
706 (setq
707 cmd
708 (YaTeX-replace-format
709 cmd "t"
710 (if (or arg (not (string-match "%t" cmd))
711 (string=
712 (setq to (read-string "To page(default none): ")) ""))
713 ""
714 (YaTeX-replace-format dviprint-to-format "e" to)))
715 )
716 (setq
717 cmd
718 (YaTeX-replace-format
719 cmd "p"
720 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
721 (setq cmd
722 (read-string-with-history
723 "Edit command line: "
724 (format cmd
725 (if (get 'dvi2-command 'region)
726 (substring YaTeX-texput-file
727 0 (rindex YaTeX-texput-file ?.))
728 (YaTeX-get-preview-file-name)))
729 'YaTeX-lpr-command-history))
730 (save-excursion
731 (YaTeX-visit-main t) ;;change execution directory
732 (setq dir default-directory)
733 (YaTeX-showup-buffer
734 lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
735 (set-buffer (get-buffer-create lbuffer))
736 (erase-buffer)
737 (cd dir) ;for 19
738 (cond
739 ((not (fboundp 'start-process))
740 (call-process shell-file-name "con" "*dvi-printing*" nil
741 YaTeX-shell-command-option cmd))
742 (t
743 (set-process-buffer
744 (start-process "print" "*dvi-printing*" shell-file-name
745 YaTeX-shell-command-option cmd)
746 (get-buffer lbuffer))
747 (message "Starting printing command: %s..." cmd))))))
749 (defun YaTeX-main-file-p ()
750 "Return if current buffer is main LaTeX source."
751 (cond
752 (YaTeX-parent-file
753 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
754 ((YaTeX-get-builtin "!")
755 (string-match
756 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
757 (buffer-name)))
758 (t
759 (save-excursion
760 (let ((latex-main-id
761 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
762 (or (re-search-backward latex-main-id nil t)
763 (re-search-forward latex-main-id nil t)))))))
765 (defun YaTeX-visit-main (&optional setbuf)
766 "Switch buffer to main LaTeX source.
767 Use set-buffer instead of switch-to-buffer if the optional second argument
768 SETBUF is t(Use it only from Emacs-Lisp program)."
769 (interactive "P")
770 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
771 (let ((ff (function (lambda (f)
772 (if setbuf (set-buffer (find-file-noselect f))
773 (find-file f)))))
774 b-in main-file YaTeX-create-file-prefix-g
775 (hilit-auto-highlight (not setbuf)))
776 (if (setq b-in (YaTeX-get-builtin "!"))
777 (setq main-file (YaTeX-guess-parent b-in)))
778 (if YaTeX-parent-file
779 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
780 YaTeX-parent-file))
781 (if (YaTeX-main-file-p)
782 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
783 (cond
784 ((and ;;(interactive-p)
785 main-file
786 (cond ((get-file-buffer main-file)
787 (cond
788 (setbuf (set-buffer (get-file-buffer main-file)))
789 ((get-buffer-window (get-file-buffer main-file))
790 (select-window
791 (get-buffer-window (get-file-buffer main-file))))
792 (t (switch-to-buffer (get-file-buffer main-file)))))
793 ((file-exists-p main-file)
794 (funcall ff main-file)))))
795 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
796 ((and main-file
797 (file-exists-p (setq main-file (concat "../" main-file)))
798 (y-or-n-p (concat (expand-file-name main-file)
799 " is main file?:")))
800 (setq YaTeX-parent-file main-file)
801 ;(YaTeX-switch-to-buffer main-file setbuf)
802 (funcall ff main-file)
803 )
804 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
805 (setq YaTeX-parent-file main-file)
806 ; (YaTeX-switch-to-buffer main-file setbuf))
807 (funcall ff main-file))
808 )))
809 nil)
811 (defun YaTeX-guess-parent (command-line)
812 (setq command-line
813 (if (string-match ".*\\s " command-line)
814 (substring command-line (match-end 0))
815 (file-name-nondirectory (buffer-file-name)))
816 command-line
817 (concat (if (string-match "\\(.*\\)\\." command-line)
818 (substring command-line (match-beginning 1) (match-end 1))
819 command-line)
820 ".tex")))
822 (defun YaTeX-visit-main-other-window ()
823 "Switch to buffer main LaTeX source in other window."
824 (interactive)
825 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
826 (YaTeX-switch-to-buffer-other-window
827 (concat (YaTeX-get-preview-file-name) ".tex"))))
829 (defun YaTeX-get-builtin (key)
830 "Read source built-in command of %# usage."
831 (save-excursion
832 (goto-char (point-min))
833 (if (and (re-search-forward
834 (concat "^" (regexp-quote (concat "%#" key))) nil t)
835 (not (eolp)))
836 (buffer-substring
837 (progn (skip-chars-forward " " (point-end-of-line))(point))
838 (point-end-of-line))
839 nil)))
841 (defun YaTeX-save-buffers ()
842 "Save buffers whose major-mode is equal to current major-mode."
843 (basic-save-buffer)
844 (let ((cmm major-mode))
845 (save-excursion
846 (mapcar '(lambda (buf)
847 (set-buffer buf)
848 (if (and (buffer-file-name buf)
849 (eq major-mode cmm)
850 (buffer-modified-p buf)
851 (y-or-n-p (format "Save %s" (buffer-name buf))))
852 (save-buffer buf)))
853 (buffer-list)))))
855 (provide 'yatexprc)