yatex

view yatexlib.el @ 247:3e3ccba06ca1

Construct `bb=...' line automatically and push it into kill-ring.
author yuuji@gentei.org
date Fri, 10 Feb 2012 12:31:58 +0900
parents d5e0c33d7dfd
children 15ec86ff549c
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX and yahtml common libraries, general functions and definitions
3 ;;; yatexlib.el
4 ;;; (c)1994-2012 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Fri Feb 10 12:30:49 2012 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 "Return the last position of STRING where character CHAR found."
468 (let ((pos (1- (length string)))(index -1))
469 (catch 'rindex
470 (while (>= pos 0)
471 (cond
472 ((= (aref string pos) char)
473 (throw 'rindex pos))
474 (t (setq pos (1- pos))))))))
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 (fset 'YaTeX-set-screen-height 'set-screen-height)
536 (fset 'YaTeX-set-screen-width 'set-screen-width))
537 ((fboundp 'frame-height)
538 (fset 'YaTeX-screen-height 'frame-height)
539 (fset 'YaTeX-screen-width 'frame-width)
540 (fset 'YaTeX-set-screen-height 'set-frame-height)
541 (fset 'YaTeX-set-screen-width 'set-frame-width))
542 (t (error "I don't know how to run YaTeX on this Emacs...")))
544 ;;;###autoload
545 (defun split-window-calculate-height (height)
546 "Split current window wight specified HEIGHT.
547 If HEIGHT is number, make a new window that has HEIGHT lines.
548 If HEIGHT is string, make a new window that occupies HEIGT % of screen height.
549 Otherwise split window conventionally."
550 (if (one-window-p t)
551 (split-window
552 (selected-window)
553 (max
554 (min
555 (- (YaTeX-screen-height)
556 (if (numberp height)
557 (+ height 2)
558 (/ (* (YaTeX-screen-height)
559 (string-to-int height))
560 100)))
561 (- (YaTeX-screen-height) window-min-height 1))
562 window-min-height))))
564 ;;;###autoload
565 (defun YaTeX-window-list ()
566 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
567 (while (not (eq curw (setq win (next-window win))))
568 (or (eq win (minibuffer-window))
569 (setq wlist (cons win wlist))))
570 wlist))
572 (if YaTeX-emacs-21
573 ;; Emacs-21's next-window returns other frame's window even if called
574 ;; with argument ALL-FRAMES nil, when called from minibuffer context.
575 ;; Therefore, check frame identity here.
576 (defun YaTeX-window-list ()
577 (let*((curw (selected-window)) (win curw) (wlist (list curw))
578 (curf (window-frame curw)))
579 (while (and (not (eq curw (setq win (next-window win))))
580 (eq curf (window-frame win)))
581 (or (eq win (minibuffer-window))
582 (setq wlist (cons win wlist))))
583 wlist)))
585 ;;;###autoload
586 (defun substitute-all-key-definition (olddef newdef keymap)
587 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
588 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
589 where ever it appears."
590 (if YaTeX-emacs-19
591 (substitute-key-definition olddef newdef keymap global-map)
592 (mapcar
593 (function (lambda (key) (define-key keymap key newdef)))
594 (where-is-internal olddef keymap))))
596 ;;;###autoload
597 (defun YaTeX-match-string (n &optional m)
598 "Return (buffer-substring (match-beginning n) (match-beginning m))."
599 (if (match-beginning n)
600 (YaTeX-buffer-substring (match-beginning n)
601 (match-end (or m n)))))
603 ;;;###autoload
604 (defun YaTeX-minibuffer-complete ()
605 "Complete in minibuffer.
606 If the symbol 'delim is bound and is string, its value is assumed to be
607 the character class of delimiters. Completion will be performed on
608 the last field separated by those delimiters.
609 If the symbol 'quick is bound and is 't, when the try-completion results
610 in t, exit minibuffer immediately."
611 (interactive)
612 (save-restriction
613 (narrow-to-region
614 (if (fboundp 'field-beginning) (field-beginning (point-max)) (point-min))
615 (point-max))
616 (let ((md (match-data)) beg word compl
617 (quick (and (boundp 'quick) (eq quick t)))
618 (displist ;function to display completion-list
619 (function
620 (lambda ()
621 (with-output-to-temp-buffer "*Completions*"
622 (display-completion-list
623 (all-completions word minibuffer-completion-table)))))))
624 (setq beg (if (and (boundp 'delim) (stringp delim))
625 (save-excursion
626 (skip-chars-backward (concat "^" delim))
627 (point))
628 (point-min))
629 word (buffer-substring beg (point-max))
630 compl (try-completion word minibuffer-completion-table))
631 (cond
632 ((eq compl t)
633 (if quick (exit-minibuffer)
634 (let ((p (point)) (max (point-max)))
635 (unwind-protect
636 (progn
637 (goto-char max)
638 (insert " [Sole completion]")
639 (goto-char p)
640 (sit-for 1))
641 (delete-region max (point-max))
642 (goto-char p)))))
643 ((eq compl nil)
644 (ding)
645 (save-excursion
646 (let (p)
647 (unwind-protect
648 (progn
649 (goto-char (setq p (point-max)))
650 (insert " [No match]")
651 (goto-char p)
652 (sit-for 2))
653 (delete-region p (point-max))))))
654 ((string= compl word)
655 (funcall displist))
656 (t (delete-region beg (point-max))
657 (insert compl)
658 (if quick
659 (if (eq (try-completion compl minibuffer-completion-table) t)
660 (exit-minibuffer)
661 (funcall displist)))))
662 (store-match-data md))))
664 (defun YaTeX-minibuffer-quick-complete ()
665 "Set 'quick to 't and call YaTeX-minibuffer-complete.
666 See documentation of YaTeX-minibuffer-complete."
667 (interactive)
668 (let ((quick t))
669 (self-insert-command 1)
670 (YaTeX-minibuffer-complete)))
672 (defun YaTeX-yatex-buffer-list ()
673 (save-excursion
674 (delq nil (mapcar (function (lambda (buf)
675 (set-buffer buf)
676 (if (eq major-mode 'yatex-mode) buf)))
677 (buffer-list)))))
679 (defun foreach-buffers (pattern job)
680 "For each buffer which matches with PATTERN, do JOB."
681 (let ((list (buffer-list)))
682 (save-excursion
683 (while list
684 (set-buffer (car list))
685 (if (or (and (stringp pattern)
686 (buffer-file-name)
687 (string-match pattern (buffer-file-name)))
688 (and (symbolp pattern) major-mode (eq major-mode pattern)))
689 (eval job))
690 (setq list (cdr list))))))
692 (defun goto-buffer-window (buffer)
693 "Select window which is bound to BUFFER.
694 If no such window exist, switch to buffer BUFFER."
695 (interactive "BGoto buffer: ")
696 (if (stringp buffer)
697 (setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
698 (if (get-buffer buffer)
699 (cond
700 ((get-buffer-window buffer)
701 (select-window (get-buffer-window buffer)))
702 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
703 (let*((win (get-buffer-window buffer t))
704 (frame (window-frame win)))
705 (select-frame frame)
706 (raise-frame frame)
707 (focus-frame frame)
708 (select-window win)
709 (set-mouse-position frame 0 0)
710 (and (featurep 'windows) (fboundp 'win:adjust-window)
711 (win:adjust-window))))
712 ((and (featurep 'windows) (fboundp 'win:get-buffer-window)
713 (let ((w (win:get-buffer-window buffer)))
714 (and w (win:switch-window w))))
715 (select-window (get-buffer-window buffer)))
716 (t (switch-to-buffer buffer)))))
718 ;; Here starts the functions which support gmhist-vs-Emacs19 compatible
719 ;; reading with history.
720 ;;;###autoload
721 (defun completing-read-with-history
722 (prompt table &optional predicate must-match initial hsym)
723 "Completing read with general history: gmhist, Emacs-19."
724 (let ((minibuffer-history
725 (or (symbol-value hsym)
726 (and (boundp 'minibuffer-history) minibuffer-history)))
727 (minibuffer-history-symbol (or hsym 'minibuffer-history)))
728 (prog1
729 (if (fboundp 'completing-read-with-history-in)
730 (completing-read-with-history-in
731 minibuffer-history-symbol prompt table predicate must-match initial)
732 (completing-read prompt table predicate must-match initial))
733 (if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
735 ;;;###autoload
736 (defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
737 "Read from minibuffer with general history: gmhist, Emacs-19."
738 (cond
739 (YaTeX-emacs-19
740 (read-from-minibuffer prompt init map read hsym))
741 (t
742 (let ((minibuffer-history-symbol hsym))
743 (read-from-minibuffer prompt init map read)))))
745 ;;;###autoload
746 (defun read-string-with-history (prompt &optional init hsym)
747 "Read string with history: gmhist(Emacs-18) and Emacs-19."
748 (cond
749 (YaTeX-emacs-19
750 (read-from-minibuffer prompt init minibuffer-local-map nil hsym))
751 ((featurep 'gmhist-mh)
752 (read-with-history-in hsym prompt init))
753 (t (read-string prompt init))))
755 (defvar YaTeX-skip-next-reader-char ?\C-j)
756 (defun YaTeX-read-string-or-skip (&rest args)
757 "Read string, or skip if last input char is \C-j."
758 (if (equal last-input-char YaTeX-skip-next-reader-char)
759 ""
760 (apply 'read-string args)))
762 (defun YaTeX-completing-read-or-skip (&rest args)
763 "Do completing-read, or skip if last input char is \C-j."
764 (if (equal last-input-char YaTeX-skip-next-reader-char)
765 ""
766 (apply 'completing-read args)))
768 ;;;###autoload
769 (fset 'YaTeX-rassoc
770 (if (and nil (fboundp 'rassoc) (subrp (symbol-function 'rassoc)))
771 (symbol-function 'rassoc)
772 (function
773 (lambda (key list)
774 (let ((l list))
775 (catch 'found
776 (while l
777 (if (equal key (cdr (car l)))
778 (throw 'found (car l)))
779 (setq l (cdr l)))))))))
781 (defun YaTeX-insert-file-contents (file visit &optional beg end)
782 (cond
783 ((and (string< "19" emacs-version) (not (featurep 'xemacs)))
784 (insert-file-contents file visit beg end))
785 ((string-match "unix\\|linux" (symbol-name system-type))
786 (let ((default-process-coding-system
787 (and (boundp '*noconv*) (list '*noconv*)))
788 (file-coding-system (and (boundp '*noconv*) '*noconv*))
789 kanji-fileio-code
790 (default-process-kanji-code 0))
791 (call-process shell-file-name file (current-buffer) nil
792 (or (and (boundp 'shell-command-option)
793 shell-command-option)
794 "-c")
795 (format "dd bs=1 count=%d | tail -c +%d" end beg))))
796 (t (insert-file-contents file))))
798 (defun YaTeX-split-string (str &optional sep null)
799 "Split string STR by every occurrence of SEP(regexp).
800 If the optional second argument SEP is nil, it defaults to \"[ \f\t\n\r\v]+\".
801 Do not include null string by default. Non-nil for optional third argument
802 NULL includes null string in a list."
803 (let ((sep (or sep "[ \f\t\n\r\v]+"))
804 list m)
805 (while str
806 (if (setq m (string-match sep str))
807 (progn
808 (if (or (> m 0) null)
809 (setq list (cons (substring str 0 m) list)))
810 (setq str (substring str (match-end 0))))
811 (if (or null (string< "" str))
812 (setq list (cons str list)))
813 (setq str nil)))
814 (nreverse list)))
816 ;;;###autoload
817 (defun YaTeX-delete1 (elt list)
818 "Delete"
819 (let (e)
820 (while (setq e (YaTeX-member elt list))
821 (setq list (delq (car e) list))))
822 list)
823 (if (fboundp 'delete)
824 (fset 'YaTeX-delete (symbol-function 'delete))
825 (fset 'YaTeX-delete (symbol-function 'YaTeX-delete1)))
827 (defun YaTeX-member1 (elt list)
828 (catch 'found
829 (while list
830 (if (equal elt (car list))
831 (throw 'found list))
832 (setq list (cdr list)))))
834 (if (and (fboundp 'member) (subrp (symbol-function 'member)))
835 (fset 'YaTeX-member (symbol-function 'member))
836 (fset 'YaTeX-member (symbol-function 'YaTeX-member1)))
838 ;;;
839 ;; Interface function for windows.el
840 ;;;
841 ;;;###autoload
842 (defun YaTeX-switch-to-window ()
843 "Switch to windows.el's window decided by last pressed key."
844 (interactive)
845 (or (featurep 'windows) (error "Why don't you use `windows.el'?"))
846 (win-switch-to-window 1 (- last-command-char win:base-key)))
848 ;;;###autoload
849 (defun YaTeX-command-to-string (cmd)
850 (if (fboundp 'shell-command-to-string)
851 (funcall 'shell-command-to-string cmd)
852 (let ((tbuf " *tmpout*"))
853 (if (get-buffer-create tbuf) (kill-buffer tbuf))
854 (let ((standard-output (get-buffer-create tbuf)))
855 (unwind-protect
856 (save-excursion
857 (call-process
858 shell-file-name nil tbuf nil YaTeX-shell-command-option cmd)
859 (set-buffer tbuf)
860 (buffer-string))
861 (kill-buffer tbuf))))))
863 ;;;###autoload
864 (defun YaTeX-reindent (col)
865 "Remove current indentation and reindento to COL column."
866 (save-excursion
867 (beginning-of-line)
868 (skip-chars-forward " \t")
869 (if (/= col (current-column))
870 (progn
871 (delete-region (point) (progn (beginning-of-line) (point)))
872 (indent-to col))))
873 (skip-chars-forward " \t" (point-end-of-line)))
875 (defun YaTeX-inner-environment (&optional quick)
876 "Return current inner-most environment.
877 Non-nil for optional argument QUICK restricts search bound to most
878 recent sectioning command. Matching point is stored to property 'point
879 of 'YaTeX-inner-environment, which can be referred by
880 (get 'YaTeX-inner-environment 'point)."
881 (put 'YaTeX-inner-environment 'point (point-min))
882 (put 'YaTeX-inner-environment 'indent 0)
883 (let*((nest 0)
884 (beg (YaTeX-replace-format-args
885 (regexp-quote YaTeX-struct-begin)
886 ;YaTeX-struct-begin ;=== TENTATIVE!! ==
887 YaTeX-struct-name-regexp
888 (if (eq major-mode 'yahtml-mode) "\\s *.*" "")
889 ""))
890 (end (YaTeX-replace-format-args
891 (regexp-quote YaTeX-struct-end)
892 YaTeX-struct-name-regexp "" ""))
893 (begend (concat "\\(" beg "\\)\\|\\(" end "\\)"))
894 bound m0
895 (htmlp (eq major-mode 'yahtml-mode))
896 (open
897 (concat "^" (or (cdr (assq major-mode '((yahtml-mode . "<")))) "{")))
898 (close
899 (concat "^"
900 (or (cdr(assq major-mode '((yahtml-mode . "\n\t >")))) "}"))))
901 (save-excursion
902 (if quick
903 (setq bound
904 (save-excursion
905 (if htmlp
906 ;;(re-search-backward YaTeX-sectioning-regexp nil 1)
907 ;;(goto-char (point-min)) ;Is this enough? 97/6/26
908 (re-search-backward yahtml-indentation-boundary nil 1)
909 (YaTeX-re-search-active-backward
910 (concat YaTeX-ec-regexp
911 "\\(" YaTeX-sectioning-regexp "\\)\\*?{")
912 YaTeX-comment-prefix nil 1))
913 (or (bobp) (end-of-line))
914 (point))))
915 (if (catch 'begin
916 (if (and (numberp bound) (< (point) bound)) (throw 'begin nil))
917 (while (YaTeX-re-search-active-backward
918 begend YaTeX-comment-prefix bound t)
919 (setq m0 (match-beginning 0))
920 (if (looking-at end) ;;(match-beginning 2)
921 (setq nest (1+ nest))
922 (setq nest (1- nest)))
923 (if (< nest 0)
924 (progn
925 (put 'YaTeX-inner-environment 'point m0)
926 (goto-char m0)
927 (put 'YaTeX-inner-environment 'indent (current-column))
928 (throw 'begin t)))))
929 (YaTeX-buffer-substring
930 (progn (skip-chars-forward open) (1+ (point)))
931 (progn (skip-chars-forward close) (point)))))))
933 (defun YaTeX-goto-corresponding-environment (&optional allow-mismatch noerr)
934 "Go to corresponding begin/end enclosure.
935 Optional argument ALLOW-MISMATCH allows mismatch open/clese. Use this
936 for \left(, \right).
937 Optional third argument NOERR causes no error for unballanced environment."
938 (interactive)
939 (if (not (YaTeX-on-begin-end-p)) nil
940 (let ((p (match-end 0)) b0 b1 env (nest 0) regexp re-s (op (point))
941 (m0 (match-beginning 0)) ;whole matching
942 (m1 (match-beginning 1)) ;environment in \begin{}
943 (m2 (match-beginning 2)) ;environment in \end{}
944 (m3 (match-beginning 3))) ;environment in \[ \] \( \)
945 ;(setq env (regexp-quote (buffer-substring p (match-beginning 0))))
946 (if (cond
947 (m1 ;if begin{xxx}
948 (setq env
949 (if allow-mismatch YaTeX-struct-name-regexp
950 (regexp-quote (buffer-substring m1 (match-end 1)))))
951 ; (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
952 ; "\\(\\\\begin{" env "}\\)"))
953 (setq regexp
954 (concat
955 "\\("
956 (YaTeX-replace-format-args
957 (regexp-quote YaTeX-struct-end) env "" "")
958 "\\)\\|\\("
959 (YaTeX-replace-format-args
960 (regexp-quote YaTeX-struct-begin) env "" "")
961 "\\)"))
962 (setq re-s 're-search-forward))
963 (m2 ;if end{xxx}
964 (setq env
965 (if allow-mismatch YaTeX-struct-name-regexp
966 (regexp-quote (buffer-substring m2 (match-end 2)))))
967 ; (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
968 ; "\\(\\\\end{" env "}\\)"))
969 (setq regexp
970 (concat
971 "\\("
972 (YaTeX-replace-format-args
973 (regexp-quote YaTeX-struct-begin) env "" "")
974 "\\)\\|\\("
975 (YaTeX-replace-format-args
976 (regexp-quote YaTeX-struct-end) env "" "")
977 "\\)"))
978 (setq re-s 're-search-backward))
979 (m3 ;math environment
980 (setq env (char-after (1+ m3))
981 regexp (format "\\(%s%s\\)\\|\\(%s%s\\)"
982 YaTeX-ec-regexp
983 (regexp-quote
984 (cdr (assq env '((?( . ")") (?) . "(")
985 (?[ . "]") (?] . "[")))))
986 YaTeX-ec-regexp
987 (regexp-quote (char-to-string env)))
988 re-s (if (memq env '(?\( ?\[))
989 're-search-forward
990 're-search-backward)))
991 (t (if noerr nil (error "Corresponding environment not found."))))
992 (progn
993 (while (and (>= nest 0) (funcall re-s regexp nil t))
994 (setq b0 (match-beginning 0) b1 (match-beginning 1))
995 (if (or (equal b0 m0)
996 (YaTeX-literal-p b0))
997 nil
998 (setq nest (if (equal b0 b1)
999 (1- nest) (1+ nest)))))
1000 (if (< nest 0)
1001 (goto-char (match-beginning 0)) ;found.
1002 (goto-char op)
1003 (funcall
1004 (if noerr 'message 'error)
1005 "Corresponding environment `%s' not found." env)
1006 (sit-for 1)
1007 nil))))))
1009 (defun YaTeX-end-environment ()
1010 "Close opening environment"
1011 (interactive)
1012 (let ((env (YaTeX-inner-environment)))
1013 (if (not env) (error "No premature environment")
1014 (save-excursion
1015 (if (and
1016 (YaTeX-re-search-active-forward
1017 (concat
1018 "\\(" (YaTeX-replace-format-args
1019 YaTeX-struct-end env "" "")
1020 "\\)\\|\\(" (YaTeX-replace-format-args
1021 YaTeX-struct-begin env "" "")
1022 "\\)")
1023 YaTeX-comment-prefix nil t)
1024 (match-beginning 1)) ;is closing struc.
1025 (if (y-or-n-p
1026 (concat "Environment `" env
1027 "' may be already closed. Force close?"))
1028 nil
1029 (error "end environment aborted."))))
1030 (message "") ;Erase (y or n) message.
1031 (YaTeX-insert-struc 'end env)
1032 (save-excursion
1033 (goto-char (or (get 'YaTeX-inner-environment 'point) (match-end 0)))
1034 (if (pos-visible-in-window-p)
1035 (sit-for (if YaTeX-dos 2 1))
1036 (message "Matches with %s at line %d"
1037 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
1038 (count-lines (point-min) (point))))))))
1040 (defun YaTeX-beginning-of-environment (&optional limit-search-bound end)
1041 "Goto the beginning of the current environment.
1042 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound to
1043 most recent sectioning command. Non-nil for optional third argument END
1044 goes to end of environment."
1045 (interactive)
1046 (let ((op (point)))
1047 (if (YaTeX-inner-environment limit-search-bound)
1048 (progn
1049 (goto-char (get 'YaTeX-inner-environment 'point))
1050 (and end (YaTeX-goto-corresponding-environment))
1051 (if (interactive-p) (push-mark op))
1052 (point)))))
1054 (defun YaTeX-end-of-environment (&optional limit-search-bound)
1055 "Goto the end of the current environment.
1056 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound
1057 to most recent sectioning command."
1058 (interactive)
1059 (YaTeX-beginning-of-environment limit-search-bound t))
1061 (defun YaTeX-mark-environment ()
1062 "Mark current position and move point to end of environment."
1063 (interactive)
1064 (require 'yatexmth)
1065 (let ((curp (point)))
1066 (if (YaTeX-in-math-mode-p)
1067 (YaTeX-mark-mathenv)
1068 (if (and (YaTeX-on-begin-end-p) (match-beginning 1)) ;if on \\begin
1069 (progn (goto-char (match-end 0)))
1070 (if (= (char-after (point)) ?\\) nil ;if on \\end
1071 (skip-chars-backward "^\n\\\\")
1072 (or (bolp) (forward-char -1))))
1073 (if (not (YaTeX-end-of-environment)) ;arg1 turns to match-beginning 1
1074 (progn
1075 (goto-char curp)
1076 (error "Cannot found the end of current environment."))
1077 (YaTeX-goto-corresponding-environment)
1078 (beginning-of-line) ;for confirmation
1079 (if (< curp (point))
1080 (progn
1081 (message "Mark this environment?(y or n): ")
1082 (if (= (read-char) ?y) nil
1083 (goto-char curp)
1084 (error "Abort. Please call again at more proper position."))))
1085 (set-mark-command nil)
1086 (YaTeX-goto-corresponding-environment)
1087 (end-of-line)
1088 (if (eobp) nil (forward-char 1))))))
1090 (defun YaTeX-kill-buffer (buffer)
1091 "Make effort to show parent buffer after kill."
1092 (interactive "bKill buffer: ")
1093 (or (get-buffer buffer)
1094 (error "No such buffer %s" buffer))
1095 (let ((pf YaTeX-parent-file))
1096 (kill-buffer buffer)
1097 (and pf
1098 (get-file-buffer pf)
1099 (switch-to-buffer (get-file-buffer pf)))))
1101 (defun YaTeX-getset-builtin (key &optional value)
1102 "Read source built-in command of %# usage."
1103 (catch 'builtin
1104 (let*((bl (delq nil (list (current-buffer)
1105 (and YaTeX-parent-file
1106 (get-file-buffer YaTeX-parent-file)))))
1107 (tuple (cdr (assq major-mode
1108 '((yatex-mode "%#" . "\n")
1109 (yahtml-mode "<!-- #" . "[ \t]*-->\\|\n")))))
1110 (leader (or (car tuple) ""))
1111 (closer (or (cdr tuple) ""))
1112 (prompt (format "Built-in for %s: " key)))
1113 (save-excursion
1114 (while bl
1115 (set-buffer (car bl))
1116 (save-excursion
1117 (goto-char (point-min))
1118 (if (and (re-search-forward
1119 (concat "^" (regexp-quote (concat leader key))) nil t)
1120 (not (eolp)))
1121 (throw 'builtin
1122 (let (b e w)
1123 (skip-chars-forward " \t" (point-end-of-line))
1124 (setq b (point)
1125 e (if (re-search-forward closer nil t)
1126 (match-beginning 0)
1127 (point-end-of-line))
1128 w (YaTeX-buffer-substring b e))
1129 (if (null value)
1131 (delete-region b e)
1132 (goto-char b)
1133 (if (symbolp value)
1134 (setq value (read-string prompt w)))
1135 (insert value)
1136 value)))))
1137 (setq bl (cdr bl)))
1138 ; not found
1139 (if (null value)
1140 nil ;not set mode, return simply nil
1141 (if (symbolp value)
1142 (setq value (read-string prompt)))
1143 (save-excursion
1144 (goto-char (point-min))
1145 (insert leader key " " value "\n")
1146 value)))))) ;on set mode, return set value
1148 (defun YaTeX-get-builtin (key)
1149 "Read source built-in command of %# usage."
1150 (YaTeX-getset-builtin key))
1152 ;;;VER2
1153 (defun YaTeX-insert-struc (what env)
1154 (cond
1155 ((eq what 'begin)
1156 (insert (YaTeX-replace-format-args
1157 YaTeX-struct-begin env (YaTeX-addin env))))
1158 ((eq what 'end)
1159 (insert (YaTeX-replace-format-args YaTeX-struct-end env)))
1160 (t nil)))
1162 (defun YaTeX-string-width (str)
1163 "Return the display width of string."
1164 (if (fboundp 'string-width)
1165 (string-width str)
1166 (length str)))
1167 (defun YaTeX-truncate-string-width (str width)
1168 (cond
1169 ((fboundp 'truncate-string-to-width) (truncate-string-to-width str width))
1170 ((fboundp 'truncate-string) (truncate-string str width))
1171 (t (substring str 0 width))))
1173 (defun YaTeX-hex (str)
1174 "Return int expressed by hexadecimal string STR."
1175 (if (string< "20" emacs-version)
1176 (string-to-number str 16)
1177 (let ((md (match-data)))
1178 (unwind-protect
1179 (if (string-match "[^0-9a-f]" str)
1180 (error "Non hexadecimal character in %s" str)
1181 (let ((i 0) d)
1182 (setq str (downcase str))
1183 (while (string< "" str)
1184 (setq d (+ 0 (string-to-char str)) ; + 0 for XEmacs
1185 i (+ (* 16 i) (- d (if (<= d ?9) ?0 (- ?a 10))))
1186 str (substring str 1)))
1187 i))
1188 (store-match-data md)))))
1191 ;;; Function for menu support
1192 (defun YaTeX-define-menu (keymap bindlist)
1193 "Define KEYMAP(symbol)'s menu-bindings according to BINDLIST.
1194 KEYMAP should be a quoted symbol of newly allocated keymap.
1195 BINDLIST consists of binding list. Each element is as follows.
1197 '(menusymbol DOC_String . contents)
1199 CONTENTS is one of lambda-form, interactive function, or other keymap.
1200 See yatex19.el for example."
1201 (cond
1202 ((featurep 'xemacs)
1203 (let (name)
1204 (if (keymapp (symbol-value keymap))
1205 (progn
1206 (setq name (keymap-name (symbol-value keymap)))
1207 (set keymap nil))
1208 (setq name (car (symbol-value keymap)))
1209 (set keymap (cdr (symbol-value keymap))))
1210 (mapcar
1211 (function
1212 (lambda (bind)
1213 (setq bind (cdr bind))
1214 (if (eq (car-safe (cdr bind)) 'lambda)
1215 (setcar (cdr bind) 'progn))
1216 (if (stringp (car-safe (cdr bind)))
1217 (set keymap
1218 (cons (cdr bind) (symbol-value keymap)))
1219 (set keymap
1220 (cons (vector (car bind) (cdr bind) t)
1221 (symbol-value keymap))))))
1222 bindlist)
1223 (set keymap (cons name (symbol-value keymap)))))
1224 (t
1225 (mapcar
1226 (function
1227 (lambda (bind)
1228 (define-key (symbol-value keymap) (vector (car bind)) (cdr bind))))
1229 bindlist))))
1231 ;;;
1232 ;; Emacs 21 compensational wrapper
1233 ;;;
1234 (defun YaTeX-minibuffer-begin ()
1235 (if (fboundp 'field-beginning)
1236 (field-beginning (point-max))
1237 (point-min)))
1239 (defun YaTeX-minibuffer-end ()
1240 (if (fboundp 'field-end)
1241 (field-end (point-max))
1242 (point-max)))
1244 (defun YaTeX-minibuffer-string ()
1245 (buffer-substring (YaTeX-minibuffer-begin) (YaTeX-minibuffer-end)))
1247 (defun YaTeX-minibuffer-erase ()
1248 (if (eq (selected-window) (minibuffer-window))
1249 (if (fboundp 'delete-field) (delete-field) (erase-buffer))))
1251 (fset 'YaTeX-buffer-substring
1252 (if (fboundp 'buffer-substring-no-properties)
1253 'buffer-substring-no-properties
1254 'buffer-substring))
1256 ;;;
1257 ;; hilit19 vs. font-lock
1258 ;;;
1259 (defvar YaTeX-19-functions-font-lock-direct
1260 '(YaTeX-19-re-search-in-env))
1262 (defun YaTeX-convert-pattern-hilit2fontlock (h19pa)
1263 "Convert hilit19's H19PA patterns alist to font-lock's one.
1264 This function is a makeshift for YaTeX and yahtml."
1265 (let ((ignorecase (not (null (car h19pa))))
1266 (palist (cdr h19pa))
1267 flpa i newface
1268 (mapping
1269 '((bold . YaTeX-font-lock-bold-face)
1270 (italic . YaTeX-font-lock-italic-face)
1271 (defun . font-lock-function-name-face)
1272 (define . font-lock-variable-name-face)
1273 (keyword . font-lock-keyword-face)
1274 (decl . YaTeX-font-lock-declaration-face)
1275 (label . YaTeX-font-lock-label-face)
1276 (crossref . YaTeX-font-lock-crossref-face)
1277 (include . YaTeX-font-lock-include-face)
1278 (formula . YaTeX-font-lock-formula-face)
1279 (delimiter . YaTeX-font-lock-delimiter-face)
1280 (string . ignore) (comment . ignore)
1281 )))
1282 (while (setq i (car palist))
1283 (setq newface (nth 2 i)
1284 newface (or (cdr (assq newface mapping)) newface))
1285 (cond
1286 ((eq newface 'ignore) nil) ;no translation
1287 ((stringp (car i)) ;hiliting by regexp
1288 (setq flpa
1289 (cons
1290 (if (numberp (car (cdr i)))
1291 (list (car i) ;regexp
1292 (car (cdr i)) ;matching group number
1293 newface nil) ;'keep) ;keep is hilit19 taste
1294 (list
1295 (concat
1296 (car i) ;original regexp and..
1297 ;;"[^"
1298 ;;(regexp-quote (substring (car (cdr i)) 0 1))
1299 ;;"]+" ;for shortest match
1300 ".*"
1301 (car (cdr i)))
1302 0 (list 'quote newface) nil)) ;;'keep))
1303 flpa)))
1304 ((and (symbolp (car i)) (fboundp (car i)))
1305 (if (memq (car i) YaTeX-19-functions-font-lock-direct)
1306 ;; Put direct function call for it.
1307 ;; When calling this function, fontify entire matched string.
1308 (setq flpa
1309 (cons
1310 (list
1311 (list 'lambda (list 'dummy) ;dummy should be boundary
1312 (list (car i) (list 'quote (car (cdr i)))))
1313 (list 0 newface))
1314 flpa))
1315 (setq flpa
1316 (cons
1317 (list (car (cdr i)) ;regexp
1318 (list
1319 (list
1320 'lambda (list 'dummy)
1321 '(goto-char (match-beginning 0))
1322 (if (eq (nth 3 i) 'overwrite)
1323 nil
1324 '(remove-text-properties
1325 (point) (min (point-max) (1+ (point)))
1326 '(face nil font-lock-multiline nil)))
1327 (list
1328 'let (list '(e (match-end 0))
1329 (list 'm (list (car i) (car (cdr i)))))
1330 (list
1331 'if 'm
1332 (list
1333 'YaTeX-font-lock-fillin
1334 (list 'car 'm)
1335 (list 'cdr 'm)
1336 (list 'quote 'face)
1337 (list 'quote 'font-lock)
1338 (list 'quote newface))
1339 '(goto-char e)
1340 ))
1341 nil) ;retun nil to cheat font-lock
1342 nil nil)) ;pre-match, post-match both nil
1343 flpa)))))
1344 (setq palist (cdr palist)));while
1345 (if (featurep 'xemacsp)
1346 (nreverse flpa)
1347 flpa)))
1349 (if (and (boundp 'YaTeX-use-font-lock)
1350 YaTeX-use-font-lock)
1351 (require 'font-lock))
1353 (cond
1354 ((and (featurep 'font-lock) (fboundp 'defface))
1355 ;; In each defface, '(class static-color) is for Emacs-21 -nw
1356 ;; '(class tty) is for XEmacs-21 -nw
1357 (defface YaTeX-font-lock-label-face
1358 '((((class static-color)) (:foreground "yellow" :underline t))
1359 (((type tty)) (:foreground "yellow" :underline t))
1360 (((class color) (background dark)) (:foreground "pink" :underline t))
1361 (((class color) (background light)) (:foreground "red" :underline t))
1362 (t (:bold t :underline t)))
1363 "Font Lock mode face used to highlight labels."
1364 :group 'font-lock-faces)
1365 (defvar YaTeX-font-lock-label-face 'YaTeX-font-lock-label-face)
1367 (defface YaTeX-font-lock-declaration-face
1368 '((((class color) (background dark)) (:foreground "cyan"))
1369 (((class color) (background light)) (:foreground "RoyalBlue"))
1370 (t (:bold t :underline t)))
1371 "Font Lock mode face used to highlight some declarations."
1372 :group 'font-lock-faces)
1373 (defvar YaTeX-font-lock-declaration-face 'YaTeX-font-lock-declaration-face)
1375 (defface YaTeX-font-lock-include-face
1376 '((((class color) (background dark)) (:foreground "Plum1"))
1377 (((class color) (background light)) (:foreground "purple"))
1378 (t (:bold t :underline t)))
1379 "Font Lock mode face used to highlight expression for including."
1380 :group 'font-lock-faces)
1381 (defvar YaTeX-font-lock-include-face 'YaTeX-font-lock-include-face)
1383 (defface YaTeX-font-lock-formula-face
1384 '((((class static-color)) (:bold t))
1385 (((type tty)) (:bold t))
1386 (((class color) (background dark)) (:foreground "khaki" :bold t))
1387 (((class color) (background light)) (:foreground "Goldenrod"))
1388 (t (:bold t :underline t)))
1389 "Font Lock mode face used to highlight formula."
1390 :group 'font-lock-faces)
1391 (defvar YaTeX-font-lock-formula-face 'YaTeX-font-lock-formula-face)
1393 (defface YaTeX-font-lock-delimiter-face
1394 '((((class static-color)) (:bold t))
1395 (((type tty)) (:bold t))
1396 (((class color) (background dark))
1397 (:foreground "saddlebrown" :background "ivory" :bold t))
1398 (((class color) (background light)) (:foreground "red"))
1399 (t (:bold t :underline t)))
1400 "Font Lock mode face used to highlight delimiters."
1401 :group 'font-lock-faces)
1402 (defvar YaTeX-font-lock-delimiter-face 'YaTeX-font-lock-delimiter-face)
1404 (defface YaTeX-font-lock-math-sub-face
1405 '((((class static-color)) (:bold t))
1406 (((type tty)) (:bold t))
1407 (((class color) (background dark))
1408 (:foreground "khaki" :bold t :underline t))
1409 (((class color) (background light))
1410 (:foreground "Goldenrod" :underline t))
1411 (t (:bold t :underline t)))
1412 "Font Lock mode face used to highlight subscripts in formula."
1413 :group 'font-lock-faces)
1414 (defvar YaTeX-font-lock-math-sub-face 'YaTeX-font-lock-math-sub-face)
1416 (defface YaTeX-font-lock-math-sup-face
1417 '((((class static-color)) (:bold t))
1418 (((type tty)) (:bold t))
1419 (((class color) (background dark))
1420 (:bold nil :foreground "ivory" :background "lightyellow4"))
1421 (((class color) (background light))
1422 (:underline t :foreground "gold"))
1423 (t (:bold t :underline t)))
1424 "Font Lock mode face used to highlight superscripts in formula."
1425 :group 'font-lock-faces)
1426 (defvar YaTeX-font-lock-math-sup-face 'YaTeX-font-lock-math-sup-face)
1428 (defface YaTeX-font-lock-crossref-face
1429 '((((class color) (background dark)) (:foreground "lightgoldenrod"))
1430 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1431 (t (:bold t :underline t)))
1432 "Font Lock mode face used to highlight cross references."
1433 :group 'font-lock-faces)
1434 (defvar YaTeX-font-lock-crossref-face 'YaTeX-font-lock-crossref-face)
1436 (defface YaTeX-font-lock-bold-face
1437 '((t (:bold t)))
1438 "Font Lock mode face used to express bold itself."
1439 :group 'font-lock-faces)
1440 (defvar YaTeX-font-lock-bold-face 'YaTeX-font-lock-bold-face)
1442 (defface YaTeX-font-lock-italic-face
1443 '((t (:italic t)))
1444 "Font Lock mode face used to express italic itself."
1445 :group 'font-lock-faces)
1446 (defvar YaTeX-font-lock-italic-face 'YaTeX-font-lock-italic-face)
1448 ;; Make sure the 'YaTeX-font-lock-{italic,bold}-face is bound with
1449 ;; italic/bold fontsets
1450 (if (and (fboundp 'fontset-list) YaTeX-use-italic-bold)
1451 (let ((flist (fontset-list)) fnt italic bold
1452 (df (or (and (fboundp 'face-font-name) (face-font-name 'default))
1453 (face-font 'default)
1454 (face-font 'italic)
1455 (face-font 'bold)
1456 "giveup!"))
1457 sz medium-i bold-r)
1458 (string-match
1459 "^-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-\\(\\([0-9]+\\)\\)" df)
1460 (setq sz (or (match-string 1 df) "16"))
1461 (setq medium-i (format "-medium-i-[^-]+--%s" sz)
1462 bold-r (format "-bold-r-[^-]+--%s" sz))
1463 (while flist
1464 (setq fnt (car flist))
1465 (condition-case err
1466 (cond
1467 ((and (string-match medium-i fnt)
1468 (null italic))
1469 (set-face-font 'YaTeX-font-lock-italic-face (setq italic fnt)))
1470 ((and (string-match bold-r fnt) (null bold))
1471 (set-face-font 'YaTeX-font-lock-bold-face (setq bold fnt))))
1472 (error nil))
1473 (setq flist (cdr flist)))))
1475 ;;Borrowed from XEmacs's font-lock.el
1476 (defsubst YaTeX-font-lock-fillin (start end setprop markprop value &optional object)
1477 "Fill in one property of the text from START to END.
1478 Arguments PROP and VALUE specify the property and value to put where none are
1479 already in place. Therefore existing property values are not overwritten.
1480 Optional argument OBJECT is the string or buffer containing the text."
1481 (let ((start (text-property-any start end markprop nil object)) next
1482 (putfunc (if (fboundp 'put-nonduplicable-text-property)
1483 'put-nonduplicable-text-property
1484 'put-text-property)))
1485 (if (eq putfunc 'put-text-property)
1486 (setq markprop setprop))
1487 (while start
1488 (setq next (next-single-property-change start markprop object end))
1489 (funcall putfunc start next setprop value object)
1490 (funcall putfunc start next markprop value object)
1491 (setq start (text-property-any next end markprop nil object)))))
1493 (defun YaTeX-warning-font-lock (mode)
1494 (let ((sw (selected-window)))
1495 ;;(pop-to-buffer (format " *%s warning*" mode))
1496 ;;(erase-buffer)
1497 (momentary-string-display
1498 (cond
1499 (YaTeX-japan
1500 (concat mode " は、既に font-lock に対応しました。\n"
1501 "~/.emacs などにある\n"
1502 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
1503 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
1504 "などの間に合わせの記述はもはや不要です。"))
1505 (t
1506 (concat mode " now supports the font-lock by itself.\n"
1507 "So you can remove the descriptions such as\n"
1508 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
1509 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
1510 "in your ~/.emacs file. Thank you."))) (point))
1511 (select-window sw)))
1512 ))
1514 (defun YaTeX-assoc-regexp (elt alist)
1515 "Like assoc, return a list of whose car match with ELT. Search from ALIST.
1516 Note that each car of cons-cell is regexp. ELT is a plain text to be
1517 compared by regexp."
1518 (let (x)
1519 (catch 'found
1520 (while alist
1521 (setq x (car (car alist)))
1522 (if (string-match x elt)
1523 (throw 'found (car alist)))
1524 (setq alist (cdr alist))))))
1526 (defun YaTeX-push-to-kill-ring (string)
1527 "Push STRING to kill-ring, then show guidance message."
1528 (and (stringp string) (string< "" string)
1529 (let ((key (key-description (where-is-internal 'yank nil t)))
1530 (msg
1531 (if YaTeX-japan
1532 " をkill-ringに入れました。次のyank(%s)で貼付できます"
1533 " is stored into kill-ring. Paste it by yank(%s).")))
1534 (kill-new string)
1535 (message (concat "`%s'" msg) string key))))
1537 ;;;
1538 ;; Functions for the Installation time
1539 ;;;
1541 (defun bcf-and-exit ()
1542 "Byte compile rest of argument and kill-emacs."
1543 (if command-line-args-left
1544 (let ((load-path (cons "." load-path)))
1545 (and (fboundp 'set-language-environment)
1546 (featurep 'mule)
1547 (set-language-environment "Japanese"))
1548 (mapcar 'byte-compile-file command-line-args-left)
1549 (kill-emacs))))
1551 (defun tfb-and-exit ()
1552 "Texinfo-format-buffer and kill-emacs."
1553 (if command-line-args-left
1554 (let ((load-path (cons ".." load-path)))
1555 (and (fboundp 'set-language-environment)
1556 (featurep 'mule)
1557 (set-language-environment "Japanese"))
1558 (mapcar (function
1559 (lambda (arg)
1560 (find-file arg)
1561 (texinfo-format-buffer)
1562 (basic-save-buffer)))
1563 command-line-args-left)
1564 (kill-emacs))))
1566 (provide 'yatexlib)
1567 ; Local variables:
1568 ; fill-prefix: ";;; "
1569 ; paragraph-start: "^$\\| \\|;;;$"
1570 ; paragraph-separate: "^$\\| \\|;;;$"
1571 ; coding: sjis
1572 ; End: