yatex

view yatexlib.el @ 53:5f4b18da14b3

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