yatex

view yatexprc.el @ 65:df0bf1465254

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