yatex

annotate yatexlib.el @ 627:f294195d0d3f

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