yatex

view yatexlib.el @ 68:0eb6997bee16

More adjustment for Emacs20 and XEmacs [prefix] g for <applet> <!--#include> <!--#exec>
author yuuji
date Mon, 26 Oct 1998 12:05:32 +0000
parents 36a48185b95a
children 807c1e7e68b7
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX and yahtml common libraries, general functions and definitions
3 ;;; yatexlib.el
4 ;;; (c )1994-1997 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Mon Oct 19 13:41:29 1998 on firestorm
6 ;;; $Id$
8 ;; General variables
9 (defvar YaTeX-dos (memq system-type '(ms-dos windows-nt OS/2)))
10 (defvar YaTeX-emacs-19 (>= (string-to-int emacs-version) 19))
11 (defvar YaTeX-emacs-20 (>= (string-to-int emacs-version) 20))
12 (defvar YaTeX-user-completion-table
13 (if YaTeX-dos "~/_yatexrc" "~/.yatexrc")
14 "*Default filename in which user completion table is saved.")
16 (defvar YaTeX-japan (or (boundp 'NEMACS) (boundp 'MULE) YaTeX-emacs-20)
17 "Whether yatex mode is running on Japanese environment or not.")
19 (defvar YaTeX-kanji-code-alist
20 (cond
21 ((boundp '*junet*)
22 (list (cons
23 1
24 (if YaTeX-dos (if (boundp '*sjis-dos*) *sjis-dos* *sjis*dos)
25 *sjis*))
26 '(2 . *junet*) '(3 . *euc-japan*)))
27 (YaTeX-emacs-20
28 ;;(cdr-safe(assq 'coding-system (assoc "Japanese" language-info-alist)))
29 (list (cons
30 1 (cond (YaTeX-dos 'shift_jis-dos)
31 ((member 'shift_jis (coding-system-list)) 'shift_jis-unix)
32 (t 'sjis)))
33 '(2 . iso-2022-jp-unix)
34 '(3 . euc-jp-unix))))
35 "Kanji-code expression translation table.")
36 (defvar YaTeX-inhibit-prefix-letter nil
37 "*T for changing key definitions from [prefix] Letter to [prefix] C-Letter.")
39 (defvar YaTeX-no-begend-shortcut nil
40 "*T for disabling shortcut of begin-type completion, [prefix] b d, etc.")
42 (defvar YaTeX-default-pop-window-height 10
43 "Default typesetting buffer height.
44 If integer, sets the window-height of typesetting buffer.
45 If string, sets the percentage of it.
46 If nil, use default pop-to-buffer.")
48 (defvar YaTeX-create-file-prefix-g nil
49 "*Non-nil creates new file when [prefix] g on \\include{foo}.")
51 (defvar YaTeX-nervous t
52 "*If you are nervous about maintenance of yatexrc, set this value to T.
53 And you will have the local dictionary.")
55 ;----------- work variables ----------------------------------------
56 (defvar YaTeX-typesetting-mode-map nil
57 "Keymap used in YaTeX typesetting buffer"
58 )
59 (if YaTeX-typesetting-mode-map nil
60 (setq YaTeX-typesetting-mode-map (make-keymap))
61 ;(suppress-keymap YaTeX-typesetting-mode-map t)
62 (define-key YaTeX-typesetting-mode-map " " 'YaTeX-jump-error-line)
63 (define-key YaTeX-typesetting-mode-map "\C-m" 'YaTeX-send-string)
64 (define-key YaTeX-typesetting-mode-map "1" 'delete-other-windows)
65 (define-key YaTeX-typesetting-mode-map "0" 'delete-window)
66 (define-key YaTeX-typesetting-mode-map "q" 'delete-window))
68 (defvar YaTeX-parent-file nil
69 "*Main LaTeX source file name used when %#! expression doesn't exist.")
70 (make-variable-buffer-local 'YaTeX-parent-file)
72 ;---------- Define default key bindings on YaTeX mode map ----------
73 ;;;###autoload
74 (defun YaTeX-define-key (key binding &optional map)
75 "Define key on YaTeX-prefix-map."
76 (if YaTeX-inhibit-prefix-letter
77 (let ((c (aref key 0)))
78 (cond
79 ((and (>= c ?a) (<= c ?z)) (aset key 0 (1+ (- c ?a))))
80 ((and (>= c ?A) (<= c ?Z) (numberp YaTeX-inhibit-prefix-letter))
81 (aset key 0 (1+ (- c ?A))))
82 (t nil))))
83 (define-key (or map YaTeX-prefix-map) key binding))
85 ;;;###autoload
86 (defun YaTeX-local-table-symbol (symbol)
87 "Return the lisp symbol which keeps local completion table of SYMBOL."
88 (intern (concat "YaTeX$"
89 default-directory
90 (symbol-name symbol))))
92 ;;;###autoload
93 (defun YaTeX-sync-local-table (symbol)
94 "Synchronize local variable SYMBOL.
95 Copy its corresponding directory dependent completion table to SYMBOL."
96 (if (boundp (YaTeX-local-table-symbol symbol))
97 (set symbol (symbol-value (YaTeX-local-table-symbol symbol)))))
99 (defvar YaTeX-user-table-is-read nil
100 "Flag that means whether user completion table has been read or not.")
101 ;;;###autoload
102 (defun YaTeX-read-user-completion-table (&optional forcetoread)
103 "Append user completion table of LaTeX macros"
104 (let*((user-table (expand-file-name YaTeX-user-completion-table))
105 (local-table (expand-file-name (file-name-nondirectory user-table)))
106 var localvar localbuf (curbuf (current-buffer)) sexp)
107 (if YaTeX-user-table-is-read nil
108 (message "Loading user completion table")
109 (if (file-exists-p user-table) (load-file user-table)
110 (message "Welcome to the field of YaTeX. I'm glad to see you!")))
111 (setq YaTeX-user-table-is-read t)
112 (cond
113 ((file-exists-p local-table)
114 (set-buffer (setq localbuf (find-file-noselect local-table)))
115 (widen)
116 (goto-char (point-min))
117 (while (re-search-forward "(setq \\([^ ]+\\)" nil t)
118 (setq var (intern (buffer-substring
119 (match-beginning 1) (match-end 1)))
120 localvar (YaTeX-local-table-symbol var))
121 (goto-char (match-beginning 0))
122 (setq sexp (buffer-substring (point)
123 (progn (forward-sexp) (point))))
124 (set-buffer curbuf)
125 (or (assq var (buffer-local-variables)) (make-local-variable var))
126 (eval (read sexp))
127 (or (and (boundp localvar)
128 (symbol-value localvar)
129 (not forcetoread))
130 (set localvar (symbol-value var)))
131 (set-buffer localbuf))
132 (kill-buffer localbuf)))
133 (set-buffer curbuf)))
135 ;;;###autoload
136 (defun YaTeX-reload-dictionary ()
137 "Reload local dictionary.
138 Use this function after editing ./.yatexrc."
139 (interactive)
140 (let ((YaTeX-user-table-is-read nil))
141 (YaTeX-read-user-completion-table t)))
143 ;;;###autoload
144 (defun YaTeX-lookup-table (word type)
145 "Lookup WORD in completion table whose type is TYPE.
146 This function refers the symbol tmp-TYPE-table, user-TYPE-table, TYPE-table.
147 Typically, TYPE is one of 'env, 'section, 'fontsize, 'singlecmd."
148 (if (symbolp type) (setq type (symbol-name type)))
149 (or (assoc word (symbol-value (intern (concat "tmp-" type "-table"))))
150 (assoc word (symbol-value (intern (concat "user-" type "-table"))))
151 (assoc word (symbol-value (intern (concat type "-table"))))))
153 ;;;###autoload
154 (defun YaTeX-update-table (vallist default-table user-table local-table)
155 "Update completion table if the car of VALLIST is not in current tables.
156 Second argument DEFAULT-TABLE is the quoted symbol of default completion
157 table, third argument USER-TABLE is user table which will be saved in
158 YaTeX-user-completion-table, fourth argument LOCAL-TABLE should have the
159 completion which is valid during current Emacs's session. If you
160 want to make LOCAL-TABLE valid longer span (but restrict in this directory)
161 create the file in current directory which has the same name with
162 YaTeX-user-completion-table."
163 (let ((car-v (car vallist)) key answer
164 (file (file-name-nondirectory YaTeX-user-completion-table)))
165 (cond
166 ((assoc car-v (symbol-value default-table))
167 nil) ;Nothing to do
168 ((setq key (assoc car-v (symbol-value user-table)))
169 (if (equal (cdr vallist) (cdr key)) nil
170 ;; if association hits, but contents differ.
171 (message
172 "%s's attributes turned into %s" (car vallist) (cdr vallist))
173 (set user-table (delq key (symbol-value user-table)))
174 (set user-table (cons vallist (symbol-value user-table)))
175 (YaTeX-update-dictionary
176 YaTeX-user-completion-table user-table "user")))
177 ((setq key (assoc car-v (symbol-value local-table)))
178 (if (equal (cdr vallist) (cdr key)) nil
179 (message
180 "%s's attributes turned into %s" (car vallist) (cdr vallist))
181 (set local-table (delq key (symbol-value local-table)))
182 (set local-table (cons vallist (symbol-value local-table)))
183 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
184 (YaTeX-update-dictionary file local-table)))
185 ;; All of above cases, there are some completion in tables.
186 ;; Then update tables.
187 (t
188 (if (not YaTeX-nervous)
189 (setq answer "u")
190 (message
191 (cond
192 (YaTeX-japan
193 "`%s'の登録先: U)ユーザ辞書 L)ローカル辞書 N)メモリ D)しない")
194 (t
195 "Register `%s' into: U)serDic L)ocalDic N)one D)iscard"))
196 (if (> (length car-v) 23)
197 (concat (substring car-v 0 10) "..." (substring car-v -9))
198 car-v))
199 (setq answer (char-to-string (read-char))))
200 (cond
201 ((string-match answer "uy")
202 (set user-table (cons vallist (symbol-value user-table)))
203 (YaTeX-update-dictionary YaTeX-user-completion-table user-table "user")
204 )
205 ((string-match answer "tl")
206 (set local-table (cons vallist (symbol-value local-table)))
207 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
208 (YaTeX-update-dictionary file local-table))
209 ((string-match answer "d") nil) ;discard it
210 (t (set default-table
211 (cons vallist (symbol-value default-table)))))))))
213 ;;;###autoload
214 (defun YaTeX-cplread-with-learning
215 (prom default-table user-table local-table
216 &optional pred reqmatch init hsym)
217 "Completing read with learning.
218 Do a completing read with prompt PROM. Completion table is what
219 DEFAULT-TABLE, USER-TABLE, LOCAL table are appended in reverse order.
220 Note that these tables are passed by the symbol.
221 Optional arguments PRED, REQMATH and INIT are passed to completing-read
222 as its arguments PREDICATE, REQUIRE-MATCH and INITIAL-INPUT respectively.
223 If optional 8th argument HSYM, history symbol, is passed, use it as
224 history list variable."
225 (YaTeX-sync-local-table local-table)
226 (let*((table (append (symbol-value local-table)
227 (symbol-value user-table)
228 (symbol-value default-table)))
229 (word (completing-read-with-history
230 prom table pred reqmatch init hsym)))
231 (if (and (string< "" word) (not (assoc word table)))
232 (YaTeX-update-table (list word) default-table user-table local-table))
233 word))
235 ;;;###autoload
236 (defun YaTeX-update-dictionary (file symbol &optional type)
237 (let ((local-table-buf (find-file-noselect file))
238 (name (symbol-name symbol))
239 (value (symbol-value symbol)))
240 (save-excursion
241 (message "Updating %s dictionary..." (or type "local"))
242 (set-buffer local-table-buf)
243 (goto-char (point-max))
244 (search-backward (concat "(setq " name) nil t)
245 (delete-region (point) (progn (forward-sexp) (point)))
246 (delete-blank-lines)
247 (insert "(setq " name " '(\n")
248 (mapcar '(lambda (s)
249 (insert (format "%s\n" (prin1-to-string s))))
250 value)
251 (insert "))\n\n")
252 (delete-blank-lines)
253 (basic-save-buffer)
254 (kill-buffer local-table-buf)
255 (message "Updating %s dictionary...Done" (or type "local")))))
257 ;;;###autoload
258 (defun YaTeX-define-begend-key-normal (key env &optional map)
259 "Define short cut YaTeX-make-begin-end key."
260 (YaTeX-define-key
261 key
262 (list 'lambda '(arg) '(interactive "P")
263 (list 'YaTeX-insert-begin-end env 'arg))
264 map))
266 ;;;###autoload
267 (defun YaTeX-define-begend-region-key (key env &optional map)
268 "Define short cut YaTeX-make-begin-end-region key."
269 (YaTeX-define-key key (list 'lambda nil '(interactive)
270 (list 'YaTeX-insert-begin-end env t)) map))
272 ;;;###autoload
273 (defun YaTeX-define-begend-key (key env &optional map)
274 "Define short cut key for begin type completion both for normal
275 and region mode. To customize YaTeX, user should use this function."
276 (YaTeX-define-begend-key-normal key env map)
277 (if YaTeX-inhibit-prefix-letter nil
278 (YaTeX-define-begend-region-key
279 (concat (upcase (substring key 0 1)) (substring key 1)) env)))
281 ;;;###autoload
282 (defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func)
283 "Search STRING which is not commented out by CMNTRX.
284 Optional arguments after BOUND, ERR, CNT are passed literally to search-forward
285 or search-backward.
286 Optional sixth argument FUNC changes search-function."
287 (let ((sfunc (or func 'search-forward)) found md)
288 (while (and (prog1
289 (setq found (funcall sfunc string bound err cnt))
290 (setq md (match-data)))
291 (or
292 (and (eq major-mode 'yatex-mode)
293 (YaTeX-in-verb-p (match-beginning 0)))
294 (save-excursion
295 (beginning-of-line)
296 (re-search-forward cmntrx (match-beginning 0) t)))))
297 (store-match-data md)
298 found)
299 )
301 (defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt)
302 "Search REGEXP backward which is not commented out by regexp CMNTRX.
303 See also YaTeX-search-active-forward."
304 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward)
305 )
306 (defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt)
307 "Search STRING backward which is not commented out by regexp CMNTRX.
308 See also YaTeX-search-active-forward."
309 (YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward)
310 )
311 (defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt)
312 "Search REGEXP backward which is not commented out by regexp CMNTRX.
313 See also YaTeX-search-active-forward."
314 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-backward)
315 )
318 ;;;###autoload
319 (defun YaTeX-switch-to-buffer (file &optional setbuf)
320 "Switch to buffer if buffer exists, find file if not.
321 Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer."
322 (interactive "Fswitch to file: ")
323 (if (bufferp file) (setq file (buffer-file-name file)))
324 (let (buf (hilit-auto-highlight (not setbuf)))
325 (cond
326 ((setq buf (get-file-buffer file))
327 (funcall (if setbuf 'set-buffer 'switch-to-buffer)
328 (get-file-buffer file))
329 buf)
330 ((or YaTeX-create-file-prefix-g (file-exists-p file))
331 (or ;find-file returns nil but set current-buffer...
332 (if setbuf (set-buffer (find-file-noselect file))
333 (find-file file))
334 (current-buffer)))
335 (t (message "%s was not found in this directory." file)
336 nil)))
337 )
339 ;;;###autoload
340 (defun YaTeX-switch-to-buffer-other-window (file)
341 "Switch to buffer if buffer exists, find file if not."
342 (interactive "Fswitch to file: ")
343 (if (bufferp file) (setq file (buffer-file-name file)))
344 (cond
345 ((get-file-buffer file)
346 (switch-to-buffer-other-window (get-file-buffer file))
347 t)
348 ((or YaTeX-create-file-prefix-g (file-exists-p file))
349 (find-file-other-window file) t)
350 (t (message "%s was not found in this directory." file)
351 nil))
352 )
354 (defun YaTeX-replace-format-sub (string format repl)
355 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
356 (string-match (concat "[^%]\\(%" format "\\)") string)))
357 (len (length format)))
358 (if (null beg) string ;no conversion
359 (concat
360 (substring string 0 (match-beginning 1)) repl
361 (substring string (match-end 1)))))
362 )
364 ;;;###autoload
365 (defun YaTeX-replace-format (string format repl)
366 "In STRING, replace first appearance of FORMAT to REPL as if
367 function `format' does. FORMAT does not contain `%'"
368 (let ((ans string))
369 (while (not (string=
370 ans (setq string (YaTeX-replace-format-sub ans format repl))))
371 (setq ans string))
372 string)
373 )
375 ;;;###autoload
376 (defun YaTeX-replace-format-args (string &rest args)
377 "Translate the argument mark #1, #2, ... #n in the STRING into the
378 corresponding real arguments ARGS."
379 (let ((argp 1))
380 (while args
381 (setq string
382 (YaTeX-replace-format string (int-to-string argp) (car args)))
383 (setq args (cdr args) argp (1+ argp))))
384 string
385 )
387 ;;;###autoload
388 (defun rindex (string char)
389 (let ((pos (1- (length string)))(index -1))
390 (while (>= pos 0)
391 (cond
392 ((= (aref string pos) char)
393 (setq index pos) (setq pos -1))
394 (t (setq pos (1- pos))))
395 )
396 index))
398 ;;;###autoload
399 (defun point-beginning-of-line ()
400 (save-excursion (beginning-of-line)(point)))
402 ;;;###autoload
403 (defun point-end-of-line ()
404 (save-excursion (end-of-line)(point)))
407 ;;;###autoload
408 (defun YaTeX-showup-buffer (buffer &optional func select)
409 "Make BUFFER show up in certain window (but current window)
410 that gives the maximum value by the FUNC. FUNC should take an argument
411 of its window object. Non-nil for optional third argument SELECT selects
412 that window. This function never selects minibuffer window."
413 (or (and (if (and YaTeX-emacs-19 select)
414 (get-buffer-window buffer t)
415 (get-buffer-window buffer))
416 (progn
417 (if select
418 (goto-buffer-window buffer))
419 t))
420 (let ((window (selected-window))
421 (wlist (YaTeX-window-list)) win w (x 0))
422 (cond
423 ((> (length wlist) 2)
424 (if func
425 (while wlist
426 (setq w (car wlist))
427 (if (and (not (eq window w))
428 (> (funcall func w) x))
429 (setq win w x (funcall func w)))
430 (setq wlist (cdr wlist)))
431 (setq win (get-lru-window)))
432 (select-window win)
433 (switch-to-buffer buffer)
434 (or select (select-window window)))
435 ((= (length wlist) 2)
436 ;(other-window 1);This does not work properly on Emacs-19
437 (select-window (get-lru-window))
438 (switch-to-buffer buffer)
439 (or select (select-window window)))
440 (t ;if one-window
441 (cond
442 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
443 nil) ;if found in other frame
444 (YaTeX-default-pop-window-height
445 (split-window-calculate-height YaTeX-default-pop-window-height)
446 ;;(pop-to-buffer buffer) ;damn! emacs-19.30
447 (select-window (next-window nil 1))
448 (switch-to-buffer (get-buffer-create buffer))
449 (or select (select-window window)))
450 (t nil)))
451 )))
452 )
454 ;;;###autoload
455 (defun split-window-calculate-height (height)
456 "Split current window wight specified HEIGHT.
457 If HEIGHT is number, make a new window that has HEIGHT lines.
458 If HEIGHT is string, make a new window that occupies HEIGT % of screen height.
459 Otherwise split window conventionally."
460 (if (one-window-p t)
461 (split-window
462 (selected-window)
463 (max
464 (min
465 (- (screen-height)
466 (if (numberp height)
467 (+ height 2)
468 (/ (* (screen-height)
469 (string-to-int height))
470 100)))
471 (- (screen-height) window-min-height 1))
472 window-min-height)))
473 )
475 ;;;###autoload
476 (defun YaTeX-window-list ()
477 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
478 (while (not (eq curw (setq win (next-window win))))
479 (or (eq win (minibuffer-window))
480 (setq wlist (cons win wlist))))
481 wlist)
482 )
484 ;;;###autoload
485 (defun substitute-all-key-definition (olddef newdef keymap)
486 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
487 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
488 where ever it appears."
489 (if YaTeX-emacs-19
490 (substitute-key-definition olddef newdef keymap global-map)
491 (mapcar
492 (function (lambda (key) (define-key keymap key newdef)))
493 (where-is-internal olddef keymap))))
495 ;;;###autoload
496 (defun YaTeX-match-string (n &optional m)
497 "Return (buffer-substring (match-beginning n) (match-beginning m))."
498 (if (match-beginning n)
499 (buffer-substring (match-beginning n)
500 (match-end (or m n))))
501 )
503 ;;;###autoload
504 (defun YaTeX-minibuffer-complete ()
505 "Complete in minibuffer.
506 If the symbol 'delim is bound and is string, its value is assumed to be
507 the character class of delimiters. Completion will be performed on
508 the last field separated by those delimiters.
509 If the symbol 'quick is bound and is 't, when the try-completion results
510 in t, exit minibuffer immediately."
511 (interactive)
512 (let ((md (match-data)) beg word compl
513 (quick (and (boundp 'quick) (eq quick t)))
514 (displist ;function to display completion-list
515 (function
516 (lambda ()
517 (with-output-to-temp-buffer "*Completions*"
518 (display-completion-list
519 (all-completions word minibuffer-completion-table)))))))
520 (setq beg (if (and (boundp 'delim) (stringp delim))
521 (save-excursion
522 (skip-chars-backward (concat "^" delim))
523 (point))
524 (point-min))
525 word (buffer-substring beg (point-max))
526 compl (try-completion word minibuffer-completion-table))
527 (cond
528 ((eq compl t)
529 (if quick (exit-minibuffer)
530 (let ((p (point)) (max (point-max)))
531 (unwind-protect
532 (progn
533 (goto-char max)
534 (insert " [Sole completion]")
535 (goto-char p)
536 (sit-for 1))
537 (delete-region max (point-max))
538 (goto-char p)))))
539 ((eq compl nil)
540 (ding)
541 (save-excursion
542 (let (p)
543 (unwind-protect
544 (progn
545 (goto-char (setq p (point-max)))
546 (insert " [No match]")
547 (goto-char p)
548 (sit-for 2))
549 (delete-region p (point-max))))))
550 ((string= compl word)
551 (funcall displist))
552 (t (delete-region beg (point-max))
553 (insert compl)
554 (if quick
555 (if (eq (try-completion compl minibuffer-completion-table) t)
556 (exit-minibuffer)
557 (funcall displist)))))
558 (store-match-data md))
559 )
561 (defun YaTeX-minibuffer-quick-complete ()
562 "Set 'quick to 't and call YaTeX-minibuffer-complete.
563 See documentation of YaTeX-minibuffer-complete."
564 (interactive)
565 (let ((quick t))
566 (self-insert-command 1)
567 (YaTeX-minibuffer-complete)))
569 (defun foreach-buffers (pattern job)
570 "For each buffer which matches with PATTERN, do JOB."
571 (let ((list (buffer-list)))
572 (save-excursion
573 (while list
574 (set-buffer (car list))
575 (if (or (and (stringp pattern)
576 (buffer-file-name)
577 (string-match pattern (buffer-file-name)))
578 (and (symbolp pattern) major-mode (eq major-mode pattern)))
579 (eval job))
580 (setq list (cdr list)))))
581 )
583 (defun goto-buffer-window (buffer)
584 "Select window which is bound to BUFFER.
585 If no such window exist, switch to buffer BUFFER."
586 (interactive "BGoto buffer: ")
587 (if (stringp buffer)
588 (setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
589 (if (get-buffer buffer)
590 (cond
591 ((get-buffer-window buffer)
592 (select-window (get-buffer-window buffer)))
593 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
594 (let*((win (get-buffer-window buffer t))
595 (frame (window-frame win)))
596 (select-frame frame)
597 (raise-frame frame)
598 (focus-frame frame)
599 (select-window win)
600 (set-mouse-position frame 0 0)
601 (and (featurep 'windows) (fboundp 'win:adjust-window)
602 (win:adjust-window))))
603 ((and (featurep 'windows) (fboundp 'win:get-buffer-window)
604 (let ((w (win:get-buffer-window buffer)))
605 (and w (win:switch-window w))))
606 (select-window (get-buffer-window buffer)))
607 (t (switch-to-buffer buffer))))
608 )
610 ;; Here starts the functions which support gmhist-vs-Emacs19 compatible
611 ;; reading with history.
612 ;;;###autoload
613 (defun completing-read-with-history
614 (prompt table &optional predicate must-match initial hsym)
615 "Completing read with general history: gmhist, Emacs-19."
616 (let ((minibuffer-history
617 (or (symbol-value hsym)
618 (and (boundp 'minibuffer-history) minibuffer-history)))
619 (minibuffer-history-symbol (or hsym 'minibuffer-history)))
620 (prog1
621 (if (fboundp 'completing-read-with-history-in)
622 (completing-read-with-history-in
623 minibuffer-history-symbol prompt table predicate must-match initial)
624 (completing-read prompt table predicate must-match initial))
625 (if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
627 ;;;###autoload
628 (defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
629 "Read from minibuffer with general history: gmhist, Emacs-19."
630 (cond
631 (YaTeX-emacs-19
632 (read-from-minibuffer prompt init map read hsym))
633 (t
634 (let ((minibuffer-history-symbol hsym))
635 (read-from-minibuffer prompt init map read)))))
637 ;;;###autoload
638 (defun read-string-with-history (prompt &optional init hsym)
639 "Read string with history: gmhist(Emacs-18) and Emacs-19."
640 (cond
641 (YaTeX-emacs-19
642 (read-from-minibuffer prompt init minibuffer-local-map nil hsym))
643 ((featurep 'gmhist-mh)
644 (read-with-history-in hsym prompt init))
645 (t (read-string prompt init))))
647 ;;;
648 ;; Interface function for windows.el
649 ;;;
650 ;;;###autoload
651 (defun YaTeX-switch-to-window ()
652 "Switch to windows.el's window decided by last pressed key."
653 (interactive)
654 (or (featurep 'windows) (error "Why don't you use `windows.el'?"))
655 (win-switch-to-window 1 (- last-command-char win:base-key)))
657 ;;;###autoload
658 (defun YaTeX-reindent (col)
659 "Remove current indentation and reindento to COL column."
660 (save-excursion
661 (beginning-of-line)
662 (skip-chars-forward " \t")
663 (if (/= col (current-column))
664 (progn
665 (delete-region (point) (progn (beginning-of-line) (point)))
666 (indent-to col))))
667 (skip-chars-forward " \t" (point-end-of-line)))
669 (defun YaTeX-inner-environment (&optional quick)
670 "Return current inner-most environment.
671 Non-nil for optional argument QUICK restricts search bound to most
672 recent sectioning command. Matching point is stored to property 'point
673 of 'YaTeX-inner-environment, which can be referred by
674 (get 'YaTeX-inner-environment 'point)."
675 (let*((nest 0)
676 (beg (YaTeX-replace-format-args
677 (regexp-quote YaTeX-struct-begin)
678 ;YaTeX-struct-begin ;=== TENTATIVE!! ==
679 YaTeX-struct-name-regexp
680 (if (eq major-mode 'yahtml-mode) "\\s *.*" "")
681 ""))
682 (end (YaTeX-replace-format-args
683 (regexp-quote YaTeX-struct-end)
684 YaTeX-struct-name-regexp "" ""))
685 (begend (concat "\\(" beg "\\)\\|\\(" end "\\)"))
686 bound m0
687 (htmlp (eq major-mode 'yahtml-mode))
688 (open
689 (concat "^" (or (cdr (assq major-mode '((yahtml-mode . "<")))) "{")))
690 (close
691 (concat "^"
692 (or (cdr(assq major-mode '((yahtml-mode . "\n\t >")))) "}"))))
693 (save-excursion
694 (if quick
695 (setq bound
696 (save-excursion
697 (if htmlp
698 ;;(re-search-backward YaTeX-sectioning-regexp nil 1)
699 (goto-char (point-min)) ;Is this enough? 97/6/26
700 (YaTeX-re-search-active-backward
701 (concat YaTeX-ec-regexp
702 "\\(" YaTeX-sectioning-regexp "\\)\\*?{")
703 YaTeX-comment-prefix nil 1))
704 (or (bobp) (end-of-line))
705 (point))))
706 (if (catch 'begin
707 (if (and (numberp bound) (< (point) bound)) (throw 'begin nil))
708 (while (YaTeX-re-search-active-backward
709 begend YaTeX-comment-prefix bound t)
710 (setq m0 (match-beginning 0))
711 (if (looking-at end) ;;(match-beginning 2)
712 (setq nest (1+ nest))
713 (setq nest (1- nest)))
714 (if (< nest 0)
715 (progn
716 (put 'YaTeX-inner-environment 'point m0)
717 (goto-char m0)
718 (put 'YaTeX-inner-environment 'indent (current-column))
719 (throw 'begin t)))))
720 (buffer-substring
721 (progn (skip-chars-forward open) (1+ (point)))
722 (progn (skip-chars-forward close) (point))))))
723 )
725 (defun YaTeX-end-environment ()
726 "Close opening environment"
727 (interactive)
728 (let ((env (YaTeX-inner-environment)))
729 (if (not env) (error "No premature environment")
730 (save-excursion
731 (if (YaTeX-search-active-forward
732 (YaTeX-replace-format-args YaTeX-struct-end env "" "")
733 YaTeX-comment-prefix nil t)
734 (if (y-or-n-p
735 (concat "Environment `" env
736 "' may be already closed. Force close?"))
737 nil
738 (error "end environment aborted."))))
739 (message "") ;Erase (y or n) message.
740 (YaTeX-insert-struc 'end env)
741 (save-excursion
742 (goto-char (or (get 'YaTeX-inner-environment 'point) (match-end 0)))
743 (if (pos-visible-in-window-p)
744 (sit-for (if YaTeX-dos 2 1))
745 (message "Matches with %s at line %d"
746 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
747 (count-lines (point-min) (point)))))))
748 )
750 ;;;VER2
751 (defun YaTeX-insert-struc (what env)
752 (cond
753 ((eq what 'begin)
754 (insert (YaTeX-replace-format-args
755 YaTeX-struct-begin env (YaTeX-addin env))))
756 ((eq what 'end)
757 (insert (YaTeX-replace-format-args YaTeX-struct-end env)))
758 (t nil))
759 )
761 ;;; Function for menu support
762 (defun YaTeX-define-menu (keymap bindlist)
763 "Define KEYMAP(symbol)'s menu-bindings according to BINDLIST.
764 KEYMAP should be a quoted symbol of newly allocated keymap.
765 BINDLIST consists of binding list. Each element is as follows.
767 '(menusymbol DOC_String . contents)
769 CONTENTS is one of lambda-form, interactive function, or other keymap.
770 See yatex19.el for example."
771 (cond
772 ((featurep 'xemacs)
773 (let (name)
774 (if (keymapp (symbol-value keymap))
775 (progn
776 (setq name (keymap-name (symbol-value keymap)))
777 (set keymap nil))
778 (setq name (car (symbol-value keymap)))
779 (set keymap (cdr (symbol-value keymap))))
780 (mapcar
781 (function
782 (lambda (bind)
783 (setq bind (cdr bind))
784 (if (eq (car-safe (cdr bind)) 'lambda)
785 (setcar (cdr bind) 'progn))
786 (if (stringp (car-safe (cdr bind)))
787 (set keymap
788 (cons (cdr bind) (symbol-value keymap)))
789 (set keymap
790 (cons (vector (car bind) (cdr bind) t)
791 (symbol-value keymap))))))
792 bindlist)
793 (set keymap (cons name (symbol-value keymap)))))
794 (t
795 (mapcar
796 (function
797 (lambda (bind)
798 (define-key (symbol-value keymap) (vector (car bind)) (cdr bind))))
799 bindlist))))
802 ;;;
803 ;; Functions for the Installation time
804 ;;;
806 (defun bcf-and-exit ()
807 "Byte compile rest of argument and kill-emacs."
808 (if command-line-args-left
809 (let ((load-path (cons "." load-path)))
810 (and (fboundp 'set-language-environment)
811 (featurep 'mule)
812 (set-language-environment "Japanese"))
813 (mapcar 'byte-compile-file command-line-args-left)
814 (kill-emacs))))
817 (provide 'yatexlib)