yatex

view yatexprc.el @ 47:d7e7b4654058

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