yatex

annotate yatexlib.el @ 64:36a48185b95a

Changes are listed in yatex.new. Major one is supporing GNU Emacs20 and XEmacs.
author yuuji
date Tue, 16 Dec 1997 13:28:38 +0000
parents 48ac97a6b6ce
children 0eb6997bee16
rev   line source
yuuji@23 1 ;;; -*- Emacs-Lisp -*-
yuuji@64 2 ;;; YaTeX and yahtml common libraries, general functions and definitions
yuuji@23 3 ;;; yatexlib.el
yuuji@64 4 ;;; (c )1994-1997 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
yuuji@64 5 ;;; Last modified Tue Dec 16 14:48:22 1997 on firestorm
yuuji@23 6 ;;; $Id$
yuuji@23 7
yuuji@64 8 ;; General variables
yuuji@64 9 (defvar YaTeX-dos (memq system-type '(ms-dos windows-nt OS/2)))
yuuji@64 10 (defvar YaTeX-emacs-19 (>= (string-to-int emacs-version) 19))
yuuji@64 11 (defvar YaTeX-emacs-20 (>= (string-to-int emacs-version) 20))
yuuji@64 12 (defvar YaTeX-user-completion-table
yuuji@64 13 (if YaTeX-dos "~/_yatexrc" "~/.yatexrc")
yuuji@64 14 "*Default filename in which user completion table is saved.")
yuuji@64 15
yuuji@64 16 (defvar YaTeX-japan (or (boundp 'NEMACS) (boundp 'MULE) YaTeX-emacs-20)
yuuji@64 17 "Whether yatex mode is running on Japanese environment or not.")
yuuji@64 18
yuuji@64 19 (defvar YaTeX-kanji-code-alist
yuuji@64 20 (cond
yuuji@64 21 ((boundp '*junet*)
yuuji@64 22 (list (cons
yuuji@64 23 1
yuuji@64 24 (if YaTeX-dos (if (boundp '*sjis-dos*) *sjis-dos* *sjis*dos)
yuuji@64 25 *sjis*))
yuuji@64 26 '(2 . *junet*) '(3 . *euc-japan*)))
yuuji@64 27 (YaTeX-emacs-20
yuuji@64 28 ;;(cdr-safe(assq 'coding-system (assoc "Japanese" language-info-alist)))
yuuji@64 29 (list (cons
yuuji@64 30 1 (cond (YaTeX-dos 'shift_jis-dos)
yuuji@64 31 ((member 'shift_jis (coding-system-list)) 'shift_jis-unix)
yuuji@64 32 (t 'sjis)))
yuuji@64 33 '(2 . iso-2022-7bit-unix)
yuuji@64 34 '(3 . euc-japan))))
yuuji@64 35 "Kanji-code expression translation table.")
yuuji@64 36 (defvar YaTeX-inhibit-prefix-letter nil
yuuji@64 37 "*T for changing key definitions from [prefix] Letter to [prefix] C-Letter.")
yuuji@64 38
yuuji@64 39 (defvar YaTeX-no-begend-shortcut nil
yuuji@64 40 "*T for disabling shortcut of begin-type completion, [prefix] b d, etc.")
yuuji@64 41
yuuji@64 42 (defvar YaTeX-default-pop-window-height 10
yuuji@64 43 "Default typesetting buffer height.
yuuji@64 44 If integer, sets the window-height of typesetting buffer.
yuuji@64 45 If string, sets the percentage of it.
yuuji@64 46 If nil, use default pop-to-buffer.")
yuuji@64 47
yuuji@64 48 (defvar YaTeX-create-file-prefix-g nil
yuuji@64 49 "*Non-nil creates new file when [prefix] g on \\include{foo}.")
yuuji@64 50
yuuji@64 51 (defvar YaTeX-nervous t
yuuji@64 52 "*If you are nervous about maintenance of yatexrc, set this value to T.
yuuji@64 53 And you will have the local dictionary.")
yuuji@64 54
yuuji@64 55 ;----------- work variables ----------------------------------------
yuuji@64 56 (defvar YaTeX-typesetting-mode-map nil
yuuji@64 57 "Keymap used in YaTeX typesetting buffer"
yuuji@64 58 )
yuuji@64 59 (if YaTeX-typesetting-mode-map nil
yuuji@64 60 (setq YaTeX-typesetting-mode-map (make-keymap))
yuuji@64 61 ;(suppress-keymap YaTeX-typesetting-mode-map t)
yuuji@64 62 (define-key YaTeX-typesetting-mode-map " " 'YaTeX-jump-error-line)
yuuji@64 63 (define-key YaTeX-typesetting-mode-map "\C-m" 'YaTeX-send-string)
yuuji@64 64 (define-key YaTeX-typesetting-mode-map "1" 'delete-other-windows)
yuuji@64 65 (define-key YaTeX-typesetting-mode-map "0" 'delete-window)
yuuji@64 66 (define-key YaTeX-typesetting-mode-map "q" 'delete-window))
yuuji@64 67
yuuji@64 68 (defvar YaTeX-parent-file nil
yuuji@64 69 "*Main LaTeX source file name used when %#! expression doesn't exist.")
yuuji@64 70 (make-variable-buffer-local 'YaTeX-parent-file)
yuuji@64 71
yuuji@64 72 ;---------- Define default key bindings on YaTeX mode map ----------
yuuji@64 73 ;;;###autoload
yuuji@64 74 (defun YaTeX-define-key (key binding &optional map)
yuuji@64 75 "Define key on YaTeX-prefix-map."
yuuji@64 76 (if YaTeX-inhibit-prefix-letter
yuuji@64 77 (let ((c (aref key 0)))
yuuji@64 78 (cond
yuuji@64 79 ((and (>= c ?a) (<= c ?z)) (aset key 0 (1+ (- c ?a))))
yuuji@64 80 ((and (>= c ?A) (<= c ?Z) (numberp YaTeX-inhibit-prefix-letter))
yuuji@64 81 (aset key 0 (1+ (- c ?A))))
yuuji@64 82 (t nil))))
yuuji@64 83 (define-key (or map YaTeX-prefix-map) key binding))
yuuji@64 84
yuuji@64 85 ;;;###autoload
yuuji@64 86 (defun YaTeX-local-table-symbol (symbol)
yuuji@64 87 "Return the lisp symbol which keeps local completion table of SYMBOL."
yuuji@64 88 (intern (concat "YaTeX$"
yuuji@64 89 default-directory
yuuji@64 90 (symbol-name symbol))))
yuuji@64 91
yuuji@64 92 ;;;###autoload
yuuji@64 93 (defun YaTeX-sync-local-table (symbol)
yuuji@64 94 "Synchronize local variable SYMBOL.
yuuji@64 95 Copy its corresponding directory dependent completion table to SYMBOL."
yuuji@64 96 (if (boundp (YaTeX-local-table-symbol symbol))
yuuji@64 97 (set symbol (symbol-value (YaTeX-local-table-symbol symbol)))))
yuuji@64 98
yuuji@64 99 (defvar YaTeX-user-table-is-read nil
yuuji@64 100 "Flag that means whether user completion table has been read or not.")
yuuji@64 101 ;;;###autoload
yuuji@64 102 (defun YaTeX-read-user-completion-table (&optional forcetoread)
yuuji@64 103 "Append user completion table of LaTeX macros"
yuuji@64 104 (let*((user-table (expand-file-name YaTeX-user-completion-table))
yuuji@64 105 (local-table (expand-file-name (file-name-nondirectory user-table)))
yuuji@64 106 var localvar localbuf (curbuf (current-buffer)) sexp)
yuuji@64 107 (if YaTeX-user-table-is-read nil
yuuji@64 108 (message "Loading user completion table")
yuuji@64 109 (if (file-exists-p user-table) (load-file user-table)
yuuji@64 110 (message "Welcome to the field of YaTeX. I'm glad to see you!")))
yuuji@64 111 (setq YaTeX-user-table-is-read t)
yuuji@64 112 (cond
yuuji@64 113 ((file-exists-p local-table)
yuuji@64 114 (set-buffer (setq localbuf (find-file-noselect local-table)))
yuuji@64 115 (widen)
yuuji@64 116 (goto-char (point-min))
yuuji@64 117 (while (re-search-forward "(setq \\([^ ]+\\)" nil t)
yuuji@64 118 (setq var (intern (buffer-substring
yuuji@64 119 (match-beginning 1) (match-end 1)))
yuuji@64 120 localvar (YaTeX-local-table-symbol var))
yuuji@64 121 (goto-char (match-beginning 0))
yuuji@64 122 (setq sexp (buffer-substring (point)
yuuji@64 123 (progn (forward-sexp) (point))))
yuuji@64 124 (set-buffer curbuf)
yuuji@64 125 (or (assq var (buffer-local-variables)) (make-local-variable var))
yuuji@64 126 (eval (read sexp))
yuuji@64 127 (or (and (boundp localvar)
yuuji@64 128 (symbol-value localvar)
yuuji@64 129 (not forcetoread))
yuuji@64 130 (set localvar (symbol-value var)))
yuuji@64 131 (set-buffer localbuf))
yuuji@64 132 (kill-buffer localbuf)))
yuuji@64 133 (set-buffer curbuf)))
yuuji@64 134
yuuji@64 135 ;;;###autoload
yuuji@64 136 (defun YaTeX-reload-dictionary ()
yuuji@64 137 "Reload local dictionary.
yuuji@64 138 Use this function after editing ./.yatexrc."
yuuji@64 139 (interactive)
yuuji@64 140 (let ((YaTeX-user-table-is-read nil))
yuuji@64 141 (YaTeX-read-user-completion-table t)))
yuuji@64 142
yuuji@64 143 ;;;###autoload
yuuji@64 144 (defun YaTeX-lookup-table (word type)
yuuji@64 145 "Lookup WORD in completion table whose type is TYPE.
yuuji@64 146 This function refers the symbol tmp-TYPE-table, user-TYPE-table, TYPE-table.
yuuji@64 147 Typically, TYPE is one of 'env, 'section, 'fontsize, 'singlecmd."
yuuji@64 148 (if (symbolp type) (setq type (symbol-name type)))
yuuji@64 149 (or (assoc word (symbol-value (intern (concat "tmp-" type "-table"))))
yuuji@64 150 (assoc word (symbol-value (intern (concat "user-" type "-table"))))
yuuji@64 151 (assoc word (symbol-value (intern (concat type "-table"))))))
yuuji@64 152
yuuji@64 153 ;;;###autoload
yuuji@64 154 (defun YaTeX-update-table (vallist default-table user-table local-table)
yuuji@64 155 "Update completion table if the car of VALLIST is not in current tables.
yuuji@64 156 Second argument DEFAULT-TABLE is the quoted symbol of default completion
yuuji@64 157 table, third argument USER-TABLE is user table which will be saved in
yuuji@64 158 YaTeX-user-completion-table, fourth argument LOCAL-TABLE should have the
yuuji@64 159 completion which is valid during current Emacs's session. If you
yuuji@64 160 want to make LOCAL-TABLE valid longer span (but restrict in this directory)
yuuji@64 161 create the file in current directory which has the same name with
yuuji@64 162 YaTeX-user-completion-table."
yuuji@64 163 (let ((car-v (car vallist)) key answer
yuuji@64 164 (file (file-name-nondirectory YaTeX-user-completion-table)))
yuuji@64 165 (cond
yuuji@64 166 ((assoc car-v (symbol-value default-table))
yuuji@64 167 nil) ;Nothing to do
yuuji@64 168 ((setq key (assoc car-v (symbol-value user-table)))
yuuji@64 169 (if (equal (cdr vallist) (cdr key)) nil
yuuji@64 170 ;; if association hits, but contents differ.
yuuji@64 171 (message
yuuji@64 172 "%s's attributes turned into %s" (car vallist) (cdr vallist))
yuuji@64 173 (set user-table (delq key (symbol-value user-table)))
yuuji@64 174 (set user-table (cons vallist (symbol-value user-table)))
yuuji@64 175 (YaTeX-update-dictionary
yuuji@64 176 YaTeX-user-completion-table user-table "user")))
yuuji@64 177 ((setq key (assoc car-v (symbol-value local-table)))
yuuji@64 178 (if (equal (cdr vallist) (cdr key)) nil
yuuji@64 179 (message
yuuji@64 180 "%s's attributes turned into %s" (car vallist) (cdr vallist))
yuuji@64 181 (set local-table (delq key (symbol-value local-table)))
yuuji@64 182 (set local-table (cons vallist (symbol-value local-table)))
yuuji@64 183 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
yuuji@64 184 (YaTeX-update-dictionary file local-table)))
yuuji@64 185 ;; All of above cases, there are some completion in tables.
yuuji@64 186 ;; Then update tables.
yuuji@64 187 (t
yuuji@64 188 (if (not YaTeX-nervous)
yuuji@64 189 (setq answer "u")
yuuji@64 190 (message
yuuji@64 191 (cond
yuuji@64 192 (YaTeX-japan
yuuji@64 193 "`%s'$B$NEPO?@h(B: U)$B%f!<%6<-=q(B L)$B%m!<%+%k<-=q(B N)$B%a%b%j(B D)$B$7$J$$(B")
yuuji@64 194 (t
yuuji@64 195 "Register `%s' into: U)serDic L)ocalDic N)one D)iscard"))
yuuji@64 196 (if (> (length car-v) 23)
yuuji@64 197 (concat (substring car-v 0 10) "..." (substring car-v -10))
yuuji@64 198 car-v))
yuuji@64 199 (setq answer (char-to-string (read-char))))
yuuji@64 200 (cond
yuuji@64 201 ((string-match answer "uy")
yuuji@64 202 (set user-table (cons vallist (symbol-value user-table)))
yuuji@64 203 (YaTeX-update-dictionary YaTeX-user-completion-table user-table "user")
yuuji@64 204 )
yuuji@64 205 ((string-match answer "tl")
yuuji@64 206 (set local-table (cons vallist (symbol-value local-table)))
yuuji@64 207 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
yuuji@64 208 (YaTeX-update-dictionary file local-table))
yuuji@64 209 ((string-match answer "d") nil) ;discard it
yuuji@64 210 (t (set default-table
yuuji@64 211 (cons vallist (symbol-value default-table)))))))))
yuuji@64 212
yuuji@64 213 ;;;###autoload
yuuji@64 214 (defun YaTeX-cplread-with-learning
yuuji@64 215 (prom default-table user-table local-table
yuuji@64 216 &optional pred reqmatch init hsym)
yuuji@64 217 "Completing read with learning.
yuuji@64 218 Do a completing read with prompt PROM. Completion table is what
yuuji@64 219 DEFAULT-TABLE, USER-TABLE, LOCAL table are appended in reverse order.
yuuji@64 220 Note that these tables are passed by the symbol.
yuuji@64 221 Optional arguments PRED, REQMATH and INIT are passed to completing-read
yuuji@64 222 as its arguments PREDICATE, REQUIRE-MATCH and INITIAL-INPUT respectively.
yuuji@64 223 If optional 8th argument HSYM, history symbol, is passed, use it as
yuuji@64 224 history list variable."
yuuji@64 225 (YaTeX-sync-local-table local-table)
yuuji@64 226 (let*((table (append (symbol-value local-table)
yuuji@64 227 (symbol-value user-table)
yuuji@64 228 (symbol-value default-table)))
yuuji@64 229 (word (completing-read-with-history
yuuji@64 230 prom table pred reqmatch init hsym)))
yuuji@64 231 (if (and (string< "" word) (not (assoc word table)))
yuuji@64 232 (YaTeX-update-table (list word) default-table user-table local-table))
yuuji@64 233 word))
yuuji@64 234
yuuji@64 235 ;;;###autoload
yuuji@64 236 (defun YaTeX-update-dictionary (file symbol &optional type)
yuuji@64 237 (let ((local-table-buf (find-file-noselect file))
yuuji@64 238 (name (symbol-name symbol))
yuuji@64 239 (value (symbol-value symbol)))
yuuji@64 240 (save-excursion
yuuji@64 241 (message "Updating %s dictionary..." (or type "local"))
yuuji@64 242 (set-buffer local-table-buf)
yuuji@64 243 (goto-char (point-max))
yuuji@64 244 (search-backward (concat "(setq " name) nil t)
yuuji@64 245 (delete-region (point) (progn (forward-sexp) (point)))
yuuji@64 246 (delete-blank-lines)
yuuji@64 247 (insert "(setq " name " '(\n")
yuuji@64 248 (mapcar '(lambda (s)
yuuji@64 249 (insert (format "%s\n" (prin1-to-string s))))
yuuji@64 250 value)
yuuji@64 251 (insert "))\n\n")
yuuji@64 252 (delete-blank-lines)
yuuji@64 253 (basic-save-buffer)
yuuji@64 254 (kill-buffer local-table-buf)
yuuji@64 255 (message "Updating %s dictionary...Done" (or type "local")))))
yuuji@64 256
yuuji@64 257 ;;;###autoload
yuuji@64 258 (defun YaTeX-define-begend-key-normal (key env &optional map)
yuuji@64 259 "Define short cut YaTeX-make-begin-end key."
yuuji@64 260 (YaTeX-define-key
yuuji@64 261 key
yuuji@64 262 (list 'lambda '(arg) '(interactive "P")
yuuji@64 263 (list 'YaTeX-insert-begin-end env 'arg))
yuuji@64 264 map))
yuuji@64 265
yuuji@64 266 ;;;###autoload
yuuji@64 267 (defun YaTeX-define-begend-region-key (key env &optional map)
yuuji@64 268 "Define short cut YaTeX-make-begin-end-region key."
yuuji@64 269 (YaTeX-define-key key (list 'lambda nil '(interactive)
yuuji@64 270 (list 'YaTeX-insert-begin-end env t)) map))
yuuji@64 271
yuuji@64 272 ;;;###autoload
yuuji@64 273 (defun YaTeX-define-begend-key (key env &optional map)
yuuji@64 274 "Define short cut key for begin type completion both for normal
yuuji@64 275 and region mode. To customize YaTeX, user should use this function."
yuuji@64 276 (YaTeX-define-begend-key-normal key env map)
yuuji@64 277 (if YaTeX-inhibit-prefix-letter nil
yuuji@64 278 (YaTeX-define-begend-region-key
yuuji@64 279 (concat (upcase (substring key 0 1)) (substring key 1)) env)))
yuuji@64 280
yuuji@23 281 ;;;###autoload
yuuji@23 282 (defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func)
yuuji@23 283 "Search STRING which is not commented out by CMNTRX.
yuuji@23 284 Optional arguments after BOUND, ERR, CNT are passed literally to search-forward
yuuji@23 285 or search-backward.
yuuji@23 286 Optional sixth argument FUNC changes search-function."
yuuji@49 287 (let ((sfunc (or func 'search-forward)) found md)
yuuji@23 288 (while (and (prog1
yuuji@23 289 (setq found (funcall sfunc string bound err cnt))
yuuji@23 290 (setq md (match-data)))
yuuji@23 291 (or
yuuji@64 292 (and (eq major-mode 'yatex-mode)
yuuji@64 293 (YaTeX-in-verb-p (match-beginning 0)))
yuuji@23 294 (save-excursion
yuuji@23 295 (beginning-of-line)
yuuji@23 296 (re-search-forward cmntrx (match-beginning 0) t)))))
yuuji@23 297 (store-match-data md)
yuuji@23 298 found)
yuuji@23 299 )
yuuji@23 300
yuuji@23 301 (defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt)
yuuji@23 302 "Search REGEXP backward which is not commented out by regexp CMNTRX.
yuuji@23 303 See also YaTeX-search-active-forward."
yuuji@23 304 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward)
yuuji@23 305 )
yuuji@23 306 (defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt)
yuuji@23 307 "Search STRING backward which is not commented out by regexp CMNTRX.
yuuji@23 308 See also YaTeX-search-active-forward."
yuuji@23 309 (YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward)
yuuji@23 310 )
yuuji@23 311 (defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt)
yuuji@23 312 "Search REGEXP backward which is not commented out by regexp CMNTRX.
yuuji@23 313 See also YaTeX-search-active-forward."
yuuji@23 314 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-backward)
yuuji@23 315 )
yuuji@23 316
yuuji@23 317
yuuji@23 318 ;;;###autoload
yuuji@23 319 (defun YaTeX-switch-to-buffer (file &optional setbuf)
yuuji@23 320 "Switch to buffer if buffer exists, find file if not.
yuuji@23 321 Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer."
yuuji@23 322 (interactive "Fswitch to file: ")
yuuji@52 323 (if (bufferp file) (setq file (buffer-file-name file)))
yuuji@52 324 (let (buf (hilit-auto-highlight (not setbuf)))
yuuji@52 325 (cond
yuuji@52 326 ((setq buf (get-file-buffer file))
yuuji@52 327 (funcall (if setbuf 'set-buffer 'switch-to-buffer)
yuuji@52 328 (get-file-buffer file))
yuuji@52 329 buf)
yuuji@52 330 ((or YaTeX-create-file-prefix-g (file-exists-p file))
yuuji@52 331 (or ;find-file returns nil but set current-buffer...
yuuji@52 332 (if setbuf (set-buffer (find-file-noselect file))
yuuji@52 333 (find-file file))
yuuji@52 334 (current-buffer)))
yuuji@52 335 (t (message "%s was not found in this directory." file)
yuuji@23 336 nil)))
yuuji@23 337 )
yuuji@23 338
yuuji@23 339 ;;;###autoload
yuuji@23 340 (defun YaTeX-switch-to-buffer-other-window (file)
yuuji@23 341 "Switch to buffer if buffer exists, find file if not."
yuuji@23 342 (interactive "Fswitch to file: ")
yuuji@52 343 (if (bufferp file) (setq file (buffer-file-name file)))
yuuji@52 344 (cond
yuuji@52 345 ((get-file-buffer file)
yuuji@52 346 (switch-to-buffer-other-window (get-file-buffer file))
yuuji@52 347 t)
yuuji@52 348 ((or YaTeX-create-file-prefix-g (file-exists-p file))
yuuji@52 349 (find-file-other-window file) t)
yuuji@52 350 (t (message "%s was not found in this directory." file)
yuuji@23 351 nil))
yuuji@23 352 )
yuuji@23 353
yuuji@23 354 (defun YaTeX-replace-format-sub (string format repl)
yuuji@23 355 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
yuuji@23 356 (string-match (concat "[^%]\\(%" format "\\)") string)))
yuuji@23 357 (len (length format)))
yuuji@23 358 (if (null beg) string ;no conversion
yuuji@23 359 (concat
yuuji@23 360 (substring string 0 (match-beginning 1)) repl
yuuji@23 361 (substring string (match-end 1)))))
yuuji@23 362 )
yuuji@23 363
yuuji@23 364 ;;;###autoload
yuuji@23 365 (defun YaTeX-replace-format (string format repl)
yuuji@23 366 "In STRING, replace first appearance of FORMAT to REPL as if
yuuji@23 367 function `format' does. FORMAT does not contain `%'"
yuuji@23 368 (let ((ans string))
yuuji@23 369 (while (not (string=
yuuji@23 370 ans (setq string (YaTeX-replace-format-sub ans format repl))))
yuuji@23 371 (setq ans string))
yuuji@23 372 string)
yuuji@23 373 )
yuuji@23 374
yuuji@23 375 ;;;###autoload
yuuji@23 376 (defun YaTeX-replace-format-args (string &rest args)
yuuji@23 377 "Translate the argument mark #1, #2, ... #n in the STRING into the
yuuji@23 378 corresponding real arguments ARGS."
yuuji@23 379 (let ((argp 1))
yuuji@23 380 (while args
yuuji@23 381 (setq string
yuuji@23 382 (YaTeX-replace-format string (int-to-string argp) (car args)))
yuuji@23 383 (setq args (cdr args) argp (1+ argp))))
yuuji@23 384 string
yuuji@23 385 )
yuuji@23 386
yuuji@23 387 ;;;###autoload
yuuji@23 388 (defun rindex (string char)
yuuji@23 389 (let ((pos (1- (length string)))(index -1))
yuuji@23 390 (while (>= pos 0)
yuuji@23 391 (cond
yuuji@23 392 ((= (aref string pos) char)
yuuji@23 393 (setq index pos) (setq pos -1))
yuuji@23 394 (t (setq pos (1- pos))))
yuuji@23 395 )
yuuji@64 396 index))
yuuji@64 397
yuuji@64 398 ;;;###autoload
yuuji@64 399 (defun point-beginning-of-line ()
yuuji@64 400 (save-excursion (beginning-of-line)(point)))
yuuji@64 401
yuuji@64 402 ;;;###autoload
yuuji@64 403 (defun point-end-of-line ()
yuuji@64 404 (save-excursion (end-of-line)(point)))
yuuji@64 405
yuuji@23 406
yuuji@23 407 ;;;###autoload
yuuji@23 408 (defun YaTeX-showup-buffer (buffer &optional func select)
yuuji@23 409 "Make BUFFER show up in certain window (but current window)
yuuji@23 410 that gives the maximum value by the FUNC. FUNC should take an argument
yuuji@23 411 of its window object. Non-nil for optional third argument SELECT selects
yuuji@49 412 that window. This function never selects minibuffer window."
yuuji@53 413 (or (and (if (and YaTeX-emacs-19 select)
yuuji@47 414 (get-buffer-window buffer t)
yuuji@47 415 (get-buffer-window buffer))
yuuji@47 416 (progn
yuuji@47 417 (if select
yuuji@51 418 (goto-buffer-window buffer))
yuuji@47 419 t))
yuuji@23 420 (let ((window (selected-window))
yuuji@23 421 (wlist (YaTeX-window-list)) win w (x 0))
yuuji@23 422 (cond
yuuji@23 423 ((> (length wlist) 2)
yuuji@23 424 (if func
yuuji@23 425 (while wlist
yuuji@23 426 (setq w (car wlist))
yuuji@23 427 (if (and (not (eq window w))
yuuji@23 428 (> (funcall func w) x))
yuuji@23 429 (setq win w x (funcall func w)))
yuuji@23 430 (setq wlist (cdr wlist)))
yuuji@23 431 (setq win (get-lru-window)))
yuuji@23 432 (select-window win)
yuuji@23 433 (switch-to-buffer buffer)
yuuji@23 434 (or select (select-window window)))
yuuji@23 435 ((= (length wlist) 2)
yuuji@49 436 ;(other-window 1);This does not work properly on Emacs-19
yuuji@49 437 (select-window (get-lru-window))
yuuji@23 438 (switch-to-buffer buffer)
yuuji@23 439 (or select (select-window window)))
yuuji@23 440 (t ;if one-window
yuuji@23 441 (cond
yuuji@47 442 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
yuuji@47 443 nil) ;if found in other frame
yuuji@23 444 (YaTeX-default-pop-window-height
yuuji@51 445 (split-window-calculate-height YaTeX-default-pop-window-height)
yuuji@59 446 ;;(pop-to-buffer buffer) ;damn! emacs-19.30
yuuji@59 447 (select-window (next-window nil 1))
yuuji@59 448 (switch-to-buffer (get-buffer-create buffer))
yuuji@23 449 (or select (select-window window)))
yuuji@23 450 (t nil)))
yuuji@23 451 )))
yuuji@23 452 )
yuuji@23 453
yuuji@23 454 ;;;###autoload
yuuji@51 455 (defun split-window-calculate-height (height)
yuuji@51 456 "Split current window wight specified HEIGHT.
yuuji@59 457 If HEIGHT is number, make a new window that has HEIGHT lines.
yuuji@59 458 If HEIGHT is string, make a new window that occupies HEIGT % of screen height.
yuuji@51 459 Otherwise split window conventionally."
yuuji@59 460 (if (one-window-p t)
yuuji@51 461 (split-window
yuuji@51 462 (selected-window)
yuuji@51 463 (max
yuuji@51 464 (min
yuuji@51 465 (- (screen-height)
yuuji@59 466 (if (numberp height)
yuuji@59 467 (+ height 2)
yuuji@51 468 (/ (* (screen-height)
yuuji@59 469 (string-to-int height))
yuuji@51 470 100)))
yuuji@51 471 (- (screen-height) window-min-height 1))
yuuji@51 472 window-min-height)))
yuuji@51 473 )
yuuji@51 474
yuuji@51 475 ;;;###autoload
yuuji@23 476 (defun YaTeX-window-list ()
yuuji@23 477 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
yuuji@23 478 (while (not (eq curw (setq win (next-window win))))
yuuji@23 479 (or (eq win (minibuffer-window))
yuuji@23 480 (setq wlist (cons win wlist))))
yuuji@23 481 wlist)
yuuji@23 482 )
yuuji@23 483
yuuji@23 484 ;;;###autoload
yuuji@23 485 (defun substitute-all-key-definition (olddef newdef keymap)
yuuji@23 486 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
yuuji@23 487 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
yuuji@23 488 where ever it appears."
yuuji@23 489 (mapcar
yuuji@23 490 (function (lambda (key) (define-key keymap key newdef)))
yuuji@47 491 (where-is-internal olddef keymap))
yuuji@23 492 )
yuuji@23 493
yuuji@23 494 ;;;###autoload
yuuji@23 495 (defun YaTeX-match-string (n &optional m)
yuuji@23 496 "Return (buffer-substring (match-beginning n) (match-beginning m))."
yuuji@23 497 (if (match-beginning n)
yuuji@23 498 (buffer-substring (match-beginning n)
yuuji@49 499 (match-end (or m n))))
yuuji@23 500 )
yuuji@23 501
yuuji@23 502 ;;;###autoload
yuuji@23 503 (defun YaTeX-minibuffer-complete ()
yuuji@49 504 "Complete in minibuffer.
yuuji@51 505 If the symbol 'delim is bound and is string, its value is assumed to be
yuuji@49 506 the character class of delimiters. Completion will be performed on
yuuji@51 507 the last field separated by those delimiters.
yuuji@51 508 If the symbol 'quick is bound and is 't, when the try-completion results
yuuji@51 509 in t, exit minibuffer immediately."
yuuji@23 510 (interactive)
yuuji@51 511 (let ((md (match-data)) beg word compl
yuuji@51 512 (quick (and (boundp 'quick) (eq quick t)))
yuuji@51 513 (displist ;function to display completion-list
yuuji@51 514 (function
yuuji@51 515 (lambda ()
yuuji@51 516 (with-output-to-temp-buffer "*Completions*"
yuuji@51 517 (display-completion-list
yuuji@51 518 (all-completions word minibuffer-completion-table)))))))
yuuji@49 519 (setq beg (if (and (boundp 'delim) (stringp delim))
yuuji@23 520 (save-excursion
yuuji@23 521 (skip-chars-backward (concat "^" delim))
yuuji@49 522 (point))
yuuji@23 523 (point-min))
yuuji@23 524 word (buffer-substring beg (point-max))
yuuji@23 525 compl (try-completion word minibuffer-completion-table))
yuuji@23 526 (cond
yuuji@49 527 ((eq compl t)
yuuji@51 528 (if quick (exit-minibuffer)
yuuji@51 529 (let ((p (point)) (max (point-max)))
yuuji@51 530 (unwind-protect
yuuji@51 531 (progn
yuuji@51 532 (goto-char max)
yuuji@51 533 (insert " [Sole completion]")
yuuji@51 534 (goto-char p)
yuuji@51 535 (sit-for 1))
yuuji@51 536 (delete-region max (point-max))
yuuji@51 537 (goto-char p)))))
yuuji@23 538 ((eq compl nil)
yuuji@23 539 (ding)
yuuji@23 540 (save-excursion
yuuji@23 541 (let (p)
yuuji@51 542 (unwind-protect
yuuji@51 543 (progn
yuuji@51 544 (goto-char (setq p (point-max)))
yuuji@51 545 (insert " [No match]")
yuuji@51 546 (goto-char p)
yuuji@51 547 (sit-for 2))
yuuji@51 548 (delete-region p (point-max))))))
yuuji@23 549 ((string= compl word)
yuuji@51 550 (funcall displist))
yuuji@23 551 (t (delete-region beg (point-max))
yuuji@51 552 (insert compl)
yuuji@51 553 (if quick
yuuji@51 554 (if (eq (try-completion compl minibuffer-completion-table) t)
yuuji@51 555 (exit-minibuffer)
yuuji@51 556 (funcall displist)))))
yuuji@49 557 (store-match-data md))
yuuji@23 558 )
yuuji@23 559
yuuji@51 560 (defun YaTeX-minibuffer-quick-complete ()
yuuji@51 561 "Set 'quick to 't and call YaTeX-minibuffer-complete.
yuuji@51 562 See documentation of YaTeX-minibuffer-complete."
yuuji@51 563 (interactive)
yuuji@51 564 (let ((quick t))
yuuji@51 565 (self-insert-command 1)
yuuji@51 566 (YaTeX-minibuffer-complete)))
yuuji@51 567
yuuji@51 568 (defun foreach-buffers (pattern job)
yuuji@51 569 "For each buffer which matches with PATTERN, do JOB."
yuuji@51 570 (let ((list (buffer-list)))
yuuji@51 571 (save-excursion
yuuji@51 572 (while list
yuuji@51 573 (set-buffer (car list))
yuuji@51 574 (if (or (and (stringp pattern)
yuuji@51 575 (buffer-file-name)
yuuji@51 576 (string-match pattern (buffer-file-name)))
yuuji@51 577 (and (symbolp pattern) major-mode (eq major-mode pattern)))
yuuji@51 578 (eval job))
yuuji@51 579 (setq list (cdr list)))))
yuuji@51 580 )
yuuji@51 581
yuuji@51 582 (defun goto-buffer-window (buffer)
yuuji@51 583 "Select window which is bound to BUFFER.
yuuji@51 584 If no such window exist, switch to buffer BUFFER."
yuuji@52 585 (interactive "BGoto buffer: ")
yuuji@51 586 (if (stringp buffer)
yuuji@51 587 (setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
yuuji@51 588 (if (get-buffer buffer)
yuuji@51 589 (cond
yuuji@51 590 ((get-buffer-window buffer)
yuuji@51 591 (select-window (get-buffer-window buffer)))
yuuji@51 592 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
yuuji@51 593 (let*((win (get-buffer-window buffer t))
yuuji@51 594 (frame (window-frame win)))
yuuji@51 595 (select-frame frame)
yuuji@51 596 (raise-frame frame)
yuuji@51 597 (focus-frame frame)
yuuji@51 598 (select-window win)
yuuji@51 599 (set-mouse-position frame 0 0)
yuuji@51 600 (and (featurep 'windows) (fboundp 'win:adjust-window)
yuuji@51 601 (win:adjust-window))))
yuuji@54 602 ((and (featurep 'windows) (fboundp 'win:get-buffer-window)
yuuji@56 603 (let ((w (win:get-buffer-window buffer)))
yuuji@56 604 (and w (win:switch-window w))))
yuuji@54 605 (select-window (get-buffer-window buffer)))
yuuji@51 606 (t (switch-to-buffer buffer))))
yuuji@51 607 )
yuuji@51 608
yuuji@51 609 ;; Here starts the functions which support gmhist-vs-Emacs19 compatible
yuuji@51 610 ;; reading with history.
yuuji@51 611 ;;;###autoload
yuuji@51 612 (defun completing-read-with-history
yuuji@51 613 (prompt table &optional predicate must-match initial hsym)
yuuji@51 614 "Completing read with general history: gmhist, Emacs-19."
yuuji@51 615 (let ((minibuffer-history
yuuji@51 616 (or (symbol-value hsym)
yuuji@51 617 (and (boundp 'minibuffer-history) minibuffer-history)))
yuuji@51 618 (minibuffer-history-symbol (or hsym 'minibuffer-history)))
yuuji@51 619 (prog1
yuuji@51 620 (if (fboundp 'completing-read-with-history-in)
yuuji@51 621 (completing-read-with-history-in
yuuji@51 622 minibuffer-history-symbol prompt table predicate must-match initial)
yuuji@51 623 (completing-read prompt table predicate must-match initial))
yuuji@51 624 (if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
yuuji@51 625
yuuji@51 626 ;;;###autoload
yuuji@51 627 (defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
yuuji@51 628 "Read from minibuffer with general history: gmhist, Emacs-19."
yuuji@51 629 (cond
yuuji@51 630 (YaTeX-emacs-19
yuuji@51 631 (read-from-minibuffer prompt init map read hsym))
yuuji@51 632 (t
yuuji@51 633 (let ((minibuffer-history-symbol hsym))
yuuji@51 634 (read-from-minibuffer prompt init map read)))))
yuuji@51 635
yuuji@51 636 ;;;###autoload
yuuji@51 637 (defun read-string-with-history (prompt &optional init hsym)
yuuji@51 638 "Read string with history: gmhist(Emacs-18) and Emacs-19."
yuuji@51 639 (cond
yuuji@51 640 (YaTeX-emacs-19
yuuji@51 641 (read-from-minibuffer prompt init minibuffer-local-map nil hsym))
yuuji@51 642 ((featurep 'gmhist-mh)
yuuji@51 643 (read-with-history-in hsym prompt init))
yuuji@51 644 (t (read-string prompt init))))
yuuji@23 645
yuuji@53 646 ;;;
yuuji@53 647 ;; Interface function for windows.el
yuuji@53 648 ;;;
yuuji@53 649 ;;;###autoload
yuuji@53 650 (defun YaTeX-switch-to-window ()
yuuji@53 651 "Switch to windows.el's window decided by last pressed key."
yuuji@53 652 (interactive)
yuuji@53 653 (or (featurep 'windows) (error "Why don't you use `windows.el'?"))
yuuji@53 654 (win-switch-to-window 1 (- last-command-char win:base-key)))
yuuji@53 655
yuuji@64 656 ;;;###autoload
yuuji@64 657 (defun YaTeX-reindent (col)
yuuji@64 658 "Remove current indentation and reindento to COL column."
yuuji@64 659 (save-excursion
yuuji@64 660 (beginning-of-line)
yuuji@64 661 (skip-chars-forward " \t")
yuuji@64 662 (if (/= col (current-column))
yuuji@64 663 (progn
yuuji@64 664 (delete-region (point) (progn (beginning-of-line) (point)))
yuuji@64 665 (indent-to col))))
yuuji@64 666 (skip-chars-forward " \t" (point-end-of-line)))
yuuji@64 667
yuuji@64 668 (defun YaTeX-inner-environment (&optional quick)
yuuji@64 669 "Return current inner-most environment.
yuuji@64 670 Non-nil for optional argument QUICK restricts search bound to most
yuuji@64 671 recent sectioning command. Matching point is stored to property 'point
yuuji@64 672 of 'YaTeX-inner-environment, which can be referred by
yuuji@64 673 (get 'YaTeX-inner-environment 'point)."
yuuji@64 674 (let*((nest 0)
yuuji@64 675 (beg (YaTeX-replace-format-args
yuuji@64 676 (regexp-quote YaTeX-struct-begin)
yuuji@64 677 ;YaTeX-struct-begin ;=== TENTATIVE!! ==
yuuji@64 678 YaTeX-struct-name-regexp
yuuji@64 679 (if (eq major-mode 'yahtml-mode) "\\s *.*" "")
yuuji@64 680 ""))
yuuji@64 681 (end (YaTeX-replace-format-args
yuuji@64 682 (regexp-quote YaTeX-struct-end)
yuuji@64 683 YaTeX-struct-name-regexp "" ""))
yuuji@64 684 (begend (concat "\\(" beg "\\)\\|\\(" end "\\)"))
yuuji@64 685 bound m0
yuuji@64 686 (htmlp (eq major-mode 'yahtml-mode))
yuuji@64 687 (open
yuuji@64 688 (concat "^" (or (cdr (assq major-mode '((yahtml-mode . "<")))) "{")))
yuuji@64 689 (close
yuuji@64 690 (concat "^"
yuuji@64 691 (or (cdr(assq major-mode '((yahtml-mode . "\n\t >")))) "}"))))
yuuji@64 692 (save-excursion
yuuji@64 693 (if quick
yuuji@64 694 (setq bound
yuuji@64 695 (save-excursion
yuuji@64 696 (if htmlp
yuuji@64 697 ;;(re-search-backward YaTeX-sectioning-regexp nil 1)
yuuji@64 698 (goto-char (point-min)) ;Is this enough? 97/6/26
yuuji@64 699 (YaTeX-re-search-active-backward
yuuji@64 700 (concat YaTeX-ec-regexp
yuuji@64 701 "\\(" YaTeX-sectioning-regexp "\\)\\*?{")
yuuji@64 702 YaTeX-comment-prefix nil 1))
yuuji@64 703 (or (bobp) (end-of-line))
yuuji@64 704 (point))))
yuuji@64 705 (if (catch 'begin
yuuji@64 706 (if (and (numberp bound) (< (point) bound)) (throw 'begin nil))
yuuji@64 707 (while (YaTeX-re-search-active-backward
yuuji@64 708 begend YaTeX-comment-prefix bound t)
yuuji@64 709 (setq m0 (match-beginning 0))
yuuji@64 710 (if (looking-at end) ;;(match-beginning 2)
yuuji@64 711 (setq nest (1+ nest))
yuuji@64 712 (setq nest (1- nest)))
yuuji@64 713 (if (< nest 0)
yuuji@64 714 (progn
yuuji@64 715 (put 'YaTeX-inner-environment 'point m0)
yuuji@64 716 (goto-char m0)
yuuji@64 717 (put 'YaTeX-inner-environment 'indent (current-column))
yuuji@64 718 (throw 'begin t)))))
yuuji@64 719 (buffer-substring
yuuji@64 720 (progn (skip-chars-forward open) (1+ (point)))
yuuji@64 721 (progn (skip-chars-forward close) (point))))))
yuuji@64 722 )
yuuji@64 723
yuuji@64 724 (defun YaTeX-end-environment ()
yuuji@64 725 "Close opening environment"
yuuji@64 726 (interactive)
yuuji@64 727 (let ((env (YaTeX-inner-environment)))
yuuji@64 728 (if (not env) (error "No premature environment")
yuuji@64 729 (save-excursion
yuuji@64 730 (if (YaTeX-search-active-forward
yuuji@64 731 (YaTeX-replace-format-args YaTeX-struct-end env "" "")
yuuji@64 732 YaTeX-comment-prefix nil t)
yuuji@64 733 (if (y-or-n-p
yuuji@64 734 (concat "Environment `" env
yuuji@64 735 "' may be already closed. Force close?"))
yuuji@64 736 nil
yuuji@64 737 (error "end environment aborted."))))
yuuji@64 738 (message "") ;Erase (y or n) message.
yuuji@64 739 (YaTeX-insert-struc 'end env)
yuuji@64 740 (save-excursion
yuuji@64 741 (goto-char (or (get 'YaTeX-inner-environment 'point) (match-end 0)))
yuuji@64 742 (if (pos-visible-in-window-p)
yuuji@64 743 (sit-for (if YaTeX-dos 2 1))
yuuji@64 744 (message "Matches with %s at line %d"
yuuji@64 745 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
yuuji@64 746 (count-lines (point-min) (point)))))))
yuuji@64 747 )
yuuji@64 748
yuuji@64 749 ;;;VER2
yuuji@64 750 (defun YaTeX-insert-struc (what env)
yuuji@64 751 (cond
yuuji@64 752 ((eq what 'begin)
yuuji@64 753 (insert (YaTeX-replace-format-args
yuuji@64 754 YaTeX-struct-begin env (YaTeX-addin env))))
yuuji@64 755 ((eq what 'end)
yuuji@64 756 (insert (YaTeX-replace-format-args YaTeX-struct-end env)))
yuuji@64 757 (t nil))
yuuji@64 758 )
yuuji@64 759
yuuji@64 760 ;;; Function for menu support
yuuji@64 761 (defun YaTeX-define-menu (keymap bindlist)
yuuji@64 762 "Define KEYMAP(symbol)'s menu-bindings according to BINDLIST.
yuuji@64 763 KEYMAP should be a quoted symbol of newly allocated keymap.
yuuji@64 764 BINDLIST consists of binding list. Each element is as follows.
yuuji@64 765
yuuji@64 766 '(menusymbol DOC_String . contents)
yuuji@64 767
yuuji@64 768 CONTENTS is one of lambda-form, interactive function, or other keymap.
yuuji@64 769 See yatex19.el for example."
yuuji@64 770 (cond
yuuji@64 771 ((featurep 'xemacs)
yuuji@64 772 (let (name)
yuuji@64 773 (if (keymapp (symbol-value keymap))
yuuji@64 774 (progn
yuuji@64 775 (setq name (keymap-name (symbol-value keymap)))
yuuji@64 776 (set keymap nil))
yuuji@64 777 (setq name (car (symbol-value keymap)))
yuuji@64 778 (set keymap (cdr (symbol-value keymap))))
yuuji@64 779 (mapcar
yuuji@64 780 (function
yuuji@64 781 (lambda (bind)
yuuji@64 782 (setq bind (cdr bind))
yuuji@64 783 (if (eq (car-safe (cdr bind)) 'lambda)
yuuji@64 784 (setcar (cdr bind) 'progn))
yuuji@64 785 (if (stringp (car-safe (cdr bind)))
yuuji@64 786 (set keymap
yuuji@64 787 (cons (cdr bind) (symbol-value keymap)))
yuuji@64 788 (set keymap
yuuji@64 789 (cons (vector (car bind) (cdr bind) t)
yuuji@64 790 (symbol-value keymap))))))
yuuji@64 791 bindlist)
yuuji@64 792 (set keymap (cons name (symbol-value keymap)))))
yuuji@64 793 (t
yuuji@64 794 (mapcar
yuuji@64 795 (function
yuuji@64 796 (lambda (bind)
yuuji@64 797 (define-key (symbol-value keymap) (vector (car bind)) (cdr bind))))
yuuji@64 798 bindlist))))
yuuji@64 799
yuuji@64 800
yuuji@64 801
yuuji@58 802 (defun bcf-and-exit ()
yuuji@58 803 "Byte compile rest of argument and kill-emacs."
yuuji@58 804 (if command-line-args-left
yuuji@58 805 (progn
yuuji@58 806 (mapcar 'byte-compile-file command-line-args-left)
yuuji@58 807 (kill-emacs))))
yuuji@64 808
yuuji@58 809
yuuji@23 810 (provide 'yatexlib)