yatex

view yatexprc.el @ 7:9a56acb6c287

Fill-paragraph and (un)comment-paragraph work fine. Fix kill range of YaTeX-kill-some-pairs. Ignore begin/end in verb or verbatim. Indent rigidly initial space between begin/end pairs. Add yatex-mode-load-hook. Go to corresponding \label or \ref.
author yuuji
date Tue, 04 May 1993 12:57:27 +0000
parents
children 390df0e505da
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX process handler.
3 ;;; yatexprc.el rev.1.42
4 ;;; (c)1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Tue May 4 21:49:59 1993 on figaro
6 ;;; $Id$
8 (require 'yatex)
10 (defvar YaTeX-typeset-process nil
11 "Process identifier for jlatex"
12 )
13 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
14 "Process buffer for jlatex")
16 (defun YaTeX-typeset (command buffer)
17 "Execute jlatex (or other) to LaTeX typeset."
18 (interactive)
19 (if (and YaTeX-typeset-process
20 (eq (process-status YaTeX-typeset-process) 'run))
21 ;; if tex command is halting.
22 (YaTeX-kill-typeset-process YaTeX-typeset-process))
23 (YaTeX-visit-main t) ;;execution directory
24 (with-output-to-temp-buffer buffer
25 (if (eq system-type 'ms-dos) ;if MS-DOS
26 (progn
27 (message (concat "Typesetting " (buffer-name) "..."))
28 (YaTeX-put-nonstopmode)
29 (call-process shell-file-name
30 nil buffer nil "/c" command)
31 (YaTeX-remove-nonstopmode))
32 (setq YaTeX-typeset-process ;if UNIX
33 (start-process "LaTeX" buffer shell-file-name "-c"
34 command))
35 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
36 (setq current-TeX-buffer (buffer-name))
37 (other-window 1)
38 (use-local-map YaTeX-typesetting-mode-map)
39 (setq mode-name "typeset")
40 (if YaTeX-typeset-process ; if process is running (maybe on UNIX)
41 (cond ((boundp 'MULE)
42 (set-current-process-coding-system
43 YaTeX-latex-message-code YaTeX-coding-system))
44 ((boundp 'NEMACS)
45 (set-kanji-process-code YaTeX-latex-message-code))))
46 (message "Type SPC to continue.")
47 (goto-char (point-max))
48 (if (eq system-type 'ms-dos) (message "Done.")
49 (while (bobp) (message "Invoking process. wait...") (sleep-for 1))
50 (insert (message " ")))
51 (if (bolp) (forward-line -1))
52 (recenter -1)
53 (other-window -1)
54 )
56 (defun YaTeX-typeset-sentinel (proc mes)
57 (cond ((null (buffer-name (process-buffer proc)))
58 ;; buffer killed
59 (set-process-buffer proc nil))
60 ((memq (process-status proc) '(signal exit))
61 (let* ((obuf (current-buffer)))
62 ;; save-excursion isn't the right thing if
63 ;; process-buffer is current-buffer
64 (unwind-protect
65 (progn
66 ;; Write something in *typesetting* and hack its mode line
67 (if (equal (current-buffer) (process-buffer proc))
68 nil
69 (other-window 1)
70 (switch-to-buffer (process-buffer proc))
71 (goto-char (point-max))
72 (recenter -3)
73 (other-window -1))
74 (set-buffer (process-buffer proc))
75 (goto-char (point-max))
76 (insert ?\n "latex typesetting " mes)
77 (forward-char -1)
78 (insert " at " (substring (current-time-string) 0 -5) "\n")
79 (forward-char 1)
80 (setq mode-line-process
81 (concat ": "
82 (symbol-name (process-status proc))))
83 (message "latex typesetting done.")
84 ;; If buffer and mode line will show that the process
85 ;; is dead, we can delete it now. Otherwise it
86 ;; will stay around until M-x list-processes.
87 (delete-process proc)
88 )
89 (setq YaTeX-typesetting-process nil)
90 ;; Force mode line redisplay soon
91 (set-buffer-modified-p (buffer-modified-p))
92 )
93 (set-buffer obuf))))
94 )
96 (defvar YaTeX-texput-file "texput.tex"
97 "*File name for temporary file of typeset-region."
98 )
100 (defun YaTeX-typeset-region ()
101 "Paste the region to the file `texput.tex' and execute jlatex (or other)
102 to LaTeX typeset. The region is specified by the rule:
103 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
104 (1-1)if the keyword `%#END' is found after `%#BEGIN',
105 ->Assume the text between `%#BEGIN' and `%#END' as region.
106 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
107 ->Assume the text after `%#BEGIN' as region.
108 (2)If no `%#BEGIN' usage is found before the (point),
109 ->Assume the text between current (point) and (mark) as region.
110 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
111 operation to the region."
112 (interactive)
113 (save-excursion
114 (let*
115 ((end "") typeout ;Type out message that tells the method of cutting.
116 (cmd (concat (YaTeX-get-latex-command nil) " " YaTeX-texput-file))
117 (buffer (current-buffer)) opoint preamble
118 (region
119 (if (re-search-backward
120 "%#BEGIN" nil t)
121 (progn
122 (setq typeout "--- Region from BEGIN to " end "END ---")
123 (buffer-substring
124 (match-end 0)
125 (if (re-search-forward "%#END" nil t)
126 (match-beginning 0)
127 (setq end "end of buffer ---")
128 (point-max))))
129 (setq typeout "=== Region from (point) to (mark) ===")
130 (buffer-substring (point) (mark)))))
131 (YaTeX-visit-main t)
132 (setq opoint (point))
133 (goto-char (point-min))
134 (setq
135 preamble
136 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
137 (buffer-substring (point-min) (match-end 0))
138 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
139 "\\begin{document}")))
140 (goto-char opoint)
141 ;;(set-buffer buffer) ;for clarity
142 (find-file YaTeX-texput-file)
143 (erase-buffer)
144 (if YaTeX-need-nonstop
145 (insert "\\nonstopmode{}\n"))
146 (insert preamble "\n")
147 (insert region)
148 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
149 (insert "\n\\end{document}\n")
150 (basic-save-buffer)
151 (kill-buffer (current-buffer))
152 (YaTeX-visit-main t)
153 (YaTeX-typeset cmd YaTeX-typeset-buffer)
154 (put 'dvi2-command 'region t)))
155 )
157 (defun YaTeX-typeset-buffer ()
158 "Typeset whole buffer. If %#! usage says other buffer is main text,
159 visit main buffer to confirm if its includeonly list contains current
160 buffer's file. And if it doesn't contain editing text, ask user which
161 action want to be done, A:Add list, R:Replace list, %:comment-out list."
162 (interactive)
163 (YaTeX-save-buffers)
164 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
165 (mydir (file-name-directory (buffer-file-name)))
166 (cmd (YaTeX-get-latex-command t)))
167 (if (YaTeX-main-file-p) nil
168 (save-excursion
169 (YaTeX-visit-main t) ;search into main buffer
170 (save-excursion
171 (push-mark (point) t)
172 (goto-char (point-min))
173 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
174 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
175 (let*
176 ((b (progn (skip-chars-forward "^{") (point)))
177 (e (progn (skip-chars-forward "^}") (1+ (point))))
178 (s (buffer-substring b e)) c
179 (pardir (file-name-directory (buffer-file-name))))
180 (if (string-match (concat "[{,/]" me "[,}]") s)
181 nil ; Nothing to do when it's already in includeonly.
182 (ding)
183 (switch-to-buffer (current-buffer));Display this buffer.
184 (setq
185 me ;;Rewrite my name(me) to contain sub directory name.
186 (concat
187 (if (string-match pardir mydir) ;if mydir is child of main
188 (substring mydir (length pardir)) ;cut absolute path
189 mydir) ;else concat absolute path name.
190 me))
191 (message
192 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
193 me)
194 (setq c (read-char))
195 (cond
196 ((= c ?a)
197 (goto-char (1+ b))
198 (insert me (if (string= s "{}") "" ",")))
199 ((= c ?r)
200 (delete-region (1+ b) (1- e)) (insert me))
201 ((= c ?%)
202 (beginning-of-line) (insert "%"))
203 (t nil))
204 (basic-save-buffer))))
205 (exchange-point-and-mark))
206 ))
207 (YaTeX-typeset cmd YaTeX-typeset-buffer)
208 (put 'dvi2-command 'region nil))
209 )
211 (defun YaTeX-bibtex-buffer ()
212 "Pass the bibliography data of editing file to bibtex."
213 (interactive)
214 (YaTeX-save-buffers)
215 (YaTeX-typeset
216 (read-string "BibTeX command: "
217 (concat bibtex-command " " (YaTeX-get-preview-file-name)))
218 "*YaTeX-bibtex*" )
219 )
221 (defun YaTeX-kill-typeset-process (proc)
222 "Kill process PROC after sending signal to PROC.
223 PROC should be process identifier."
224 (cond
225 ((eq system-type 'ms-dos)
226 (error "MS-DOS can't have concurrent process."))
227 ((or (null proc) (not (eq (process-status proc) 'run)))
228 (error "No typesetting process."))
229 (t (interrupt-process proc)
230 (delete-process proc)))
231 )
233 (defun YaTeX-system (command buffer)
234 "Execute some command on buffer. Not a official function."
235 (save-excursion
236 (with-output-to-temp-buffer buffer
237 (if (eq system-type 'ms-dos)
238 (call-process shell-file-name nil buffer nil "/c " command)
239 (start-process "system" buffer shell-file-name "-c " command))))
240 )
242 (defun YaTeX-preview (preview-command preview-file)
243 "Execute xdvi (or other) to tex-preview."
244 (interactive
245 (list (read-string "Preview command: " dvi2-command)
246 (read-string "Prefiew file[.dvi]: "
247 ;;(substring (buffer-name) 0 -4)
248 (if (get 'dvi2-command 'region)
249 (substring YaTeX-texput-file
250 0 (rindex YaTeX-texput-file ?.))
251 (YaTeX-get-preview-file-name))
252 )))
253 (setq dvi2-command preview-command)
254 (save-excursion
255 (YaTeX-visit-main t)
256 (with-output-to-temp-buffer "*dvi-preview*"
257 (if (eq system-type 'ms-dos)
258 (progn (send-string-to-terminal "\e[2J") ;if MS-DOS
259 (call-process shell-file-name "con" "*dvi-preview*" nil
260 "/c " dvi2-command preview-file)
261 (redraw-display))
262 (start-process "preview" "*dvi-preview*" shell-file-name "-c"
263 (concat dvi2-command " " preview-file)) ;if UNIX
264 (message
265 (concat "Starting " dvi2-command " to preview " preview-file)))))
266 )
268 (defun YaTeX-prev-error ()
269 "Visit previous error. The reason why not NEXT-error is to
270 avoid make confliction of line numbers by editing."
271 (interactive)
272 (let ((cur-buf (buffer-name))
273 YaTeX-error-line error-buffer)
274 (if (null (get-buffer YaTeX-typeset-buffer))
275 (message "There is no output buffer of typesetting.")
276 (pop-to-buffer YaTeX-typeset-buffer)
277 (if (eq system-type 'ms-dos)
278 (if (search-backward latex-dos-emergency-message nil t)
279 (progn (goto-char (point-max))
280 (setq error-regexp latex-error-regexp))
281 (beginning-of-line)
282 (forward-char -1)
283 (setq error-regexp latex-warning-regexp))
284 (if YaTeX-typeset-process ; if jlatex on UNIX
285 (if (eq (process-status YaTeX-typeset-process) 'run)
286 (progn
287 (goto-char (point-max))
288 (setq error-regexp latex-error-regexp))
289 (beginning-of-line)
290 (setq error-regexp latex-warning-regexp))))
291 (if (re-search-backward error-regexp nil t)
292 (save-restriction
293 (set-mark-command nil)
294 (end-of-line)
295 (narrow-to-region (point) (mark))
296 (goto-char (point-min))
297 (re-search-forward "[0-9]")
298 (forward-char -1)
299 (set-mark (point))
300 (skip-chars-forward "0-9")
301 (narrow-to-region (point) (mark))
302 (goto-char (point-min))
303 (setq YaTeX-error-line (read (current-buffer))))
304 (message "No more error on %s" cur-buf)
305 (ding)
306 )
307 (setq error-buffer (YaTeX-get-error-file cur-buf))
308 (other-window -1)
309 (switch-to-buffer cur-buf)
310 (if (null YaTeX-error-line)
311 nil
312 ;; if warning or error found
313 (YaTeX-switch-to-buffer error-buffer)
314 (goto-line YaTeX-error-line)
315 (message "latex error or warning in '%s' at line: %d"
316 error-buffer YaTeX-error-line)
317 (other-window 1)
318 (skip-chars-backward "[0-9]")
319 (recenter (/ (window-height) 2))
320 (sit-for 3)
321 (forward-char -1)
322 (other-window -1)
323 )))
324 )
326 (defun YaTeX-jump-error-line ()
327 "Jump corresponding line on latex command's error message."
328 (interactive)
329 (let ((p (point))
330 (end (progn (end-of-line) (point)))
331 (begin (progn (beginning-of-line)(point))))
332 (if (null (re-search-forward "l[ ines]*\\.*[1-9][0-9]*" end t))
333 (if (save-excursion (end-of-line) (eobp))
334 (progn (goto-char p) (insert (this-command-keys)))
335 (message "No line number expression"))
336 (goto-char (match-beginning 0))
337 (re-search-forward "[1-9][0-9]*" end t)
338 (save-restriction
339 (let ((error-line
340 (string-to-int (buffer-substring (match-beginning 0)
341 (match-end 0))))
342 (error-file (YaTeX-get-error-file current-TeX-buffer)))
343 ;;(goto-char (match-beginning 0))
344 (other-window -1)
345 (message "errors in %s" error-file)
346 ;(switch-to-buffer current-TeX-buffer)
347 (if (not (YaTeX-switch-to-buffer error-file))
348 (error "%s is not found in this directory."))
349 (goto-line error-line)))))
350 )
352 (defun YaTeX-send-string ()
353 "Send string to current typeset process."
354 (interactive)
355 (if (and (eq (process-status YaTeX-typeset-process) 'run)
356 (>= (point) (process-mark YaTeX-typeset-process)))
357 (let ((b (process-mark YaTeX-typeset-process))
358 (e (point-end-of-line)))
359 (goto-char b)
360 (skip-chars-forward " \t" e)
361 (setq b (point))
362 (process-send-string
363 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
364 (goto-char e)
365 (insert "\n")
366 (set-marker (process-mark YaTeX-typeset-process) (point))
367 (insert " "))
368 (ding))
369 )
371 (defun YaTeX-view-error ()
372 (interactive)
373 (if (null (get-buffer YaTeX-typeset-buffer))
374 (message "No typeset buffer found.")
375 (pop-to-buffer YaTeX-typeset-buffer)
376 (goto-char (point-max))
377 (recenter -1)
378 (other-window -1))
379 )
381 (defun YaTeX-get-error-file (default)
382 "Get current processing file from typesetting log."
383 (save-excursion
384 (let(s)
385 (condition-case () (up-list -1)
386 (error
387 (let ((list 0) found)
388 (while
389 (and (<= list 0) (not found)
390 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
391 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
392 (setq list (1- list)) ;open paren
393 (setq list (1+ list))
394 (if (= list 1)
395 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
396 (setq found t)
397 (setq list (1- list)))))))))
398 (setq s
399 (buffer-substring
400 (progn (forward-char 1) (point))
401 (progn (skip-chars-forward "-A-Za-z0-9_/\.\\" (point-end-of-line))
402 (point))))
403 (if (string= "" s) default s)))
404 )
406 (defun YaTeX-put-nonstopmode ()
407 (if YaTeX-need-nonstop
408 (if (re-search-backward "\\nonstopmode{}" (point-min) t)
409 nil ;if already written in text then do nothing
410 (save-excursion
411 (YaTeX-visit-main t)
412 (goto-char (point-min))
413 (insert "\\nonstopmode{}%_YaTeX_%\n")))
414 )
415 )
417 (defun YaTeX-remove-nonstopmode ()
418 (if YaTeX-need-nonstop ;for speed
419 (save-excursion
420 (YaTeX-visit-main t)
421 (goto-char (point-min))
422 (forward-line 1)
423 (narrow-to-region (point-min) (point))
424 (goto-char (point-min))
425 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
426 (widen)))
427 )
429 (defun YaTeX-get-preview-file-name ()
430 "Get file name to preview by inquiring YaTeX-get-latex-command"
431 (let* ((latex-cmd (YaTeX-get-latex-command t))
432 (rin (rindex latex-cmd ? ))
433 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
434 (period))
435 (if (string= fname "")
436 (setq fname (substring (buffer-name) 0 -4))
437 (setq period (rindex fname ?.))
438 (setq fname (substring fname 0 (if (eq -1 period) nil period)))
439 ))
440 )
442 (defun YaTeX-get-latex-command (&optional switch)
443 "Specify the latex-command name and its argument.
444 If there is a line which begins by string: \"%#!\", the following
445 strings are assumed to be the latex-command and arguments. The
446 default value of latex-command is:
447 tex-command (buffer-name)
448 and if you write \"%#!jlatex\" in the beginning of certain line.
449 \"jlatex \" (buffer-name)
450 will be the latex-command,
451 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
452 is t, then
453 \"jlatex main.tex\"
454 will be given to the shell."
455 (let*
456 ((default-command
457 (concat tex-command " "
458 (if switch (buffer-name) ""))));default value
459 (save-excursion
460 (goto-char (point-min))
461 (if (null (re-search-forward "^%#!" (point-max) t))
462 default-command
463 (skip-chars-forward "%#! ")
464 (if (eolp)
465 default-command
466 (let ((s (point)))
467 (skip-chars-forward "^ " (point-end-of-line)) ;Skip command
468 (skip-chars-forward " " (point-end-of-line))
469 ;(setq YaTeX-latex-command (buffer-substring s (point)))
470 (cond
471 ((null switch)
472 (buffer-substring s (point)))
473 ((eolp) ;Only return command name
474 (concat (buffer-substring s (point)) " " (buffer-name)))
475 (t(end-of-line) ;Change entire command name
476 (buffer-substring s (point))) ;including arguments.
477 ))
478 ))))
479 )
481 (defun YaTeX-replace-format (string format repl)
482 "In STRING, replace first appearance of FORMAT to REPL as if
483 function `format' does. FORMAT does not contain `%'"
484 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
485 (string-match (concat "[^%]\\(%" format "\\)") string)))
486 (len (length format)))
487 (if (null beg) string ;no conversion
488 (concat
489 (substring string 0 (match-beginning 1)) repl
490 (substring string (match-end 1)))))
491 )
493 (defun YaTeX-lpr (arg)
494 "Print out. If prefix arg ARG is non nil, call print driver without
495 page range description."
496 (interactive "P")
497 (let*(from to (cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format)))
498 (setq
499 cmd
500 (YaTeX-replace-format
501 cmd "f"
502 (if (or arg (not (string-match "%f" cmd)))
503 ""
504 (YaTeX-replace-format
505 dviprint-from-format
506 "b"
507 (if (string=
508 (setq from (read-string "From page(default 1): ")) "")
509 "1" from))))
510 )
511 (setq
512 cmd
513 (YaTeX-replace-format
514 cmd "t"
515 (if (or arg (not (string-match "%t" cmd))
516 (string=
517 (setq to (read-string "To page(default none): ")) ""))
518 ""
519 (YaTeX-replace-format dviprint-to-format "e" to)))
520 )
521 (setq cmd (read-string "Edit command line: "
522 (format cmd (YaTeX-get-preview-file-name))))
523 (save-excursion
524 (YaTeX-visit-main t) ;;change execution directory
525 (with-output-to-temp-buffer "*dvi-printing*"
526 (if (eq system-type 'ms-dos)
527 (call-process shell-file-name "con" "*dvi-printing*" nil
528 "/c " cmd)
529 (start-process "print" "*dvi-printing*" shell-file-name "-c" cmd)
530 (message (concat "Starting " cmd " to printing "
531 (YaTeX-get-preview-file-name))))
532 )))
533 )
535 (defun YaTeX-main-file-p ()
536 "Return if current buffer is main LaTeX source."
537 (string-match (concat "^" (YaTeX-get-preview-file-name) ".tex")(buffer-name))
538 )
540 (defun YaTeX-visit-main (&optional setbuf)
541 "Switch to buffer main LaTeX source. Use set-buffer instead of
542 switch-to-buffer if optional second argument SETBUF is t(Use it only
543 in Emacs-Lisp program)."
544 (interactive)
545 (let ((main-file (YaTeX-get-preview-file-name)))
546 (if (string-match (concat "^" main-file ".tex") (buffer-name))
547 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
548 (cond
549 ((YaTeX-switch-to-buffer (setq main-file (concat main-file ".tex"))
550 setbuf))
551 ((and (file-exists-p (setq main-file (concat "../" main-file)))
552 (y-or-n-p (concat (expand-file-name main-file)
553 " is main file?:")))
554 (YaTeX-switch-to-buffer main-file setbuf))
555 (t (find-file (read-file-name "Enter your main text: " nil nil 1)))
556 )))
557 nil
558 )
560 (defun YaTeX-visit-main-other-window ()
561 "Switch to buffer main LaTeX source in other window."
562 (interactive)
563 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
564 (YaTeX-switch-to-buffer-other-window
565 (concat (YaTeX-get-preview-file-name) ".tex")))
566 )
568 (defun YaTeX-get-builtin (key)
569 "Read source built-in command of %# usage."
570 (save-excursion
571 (goto-char (point-min))
572 (if (and (search-forward (concat "%#" key) nil t)
573 (not (eolp)))
574 (buffer-substring
575 (progn (skip-chars-forward " " (point-end-of-line))(point))
576 (point-end-of-line))
577 nil))
578 )
580 (defun YaTeX-save-buffers ()
581 "Save buffers which is in yatex-mode."
582 (basic-save-buffer)
583 (save-excursion
584 (mapcar '(lambda (buf)
585 (set-buffer buf)
586 (if (and (buffer-file-name buf)
587 (eq major-mode 'yatex-mode)
588 (buffer-modified-p buf)
589 (y-or-n-p (format "Save %s" (buffer-name buf))))
590 (save-buffer buf)))
591 (buffer-list)))
592 )
594 (provide 'yatexprc)