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