yuuji@23: ;;; -*- Emacs-Lisp -*- yuuji@23: ;;; YaTeX library of general functions. yuuji@23: ;;; yatexlib.el yuuji@23: ;;; (c )1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp] yuuji@47: ;;; Last modified Mon Oct 10 22:14:14 1994 on VFR yuuji@23: ;;; $Id$ yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func) yuuji@23: "Search STRING which is not commented out by CMNTRX. yuuji@23: Optional arguments after BOUND, ERR, CNT are passed literally to search-forward yuuji@23: or search-backward. yuuji@23: Optional sixth argument FUNC changes search-function." yuuji@23: (let ((sfunc (if func func 'search-forward)) found md) yuuji@23: (while (and (prog1 yuuji@23: (setq found (funcall sfunc string bound err cnt)) yuuji@23: (setq md (match-data))) yuuji@23: (or yuuji@23: (YaTeX-in-verb-p (match-beginning 0)) yuuji@23: (save-excursion yuuji@23: (beginning-of-line) yuuji@23: (re-search-forward cmntrx (match-beginning 0) t))))) yuuji@23: (store-match-data md) yuuji@23: found) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt) yuuji@23: "Search REGEXP backward which is not commented out by regexp CMNTRX. yuuji@23: See also YaTeX-search-active-forward." yuuji@23: (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward) yuuji@23: ) yuuji@23: (defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt) yuuji@23: "Search STRING backward which is not commented out by regexp CMNTRX. yuuji@23: See also YaTeX-search-active-forward." yuuji@23: (YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward) yuuji@23: ) yuuji@23: (defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt) yuuji@23: "Search REGEXP backward which is not commented out by regexp CMNTRX. yuuji@23: See also YaTeX-search-active-forward." yuuji@23: (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-backward) yuuji@23: ) yuuji@23: yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-switch-to-buffer (file &optional setbuf) yuuji@23: "Switch to buffer if buffer exists, find file if not. yuuji@23: Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer." yuuji@23: (interactive "Fswitch to file: ") yuuji@23: (let (buf) yuuji@23: (if (setq buf (get-buffer (file-name-nondirectory file))) yuuji@23: (progn yuuji@23: (funcall (if setbuf 'set-buffer 'switch-to-buffer) yuuji@23: (file-name-nondirectory file)) yuuji@23: buf) yuuji@23: (if (file-exists-p file) yuuji@46: (funcall yuuji@46: (if setbuf 'find-file-noselect 'find-file) yuuji@46: file) yuuji@23: (message "%s was not found in this directory." file) yuuji@23: nil))) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-switch-to-buffer-other-window (file) yuuji@23: "Switch to buffer if buffer exists, find file if not." yuuji@23: (interactive "Fswitch to file: ") yuuji@23: (if (get-buffer (file-name-nondirectory file)) yuuji@23: (progn (switch-to-buffer-other-window file) t) yuuji@23: (if (file-exists-p file) yuuji@23: (progn (find-file-other-window file) t) yuuji@23: (message "%s was not found in this directory." file) yuuji@23: nil)) yuuji@23: ) yuuji@23: yuuji@23: (defun YaTeX-replace-format-sub (string format repl) yuuji@23: (let ((beg (or (string-match (concat "^\\(%" format "\\)") string) yuuji@23: (string-match (concat "[^%]\\(%" format "\\)") string))) yuuji@23: (len (length format))) yuuji@23: (if (null beg) string ;no conversion yuuji@23: (concat yuuji@23: (substring string 0 (match-beginning 1)) repl yuuji@23: (substring string (match-end 1))))) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-replace-format (string format repl) yuuji@23: "In STRING, replace first appearance of FORMAT to REPL as if yuuji@23: function `format' does. FORMAT does not contain `%'" yuuji@23: (let ((ans string)) yuuji@23: (while (not (string= yuuji@23: ans (setq string (YaTeX-replace-format-sub ans format repl)))) yuuji@23: (setq ans string)) yuuji@23: string) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-replace-format-args (string &rest args) yuuji@23: "Translate the argument mark #1, #2, ... #n in the STRING into the yuuji@23: corresponding real arguments ARGS." yuuji@23: (let ((argp 1)) yuuji@23: (while args yuuji@23: (setq string yuuji@23: (YaTeX-replace-format string (int-to-string argp) (car args))) yuuji@23: (setq args (cdr args) argp (1+ argp)))) yuuji@23: string yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun rindex (string char) yuuji@23: (let ((pos (1- (length string)))(index -1)) yuuji@23: (while (>= pos 0) yuuji@23: (cond yuuji@23: ((= (aref string pos) char) yuuji@23: (setq index pos) (setq pos -1)) yuuji@23: (t (setq pos (1- pos)))) yuuji@23: ) yuuji@23: index) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-showup-buffer (buffer &optional func select) yuuji@23: "Make BUFFER show up in certain window (but current window) yuuji@23: that gives the maximum value by the FUNC. FUNC should take an argument yuuji@23: of its window object. Non-nil for optional third argument SELECT selects yuuji@23: that window." yuuji@47: (or (and (if YaTeX-emacs-19 yuuji@47: (get-buffer-window buffer t) yuuji@47: (get-buffer-window buffer)) yuuji@47: (progn yuuji@47: (if select yuuji@47: (cond yuuji@47: (YaTeX-emacs-19 yuuji@47: (let ((frame (window-frame (get-buffer-window buffer t)))) yuuji@47: (select-frame frame) yuuji@47: (focus-frame frame) yuuji@47: (set-mouse-position frame 0 0) yuuji@47: (raise-frame frame) yuuji@47: (select-window (get-buffer-window buffer)) yuuji@47: (if (and (featurep 'windows) yuuji@47: (win:frame-window frame)) yuuji@47: (win:adjust-window)))) yuuji@47: (t yuuji@47: (select-window (get-buffer-window buffer))))) yuuji@47: t)) yuuji@23: (let ((window (selected-window)) yuuji@23: (wlist (YaTeX-window-list)) win w (x 0)) yuuji@23: (cond yuuji@23: ((> (length wlist) 2) yuuji@23: (if func yuuji@23: (while wlist yuuji@23: (setq w (car wlist)) yuuji@23: (if (and (not (eq window w)) yuuji@23: (> (funcall func w) x)) yuuji@23: (setq win w x (funcall func w))) yuuji@23: (setq wlist (cdr wlist))) yuuji@23: (setq win (get-lru-window))) yuuji@23: (select-window win) yuuji@23: (switch-to-buffer buffer) yuuji@23: (or select (select-window window))) yuuji@23: ((= (length wlist) 2) yuuji@23: (other-window 1) yuuji@23: (switch-to-buffer buffer) yuuji@23: (or select (select-window window))) yuuji@23: (t ;if one-window yuuji@23: (cond yuuji@47: ((and YaTeX-emacs-19 (get-buffer-window buffer t)) yuuji@47: nil) ;if found in other frame yuuji@23: (YaTeX-default-pop-window-height yuuji@23: (split-window yuuji@23: (selected-window) yuuji@23: (max yuuji@23: (min yuuji@23: (- (screen-height) yuuji@23: (if (numberp YaTeX-default-pop-window-height) yuuji@23: (+ YaTeX-default-pop-window-height 2) yuuji@23: (/ (* (screen-height) yuuji@23: (string-to-int YaTeX-default-pop-window-height)) yuuji@23: 100))) yuuji@23: (- (screen-height) window-min-height 1)) yuuji@23: window-min-height)) yuuji@23: (pop-to-buffer buffer) yuuji@23: (or select (select-window window))) yuuji@23: (t nil))) yuuji@23: ))) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-window-list () yuuji@23: (let*((curw (selected-window)) (win curw) (wlist (list curw))) yuuji@23: (while (not (eq curw (setq win (next-window win)))) yuuji@23: (or (eq win (minibuffer-window)) yuuji@23: (setq wlist (cons win wlist)))) yuuji@23: wlist) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun substitute-all-key-definition (olddef newdef keymap) yuuji@23: "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now yuuji@23: defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF yuuji@23: where ever it appears." yuuji@23: (mapcar yuuji@23: (function (lambda (key) (define-key keymap key newdef))) yuuji@47: (where-is-internal olddef keymap)) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-match-string (n &optional m) yuuji@23: "Return (buffer-substring (match-beginning n) (match-beginning m))." yuuji@23: (if (match-beginning n) yuuji@23: (buffer-substring (match-beginning n) yuuji@23: (match-end (if m m n)))) yuuji@23: ) yuuji@23: yuuji@23: ;;;###autoload yuuji@23: (defun YaTeX-minibuffer-complete () yuuji@23: "Complete in minibuffer" yuuji@23: (interactive) yuuji@23: (let (beg word compl) yuuji@23: (setq beg (if (and (boundp 'delim) delim) yuuji@23: (save-excursion yuuji@23: (skip-chars-backward (concat "^" delim)) yuuji@23: (1- (point))) yuuji@23: (point-min)) yuuji@23: word (buffer-substring beg (point-max)) yuuji@23: compl (try-completion word minibuffer-completion-table)) yuuji@23: (cond yuuji@23: ((eq compl t) nil) yuuji@23: ((eq compl nil) yuuji@23: (ding) yuuji@23: (save-excursion yuuji@23: (let (p) yuuji@23: (goto-char (setq p (point-max))) yuuji@23: (insert " [No match]") yuuji@23: (goto-char p) yuuji@23: (sit-for 2) yuuji@23: (delete-region p (point-max))))) yuuji@23: ((string= compl word) yuuji@23: (with-output-to-temp-buffer "*Completions*" yuuji@23: (display-completion-list yuuji@23: (all-completions word minibuffer-completion-table)))) yuuji@23: (t (delete-region beg (point-max)) yuuji@23: (insert compl)) yuuji@23: )) yuuji@23: ) yuuji@23: yuuji@23: yuuji@23: (provide 'yatexlib)