yatex

view yatexlib.el @ 51:b0371b6ed799

Created `ChangeLog'. Log hereafter will be written in `ChangeLog'.
author yuuji
date Tue, 20 Dec 1994 21:00:21 +0000
parents eb0512bfcb7f
children 5d94deabb9f9
line source
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 Wed Dec 21 05:58:06 1994 on landcruiser
6 ;;; $Id$
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 (or 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 )
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 )
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-file-buffer file))
51 (progn
52 (funcall (if setbuf 'set-buffer 'switch-to-buffer)
53 (get-file-buffer file))
54 buf)
55 (if (file-exists-p file)
56 (or ;find-file returns nil but set current-buffer...
57 (funcall (if setbuf 'find-file-noselect 'find-file) file)
58 (current-buffer))
59 (message "%s was not found in this directory." file)
60 nil)))
61 )
63 ;;;###autoload
64 (defun YaTeX-switch-to-buffer-other-window (file)
65 "Switch to buffer if buffer exists, find file if not."
66 (interactive "Fswitch to file: ")
67 (if (get-file-buffer file)
68 (progn (switch-to-buffer-other-window (get-file-buffer file)) t)
69 (if (file-exists-p file)
70 (progn (find-file-other-window file) t)
71 (message "%s was not found in this directory." file)
72 nil))
73 )
75 (defun YaTeX-replace-format-sub (string format repl)
76 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
77 (string-match (concat "[^%]\\(%" format "\\)") string)))
78 (len (length format)))
79 (if (null beg) string ;no conversion
80 (concat
81 (substring string 0 (match-beginning 1)) repl
82 (substring string (match-end 1)))))
83 )
85 ;;;###autoload
86 (defun YaTeX-replace-format (string format repl)
87 "In STRING, replace first appearance of FORMAT to REPL as if
88 function `format' does. FORMAT does not contain `%'"
89 (let ((ans string))
90 (while (not (string=
91 ans (setq string (YaTeX-replace-format-sub ans format repl))))
92 (setq ans string))
93 string)
94 )
96 ;;;###autoload
97 (defun YaTeX-replace-format-args (string &rest args)
98 "Translate the argument mark #1, #2, ... #n in the STRING into the
99 corresponding real arguments ARGS."
100 (let ((argp 1))
101 (while args
102 (setq string
103 (YaTeX-replace-format string (int-to-string argp) (car args)))
104 (setq args (cdr args) argp (1+ argp))))
105 string
106 )
108 ;;;###autoload
109 (defun rindex (string char)
110 (let ((pos (1- (length string)))(index -1))
111 (while (>= pos 0)
112 (cond
113 ((= (aref string pos) char)
114 (setq index pos) (setq pos -1))
115 (t (setq pos (1- pos))))
116 )
117 index)
118 )
120 ;;;###autoload
121 (defun YaTeX-showup-buffer (buffer &optional func select)
122 "Make BUFFER show up in certain window (but current window)
123 that gives the maximum value by the FUNC. FUNC should take an argument
124 of its window object. Non-nil for optional third argument SELECT selects
125 that window. This function never selects minibuffer window."
126 (or (and (if YaTeX-emacs-19
127 (get-buffer-window buffer t)
128 (get-buffer-window buffer))
129 (progn
130 (if select
131 (goto-buffer-window buffer))
132 t))
133 (let ((window (selected-window))
134 (wlist (YaTeX-window-list)) win w (x 0))
135 (cond
136 ((> (length wlist) 2)
137 (if func
138 (while wlist
139 (setq w (car wlist))
140 (if (and (not (eq window w))
141 (> (funcall func w) x))
142 (setq win w x (funcall func w)))
143 (setq wlist (cdr wlist)))
144 (setq win (get-lru-window)))
145 (select-window win)
146 (switch-to-buffer buffer)
147 (or select (select-window window)))
148 ((= (length wlist) 2)
149 ;(other-window 1);This does not work properly on Emacs-19
150 (select-window (get-lru-window))
151 (switch-to-buffer buffer)
152 (or select (select-window window)))
153 (t ;if one-window
154 (cond
155 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
156 nil) ;if found in other frame
157 (YaTeX-default-pop-window-height
158 (split-window-calculate-height YaTeX-default-pop-window-height)
159 (pop-to-buffer buffer)
160 (or select (select-window window)))
161 (t nil)))
162 )))
163 )
165 ;;;###autoload
166 (defun split-window-calculate-height (height)
167 "Split current window wight specified HEIGHT.
168 If HEIGHT is number, make new window that has HEIGHT lines.
169 If HEIGHT is string, make new window that occupy HEIGT % of screen height.
170 Otherwise split window conventionally."
171 (if (one-window-p)
172 (split-window
173 (selected-window)
174 (max
175 (min
176 (- (screen-height)
177 (if (numberp YaTeX-default-pop-window-height)
178 (+ YaTeX-default-pop-window-height 2)
179 (/ (* (screen-height)
180 (string-to-int YaTeX-default-pop-window-height))
181 100)))
182 (- (screen-height) window-min-height 1))
183 window-min-height)))
184 )
186 ;;;###autoload
187 (defun YaTeX-window-list ()
188 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
189 (while (not (eq curw (setq win (next-window win))))
190 (or (eq win (minibuffer-window))
191 (setq wlist (cons win wlist))))
192 wlist)
193 )
195 ;;;###autoload
196 (defun substitute-all-key-definition (olddef newdef keymap)
197 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
198 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
199 where ever it appears."
200 (mapcar
201 (function (lambda (key) (define-key keymap key newdef)))
202 (where-is-internal olddef keymap))
203 )
205 ;;;###autoload
206 (defun YaTeX-match-string (n &optional m)
207 "Return (buffer-substring (match-beginning n) (match-beginning m))."
208 (if (match-beginning n)
209 (buffer-substring (match-beginning n)
210 (match-end (or m n))))
211 )
213 ;;;###autoload
214 (defun YaTeX-minibuffer-complete ()
215 "Complete in minibuffer.
216 If the symbol 'delim is bound and is string, its value is assumed to be
217 the character class of delimiters. Completion will be performed on
218 the last field separated by those delimiters.
219 If the symbol 'quick is bound and is 't, when the try-completion results
220 in t, exit minibuffer immediately."
221 (interactive)
222 (let ((md (match-data)) beg word compl
223 (quick (and (boundp 'quick) (eq quick t)))
224 (displist ;function to display completion-list
225 (function
226 (lambda ()
227 (with-output-to-temp-buffer "*Completions*"
228 (display-completion-list
229 (all-completions word minibuffer-completion-table)))))))
230 (setq beg (if (and (boundp 'delim) (stringp delim))
231 (save-excursion
232 (skip-chars-backward (concat "^" delim))
233 (point))
234 (point-min))
235 word (buffer-substring beg (point-max))
236 compl (try-completion word minibuffer-completion-table))
237 (cond
238 ((eq compl t)
239 (if quick (exit-minibuffer)
240 (let ((p (point)) (max (point-max)))
241 (unwind-protect
242 (progn
243 (goto-char max)
244 (insert " [Sole completion]")
245 (goto-char p)
246 (sit-for 1))
247 (delete-region max (point-max))
248 (goto-char p)))))
249 ((eq compl nil)
250 (ding)
251 (save-excursion
252 (let (p)
253 (unwind-protect
254 (progn
255 (goto-char (setq p (point-max)))
256 (insert " [No match]")
257 (goto-char p)
258 (sit-for 2))
259 (delete-region p (point-max))))))
260 ((string= compl word)
261 (funcall displist))
262 (t (delete-region beg (point-max))
263 (insert compl)
264 (if quick
265 (if (eq (try-completion compl minibuffer-completion-table) t)
266 (exit-minibuffer)
267 (funcall displist)))))
268 (store-match-data md))
269 )
271 (defun YaTeX-minibuffer-quick-complete ()
272 "Set 'quick to 't and call YaTeX-minibuffer-complete.
273 See documentation of YaTeX-minibuffer-complete."
274 (interactive)
275 (let ((quick t))
276 (self-insert-command 1)
277 (YaTeX-minibuffer-complete)))
279 (defun foreach-buffers (pattern job)
280 "For each buffer which matches with PATTERN, do JOB."
281 (let ((list (buffer-list)))
282 (save-excursion
283 (while list
284 (set-buffer (car list))
285 (if (or (and (stringp pattern)
286 (buffer-file-name)
287 (string-match pattern (buffer-file-name)))
288 (and (symbolp pattern) major-mode (eq major-mode pattern)))
289 (eval job))
290 (setq list (cdr list)))))
291 )
293 (defun goto-buffer-window (buffer)
294 "Select window which is bound to BUFFER.
295 If no such window exist, switch to buffer BUFFER."
296 (if (stringp buffer)
297 (setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
298 (if (get-buffer buffer)
299 (cond
300 ((get-buffer-window buffer)
301 (select-window (get-buffer-window buffer)))
302 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
303 (let*((win (get-buffer-window buffer t))
304 (frame (window-frame win)))
305 (select-frame frame)
306 (raise-frame frame)
307 (focus-frame frame)
308 (select-window win)
309 (set-mouse-position frame 0 0)
310 (and (featurep 'windows) (fboundp 'win:adjust-window)
311 (win:adjust-window))))
312 (t (switch-to-buffer buffer))))
313 )
315 ;; Here starts the functions which support gmhist-vs-Emacs19 compatible
316 ;; reading with history.
317 ;;;###autoload
318 (defun completing-read-with-history
319 (prompt table &optional predicate must-match initial hsym)
320 "Completing read with general history: gmhist, Emacs-19."
321 (let ((minibuffer-history
322 (or (symbol-value hsym)
323 (and (boundp 'minibuffer-history) minibuffer-history)))
324 (minibuffer-history-symbol (or hsym 'minibuffer-history)))
325 (prog1
326 (if (fboundp 'completing-read-with-history-in)
327 (completing-read-with-history-in
328 minibuffer-history-symbol prompt table predicate must-match initial)
329 (completing-read prompt table predicate must-match initial))
330 (if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
332 ;;;###autoload
333 (defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
334 "Read from minibuffer with general history: gmhist, Emacs-19."
335 (cond
336 (YaTeX-emacs-19
337 (read-from-minibuffer prompt init map read hsym))
338 (t
339 (let ((minibuffer-history-symbol hsym))
340 (read-from-minibuffer prompt init map read)))))
342 ;;;###autoload
343 (defun read-string-with-history (prompt &optional init hsym)
344 "Read string with history: gmhist(Emacs-18) and Emacs-19."
345 (cond
346 (YaTeX-emacs-19
347 (read-from-minibuffer prompt init minibuffer-local-map nil hsym))
348 ((featurep 'gmhist-mh)
349 (read-with-history-in hsym prompt init))
350 (t (read-string prompt init))))
352 (provide 'yatexlib)