yatex

view yatexprc.el @ 60:9e08ed569d80

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