23
|
1 ;;; -*- Emacs-Lisp -*-
|
|
2 ;;; YaTeX library of general functions.
|
|
3 ;;; yatexlib.el
|
|
4 ;;; (c )1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
|
|
5 ;;; Last modified Fri Jul 8 00:44:41 1994 on figaro
|
|
6 ;;; $Id$
|
|
7
|
|
8 ;;;###autoload
|
|
9 (defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func)
|
|
10 "Search STRING which is not commented out by CMNTRX.
|
|
11 Optional arguments after BOUND, ERR, CNT are passed literally to search-forward
|
|
12 or search-backward.
|
|
13 Optional sixth argument FUNC changes search-function."
|
|
14 (let ((sfunc (if func func 'search-forward)) found md)
|
|
15 (while (and (prog1
|
|
16 (setq found (funcall sfunc string bound err cnt))
|
|
17 (setq md (match-data)))
|
|
18 (or
|
|
19 (YaTeX-in-verb-p (match-beginning 0))
|
|
20 (save-excursion
|
|
21 (beginning-of-line)
|
|
22 (re-search-forward cmntrx (match-beginning 0) t)))))
|
|
23 (store-match-data md)
|
|
24 found)
|
|
25 )
|
|
26
|
|
27 (defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt)
|
|
28 "Search REGEXP backward which is not commented out by regexp CMNTRX.
|
|
29 See also YaTeX-search-active-forward."
|
|
30 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward)
|
|
31 )
|
|
32 (defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt)
|
|
33 "Search STRING backward which is not commented out by regexp CMNTRX.
|
|
34 See also YaTeX-search-active-forward."
|
|
35 (YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward)
|
|
36 )
|
|
37 (defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt)
|
|
38 "Search REGEXP backward which is not commented out by regexp CMNTRX.
|
|
39 See also YaTeX-search-active-forward."
|
|
40 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-backward)
|
|
41 )
|
|
42
|
|
43
|
|
44 ;;;###autoload
|
|
45 (defun YaTeX-switch-to-buffer (file &optional setbuf)
|
|
46 "Switch to buffer if buffer exists, find file if not.
|
|
47 Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer."
|
|
48 (interactive "Fswitch to file: ")
|
|
49 (let (buf)
|
|
50 (if (setq buf (get-buffer (file-name-nondirectory file)))
|
|
51 (progn
|
|
52 (funcall (if setbuf 'set-buffer 'switch-to-buffer)
|
|
53 (file-name-nondirectory file))
|
|
54 buf)
|
|
55 (if (file-exists-p file)
|
|
56 (progn (find-file file) (current-buffer))
|
|
57 (message "%s was not found in this directory." file)
|
|
58 nil)))
|
|
59 )
|
|
60
|
|
61 ;;;###autoload
|
|
62 (defun YaTeX-switch-to-buffer-other-window (file)
|
|
63 "Switch to buffer if buffer exists, find file if not."
|
|
64 (interactive "Fswitch to file: ")
|
|
65 (if (get-buffer (file-name-nondirectory file))
|
|
66 (progn (switch-to-buffer-other-window file) t)
|
|
67 (if (file-exists-p file)
|
|
68 (progn (find-file-other-window file) t)
|
|
69 (message "%s was not found in this directory." file)
|
|
70 nil))
|
|
71 )
|
|
72
|
|
73 (defun YaTeX-replace-format-sub (string format repl)
|
|
74 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
|
|
75 (string-match (concat "[^%]\\(%" format "\\)") string)))
|
|
76 (len (length format)))
|
|
77 (if (null beg) string ;no conversion
|
|
78 (concat
|
|
79 (substring string 0 (match-beginning 1)) repl
|
|
80 (substring string (match-end 1)))))
|
|
81 )
|
|
82
|
|
83 ;;;###autoload
|
|
84 (defun YaTeX-replace-format (string format repl)
|
|
85 "In STRING, replace first appearance of FORMAT to REPL as if
|
|
86 function `format' does. FORMAT does not contain `%'"
|
|
87 (let ((ans string))
|
|
88 (while (not (string=
|
|
89 ans (setq string (YaTeX-replace-format-sub ans format repl))))
|
|
90 (setq ans string))
|
|
91 string)
|
|
92 )
|
|
93
|
|
94 ;;;###autoload
|
|
95 (defun YaTeX-replace-format-args (string &rest args)
|
|
96 "Translate the argument mark #1, #2, ... #n in the STRING into the
|
|
97 corresponding real arguments ARGS."
|
|
98 (let ((argp 1))
|
|
99 (while args
|
|
100 (setq string
|
|
101 (YaTeX-replace-format string (int-to-string argp) (car args)))
|
|
102 (setq args (cdr args) argp (1+ argp))))
|
|
103 string
|
|
104 )
|
|
105
|
|
106 ;;;###autoload
|
|
107 (defun rindex (string char)
|
|
108 (let ((pos (1- (length string)))(index -1))
|
|
109 (while (>= pos 0)
|
|
110 (cond
|
|
111 ((= (aref string pos) char)
|
|
112 (setq index pos) (setq pos -1))
|
|
113 (t (setq pos (1- pos))))
|
|
114 )
|
|
115 index)
|
|
116 )
|
|
117
|
|
118 ;;;###autoload
|
|
119 (defun YaTeX-showup-buffer (buffer &optional func select)
|
|
120 "Make BUFFER show up in certain window (but current window)
|
|
121 that gives the maximum value by the FUNC. FUNC should take an argument
|
|
122 of its window object. Non-nil for optional third argument SELECT selects
|
|
123 that window."
|
|
124 (or (and (get-buffer-window buffer)
|
|
125 (progn (if select (select-window (get-buffer-window buffer)))
|
|
126 t))
|
|
127 (let ((window (selected-window))
|
|
128 (wlist (YaTeX-window-list)) win w (x 0))
|
|
129 (cond
|
|
130 ((> (length wlist) 2)
|
|
131 (if func
|
|
132 (while wlist
|
|
133 (setq w (car wlist))
|
|
134 (if (and (not (eq window w))
|
|
135 (> (funcall func w) x))
|
|
136 (setq win w x (funcall func w)))
|
|
137 (setq wlist (cdr wlist)))
|
|
138 (setq win (get-lru-window)))
|
|
139 (select-window win)
|
|
140 (switch-to-buffer buffer)
|
|
141 (or select (select-window window)))
|
|
142 ((= (length wlist) 2)
|
|
143 (other-window 1)
|
|
144 (switch-to-buffer buffer)
|
|
145 (or select (select-window window)))
|
|
146 (t ;if one-window
|
|
147 (cond
|
|
148 (YaTeX-default-pop-window-height
|
|
149 (split-window
|
|
150 (selected-window)
|
|
151 (max
|
|
152 (min
|
|
153 (- (screen-height)
|
|
154 (if (numberp YaTeX-default-pop-window-height)
|
|
155 (+ YaTeX-default-pop-window-height 2)
|
|
156 (/ (* (screen-height)
|
|
157 (string-to-int YaTeX-default-pop-window-height))
|
|
158 100)))
|
|
159 (- (screen-height) window-min-height 1))
|
|
160 window-min-height))
|
|
161 (pop-to-buffer buffer)
|
|
162 (or select (select-window window)))
|
|
163 (t nil)))
|
|
164 )))
|
|
165 )
|
|
166
|
|
167 ;;;###autoload
|
|
168 (defun YaTeX-window-list ()
|
|
169 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
|
|
170 (while (not (eq curw (setq win (next-window win))))
|
|
171 (or (eq win (minibuffer-window))
|
|
172 (setq wlist (cons win wlist))))
|
|
173 wlist)
|
|
174 )
|
|
175
|
|
176 ;;;###autoload
|
|
177 (defun substitute-all-key-definition (olddef newdef keymap)
|
|
178 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
|
|
179 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
|
|
180 where ever it appears."
|
|
181 (mapcar
|
|
182 (function (lambda (key) (define-key keymap key newdef)))
|
|
183 (where-is-internal olddef))
|
|
184 )
|
|
185
|
|
186 ;;;###autoload
|
|
187 (defun YaTeX-match-string (n &optional m)
|
|
188 "Return (buffer-substring (match-beginning n) (match-beginning m))."
|
|
189 (if (match-beginning n)
|
|
190 (buffer-substring (match-beginning n)
|
|
191 (match-end (if m m n))))
|
|
192 )
|
|
193
|
|
194 ;;;###autoload
|
|
195 (defun YaTeX-minibuffer-complete ()
|
|
196 "Complete in minibuffer"
|
|
197 (interactive)
|
|
198 (let (beg word compl)
|
|
199 (setq beg (if (and (boundp 'delim) delim)
|
|
200 (save-excursion
|
|
201 (skip-chars-backward (concat "^" delim))
|
|
202 (1- (point)))
|
|
203 (point-min))
|
|
204 word (buffer-substring beg (point-max))
|
|
205 compl (try-completion word minibuffer-completion-table))
|
|
206 (cond
|
|
207 ((eq compl t) nil)
|
|
208 ((eq compl nil)
|
|
209 (ding)
|
|
210 (save-excursion
|
|
211 (let (p)
|
|
212 (goto-char (setq p (point-max)))
|
|
213 (insert " [No match]")
|
|
214 (goto-char p)
|
|
215 (sit-for 2)
|
|
216 (delete-region p (point-max)))))
|
|
217 ((string= compl word)
|
|
218 (with-output-to-temp-buffer "*Completions*"
|
|
219 (display-completion-list
|
|
220 (all-completions word minibuffer-completion-table))))
|
|
221 (t (delete-region beg (point-max))
|
|
222 (insert compl))
|
|
223 ))
|
|
224 )
|
|
225
|
|
226
|
|
227 (provide 'yatexlib)
|