yatex

annotate yatexlib.el @ 56:a9653fbd1c1c

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