yatex

view yatexprc.el @ 70:44e3a5e1e883

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