yatex

view yatexprc.el @ 147:f9d3c2451d19

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