yatex

view yatexlib.el @ 624:0018833906a1

merged
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 01 Aug 2026 13:57:31 +0900
parents 38a414d755c1
children
line source
1 ;;; yatexlib.el --- YaTeX and yahtml common libraries -*- coding: sjis; lexical-binding: nil -*-
2 ;;;
3 ;;; (c)1994-2026 by HIROSE Yuuji.[yuuji@yatex.org]
4 ;;; Last modified Sat Aug 1 13:14:19 2026 on firestorm
5 ;;; $Id$
7 ;;; Code:
9 ;; High-precedence compatible function
10 (fset 'YaTeX-str2int
11 (if (fboundp 'string-to-number)
12 (function
13 (lambda (string &optional base)
14 (ceiling (string-to-number string base))))
15 'string-to-int))
17 ;; General variables
18 (defvar YaTeX-dos (memq system-type '(ms-dos windows-nt OS/2)))
19 (defvar YaTeX-macos (memq system-type '(darwin)))
20 (defvar YaTeX-emacs-19 (>= (YaTeX-str2int emacs-version) 19))
21 (defvar YaTeX-emacs-20 (>= (YaTeX-str2int emacs-version) 20))
22 (defvar YaTeX-emacs-21 (>= (YaTeX-str2int emacs-version) 21))
23 (defvar YaTeX-user-completion-table
24 (if YaTeX-dos "~/_yatexrc" "~/.yatexrc")
25 "*Default filename in which user completion table is saved.")
27 (defvar YaTeX-display-color-p
28 (or (and (fboundp 'display-color-p) (display-color-p))
29 (and (fboundp 'device-class)
30 (eq 'color (device-class (selected-device))))
31 window-system) ; falls down lazy check..
32 "Current display's capability of expressing colors.")
34 (defvar YaTeX-japan
35 (or (boundp 'NEMACS)
36 (boundp 'MULE)
37 (and (boundp 'current-language-environment)
38 (string-match "[Jj]apanese" current-language-environment)))
39 "Whether yatex mode is running on Japanese environment or not.")
41 ;; autoload from yahtml.el
42 (autoload 'yahtml-inner-environment-but "yahtml" "yahtml internal func." t)
44 (defvar latex-message-kanji-code 2
45 "*Kanji coding system latex command types out.
46 1 = Shift JIS, 2 = JIS, 3 = EUC. 4 = UTF-8")
48 (defvar YaTeX-kanji-code-alist
49 (cond
50 ((boundp '*junet*)
51 (list '(0 . *noconv*)
52 (cons
53 1
54 (cond
55 (YaTeX-dos (if (boundp '*sjis-dos*) *sjis-dos* *sjis*dos))
56 (YaTeX-macos (if (boundp '*sjis-mac*) *sjis-mac* *sjis*mac))
57 (t *sjis*)))
58 '(2 . *junet*) '(3 . *euc-japan*)))
59 ((and YaTeX-emacs-20 (featurep 'mule))
60 ;;(cdr-safe(assq 'coding-system (assoc "Japanese" language-info-alist)))
61 (list '(0 . no-conversion)
62 (cons
63 1 (cond (YaTeX-dos 'shift_jis-dos)
64 (YaTeX-macos 'shift_jis-mac)
65 ((member 'shift_jis (coding-system-list)) 'shift_jis-unix)
66 (t 'sjis)))
67 '(2 . iso-2022-jp-unix)
68 '(3 . euc-jp-unix)
69 '(4 . utf-8))))
70 "Kanji-code expression translation table.")
71 (defvar YaTeX-inhibit-prefix-letter nil
72 "*T for changing key definitions from [prefix] Letter to [prefix] C-Letter.")
74 (defvar YaTeX-no-begend-shortcut nil
75 "*T for disabling shortcut of begin-type completion, [prefix] b d, etc.")
77 (defvar YaTeX-default-pop-window-height 10
78 "Default typesetting buffer height.
79 If integer, sets the window-height of typesetting buffer.
80 If string, sets the percentage of it.
81 If nil, use default pop-to-buffer.")
83 (defvar YaTeX-create-file-prefix-g nil
84 "*Non-nil creates new file when [prefix] g on \\include{foo}.")
86 (defvar YaTeX-nervous t
87 "*If you are nervous about maintenance of yatexrc, set this value to T.
88 And you will have the local dictionary.")
90 (defvar YaTeX-use-italic-bold (string< "20" emacs-version)
91 "*Non-nil tries to find italic/bold fontset.
92 This variable is effective when font-lock is used.
93 \\it, \\bf 内部での日本語が□になってしまう場合はこれをnilにして下さい。")
95 ;----------- work variables ----------------------------------------
96 (defvar YaTeX-minibuffer-completion-map nil
97 "Minibuffer completion key map that allows comma completion.")
98 (if YaTeX-minibuffer-completion-map nil
99 (setq YaTeX-minibuffer-completion-map
100 (copy-keymap minibuffer-local-completion-map))
101 (define-key YaTeX-minibuffer-completion-map " "
102 'YaTeX-minibuffer-complete)
103 (define-key YaTeX-minibuffer-completion-map "\t"
104 'YaTeX-minibuffer-complete))
106 (defvar YaTeX-typesetting-mode-map nil
107 "Keymap used in YaTeX typesetting buffer")
109 (if YaTeX-typesetting-mode-map nil
110 (setq YaTeX-typesetting-mode-map (make-keymap))
111 ;(suppress-keymap YaTeX-typesetting-mode-map t)
112 (define-key YaTeX-typesetting-mode-map " " 'YaTeX-jump-error-line)
113 (define-key YaTeX-typesetting-mode-map "\C-m" 'YaTeX-send-string)
114 (define-key YaTeX-typesetting-mode-map "1" 'delete-other-windows)
115 (define-key YaTeX-typesetting-mode-map "0" 'delete-window)
116 (define-key YaTeX-typesetting-mode-map "q" 'delete-window))
118 (defvar YaTeX-parent-file nil
119 "*Main LaTeX source file name used when %#! expression doesn't exist.")
120 (make-variable-buffer-local 'YaTeX-parent-file)
122 ;;;
123 ;; Emacs 21 compensational wrapper
124 ;;;
125 (defun YaTeX-minibuffer-begin ()
126 (if (fboundp 'field-beginning)
127 (field-beginning (point-max))
128 (point-min)))
130 (defun YaTeX-minibuffer-end ()
131 (if (fboundp 'field-end)
132 (field-end (point-max))
133 (point-max)))
135 (defun YaTeX-minibuffer-string ()
136 (buffer-substring (YaTeX-minibuffer-begin) (YaTeX-minibuffer-end)))
138 (defun YaTeX-minibuffer-erase ()
139 (if (eq (selected-window) (minibuffer-window))
140 (if (fboundp 'delete-field) (delete-field) (erase-buffer))))
142 (fset 'YaTeX-buffer-substring
143 (if (fboundp 'buffer-substring-no-properties)
144 'buffer-substring-no-properties
145 'buffer-substring))
146 (fset 'YaTeX-substring
147 (if (fboundp 'substring-no-properties)
148 'substring-no-properties
149 'substing))
151 (defun YaTeX-region-active-p ()
152 (and (fboundp 'region-active-p) (region-active-p)))
154 ;---------- Define default key bindings on YaTeX mode map ----------
155 ;;;###autoload
156 (defun YaTeX-kanji-ptex-mnemonic ()
157 "Return the kanji-mnemonic of pTeX from current buffer's coding-system."
158 (if (boundp 'NEMACS)
159 (or (cdr-safe (assq kanji-fileio-code
160 '((1 . "sjis") (2 . "jis") (3 . "euc"))))
161 "")
162 (let ((coding
163 (cond
164 ((boundp 'buffer-file-coding-system)
165 (symbol-name (if (fboundp 'coding-system-name)
166 (coding-system-name buffer-file-coding-system)
167 buffer-file-coding-system)))
168 ((boundp 'file-coding-system) (symbol-name file-coding-system))))
169 (case-fold-search t))
170 (cond ((string-match "utf-8\\>" coding) "utf8")
171 ((string-match "shift.jis\\|cp932\\>" coding) "sjis")
172 ((string-match "junet\\|iso.2022" coding) "jis")
173 ((string-match "euc.jp\\|ja.*iso.8bit" coding) "euc")
174 (t "")))))
176 ;;;###autoload
177 (defun YaTeX-define-key (key binding &optional map)
178 "Define key on YaTeX-prefix-map."
179 (if YaTeX-inhibit-prefix-letter
180 (let ((c (aref key 0)))
181 (cond
182 ((and (>= c ?a) (<= c ?z)) (aset key 0 (1+ (- c ?a))))
183 ((and (>= c ?A) (<= c ?Z) (numberp YaTeX-inhibit-prefix-letter))
184 (aset key 0 (1+ (- c ?A))))
185 (t nil))))
186 (define-key (or map YaTeX-prefix-map) key binding))
188 ;;;###autoload
189 (defun YaTeX-local-table-symbol (symbol)
190 "Return the lisp symbol which keeps local completion table of SYMBOL."
191 (intern (concat "YaTeX$"
192 default-directory
193 (symbol-name symbol))))
195 ;;;###autoload
196 (defun YaTeX-sync-local-table (symbol)
197 "Synchronize local variable SYMBOL.
198 Copy its corresponding directory dependent completion table to SYMBOL."
199 (if (boundp (YaTeX-local-table-symbol symbol))
200 (set symbol (symbol-value (YaTeX-local-table-symbol symbol)))))
202 (defvar YaTeX-user-table-is-read nil
203 "Flag that means whether user completion table has been read or not.")
204 ;;;###autoload
205 (defun YaTeX-read-user-completion-table (&optional forcetoread)
206 "Append user completion table of LaTeX macros"
207 (interactive)
208 (let*((user-table (expand-file-name YaTeX-user-completion-table))
209 (local-table (expand-file-name (file-name-nondirectory user-table)))
210 var localvar localbuf (curbuf (current-buffer)) sexp
211 (warning-suppress-log-types '((files missing-lexbind-cookie))))
212 (if YaTeX-user-table-is-read nil
213 (message "Loading user completion table")
214 (if (file-exists-p user-table) (load-file user-table)
215 (message "Welcome to the field of YaTeX. I'm glad to see you!")))
216 (setq YaTeX-user-table-is-read t)
217 (cond
218 ((file-exists-p local-table)
219 (set-buffer (setq localbuf (find-file-noselect local-table)))
220 (widen)
221 (goto-char (point-min))
222 (while (re-search-forward "(setq \\([^ \t\n]+\\)" nil t)
223 (setq var (intern (buffer-substring
224 (match-beginning 1) (match-end 1)))
225 localvar (YaTeX-local-table-symbol var))
226 (goto-char (match-beginning 0))
227 (setq sexp (buffer-substring (point)
228 (progn (forward-sexp) (point))))
229 (set-buffer curbuf)
230 (or (assq var (buffer-local-variables)) (make-local-variable var))
231 (eval (read sexp))
232 (or (and (boundp localvar)
233 (symbol-value localvar)
234 (not forcetoread))
235 (set localvar (symbol-value var)))
236 (set-buffer localbuf))
237 (kill-buffer localbuf)))
238 (set-buffer curbuf)))
240 ;;;###autoload
241 (defun YaTeX-reload-dictionary ()
242 "Reload local dictionary.
243 Use this function after editing ./.yatexrc."
244 (interactive)
245 (let ((YaTeX-user-table-is-read nil))
246 (YaTeX-read-user-completion-table t)))
248 ;;;###autoload
249 (defun YaTeX-lookup-table (word type)
250 "Lookup WORD in completion table whose type is TYPE.
251 This function refers the symbol tmp-TYPE-table, user-TYPE-table, TYPE-table.
252 Typically, TYPE is one of 'env, 'section, 'fontsize, 'singlecmd."
253 (if (symbolp type) (setq type (symbol-name type)))
254 (or (assoc word (symbol-value (intern (concat "tmp-" type "-table"))))
255 (assoc word (symbol-value (intern (concat "user-" type "-table"))))
256 (assoc word (symbol-value (intern (concat type "-table"))))))
258 ;;;###autoload
259 (defun YaTeX-update-table (vallist default-table user-table local-table)
260 "Update completion table if the car of VALLIST is not in current tables.
261 Second argument DEFAULT-TABLE is the quoted symbol of default completion
262 table, third argument USER-TABLE is user table which will be saved in
263 YaTeX-user-completion-table, fourth argument LOCAL-TABLE should have the
264 completion which is valid during current Emacs's session. If you
265 want to make LOCAL-TABLE valid longer span (but restrict in this directory)
266 create the file in current directory which has the same name with
267 YaTeX-user-completion-table."
268 (let ((car-v (car vallist)) key answer
269 (file (file-name-nondirectory YaTeX-user-completion-table)))
270 (cond
271 ((assoc car-v (symbol-value default-table))
272 nil) ;Nothing to do
273 ((setq key (assoc car-v (symbol-value user-table)))
274 (if (equal (cdr vallist) (cdr key)) nil
275 ;; if association hits, but contents differ.
276 (message
277 "%s's attributes turned into %s" (car vallist) (cdr vallist))
278 (set user-table (delq key (symbol-value user-table)))
279 (set user-table (cons vallist (symbol-value user-table)))
280 (YaTeX-update-dictionary
281 YaTeX-user-completion-table user-table "user")))
282 ((setq key (assoc car-v (symbol-value local-table)))
283 (if (equal (cdr vallist) (cdr key)) nil
284 (message
285 "%s's attributes turned into %s" (car vallist) (cdr vallist))
286 (set local-table (delq key (symbol-value local-table)))
287 (set local-table (cons vallist (symbol-value local-table)))
288 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
289 (YaTeX-update-dictionary file local-table)))
290 ;; All of above cases, there are some completion in tables.
291 ;; Then update tables.
292 (t
293 (if (not YaTeX-nervous)
294 (setq answer "u")
295 (message
296 (cond
297 (YaTeX-japan
298 "`%s'の登録先: U)ユーザ辞書 L)ローカル辞書 N)メモリ D)しない")
299 (t
300 "Register `%s' into: U)serDic L)ocalDic N)one D)iscard"))
301 (if (> (length car-v) 23)
302 (concat (substring car-v 0 10) "..." (substring car-v -9))
303 car-v))
304 (setq answer (char-to-string (read-char))))
305 (cond
306 ((string-match answer "uy")
307 (set user-table (cons vallist (symbol-value user-table)))
308 (YaTeX-update-dictionary YaTeX-user-completion-table user-table "user")
309 )
310 ((string-match answer "tl")
311 (set local-table (cons vallist (symbol-value local-table)))
312 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
313 (YaTeX-update-dictionary file local-table))
314 ((string-match answer "d") nil) ;discard it
315 (t (set default-table
316 (cons vallist (symbol-value default-table)))))))))
318 ;;;###autoload
319 (defun YaTeX-cplread-with-learning
320 (prom default-table user-table local-table
321 &optional pred reqmatch init hsym)
322 "Completing read with learning.
323 Do a completing read with prompt PROM. Completion table is what
324 DEFAULT-TABLE, USER-TABLE, LOCAL table are appended in reverse order.
325 Note that these tables are passed by the symbol.
326 Optional arguments PRED, REQMATH and INIT are passed to completing-read
327 as its arguments PREDICATE, REQUIRE-MATCH and INITIAL-INPUT respectively.
328 If optional 8th argument HSYM, history symbol, is passed, use it as
329 history list variable."
330 (YaTeX-sync-local-table local-table)
331 (let*((table (append (symbol-value local-table)
332 (symbol-value user-table)
333 (symbol-value default-table)))
334 (word (completing-read-with-history
335 prom table pred reqmatch init hsym)))
336 (if (and (string< "" word) (not (assoc word table)))
337 (YaTeX-update-table (list word) default-table user-table local-table))
338 word))
340 ;;;###autoload
341 (defun YaTeX-update-dictionary (file symbol &optional type)
342 (let ((local-table-buf (find-file-noselect file))
343 (name (symbol-name symbol))
344 (value (symbol-value symbol)))
345 (save-excursion
346 (message "Updating %s dictionary..." (or type "local"))
347 (set-buffer local-table-buf)
348 (goto-char (point-max))
349 (search-backward (concat "(setq " name) nil t)
350 (delete-region (point) (progn (forward-sexp) (point)))
351 (delete-blank-lines)
352 (insert "(setq " name " '(\n")
353 (mapcar (function (lambda (s)
354 (insert (format "%s\n" (prin1-to-string s)))))
355 value)
356 (insert "))\n\n")
357 (delete-blank-lines)
358 (basic-save-buffer)
359 (kill-buffer local-table-buf)
360 (message "Updating %s dictionary...Done" (or type "local")))))
362 ;;;###autoload
363 (defun YaTeX-define-begend-key-normal (key env &optional map)
364 "Define short cut YaTeX-make-begin-end key."
365 (YaTeX-define-key
366 key
367 (list 'lambda '(arg) '(interactive "P")
368 (list 'YaTeX-insert-begin-end env 'arg))
369 map))
371 ;;;###autoload
372 (defun YaTeX-define-begend-region-key (key env &optional map)
373 "Define short cut YaTeX-make-begin-end-region key."
374 (YaTeX-define-key key (list 'lambda nil '(interactive)
375 (list 'YaTeX-insert-begin-end env t)) map))
377 ;;;###autoload
378 (defun YaTeX-define-begend-key (key env &optional map)
379 "Define short cut key for begin type completion.
380 Define both strokes for normal and region mode.
381 To customize YaTeX, user should use this function."
382 (YaTeX-define-begend-key-normal key env map)
383 (if YaTeX-inhibit-prefix-letter nil
384 (YaTeX-define-begend-region-key
385 (concat (upcase (substring key 0 1)) (substring key 1)) env)))
387 ;;;###autoload
388 (defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func)
389 "Search STRING which is not commented out by CMNTRX.
390 Optional arguments after BOUND, ERR, CNT are passed literally to search-forward
391 or search-backward.
392 Optional sixth argument FUNC changes search-function."
393 (let ((sfunc (or func 'search-forward)) found md)
394 (while (and (prog1
395 (setq found (funcall sfunc string bound err cnt))
396 (setq md (match-data)))
397 (or
398 (and (eq major-mode 'yatex-mode)
399 (YaTeX-in-verb-p (match-beginning 0)))
400 (save-excursion
401 (goto-char (match-beginning 0))
402 (beginning-of-line)
403 (re-search-forward cmntrx (match-beginning 0) t)))))
404 (store-match-data md)
405 found))
407 (defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt)
408 "Search REGEXP backward which is not commented out by regexp CMNTRX.
409 See also YaTeX-search-active-forward."
410 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward))
412 (defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt)
413 "Search STRING backward which is not commented out by regexp CMNTRX.
414 See also YaTeX-search-active-forward."
415 (YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward))
417 (defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt)
418 "Search REGEXP backward which is not commented out by regexp CMNTRX.
419 See also YaTeX-search-active-forward."
420 (YaTeX-search-active-forward
421 regexp cmntrx bound err cnt 're-search-backward))
423 (defun YaTeX-relative-path-p (path)
424 "Return non-nil if PATH is not absolute one."
425 (let ((md (match-data)))
426 (unwind-protect
427 (not (string-match "^\\(/\\|[a-z]:\\|\\\\\\).*/" file))
428 (store-match-data md))))
430 ;;;###autoload
431 (defun YaTeX-switch-to-buffer (file &optional setbuf)
432 "Switch to buffer if buffer exists, find file if not.
433 Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer."
434 (interactive "Fswitch to file: ")
435 (if (bufferp file)
436 (setq file (buffer-file-name file))
437 (and (YaTeX-relative-path-p file)
438 (eq major-mode 'yatex-mode)
439 YaTeX-search-file-from-top-directory
440 (save-excursion
441 (YaTeX-visit-main t)
442 (setq file (expand-file-name file)))))
443 (let (buf (hilit-auto-highlight (not setbuf)))
444 (cond
445 ((setq buf (get-file-buffer file))
446 (funcall (if setbuf 'set-buffer 'switch-to-buffer)
447 (get-file-buffer file))
448 buf)
449 ((or YaTeX-create-file-prefix-g (file-exists-p file))
450 (or ;find-file returns nil but set current-buffer...
451 (if setbuf (set-buffer (find-file-noselect file))
452 (find-file file))
453 (current-buffer)))
454 (t (message "%s was not found in this directory." file)
455 nil))))
457 ;;;###autoload
458 (defun YaTeX-switch-to-buffer-other-window (file)
459 "Switch to buffer if buffer exists, find file if not."
460 (interactive "Fswitch to file: ")
461 (and (eq major-mode 'yatex-mode)
462 (stringp file)
463 (YaTeX-relative-path-p file)
464 YaTeX-search-file-from-top-directory
465 (save-excursion
466 (YaTeX-visit-main t)
467 (setq file (expand-file-name file))))
468 (if (bufferp file) (setq file (buffer-file-name file)))
469 (cond
470 ((get-file-buffer file)
471 (switch-to-buffer-other-window (get-file-buffer file))
472 t)
473 ((or YaTeX-create-file-prefix-g (file-exists-p file))
474 (find-file-other-window file) t)
475 (t (message "%s was not found in this directory." file)
476 nil)))
478 (defun YaTeX-get-file-buffer (file)
479 "Return the FILE's buffer.
480 Base directory is that of main file or current directory."
481 (let (dir main (cdir default-directory))
482 (or (and (eq major-mode 'yatex-mode)
483 YaTeX-search-file-from-top-directory
484 (save-excursion
485 (YaTeX-visit-main t)
486 (get-file-buffer file)))
487 (get-file-buffer file))))
489 (defun YaTeX-replace-format-sub (string format repl)
490 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
491 (string-match (concat "[^%]\\(%" format "\\)") string)))
492 (len (length format)))
493 (if (null beg) string ;no conversion
494 (concat
495 (substring string 0 (match-beginning 1)) (or repl "")
496 (substring string (match-end 1))))))
498 ;;;###autoload
499 (defun YaTeX-replace-format (string format repl)
500 "In STRING, replace first appearance of FORMAT to REPL as if
501 function `format' does. FORMAT does not contain `%'"
502 (let ((ans string) (case-fold-search nil))
503 (while (not (string=
504 ans (setq string (YaTeX-replace-format-sub ans format repl))))
505 (setq ans string))
506 string))
508 ;;;###autoload
509 (defun YaTeX-replace-formats (string replace-list)
510 (let ((list replace-list))
511 (while list
512 (setq string (YaTeX-replace-format
513 string (car (car list)) (cdr (car list)))
514 list (cdr list)))
515 string))
517 ;;;###autoload
518 (defun YaTeX-replace-format-args (string &rest args)
519 "Translate the argument mark #1, #2, ... #n in the STRING into the
520 corresponding real arguments ARGS."
521 (let ((argp 1))
522 (while args
523 (setq string
524 (YaTeX-replace-format string (int-to-string argp) (car args)))
525 (setq args (cdr args) argp (1+ argp))))
526 string)
528 ;;;###autoload
529 (defun rindex (string char)
530 "Return the last position of STRING where character CHAR found."
531 (let ((pos (1- (length string)))(index -1))
532 (catch 'rindex
533 (while (>= pos 0)
534 (cond
535 ((= (aref string pos) char)
536 (throw 'rindex pos))
537 (t (setq pos (1- pos))))))))
539 ;;;###autoload
540 (defun point-beginning-of-line ()
541 (save-excursion (beginning-of-line)(point)))
543 ;;;###autoload
544 (defun point-end-of-line ()
545 (save-excursion (end-of-line)(point)))
547 (defun YaTeX-showup-buffer-bottom-most (x) (nth 3 (window-edges x)))
548 ;;;###autoload
549 (defun YaTeX-showup-buffer (buffer &optional func select)
550 "Make BUFFER show up in certain window (but current window)
551 that gives the maximum value by the FUNC. FUNC should take an argument
552 of its window object. Non-nil for optional third argument SELECT selects
553 that window. This function never selects minibuffer window."
554 (or (and (if (and YaTeX-emacs-19 select window-system)
555 (get-buffer-window buffer t)
556 (get-buffer-window buffer))
557 (progn
558 (if select
559 (goto-buffer-window buffer))
560 t))
561 (let ((window (selected-window))
562 (wlist (YaTeX-window-list)) win w (x 0))
563 (cond
564 ((> (length wlist) 2)
565 (if func
566 (while wlist
567 (setq w (car wlist))
568 (if (and (not (eq window w))
569 (> (funcall func w) x))
570 (setq win w x (funcall func w)))
571 (setq wlist (cdr wlist)))
572 (setq win (get-lru-window)))
573 (select-window win)
574 (switch-to-buffer buffer)
575 (or select (select-window window)))
576 ((= (length wlist) 2)
577 ;(other-window 1);This does not work properly on Emacs-19
578 (select-window (get-lru-window))
579 (switch-to-buffer buffer)
580 (if (< (window-height) (/ YaTeX-default-pop-window-height 2))
581 (enlarge-window (- YaTeX-default-pop-window-height
582 (window-height))))
583 (or select (select-window window)))
584 (t ;if one-window
585 (cond
586 ((and YaTeX-emacs-19 window-system (get-buffer-window buffer t))
587 nil) ;if found in other frame
588 (YaTeX-default-pop-window-height
589 (split-window-calculate-height YaTeX-default-pop-window-height)
590 ;;(pop-to-buffer buffer) ;damn! emacs-19.30
591 (select-window (next-window nil 1))
592 (switch-to-buffer (get-buffer-create buffer))
593 (or select (select-window window)))
594 (t nil)))
595 ))))
597 (cond
598 ((fboundp 'screen-height)
599 (fset 'YaTeX-screen-height 'screen-height)
600 (fset 'YaTeX-screen-width 'screen-width)
601 (fset 'YaTeX-set-screen-height 'set-screen-height)
602 (fset 'YaTeX-set-screen-width 'set-screen-width))
603 ((fboundp 'frame-height)
604 (fset 'YaTeX-screen-height 'frame-height)
605 (fset 'YaTeX-screen-width 'frame-width)
606 (fset 'YaTeX-set-screen-height 'set-frame-height)
607 (fset 'YaTeX-set-screen-width 'set-frame-width))
608 (t (error "I don't know how to run YaTeX on this Emacs...")))
610 ;;;###autoload
611 (defun split-window-calculate-height (height)
612 "Split current window wight specified HEIGHT.
613 If HEIGHT is number, make a new window that has HEIGHT lines.
614 If HEIGHT is string, make a new window that occupies HEIGT % of screen height.
615 Otherwise split window conventionally."
616 (if (one-window-p t)
617 (split-window
618 (selected-window)
619 (max
620 (min
621 (- (YaTeX-screen-height)
622 (if (numberp height)
623 (+ height 2)
624 (/ (* (YaTeX-screen-height)
625 (YaTeX-str2int height))
626 100)))
627 (- (YaTeX-screen-height) window-min-height 1))
628 window-min-height))))
630 ;;;###autoload
631 (defun YaTeX-window-list ()
632 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
633 (while (not (eq curw (setq win (next-window win))))
634 (or (eq win (minibuffer-window))
635 (setq wlist (cons win wlist))))
636 wlist))
638 (if YaTeX-emacs-21
639 ;; Emacs-21's next-window returns other frame's window even if called
640 ;; with argument ALL-FRAMES nil, when called from minibuffer context.
641 ;; Therefore, check frame identity here.
642 (defun YaTeX-window-list ()
643 (let*((curw (selected-window)) (win curw) (wlist (list curw))
644 (curf (window-frame curw)))
645 (while (and (not (eq curw (setq win (next-window win))))
646 (eq curf (window-frame win)))
647 (or (eq win (minibuffer-window))
648 (setq wlist (cons win wlist))))
649 wlist)))
651 ;;;###autoload
652 (defun substitute-all-key-definition (olddef newdef keymap)
653 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
654 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
655 where ever it appears."
656 (if YaTeX-emacs-19
657 (substitute-key-definition olddef newdef keymap global-map)
658 (mapcar
659 (function (lambda (key) (define-key keymap key newdef)))
660 (where-is-internal olddef keymap))))
662 ;;;###autoload
663 (defun YaTeX-match-string (n &optional m str)
664 "Return (buffer-substring (match-beginning n) (match-beginning m)).
665 Optional third argument STR gives substring from string STR."
666 (if (match-beginning n)
667 (if str (YaTeX-substring
668 str (match-beginning m) (match-end (or m n)))
669 (YaTeX-buffer-substring (match-beginning n)
670 (match-end (or m n))))))
672 ;;;###autoload
673 (defun YaTeX-minibuffer-complete ()
674 "Complete in minibuffer.
675 If the symbol 'delim is bound and is string, its value is assumed to be
676 the character class of delimiters. Completion will be performed on
677 the last field separated by those delimiters.
678 If the symbol 'quick is bound and is 't, when the try-completion results
679 in t, exit minibuffer immediately."
680 (interactive)
681 (save-restriction
682 (narrow-to-region
683 (if (fboundp 'field-beginning) (field-beginning (point-max)) (point-min))
684 (point-max))
685 (let ((md (match-data)) beg word compl
686 (quick (and (boundp 'quick) (eq quick t)))
687 (displist ;function to display completion-list
688 (function
689 (lambda ()
690 (with-output-to-temp-buffer "*Completions*"
691 (display-completion-list
692 (all-completions word minibuffer-completion-table)))))))
693 (setq beg (if (and (boundp 'delim) (stringp delim))
694 (save-excursion
695 (skip-chars-backward (concat "^" delim))
696 (point))
697 (point-min))
698 word (buffer-substring beg (point-max))
699 compl (try-completion word minibuffer-completion-table))
700 (cond
701 ((eq compl t)
702 (if quick (exit-minibuffer)
703 (let ((p (point)) (max (point-max)))
704 (unwind-protect
705 (progn
706 (goto-char max)
707 (insert " [Sole completion]")
708 (goto-char p)
709 (sit-for 1))
710 (delete-region max (point-max))
711 (goto-char p)))))
712 ((eq compl nil)
713 (ding)
714 (save-excursion
715 (let (p)
716 (unwind-protect
717 (progn
718 (goto-char (setq p (point-max)))
719 (insert " [No match]")
720 (goto-char p)
721 (sit-for 2))
722 (delete-region p (point-max))))))
723 ((string= compl word)
724 (funcall displist))
725 (t (delete-region beg (point-max))
726 (insert compl)
727 (if quick
728 (if (eq (try-completion compl minibuffer-completion-table) t)
729 (exit-minibuffer)
730 (funcall displist)))))
731 (store-match-data md))))
733 (defun YaTeX-minibuffer-quick-complete ()
734 "Set 'quick to 't and call YaTeX-minibuffer-complete.
735 See documentation of YaTeX-minibuffer-complete."
736 (interactive)
737 (let ((quick t))
738 (self-insert-command 1)
739 (YaTeX-minibuffer-complete)))
741 (defun YaTeX-yatex-buffer-list ()
742 (save-excursion
743 (delq nil (mapcar (function (lambda (buf)
744 (set-buffer buf)
745 (if (eq major-mode 'yatex-mode) buf)))
746 (buffer-list)))))
748 (defun foreach-buffers (pattern job)
749 "For each buffer which matches with PATTERN, do JOB."
750 (let ((list (buffer-list)))
751 (save-excursion
752 (while list
753 (set-buffer (car list))
754 (if (or (and (stringp pattern)
755 (buffer-file-name)
756 (string-match pattern (buffer-file-name)))
757 (and (symbolp pattern) major-mode (eq major-mode pattern)))
758 (eval job))
759 (setq list (cdr list))))))
761 (defun goto-buffer-window (buffer)
762 "Select window which is bound to BUFFER.
763 If no such window exist, switch to buffer BUFFER."
764 (interactive "BGoto buffer: ")
765 (if (stringp buffer)
766 (setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
767 (if (get-buffer buffer)
768 (cond
769 ((get-buffer-window buffer)
770 (select-window (get-buffer-window buffer)))
771 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
772 (let*((win (get-buffer-window buffer t))
773 (frame (window-frame win)))
774 (select-frame frame)
775 (raise-frame frame)
776 (focus-frame frame)
777 (select-window win)
778 (set-mouse-position frame 0 0)
779 (and (featurep 'windows) (fboundp 'win:adjust-window)
780 (win:adjust-window))))
781 ((and (featurep 'windows) (fboundp 'win:get-buffer-window)
782 (let ((w (win:get-buffer-window buffer)))
783 (and w (win:switch-window w))))
784 (select-window (get-buffer-window buffer)))
785 (t (switch-to-buffer buffer)))))
787 ;; Here starts the functions which support gmhist-vs-Emacs19 compatible
788 ;; reading with history.
789 ;;;###autoload
790 (defun completing-read-with-history
791 (prompt table &optional predicate must-match initial hsym)
792 "Completing read with general history: gmhist, Emacs-19."
793 (let ((minibuffer-history
794 (or (symbol-value hsym)
795 (and (boundp 'minibuffer-history) minibuffer-history)))
796 (minibuffer-history-symbol (or hsym 'minibuffer-history)))
797 (prog1
798 (if (fboundp 'completing-read-with-history-in)
799 (completing-read-with-history-in
800 minibuffer-history-symbol prompt table predicate must-match initial)
801 (save-excursion ;work around to avoid cursor warp
802 (completing-read prompt table predicate must-match initial)))
803 (if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
805 ;;;###autoload
806 (defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
807 "Read from minibuffer with general history: gmhist, Emacs-19."
808 (cond
809 (YaTeX-emacs-19
810 (save-excursion ;work around to avoid cursor warp
811 (read-from-minibuffer prompt init map read hsym)))
812 (t
813 (let ((minibuffer-history-symbol hsym))
814 (read-from-minibuffer prompt init map read)))))
816 ;;;###autoload
817 (defun read-string-with-history (prompt &optional init hsym)
818 "Read string with history: gmhist(Emacs-18) and Emacs-19."
819 (cond
820 (YaTeX-emacs-19
821 (save-excursion ;work around to avoid cursor warp
822 (read-from-minibuffer prompt init minibuffer-local-map nil hsym)))
823 ((featurep 'gmhist-mh)
824 (read-with-history-in hsym prompt init))
825 (t (read-string prompt init))))
827 (defvar YaTeX-skip-next-reader-char ?\C-j)
828 (defun YaTeX-read-string-or-skip (&rest args)
829 "Read string, or skip if last input char is \C-j."
830 (if (equal (if (boundp 'last-input-event) last-input-event last-input-char)
831 YaTeX-skip-next-reader-char)
832 ""
833 (save-excursion ;work around to avoid cursor warp
834 (apply 'read-string args))))
836 (defun YaTeX-completing-read-or-skip (&rest args)
837 "Do completing-read, or skip if last input char is \C-j."
838 (if (equal (if (boundp 'last-input-event) last-input-event last-input-char)
839 YaTeX-skip-next-reader-char)
840 ""
841 (save-excursion ;work around to avoid cursor warp
842 (apply 'completing-read args))))
844 ;;;###autoload
845 (fset 'YaTeX-rassoc
846 (if (and nil (fboundp 'rassoc) (subrp (symbol-function 'rassoc)))
847 (symbol-function 'rassoc)
848 (function
849 (lambda (key list)
850 (let ((l list))
851 (catch 'found
852 (while l
853 (if (equal key (cdr (car l)))
854 (throw 'found (car l)))
855 (setq l (cdr l)))))))))
857 (defun YaTeX-set-file-coding-system (code coding)
858 "Set current buffer's coding system according to symbol."
859 (cond ((null code)
860 nil)
861 ((boundp 'MULE)
862 (set-file-coding-system coding))
863 ((and YaTeX-emacs-20 (boundp 'buffer-file-coding-system))
864 (setq buffer-file-coding-system
865 (or (and (fboundp 'set-auto-coding) buffer-file-name
866 (save-excursion
867 (goto-char (point-min))
868 (set-auto-coding buffer-file-name (buffer-size))))
869 coding)))
870 ((featurep 'mule)
871 (set-file-coding-system coding))
872 ((boundp 'NEMACS)
873 (make-local-variable 'kanji-fileio-code)
874 (setq kanji-fileio-code code))))
876 (defun YaTeX-insert-file-contents (file visit &optional beg end)
877 (cond
878 ((and (string< "19" emacs-version) (not (featurep 'xemacs)))
879 (insert-file-contents file visit beg end))
880 ((string-match "unix\\|linux" (symbol-name system-type))
881 (let ((default-process-coding-system
882 (and (boundp '*noconv*) (list '*noconv*)))
883 (file-coding-system (and (boundp '*noconv*) '*noconv*))
884 kanji-fileio-code
885 (default-process-kanji-code 0))
886 (call-process shell-file-name file (current-buffer) nil
887 (or (and (boundp 'shell-command-option)
888 shell-command-option)
889 "-c")
890 (format "dd bs=1 count=%d | tail -c +%d" end beg))))
891 (t (insert-file-contents file))))
893 (defun YaTeX-split-string (str &optional sep null)
894 "Split string STR by every occurrence of SEP(regexp).
895 If the optional second argument SEP is nil, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
896 Do not include null string by default. Non-nil for optional third argument
897 NULL includes null string in a list."
898 (let ((sep (or sep "[ \f\t\n\r\v]+"))
899 list m)
900 (while str
901 (if (setq m (string-match sep str))
902 (progn
903 (if (or (> m 0) null)
904 (setq list (cons (substring str 0 m) list)))
905 (setq str (substring str (match-end 0))))
906 (if (or null (string< "" str))
907 (setq list (cons str list)))
908 (setq str nil)))
909 (nreverse list)))
911 ;;;###autoload
912 (defun YaTeX-delete1 (elt list)
913 "Delete"
914 (let (e)
915 (while (setq e (YaTeX-member elt list))
916 (setq list (delq (car e) list))))
917 list)
918 (if (fboundp 'delete)
919 (fset 'YaTeX-delete (symbol-function 'delete))
920 (fset 'YaTeX-delete (symbol-function 'YaTeX-delete1)))
922 (defun YaTeX-member1 (elt list)
923 (catch 'found
924 (while list
925 (if (equal elt (car list))
926 (throw 'found list))
927 (setq list (cdr list)))))
929 (if (and (fboundp 'member) (subrp (symbol-function 'member)))
930 (fset 'YaTeX-member (symbol-function 'member))
931 (fset 'YaTeX-member (symbol-function 'YaTeX-member1)))
933 ;;; interactive-p
934 (if (fboundp 'called-interactively-p)
935 (defun YaTeX-interactive-p()
936 (called-interactively-p 'interactive))
937 (fset 'YaTeX-interactive-p 'interactive-p))
938 (fset 'YaTeX-eval-buffer
939 (if (fboundp 'eval-current-buffer) 'eval-current-buffer 'eval-buffer))
941 ;;;
942 ;; Interface function for windows.el
943 ;;;
944 ;;;###autoload
945 (fset 'YaTeX-last-key
946 (if (fboundp 'win:last-key)
947 'win:last-key
948 (function (lambda () (if (boundp 'last-command-char)
949 last-command-char
950 last-command-event)))))
951 (defun YaTeX-switch-to-window ()
952 "Switch to windows.el's window decided by last pressed key."
953 (interactive)
954 (or (featurep 'windows) (error "Why don't you use `windows.el'?"))
955 (win-switch-to-window 1 (- (YaTeX-last-key) win:base-key)))
958 ;;;###autoload
959 (defun YaTeX-command-to-string (cmd)
960 (if (fboundp 'shell-command-to-string)
961 (funcall 'shell-command-to-string cmd)
962 (let ((tbuf " *tmpout*"))
963 (if (get-buffer-create tbuf) (kill-buffer tbuf))
964 (let ((standard-output (get-buffer-create tbuf)))
965 (unwind-protect
966 (save-excursion
967 (call-process
968 shell-file-name nil tbuf nil YaTeX-shell-command-option cmd)
969 (set-buffer tbuf)
970 (buffer-string))
971 (kill-buffer tbuf))))))
973 ;;; (defun YaTeX-executable-find(cmd)...)
974 (fset 'YaTeX-executable-find
975 (if (fboundp 'executable-find)
976 'executable-find
977 (function (lambda (cmd)
978 (let ((list exec-path) path)
979 (catch 'exec
980 (while list
981 (if (file-executable-p
982 (setq path (expand-file-name cmd (car list))))
983 (throw 'exec path))
984 (setq list (cdr list)))))))))
986 ;;;###autoload
987 (defun YaTeX-reindent (col)
988 "Remove current indentation and reindento to COL column."
989 (save-excursion
990 (beginning-of-line)
991 (skip-chars-forward " \t")
992 (if (/= col (current-column))
993 (progn
994 (delete-region (point) (progn (beginning-of-line) (point)))
995 (indent-to col))))
996 (skip-chars-forward " \t" (point-end-of-line)))
998 (defun YaTeX-inner-environment (&optional quick)
999 "Return current inner-most environment.
1000 Non-nil for optional argument QUICK restricts search bound to most
1001 recent sectioning command. Matching point is stored to property 'point
1002 of 'YaTeX-inner-environment, which can be referred by
1003 (get 'YaTeX-inner-environment 'point)."
1004 (put 'YaTeX-inner-environment 'point (point-min))
1005 (put 'YaTeX-inner-environment 'indent 0)
1006 (let*((nest 0)
1007 (beg (YaTeX-replace-format-args
1008 (regexp-quote YaTeX-struct-begin)
1009 ;YaTeX-struct-begin ;=== TENTATIVE!! ==
1010 YaTeX-struct-name-regexp
1011 (if (eq major-mode 'yahtml-mode) "\\s *.*" "")
1012 ""))
1013 (end (YaTeX-replace-format-args
1014 (regexp-quote YaTeX-struct-end)
1015 YaTeX-struct-name-regexp "" ""))
1016 (begend (concat "\\(" beg "\\)\\|\\(" end "\\)"))
1017 bound m0
1018 (htmlp (eq major-mode 'yahtml-mode))
1019 (open
1020 (concat "^" (or (cdr (assq major-mode '((yahtml-mode . "<")))) "{")))
1021 (close
1022 (concat "^"
1023 (or (cdr(assq major-mode '((yahtml-mode . "\n\t >")))) "}"))))
1024 (save-excursion
1025 (if quick
1026 (setq bound
1027 (save-excursion
1028 (if htmlp
1029 ;;(re-search-backward YaTeX-sectioning-regexp nil 1)
1030 ;;(goto-char (point-min)) ;Is this enough? 97/6/26
1031 (re-search-backward yahtml-indentation-boundary nil 1)
1032 (YaTeX-re-search-active-backward
1033 (concat YaTeX-ec-regexp
1034 "\\(" YaTeX-sectioning-regexp "\\)\\*?{")
1035 YaTeX-comment-prefix nil 1))
1036 (or (bobp) (end-of-line))
1037 (point))))
1038 (if (catch 'begin
1039 (if (and (numberp bound) (< (point) bound)) (throw 'begin nil))
1040 (while (YaTeX-re-search-active-backward
1041 begend YaTeX-comment-prefix bound t)
1042 (setq m0 (match-beginning 0))
1043 (if (looking-at end) ;;(match-beginning 2)
1044 (setq nest (1+ nest))
1045 (setq nest (1- nest)))
1046 (if (< nest 0)
1047 (progn
1048 (put 'YaTeX-inner-environment 'point m0)
1049 (goto-char m0)
1050 (put 'YaTeX-inner-environment 'indent (current-column))
1051 (throw 'begin t)))))
1052 (YaTeX-buffer-substring
1053 (progn (skip-chars-forward open) (1+ (point)))
1054 (progn (skip-chars-forward close) (point)))))))
1056 (defun YaTeX-in-environment-p (env)
1057 "Return if current LaTeX environment is ENV.
1058 ENV is given in the form of environment's name or its list."
1059 (let ((md (match-data)) (nest 0) p envrx)
1060 (cond
1061 ((atom env)
1062 (setq envrx
1063 (concat "\\("
1064 (regexp-quote
1065 (YaTeX-replace-format-args
1066 YaTeX-struct-begin env "" ""))
1067 "\\>\\)\\|\\("
1068 (regexp-quote
1069 (YaTeX-replace-format-args
1070 YaTeX-struct-end env "" ""))
1071 "\\)"))
1072 (save-excursion
1073 (setq p (catch 'open
1074 (while (YaTeX-re-search-active-backward
1075 envrx YaTeX-comment-prefix nil t)
1076 (if (match-beginning 2)
1077 (setq nest (1+ nest))
1078 (setq nest (1- nest)))
1079 (if (< nest 0)
1080 (throw 'open (cons env (match-beginning 0)))))))))
1081 ((listp env)
1082 (setq p
1083 (or (YaTeX-in-environment-p (car env))
1084 (and (cdr env) (YaTeX-in-environment-p (cdr env)))))))
1085 (store-match-data md)
1086 p;(or p (YaTeX-in-verb-p (match-beginning 0)))
1087 ))
1089 (defun YaTeX-quick-in-environment-p (env)
1090 "Check quickly but unsure if current environment is ENV.
1091 ENV is given in the form of environment's name or its list.
1092 This function returns correct result only if ENV is NOT nested."
1093 (save-excursion
1094 (let ((md (match-data)) m0 (p (point)) rc clfound)
1095 (cond
1096 ((listp env)
1097 (or (YaTeX-quick-in-environment-p (car env))
1098 (and (cdr env) (YaTeX-quick-in-environment-p (cdr env)))))
1099 (t
1100 (unwind-protect
1101 (if (prog1
1102 (YaTeX-search-active-backward
1103 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
1104 YaTeX-comment-prefix nil t)
1105 (setq m0 (match-beginning 0)))
1106 (if (YaTeX-search-active-forward
1107 (YaTeX-replace-format-args
1108 YaTeX-struct-end env)
1109 YaTeX-comment-prefix p t nil)
1110 nil ;if \end{env} found, return nil
1111 (cons env m0))) ;else, return meaningful values
1112 (store-match-data md)))))))
1114 (defun YaTeX-goto-corresponding-environment (&optional allow-mismatch noerr bg)
1115 "Go to corresponding begin/end enclosure.
1116 Optional argument ALLOW-MISMATCH allows mismatch open/clese. Use this
1117 for \\left(, \\right).
1118 Optional third argument NOERR causes no error for unballanced environment."
1119 (interactive)
1120 (if (not (YaTeX-on-begin-end-p)) nil
1121 (let ((p (match-end 0)) b0 b1 env (nest 0) regexp re-s (op (point))
1122 (m0 (match-beginning 0)) ;whole matching
1123 (m1 (match-beginning 1)) ;environment in \begin{}
1124 (m2 (match-beginning 2)) ;environment in \end{}
1125 (m3 (match-beginning 3))) ;environment in \[ \] \( \)
1126 ;(setq env (regexp-quote (buffer-substring p (match-beginning 0))))
1127 (if (cond
1128 (m1 ;if begin{xxx}
1129 (setq env
1130 (if allow-mismatch YaTeX-struct-name-regexp
1131 (regexp-quote (buffer-substring m1 (match-end 1)))))
1132 ; (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
1133 ; "\\(\\\\begin{" env "}\\)"))
1134 (setq regexp
1135 (concat
1136 "\\("
1137 (YaTeX-replace-format-args
1138 (regexp-quote YaTeX-struct-end) env "" "")
1139 "\\)\\|\\("
1140 (YaTeX-replace-format-args
1141 (regexp-quote YaTeX-struct-begin) env "" "")
1142 "\\)"))
1143 (setq re-s 're-search-forward))
1144 (m2 ;if end{xxx}
1145 (setq env
1146 (if allow-mismatch YaTeX-struct-name-regexp
1147 (regexp-quote (buffer-substring m2 (match-end 2)))))
1148 ; (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
1149 ; "\\(\\\\end{" env "}\\)"))
1150 (setq regexp
1151 (concat
1152 "\\("
1153 (YaTeX-replace-format-args
1154 (regexp-quote YaTeX-struct-begin) env "" "")
1155 "\\)\\|\\("
1156 (YaTeX-replace-format-args
1157 (regexp-quote YaTeX-struct-end) env "" "")
1158 "\\)"))
1159 (setq re-s 're-search-backward))
1160 (m3 ;math environment
1161 (setq env (char-after (1+ m3))
1162 regexp (format "\\(%s%s\\)\\|\\(%s%s\\)"
1163 YaTeX-ec-regexp
1164 (regexp-quote
1165 (cdr (assq env '((?\( . ")") (?\) . "(")
1166 (?\[ . "]") (?\] . "[")))))
1167 YaTeX-ec-regexp
1168 (regexp-quote (char-to-string env)))
1169 re-s (if (memq env '(?\( ?\[))
1170 're-search-forward
1171 're-search-backward)))
1172 (t (if noerr nil (error "Corresponding environment not found."))))
1173 (progn
1174 (while (and (>= nest 0) (funcall re-s regexp nil t))
1175 (setq b0 (match-beginning 0) b1 (match-beginning 1))
1176 (if (or (equal b0 m0)
1177 (YaTeX-literal-p b0))
1178 nil
1179 (setq nest (if (equal b0 b1)
1180 (1- nest) (1+ nest)))))
1181 (if (< nest 0)
1182 (goto-char (match-beginning 0)) ;found.
1183 (goto-char op)
1184 (funcall
1185 (if noerr 'message 'error)
1186 "Corresponding environment `%s' not found." env)
1187 (or bg (sit-for 1))
1188 nil))))))
1190 (defun YaTeX-end-environment ()
1191 "Close opening environment"
1192 (interactive)
1193 (let ((env (YaTeX-inner-environment)))
1194 (if (not env) (error "No premature environment")
1195 (save-excursion
1196 (if (and
1197 (YaTeX-re-search-active-forward
1198 (concat
1199 "\\(" (YaTeX-replace-format-args
1200 YaTeX-struct-end env "" "")
1201 "\\)\\|\\(" (YaTeX-replace-format-args
1202 YaTeX-struct-begin env "" "")
1203 "\\)")
1204 YaTeX-comment-prefix nil t)
1205 (match-beginning 1)) ;is closing struc.
1206 (if (y-or-n-p
1207 (concat "Environment `" env
1208 "' may be already closed. Force close?"))
1209 nil
1210 (error "end environment aborted."))))
1211 (message "") ;Erase (y or n) message.
1212 (YaTeX-insert-struc 'end env)
1213 (save-excursion
1214 (goto-char (or (get 'YaTeX-inner-environment 'point) (match-end 0)))
1215 (if (pos-visible-in-window-p)
1216 (sit-for (if YaTeX-dos 2 1))
1217 (message "Matches with %s at line %d"
1218 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
1219 (count-lines (point-min) (point))))))))
1221 (defun YaTeX-beginning-of-environment (&optional limit-search-bound end)
1222 "Goto the beginning of the current environment.
1223 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound to
1224 most recent sectioning command. Non-nil for optional third argument END
1225 goes to end of environment."
1226 (interactive)
1227 (let ((op (point)))
1228 (if (YaTeX-inner-environment limit-search-bound)
1229 (progn
1230 (goto-char (get 'YaTeX-inner-environment 'point))
1231 (and end (YaTeX-goto-corresponding-environment))
1232 (if (YaTeX-interactive-p) (push-mark op))
1233 (point)))))
1235 (defun YaTeX-end-of-environment (&optional limit-search-bound)
1236 "Goto the end of the current environment.
1237 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound
1238 to most recent sectioning command."
1239 (interactive)
1240 (YaTeX-beginning-of-environment limit-search-bound t))
1242 (defun YaTeX-mark-environment ()
1243 "Mark current position and move point to end of environment."
1244 (interactive)
1245 (require 'yatexmth)
1246 (let ((curp (point)))
1247 (if (YaTeX-in-math-mode-p)
1248 (YaTeX-mark-mathenv)
1249 (if (and (YaTeX-on-begin-end-p) (match-beginning 1)) ;if on \\begin
1250 (progn (goto-char (match-end 0)))
1251 (if (= (char-after (point)) ?\\) nil ;if on \\end
1252 (skip-chars-backward "^\n\\\\")
1253 (or (bolp) (forward-char -1))))
1254 (if (not (YaTeX-end-of-environment)) ;arg1 turns to match-beginning 1
1255 (progn
1256 (goto-char curp)
1257 (error "Cannot found the end of current environment."))
1258 (YaTeX-goto-corresponding-environment)
1259 ;;(beginning-of-line) ;for confirmation ;OUT 2015/1/4
1260 (if (< curp (point))
1261 (progn
1262 (message "Mark this environment?(y or n): ")
1263 (if (= (read-char) ?y) nil
1264 (goto-char curp)
1265 (error "Abort. Please call again at more proper position."))))
1266 (set-mark-command nil)
1267 (YaTeX-goto-corresponding-environment)
1268 (goto-char (match-end 0))
1269 ;;(end-of-line) ;OUT 2015/1/5
1270 ;;(if (eobp) nil (forward-char 1)) ;OUT 2015/1/5
1271 ))))
1273 (defun YaTeX-in-BEGEND-p (&optional pt)
1274 "Check if the point (or PT) is in a %#BEGIN...%#END region.
1275 Return the list of beginning and ending point of the region and arg-string
1276 if the point is in BEGEND. Otherwise nil."
1277 (let ((b "%#BEGIN") bp args (e "%#END") (p (point)))
1278 (save-excursion
1279 (save-match-data ;emacs-19+ yatex1.80+
1280 (and (re-search-backward b nil t)
1281 (progn
1282 (setq bp (match-beginning 0))
1283 (goto-char (match-end 0)) ;Start to get args of %#BEGIN
1284 (skip-chars-forward " \t")
1285 (setq args (YaTeX-buffer-substring (point) (point-end-of-line))))
1286 (re-search-forward e nil t)
1287 (> (point) p)
1288 (list bp (match-end 0) args))))))
1290 (defun YaTeX-kill-buffer (buffer)
1291 "Make effort to show parent buffer after kill."
1292 (interactive "bKill buffer: ")
1293 (or (get-buffer buffer)
1294 (error "No such buffer %s" buffer))
1295 (let ((pf YaTeX-parent-file))
1296 (kill-buffer buffer)
1297 (and pf
1298 (get-file-buffer pf)
1299 (switch-to-buffer (get-file-buffer pf)))))
1301 (defun YaTeX-getset-builtin (key &optional value)
1302 "Read source built-in command of %# usage."
1303 (catch 'builtin
1304 (let*((bl (delq nil (list (current-buffer)
1305 (and YaTeX-parent-file
1306 (get-file-buffer YaTeX-parent-file)))))
1307 (tuple (cdr (assq major-mode
1308 '((yatex-mode "%#" . "\n")
1309 (yahtml-mode "<!-- #" . "[ \t]*-->\\|\n")))))
1310 (leader (or (car tuple) ""))
1311 (closer (or (cdr tuple) ""))
1312 (prompt (format "Built-in for %s: " key)))
1313 (save-excursion
1314 (while bl
1315 (set-buffer (car bl))
1316 (save-excursion
1317 (goto-char (point-min))
1318 (if (and (re-search-forward
1319 (concat "^" (regexp-quote (concat leader key))) nil t)
1320 (not (eolp)))
1321 (throw 'builtin
1322 (let (b e w)
1323 (skip-chars-forward " \t" (point-end-of-line))
1324 (setq b (point)
1325 e (if (re-search-forward closer nil t)
1326 (match-beginning 0)
1327 (point-end-of-line))
1328 w (YaTeX-buffer-substring b e))
1329 (if (null value)
1331 (delete-region b e)
1332 (goto-char b)
1333 (if (symbolp value)
1334 (setq value (read-string prompt w)))
1335 (insert value)
1336 value)))))
1337 (setq bl (cdr bl)))
1338 ; not found
1339 (if (null value)
1340 nil ;not set mode, return simply nil
1341 (if (symbolp value)
1342 (setq value (read-string prompt)))
1343 (save-excursion
1344 (goto-char (point-min))
1345 (insert leader key " " value "\n")
1346 value)))))) ;on set mode, return set value
1348 (defun YaTeX-get-builtin (key)
1349 "Read source built-in command of %# usage."
1350 (YaTeX-getset-builtin key))
1352 ;;;VER2
1353 (defun YaTeX-insert-struc (what env)
1354 (cond
1355 ((eq what 'begin)
1356 (insert (YaTeX-replace-format-args
1357 YaTeX-struct-begin env (YaTeX-addin env))))
1358 ((eq what 'end)
1359 (insert (YaTeX-replace-format-args YaTeX-struct-end env)))
1360 (t nil)))
1362 (defun YaTeX-string-width (str)
1363 "Return the display width of string."
1364 (if (fboundp 'string-width)
1365 (string-width str)
1366 (length str)))
1367 (defun YaTeX-truncate-string-width (str width)
1368 (cond
1369 ((fboundp 'truncate-string-to-width) (truncate-string-to-width str width))
1370 ((fboundp 'truncate-string) (truncate-string str width))
1371 (t (substring str 0 width))))
1373 (defun YaTeX-hex (str)
1374 "Return int expressed by hexadecimal string STR."
1375 (if (string< "20" emacs-version)
1376 (string-to-number str 16)
1377 (let ((md (match-data)))
1378 (unwind-protect
1379 (if (string-match "[^0-9a-f]" str)
1380 (error "Non hexadecimal character in %s" str)
1381 (let ((i 0) d)
1382 (setq str (downcase str))
1383 (while (string< "" str)
1384 (setq d (+ 0 (string-to-char str)) ; + 0 for XEmacs
1385 i (+ (* 16 i) (- d (if (<= d ?9) ?0 (- ?a 10))))
1386 str (substring str 1)))
1387 i))
1388 (store-match-data md)))))
1391 ;;; Function for menu support
1392 (defun YaTeX-define-menu (keymap bindlist)
1393 "Define KEYMAP(symbol)'s menu-bindings according to BINDLIST.
1394 KEYMAP should be a quoted symbol of newly allocated keymap.
1395 BINDLIST consists of binding list. Each element is as follows.
1397 '(menusymbol DOC_String . contents)
1399 CONTENTS is one of lambda-form, interactive function, or other keymap.
1400 See yatex19.el for example."
1401 (cond
1402 ((featurep 'xemacs)
1403 (let (name)
1404 (if (keymapp (symbol-value keymap))
1405 (progn
1406 (setq name (keymap-name (symbol-value keymap)))
1407 (set keymap nil))
1408 (setq name (car (symbol-value keymap)))
1409 (set keymap (cdr (symbol-value keymap))))
1410 (mapcar
1411 (function
1412 (lambda (bind)
1413 (setq bind (cdr bind))
1414 (if (eq (car-safe (cdr bind)) 'lambda)
1415 (setcar (cdr bind) 'progn))
1416 (if (stringp (car-safe (cdr bind)))
1417 (set keymap
1418 (cons (cdr bind) (symbol-value keymap)))
1419 (set keymap
1420 (cons (vector (car bind) (cdr bind) t)
1421 (symbol-value keymap))))))
1422 bindlist)
1423 (set keymap (cons name (symbol-value keymap)))))
1424 (t
1425 (mapcar
1426 (function
1427 (lambda (bind)
1428 (define-key (symbol-value keymap) (vector (car bind)) (cdr bind))))
1429 bindlist))))
1431 ;;;
1432 ;; hilit19 vs. font-lock
1433 ;;;
1434 (defvar YaTeX-19-functions-font-lock-direct
1435 '(YaTeX-19-re-search-in-env))
1437 (defun YaTeX-convert-pattern-hilit2fontlock (h19pa)
1438 "Convert hilit19's H19PA patterns alist to font-lock's one.
1439 This function is a makeshift for YaTeX and yahtml."
1440 (let ((ignorecase (not (null (car h19pa))))
1441 (palist (cdr h19pa))
1442 flpa i newface
1443 (mapping
1444 '((bold . YaTeX-font-lock-bold-face)
1445 (italic . YaTeX-font-lock-italic-face)
1446 (defun . font-lock-function-name-face)
1447 (define . font-lock-variable-name-face)
1448 (keyword . font-lock-keyword-face)
1449 (decl . YaTeX-font-lock-declaration-face)
1450 (label . YaTeX-font-lock-label-face)
1451 (crossref . YaTeX-font-lock-crossref-face)
1452 (include . YaTeX-font-lock-include-face)
1453 (formula . YaTeX-font-lock-formula-face)
1454 (delimiter . YaTeX-font-lock-delimiter-face)
1455 (string . ignore) (comment . ignore)
1456 )))
1457 (while (setq i (car palist))
1458 (setq newface (nth 2 i)
1459 newface (or (cdr (assq newface mapping)) newface))
1460 (cond
1461 ((eq newface 'ignore) nil) ;no translation
1462 ((stringp (car i)) ;hiliting by regexp
1463 (setq flpa
1464 (cons
1465 (if (numberp (car (cdr i)))
1466 (list (car i) ;regexp
1467 (car (cdr i)) ;matching group number
1468 newface nil) ;'keep) ;keep is hilit19 taste
1469 (list
1470 (concat
1471 (car i) ;original regexp and..
1472 ;;"[^"
1473 ;;(regexp-quote (substring (car (cdr i)) 0 1))
1474 ;;"]+" ;for shortest match
1475 ".*"
1476 (car (cdr i)))
1477 0 (list 'quote newface) nil)) ;;'keep))
1478 flpa)))
1479 ((and (symbolp (car i)) (fboundp (car i)))
1480 (if (memq (car i) YaTeX-19-functions-font-lock-direct)
1481 ;; Put direct function call for it.
1482 ;; When calling this function, fontify entire matched string.
1483 (setq flpa
1484 (cons
1485 (list
1486 (list 'lambda (list 'dummy) ;dummy should be boundary
1487 (list (car i) (list 'quote (car (cdr i)))))
1488 (list 0 newface))
1489 flpa))
1490 (setq flpa
1491 (cons
1492 (list (car (cdr i)) ;regexp
1493 (list
1494 (list
1495 'lambda (list 'dummy)
1496 '(goto-char (match-beginning 0))
1497 (if (eq (nth 3 i) 'overwrite)
1498 nil
1499 '(remove-text-properties
1500 (point) (min (point-max) (1+ (point)))
1501 '(face nil font-lock-multiline nil)))
1502 (list
1503 'let (list '(e (match-end 0))
1504 (list 'm (list (car i) (car (cdr i)))))
1505 (list
1506 'if 'm
1507 (list
1508 'YaTeX-font-lock-fillin
1509 (list 'car 'm)
1510 (list 'cdr 'm)
1511 (list 'quote 'face)
1512 (list 'quote 'font-lock)
1513 (list 'quote newface))
1514 '(goto-char e)
1515 ))
1516 nil) ;retun nil to cheat font-lock
1517 nil nil)) ;pre-match, post-match both nil
1518 flpa)))))
1519 (setq palist (cdr palist)));while
1520 (if (featurep 'xemacsp)
1521 (nreverse flpa)
1522 flpa)))
1524 (if (and (boundp 'YaTeX-use-font-lock)
1525 YaTeX-use-font-lock)
1526 (require 'font-lock))
1528 (cond
1529 ((and (featurep 'font-lock) (fboundp 'defface))
1530 ;; In each defface, '(class static-color) is for Emacs-21 -nw
1531 ;; '(class tty) is for XEmacs-21 -nw
1532 (defface YaTeX-font-lock-label-face
1533 '((((class static-color)) (:foreground "yellow" :underline t))
1534 (((type tty)) (:foreground "yellow" :underline t))
1535 (((class color) (background dark)) (:foreground "pink" :underline t))
1536 (((class color) (background light)) (:foreground "red" :underline t))
1537 (t (:bold t :underline t)))
1538 "Font Lock mode face used to highlight labels."
1539 :group 'font-lock-faces)
1540 (defvar YaTeX-font-lock-label-face 'YaTeX-font-lock-label-face)
1542 (defface YaTeX-font-lock-declaration-face
1543 '((((class color) (background dark)) (:foreground "cyan"))
1544 (((class color) (background light)) (:foreground "RoyalBlue"))
1545 (t (:bold t :underline t)))
1546 "Font Lock mode face used to highlight some declarations."
1547 :group 'font-lock-faces)
1548 (defvar YaTeX-font-lock-declaration-face 'YaTeX-font-lock-declaration-face)
1550 (defface YaTeX-font-lock-include-face
1551 '((((class color) (background dark)) (:foreground "Plum1"))
1552 (((class color) (background light)) (:foreground "purple"))
1553 (t (:bold t :underline t)))
1554 "Font Lock mode face used to highlight expression for including."
1555 :group 'font-lock-faces)
1556 (defvar YaTeX-font-lock-include-face 'YaTeX-font-lock-include-face)
1558 (defface YaTeX-font-lock-formula-face
1559 '((((class static-color)) (:bold t))
1560 (((type tty)) (:bold t))
1561 (((class color) (background dark)) (:foreground "khaki" :bold t))
1562 (((class color) (background light)) (:foreground "DarkGoldenrod4"))
1563 (t (:bold t :underline t)))
1564 "Font Lock mode face used to highlight formula."
1565 :group 'font-lock-faces)
1566 (defvar YaTeX-font-lock-formula-face 'YaTeX-font-lock-formula-face)
1568 (defface YaTeX-font-lock-delimiter-face
1569 '((((class static-color)) (:bold t))
1570 (((type tty)) (:bold t))
1571 (((class color) (background dark))
1572 (:foreground "lightyellow3" :background "navy" :bold t))
1573 (((class color) (background light)) (:foreground "red"))
1574 (t (:bold t :underline t)))
1575 "Font Lock mode face used to highlight delimiters."
1576 :group 'font-lock-faces)
1577 (defvar YaTeX-font-lock-delimiter-face 'YaTeX-font-lock-delimiter-face)
1579 (defface YaTeX-font-lock-math-sub-face
1580 '((((class static-color)) (:bold t))
1581 (((type tty)) (:bold t))
1582 (((class color) (background dark))
1583 (:foreground "khaki" :bold t :underline t))
1584 (((class color) (background light))
1585 (:foreground "DarkGoldenrod4" :underline t))
1586 (t (:bold t :underline t)))
1587 "Font Lock mode face used to highlight subscripts in formula."
1588 :group 'font-lock-faces)
1589 (defvar YaTeX-font-lock-math-sub-face 'YaTeX-font-lock-math-sub-face)
1591 (defface YaTeX-font-lock-math-sup-face
1592 '((((class static-color)) (:bold t))
1593 (((type tty)) (:bold t))
1594 (((class color) (background dark))
1595 (:bold nil :foreground "ivory" :background "lightyellow4"))
1596 (((class color) (background light))
1597 (:underline t :foreground "DarkGoldenrod3"))
1598 (t (:bold t :underline t)))
1599 "Font Lock mode face used to highlight superscripts in formula."
1600 :group 'font-lock-faces)
1601 (defvar YaTeX-font-lock-math-sup-face 'YaTeX-font-lock-math-sup-face)
1603 (defface YaTeX-font-lock-crossref-face
1604 '((((class color) (background dark)) (:foreground "lightgoldenrod"))
1605 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1606 (t (:bold t :underline t)))
1607 "Font Lock mode face used to highlight cross references."
1608 :group 'font-lock-faces)
1609 (defvar YaTeX-font-lock-crossref-face 'YaTeX-font-lock-crossref-face)
1611 (defface YaTeX-font-lock-bold-face
1612 '((t (:bold t)))
1613 "Font Lock mode face used to express bold itself."
1614 :group 'font-lock-faces)
1615 (defvar YaTeX-font-lock-bold-face 'YaTeX-font-lock-bold-face)
1617 (defface YaTeX-font-lock-italic-face
1618 '((t (:italic t)))
1619 "Font Lock mode face used to express italic itself."
1620 :group 'font-lock-faces)
1621 (defvar YaTeX-font-lock-italic-face 'YaTeX-font-lock-italic-face)
1623 ;; Make sure the 'YaTeX-font-lock-{italic,bold}-face is bound with
1624 ;; italic/bold fontsets
1625 (if (and (fboundp 'fontset-list) YaTeX-use-italic-bold)
1626 (let ((flist (fontset-list)) fnt italic bold
1627 (df (or (and (fboundp 'face-font-name) (face-font-name 'default))
1628 (face-font 'default)
1629 (face-font 'italic)
1630 (face-font 'bold)
1631 "giveup!"))
1632 sz medium-i bold-r)
1633 (if (string-match
1634 "^-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-\\(\\([0-9]+\\)\\)" df)
1635 (setq sz (or (match-string 1 df) "16"))
1636 (setq sz "16"))
1637 (setq medium-i (format "-medium-i-[^-]+--%s" sz)
1638 bold-r (format "-bold-r-[^-]+--%s" sz))
1639 (while flist
1640 (setq fnt (car flist))
1641 (condition-case err
1642 (cond
1643 ((and (string-match medium-i fnt)
1644 (null italic))
1645 (set-face-font 'YaTeX-font-lock-italic-face (setq italic fnt)))
1646 ((and (string-match bold-r fnt) (null bold))
1647 (set-face-font 'YaTeX-font-lock-bold-face (setq bold fnt))))
1648 (error nil))
1649 (setq flist (cdr flist)))))
1651 ;;Borrowed from XEmacs's font-lock.el
1652 (defsubst YaTeX-font-lock-fillin (start end setprop markprop value &optional object)
1653 "Fill in one property of the text from START to END.
1654 Arguments PROP and VALUE specify the property and value to put where none are
1655 already in place. Therefore existing property values are not overwritten.
1656 Optional argument OBJECT is the string or buffer containing the text."
1657 (let ((start (text-property-any start end markprop nil object)) next
1658 (putfunc (if (fboundp 'put-nonduplicable-text-property)
1659 'put-nonduplicable-text-property
1660 'put-text-property)))
1661 (if (eq putfunc 'put-text-property)
1662 (setq markprop setprop))
1663 (while start
1664 (setq next (next-single-property-change start markprop object end))
1665 (funcall putfunc start next setprop value object)
1666 (funcall putfunc start next markprop value object)
1667 (setq start (text-property-any next end markprop nil object)))))
1669 (defun YaTeX-warning-font-lock (mode)
1670 (let ((sw (selected-window)))
1671 ;;(pop-to-buffer (format " *%s warning*" mode))
1672 ;;(erase-buffer)
1673 (momentary-string-display
1674 (cond
1675 (YaTeX-japan
1676 (concat mode " は、既に font-lock に対応しました。\n"
1677 "~/.emacs などにある\n"
1678 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
1679 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
1680 "などの間に合わせの記述はもはや不要です。"))
1681 (t
1682 (concat mode " now supports the font-lock by itself.\n"
1683 "So you can remove the descriptions such as\n"
1684 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
1685 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
1686 "in your ~/.emacs file. Thank you."))) (point))
1687 (select-window sw)))
1688 ))
1690 (defun YaTeX-assoc-regexp (elt alist)
1691 "Like assoc, return a list of whose car match with ELT. Search from ALIST.
1692 Note that each car of cons-cell is regexp. ELT is a plain text to be
1693 compared by regexp."
1694 (let (x)
1695 (catch 'found
1696 (while alist
1697 (setq x (car (car alist)))
1698 (if (string-match x elt)
1699 (throw 'found (car alist)))
1700 (setq alist (cdr alist))))))
1702 (defun YaTeX-push-to-kill-ring (string)
1703 "Push STRING to kill-ring, then show guidance message."
1704 (and (stringp string) (string< "" string)
1705 (let ((key (key-description (where-is-internal 'yank nil t)))
1706 (msg
1707 (if YaTeX-japan
1708 " をkill-ringに入れました。次のyank(%s)で貼付できます"
1709 " is stored into kill-ring. Paste it by yank(%s).")))
1710 (kill-new string)
1711 (message (concat "`%s'" msg) string key))))
1713 (defun YaTeX-elapsed-time (before after)
1714 "Get elapsed time from BEFORE and AFTER, which are given from currente-time."
1715 (if (fboundp 'float) ;Then, current-time function should be.
1716 (let ((mil (float 1000000))) ;To protect parse error before 19
1717 (+ (* (- (nth 0 after) (nth 0 before)) 65536)
1718 (- (nth 1 after) (nth 1 before))
1719 (- (/ (nth 2 after) mil)
1720 (/ (nth 2 before) mil))))))
1722 ;;;
1723 ;; Moved from comment.el
1724 ;;;
1725 (defun YaTeX-comment-region-sub (string &optional beg end once)
1726 "Insert STRING at the beginning of every line between BEG and END."
1727 (if (not (stringp string)) (setq string YaTeX-comment-prefix))
1728 (let ((b (or beg (region-beginning))) (e (or end (region-end))))
1729 (save-excursion
1730 (goto-char (max b e))
1731 (if (bolp)
1732 (forward-line -1))
1733 (save-restriction
1734 (narrow-to-region (min b e) (point))
1735 (goto-char (point-min))
1736 (message "%s" string)
1737 (while (re-search-forward "^" nil t)
1738 (insert string))))))
1740 (defun YaTeX-uncomment-region-sub (string &optional beg end once)
1741 "Delete STRING from the beginning of every line between BEG and END.
1742 BEG and END are optional. If omitted, active region used.
1743 Non-nil for optional 4th argument ONCE withholds from removing
1744 successive comment chars at the beggining of lines."
1745 (save-excursion
1746 (save-restriction
1747 (narrow-to-region (or beg (region-beginning)) (or end (region-end)))
1748 (goto-char (point-min))
1749 (while (re-search-forward (concat "^" string) nil t)
1750 (replace-match "")
1751 (if once (end-of-line))))))
1753 ;;;
1754 ;; Functions for the Installation time
1755 ;;;
1757 (defun bcf-and-exit ()
1758 "Byte compile rest of argument and kill-emacs."
1759 (if command-line-args-left
1760 (let ((load-path (cons "." load-path)))
1761 (and (fboundp 'set-language-environment)
1762 (featurep 'mule)
1763 (set-language-environment "Japanese"))
1764 (mapcar 'byte-compile-file command-line-args-left)
1765 (kill-emacs))))
1767 (defun tfb-and-exit ()
1768 "Texinfo-format-buffer and kill-emacs."
1769 (if command-line-args-left
1770 (let ((load-path (cons ".." load-path)))
1771 (and (fboundp 'set-language-environment)
1772 (featurep 'mule)
1773 (set-language-environment "Japanese"))
1774 (mapcar (function
1775 (lambda (arg)
1776 (find-file arg)
1777 (texinfo-format-buffer)
1778 (cond
1779 ((fboundp 'set-buffer-file-coding-system)
1780 (set-buffer-file-coding-system 'sjis-dos))
1781 ((fboundp 'set-file-coding-system)
1782 (set-file-coding-system '*sjis*dos))
1783 ((boundp 'NEMACS)
1784 (set (make-local-variable 'kanji-fileio-code) 1)))
1785 (let ((coding-system-for-write buffer-file-coding-system))
1786 (basic-save-buffer))))
1787 command-line-args-left)
1788 (kill-emacs))))
1790 (provide 'yatexlib)