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