yatex

view yatexprc.el @ 58:3a7c0c2bf16d

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