yatex

view yatexprc.el @ 68:0eb6997bee16

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