yatex

view yatexprc.el @ 38:1ce952ad9c24

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