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