yatex

view yatexlib.el @ 88:ce2deaceb818

yatex-1.74
author yuuji@gentei.org
date Mon, 28 Sep 2009 11:37:51 +0900
parents f14ec50103d0
children 8703f090c628
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX and yahtml common libraries, general functions and definitions
3 ;;; yatexlib.el
4 ;;; (c)1994-2009 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Mon Sep 28 10:46:39 2009 on firestorm
6 ;;; $Id$
8 ;; General variables
9 (defvar YaTeX-dos (memq system-type '(ms-dos windows-nt OS/2)))
10 (defvar YaTeX-macos (memq system-type '(darwin)))
11 (defvar YaTeX-emacs-19 (>= (string-to-int emacs-version) 19))
12 (defvar YaTeX-emacs-20 (>= (string-to-int emacs-version) 20))
13 (defvar YaTeX-emacs-21 (>= (string-to-int emacs-version) 21))
14 (defvar YaTeX-user-completion-table
15 (if YaTeX-dos "~/_yatexrc" "~/.yatexrc")
16 "*Default filename in which user completion table is saved.")
18 (defvar YaTeX-display-color-p
19 (or (and (fboundp 'display-color-p) (display-color-p))
20 (and (fboundp 'device-class)
21 (eq 'color (device-class (selected-device))))
22 window-system) ; falls down lazy check..
23 "Current display's capability of expressing colors.")
25 (defvar YaTeX-japan
26 (or (boundp 'NEMACS)
27 (boundp 'MULE)
28 (and (boundp 'current-language-environment)
29 (string-match "[Jj]apanese" current-language-environment)))
30 "Whether yatex mode is running on Japanese environment or not.")
32 ;; autoload from yahtml.el
33 (autoload 'yahtml-inner-environment-but "yahtml" "yahtml internal func." t)
35 (defvar latex-message-kanji-code 2
36 "*Kanji coding system latex command types out.
37 1 = Shift JIS, 2 = JIS, 3 = EUC. 4 = UTF-8")
39 (defvar YaTeX-kanji-code-alist
40 (cond
41 ((boundp '*junet*)
42 (list '(0 . *noconv*)
43 (cons
44 1
45 (cond
46 (YaTeX-dos (if (boundp '*sjis-dos*) *sjis-dos* *sjis*dos))
47 (YaTeX-macos (if (boundp '*sjis-mac*) *sjis-mac* *sjis*mac))
48 (t *sjis*)))
49 '(2 . *junet*) '(3 . *euc-japan*)))
50 ((and YaTeX-emacs-20 (featurep 'mule))
51 ;;(cdr-safe(assq 'coding-system (assoc "Japanese" language-info-alist)))
52 (list '(0 . no-conversion)
53 (cons
54 1 (cond (YaTeX-dos 'shift_jis-dos)
55 (YaTeX-macos 'shift_jis-mac)
56 ((member 'shift_jis (coding-system-list)) 'shift_jis-unix)
57 (t 'sjis)))
58 '(2 . iso-2022-jp-unix)
59 '(3 . euc-jp-unix)
60 '(4 . utf-8))))
61 "Kanji-code expression translation table.")
62 (defvar YaTeX-inhibit-prefix-letter nil
63 "*T for changing key definitions from [prefix] Letter to [prefix] C-Letter.")
65 (defvar YaTeX-no-begend-shortcut nil
66 "*T for disabling shortcut of begin-type completion, [prefix] b d, etc.")
68 (defvar YaTeX-default-pop-window-height 10
69 "Default typesetting buffer height.
70 If integer, sets the window-height of typesetting buffer.
71 If string, sets the percentage of it.
72 If nil, use default pop-to-buffer.")
74 (defvar YaTeX-create-file-prefix-g nil
75 "*Non-nil creates new file when [prefix] g on \\include{foo}.")
77 (defvar YaTeX-nervous t
78 "*If you are nervous about maintenance of yatexrc, set this value to T.
79 And you will have the local dictionary.")
81 (defvar YaTeX-use-italic-bold (string< "20" emacs-version)
82 "*Non-nil tries to find italic/bold fontset.
83 This variable is effective when font-lock is used.
84 \it, \bf 内部での日本語が□になってしまう場合はこれをnilにして下さい。")
86 ;----------- work variables ----------------------------------------
87 (defvar YaTeX-minibuffer-completion-map nil
88 "Minibuffer completion key map that allows comma completion.")
89 (if YaTeX-minibuffer-completion-map nil
90 (setq YaTeX-minibuffer-completion-map
91 (copy-keymap minibuffer-local-completion-map))
92 (define-key YaTeX-minibuffer-completion-map " "
93 'YaTeX-minibuffer-complete)
94 (define-key YaTeX-minibuffer-completion-map "\t"
95 'YaTeX-minibuffer-complete))
97 (defvar YaTeX-typesetting-mode-map nil
98 "Keymap used in YaTeX typesetting buffer")
100 (if YaTeX-typesetting-mode-map nil
101 (setq YaTeX-typesetting-mode-map (make-keymap))
102 ;(suppress-keymap YaTeX-typesetting-mode-map t)
103 (define-key YaTeX-typesetting-mode-map " " 'YaTeX-jump-error-line)
104 (define-key YaTeX-typesetting-mode-map "\C-m" 'YaTeX-send-string)
105 (define-key YaTeX-typesetting-mode-map "1" 'delete-other-windows)
106 (define-key YaTeX-typesetting-mode-map "0" 'delete-window)
107 (define-key YaTeX-typesetting-mode-map "q" 'delete-window))
109 (defvar YaTeX-parent-file nil
110 "*Main LaTeX source file name used when %#! expression doesn't exist.")
111 (make-variable-buffer-local 'YaTeX-parent-file)
113 ;---------- Define default key bindings on YaTeX mode map ----------
114 ;;;###autoload
115 (defun YaTeX-define-key (key binding &optional map)
116 "Define key on YaTeX-prefix-map."
117 (if YaTeX-inhibit-prefix-letter
118 (let ((c (aref key 0)))
119 (cond
120 ((and (>= c ?a) (<= c ?z)) (aset key 0 (1+ (- c ?a))))
121 ((and (>= c ?A) (<= c ?Z) (numberp YaTeX-inhibit-prefix-letter))
122 (aset key 0 (1+ (- c ?A))))
123 (t nil))))
124 (define-key (or map YaTeX-prefix-map) key binding))
126 ;;;###autoload
127 (defun YaTeX-local-table-symbol (symbol)
128 "Return the lisp symbol which keeps local completion table of SYMBOL."
129 (intern (concat "YaTeX$"
130 default-directory
131 (symbol-name symbol))))
133 ;;;###autoload
134 (defun YaTeX-sync-local-table (symbol)
135 "Synchronize local variable SYMBOL.
136 Copy its corresponding directory dependent completion table to SYMBOL."
137 (if (boundp (YaTeX-local-table-symbol symbol))
138 (set symbol (symbol-value (YaTeX-local-table-symbol symbol)))))
140 (defvar YaTeX-user-table-is-read nil
141 "Flag that means whether user completion table has been read or not.")
142 ;;;###autoload
143 (defun YaTeX-read-user-completion-table (&optional forcetoread)
144 "Append user completion table of LaTeX macros"
145 (interactive)
146 (let*((user-table (expand-file-name YaTeX-user-completion-table))
147 (local-table (expand-file-name (file-name-nondirectory user-table)))
148 var localvar localbuf (curbuf (current-buffer)) sexp)
149 (if YaTeX-user-table-is-read nil
150 (message "Loading user completion table")
151 (if (file-exists-p user-table) (load-file user-table)
152 (message "Welcome to the field of YaTeX. I'm glad to see you!")))
153 (setq YaTeX-user-table-is-read t)
154 (cond
155 ((file-exists-p local-table)
156 (set-buffer (setq localbuf (find-file-noselect local-table)))
157 (widen)
158 (goto-char (point-min))
159 (while (re-search-forward "(setq \\([^ \t\n]+\\)" nil t)
160 (setq var (intern (buffer-substring
161 (match-beginning 1) (match-end 1)))
162 localvar (YaTeX-local-table-symbol var))
163 (goto-char (match-beginning 0))
164 (setq sexp (buffer-substring (point)
165 (progn (forward-sexp) (point))))
166 (set-buffer curbuf)
167 (or (assq var (buffer-local-variables)) (make-local-variable var))
168 (eval (read sexp))
169 (or (and (boundp localvar)
170 (symbol-value localvar)
171 (not forcetoread))
172 (set localvar (symbol-value var)))
173 (set-buffer localbuf))
174 (kill-buffer localbuf)))
175 (set-buffer curbuf)))
177 ;;;###autoload
178 (defun YaTeX-reload-dictionary ()
179 "Reload local dictionary.
180 Use this function after editing ./.yatexrc."
181 (interactive)
182 (let ((YaTeX-user-table-is-read nil))
183 (YaTeX-read-user-completion-table t)))
185 ;;;###autoload
186 (defun YaTeX-lookup-table (word type)
187 "Lookup WORD in completion table whose type is TYPE.
188 This function refers the symbol tmp-TYPE-table, user-TYPE-table, TYPE-table.
189 Typically, TYPE is one of 'env, 'section, 'fontsize, 'singlecmd."
190 (if (symbolp type) (setq type (symbol-name type)))
191 (or (assoc word (symbol-value (intern (concat "tmp-" type "-table"))))
192 (assoc word (symbol-value (intern (concat "user-" type "-table"))))
193 (assoc word (symbol-value (intern (concat type "-table"))))))
195 ;;;###autoload
196 (defun YaTeX-update-table (vallist default-table user-table local-table)
197 "Update completion table if the car of VALLIST is not in current tables.
198 Second argument DEFAULT-TABLE is the quoted symbol of default completion
199 table, third argument USER-TABLE is user table which will be saved in
200 YaTeX-user-completion-table, fourth argument LOCAL-TABLE should have the
201 completion which is valid during current Emacs's session. If you
202 want to make LOCAL-TABLE valid longer span (but restrict in this directory)
203 create the file in current directory which has the same name with
204 YaTeX-user-completion-table."
205 (let ((car-v (car vallist)) key answer
206 (file (file-name-nondirectory YaTeX-user-completion-table)))
207 (cond
208 ((assoc car-v (symbol-value default-table))
209 nil) ;Nothing to do
210 ((setq key (assoc car-v (symbol-value user-table)))
211 (if (equal (cdr vallist) (cdr key)) nil
212 ;; if association hits, but contents differ.
213 (message
214 "%s's attributes turned into %s" (car vallist) (cdr vallist))
215 (set user-table (delq key (symbol-value user-table)))
216 (set user-table (cons vallist (symbol-value user-table)))
217 (YaTeX-update-dictionary
218 YaTeX-user-completion-table user-table "user")))
219 ((setq key (assoc car-v (symbol-value local-table)))
220 (if (equal (cdr vallist) (cdr key)) nil
221 (message
222 "%s's attributes turned into %s" (car vallist) (cdr vallist))
223 (set local-table (delq key (symbol-value local-table)))
224 (set local-table (cons vallist (symbol-value local-table)))
225 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
226 (YaTeX-update-dictionary file local-table)))
227 ;; All of above cases, there are some completion in tables.
228 ;; Then update tables.
229 (t
230 (if (not YaTeX-nervous)
231 (setq answer "u")
232 (message
233 (cond
234 (YaTeX-japan
235 "`%s'の登録先: U)ユーザ辞書 L)ローカル辞書 N)メモリ D)しない")
236 (t
237 "Register `%s' into: U)serDic L)ocalDic N)one D)iscard"))
238 (if (> (length car-v) 23)
239 (concat (substring car-v 0 10) "..." (substring car-v -9))
240 car-v))
241 (setq answer (char-to-string (read-char))))
242 (cond
243 ((string-match answer "uy")
244 (set user-table (cons vallist (symbol-value user-table)))
245 (YaTeX-update-dictionary YaTeX-user-completion-table user-table "user")
246 )
247 ((string-match answer "tl")
248 (set local-table (cons vallist (symbol-value local-table)))
249 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
250 (YaTeX-update-dictionary file local-table))
251 ((string-match answer "d") nil) ;discard it
252 (t (set default-table
253 (cons vallist (symbol-value default-table)))))))))
255 ;;;###autoload
256 (defun YaTeX-cplread-with-learning
257 (prom default-table user-table local-table
258 &optional pred reqmatch init hsym)
259 "Completing read with learning.
260 Do a completing read with prompt PROM. Completion table is what
261 DEFAULT-TABLE, USER-TABLE, LOCAL table are appended in reverse order.
262 Note that these tables are passed by the symbol.
263 Optional arguments PRED, REQMATH and INIT are passed to completing-read
264 as its arguments PREDICATE, REQUIRE-MATCH and INITIAL-INPUT respectively.
265 If optional 8th argument HSYM, history symbol, is passed, use it as
266 history list variable."
267 (YaTeX-sync-local-table local-table)
268 (let*((table (append (symbol-value local-table)
269 (symbol-value user-table)
270 (symbol-value default-table)))
271 (word (completing-read-with-history
272 prom table pred reqmatch init hsym)))
273 (if (and (string< "" word) (not (assoc word table)))
274 (YaTeX-update-table (list word) default-table user-table local-table))
275 word))
277 ;;;###autoload
278 (defun YaTeX-update-dictionary (file symbol &optional type)
279 (let ((local-table-buf (find-file-noselect file))
280 (name (symbol-name symbol))
281 (value (symbol-value symbol)))
282 (save-excursion
283 (message "Updating %s dictionary..." (or type "local"))
284 (set-buffer local-table-buf)
285 (goto-char (point-max))
286 (search-backward (concat "(setq " name) nil t)
287 (delete-region (point) (progn (forward-sexp) (point)))
288 (delete-blank-lines)
289 (insert "(setq " name " '(\n")
290 (mapcar '(lambda (s)
291 (insert (format "%s\n" (prin1-to-string s))))
292 value)
293 (insert "))\n\n")
294 (delete-blank-lines)
295 (basic-save-buffer)
296 (kill-buffer local-table-buf)
297 (message "Updating %s dictionary...Done" (or type "local")))))
299 ;;;###autoload
300 (defun YaTeX-define-begend-key-normal (key env &optional map)
301 "Define short cut YaTeX-make-begin-end key."
302 (YaTeX-define-key
303 key
304 (list 'lambda '(arg) '(interactive "P")
305 (list 'YaTeX-insert-begin-end env 'arg))
306 map))
308 ;;;###autoload
309 (defun YaTeX-define-begend-region-key (key env &optional map)
310 "Define short cut YaTeX-make-begin-end-region key."
311 (YaTeX-define-key key (list 'lambda nil '(interactive)
312 (list 'YaTeX-insert-begin-end env t)) map))
314 ;;;###autoload
315 (defun YaTeX-define-begend-key (key env &optional map)
316 "Define short cut key for begin type completion.
317 Define both strokes for normal and region mode.
318 To customize YaTeX, user should use this function."
319 (YaTeX-define-begend-key-normal key env map)
320 (if YaTeX-inhibit-prefix-letter nil
321 (YaTeX-define-begend-region-key
322 (concat (upcase (substring key 0 1)) (substring key 1)) env)))
324 ;;;###autoload
325 (defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func)
326 "Search STRING which is not commented out by CMNTRX.
327 Optional arguments after BOUND, ERR, CNT are passed literally to search-forward
328 or search-backward.
329 Optional sixth argument FUNC changes search-function."
330 (let ((sfunc (or func 'search-forward)) found md)
331 (while (and (prog1
332 (setq found (funcall sfunc string bound err cnt))
333 (setq md (match-data)))
334 (or
335 (and (eq major-mode 'yatex-mode)
336 (YaTeX-in-verb-p (match-beginning 0)))
337 (save-excursion
338 (goto-char (match-beginning 0))
339 (beginning-of-line)
340 (re-search-forward cmntrx (match-beginning 0) t)))))
341 (store-match-data md)
342 found))
344 (defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt)
345 "Search REGEXP backward which is not commented out by regexp CMNTRX.
346 See also YaTeX-search-active-forward."
347 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward))
349 (defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt)
350 "Search STRING backward which is not commented out by regexp CMNTRX.
351 See also YaTeX-search-active-forward."
352 (YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward))
354 (defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt)
355 "Search REGEXP backward which is not commented out by regexp CMNTRX.
356 See also YaTeX-search-active-forward."
357 (YaTeX-search-active-forward
358 regexp cmntrx bound err cnt 're-search-backward))
360 (defun YaTeX-relative-path-p (path)
361 "Return non-nil if PATH is not absolute one."
362 (let ((md (match-data)))
363 (unwind-protect
364 (not (string-match "^\\(/\\|[a-z]:\\|\\\\\\).*/" file))
365 (store-match-data md))))
367 ;;;###autoload
368 (defun YaTeX-switch-to-buffer (file &optional setbuf)
369 "Switch to buffer if buffer exists, find file if not.
370 Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer."
371 (interactive "Fswitch to file: ")
372 (if (bufferp file)
373 (setq file (buffer-file-name file))
374 (and (YaTeX-relative-path-p file)
375 (eq major-mode 'yatex-mode)
376 YaTeX-search-file-from-top-directory
377 (save-excursion
378 (YaTeX-visit-main t)
379 (setq file (expand-file-name file)))))
380 (let (buf (hilit-auto-highlight (not setbuf)))
381 (cond
382 ((setq buf (get-file-buffer file))
383 (funcall (if setbuf 'set-buffer 'switch-to-buffer)
384 (get-file-buffer file))
385 buf)
386 ((or YaTeX-create-file-prefix-g (file-exists-p file))
387 (or ;find-file returns nil but set current-buffer...
388 (if setbuf (set-buffer (find-file-noselect file))
389 (find-file file))
390 (current-buffer)))
391 (t (message "%s was not found in this directory." file)
392 nil))))
394 ;;;###autoload
395 (defun YaTeX-switch-to-buffer-other-window (file)
396 "Switch to buffer if buffer exists, find file if not."
397 (interactive "Fswitch to file: ")
398 (and (eq major-mode 'yatex-mode)
399 (stringp file)
400 (YaTeX-relative-path-p file)
401 YaTeX-search-file-from-top-directory
402 (save-excursion
403 (YaTeX-visit-main t)
404 (setq file (expand-file-name file))))
405 (if (bufferp file) (setq file (buffer-file-name file)))
406 (cond
407 ((get-file-buffer file)
408 (switch-to-buffer-other-window (get-file-buffer file))
409 t)
410 ((or YaTeX-create-file-prefix-g (file-exists-p file))
411 (find-file-other-window file) t)
412 (t (message "%s was not found in this directory." file)
413 nil)))
415 (defun YaTeX-get-file-buffer (file)
416 "Return the FILE's buffer.
417 Base directory is that of main file or current directory."
418 (let (dir main (cdir default-directory))
419 (or (and (eq major-mode 'yatex-mode)
420 YaTeX-search-file-from-top-directory
421 (save-excursion
422 (YaTeX-visit-main t)
423 (get-file-buffer file)))
424 (get-file-buffer file))))
426 (defun YaTeX-replace-format-sub (string format repl)
427 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
428 (string-match (concat "[^%]\\(%" format "\\)") string)))
429 (len (length format)))
430 (if (null beg) string ;no conversion
431 (concat
432 (substring string 0 (match-beginning 1)) (or repl "")
433 (substring string (match-end 1))))))
435 ;;;###autoload
436 (defun YaTeX-replace-format (string format repl)
437 "In STRING, replace first appearance of FORMAT to REPL as if
438 function `format' does. FORMAT does not contain `%'"
439 (let ((ans string) (case-fold-search nil))
440 (while (not (string=
441 ans (setq string (YaTeX-replace-format-sub ans format repl))))
442 (setq ans string))
443 string))
445 ;;;###autoload
446 (defun YaTeX-replace-formats (string replace-list)
447 (let ((list replace-list))
448 (while list
449 (setq string (YaTeX-replace-format
450 string (car (car list)) (cdr (car list)))
451 list (cdr list)))
452 string))
454 ;;;###autoload
455 (defun YaTeX-replace-format-args (string &rest args)
456 "Translate the argument mark #1, #2, ... #n in the STRING into the
457 corresponding real arguments ARGS."
458 (let ((argp 1))
459 (while args
460 (setq string
461 (YaTeX-replace-format string (int-to-string argp) (car args)))
462 (setq args (cdr args) argp (1+ argp))))
463 string)
465 ;;;###autoload
466 (defun rindex (string char)
467 (let ((pos (1- (length string)))(index -1))
468 (while (>= pos 0)
469 (cond
470 ((= (aref string pos) char)
471 (setq index pos) (setq pos -1))
472 (t (setq pos (1- pos))))
473 )
474 index))
476 ;;;###autoload
477 (defun point-beginning-of-line ()
478 (save-excursion (beginning-of-line)(point)))
480 ;;;###autoload
481 (defun point-end-of-line ()
482 (save-excursion (end-of-line)(point)))
485 ;;;###autoload
486 (defun YaTeX-showup-buffer (buffer &optional func select)
487 "Make BUFFER show up in certain window (but current window)
488 that gives the maximum value by the FUNC. FUNC should take an argument
489 of its window object. Non-nil for optional third argument SELECT selects
490 that window. This function never selects minibuffer window."
491 (or (and (if (and YaTeX-emacs-19 select window-system)
492 (get-buffer-window buffer t)
493 (get-buffer-window buffer))
494 (progn
495 (if select
496 (goto-buffer-window buffer))
497 t))
498 (let ((window (selected-window))
499 (wlist (YaTeX-window-list)) win w (x 0))
500 (cond
501 ((> (length wlist) 2)
502 (if func
503 (while wlist
504 (setq w (car wlist))
505 (if (and (not (eq window w))
506 (> (funcall func w) x))
507 (setq win w x (funcall func w)))
508 (setq wlist (cdr wlist)))
509 (setq win (get-lru-window)))
510 (select-window win)
511 (switch-to-buffer buffer)
512 (or select (select-window window)))
513 ((= (length wlist) 2)
514 ;(other-window 1);This does not work properly on Emacs-19
515 (select-window (get-lru-window))
516 (switch-to-buffer buffer)
517 (or select (select-window window)))
518 (t ;if one-window
519 (cond
520 ((and YaTeX-emacs-19 window-system (get-buffer-window buffer t))
521 nil) ;if found in other frame
522 (YaTeX-default-pop-window-height
523 (split-window-calculate-height YaTeX-default-pop-window-height)
524 ;;(pop-to-buffer buffer) ;damn! emacs-19.30
525 (select-window (next-window nil 1))
526 (switch-to-buffer (get-buffer-create buffer))
527 (or select (select-window window)))
528 (t nil)))
529 ))))
531 (cond
532 ((fboundp 'screen-height)
533 (fset 'YaTeX-screen-height 'screen-height)
534 (fset 'YaTeX-screen-width 'screen-width))
535 ((fboundp 'frame-height)
536 (fset 'YaTeX-screen-height 'frame-height)
537 (fset 'YaTeX-screen-width 'frame-width))
538 (t (error "I don't know how to run windows.el on this Emacs...")))
540 ;;;###autoload
541 (defun split-window-calculate-height (height)
542 "Split current window wight specified HEIGHT.
543 If HEIGHT is number, make a new window that has HEIGHT lines.
544 If HEIGHT is string, make a new window that occupies HEIGT % of screen height.
545 Otherwise split window conventionally."
546 (if (one-window-p t)
547 (split-window
548 (selected-window)
549 (max
550 (min
551 (- (YaTeX-screen-height)
552 (if (numberp height)
553 (+ height 2)
554 (/ (* (YaTeX-screen-height)
555 (string-to-int height))
556 100)))
557 (- (YaTeX-screen-height) window-min-height 1))
558 window-min-height))))
560 ;;;###autoload
561 (defun YaTeX-window-list ()
562 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
563 (while (not (eq curw (setq win (next-window win))))
564 (or (eq win (minibuffer-window))
565 (setq wlist (cons win wlist))))
566 wlist))
568 (if YaTeX-emacs-21
569 ;; Emacs-21's next-window returns other frame's window even if called
570 ;; with argument ALL-FRAMES nil, when called from minibuffer context.
571 ;; Therefore, check frame identity here.
572 (defun YaTeX-window-list ()
573 (let*((curw (selected-window)) (win curw) (wlist (list curw))
574 (curf (window-frame curw)))
575 (while (and (not (eq curw (setq win (next-window win))))
576 (eq curf (window-frame win)))
577 (or (eq win (minibuffer-window))
578 (setq wlist (cons win wlist))))
579 wlist)))
581 ;;;###autoload
582 (defun substitute-all-key-definition (olddef newdef keymap)
583 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
584 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
585 where ever it appears."
586 (if YaTeX-emacs-19
587 (substitute-key-definition olddef newdef keymap global-map)
588 (mapcar
589 (function (lambda (key) (define-key keymap key newdef)))
590 (where-is-internal olddef keymap))))
592 ;;;###autoload
593 (defun YaTeX-match-string (n &optional m)
594 "Return (buffer-substring (match-beginning n) (match-beginning m))."
595 (if (match-beginning n)
596 (YaTeX-buffer-substring (match-beginning n)
597 (match-end (or m n)))))
599 ;;;###autoload
600 (defun YaTeX-minibuffer-complete ()
601 "Complete in minibuffer.
602 If the symbol 'delim is bound and is string, its value is assumed to be
603 the character class of delimiters. Completion will be performed on
604 the last field separated by those delimiters.
605 If the symbol 'quick is bound and is 't, when the try-completion results
606 in t, exit minibuffer immediately."
607 (interactive)
608 (save-restriction
609 (narrow-to-region
610 (if (fboundp 'field-beginning) (field-beginning (point-max)) (point-min))
611 (point-max))
612 (let ((md (match-data)) beg word compl
613 (quick (and (boundp 'quick) (eq quick t)))
614 (displist ;function to display completion-list
615 (function
616 (lambda ()
617 (with-output-to-temp-buffer "*Completions*"
618 (display-completion-list
619 (all-completions word minibuffer-completion-table)))))))
620 (setq beg (if (and (boundp 'delim) (stringp delim))
621 (save-excursion
622 (skip-chars-backward (concat "^" delim))
623 (point))
624 (point-min))
625 word (buffer-substring beg (point-max))
626 compl (try-completion word minibuffer-completion-table))
627 (cond
628 ((eq compl t)
629 (if quick (exit-minibuffer)
630 (let ((p (point)) (max (point-max)))
631 (unwind-protect
632 (progn
633 (goto-char max)
634 (insert " [Sole completion]")
635 (goto-char p)
636 (sit-for 1))
637 (delete-region max (point-max))
638 (goto-char p)))))
639 ((eq compl nil)
640 (ding)
641 (save-excursion
642 (let (p)
643 (unwind-protect
644 (progn
645 (goto-char (setq p (point-max)))
646 (insert " [No match]")
647 (goto-char p)
648 (sit-for 2))
649 (delete-region p (point-max))))))
650 ((string= compl word)
651 (funcall displist))
652 (t (delete-region beg (point-max))
653 (insert compl)
654 (if quick
655 (if (eq (try-completion compl minibuffer-completion-table) t)
656 (exit-minibuffer)
657 (funcall displist)))))
658 (store-match-data md))))
660 (defun YaTeX-minibuffer-quick-complete ()
661 "Set 'quick to 't and call YaTeX-minibuffer-complete.
662 See documentation of YaTeX-minibuffer-complete."
663 (interactive)
664 (let ((quick t))
665 (self-insert-command 1)
666 (YaTeX-minibuffer-complete)))
668 (defun foreach-buffers (pattern job)
669 "For each buffer which matches with PATTERN, do JOB."
670 (let ((list (buffer-list)))
671 (save-excursion
672 (while list
673 (set-buffer (car list))
674 (if (or (and (stringp pattern)
675 (buffer-file-name)
676 (string-match pattern (buffer-file-name)))
677 (and (symbolp pattern) major-mode (eq major-mode pattern)))
678 (eval job))
679 (setq list (cdr list))))))
681 (defun goto-buffer-window (buffer)
682 "Select window which is bound to BUFFER.
683 If no such window exist, switch to buffer BUFFER."
684 (interactive "BGoto buffer: ")
685 (if (stringp buffer)
686 (setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
687 (if (get-buffer buffer)
688 (cond
689 ((get-buffer-window buffer)
690 (select-window (get-buffer-window buffer)))
691 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
692 (let*((win (get-buffer-window buffer t))
693 (frame (window-frame win)))
694 (select-frame frame)
695 (raise-frame frame)
696 (focus-frame frame)
697 (select-window win)
698 (set-mouse-position frame 0 0)
699 (and (featurep 'windows) (fboundp 'win:adjust-window)
700 (win:adjust-window))))
701 ((and (featurep 'windows) (fboundp 'win:get-buffer-window)
702 (let ((w (win:get-buffer-window buffer)))
703 (and w (win:switch-window w))))
704 (select-window (get-buffer-window buffer)))
705 (t (switch-to-buffer buffer)))))
707 ;; Here starts the functions which support gmhist-vs-Emacs19 compatible
708 ;; reading with history.
709 ;;;###autoload
710 (defun completing-read-with-history
711 (prompt table &optional predicate must-match initial hsym)
712 "Completing read with general history: gmhist, Emacs-19."
713 (let ((minibuffer-history
714 (or (symbol-value hsym)
715 (and (boundp 'minibuffer-history) minibuffer-history)))
716 (minibuffer-history-symbol (or hsym 'minibuffer-history)))
717 (prog1
718 (if (fboundp 'completing-read-with-history-in)
719 (completing-read-with-history-in
720 minibuffer-history-symbol prompt table predicate must-match initial)
721 (completing-read prompt table predicate must-match initial))
722 (if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
724 ;;;###autoload
725 (defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
726 "Read from minibuffer with general history: gmhist, Emacs-19."
727 (cond
728 (YaTeX-emacs-19
729 (read-from-minibuffer prompt init map read hsym))
730 (t
731 (let ((minibuffer-history-symbol hsym))
732 (read-from-minibuffer prompt init map read)))))
734 ;;;###autoload
735 (defun read-string-with-history (prompt &optional init hsym)
736 "Read string with history: gmhist(Emacs-18) and Emacs-19."
737 (cond
738 (YaTeX-emacs-19
739 (read-from-minibuffer prompt init minibuffer-local-map nil hsym))
740 ((featurep 'gmhist-mh)
741 (read-with-history-in hsym prompt init))
742 (t (read-string prompt init))))
744 ;;;###autoload
745 (fset 'YaTeX-rassoc
746 (if (and nil (fboundp 'rassoc) (subrp (symbol-function 'rassoc)))
747 (symbol-function 'rassoc)
748 (function
749 (lambda (key list)
750 (let ((l list))
751 (catch 'found
752 (while l
753 (if (equal key (cdr (car l)))
754 (throw 'found (car l)))
755 (setq l (cdr l)))))))))
757 (defun YaTeX-insert-file-contents (file visit &optional beg end)
758 (cond
759 ((and (string< "19" emacs-version) (not (featurep 'xemacs)))
760 (insert-file-contents file visit beg end))
761 ((string-match "unix\\|linux" (symbol-name system-type))
762 (let ((default-process-coding-system
763 (and (boundp '*noconv*) (list '*noconv*)))
764 (file-coding-system (and (boundp '*noconv*) '*noconv*))
765 kanji-fileio-code
766 (default-process-kanji-code 0))
767 (call-process shell-file-name file (current-buffer) nil
768 (or (and (boundp 'shell-command-option)
769 shell-command-option)
770 "-c")
771 (format "dd bs=1 count=%d | tail -c +%d" end beg))))
772 (t (insert-file-contents file))))
774 (defun YaTeX-split-string (str &optional sep null)
775 "Split string STR by every occurrence of SEP(regexp).
776 If the optional second argument SEP is nil, it defaults to \"[ \f\t\n\r\v]+\".
777 Do not include null string by default. Non-nil for optional third argument
778 NULL includes null string in a list."
779 (let ((sep (or sep "[ \f\t\n\r\v]+"))
780 list m)
781 (while str
782 (if (setq m (string-match sep str))
783 (progn
784 (if (or (> m 0) null)
785 (setq list (cons (substring str 0 m) list)))
786 (setq str (substring str (match-end 0))))
787 (if (or null (string< "" str))
788 (setq list (cons str list)))
789 (setq str nil)))
790 (nreverse list)))
792 ;;;###autoload
793 (defun YaTeX-delete1 (elt list)
794 "Delete"
795 (let (e)
796 (while (setq e (YaTeX-member elt list))
797 (setq list (delq (car e) list))))
798 list)
799 (if (fboundp 'delete)
800 (fset 'YaTeX-delete (symbol-function 'delete))
801 (fset 'YaTeX-delete (symbol-function 'YaTeX-delete1)))
803 (defun YaTeX-member1 (elt list)
804 (catch 'found
805 (while list
806 (if (equal elt (car list))
807 (throw 'found list))
808 (setq list (cdr list)))))
810 (if (and (fboundp 'member) (subrp (symbol-function 'member)))
811 (fset 'YaTeX-member (symbol-function 'member))
812 (fset 'YaTeX-member (symbol-function 'YaTeX-member1)))
814 ;;;
815 ;; Interface function for windows.el
816 ;;;
817 ;;;###autoload
818 (defun YaTeX-switch-to-window ()
819 "Switch to windows.el's window decided by last pressed key."
820 (interactive)
821 (or (featurep 'windows) (error "Why don't you use `windows.el'?"))
822 (win-switch-to-window 1 (- last-command-char win:base-key)))
824 ;;;###autoload
825 (defun YaTeX-reindent (col)
826 "Remove current indentation and reindento to COL column."
827 (save-excursion
828 (beginning-of-line)
829 (skip-chars-forward " \t")
830 (if (/= col (current-column))
831 (progn
832 (delete-region (point) (progn (beginning-of-line) (point)))
833 (indent-to col))))
834 (skip-chars-forward " \t" (point-end-of-line)))
836 (defun YaTeX-inner-environment (&optional quick)
837 "Return current inner-most environment.
838 Non-nil for optional argument QUICK restricts search bound to most
839 recent sectioning command. Matching point is stored to property 'point
840 of 'YaTeX-inner-environment, which can be referred by
841 (get 'YaTeX-inner-environment 'point)."
842 (put 'YaTeX-inner-environment 'point (point-min))
843 (put 'YaTeX-inner-environment 'indent 0)
844 (let*((nest 0)
845 (beg (YaTeX-replace-format-args
846 (regexp-quote YaTeX-struct-begin)
847 ;YaTeX-struct-begin ;=== TENTATIVE!! ==
848 YaTeX-struct-name-regexp
849 (if (eq major-mode 'yahtml-mode) "\\s *.*" "")
850 ""))
851 (end (YaTeX-replace-format-args
852 (regexp-quote YaTeX-struct-end)
853 YaTeX-struct-name-regexp "" ""))
854 (begend (concat "\\(" beg "\\)\\|\\(" end "\\)"))
855 bound m0
856 (htmlp (eq major-mode 'yahtml-mode))
857 (open
858 (concat "^" (or (cdr (assq major-mode '((yahtml-mode . "<")))) "{")))
859 (close
860 (concat "^"
861 (or (cdr(assq major-mode '((yahtml-mode . "\n\t >")))) "}"))))
862 (save-excursion
863 (if quick
864 (setq bound
865 (save-excursion
866 (if htmlp
867 ;;(re-search-backward YaTeX-sectioning-regexp nil 1)
868 ;;(goto-char (point-min)) ;Is this enough? 97/6/26
869 (re-search-backward yahtml-indentation-boundary nil 1)
870 (YaTeX-re-search-active-backward
871 (concat YaTeX-ec-regexp
872 "\\(" YaTeX-sectioning-regexp "\\)\\*?{")
873 YaTeX-comment-prefix nil 1))
874 (or (bobp) (end-of-line))
875 (point))))
876 (if (catch 'begin
877 (if (and (numberp bound) (< (point) bound)) (throw 'begin nil))
878 (while (YaTeX-re-search-active-backward
879 begend YaTeX-comment-prefix bound t)
880 (setq m0 (match-beginning 0))
881 (if (looking-at end) ;;(match-beginning 2)
882 (setq nest (1+ nest))
883 (setq nest (1- nest)))
884 (if (< nest 0)
885 (progn
886 (put 'YaTeX-inner-environment 'point m0)
887 (goto-char m0)
888 (put 'YaTeX-inner-environment 'indent (current-column))
889 (throw 'begin t)))))
890 (buffer-substring
891 (progn (skip-chars-forward open) (1+ (point)))
892 (progn (skip-chars-forward close) (point)))))))
894 (defun YaTeX-goto-corresponding-environment (&optional allow-mismatch noerr)
895 "Go to corresponding begin/end enclosure.
896 Optional argument ALLOW-MISMATCH allows mismatch open/clese. Use this
897 for \left(, \right).
898 Optional third argument NOERR causes no error for unballanced environment."
899 (interactive)
900 (if (not (YaTeX-on-begin-end-p)) nil
901 (let ((p (match-end 0)) b0 b1 env (nest 0) regexp re-s (op (point))
902 (m0 (match-beginning 0)) ;whole matching
903 (m1 (match-beginning 1)) ;environment in \begin{}
904 (m2 (match-beginning 2)) ;environment in \end{}
905 (m3 (match-beginning 3))) ;environment in \[ \] \( \)
906 ;(setq env (regexp-quote (buffer-substring p (match-beginning 0))))
907 (if (cond
908 (m1 ;if begin{xxx}
909 (setq env
910 (if allow-mismatch YaTeX-struct-name-regexp
911 (regexp-quote (buffer-substring m1 (match-end 1)))))
912 ; (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
913 ; "\\(\\\\begin{" env "}\\)"))
914 (setq regexp
915 (concat
916 "\\("
917 (YaTeX-replace-format-args
918 (regexp-quote YaTeX-struct-end) env "" "")
919 "\\)\\|\\("
920 (YaTeX-replace-format-args
921 (regexp-quote YaTeX-struct-begin) env "" "")
922 "\\)"))
923 (setq re-s 're-search-forward))
924 (m2 ;if end{xxx}
925 (setq env
926 (if allow-mismatch YaTeX-struct-name-regexp
927 (regexp-quote (buffer-substring m2 (match-end 2)))))
928 ; (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
929 ; "\\(\\\\end{" env "}\\)"))
930 (setq regexp
931 (concat
932 "\\("
933 (YaTeX-replace-format-args
934 (regexp-quote YaTeX-struct-begin) env "" "")
935 "\\)\\|\\("
936 (YaTeX-replace-format-args
937 (regexp-quote YaTeX-struct-end) env "" "")
938 "\\)"))
939 (setq re-s 're-search-backward))
940 (m3 ;math environment
941 (setq env (char-after (1+ m3))
942 regexp (format "\\(%s%s\\)\\|\\(%s%s\\)"
943 YaTeX-ec-regexp
944 (regexp-quote
945 (cdr (assq env '((?( . ")") (?) . "(")
946 (?[ . "]") (?] . "[")))))
947 YaTeX-ec-regexp
948 (regexp-quote (char-to-string env)))
949 re-s (if (memq env '(?\( ?\[))
950 're-search-forward
951 're-search-backward)))
952 (t (if noerr nil (error "Corresponding environment not found."))))
953 (progn
954 (while (and (>= nest 0) (funcall re-s regexp nil t))
955 (setq b0 (match-beginning 0) b1 (match-beginning 1))
956 (if (or (equal b0 m0)
957 (YaTeX-literal-p b0))
958 nil
959 (setq nest (if (equal b0 b1)
960 (1- nest) (1+ nest)))))
961 (if (< nest 0)
962 (goto-char (match-beginning 0)) ;found.
963 (goto-char op)
964 (funcall
965 (if noerr 'message 'error)
966 "Corresponding environment `%s' not found." env)
967 (sit-for 1)
968 nil))))))
970 (defun YaTeX-end-environment ()
971 "Close opening environment"
972 (interactive)
973 (let ((env (YaTeX-inner-environment)))
974 (if (not env) (error "No premature environment")
975 (save-excursion
976 (if (YaTeX-search-active-forward
977 (YaTeX-replace-format-args YaTeX-struct-end env "" "")
978 YaTeX-comment-prefix nil t)
979 (if (y-or-n-p
980 (concat "Environment `" env
981 "' may be already closed. Force close?"))
982 nil
983 (error "end environment aborted."))))
984 (message "") ;Erase (y or n) message.
985 (YaTeX-insert-struc 'end env)
986 (save-excursion
987 (goto-char (or (get 'YaTeX-inner-environment 'point) (match-end 0)))
988 (if (pos-visible-in-window-p)
989 (sit-for (if YaTeX-dos 2 1))
990 (message "Matches with %s at line %d"
991 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
992 (count-lines (point-min) (point))))))))
994 (defun YaTeX-beginning-of-environment (&optional limit-search-bound end)
995 "Goto the beginning of the current environment.
996 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound to
997 most recent sectioning command. Non-nil for optional third argument END
998 goes to end of environment."
999 (interactive)
1000 (let ((op (point)))
1001 (if (YaTeX-inner-environment limit-search-bound)
1002 (progn
1003 (goto-char (get 'YaTeX-inner-environment 'point))
1004 (and end (YaTeX-goto-corresponding-environment))
1005 (if (interactive-p) (push-mark op))
1006 (point)))))
1008 (defun YaTeX-end-of-environment (&optional limit-search-bound)
1009 "Goto the end of the current environment.
1010 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound
1011 to most recent sectioning command."
1012 (interactive)
1013 (YaTeX-beginning-of-environment limit-search-bound t))
1015 (defun YaTeX-mark-environment ()
1016 "Mark current position and move point to end of environment."
1017 (interactive)
1018 (let ((curp (point)))
1019 (if (and (YaTeX-on-begin-end-p) (match-beginning 1)) ;if on \\begin
1020 (progn (goto-char (match-end 0)))
1021 (if (= (char-after (point)) ?\\) nil ;if on \\end
1022 (skip-chars-backward "^\n\\\\")
1023 (or (bolp) (forward-char -1))))
1024 (if (not (YaTeX-end-of-environment)) ;arg1 turns to match-beginning 1
1025 (progn
1026 (goto-char curp)
1027 (error "Cannot found the end of current environment."))
1028 (YaTeX-goto-corresponding-environment)
1029 (beginning-of-line) ;for confirmation
1030 (if (< curp (point))
1031 (progn
1032 (message "Mark this environment?(y or n): ")
1033 (if (= (read-char) ?y) nil
1034 (goto-char curp)
1035 (error "Abort. Please call again at more proper position."))))
1036 (set-mark-command nil)
1037 (YaTeX-goto-corresponding-environment)
1038 (end-of-line)
1039 (if (eobp) nil (forward-char 1)))))
1041 (defun YaTeX-kill-buffer (buffer)
1042 "Make effort to show parent buffer after kill."
1043 (interactive "bKill buffer: ")
1044 (or (get-buffer buffer)
1045 (error "No such buffer %s" buffer))
1046 (let ((pf YaTeX-parent-file))
1047 (kill-buffer buffer)
1048 (and pf
1049 (get-file-buffer pf)
1050 (switch-to-buffer (get-file-buffer pf)))))
1052 ;;;VER2
1053 (defun YaTeX-insert-struc (what env)
1054 (cond
1055 ((eq what 'begin)
1056 (insert (YaTeX-replace-format-args
1057 YaTeX-struct-begin env (YaTeX-addin env))))
1058 ((eq what 'end)
1059 (insert (YaTeX-replace-format-args YaTeX-struct-end env)))
1060 (t nil)))
1062 (defun YaTeX-string-width (str)
1063 "Return the display width of string."
1064 (if (fboundp 'string-width)
1065 (string-width str)
1066 (length str)))
1067 (defun YaTeX-truncate-string-width (str width)
1068 (cond
1069 ((fboundp 'truncate-string-to-width) (truncate-string-to-width str width))
1070 ((fboundp 'truncate-string) (truncate-string str width))
1071 (t (substring str 0 width))))
1073 ;;; Function for menu support
1074 (defun YaTeX-define-menu (keymap bindlist)
1075 "Define KEYMAP(symbol)'s menu-bindings according to BINDLIST.
1076 KEYMAP should be a quoted symbol of newly allocated keymap.
1077 BINDLIST consists of binding list. Each element is as follows.
1079 '(menusymbol DOC_String . contents)
1081 CONTENTS is one of lambda-form, interactive function, or other keymap.
1082 See yatex19.el for example."
1083 (cond
1084 ((featurep 'xemacs)
1085 (let (name)
1086 (if (keymapp (symbol-value keymap))
1087 (progn
1088 (setq name (keymap-name (symbol-value keymap)))
1089 (set keymap nil))
1090 (setq name (car (symbol-value keymap)))
1091 (set keymap (cdr (symbol-value keymap))))
1092 (mapcar
1093 (function
1094 (lambda (bind)
1095 (setq bind (cdr bind))
1096 (if (eq (car-safe (cdr bind)) 'lambda)
1097 (setcar (cdr bind) 'progn))
1098 (if (stringp (car-safe (cdr bind)))
1099 (set keymap
1100 (cons (cdr bind) (symbol-value keymap)))
1101 (set keymap
1102 (cons (vector (car bind) (cdr bind) t)
1103 (symbol-value keymap))))))
1104 bindlist)
1105 (set keymap (cons name (symbol-value keymap)))))
1106 (t
1107 (mapcar
1108 (function
1109 (lambda (bind)
1110 (define-key (symbol-value keymap) (vector (car bind)) (cdr bind))))
1111 bindlist))))
1113 ;;;
1114 ;; Emacs 21 compensational wrapper
1115 ;;;
1116 (defun YaTeX-minibuffer-begin ()
1117 (if (fboundp 'field-beginning)
1118 (field-beginning (point-max))
1119 (point-min)))
1121 (defun YaTeX-minibuffer-end ()
1122 (if (fboundp 'field-end)
1123 (field-end (point-max))
1124 (point-max)))
1126 (defun YaTeX-minibuffer-string ()
1127 (buffer-substring (YaTeX-minibuffer-begin) (YaTeX-minibuffer-end)))
1129 (defun YaTeX-minibuffer-erase ()
1130 (if (eq (selected-window) (minibuffer-window))
1131 (if (fboundp 'delete-field) (delete-field) (erase-buffer))))
1133 (fset 'YaTeX-buffer-substring
1134 (if (fboundp 'buffer-substring-no-properties)
1135 'buffer-substring-no-properties
1136 'buffer-substring))
1138 ;;;
1139 ;; hilit19 vs. font-lock
1140 ;;;
1141 (defvar YaTeX-19-functions-font-lock-direct
1142 '(YaTeX-19-re-search-in-env))
1144 (defun YaTeX-convert-pattern-hilit2fontlock (h19pa)
1145 "Convert hilit19's H19PA patterns alist to font-lock's one.
1146 This function is a makeshift for YaTeX and yahtml."
1147 (let ((ignorecase (not (null (car h19pa))))
1148 (palist (cdr h19pa))
1149 flpa i newface
1150 (mapping
1151 '((bold . YaTeX-font-lock-bold-face)
1152 (italic . YaTeX-font-lock-italic-face)
1153 (defun . font-lock-function-name-face)
1154 (define . font-lock-variable-name-face)
1155 (keyword . font-lock-keyword-face)
1156 (decl . YaTeX-font-lock-declaration-face)
1157 (label . YaTeX-font-lock-label-face)
1158 (crossref . YaTeX-font-lock-crossref-face)
1159 (include . YaTeX-font-lock-include-face)
1160 (formula . YaTeX-font-lock-formula-face)
1161 (delimiter . YaTeX-font-lock-delimiter-face)
1162 (string . ignore) (comment . ignore)
1163 )))
1164 (while (setq i (car palist))
1165 (setq newface (nth 2 i)
1166 newface (or (cdr (assq newface mapping)) newface))
1167 (cond
1168 ((eq newface 'ignore) nil) ;no translation
1169 ((stringp (car i)) ;hiliting by regexp
1170 (setq flpa
1171 (cons
1172 (if (numberp (car (cdr i)))
1173 (list (car i) ;regexp
1174 (car (cdr i)) ;matching group number
1175 newface nil) ;'keep) ;keep is hilit19 taste
1176 (list
1177 (concat
1178 (car i) ;original regexp and..
1179 ;;"[^"
1180 ;;(regexp-quote (substring (car (cdr i)) 0 1))
1181 ;;"]+" ;for shortest match
1182 ".*"
1183 (car (cdr i)))
1184 0 (list 'quote newface) nil)) ;;'keep))
1185 flpa)))
1186 ((and (symbolp (car i)) (fboundp (car i)))
1187 (if (memq (car i) YaTeX-19-functions-font-lock-direct)
1188 ;; Put direct function call for it.
1189 ;; When calling this function, fontify entire matched string.
1190 (setq flpa
1191 (cons
1192 (list
1193 (list 'lambda (list 'dummy) ;dummy should be boundary
1194 (list (car i) (list 'quote (car (cdr i)))))
1195 (list 0 newface))
1196 flpa))
1197 (setq flpa
1198 (cons
1199 (list (car (cdr i)) ;regexp
1200 (list
1201 (list
1202 'lambda (list 'dummy)
1203 '(goto-char (match-beginning 0))
1204 (if (eq (nth 3 i) 'overwrite)
1205 nil
1206 '(remove-text-properties
1207 (point) (min (point-max) (1+ (point)))
1208 '(face nil font-lock-multiline nil)))
1209 (list
1210 'let (list '(e (match-end 0))
1211 (list 'm (list (car i) (car (cdr i)))))
1212 (list
1213 'if 'm
1214 (list
1215 'YaTeX-font-lock-fillin
1216 (list 'car 'm)
1217 (list 'cdr 'm)
1218 (list 'quote 'face)
1219 (list 'quote 'font-lock)
1220 (list 'quote newface))
1221 '(goto-char e)
1222 ))
1223 nil) ;retun nil to cheat font-lock
1224 nil nil)) ;pre-match, post-match both nil
1225 flpa)))))
1226 (setq palist (cdr palist)));while
1227 (if (featurep 'xemacsp)
1228 (nreverse flpa)
1229 flpa)))
1231 (if (and (boundp 'YaTeX-use-font-lock)
1232 YaTeX-use-font-lock)
1233 (require 'font-lock))
1235 (cond
1236 ((and (featurep 'font-lock) (fboundp 'defface))
1237 ;; In each defface, '(class static-color) is for Emacs-21 -nw
1238 ;; '(class tty) is for XEmacs-21 -nw
1239 (defface YaTeX-font-lock-label-face
1240 '((((class static-color)) (:foreground "yellow" :underline t))
1241 (((type tty)) (:foreground "yellow" :underline t))
1242 (((class color) (background dark)) (:foreground "pink" :underline t))
1243 (((class color) (background light)) (:foreground "red" :underline t))
1244 (t (:bold t :underline t)))
1245 "Font Lock mode face used to highlight labels."
1246 :group 'font-lock-faces)
1247 (defvar YaTeX-font-lock-label-face 'YaTeX-font-lock-label-face)
1249 (defface YaTeX-font-lock-declaration-face
1250 '((((class color) (background dark)) (:foreground "cyan"))
1251 (((class color) (background light)) (:foreground "RoyalBlue"))
1252 (t (:bold t :underline t)))
1253 "Font Lock mode face used to highlight some declarations."
1254 :group 'font-lock-faces)
1255 (defvar YaTeX-font-lock-declaration-face 'YaTeX-font-lock-declaration-face)
1257 (defface YaTeX-font-lock-include-face
1258 '((((class color) (background dark)) (:foreground "Plum1"))
1259 (((class color) (background light)) (:foreground "purple"))
1260 (t (:bold t :underline t)))
1261 "Font Lock mode face used to highlight expression for including."
1262 :group 'font-lock-faces)
1263 (defvar YaTeX-font-lock-include-face 'YaTeX-font-lock-include-face)
1265 (defface YaTeX-font-lock-formula-face
1266 '((((class static-color)) (:bold t))
1267 (((type tty)) (:bold t))
1268 (((class color) (background dark)) (:foreground "khaki" :bold t))
1269 (((class color) (background light)) (:foreground "Goldenrod"))
1270 (t (:bold t :underline t)))
1271 "Font Lock mode face used to highlight formula."
1272 :group 'font-lock-faces)
1273 (defvar YaTeX-font-lock-formula-face 'YaTeX-font-lock-formula-face)
1275 (defface YaTeX-font-lock-delimiter-face
1276 '((((class static-color)) (:bold t))
1277 (((type tty)) (:bold t))
1278 (((class color) (background dark))
1279 (:foreground "saddlebrown" :background "ivory" :bold t))
1280 (((class color) (background light)) (:foreground "red"))
1281 (t (:bold t :underline t)))
1282 "Font Lock mode face used to highlight delimiters."
1283 :group 'font-lock-faces)
1284 (defvar YaTeX-font-lock-delimiter-face 'YaTeX-font-lock-delimiter-face)
1286 (defface YaTeX-font-lock-math-sub-face
1287 '((((class static-color)) (:bold t))
1288 (((type tty)) (:bold t))
1289 (((class color) (background dark))
1290 (:foreground "khaki" :bold t :underline t))
1291 (((class color) (background light))
1292 (:foreground "Goldenrod" :underline t))
1293 (t (:bold t :underline t)))
1294 "Font Lock mode face used to highlight subscripts in formula."
1295 :group 'font-lock-faces)
1296 (defvar YaTeX-font-lock-math-sub-face 'YaTeX-font-lock-math-sub-face)
1298 (defface YaTeX-font-lock-math-sup-face
1299 '((((class static-color)) (:bold t))
1300 (((type tty)) (:bold t))
1301 (((class color) (background dark))
1302 (:bold nil :foreground "ivory" :background "lightyellow4"))
1303 (((class color) (background light))
1304 (:underline t :foreground "gold"))
1305 (t (:bold t :underline t)))
1306 "Font Lock mode face used to highlight superscripts in formula."
1307 :group 'font-lock-faces)
1308 (defvar YaTeX-font-lock-math-sup-face 'YaTeX-font-lock-math-sup-face)
1310 (defface YaTeX-font-lock-crossref-face
1311 '((((class color) (background dark)) (:foreground "lightgoldenrod"))
1312 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1313 (t (:bold t :underline t)))
1314 "Font Lock mode face used to highlight cross references."
1315 :group 'font-lock-faces)
1316 (defvar YaTeX-font-lock-crossref-face 'YaTeX-font-lock-crossref-face)
1318 (defface YaTeX-font-lock-bold-face
1319 '((t (:bold t)))
1320 "Font Lock mode face used to express bold itself."
1321 :group 'font-lock-faces)
1322 (defvar YaTeX-font-lock-bold-face 'YaTeX-font-lock-bold-face)
1324 (defface YaTeX-font-lock-italic-face
1325 '((t (:italic t)))
1326 "Font Lock mode face used to express italic itself."
1327 :group 'font-lock-faces)
1328 (defvar YaTeX-font-lock-italic-face 'YaTeX-font-lock-italic-face)
1330 ;; Make sure the 'YaTeX-font-lock-{italic,bold}-face is bound with
1331 ;; italic/bold fontsets
1332 (if (and (fboundp 'fontset-list) YaTeX-use-italic-bold)
1333 (let ((flist (fontset-list)) fnt italic bold
1334 (df (or (and (fboundp 'face-font-name) (face-font-name 'default))
1335 (face-font 'default)
1336 (face-font 'italic)
1337 (face-font 'bold)
1338 "giveup!"))
1339 sz medium-i bold-r)
1340 (string-match
1341 "^-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-\\(\\([0-9]+\\)\\)" df)
1342 (setq sz (or (match-string 1 df) "16"))
1343 (setq medium-i (format "-medium-i-[^-]+--%s" sz)
1344 bold-r (format "-bold-r-[^-]+--%s" sz))
1345 (while flist
1346 (setq fnt (car flist))
1347 (condition-case err
1348 (cond
1349 ((and (string-match medium-i fnt)
1350 (null italic))
1351 (set-face-font 'YaTeX-font-lock-italic-face (setq italic fnt)))
1352 ((and (string-match bold-r fnt) (null bold))
1353 (set-face-font 'YaTeX-font-lock-bold-face (setq bold fnt))))
1354 (error nil))
1355 (setq flist (cdr flist)))))
1357 ;;Borrowed from XEmacs's font-lock.el
1358 (defsubst YaTeX-font-lock-fillin (start end setprop markprop value &optional object)
1359 "Fill in one property of the text from START to END.
1360 Arguments PROP and VALUE specify the property and value to put where none are
1361 already in place. Therefore existing property values are not overwritten.
1362 Optional argument OBJECT is the string or buffer containing the text."
1363 (let ((start (text-property-any start end markprop nil object)) next
1364 (putfunc (if (fboundp 'put-nonduplicable-text-property)
1365 'put-nonduplicable-text-property
1366 'put-text-property)))
1367 (if (eq putfunc 'put-text-property)
1368 (setq markprop setprop))
1369 (while start
1370 (setq next (next-single-property-change start markprop object end))
1371 (funcall putfunc start next setprop value object)
1372 (funcall putfunc start next markprop value object)
1373 (setq start (text-property-any next end markprop nil object)))))
1375 (defun YaTeX-warning-font-lock (mode)
1376 (let ((sw (selected-window)))
1377 ;;(pop-to-buffer (format " *%s warning*" mode))
1378 ;;(erase-buffer)
1379 (momentary-string-display
1380 (cond
1381 (YaTeX-japan
1382 (concat mode " は、既に font-lock に対応しました。\n"
1383 "~/.emacs などにある\n"
1384 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
1385 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
1386 "などの間に合わせの記述はもはや不要です。"))
1387 (t
1388 (concat mode " now supports the font-lock by itself.\n"
1389 "So you can remove the descriptions such as\n"
1390 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
1391 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
1392 "in your ~/.emacs file. Thank you."))) (point))
1393 (select-window sw)))
1394 ))
1396 (defun YaTeX-assoc-regexp (elt alist)
1397 "Like assoc, return a list of whose car match with ELT. Search from ALIST.
1398 Note that each car of cons-cell is regexp. ELT is a plain text to be
1399 compared by regexp."
1400 (let (x)
1401 (catch 'found
1402 (while alist
1403 (setq x (car (car alist)))
1404 (if (string-match x elt)
1405 (throw 'found (car alist)))
1406 (setq alist (cdr alist))))))
1408 ;;;
1409 ;; Functions for the Installation time
1410 ;;;
1412 (defun bcf-and-exit ()
1413 "Byte compile rest of argument and kill-emacs."
1414 (if command-line-args-left
1415 (let ((load-path (cons "." load-path)))
1416 (and (fboundp 'set-language-environment)
1417 (featurep 'mule)
1418 (set-language-environment "Japanese"))
1419 (mapcar 'byte-compile-file command-line-args-left)
1420 (kill-emacs))))
1422 (defun tfb-and-exit ()
1423 "Texinfo-format-buffer and kill-emacs."
1424 (if command-line-args-left
1425 (let ((load-path (cons ".." load-path)))
1426 (and (fboundp 'set-language-environment)
1427 (featurep 'mule)
1428 (set-language-environment "Japanese"))
1429 (mapcar (function
1430 (lambda (arg)
1431 (find-file arg)
1432 (texinfo-format-buffer)
1433 (basic-save-buffer)))
1434 command-line-args-left)
1435 (kill-emacs))))
1437 (provide 'yatexlib)
1438 ; Local variables:
1439 ; fill-prefix: ";;; "
1440 ; paragraph-start: "^$\\| \\|;;;$"
1441 ; paragraph-separate: "^$\\| \\|;;;$"
1442 ; coding: sjis
1443 ; End: