yatex

view yatexprc.el @ 122:57d3d9abd661

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