yatex

annotate yatexlib.el @ 366:77a2fd4fd634

merged
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 26 Dec 2014 01:06:32 +0900
parents d69fd7b1ac4d
children e8f95c375526
rev   line source
yuuji@287 1 ;;; yatexlib.el --- YaTeX and yahtml common libraries
yuuji@287 2 ;;;
yuuji@294 3 ;;; (c)1994-2013 by HIROSE Yuuji.[yuuji@yatex.org]
yuuji@354 4 ;;; Last modified Sun Dec 21 23:55:30 2014 on firestorm
yuuji@366 5 ;;; $Id$
yuuji@23 6
yuuji@287 7 ;;; Code:
yuuji@64 8 ;; General variables
yuuji@64 9 (defvar YaTeX-dos (memq system-type '(ms-dos windows-nt OS/2)))
yuuji@79 10 (defvar YaTeX-macos (memq system-type '(darwin)))
yuuji@64 11 (defvar YaTeX-emacs-19 (>= (string-to-int emacs-version) 19))
yuuji@64 12 (defvar YaTeX-emacs-20 (>= (string-to-int emacs-version) 20))
yuuji@72 13 (defvar YaTeX-emacs-21 (>= (string-to-int emacs-version) 21))
yuuji@64 14 (defvar YaTeX-user-completion-table
yuuji@64 15 (if YaTeX-dos "~/_yatexrc" "~/.yatexrc")
yuuji@64 16 "*Default filename in which user completion table is saved.")
yuuji@64 17
yuuji@72 18 (defvar YaTeX-display-color-p
yuuji@72 19 (or (and (fboundp 'display-color-p) (display-color-p))
yuuji@72 20 (and (fboundp 'device-class)
yuuji@72 21 (eq 'color (device-class (selected-device))))
yuuji@72 22 window-system) ; falls down lazy check..
yuuji@72 23 "Current display's capability of expressing colors.")
yuuji@72 24
yuuji@79 25 (defvar YaTeX-japan
yuuji@79 26 (or (boundp 'NEMACS)
yuuji@79 27 (boundp 'MULE)
yuuji@79 28 (and (boundp 'current-language-environment)
yuuji@79 29 (string-match "[Jj]apanese" current-language-environment)))
yuuji@64 30 "Whether yatex mode is running on Japanese environment or not.")
yuuji@64 31
yuuji@70 32 ;; autoload from yahtml.el
yuuji@70 33 (autoload 'yahtml-inner-environment-but "yahtml" "yahtml internal func." t)
yuuji@70 34
yuuji@80 35 (defvar latex-message-kanji-code 2
yuuji@80 36 "*Kanji coding system latex command types out.
yuuji@80 37 1 = Shift JIS, 2 = JIS, 3 = EUC. 4 = UTF-8")
yuuji@80 38
yuuji@64 39 (defvar YaTeX-kanji-code-alist
yuuji@64 40 (cond
yuuji@64 41 ((boundp '*junet*)
yuuji@77 42 (list '(0 . *noconv*)
yuuji@77 43 (cons
yuuji@64 44 1
yuuji@79 45 (cond
yuuji@79 46 (YaTeX-dos (if (boundp '*sjis-dos*) *sjis-dos* *sjis*dos))
yuuji@79 47 (YaTeX-macos (if (boundp '*sjis-mac*) *sjis-mac* *sjis*mac))
yuuji@79 48 (t *sjis*)))
yuuji@64 49 '(2 . *junet*) '(3 . *euc-japan*)))
yuuji@80 50 ((and YaTeX-emacs-20 (featurep 'mule))
yuuji@64 51 ;;(cdr-safe(assq 'coding-system (assoc "Japanese" language-info-alist)))
yuuji@77 52 (list '(0 . no-conversion)
yuuji@77 53 (cons
yuuji@64 54 1 (cond (YaTeX-dos 'shift_jis-dos)
yuuji@79 55 (YaTeX-macos 'shift_jis-mac)
yuuji@64 56 ((member 'shift_jis (coding-system-list)) 'shift_jis-unix)
yuuji@64 57 (t 'sjis)))
yuuji@68 58 '(2 . iso-2022-jp-unix)
yuuji@80 59 '(3 . euc-jp-unix)
yuuji@80 60 '(4 . utf-8))))
yuuji@64 61 "Kanji-code expression translation table.")
yuuji@64 62 (defvar YaTeX-inhibit-prefix-letter nil
yuuji@64 63 "*T for changing key definitions from [prefix] Letter to [prefix] C-Letter.")
yuuji@64 64
yuuji@64 65 (defvar YaTeX-no-begend-shortcut nil
yuuji@64 66 "*T for disabling shortcut of begin-type completion, [prefix] b d, etc.")
yuuji@64 67
yuuji@64 68 (defvar YaTeX-default-pop-window-height 10
yuuji@64 69 "Default typesetting buffer height.
yuuji@64 70 If integer, sets the window-height of typesetting buffer.
yuuji@64 71 If string, sets the percentage of it.
yuuji@64 72 If nil, use default pop-to-buffer.")
yuuji@64 73
yuuji@64 74 (defvar YaTeX-create-file-prefix-g nil
yuuji@64 75 "*Non-nil creates new file when [prefix] g on \\include{foo}.")
yuuji@64 76
yuuji@64 77 (defvar YaTeX-nervous t
yuuji@64 78 "*If you are nervous about maintenance of yatexrc, set this value to T.
yuuji@64 79 And you will have the local dictionary.")
yuuji@64 80
yuuji@72 81 (defvar YaTeX-use-italic-bold (string< "20" emacs-version)
yuuji@72 82 "*Non-nil tries to find italic/bold fontset.
yuuji@72 83 This variable is effective when font-lock is used.
yuuji@72 84 \it, \bf 内部での日本語が□になってしまう場合はこれをnilにして下さい。")
yuuji@72 85
yuuji@64 86 ;----------- work variables ----------------------------------------
yuuji@80 87 (defvar YaTeX-minibuffer-completion-map nil
yuuji@80 88 "Minibuffer completion key map that allows comma completion.")
yuuji@80 89 (if YaTeX-minibuffer-completion-map nil
yuuji@80 90 (setq YaTeX-minibuffer-completion-map
yuuji@80 91 (copy-keymap minibuffer-local-completion-map))
yuuji@80 92 (define-key YaTeX-minibuffer-completion-map " "
yuuji@80 93 'YaTeX-minibuffer-complete)
yuuji@80 94 (define-key YaTeX-minibuffer-completion-map "\t"
yuuji@80 95 'YaTeX-minibuffer-complete))
yuuji@80 96
yuuji@64 97 (defvar YaTeX-typesetting-mode-map nil
yuuji@69 98 "Keymap used in YaTeX typesetting buffer")
yuuji@69 99
yuuji@64 100 (if YaTeX-typesetting-mode-map nil
yuuji@64 101 (setq YaTeX-typesetting-mode-map (make-keymap))
yuuji@64 102 ;(suppress-keymap YaTeX-typesetting-mode-map t)
yuuji@64 103 (define-key YaTeX-typesetting-mode-map " " 'YaTeX-jump-error-line)
yuuji@64 104 (define-key YaTeX-typesetting-mode-map "\C-m" 'YaTeX-send-string)
yuuji@64 105 (define-key YaTeX-typesetting-mode-map "1" 'delete-other-windows)
yuuji@64 106 (define-key YaTeX-typesetting-mode-map "0" 'delete-window)
yuuji@64 107 (define-key YaTeX-typesetting-mode-map "q" 'delete-window))
yuuji@64 108
yuuji@64 109 (defvar YaTeX-parent-file nil
yuuji@64 110 "*Main LaTeX source file name used when %#! expression doesn't exist.")
yuuji@64 111 (make-variable-buffer-local 'YaTeX-parent-file)
yuuji@64 112
yuuji@64 113 ;---------- Define default key bindings on YaTeX mode map ----------
yuuji@64 114 ;;;###autoload
yuuji@64 115 (defun YaTeX-define-key (key binding &optional map)
yuuji@64 116 "Define key on YaTeX-prefix-map."
yuuji@64 117 (if YaTeX-inhibit-prefix-letter
yuuji@64 118 (let ((c (aref key 0)))
yuuji@64 119 (cond
yuuji@64 120 ((and (>= c ?a) (<= c ?z)) (aset key 0 (1+ (- c ?a))))
yuuji@64 121 ((and (>= c ?A) (<= c ?Z) (numberp YaTeX-inhibit-prefix-letter))
yuuji@64 122 (aset key 0 (1+ (- c ?A))))
yuuji@64 123 (t nil))))
yuuji@64 124 (define-key (or map YaTeX-prefix-map) key binding))
yuuji@64 125
yuuji@64 126 ;;;###autoload
yuuji@64 127 (defun YaTeX-local-table-symbol (symbol)
yuuji@64 128 "Return the lisp symbol which keeps local completion table of SYMBOL."
yuuji@64 129 (intern (concat "YaTeX$"
yuuji@64 130 default-directory
yuuji@64 131 (symbol-name symbol))))
yuuji@64 132
yuuji@64 133 ;;;###autoload
yuuji@64 134 (defun YaTeX-sync-local-table (symbol)
yuuji@64 135 "Synchronize local variable SYMBOL.
yuuji@64 136 Copy its corresponding directory dependent completion table to SYMBOL."
yuuji@64 137 (if (boundp (YaTeX-local-table-symbol symbol))
yuuji@64 138 (set symbol (symbol-value (YaTeX-local-table-symbol symbol)))))
yuuji@64 139
yuuji@64 140 (defvar YaTeX-user-table-is-read nil
yuuji@64 141 "Flag that means whether user completion table has been read or not.")
yuuji@64 142 ;;;###autoload
yuuji@64 143 (defun YaTeX-read-user-completion-table (&optional forcetoread)
yuuji@64 144 "Append user completion table of LaTeX macros"
yuuji@80 145 (interactive)
yuuji@64 146 (let*((user-table (expand-file-name YaTeX-user-completion-table))
yuuji@64 147 (local-table (expand-file-name (file-name-nondirectory user-table)))
yuuji@64 148 var localvar localbuf (curbuf (current-buffer)) sexp)
yuuji@64 149 (if YaTeX-user-table-is-read nil
yuuji@64 150 (message "Loading user completion table")
yuuji@64 151 (if (file-exists-p user-table) (load-file user-table)
yuuji@64 152 (message "Welcome to the field of YaTeX. I'm glad to see you!")))
yuuji@64 153 (setq YaTeX-user-table-is-read t)
yuuji@64 154 (cond
yuuji@64 155 ((file-exists-p local-table)
yuuji@64 156 (set-buffer (setq localbuf (find-file-noselect local-table)))
yuuji@64 157 (widen)
yuuji@64 158 (goto-char (point-min))
yuuji@80 159 (while (re-search-forward "(setq \\([^ \t\n]+\\)" nil t)
yuuji@64 160 (setq var (intern (buffer-substring
yuuji@64 161 (match-beginning 1) (match-end 1)))
yuuji@64 162 localvar (YaTeX-local-table-symbol var))
yuuji@64 163 (goto-char (match-beginning 0))
yuuji@64 164 (setq sexp (buffer-substring (point)
yuuji@64 165 (progn (forward-sexp) (point))))
yuuji@64 166 (set-buffer curbuf)
yuuji@64 167 (or (assq var (buffer-local-variables)) (make-local-variable var))
yuuji@64 168 (eval (read sexp))
yuuji@64 169 (or (and (boundp localvar)
yuuji@64 170 (symbol-value localvar)
yuuji@64 171 (not forcetoread))
yuuji@64 172 (set localvar (symbol-value var)))
yuuji@64 173 (set-buffer localbuf))
yuuji@64 174 (kill-buffer localbuf)))
yuuji@64 175 (set-buffer curbuf)))
yuuji@64 176
yuuji@64 177 ;;;###autoload
yuuji@64 178 (defun YaTeX-reload-dictionary ()
yuuji@64 179 "Reload local dictionary.
yuuji@64 180 Use this function after editing ./.yatexrc."
yuuji@64 181 (interactive)
yuuji@64 182 (let ((YaTeX-user-table-is-read nil))
yuuji@64 183 (YaTeX-read-user-completion-table t)))
yuuji@64 184
yuuji@64 185 ;;;###autoload
yuuji@64 186 (defun YaTeX-lookup-table (word type)
yuuji@64 187 "Lookup WORD in completion table whose type is TYPE.
yuuji@64 188 This function refers the symbol tmp-TYPE-table, user-TYPE-table, TYPE-table.
yuuji@64 189 Typically, TYPE is one of 'env, 'section, 'fontsize, 'singlecmd."
yuuji@64 190 (if (symbolp type) (setq type (symbol-name type)))
yuuji@64 191 (or (assoc word (symbol-value (intern (concat "tmp-" type "-table"))))
yuuji@64 192 (assoc word (symbol-value (intern (concat "user-" type "-table"))))
yuuji@64 193 (assoc word (symbol-value (intern (concat type "-table"))))))
yuuji@64 194
yuuji@64 195 ;;;###autoload
yuuji@64 196 (defun YaTeX-update-table (vallist default-table user-table local-table)
yuuji@64 197 "Update completion table if the car of VALLIST is not in current tables.
yuuji@64 198 Second argument DEFAULT-TABLE is the quoted symbol of default completion
yuuji@64 199 table, third argument USER-TABLE is user table which will be saved in
yuuji@64 200 YaTeX-user-completion-table, fourth argument LOCAL-TABLE should have the
yuuji@64 201 completion which is valid during current Emacs's session. If you
yuuji@64 202 want to make LOCAL-TABLE valid longer span (but restrict in this directory)
yuuji@64 203 create the file in current directory which has the same name with
yuuji@64 204 YaTeX-user-completion-table."
yuuji@64 205 (let ((car-v (car vallist)) key answer
yuuji@64 206 (file (file-name-nondirectory YaTeX-user-completion-table)))
yuuji@64 207 (cond
yuuji@64 208 ((assoc car-v (symbol-value default-table))
yuuji@64 209 nil) ;Nothing to do
yuuji@64 210 ((setq key (assoc car-v (symbol-value user-table)))
yuuji@64 211 (if (equal (cdr vallist) (cdr key)) nil
yuuji@64 212 ;; if association hits, but contents differ.
yuuji@64 213 (message
yuuji@64 214 "%s's attributes turned into %s" (car vallist) (cdr vallist))
yuuji@64 215 (set user-table (delq key (symbol-value user-table)))
yuuji@64 216 (set user-table (cons vallist (symbol-value user-table)))
yuuji@64 217 (YaTeX-update-dictionary
yuuji@64 218 YaTeX-user-completion-table user-table "user")))
yuuji@64 219 ((setq key (assoc car-v (symbol-value local-table)))
yuuji@64 220 (if (equal (cdr vallist) (cdr key)) nil
yuuji@64 221 (message
yuuji@64 222 "%s's attributes turned into %s" (car vallist) (cdr vallist))
yuuji@64 223 (set local-table (delq key (symbol-value local-table)))
yuuji@64 224 (set local-table (cons vallist (symbol-value local-table)))
yuuji@64 225 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
yuuji@64 226 (YaTeX-update-dictionary file local-table)))
yuuji@64 227 ;; All of above cases, there are some completion in tables.
yuuji@64 228 ;; Then update tables.
yuuji@64 229 (t
yuuji@64 230 (if (not YaTeX-nervous)
yuuji@64 231 (setq answer "u")
yuuji@64 232 (message
yuuji@64 233 (cond
yuuji@64 234 (YaTeX-japan
yuuji@68 235 "`%s'の登録先: U)ユーザ辞書 L)ローカル辞書 N)メモリ D)しない")
yuuji@64 236 (t
yuuji@64 237 "Register `%s' into: U)serDic L)ocalDic N)one D)iscard"))
yuuji@64 238 (if (> (length car-v) 23)
yuuji@68 239 (concat (substring car-v 0 10) "..." (substring car-v -9))
yuuji@64 240 car-v))
yuuji@64 241 (setq answer (char-to-string (read-char))))
yuuji@64 242 (cond
yuuji@64 243 ((string-match answer "uy")
yuuji@64 244 (set user-table (cons vallist (symbol-value user-table)))
yuuji@64 245 (YaTeX-update-dictionary YaTeX-user-completion-table user-table "user")
yuuji@64 246 )
yuuji@64 247 ((string-match answer "tl")
yuuji@64 248 (set local-table (cons vallist (symbol-value local-table)))
yuuji@64 249 (set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
yuuji@64 250 (YaTeX-update-dictionary file local-table))
yuuji@64 251 ((string-match answer "d") nil) ;discard it
yuuji@64 252 (t (set default-table
yuuji@64 253 (cons vallist (symbol-value default-table)))))))))
yuuji@64 254
yuuji@64 255 ;;;###autoload
yuuji@64 256 (defun YaTeX-cplread-with-learning
yuuji@64 257 (prom default-table user-table local-table
yuuji@64 258 &optional pred reqmatch init hsym)
yuuji@64 259 "Completing read with learning.
yuuji@64 260 Do a completing read with prompt PROM. Completion table is what
yuuji@64 261 DEFAULT-TABLE, USER-TABLE, LOCAL table are appended in reverse order.
yuuji@64 262 Note that these tables are passed by the symbol.
yuuji@64 263 Optional arguments PRED, REQMATH and INIT are passed to completing-read
yuuji@64 264 as its arguments PREDICATE, REQUIRE-MATCH and INITIAL-INPUT respectively.
yuuji@64 265 If optional 8th argument HSYM, history symbol, is passed, use it as
yuuji@64 266 history list variable."
yuuji@64 267 (YaTeX-sync-local-table local-table)
yuuji@64 268 (let*((table (append (symbol-value local-table)
yuuji@64 269 (symbol-value user-table)
yuuji@64 270 (symbol-value default-table)))
yuuji@64 271 (word (completing-read-with-history
yuuji@64 272 prom table pred reqmatch init hsym)))
yuuji@64 273 (if (and (string< "" word) (not (assoc word table)))
yuuji@64 274 (YaTeX-update-table (list word) default-table user-table local-table))
yuuji@64 275 word))
yuuji@64 276
yuuji@64 277 ;;;###autoload
yuuji@64 278 (defun YaTeX-update-dictionary (file symbol &optional type)
yuuji@64 279 (let ((local-table-buf (find-file-noselect file))
yuuji@64 280 (name (symbol-name symbol))
yuuji@64 281 (value (symbol-value symbol)))
yuuji@64 282 (save-excursion
yuuji@64 283 (message "Updating %s dictionary..." (or type "local"))
yuuji@64 284 (set-buffer local-table-buf)
yuuji@64 285 (goto-char (point-max))
yuuji@64 286 (search-backward (concat "(setq " name) nil t)
yuuji@64 287 (delete-region (point) (progn (forward-sexp) (point)))
yuuji@64 288 (delete-blank-lines)
yuuji@64 289 (insert "(setq " name " '(\n")
yuuji@353 290 (mapcar (function (lambda (s)
yuuji@353 291 (insert (format "%s\n" (prin1-to-string s)))))
yuuji@64 292 value)
yuuji@64 293 (insert "))\n\n")
yuuji@64 294 (delete-blank-lines)
yuuji@64 295 (basic-save-buffer)
yuuji@64 296 (kill-buffer local-table-buf)
yuuji@64 297 (message "Updating %s dictionary...Done" (or type "local")))))
yuuji@64 298
yuuji@64 299 ;;;###autoload
yuuji@64 300 (defun YaTeX-define-begend-key-normal (key env &optional map)
yuuji@64 301 "Define short cut YaTeX-make-begin-end key."
yuuji@64 302 (YaTeX-define-key
yuuji@64 303 key
yuuji@64 304 (list 'lambda '(arg) '(interactive "P")
yuuji@64 305 (list 'YaTeX-insert-begin-end env 'arg))
yuuji@64 306 map))
yuuji@64 307
yuuji@64 308 ;;;###autoload
yuuji@64 309 (defun YaTeX-define-begend-region-key (key env &optional map)
yuuji@64 310 "Define short cut YaTeX-make-begin-end-region key."
yuuji@64 311 (YaTeX-define-key key (list 'lambda nil '(interactive)
yuuji@64 312 (list 'YaTeX-insert-begin-end env t)) map))
yuuji@64 313
yuuji@64 314 ;;;###autoload
yuuji@64 315 (defun YaTeX-define-begend-key (key env &optional map)
yuuji@86 316 "Define short cut key for begin type completion.
yuuji@86 317 Define both strokes for normal and region mode.
yuuji@86 318 To customize YaTeX, user should use this function."
yuuji@64 319 (YaTeX-define-begend-key-normal key env map)
yuuji@64 320 (if YaTeX-inhibit-prefix-letter nil
yuuji@64 321 (YaTeX-define-begend-region-key
yuuji@64 322 (concat (upcase (substring key 0 1)) (substring key 1)) env)))
yuuji@64 323
yuuji@23 324 ;;;###autoload
yuuji@23 325 (defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func)
yuuji@23 326 "Search STRING which is not commented out by CMNTRX.
yuuji@23 327 Optional arguments after BOUND, ERR, CNT are passed literally to search-forward
yuuji@23 328 or search-backward.
yuuji@23 329 Optional sixth argument FUNC changes search-function."
yuuji@49 330 (let ((sfunc (or func 'search-forward)) found md)
yuuji@23 331 (while (and (prog1
yuuji@23 332 (setq found (funcall sfunc string bound err cnt))
yuuji@23 333 (setq md (match-data)))
yuuji@23 334 (or
yuuji@64 335 (and (eq major-mode 'yatex-mode)
yuuji@64 336 (YaTeX-in-verb-p (match-beginning 0)))
yuuji@23 337 (save-excursion
yuuji@72 338 (goto-char (match-beginning 0))
yuuji@23 339 (beginning-of-line)
yuuji@23 340 (re-search-forward cmntrx (match-beginning 0) t)))))
yuuji@23 341 (store-match-data md)
yuuji@69 342 found))
yuuji@23 343
yuuji@23 344 (defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt)
yuuji@23 345 "Search REGEXP backward which is not commented out by regexp CMNTRX.
yuuji@23 346 See also YaTeX-search-active-forward."
yuuji@69 347 (YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward))
yuuji@69 348
yuuji@23 349 (defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt)
yuuji@23 350 "Search STRING backward which is not commented out by regexp CMNTRX.
yuuji@23 351 See also YaTeX-search-active-forward."
yuuji@69 352 (YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward))
yuuji@69 353
yuuji@23 354 (defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt)
yuuji@23 355 "Search REGEXP backward which is not commented out by regexp CMNTRX.
yuuji@23 356 See also YaTeX-search-active-forward."
yuuji@69 357 (YaTeX-search-active-forward
yuuji@69 358 regexp cmntrx bound err cnt 're-search-backward))
yuuji@23 359
yuuji@80 360 (defun YaTeX-relative-path-p (path)
yuuji@80 361 "Return non-nil if PATH is not absolute one."
yuuji@80 362 (let ((md (match-data)))
yuuji@80 363 (unwind-protect
yuuji@80 364 (not (string-match "^\\(/\\|[a-z]:\\|\\\\\\).*/" file))
yuuji@80 365 (store-match-data md))))
yuuji@80 366
yuuji@23 367 ;;;###autoload
yuuji@23 368 (defun YaTeX-switch-to-buffer (file &optional setbuf)
yuuji@23 369 "Switch to buffer if buffer exists, find file if not.
yuuji@23 370 Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer."
yuuji@23 371 (interactive "Fswitch to file: ")
yuuji@70 372 (if (bufferp file)
yuuji@70 373 (setq file (buffer-file-name file))
yuuji@80 374 (and (YaTeX-relative-path-p file)
yuuji@70 375 (eq major-mode 'yatex-mode)
yuuji@70 376 YaTeX-search-file-from-top-directory
yuuji@70 377 (save-excursion
yuuji@70 378 (YaTeX-visit-main t)
yuuji@70 379 (setq file (expand-file-name file)))))
yuuji@52 380 (let (buf (hilit-auto-highlight (not setbuf)))
yuuji@52 381 (cond
yuuji@52 382 ((setq buf (get-file-buffer file))
yuuji@52 383 (funcall (if setbuf 'set-buffer 'switch-to-buffer)
yuuji@52 384 (get-file-buffer file))
yuuji@52 385 buf)
yuuji@52 386 ((or YaTeX-create-file-prefix-g (file-exists-p file))
yuuji@52 387 (or ;find-file returns nil but set current-buffer...
yuuji@52 388 (if setbuf (set-buffer (find-file-noselect file))
yuuji@52 389 (find-file file))
yuuji@52 390 (current-buffer)))
yuuji@52 391 (t (message "%s was not found in this directory." file)
yuuji@69 392 nil))))
yuuji@23 393
yuuji@23 394 ;;;###autoload
yuuji@23 395 (defun YaTeX-switch-to-buffer-other-window (file)
yuuji@23 396 "Switch to buffer if buffer exists, find file if not."
yuuji@23 397 (interactive "Fswitch to file: ")
yuuji@70 398 (and (eq major-mode 'yatex-mode)
yuuji@70 399 (stringp file)
yuuji@80 400 (YaTeX-relative-path-p file)
yuuji@70 401 YaTeX-search-file-from-top-directory
yuuji@70 402 (save-excursion
yuuji@70 403 (YaTeX-visit-main t)
yuuji@70 404 (setq file (expand-file-name file))))
yuuji@52 405 (if (bufferp file) (setq file (buffer-file-name file)))
yuuji@52 406 (cond
yuuji@52 407 ((get-file-buffer file)
yuuji@52 408 (switch-to-buffer-other-window (get-file-buffer file))
yuuji@52 409 t)
yuuji@52 410 ((or YaTeX-create-file-prefix-g (file-exists-p file))
yuuji@52 411 (find-file-other-window file) t)
yuuji@52 412 (t (message "%s was not found in this directory." file)
yuuji@69 413 nil)))
yuuji@23 414
yuuji@70 415 (defun YaTeX-get-file-buffer (file)
yuuji@70 416 "Return the FILE's buffer.
yuuji@70 417 Base directory is that of main file or current directory."
yuuji@70 418 (let (dir main (cdir default-directory))
yuuji@70 419 (or (and (eq major-mode 'yatex-mode)
yuuji@70 420 YaTeX-search-file-from-top-directory
yuuji@70 421 (save-excursion
yuuji@70 422 (YaTeX-visit-main t)
yuuji@70 423 (get-file-buffer file)))
yuuji@70 424 (get-file-buffer file))))
yuuji@70 425
yuuji@23 426 (defun YaTeX-replace-format-sub (string format repl)
yuuji@23 427 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
yuuji@23 428 (string-match (concat "[^%]\\(%" format "\\)") string)))
yuuji@23 429 (len (length format)))
yuuji@23 430 (if (null beg) string ;no conversion
yuuji@23 431 (concat
yuuji@70 432 (substring string 0 (match-beginning 1)) (or repl "")
yuuji@69 433 (substring string (match-end 1))))))
yuuji@23 434
yuuji@23 435 ;;;###autoload
yuuji@23 436 (defun YaTeX-replace-format (string format repl)
yuuji@23 437 "In STRING, replace first appearance of FORMAT to REPL as if
yuuji@23 438 function `format' does. FORMAT does not contain `%'"
yuuji@80 439 (let ((ans string) (case-fold-search nil))
yuuji@23 440 (while (not (string=
yuuji@23 441 ans (setq string (YaTeX-replace-format-sub ans format repl))))
yuuji@23 442 (setq ans string))
yuuji@69 443 string))
yuuji@23 444
yuuji@23 445 ;;;###autoload
yuuji@70 446 (defun YaTeX-replace-formats (string replace-list)
yuuji@70 447 (let ((list replace-list))
yuuji@70 448 (while list
yuuji@70 449 (setq string (YaTeX-replace-format
yuuji@70 450 string (car (car list)) (cdr (car list)))
yuuji@70 451 list (cdr list)))
yuuji@70 452 string))
yuuji@70 453
yuuji@70 454 ;;;###autoload
yuuji@23 455 (defun YaTeX-replace-format-args (string &rest args)
yuuji@23 456 "Translate the argument mark #1, #2, ... #n in the STRING into the
yuuji@23 457 corresponding real arguments ARGS."
yuuji@23 458 (let ((argp 1))
yuuji@23 459 (while args
yuuji@23 460 (setq string
yuuji@23 461 (YaTeX-replace-format string (int-to-string argp) (car args)))
yuuji@23 462 (setq args (cdr args) argp (1+ argp))))
yuuji@69 463 string)
yuuji@23 464
yuuji@23 465 ;;;###autoload
yuuji@23 466 (defun rindex (string char)
yuuji@226 467 "Return the last position of STRING where character CHAR found."
yuuji@23 468 (let ((pos (1- (length string)))(index -1))
yuuji@174 469 (catch 'rindex
yuuji@174 470 (while (>= pos 0)
yuuji@174 471 (cond
yuuji@174 472 ((= (aref string pos) char)
yuuji@174 473 (throw 'rindex pos))
yuuji@174 474 (t (setq pos (1- pos))))))))
yuuji@64 475
yuuji@64 476 ;;;###autoload
yuuji@64 477 (defun point-beginning-of-line ()
yuuji@64 478 (save-excursion (beginning-of-line)(point)))
yuuji@64 479
yuuji@64 480 ;;;###autoload
yuuji@64 481 (defun point-end-of-line ()
yuuji@64 482 (save-excursion (end-of-line)(point)))
yuuji@64 483
yuuji@354 484 (defun YaTeX-showup-buffer-bottom-most (x) (nth 3 (window-edges x)))
yuuji@23 485 ;;;###autoload
yuuji@23 486 (defun YaTeX-showup-buffer (buffer &optional func select)
yuuji@23 487 "Make BUFFER show up in certain window (but current window)
yuuji@23 488 that gives the maximum value by the FUNC. FUNC should take an argument
yuuji@23 489 of its window object. Non-nil for optional third argument SELECT selects
yuuji@49 490 that window. This function never selects minibuffer window."
yuuji@86 491 (or (and (if (and YaTeX-emacs-19 select window-system)
yuuji@47 492 (get-buffer-window buffer t)
yuuji@47 493 (get-buffer-window buffer))
yuuji@47 494 (progn
yuuji@47 495 (if select
yuuji@51 496 (goto-buffer-window buffer))
yuuji@47 497 t))
yuuji@23 498 (let ((window (selected-window))
yuuji@23 499 (wlist (YaTeX-window-list)) win w (x 0))
yuuji@23 500 (cond
yuuji@23 501 ((> (length wlist) 2)
yuuji@23 502 (if func
yuuji@23 503 (while wlist
yuuji@23 504 (setq w (car wlist))
yuuji@23 505 (if (and (not (eq window w))
yuuji@23 506 (> (funcall func w) x))
yuuji@23 507 (setq win w x (funcall func w)))
yuuji@23 508 (setq wlist (cdr wlist)))
yuuji@23 509 (setq win (get-lru-window)))
yuuji@23 510 (select-window win)
yuuji@23 511 (switch-to-buffer buffer)
yuuji@23 512 (or select (select-window window)))
yuuji@23 513 ((= (length wlist) 2)
yuuji@49 514 ;(other-window 1);This does not work properly on Emacs-19
yuuji@49 515 (select-window (get-lru-window))
yuuji@23 516 (switch-to-buffer buffer)
yuuji@354 517 (if (< (window-height) (/ YaTeX-default-pop-window-height 2))
yuuji@354 518 (enlarge-window (- YaTeX-default-pop-window-height
yuuji@354 519 (window-height))))
yuuji@23 520 (or select (select-window window)))
yuuji@23 521 (t ;if one-window
yuuji@23 522 (cond
yuuji@86 523 ((and YaTeX-emacs-19 window-system (get-buffer-window buffer t))
yuuji@47 524 nil) ;if found in other frame
yuuji@23 525 (YaTeX-default-pop-window-height
yuuji@51 526 (split-window-calculate-height YaTeX-default-pop-window-height)
yuuji@59 527 ;;(pop-to-buffer buffer) ;damn! emacs-19.30
yuuji@59 528 (select-window (next-window nil 1))
yuuji@59 529 (switch-to-buffer (get-buffer-create buffer))
yuuji@23 530 (or select (select-window window)))
yuuji@23 531 (t nil)))
yuuji@69 532 ))))
yuuji@69 533
yuuji@69 534 (cond
yuuji@69 535 ((fboundp 'screen-height)
yuuji@69 536 (fset 'YaTeX-screen-height 'screen-height)
yuuji@182 537 (fset 'YaTeX-screen-width 'screen-width)
yuuji@182 538 (fset 'YaTeX-set-screen-height 'set-screen-height)
yuuji@182 539 (fset 'YaTeX-set-screen-width 'set-screen-width))
yuuji@69 540 ((fboundp 'frame-height)
yuuji@69 541 (fset 'YaTeX-screen-height 'frame-height)
yuuji@182 542 (fset 'YaTeX-screen-width 'frame-width)
yuuji@182 543 (fset 'YaTeX-set-screen-height 'set-frame-height)
yuuji@182 544 (fset 'YaTeX-set-screen-width 'set-frame-width))
yuuji@182 545 (t (error "I don't know how to run YaTeX on this Emacs...")))
yuuji@23 546
yuuji@23 547 ;;;###autoload
yuuji@51 548 (defun split-window-calculate-height (height)
yuuji@51 549 "Split current window wight specified HEIGHT.
yuuji@59 550 If HEIGHT is number, make a new window that has HEIGHT lines.
yuuji@59 551 If HEIGHT is string, make a new window that occupies HEIGT % of screen height.
yuuji@51 552 Otherwise split window conventionally."
yuuji@59 553 (if (one-window-p t)
yuuji@51 554 (split-window
yuuji@51 555 (selected-window)
yuuji@51 556 (max
yuuji@51 557 (min
yuuji@69 558 (- (YaTeX-screen-height)
yuuji@59 559 (if (numberp height)
yuuji@59 560 (+ height 2)
yuuji@69 561 (/ (* (YaTeX-screen-height)
yuuji@59 562 (string-to-int height))
yuuji@51 563 100)))
yuuji@69 564 (- (YaTeX-screen-height) window-min-height 1))
yuuji@69 565 window-min-height))))
yuuji@51 566
yuuji@51 567 ;;;###autoload
yuuji@23 568 (defun YaTeX-window-list ()
yuuji@23 569 (let*((curw (selected-window)) (win curw) (wlist (list curw)))
yuuji@23 570 (while (not (eq curw (setq win (next-window win))))
yuuji@23 571 (or (eq win (minibuffer-window))
yuuji@23 572 (setq wlist (cons win wlist))))
yuuji@69 573 wlist))
yuuji@23 574
yuuji@72 575 (if YaTeX-emacs-21
yuuji@72 576 ;; Emacs-21's next-window returns other frame's window even if called
yuuji@72 577 ;; with argument ALL-FRAMES nil, when called from minibuffer context.
yuuji@72 578 ;; Therefore, check frame identity here.
yuuji@72 579 (defun YaTeX-window-list ()
yuuji@72 580 (let*((curw (selected-window)) (win curw) (wlist (list curw))
yuuji@72 581 (curf (window-frame curw)))
yuuji@72 582 (while (and (not (eq curw (setq win (next-window win))))
yuuji@72 583 (eq curf (window-frame win)))
yuuji@72 584 (or (eq win (minibuffer-window))
yuuji@72 585 (setq wlist (cons win wlist))))
yuuji@72 586 wlist)))
yuuji@72 587
yuuji@23 588 ;;;###autoload
yuuji@23 589 (defun substitute-all-key-definition (olddef newdef keymap)
yuuji@23 590 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
yuuji@23 591 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
yuuji@23 592 where ever it appears."
yuuji@68 593 (if YaTeX-emacs-19
yuuji@68 594 (substitute-key-definition olddef newdef keymap global-map)
yuuji@68 595 (mapcar
yuuji@68 596 (function (lambda (key) (define-key keymap key newdef)))
yuuji@68 597 (where-is-internal olddef keymap))))
yuuji@23 598
yuuji@23 599 ;;;###autoload
yuuji@23 600 (defun YaTeX-match-string (n &optional m)
yuuji@23 601 "Return (buffer-substring (match-beginning n) (match-beginning m))."
yuuji@23 602 (if (match-beginning n)
yuuji@80 603 (YaTeX-buffer-substring (match-beginning n)
yuuji@69 604 (match-end (or m n)))))
yuuji@23 605
yuuji@23 606 ;;;###autoload
yuuji@23 607 (defun YaTeX-minibuffer-complete ()
yuuji@49 608 "Complete in minibuffer.
yuuji@51 609 If the symbol 'delim is bound and is string, its value is assumed to be
yuuji@49 610 the character class of delimiters. Completion will be performed on
yuuji@51 611 the last field separated by those delimiters.
yuuji@51 612 If the symbol 'quick is bound and is 't, when the try-completion results
yuuji@51 613 in t, exit minibuffer immediately."
yuuji@23 614 (interactive)
yuuji@72 615 (save-restriction
yuuji@72 616 (narrow-to-region
yuuji@72 617 (if (fboundp 'field-beginning) (field-beginning (point-max)) (point-min))
yuuji@72 618 (point-max))
yuuji@72 619 (let ((md (match-data)) beg word compl
yuuji@72 620 (quick (and (boundp 'quick) (eq quick t)))
yuuji@72 621 (displist ;function to display completion-list
yuuji@72 622 (function
yuuji@72 623 (lambda ()
yuuji@72 624 (with-output-to-temp-buffer "*Completions*"
yuuji@72 625 (display-completion-list
yuuji@72 626 (all-completions word minibuffer-completion-table)))))))
yuuji@72 627 (setq beg (if (and (boundp 'delim) (stringp delim))
yuuji@72 628 (save-excursion
yuuji@72 629 (skip-chars-backward (concat "^" delim))
yuuji@72 630 (point))
yuuji@72 631 (point-min))
yuuji@72 632 word (buffer-substring beg (point-max))
yuuji@72 633 compl (try-completion word minibuffer-completion-table))
yuuji@72 634 (cond
yuuji@72 635 ((eq compl t)
yuuji@72 636 (if quick (exit-minibuffer)
yuuji@72 637 (let ((p (point)) (max (point-max)))
yuuji@72 638 (unwind-protect
yuuji@72 639 (progn
yuuji@72 640 (goto-char max)
yuuji@72 641 (insert " [Sole completion]")
yuuji@72 642 (goto-char p)
yuuji@72 643 (sit-for 1))
yuuji@72 644 (delete-region max (point-max))
yuuji@72 645 (goto-char p)))))
yuuji@72 646 ((eq compl nil)
yuuji@72 647 (ding)
yuuji@72 648 (save-excursion
yuuji@72 649 (let (p)
yuuji@72 650 (unwind-protect
yuuji@72 651 (progn
yuuji@72 652 (goto-char (setq p (point-max)))
yuuji@72 653 (insert " [No match]")
yuuji@72 654 (goto-char p)
yuuji@72 655 (sit-for 2))
yuuji@72 656 (delete-region p (point-max))))))
yuuji@72 657 ((string= compl word)
yuuji@72 658 (funcall displist))
yuuji@72 659 (t (delete-region beg (point-max))
yuuji@72 660 (insert compl)
yuuji@72 661 (if quick
yuuji@72 662 (if (eq (try-completion compl minibuffer-completion-table) t)
yuuji@72 663 (exit-minibuffer)
yuuji@72 664 (funcall displist)))))
yuuji@72 665 (store-match-data md))))
yuuji@23 666
yuuji@51 667 (defun YaTeX-minibuffer-quick-complete ()
yuuji@51 668 "Set 'quick to 't and call YaTeX-minibuffer-complete.
yuuji@51 669 See documentation of YaTeX-minibuffer-complete."
yuuji@51 670 (interactive)
yuuji@51 671 (let ((quick t))
yuuji@51 672 (self-insert-command 1)
yuuji@51 673 (YaTeX-minibuffer-complete)))
yuuji@51 674
yuuji@168 675 (defun YaTeX-yatex-buffer-list ()
yuuji@168 676 (save-excursion
yuuji@168 677 (delq nil (mapcar (function (lambda (buf)
yuuji@168 678 (set-buffer buf)
yuuji@168 679 (if (eq major-mode 'yatex-mode) buf)))
yuuji@168 680 (buffer-list)))))
yuuji@168 681
yuuji@51 682 (defun foreach-buffers (pattern job)
yuuji@51 683 "For each buffer which matches with PATTERN, do JOB."
yuuji@51 684 (let ((list (buffer-list)))
yuuji@51 685 (save-excursion
yuuji@51 686 (while list
yuuji@51 687 (set-buffer (car list))
yuuji@51 688 (if (or (and (stringp pattern)
yuuji@51 689 (buffer-file-name)
yuuji@51 690 (string-match pattern (buffer-file-name)))
yuuji@51 691 (and (symbolp pattern) major-mode (eq major-mode pattern)))
yuuji@51 692 (eval job))
yuuji@69 693 (setq list (cdr list))))))
yuuji@51 694
yuuji@51 695 (defun goto-buffer-window (buffer)
yuuji@51 696 "Select window which is bound to BUFFER.
yuuji@51 697 If no such window exist, switch to buffer BUFFER."
yuuji@52 698 (interactive "BGoto buffer: ")
yuuji@51 699 (if (stringp buffer)
yuuji@51 700 (setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
yuuji@51 701 (if (get-buffer buffer)
yuuji@51 702 (cond
yuuji@51 703 ((get-buffer-window buffer)
yuuji@51 704 (select-window (get-buffer-window buffer)))
yuuji@51 705 ((and YaTeX-emacs-19 (get-buffer-window buffer t))
yuuji@51 706 (let*((win (get-buffer-window buffer t))
yuuji@51 707 (frame (window-frame win)))
yuuji@51 708 (select-frame frame)
yuuji@51 709 (raise-frame frame)
yuuji@51 710 (focus-frame frame)
yuuji@51 711 (select-window win)
yuuji@51 712 (set-mouse-position frame 0 0)
yuuji@51 713 (and (featurep 'windows) (fboundp 'win:adjust-window)
yuuji@51 714 (win:adjust-window))))
yuuji@54 715 ((and (featurep 'windows) (fboundp 'win:get-buffer-window)
yuuji@56 716 (let ((w (win:get-buffer-window buffer)))
yuuji@56 717 (and w (win:switch-window w))))
yuuji@54 718 (select-window (get-buffer-window buffer)))
yuuji@69 719 (t (switch-to-buffer buffer)))))
yuuji@51 720
yuuji@51 721 ;; Here starts the functions which support gmhist-vs-Emacs19 compatible
yuuji@51 722 ;; reading with history.
yuuji@51 723 ;;;###autoload
yuuji@51 724 (defun completing-read-with-history
yuuji@51 725 (prompt table &optional predicate must-match initial hsym)
yuuji@51 726 "Completing read with general history: gmhist, Emacs-19."
yuuji@51 727 (let ((minibuffer-history
yuuji@51 728 (or (symbol-value hsym)
yuuji@51 729 (and (boundp 'minibuffer-history) minibuffer-history)))
yuuji@51 730 (minibuffer-history-symbol (or hsym 'minibuffer-history)))
yuuji@51 731 (prog1
yuuji@51 732 (if (fboundp 'completing-read-with-history-in)
yuuji@51 733 (completing-read-with-history-in
yuuji@51 734 minibuffer-history-symbol prompt table predicate must-match initial)
yuuji@51 735 (completing-read prompt table predicate must-match initial))
yuuji@51 736 (if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
yuuji@51 737
yuuji@51 738 ;;;###autoload
yuuji@51 739 (defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
yuuji@51 740 "Read from minibuffer with general history: gmhist, Emacs-19."
yuuji@51 741 (cond
yuuji@51 742 (YaTeX-emacs-19
yuuji@51 743 (read-from-minibuffer prompt init map read hsym))
yuuji@51 744 (t
yuuji@51 745 (let ((minibuffer-history-symbol hsym))
yuuji@51 746 (read-from-minibuffer prompt init map read)))))
yuuji@51 747
yuuji@51 748 ;;;###autoload
yuuji@51 749 (defun read-string-with-history (prompt &optional init hsym)
yuuji@51 750 "Read string with history: gmhist(Emacs-18) and Emacs-19."
yuuji@51 751 (cond
yuuji@51 752 (YaTeX-emacs-19
yuuji@51 753 (read-from-minibuffer prompt init minibuffer-local-map nil hsym))
yuuji@51 754 ((featurep 'gmhist-mh)
yuuji@51 755 (read-with-history-in hsym prompt init))
yuuji@51 756 (t (read-string prompt init))))
yuuji@23 757
yuuji@234 758 (defvar YaTeX-skip-next-reader-char ?\C-j)
yuuji@237 759 (defun YaTeX-read-string-or-skip (&rest args)
yuuji@237 760 "Read string, or skip if last input char is \C-j."
yuuji@293 761 (if (equal (if (boundp 'last-input-event) last-input-event last-input-char)
yuuji@293 762 YaTeX-skip-next-reader-char)
yuuji@237 763 ""
yuuji@237 764 (apply 'read-string args)))
yuuji@234 765
yuuji@244 766 (defun YaTeX-completing-read-or-skip (&rest args)
yuuji@244 767 "Do completing-read, or skip if last input char is \C-j."
yuuji@293 768 (if (equal (if (boundp 'last-input-event) last-input-event last-input-char)
yuuji@293 769 YaTeX-skip-next-reader-char)
yuuji@244 770 ""
yuuji@244 771 (apply 'completing-read args)))
yuuji@244 772
yuuji@69 773 ;;;###autoload
yuuji@69 774 (fset 'YaTeX-rassoc
yuuji@69 775 (if (and nil (fboundp 'rassoc) (subrp (symbol-function 'rassoc)))
yuuji@69 776 (symbol-function 'rassoc)
yuuji@70 777 (function
yuuji@70 778 (lambda (key list)
yuuji@70 779 (let ((l list))
yuuji@70 780 (catch 'found
yuuji@70 781 (while l
yuuji@70 782 (if (equal key (cdr (car l)))
yuuji@70 783 (throw 'found (car l)))
yuuji@70 784 (setq l (cdr l)))))))))
yuuji@70 785
yuuji@354 786 (defun YaTeX-set-file-coding-system (code coding)
yuuji@354 787 "Set current buffer's coding system according to symbol."
yuuji@354 788 (cond ((null code)
yuuji@354 789 nil)
yuuji@354 790 ((boundp 'MULE)
yuuji@354 791 (set-file-coding-system coding))
yuuji@354 792 ((and YaTeX-emacs-20 (boundp 'buffer-file-coding-system))
yuuji@354 793 (setq buffer-file-coding-system
yuuji@354 794 (or (and (fboundp 'set-auto-coding) buffer-file-name
yuuji@354 795 (save-excursion
yuuji@354 796 (goto-char (point-min))
yuuji@354 797 (set-auto-coding buffer-file-name (buffer-size))))
yuuji@354 798 coding)))
yuuji@354 799 ((featurep 'mule)
yuuji@354 800 (set-file-coding-system coding))
yuuji@354 801 ((boundp 'NEMACS)
yuuji@354 802 (make-local-variable 'kanji-fileio-code)
yuuji@354 803 (setq kanji-fileio-code code))))
yuuji@354 804
yuuji@70 805 (defun YaTeX-insert-file-contents (file visit &optional beg end)
yuuji@70 806 (cond
yuuji@72 807 ((and (string< "19" emacs-version) (not (featurep 'xemacs)))
yuuji@70 808 (insert-file-contents file visit beg end))
yuuji@77 809 ((string-match "unix\\|linux" (symbol-name system-type))
yuuji@70 810 (let ((default-process-coding-system
yuuji@72 811 (and (boundp '*noconv*) (list '*noconv*)))
yuuji@72 812 (file-coding-system (and (boundp '*noconv*) '*noconv*))
yuuji@70 813 kanji-fileio-code
yuuji@70 814 (default-process-kanji-code 0))
yuuji@70 815 (call-process shell-file-name file (current-buffer) nil
yuuji@70 816 (or (and (boundp 'shell-command-option)
yuuji@70 817 shell-command-option)
yuuji@70 818 "-c")
yuuji@77 819 (format "dd bs=1 count=%d | tail -c +%d" end beg))))
yuuji@70 820 (t (insert-file-contents file))))
yuuji@70 821
yuuji@70 822 (defun YaTeX-split-string (str &optional sep null)
yuuji@70 823 "Split string STR by every occurrence of SEP(regexp).
yuuji@70 824 If the optional second argument SEP is nil, it defaults to \"[ \f\t\n\r\v]+\".
yuuji@70 825 Do not include null string by default. Non-nil for optional third argument
yuuji@70 826 NULL includes null string in a list."
yuuji@70 827 (let ((sep (or sep "[ \f\t\n\r\v]+"))
yuuji@70 828 list m)
yuuji@70 829 (while str
yuuji@70 830 (if (setq m (string-match sep str))
yuuji@70 831 (progn
yuuji@70 832 (if (or (> m 0) null)
yuuji@70 833 (setq list (cons (substring str 0 m) list)))
yuuji@70 834 (setq str (substring str (match-end 0))))
yuuji@70 835 (if (or null (string< "" str))
yuuji@70 836 (setq list (cons str list)))
yuuji@70 837 (setq str nil)))
yuuji@70 838 (nreverse list)))
yuuji@69 839
yuuji@73 840 ;;;###autoload
yuuji@73 841 (defun YaTeX-delete1 (elt list)
yuuji@73 842 "Delete"
yuuji@73 843 (let (e)
yuuji@73 844 (while (setq e (YaTeX-member elt list))
yuuji@73 845 (setq list (delq (car e) list))))
yuuji@73 846 list)
yuuji@73 847 (if (fboundp 'delete)
yuuji@73 848 (fset 'YaTeX-delete (symbol-function 'delete))
yuuji@73 849 (fset 'YaTeX-delete (symbol-function 'YaTeX-delete1)))
yuuji@73 850
yuuji@73 851 (defun YaTeX-member1 (elt list)
yuuji@73 852 (catch 'found
yuuji@73 853 (while list
yuuji@73 854 (if (equal elt (car list))
yuuji@73 855 (throw 'found list))
yuuji@73 856 (setq list (cdr list)))))
yuuji@73 857
yuuji@73 858 (if (and (fboundp 'member) (subrp (symbol-function 'member)))
yuuji@73 859 (fset 'YaTeX-member (symbol-function 'member))
yuuji@73 860 (fset 'YaTeX-member (symbol-function 'YaTeX-member1)))
yuuji@73 861
yuuji@53 862 ;;;
yuuji@53 863 ;; Interface function for windows.el
yuuji@53 864 ;;;
yuuji@53 865 ;;;###autoload
yuuji@290 866 (fset 'YaTeX-last-key
yuuji@290 867 (if (fboundp 'win:last-key)
yuuji@290 868 'win:last-key
yuuji@353 869 (function (lambda () (if (boundp 'last-command-char)
yuuji@353 870 last-command-char
yuuji@353 871 last-command-event)))))
yuuji@53 872 (defun YaTeX-switch-to-window ()
yuuji@53 873 "Switch to windows.el's window decided by last pressed key."
yuuji@53 874 (interactive)
yuuji@53 875 (or (featurep 'windows) (error "Why don't you use `windows.el'?"))
yuuji@290 876 (win-switch-to-window 1 (- (YaTeX-last-key) win:base-key)))
yuuji@290 877
yuuji@53 878
yuuji@64 879 ;;;###autoload
yuuji@138 880 (defun YaTeX-command-to-string (cmd)
yuuji@138 881 (if (fboundp 'shell-command-to-string)
yuuji@138 882 (funcall 'shell-command-to-string cmd)
yuuji@138 883 (let ((tbuf " *tmpout*"))
yuuji@138 884 (if (get-buffer-create tbuf) (kill-buffer tbuf))
yuuji@138 885 (let ((standard-output (get-buffer-create tbuf)))
yuuji@138 886 (unwind-protect
yuuji@138 887 (save-excursion
yuuji@138 888 (call-process
yuuji@138 889 shell-file-name nil tbuf nil YaTeX-shell-command-option cmd)
yuuji@138 890 (set-buffer tbuf)
yuuji@138 891 (buffer-string))
yuuji@138 892 (kill-buffer tbuf))))))
yuuji@138 893
yuuji@138 894 ;;;###autoload
yuuji@64 895 (defun YaTeX-reindent (col)
yuuji@64 896 "Remove current indentation and reindento to COL column."
yuuji@64 897 (save-excursion
yuuji@64 898 (beginning-of-line)
yuuji@64 899 (skip-chars-forward " \t")
yuuji@64 900 (if (/= col (current-column))
yuuji@64 901 (progn
yuuji@64 902 (delete-region (point) (progn (beginning-of-line) (point)))
yuuji@64 903 (indent-to col))))
yuuji@64 904 (skip-chars-forward " \t" (point-end-of-line)))
yuuji@64 905
yuuji@64 906 (defun YaTeX-inner-environment (&optional quick)
yuuji@64 907 "Return current inner-most environment.
yuuji@64 908 Non-nil for optional argument QUICK restricts search bound to most
yuuji@64 909 recent sectioning command. Matching point is stored to property 'point
yuuji@64 910 of 'YaTeX-inner-environment, which can be referred by
yuuji@64 911 (get 'YaTeX-inner-environment 'point)."
yuuji@73 912 (put 'YaTeX-inner-environment 'point (point-min))
yuuji@73 913 (put 'YaTeX-inner-environment 'indent 0)
yuuji@64 914 (let*((nest 0)
yuuji@64 915 (beg (YaTeX-replace-format-args
yuuji@64 916 (regexp-quote YaTeX-struct-begin)
yuuji@64 917 ;YaTeX-struct-begin ;=== TENTATIVE!! ==
yuuji@64 918 YaTeX-struct-name-regexp
yuuji@64 919 (if (eq major-mode 'yahtml-mode) "\\s *.*" "")
yuuji@64 920 ""))
yuuji@64 921 (end (YaTeX-replace-format-args
yuuji@64 922 (regexp-quote YaTeX-struct-end)
yuuji@64 923 YaTeX-struct-name-regexp "" ""))
yuuji@64 924 (begend (concat "\\(" beg "\\)\\|\\(" end "\\)"))
yuuji@64 925 bound m0
yuuji@64 926 (htmlp (eq major-mode 'yahtml-mode))
yuuji@64 927 (open
yuuji@64 928 (concat "^" (or (cdr (assq major-mode '((yahtml-mode . "<")))) "{")))
yuuji@64 929 (close
yuuji@64 930 (concat "^"
yuuji@64 931 (or (cdr(assq major-mode '((yahtml-mode . "\n\t >")))) "}"))))
yuuji@64 932 (save-excursion
yuuji@64 933 (if quick
yuuji@64 934 (setq bound
yuuji@64 935 (save-excursion
yuuji@64 936 (if htmlp
yuuji@64 937 ;;(re-search-backward YaTeX-sectioning-regexp nil 1)
yuuji@73 938 ;;(goto-char (point-min)) ;Is this enough? 97/6/26
yuuji@73 939 (re-search-backward yahtml-indentation-boundary nil 1)
yuuji@64 940 (YaTeX-re-search-active-backward
yuuji@64 941 (concat YaTeX-ec-regexp
yuuji@64 942 "\\(" YaTeX-sectioning-regexp "\\)\\*?{")
yuuji@64 943 YaTeX-comment-prefix nil 1))
yuuji@64 944 (or (bobp) (end-of-line))
yuuji@64 945 (point))))
yuuji@64 946 (if (catch 'begin
yuuji@64 947 (if (and (numberp bound) (< (point) bound)) (throw 'begin nil))
yuuji@64 948 (while (YaTeX-re-search-active-backward
yuuji@64 949 begend YaTeX-comment-prefix bound t)
yuuji@64 950 (setq m0 (match-beginning 0))
yuuji@64 951 (if (looking-at end) ;;(match-beginning 2)
yuuji@64 952 (setq nest (1+ nest))
yuuji@64 953 (setq nest (1- nest)))
yuuji@64 954 (if (< nest 0)
yuuji@64 955 (progn
yuuji@64 956 (put 'YaTeX-inner-environment 'point m0)
yuuji@64 957 (goto-char m0)
yuuji@64 958 (put 'YaTeX-inner-environment 'indent (current-column))
yuuji@64 959 (throw 'begin t)))))
yuuji@187 960 (YaTeX-buffer-substring
yuuji@64 961 (progn (skip-chars-forward open) (1+ (point)))
yuuji@69 962 (progn (skip-chars-forward close) (point)))))))
yuuji@64 963
yuuji@73 964 (defun YaTeX-goto-corresponding-environment (&optional allow-mismatch noerr)
yuuji@73 965 "Go to corresponding begin/end enclosure.
yuuji@73 966 Optional argument ALLOW-MISMATCH allows mismatch open/clese. Use this
yuuji@73 967 for \left(, \right).
yuuji@73 968 Optional third argument NOERR causes no error for unballanced environment."
yuuji@73 969 (interactive)
yuuji@73 970 (if (not (YaTeX-on-begin-end-p)) nil
yuuji@73 971 (let ((p (match-end 0)) b0 b1 env (nest 0) regexp re-s (op (point))
yuuji@73 972 (m0 (match-beginning 0)) ;whole matching
yuuji@73 973 (m1 (match-beginning 1)) ;environment in \begin{}
yuuji@73 974 (m2 (match-beginning 2)) ;environment in \end{}
yuuji@73 975 (m3 (match-beginning 3))) ;environment in \[ \] \( \)
yuuji@73 976 ;(setq env (regexp-quote (buffer-substring p (match-beginning 0))))
yuuji@73 977 (if (cond
yuuji@73 978 (m1 ;if begin{xxx}
yuuji@73 979 (setq env
yuuji@73 980 (if allow-mismatch YaTeX-struct-name-regexp
yuuji@73 981 (regexp-quote (buffer-substring m1 (match-end 1)))))
yuuji@73 982 ; (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
yuuji@73 983 ; "\\(\\\\begin{" env "}\\)"))
yuuji@73 984 (setq regexp
yuuji@73 985 (concat
yuuji@73 986 "\\("
yuuji@73 987 (YaTeX-replace-format-args
yuuji@73 988 (regexp-quote YaTeX-struct-end) env "" "")
yuuji@73 989 "\\)\\|\\("
yuuji@73 990 (YaTeX-replace-format-args
yuuji@73 991 (regexp-quote YaTeX-struct-begin) env "" "")
yuuji@73 992 "\\)"))
yuuji@73 993 (setq re-s 're-search-forward))
yuuji@73 994 (m2 ;if end{xxx}
yuuji@73 995 (setq env
yuuji@73 996 (if allow-mismatch YaTeX-struct-name-regexp
yuuji@73 997 (regexp-quote (buffer-substring m2 (match-end 2)))))
yuuji@73 998 ; (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
yuuji@73 999 ; "\\(\\\\end{" env "}\\)"))
yuuji@73 1000 (setq regexp
yuuji@73 1001 (concat
yuuji@73 1002 "\\("
yuuji@73 1003 (YaTeX-replace-format-args
yuuji@73 1004 (regexp-quote YaTeX-struct-begin) env "" "")
yuuji@73 1005 "\\)\\|\\("
yuuji@73 1006 (YaTeX-replace-format-args
yuuji@73 1007 (regexp-quote YaTeX-struct-end) env "" "")
yuuji@73 1008 "\\)"))
yuuji@73 1009 (setq re-s 're-search-backward))
yuuji@73 1010 (m3 ;math environment
yuuji@73 1011 (setq env (char-after (1+ m3))
yuuji@73 1012 regexp (format "\\(%s%s\\)\\|\\(%s%s\\)"
yuuji@73 1013 YaTeX-ec-regexp
yuuji@73 1014 (regexp-quote
yuuji@73 1015 (cdr (assq env '((?( . ")") (?) . "(")
yuuji@73 1016 (?[ . "]") (?] . "[")))))
yuuji@73 1017 YaTeX-ec-regexp
yuuji@73 1018 (regexp-quote (char-to-string env)))
yuuji@73 1019 re-s (if (memq env '(?\( ?\[))
yuuji@73 1020 're-search-forward
yuuji@73 1021 're-search-backward)))
yuuji@73 1022 (t (if noerr nil (error "Corresponding environment not found."))))
yuuji@73 1023 (progn
yuuji@73 1024 (while (and (>= nest 0) (funcall re-s regexp nil t))
yuuji@73 1025 (setq b0 (match-beginning 0) b1 (match-beginning 1))
yuuji@73 1026 (if (or (equal b0 m0)
yuuji@73 1027 (YaTeX-literal-p b0))
yuuji@73 1028 nil
yuuji@73 1029 (setq nest (if (equal b0 b1)
yuuji@73 1030 (1- nest) (1+ nest)))))
yuuji@73 1031 (if (< nest 0)
yuuji@73 1032 (goto-char (match-beginning 0)) ;found.
yuuji@73 1033 (goto-char op)
yuuji@73 1034 (funcall
yuuji@73 1035 (if noerr 'message 'error)
yuuji@73 1036 "Corresponding environment `%s' not found." env)
yuuji@73 1037 (sit-for 1)
yuuji@73 1038 nil))))))
yuuji@73 1039
yuuji@64 1040 (defun YaTeX-end-environment ()
yuuji@64 1041 "Close opening environment"
yuuji@64 1042 (interactive)
yuuji@64 1043 (let ((env (YaTeX-inner-environment)))
yuuji@64 1044 (if (not env) (error "No premature environment")
yuuji@64 1045 (save-excursion
yuuji@187 1046 (if (and
yuuji@187 1047 (YaTeX-re-search-active-forward
yuuji@187 1048 (concat
yuuji@187 1049 "\\(" (YaTeX-replace-format-args
yuuji@187 1050 YaTeX-struct-end env "" "")
yuuji@187 1051 "\\)\\|\\(" (YaTeX-replace-format-args
yuuji@187 1052 YaTeX-struct-begin env "" "")
yuuji@187 1053 "\\)")
yuuji@187 1054 YaTeX-comment-prefix nil t)
yuuji@187 1055 (match-beginning 1)) ;is closing struc.
yuuji@64 1056 (if (y-or-n-p
yuuji@64 1057 (concat "Environment `" env
yuuji@64 1058 "' may be already closed. Force close?"))
yuuji@64 1059 nil
yuuji@64 1060 (error "end environment aborted."))))
yuuji@64 1061 (message "") ;Erase (y or n) message.
yuuji@64 1062 (YaTeX-insert-struc 'end env)
yuuji@64 1063 (save-excursion
yuuji@64 1064 (goto-char (or (get 'YaTeX-inner-environment 'point) (match-end 0)))
yuuji@64 1065 (if (pos-visible-in-window-p)
yuuji@64 1066 (sit-for (if YaTeX-dos 2 1))
yuuji@64 1067 (message "Matches with %s at line %d"
yuuji@64 1068 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
yuuji@69 1069 (count-lines (point-min) (point))))))))
yuuji@64 1070
yuuji@70 1071 (defun YaTeX-beginning-of-environment (&optional limit-search-bound end)
yuuji@70 1072 "Goto the beginning of the current environment.
yuuji@70 1073 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound to
yuuji@70 1074 most recent sectioning command. Non-nil for optional third argument END
yuuji@70 1075 goes to end of environment."
yuuji@70 1076 (interactive)
yuuji@70 1077 (let ((op (point)))
yuuji@70 1078 (if (YaTeX-inner-environment limit-search-bound)
yuuji@70 1079 (progn
yuuji@70 1080 (goto-char (get 'YaTeX-inner-environment 'point))
yuuji@70 1081 (and end (YaTeX-goto-corresponding-environment))
yuuji@70 1082 (if (interactive-p) (push-mark op))
yuuji@80 1083 (point)))))
yuuji@70 1084
yuuji@70 1085 (defun YaTeX-end-of-environment (&optional limit-search-bound)
yuuji@70 1086 "Goto the end of the current environment.
yuuji@70 1087 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound
yuuji@70 1088 to most recent sectioning command."
yuuji@70 1089 (interactive)
yuuji@70 1090 (YaTeX-beginning-of-environment limit-search-bound t))
yuuji@70 1091
yuuji@70 1092 (defun YaTeX-mark-environment ()
yuuji@70 1093 "Mark current position and move point to end of environment."
yuuji@70 1094 (interactive)
yuuji@130 1095 (require 'yatexmth)
yuuji@70 1096 (let ((curp (point)))
yuuji@130 1097 (if (YaTeX-in-math-mode-p)
yuuji@130 1098 (YaTeX-mark-mathenv)
yuuji@130 1099 (if (and (YaTeX-on-begin-end-p) (match-beginning 1)) ;if on \\begin
yuuji@130 1100 (progn (goto-char (match-end 0)))
yuuji@130 1101 (if (= (char-after (point)) ?\\) nil ;if on \\end
yuuji@130 1102 (skip-chars-backward "^\n\\\\")
yuuji@130 1103 (or (bolp) (forward-char -1))))
yuuji@130 1104 (if (not (YaTeX-end-of-environment)) ;arg1 turns to match-beginning 1
yuuji@70 1105 (progn
yuuji@130 1106 (goto-char curp)
yuuji@130 1107 (error "Cannot found the end of current environment."))
yuuji@130 1108 (YaTeX-goto-corresponding-environment)
yuuji@130 1109 (beginning-of-line) ;for confirmation
yuuji@130 1110 (if (< curp (point))
yuuji@130 1111 (progn
yuuji@130 1112 (message "Mark this environment?(y or n): ")
yuuji@130 1113 (if (= (read-char) ?y) nil
yuuji@130 1114 (goto-char curp)
yuuji@130 1115 (error "Abort. Please call again at more proper position."))))
yuuji@130 1116 (set-mark-command nil)
yuuji@130 1117 (YaTeX-goto-corresponding-environment)
yuuji@130 1118 (end-of-line)
yuuji@130 1119 (if (eobp) nil (forward-char 1))))))
yuuji@70 1120
yuuji@72 1121 (defun YaTeX-kill-buffer (buffer)
yuuji@72 1122 "Make effort to show parent buffer after kill."
yuuji@72 1123 (interactive "bKill buffer: ")
yuuji@72 1124 (or (get-buffer buffer)
yuuji@72 1125 (error "No such buffer %s" buffer))
yuuji@72 1126 (let ((pf YaTeX-parent-file))
yuuji@72 1127 (kill-buffer buffer)
yuuji@72 1128 (and pf
yuuji@72 1129 (get-file-buffer pf)
yuuji@72 1130 (switch-to-buffer (get-file-buffer pf)))))
yuuji@70 1131
yuuji@225 1132 (defun YaTeX-getset-builtin (key &optional value)
yuuji@142 1133 "Read source built-in command of %# usage."
yuuji@142 1134 (catch 'builtin
yuuji@225 1135 (let*((bl (delq nil (list (current-buffer)
yuuji@142 1136 (and YaTeX-parent-file
yuuji@142 1137 (get-file-buffer YaTeX-parent-file)))))
yuuji@225 1138 (tuple (cdr (assq major-mode
yuuji@225 1139 '((yatex-mode "%#" . "\n")
yuuji@225 1140 (yahtml-mode "<!-- #" . "[ \t]*-->\\|\n")))))
yuuji@225 1141 (leader (or (car tuple) ""))
yuuji@225 1142 (closer (or (cdr tuple) ""))
yuuji@225 1143 (prompt (format "Built-in for %s: " key)))
yuuji@142 1144 (save-excursion
yuuji@142 1145 (while bl
yuuji@142 1146 (set-buffer (car bl))
yuuji@142 1147 (save-excursion
yuuji@142 1148 (goto-char (point-min))
yuuji@142 1149 (if (and (re-search-forward
yuuji@142 1150 (concat "^" (regexp-quote (concat leader key))) nil t)
yuuji@142 1151 (not (eolp)))
yuuji@142 1152 (throw 'builtin
yuuji@225 1153 (let (b e w)
yuuji@225 1154 (skip-chars-forward " \t" (point-end-of-line))
yuuji@225 1155 (setq b (point)
yuuji@225 1156 e (if (re-search-forward closer nil t)
yuuji@225 1157 (match-beginning 0)
yuuji@225 1158 (point-end-of-line))
yuuji@225 1159 w (YaTeX-buffer-substring b e))
yuuji@225 1160 (if (null value)
yuuji@225 1161 w
yuuji@225 1162 (delete-region b e)
yuuji@225 1163 (goto-char b)
yuuji@225 1164 (if (symbolp value)
yuuji@225 1165 (setq value (read-string prompt w)))
yuuji@225 1166 (insert value)
yuuji@225 1167 value)))))
yuuji@225 1168 (setq bl (cdr bl)))
yuuji@225 1169 ; not found
yuuji@225 1170 (if (null value)
yuuji@225 1171 nil ;not set mode, return simply nil
yuuji@225 1172 (if (symbolp value)
yuuji@225 1173 (setq value (read-string prompt)))
yuuji@225 1174 (save-excursion
yuuji@225 1175 (goto-char (point-min))
yuuji@225 1176 (insert leader key " " value "\n")
yuuji@225 1177 value)))))) ;on set mode, return set value
yuuji@225 1178
yuuji@225 1179 (defun YaTeX-get-builtin (key)
yuuji@225 1180 "Read source built-in command of %# usage."
yuuji@225 1181 (YaTeX-getset-builtin key))
yuuji@142 1182
yuuji@64 1183 ;;;VER2
yuuji@64 1184 (defun YaTeX-insert-struc (what env)
yuuji@64 1185 (cond
yuuji@64 1186 ((eq what 'begin)
yuuji@64 1187 (insert (YaTeX-replace-format-args
yuuji@64 1188 YaTeX-struct-begin env (YaTeX-addin env))))
yuuji@64 1189 ((eq what 'end)
yuuji@64 1190 (insert (YaTeX-replace-format-args YaTeX-struct-end env)))
yuuji@69 1191 (t nil)))
yuuji@64 1192
yuuji@80 1193 (defun YaTeX-string-width (str)
yuuji@80 1194 "Return the display width of string."
yuuji@80 1195 (if (fboundp 'string-width)
yuuji@80 1196 (string-width str)
yuuji@80 1197 (length str)))
yuuji@80 1198 (defun YaTeX-truncate-string-width (str width)
yuuji@80 1199 (cond
yuuji@80 1200 ((fboundp 'truncate-string-to-width) (truncate-string-to-width str width))
yuuji@80 1201 ((fboundp 'truncate-string) (truncate-string str width))
yuuji@80 1202 (t (substring str 0 width))))
yuuji@80 1203
yuuji@142 1204 (defun YaTeX-hex (str)
yuuji@142 1205 "Return int expressed by hexadecimal string STR."
yuuji@142 1206 (if (string< "20" emacs-version)
yuuji@142 1207 (string-to-number str 16)
yuuji@142 1208 (let ((md (match-data)))
yuuji@142 1209 (unwind-protect
yuuji@142 1210 (if (string-match "[^0-9a-f]" str)
yuuji@142 1211 (error "Non hexadecimal character in %s" str)
yuuji@142 1212 (let ((i 0) d)
yuuji@142 1213 (setq str (downcase str))
yuuji@142 1214 (while (string< "" str)
yuuji@142 1215 (setq d (+ 0 (string-to-char str)) ; + 0 for XEmacs
yuuji@142 1216 i (+ (* 16 i) (- d (if (<= d ?9) ?0 (- ?a 10))))
yuuji@142 1217 str (substring str 1)))
yuuji@142 1218 i))
yuuji@142 1219 (store-match-data md)))))
yuuji@142 1220
yuuji@142 1221
yuuji@64 1222 ;;; Function for menu support
yuuji@64 1223 (defun YaTeX-define-menu (keymap bindlist)
yuuji@64 1224 "Define KEYMAP(symbol)'s menu-bindings according to BINDLIST.
yuuji@64 1225 KEYMAP should be a quoted symbol of newly allocated keymap.
yuuji@64 1226 BINDLIST consists of binding list. Each element is as follows.
yuuji@64 1227
yuuji@64 1228 '(menusymbol DOC_String . contents)
yuuji@64 1229
yuuji@64 1230 CONTENTS is one of lambda-form, interactive function, or other keymap.
yuuji@64 1231 See yatex19.el for example."
yuuji@64 1232 (cond
yuuji@64 1233 ((featurep 'xemacs)
yuuji@64 1234 (let (name)
yuuji@64 1235 (if (keymapp (symbol-value keymap))
yuuji@64 1236 (progn
yuuji@64 1237 (setq name (keymap-name (symbol-value keymap)))
yuuji@64 1238 (set keymap nil))
yuuji@64 1239 (setq name (car (symbol-value keymap)))
yuuji@64 1240 (set keymap (cdr (symbol-value keymap))))
yuuji@64 1241 (mapcar
yuuji@64 1242 (function
yuuji@64 1243 (lambda (bind)
yuuji@64 1244 (setq bind (cdr bind))
yuuji@64 1245 (if (eq (car-safe (cdr bind)) 'lambda)
yuuji@64 1246 (setcar (cdr bind) 'progn))
yuuji@64 1247 (if (stringp (car-safe (cdr bind)))
yuuji@64 1248 (set keymap
yuuji@64 1249 (cons (cdr bind) (symbol-value keymap)))
yuuji@64 1250 (set keymap
yuuji@64 1251 (cons (vector (car bind) (cdr bind) t)
yuuji@64 1252 (symbol-value keymap))))))
yuuji@64 1253 bindlist)
yuuji@64 1254 (set keymap (cons name (symbol-value keymap)))))
yuuji@64 1255 (t
yuuji@64 1256 (mapcar
yuuji@64 1257 (function
yuuji@64 1258 (lambda (bind)
yuuji@64 1259 (define-key (symbol-value keymap) (vector (car bind)) (cdr bind))))
yuuji@64 1260 bindlist))))
yuuji@64 1261
yuuji@72 1262 ;;;
yuuji@72 1263 ;; Emacs 21 compensational wrapper
yuuji@72 1264 ;;;
yuuji@72 1265 (defun YaTeX-minibuffer-begin ()
yuuji@72 1266 (if (fboundp 'field-beginning)
yuuji@72 1267 (field-beginning (point-max))
yuuji@72 1268 (point-min)))
yuuji@72 1269
yuuji@72 1270 (defun YaTeX-minibuffer-end ()
yuuji@72 1271 (if (fboundp 'field-end)
yuuji@72 1272 (field-end (point-max))
yuuji@72 1273 (point-max)))
yuuji@72 1274
yuuji@72 1275 (defun YaTeX-minibuffer-string ()
yuuji@72 1276 (buffer-substring (YaTeX-minibuffer-begin) (YaTeX-minibuffer-end)))
yuuji@72 1277
yuuji@72 1278 (defun YaTeX-minibuffer-erase ()
yuuji@72 1279 (if (eq (selected-window) (minibuffer-window))
yuuji@72 1280 (if (fboundp 'delete-field) (delete-field) (erase-buffer))))
yuuji@72 1281
yuuji@80 1282 (fset 'YaTeX-buffer-substring
yuuji@80 1283 (if (fboundp 'buffer-substring-no-properties)
yuuji@80 1284 'buffer-substring-no-properties
yuuji@80 1285 'buffer-substring))
yuuji@80 1286
yuuji@72 1287 ;;;
yuuji@72 1288 ;; hilit19 vs. font-lock
yuuji@72 1289 ;;;
yuuji@80 1290 (defvar YaTeX-19-functions-font-lock-direct
yuuji@80 1291 '(YaTeX-19-re-search-in-env))
yuuji@80 1292
yuuji@72 1293 (defun YaTeX-convert-pattern-hilit2fontlock (h19pa)
yuuji@72 1294 "Convert hilit19's H19PA patterns alist to font-lock's one.
yuuji@72 1295 This function is a makeshift for YaTeX and yahtml."
yuuji@72 1296 (let ((ignorecase (not (null (car h19pa))))
yuuji@72 1297 (palist (cdr h19pa))
yuuji@72 1298 flpa i newface
yuuji@72 1299 (mapping
yuuji@72 1300 '((bold . YaTeX-font-lock-bold-face)
yuuji@72 1301 (italic . YaTeX-font-lock-italic-face)
yuuji@73 1302 (defun . font-lock-function-name-face)
yuuji@73 1303 (define . font-lock-variable-name-face)
yuuji@72 1304 (keyword . font-lock-keyword-face)
yuuji@72 1305 (decl . YaTeX-font-lock-declaration-face)
yuuji@72 1306 (label . YaTeX-font-lock-label-face)
yuuji@72 1307 (crossref . YaTeX-font-lock-crossref-face)
yuuji@72 1308 (include . YaTeX-font-lock-include-face)
yuuji@72 1309 (formula . YaTeX-font-lock-formula-face)
yuuji@80 1310 (delimiter . YaTeX-font-lock-delimiter-face)
yuuji@72 1311 (string . ignore) (comment . ignore)
yuuji@72 1312 )))
yuuji@72 1313 (while (setq i (car palist))
yuuji@72 1314 (setq newface (nth 2 i)
yuuji@72 1315 newface (or (cdr (assq newface mapping)) newface))
yuuji@72 1316 (cond
yuuji@72 1317 ((eq newface 'ignore) nil) ;no translation
yuuji@72 1318 ((stringp (car i)) ;hiliting by regexp
yuuji@72 1319 (setq flpa
yuuji@72 1320 (cons
yuuji@72 1321 (if (numberp (car (cdr i)))
yuuji@72 1322 (list (car i) ;regexp
yuuji@72 1323 (car (cdr i)) ;matching group number
yuuji@73 1324 newface nil) ;'keep) ;keep is hilit19 taste
yuuji@72 1325 (list
yuuji@72 1326 (concat
yuuji@72 1327 (car i) ;original regexp and..
yuuji@72 1328 ;;"[^"
yuuji@72 1329 ;;(regexp-quote (substring (car (cdr i)) 0 1))
yuuji@72 1330 ;;"]+" ;for shortest match
yuuji@72 1331 ".*"
yuuji@72 1332 (car (cdr i)))
yuuji@73 1333 0 (list 'quote newface) nil)) ;;'keep))
yuuji@72 1334 flpa)))
yuuji@72 1335 ((and (symbolp (car i)) (fboundp (car i)))
yuuji@80 1336 (if (memq (car i) YaTeX-19-functions-font-lock-direct)
yuuji@80 1337 ;; Put direct function call for it.
yuuji@80 1338 ;; When calling this function, fontify entire matched string.
yuuji@80 1339 (setq flpa
yuuji@80 1340 (cons
yuuji@80 1341 (list
yuuji@80 1342 (list 'lambda (list 'dummy) ;dummy should be boundary
yuuji@80 1343 (list (car i) (list 'quote (car (cdr i)))))
yuuji@80 1344 (list 0 newface))
yuuji@80 1345 flpa))
yuuji@80 1346 (setq flpa
yuuji@80 1347 (cons
yuuji@80 1348 (list (car (cdr i)) ;regexp
yuuji@72 1349 (list
yuuji@72 1350 (list
yuuji@80 1351 'lambda (list 'dummy)
yuuji@80 1352 '(goto-char (match-beginning 0))
yuuji@80 1353 (if (eq (nth 3 i) 'overwrite)
yuuji@80 1354 nil
yuuji@80 1355 '(remove-text-properties
yuuji@80 1356 (point) (min (point-max) (1+ (point)))
yuuji@80 1357 '(face nil font-lock-multiline nil)))
yuuji@72 1358 (list
yuuji@80 1359 'let (list '(e (match-end 0))
yuuji@80 1360 (list 'm (list (car i) (car (cdr i)))))
yuuji@80 1361 (list
yuuji@80 1362 'if 'm
yuuji@80 1363 (list
yuuji@80 1364 'YaTeX-font-lock-fillin
yuuji@80 1365 (list 'car 'm)
yuuji@80 1366 (list 'cdr 'm)
yuuji@80 1367 (list 'quote 'face)
yuuji@80 1368 (list 'quote 'font-lock)
yuuji@80 1369 (list 'quote newface))
yuuji@80 1370 '(goto-char e)
yuuji@80 1371 ))
yuuji@80 1372 nil) ;retun nil to cheat font-lock
yuuji@80 1373 nil nil)) ;pre-match, post-match both nil
yuuji@80 1374 flpa)))))
yuuji@72 1375 (setq palist (cdr palist)));while
yuuji@72 1376 (if (featurep 'xemacsp)
yuuji@72 1377 (nreverse flpa)
yuuji@72 1378 flpa)))
yuuji@72 1379
yuuji@73 1380 (if (and (boundp 'YaTeX-use-font-lock)
yuuji@73 1381 YaTeX-use-font-lock)
yuuji@73 1382 (require 'font-lock))
yuuji@73 1383
yuuji@72 1384 (cond
yuuji@73 1385 ((and (featurep 'font-lock) (fboundp 'defface))
yuuji@72 1386 ;; In each defface, '(class static-color) is for Emacs-21 -nw
yuuji@72 1387 ;; '(class tty) is for XEmacs-21 -nw
yuuji@72 1388 (defface YaTeX-font-lock-label-face
yuuji@72 1389 '((((class static-color)) (:foreground "yellow" :underline t))
yuuji@72 1390 (((type tty)) (:foreground "yellow" :underline t))
yuuji@72 1391 (((class color) (background dark)) (:foreground "pink" :underline t))
yuuji@72 1392 (((class color) (background light)) (:foreground "red" :underline t))
yuuji@72 1393 (t (:bold t :underline t)))
yuuji@72 1394 "Font Lock mode face used to highlight labels."
yuuji@72 1395 :group 'font-lock-faces)
yuuji@72 1396 (defvar YaTeX-font-lock-label-face 'YaTeX-font-lock-label-face)
yuuji@72 1397
yuuji@72 1398 (defface YaTeX-font-lock-declaration-face
yuuji@72 1399 '((((class color) (background dark)) (:foreground "cyan"))
yuuji@72 1400 (((class color) (background light)) (:foreground "RoyalBlue"))
yuuji@72 1401 (t (:bold t :underline t)))
yuuji@72 1402 "Font Lock mode face used to highlight some declarations."
yuuji@72 1403 :group 'font-lock-faces)
yuuji@72 1404 (defvar YaTeX-font-lock-declaration-face 'YaTeX-font-lock-declaration-face)
yuuji@72 1405
yuuji@72 1406 (defface YaTeX-font-lock-include-face
yuuji@72 1407 '((((class color) (background dark)) (:foreground "Plum1"))
yuuji@72 1408 (((class color) (background light)) (:foreground "purple"))
yuuji@72 1409 (t (:bold t :underline t)))
yuuji@72 1410 "Font Lock mode face used to highlight expression for including."
yuuji@72 1411 :group 'font-lock-faces)
yuuji@72 1412 (defvar YaTeX-font-lock-include-face 'YaTeX-font-lock-include-face)
yuuji@72 1413
yuuji@72 1414 (defface YaTeX-font-lock-formula-face
yuuji@72 1415 '((((class static-color)) (:bold t))
yuuji@72 1416 (((type tty)) (:bold t))
yuuji@72 1417 (((class color) (background dark)) (:foreground "khaki" :bold t))
yuuji@72 1418 (((class color) (background light)) (:foreground "Goldenrod"))
yuuji@72 1419 (t (:bold t :underline t)))
yuuji@72 1420 "Font Lock mode face used to highlight formula."
yuuji@72 1421 :group 'font-lock-faces)
yuuji@72 1422 (defvar YaTeX-font-lock-formula-face 'YaTeX-font-lock-formula-face)
yuuji@72 1423
yuuji@80 1424 (defface YaTeX-font-lock-delimiter-face
yuuji@80 1425 '((((class static-color)) (:bold t))
yuuji@80 1426 (((type tty)) (:bold t))
yuuji@80 1427 (((class color) (background dark))
yuuji@80 1428 (:foreground "saddlebrown" :background "ivory" :bold t))
yuuji@80 1429 (((class color) (background light)) (:foreground "red"))
yuuji@80 1430 (t (:bold t :underline t)))
yuuji@80 1431 "Font Lock mode face used to highlight delimiters."
yuuji@80 1432 :group 'font-lock-faces)
yuuji@80 1433 (defvar YaTeX-font-lock-delimiter-face 'YaTeX-font-lock-delimiter-face)
yuuji@80 1434
yuuji@80 1435 (defface YaTeX-font-lock-math-sub-face
yuuji@80 1436 '((((class static-color)) (:bold t))
yuuji@80 1437 (((type tty)) (:bold t))
yuuji@80 1438 (((class color) (background dark))
yuuji@80 1439 (:foreground "khaki" :bold t :underline t))
yuuji@80 1440 (((class color) (background light))
yuuji@80 1441 (:foreground "Goldenrod" :underline t))
yuuji@80 1442 (t (:bold t :underline t)))
yuuji@80 1443 "Font Lock mode face used to highlight subscripts in formula."
yuuji@80 1444 :group 'font-lock-faces)
yuuji@80 1445 (defvar YaTeX-font-lock-math-sub-face 'YaTeX-font-lock-math-sub-face)
yuuji@80 1446
yuuji@80 1447 (defface YaTeX-font-lock-math-sup-face
yuuji@80 1448 '((((class static-color)) (:bold t))
yuuji@80 1449 (((type tty)) (:bold t))
yuuji@80 1450 (((class color) (background dark))
yuuji@80 1451 (:bold nil :foreground "ivory" :background "lightyellow4"))
yuuji@80 1452 (((class color) (background light))
yuuji@80 1453 (:underline t :foreground "gold"))
yuuji@80 1454 (t (:bold t :underline t)))
yuuji@80 1455 "Font Lock mode face used to highlight superscripts in formula."
yuuji@80 1456 :group 'font-lock-faces)
yuuji@80 1457 (defvar YaTeX-font-lock-math-sup-face 'YaTeX-font-lock-math-sup-face)
yuuji@80 1458
yuuji@72 1459 (defface YaTeX-font-lock-crossref-face
yuuji@72 1460 '((((class color) (background dark)) (:foreground "lightgoldenrod"))
yuuji@72 1461 (((class color) (background light)) (:foreground "DarkGoldenrod"))
yuuji@72 1462 (t (:bold t :underline t)))
yuuji@80 1463 "Font Lock mode face used to highlight cross references."
yuuji@72 1464 :group 'font-lock-faces)
yuuji@72 1465 (defvar YaTeX-font-lock-crossref-face 'YaTeX-font-lock-crossref-face)
yuuji@72 1466
yuuji@72 1467 (defface YaTeX-font-lock-bold-face
yuuji@72 1468 '((t (:bold t)))
yuuji@72 1469 "Font Lock mode face used to express bold itself."
yuuji@72 1470 :group 'font-lock-faces)
yuuji@72 1471 (defvar YaTeX-font-lock-bold-face 'YaTeX-font-lock-bold-face)
yuuji@72 1472
yuuji@72 1473 (defface YaTeX-font-lock-italic-face
yuuji@72 1474 '((t (:italic t)))
yuuji@72 1475 "Font Lock mode face used to express italic itself."
yuuji@72 1476 :group 'font-lock-faces)
yuuji@72 1477 (defvar YaTeX-font-lock-italic-face 'YaTeX-font-lock-italic-face)
yuuji@72 1478
yuuji@72 1479 ;; Make sure the 'YaTeX-font-lock-{italic,bold}-face is bound with
yuuji@72 1480 ;; italic/bold fontsets
yuuji@72 1481 (if (and (fboundp 'fontset-list) YaTeX-use-italic-bold)
yuuji@73 1482 (let ((flist (fontset-list)) fnt italic bold
yuuji@73 1483 (df (or (and (fboundp 'face-font-name) (face-font-name 'default))
yuuji@73 1484 (face-font 'default)
yuuji@73 1485 (face-font 'italic)
yuuji@73 1486 (face-font 'bold)
yuuji@73 1487 "giveup!"))
yuuji@73 1488 sz medium-i bold-r)
yuuji@310 1489 (if (string-match
yuuji@310 1490 "^-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-\\(\\([0-9]+\\)\\)" df)
yuuji@310 1491 (setq sz (or (match-string 1 df) "16"))
yuuji@310 1492 (setq sz "16"))
yuuji@73 1493 (setq medium-i (format "-medium-i-[^-]+--%s" sz)
yuuji@73 1494 bold-r (format "-bold-r-[^-]+--%s" sz))
yuuji@72 1495 (while flist
yuuji@72 1496 (setq fnt (car flist))
yuuji@72 1497 (condition-case err
yuuji@72 1498 (cond
yuuji@73 1499 ((and (string-match medium-i fnt)
yuuji@73 1500 (null italic))
yuuji@72 1501 (set-face-font 'YaTeX-font-lock-italic-face (setq italic fnt)))
yuuji@73 1502 ((and (string-match bold-r fnt) (null bold))
yuuji@72 1503 (set-face-font 'YaTeX-font-lock-bold-face (setq bold fnt))))
yuuji@72 1504 (error nil))
yuuji@72 1505 (setq flist (cdr flist)))))
yuuji@72 1506
yuuji@72 1507 ;;Borrowed from XEmacs's font-lock.el
yuuji@72 1508 (defsubst YaTeX-font-lock-fillin (start end setprop markprop value &optional object)
yuuji@72 1509 "Fill in one property of the text from START to END.
yuuji@72 1510 Arguments PROP and VALUE specify the property and value to put where none are
yuuji@72 1511 already in place. Therefore existing property values are not overwritten.
yuuji@72 1512 Optional argument OBJECT is the string or buffer containing the text."
yuuji@72 1513 (let ((start (text-property-any start end markprop nil object)) next
yuuji@72 1514 (putfunc (if (fboundp 'put-nonduplicable-text-property)
yuuji@72 1515 'put-nonduplicable-text-property
yuuji@72 1516 'put-text-property)))
yuuji@72 1517 (if (eq putfunc 'put-text-property)
yuuji@72 1518 (setq markprop setprop))
yuuji@72 1519 (while start
yuuji@72 1520 (setq next (next-single-property-change start markprop object end))
yuuji@72 1521 (funcall putfunc start next setprop value object)
yuuji@72 1522 (funcall putfunc start next markprop value object)
yuuji@72 1523 (setq start (text-property-any next end markprop nil object)))))
yuuji@72 1524
yuuji@72 1525 (defun YaTeX-warning-font-lock (mode)
yuuji@72 1526 (let ((sw (selected-window)))
yuuji@72 1527 ;;(pop-to-buffer (format " *%s warning*" mode))
yuuji@72 1528 ;;(erase-buffer)
yuuji@72 1529 (momentary-string-display
yuuji@72 1530 (cond
yuuji@72 1531 (YaTeX-japan
yuuji@72 1532 (concat mode " は、既に font-lock に対応しました。\n"
yuuji@72 1533 "~/.emacs などにある\n"
yuuji@72 1534 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
yuuji@72 1535 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
yuuji@72 1536 "などの間に合わせの記述はもはや不要です。"))
yuuji@72 1537 (t
yuuji@72 1538 (concat mode " now supports the font-lock by itself.\n"
yuuji@72 1539 "So you can remove the descriptions such as\n"
yuuji@72 1540 "\t(put 'yatex-mode 'font-lock-keywords 'tex-mode)\n"
yuuji@72 1541 "\t(put 'yahtml-mode 'font-lock-keywords 'html-mode)\n"
yuuji@72 1542 "in your ~/.emacs file. Thank you."))) (point))
yuuji@72 1543 (select-window sw)))
yuuji@72 1544 ))
yuuji@72 1545
yuuji@80 1546 (defun YaTeX-assoc-regexp (elt alist)
yuuji@80 1547 "Like assoc, return a list of whose car match with ELT. Search from ALIST.
yuuji@80 1548 Note that each car of cons-cell is regexp. ELT is a plain text to be
yuuji@80 1549 compared by regexp."
yuuji@80 1550 (let (x)
yuuji@80 1551 (catch 'found
yuuji@80 1552 (while alist
yuuji@80 1553 (setq x (car (car alist)))
yuuji@80 1554 (if (string-match x elt)
yuuji@80 1555 (throw 'found (car alist)))
yuuji@80 1556 (setq alist (cdr alist))))))
yuuji@64 1557
yuuji@247 1558 (defun YaTeX-push-to-kill-ring (string)
yuuji@247 1559 "Push STRING to kill-ring, then show guidance message."
yuuji@247 1560 (and (stringp string) (string< "" string)
yuuji@247 1561 (let ((key (key-description (where-is-internal 'yank nil t)))
yuuji@247 1562 (msg
yuuji@247 1563 (if YaTeX-japan
yuuji@247 1564 " をkill-ringに入れました。次のyank(%s)で貼付できます"
yuuji@247 1565 " is stored into kill-ring. Paste it by yank(%s).")))
yuuji@247 1566 (kill-new string)
yuuji@247 1567 (message (concat "`%s'" msg) string key))))
yuuji@247 1568
yuuji@262 1569 (defun YaTeX-elapsed-time (before after)
yuuji@262 1570 "Get elapsed time from BEFORE and AFTER, which are given from currente-time."
yuuji@262 1571 (if (fboundp 'float) ;Then, current-time function should be.
yuuji@262 1572 (let ((mil (float 1000000))) ;To protect parse error before 19
yuuji@262 1573 (+ (* (- (nth 0 after) (nth 0 before)) 65536)
yuuji@262 1574 (- (nth 1 after) (nth 1 before))
yuuji@262 1575 (- (/ (nth 2 after) mil)
yuuji@262 1576 (/ (nth 2 before) mil))))))
yuuji@262 1577
yuuji@68 1578 ;;;
yuuji@68 1579 ;; Functions for the Installation time
yuuji@68 1580 ;;;
yuuji@64 1581
yuuji@58 1582 (defun bcf-and-exit ()
yuuji@58 1583 "Byte compile rest of argument and kill-emacs."
yuuji@58 1584 (if command-line-args-left
yuuji@68 1585 (let ((load-path (cons "." load-path)))
yuuji@68 1586 (and (fboundp 'set-language-environment)
yuuji@68 1587 (featurep 'mule)
yuuji@68 1588 (set-language-environment "Japanese"))
yuuji@58 1589 (mapcar 'byte-compile-file command-line-args-left)
yuuji@58 1590 (kill-emacs))))
yuuji@64 1591
yuuji@80 1592 (defun tfb-and-exit ()
yuuji@80 1593 "Texinfo-format-buffer and kill-emacs."
yuuji@80 1594 (if command-line-args-left
yuuji@80 1595 (let ((load-path (cons ".." load-path)))
yuuji@80 1596 (and (fboundp 'set-language-environment)
yuuji@80 1597 (featurep 'mule)
yuuji@80 1598 (set-language-environment "Japanese"))
yuuji@80 1599 (mapcar (function
yuuji@80 1600 (lambda (arg)
yuuji@80 1601 (find-file arg)
yuuji@80 1602 (texinfo-format-buffer)
yuuji@266 1603 (cond
yuuji@266 1604 ((fboundp 'set-buffer-file-coding-system)
yuuji@266 1605 (set-buffer-file-coding-system 'sjis-dos))
yuuji@266 1606 ((fboundp 'set-file-coding-system)
yuuji@266 1607 (set-file-coding-system '*sjis*dos))
yuuji@266 1608 ((boundp 'NEMACS)
yuuji@266 1609 (set (make-local-variable 'kanji-fileio-code) 1)))
yuuji@266 1610 (let ((coding-system-for-write buffer-file-coding-system))
yuuji@266 1611 (basic-save-buffer))))
yuuji@80 1612 command-line-args-left)
yuuji@80 1613 (kill-emacs))))
yuuji@80 1614
yuuji@23 1615 (provide 'yatexlib)
yuuji@72 1616 ; Local variables:
yuuji@72 1617 ; fill-prefix: ";;; "
yuuji@72 1618 ; paragraph-start: "^$\\| \\|;;;$"
yuuji@72 1619 ; paragraph-separate: "^$\\| \\|;;;$"
yuuji@80 1620 ; coding: sjis
yuuji@72 1621 ; End: