yatex

view yatexprc.el @ 59:48ac97a6b6ce

Call drawing tools ID completion (yahtml)
author yuuji
date Wed, 01 May 1996 15:35:40 +0000
parents 3a7c0c2bf16d
children 9e08ed569d80
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 Mar 29 00:15:35 1996 on inspire
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 (let ((df default-directory)) ;preserve current buf's pwd
332 (set-buffer (get-buffer-create buffer)) ;1.61.3
333 (setq default-directory df)
334 (cd df))
335 (erase-buffer)
336 (if YaTeX-dos
337 (call-process
338 shell-file-name nil buffer nil YaTeX-shell-command-option command)
339 (if (and (get-buffer-process buffer)
340 (eq (process-status (get-buffer-process buffer)) 'run)
341 (not
342 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
343 nil
344 (set-process-buffer
345 (start-process
346 "system" buffer shell-file-name YaTeX-shell-command-option command)
347 (get-buffer buffer)))))
348 )
350 (defvar YaTeX-preview-command-history nil
351 "Holds minibuffer history of preview command.")
352 (put 'YaTeX-preview-command-history 'no-default t)
353 (defvar YaTeX-preview-file-history nil
354 "Holds minibuffer history of file to preview.")
355 (put 'YaTeX-preview-file-history 'no-default t)
356 (defun YaTeX-preview (preview-command preview-file)
357 "Execute xdvi (or other) to tex-preview."
358 (interactive
359 (list
360 (read-string-with-history
361 "Preview command: "
362 (or (YaTeX-get-builtin "PREVIEW") dvi2-command)
363 'YaTeX-preview-command-history)
364 (read-string-with-history
365 "Preview file[.dvi]: "
366 (if (get 'dvi2-command 'region)
367 (substring YaTeX-texput-file
368 0 (rindex YaTeX-texput-file ?.))
369 (YaTeX-get-preview-file-name))
370 'YaTeX-preview-file-history)))
371 (setq dvi2-command preview-command) ;`dvi2command' is buffer local
372 (save-excursion
373 (YaTeX-visit-main t)
374 (let ((pbuffer "*dvi-preview*") (dir default-directory))
375 (YaTeX-showup-buffer
376 pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
377 (set-buffer (get-buffer-create pbuffer))
378 (erase-buffer)
379 (setq default-directory dir) ;for 18
380 (cd dir) ;for 19
381 (cond
382 (YaTeX-dos ;if MS-DOS
383 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
384 (call-process shell-file-name "con" "*dvi-preview*" nil
385 YaTeX-shell-command-option preview-command preview-file)
386 (send-string-to-terminal "\e[>5l") ;show cursor
387 (redraw-display))
388 (t ;if UNIX
389 (set-process-buffer
390 (start-process "preview" "*dvi-preview*" shell-file-name
391 YaTeX-shell-command-option
392 (concat preview-command " " preview-file))
393 (get-buffer pbuffer))
394 (message
395 (concat "Starting " preview-command
396 " to preview " preview-file))))))
397 )
399 (defvar YaTeX-xdvi-remote-program "xdvi")
400 (defun YaTeX-xdvi-remote-search (&optional region-mode)
401 "Search string at the point on xdvi -remote window.
402 Non-nil for optional argument REGION-MODE specifies the search string
403 by region."
404 (interactive "P")
405 (let ((pb " *xdvi*") str proc)
406 (save-excursion
407 (if region-mode
408 (setq str (buffer-substring (region-beginning) (region-end)))
409 (setq str (buffer-substring
410 (point)
411 (progn (skip-chars-forward "^\n\\\\}") (point)))))
412 (message "Searching `%s'..." str)
413 (if (boundp 'MULE)
414 (define-program-coding-system
415 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
416 *euc-japan*))
417 (setq proc
418 (start-process
419 "xdvi" pb YaTeX-xdvi-remote-program
420 "-remote" (format "SloppySearch(%s) " str)
421 (concat (YaTeX-get-preview-file-name) ".dvi")))
422 (message "Searching `%s'...Done" str)))
423 )
425 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
426 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
427 (cond
428 ((and (get 'dvi2-command 'region)
429 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
430 (set file-sym (get 'dvi2-command 'file))
431 (set line-sym
432 (+ (- (apply '- (get 'dvi2-command 'offset)))
433 (symbol-value line-sym)
434 -1)))))
436 (defun YaTeX-prev-error ()
437 "Visit previous typeset error.
438 To avoid making confliction of line numbers by editing, jump to
439 error or warning lines in reverse order."
440 (interactive)
441 (let ((cur-buf (buffer-name)) (cur-win (selected-window))
442 error-line typeset-win error-buffer error-win)
443 (if (null (get-buffer YaTeX-typeset-buffer))
444 (error "There is no typesetting buffer."))
445 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
446 (setq typeset-win (selected-window))
447 (if (re-search-backward
448 (concat "\\(" latex-error-regexp "\\)\\|\\("
449 latex-warning-regexp "\\)")
450 nil t)
451 nil
452 (select-window cur-win)
453 (error "No more erros on %s" cur-buf))
454 (goto-char (match-beginning 0))
455 (skip-chars-forward "^0-9" (match-end 0))
456 (setq error-line
457 (string-to-int
458 (buffer-substring
459 (point)
460 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
461 error-buffer (YaTeX-get-error-file cur-buf))
462 (if (or (null error-line) (equal 0 error-line))
463 (error "Can't detect error position."))
464 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
465 (setq error-win (get-buffer-window error-buffer))
466 (select-window cur-win)
467 (cond
468 (error-win (select-window error-win))
469 ((eq (get-lru-window) typeset-win)
470 (YaTeX-switch-to-buffer error-buffer))
471 (t (select-window (get-lru-window))
472 (YaTeX-switch-to-buffer error-buffer)))
473 (setq error-win (selected-window))
474 (goto-line error-line)
475 (message "LaTeX %s in `%s' on line: %d."
476 (if (match-beginning 1) "error" "warning")
477 error-buffer error-line)
478 (select-window typeset-win)
479 (skip-chars-backward "0-9")
480 (recenter (/ (window-height) 2))
481 (sit-for 3)
482 (goto-char (match-beginning 0))
483 (select-window error-win))
484 )
486 (defun YaTeX-jump-error-line ()
487 "Jump to corresponding line on latex command's error message."
488 (interactive)
489 (let (error-line error-file error-buf)
490 (save-excursion
491 (beginning-of-line)
492 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
493 (point-end-of-line) t)))
494 (if (null error-line)
495 (if (eobp) (insert (this-command-keys))
496 (error "No line number expression."))
497 (goto-char (match-beginning 0))
498 (setq error-line (string-to-int
499 (buffer-substring (match-beginning 1) (match-end 1)))
500 error-file (YaTeX-get-error-file YaTeX-current-TeX-buffer))
501 (YaTeX-set-virtual-error-position 'error-file 'error-line)
502 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
503 (if (null error-buf)
504 (error "`%s' is not found in this directory." error-file))
505 (YaTeX-showup-buffer error-buf nil t)
506 (goto-line error-line))
507 )
509 (defun YaTeX-send-string ()
510 "Send string to current typeset process."
511 (interactive)
512 (if (and (eq (process-status YaTeX-typeset-process) 'run)
513 (>= (point) (process-mark YaTeX-typeset-process)))
514 (let ((b (process-mark YaTeX-typeset-process))
515 (e (point-end-of-line)))
516 (goto-char b)
517 (skip-chars-forward " \t" e)
518 (setq b (point))
519 (process-send-string
520 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
521 (goto-char e)
522 (insert "\n")
523 (set-marker (process-mark YaTeX-typeset-process) (point))
524 (insert " "))
525 (ding))
526 )
528 (defun YaTeX-view-error ()
529 (interactive)
530 (if (null (get-buffer YaTeX-typeset-buffer))
531 (message "No typeset buffer found.")
532 (let ((win (selected-window)))
533 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
534 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
535 ;; automatically scrolled up at typesetting.
536 ;;(goto-char (point-max))
537 ;;(forward-line -1)
538 ;;(recenter -1)
539 (select-window win)))
540 )
542 (defun YaTeX-get-error-file (default)
543 "Get current processing file from typesetting log."
544 (save-excursion
545 (let(s)
546 (condition-case () (up-list -1)
547 (error
548 (let ((list 0) found)
549 (while
550 (and (<= list 0) (not found)
551 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
552 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
553 (setq list (1- list)) ;open paren
554 (setq list (1+ list))
555 (if (= list 1)
556 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
557 (setq found t)
558 (setq list (1- list)))))))))
559 (setq s
560 (buffer-substring
561 (progn (forward-char 1) (point))
562 (progn (skip-chars-forward "^ \n" (point-end-of-line))
563 (point))))
564 (if (string= "" s) default s)))
565 )
567 (defun YaTeX-put-nonstopmode ()
568 (if YaTeX-need-nonstop
569 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
570 nil ;if already written in text then do nothing
571 (save-excursion
572 (YaTeX-visit-main t)
573 (goto-char (point-min))
574 (insert "\\nonstopmode{}%_YaTeX_%\n")))
575 )
576 )
578 (defun YaTeX-remove-nonstopmode ()
579 (if YaTeX-need-nonstop ;for speed
580 (save-excursion
581 (YaTeX-visit-main t)
582 (goto-char (point-min))
583 (forward-line 1)
584 (narrow-to-region (point-min) (point))
585 (goto-char (point-min))
586 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
587 (widen)))
588 )
590 (defun YaTeX-get-preview-file-name ()
591 "Get file name to preview by inquiring YaTeX-get-latex-command"
592 (let* ((latex-cmd (YaTeX-get-latex-command t))
593 (rin (rindex latex-cmd ? ))
594 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
595 (period))
596 (if (string= fname "")
597 (setq fname (substring (file-name-nondirectory
598 (buffer-file-name))
599 0 -4))
600 (setq period (rindex fname ?.))
601 (setq fname (substring fname 0 (if (eq -1 period) nil period)))
602 ))
603 )
605 (defun YaTeX-get-latex-command (&optional switch)
606 "Specify the latex-command name and its argument.
607 If there is a line which begins with string: \"%#!\", the following
608 strings are assumed to be the latex-command and arguments. The
609 default value of latex-command is:
610 tex-command FileName
611 and if you write \"%#!jlatex\" in the beginning of certain line.
612 \"jlatex \" FileName
613 will be the latex-command,
614 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
615 is non-nil, then
616 \"jlatex main.tex\"
618 will be given to the shell."
619 (let (magic command target)
620 (setq parent
621 (cond
622 (YaTeX-parent-file YaTeX-parent-file)
623 (t (save-excursion
624 (YaTeX-visit-main t)
625 (file-name-nondirectory (buffer-file-name)))))
626 magic (YaTeX-get-builtin "!"))
627 (cond
628 (magic
629 (cond
630 (switch (if (string-match "\\s " magic) magic
631 (concat magic " " parent)))
632 (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
633 (t (concat tex-command " " (if switch parent)))))
634 )
636 (defvar YaTeX-lpr-command-history nil
637 "Holds command line history of YaTeX-lpr.")
638 (put 'YaTeX-lpr-command-history 'no-default t)
639 (defun YaTeX-lpr (arg)
640 "Print out. If prefix arg ARG is non nil, call print driver without
641 page range description."
642 (interactive "P")
643 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
644 from to (lbuffer "*dvi-printing*") dir)
645 (setq
646 cmd
647 (YaTeX-replace-format
648 cmd "f"
649 (if (or arg (not (string-match "%f" cmd)))
650 ""
651 (YaTeX-replace-format
652 dviprint-from-format
653 "b"
654 (if (string=
655 (setq from (read-string "From page(default 1): ")) "")
656 "1" from))))
657 )
658 (setq
659 cmd
660 (YaTeX-replace-format
661 cmd "t"
662 (if (or arg (not (string-match "%t" cmd))
663 (string=
664 (setq to (read-string "To page(default none): ")) ""))
665 ""
666 (YaTeX-replace-format dviprint-to-format "e" to)))
667 )
668 (setq cmd
669 (read-string-with-history
670 "Edit command line: "
671 (format cmd
672 (if (get 'dvi2-command 'region)
673 (substring YaTeX-texput-file
674 0 (rindex YaTeX-texput-file ?.))
675 (YaTeX-get-preview-file-name)))
676 'YaTeX-lpr-command-history))
677 (save-excursion
678 (YaTeX-visit-main t) ;;change execution directory
679 (setq dir default-directory)
680 (YaTeX-showup-buffer
681 lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
682 (set-buffer (get-buffer-create lbuffer))
683 (erase-buffer)
684 (cd dir) ;for 19
685 (cond
686 (YaTeX-dos
687 (call-process shell-file-name "con" "*dvi-printing*" nil
688 YaTeX-shell-command-option cmd))
689 (t
690 (set-process-buffer
691 (start-process "print" "*dvi-printing*" shell-file-name
692 YaTeX-shell-command-option cmd)
693 (get-buffer lbuffer))
694 (message "Starting printing command: %s..." cmd)))
695 ))
696 )
698 (defun YaTeX-main-file-p ()
699 "Return if current buffer is main LaTeX source."
700 (cond
701 (YaTeX-parent-file
702 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
703 ((YaTeX-get-builtin "!")
704 (string-match (YaTeX-guess-parent (YaTeX-get-builtin "!")) (buffer-name)))
705 (t
706 (save-excursion
707 (let ((latex-main-id
708 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
709 (or (re-search-backward latex-main-id nil t)
710 (re-search-forward latex-main-id nil t))))))
711 )
713 (defun YaTeX-visit-main (&optional setbuf)
714 "Switch buffer to main LaTeX source.
715 Use set-buffer instead of switch-to-buffer if the optional second argument
716 SETBUF is t(Use it only from Emacs-Lisp program)."
717 (interactive "P")
718 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
719 (let (b-in main-file)
720 (if (setq b-in (YaTeX-get-builtin "!"))
721 (setq main-file (YaTeX-guess-parent b-in)))
722 (if YaTeX-parent-file
723 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
724 YaTeX-parent-file))
725 (if (YaTeX-main-file-p)
726 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
727 (cond
728 ((and (interactive-p) main-file (get-file-buffer main-file))
729 (goto-buffer-window main-file))
730 ((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
731 ((and main-file
732 (file-exists-p (setq main-file (concat "../" main-file)))
733 (y-or-n-p (concat (expand-file-name main-file)
734 " is main file?:")))
735 (setq YaTeX-parent-file main-file)
736 (YaTeX-switch-to-buffer main-file setbuf))
737 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
738 (setq YaTeX-parent-file main-file)
739 (YaTeX-switch-to-buffer main-file setbuf))
740 )))
741 nil
742 )
745 (defun YaTeX-guess-parent (command-line)
746 (setq command-line
747 (if (string-match ".*\\s " command-line)
748 (substring command-line (match-end 0))
749 (file-name-nondirectory (buffer-file-name)))
750 command-line
751 (concat (if (string-match "\\(.*\\)\\." command-line)
752 (substring command-line (match-beginning 1) (match-end 1))
753 command-line)
754 ".tex"))
755 )
757 (defun YaTeX-visit-main-other-window ()
758 "Switch to buffer main LaTeX source in other window."
759 (interactive)
760 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
761 (YaTeX-switch-to-buffer-other-window
762 (concat (YaTeX-get-preview-file-name) ".tex")))
763 )
765 (defun YaTeX-get-builtin (key)
766 "Read source built-in command of %# usage."
767 (save-excursion
768 (goto-char (point-min))
769 (if (and (re-search-forward
770 (concat "^" (regexp-quote (concat "%#" key))) nil t)
771 (not (eolp)))
772 (buffer-substring
773 (progn (skip-chars-forward " " (point-end-of-line))(point))
774 (point-end-of-line))
775 nil))
776 )
778 (defun YaTeX-save-buffers ()
779 "Save buffers which is in yatex-mode."
780 (basic-save-buffer)
781 (save-excursion
782 (mapcar '(lambda (buf)
783 (set-buffer buf)
784 (if (and (buffer-file-name buf)
785 (eq major-mode 'yatex-mode)
786 (buffer-modified-p buf)
787 (y-or-n-p (format "Save %s" (buffer-name buf))))
788 (save-buffer buf)))
789 (buffer-list)))
790 )
792 (provide 'yatexprc)