yatex

annotate yatexhlp.el @ 46:cd1b63102eed

Support Mule2
author yuuji
date Mon, 19 Sep 1994 16:54:19 +0000
parents b00c74813e56
children eb0512bfcb7f
rev   line source
yuuji@16 1 ;;; -*- Emacs-Lisp -*-
yuuji@16 2 ;;; YaTeX helper with LaTeX commands and macros.
yuuji@16 3 ;;; yatexhlp.el
yuuji@16 4 ;;; (c )1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
yuuji@46 5 ;;; Last modified Tue Sep 20 01:35:12 1994 on figaro
yuuji@16 6 ;;; $Id$
yuuji@16 7
yuuji@46 8 (let ((help-file (concat "YATEXHLP."
yuuji@46 9 (cond (YaTeX-japan "jp")
yuuji@46 10 (t "eng"))))
yuuji@46 11 (help-dir
yuuji@46 12 (cond (YaTeX-emacs-19 (expand-file-name "../etc" exec-directory))
yuuji@46 13 (t exec-directory))))
yuuji@16 14 (defvar YaTeX-help-file
yuuji@46 15 (expand-file-name help-file help-dir)
yuuji@16 16 "*Help file of LaTeX/TeX commands or macros.")
yuuji@16 17 (defvar YaTeX-help-file-private
yuuji@16 18 (expand-file-name (concat "~/" help-file))
yuuji@16 19 "*Private help file of LaTeX/TeX macros.")
yuuji@16 20 )
yuuji@16 21 (defvar YaTeX-help-delimiter "\C-_" "Delimiter of each help entry.")
yuuji@16 22 (defvar YaTeX-help-entry-map (copy-keymap YaTeX-mode-map)
yuuji@16 23 "Key map used in help entry.")
yuuji@16 24 (defvar YaTeX-help-file-current nil
yuuji@16 25 "Holds help file name to which the description in current buffer should go.")
yuuji@16 26 (defvar YaTeX-help-command-current nil
yuuji@16 27 "Holds command name on which the user currently write description.")
yuuji@16 28 (defvar YaTeX-help-saved-config nil
yuuji@16 29 "Holds window configruation before the editing of manual.")
yuuji@16 30 (defvar YaTeX-help-synopsis
yuuji@16 31 (cond (YaTeX-japan "【書式】")
yuuji@16 32 (t "[[ Synopsis ]]"))
yuuji@16 33 "Section header of synopsis.")
yuuji@16 34 (defvar YaTeX-help-description
yuuji@16 35 (cond (YaTeX-japan "【説明】")
yuuji@16 36 (t "[[ Description ]]"))
yuuji@16 37 "Section header of description.")
yuuji@16 38
yuuji@23 39 (defvar YaTeX-help-reference-regexp "<refer\\s +\\([^>]+\\)>"
yuuji@23 40 "Regexp of reference format of YaTeX-help file.")
yuuji@23 41 (defvar YaTeX-help-buffer "** YaTeX HELP **" "Help buffer name for yatexhlp")
yuuji@23 42
yuuji@23 43 (defun YaTeX-help-entries ()
yuuji@23 44 "Return the alist which contains all the entries in YaTeX-help file."
yuuji@23 45 (let (entries entry)
yuuji@23 46 (save-excursion
yuuji@23 47 (mapcar
yuuji@23 48 (function
yuuji@23 49 (lambda (help)
yuuji@23 50 (if (file-exists-p help)
yuuji@23 51 (progn
yuuji@23 52 (set-buffer (find-file-noselect help))
yuuji@23 53 (save-excursion
yuuji@23 54 (goto-char (point-min))
yuuji@23 55 (while (re-search-forward
yuuji@23 56 (concat "^" (regexp-quote YaTeX-help-delimiter)
yuuji@23 57 "\\(.+\\)$") nil t)
yuuji@23 58 (setq entry (buffer-substring
yuuji@23 59 (match-beginning 1) (match-end 1)))
yuuji@23 60 (or (assoc entry entries)
yuuji@23 61 (setq entries (cons (list entry) entries)))))))))
yuuji@23 62 (list YaTeX-help-file YaTeX-help-file-private)))
yuuji@23 63 entries)
yuuji@23 64 )
yuuji@23 65
yuuji@23 66 (defvar YaTeX-help-entries (YaTeX-help-entries))
yuuji@23 67
yuuji@23 68 (defun YaTeX-help-resolve-reference (buffer1 buffer2 &optional done-list)
yuuji@23 69 "Replace reference format in buffer1 with refered contents in buffer2."
yuuji@23 70 (let (ref ref-list beg end)
yuuji@23 71 (save-excursion
yuuji@23 72 (switch-to-buffer buffer1)
yuuji@23 73 (goto-char (point-min))
yuuji@23 74 (while (re-search-forward YaTeX-help-reference-regexp nil t)
yuuji@23 75 (setq ref (buffer-substring (match-beginning 1) (match-end 1))
yuuji@23 76 ref-list (cons (list ref) ref-list))
yuuji@23 77 (replace-match "")
yuuji@23 78 (if (assoc ref done-list) nil ;already documented.
yuuji@23 79 (switch-to-buffer buffer2)
yuuji@23 80 (save-excursion
yuuji@23 81 (goto-char (point-min))
yuuji@23 82 (if (re-search-forward
yuuji@23 83 (concat (regexp-quote YaTeX-help-delimiter)
yuuji@23 84 (regexp-quote ref)
yuuji@23 85 "$") nil t)
yuuji@23 86 (progn
yuuji@23 87 (setq beg (progn (forward-line 2) (point))
yuuji@23 88 end (progn
yuuji@23 89 (re-search-forward
yuuji@23 90 (concat "^" (regexp-quote YaTeX-help-delimiter))
yuuji@23 91 nil 1)
yuuji@23 92 (goto-char (match-beginning 0))
yuuji@23 93 (forward-line -1)
yuuji@23 94 (while (and (bolp) (eolp) (not (bobp)))
yuuji@23 95 (forward-char -1))
yuuji@23 96 (point)))
yuuji@23 97 (switch-to-buffer buffer1)
yuuji@23 98 (insert-buffer-substring buffer2 beg end))))
yuuji@23 99 (switch-to-buffer buffer1)))
yuuji@23 100 (if beg (YaTeX-help-resolve-reference
yuuji@23 101 buffer1 buffer2 (append done-list ref-list))))
yuuji@23 102 )
yuuji@23 103 )
yuuji@23 104
yuuji@23 105 (defun YaTeX-refer-help (command help-file &optional append)
yuuji@16 106 "Refer the COMMAND's help into HELP-FILE.
yuuji@16 107 \[Help-file format\]
yuuji@16 108 <DELIM><LaTeX/TeX command without escape character(\\)><NL>
yuuji@16 109 <Synopsis><NL>
yuuji@16 110 <Documentation><TERM>
yuuji@16 111 Where: <DELIM> is the value of YaTeX-help-delimiter.
yuuji@16 112 <NL> is newline.
yuuji@16 113 <TERM> is newline or end of buffer."
yuuji@16 114 (let ((hfbuf (find-file-noselect help-file))
yuuji@23 115 (hbuf (get-buffer-create YaTeX-help-buffer))
yuuji@16 116 (curwin (selected-window))
yuuji@16 117 sb se db de)
yuuji@16 118 (set-buffer hfbuf)
yuuji@16 119 (goto-char (point-min))
yuuji@16 120 (if (null
yuuji@23 121 (let ((case-fold-search nil))
yuuji@23 122 (re-search-forward
yuuji@23 123 (concat (regexp-quote YaTeX-help-delimiter)
yuuji@23 124 (regexp-quote command)
yuuji@23 125 "$") nil t)))
yuuji@16 126 nil ;if not found, return nil
yuuji@16 127 (forward-line 1)
yuuji@16 128 (setq sb (point)
yuuji@16 129 se (progn (forward-line 1) (point))
yuuji@16 130 db (point)
yuuji@16 131 de (progn
yuuji@23 132 (re-search-forward
yuuji@23 133 (concat "^" (regexp-quote YaTeX-help-delimiter)) nil 1)
yuuji@23 134 (- (point) (length YaTeX-help-delimiter))))
yuuji@23 135 (YaTeX-showup-buffer
yuuji@23 136 hbuf (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@16 137 (pop-to-buffer hbuf)
yuuji@23 138 (if append (goto-char (point-max)) (erase-buffer))
yuuji@16 139 (insert YaTeX-help-synopsis "\n")
yuuji@16 140 (insert-buffer-substring hfbuf sb se)
yuuji@16 141 (insert "\n" YaTeX-help-description "\n")
yuuji@16 142 (insert-buffer-substring hfbuf db de)
yuuji@23 143 (YaTeX-help-resolve-reference hbuf hfbuf (list (list command)))
yuuji@16 144 (goto-char (point-min))
yuuji@16 145 (select-window curwin)
yuuji@16 146 t))
yuuji@16 147 )
yuuji@16 148 (defun YaTeX-help-newline (&optional arg)
yuuji@16 149 (interactive "P")
yuuji@16 150 (if (and (= (current-column) 1) (= (preceding-char) ?.) (eolp))
yuuji@16 151 (let ((cbuf (current-buffer)))
yuuji@16 152 (beginning-of-line)
yuuji@16 153 (kill-line 1)
yuuji@16 154 (save-excursion
yuuji@16 155 (YaTeX-help-add-entry
yuuji@16 156 YaTeX-help-command-current YaTeX-help-file-current))
yuuji@16 157 (set-window-configuration YaTeX-help-saved-config)
yuuji@16 158 (bury-buffer cbuf))
yuuji@16 159 (newline arg))
yuuji@16 160 )
yuuji@16 161 (defun YaTeX-help-add-entry (command help-file)
yuuji@16 162 (let ((hfbuf (find-file-noselect help-file))
yuuji@16 163 (dbuf (current-buffer)) beg end)
yuuji@16 164 (goto-char (point-min))
yuuji@16 165 (re-search-forward (concat "^" (regexp-quote YaTeX-help-synopsis)))
yuuji@16 166 (forward-line 1) (setq beg (point))
yuuji@16 167 (end-of-line) (setq end (point))
yuuji@16 168 (set-buffer hfbuf)
yuuji@16 169 (goto-char (point-min))
yuuji@16 170 (insert YaTeX-help-delimiter command "\n")
yuuji@16 171 (insert-buffer-substring dbuf beg end)
yuuji@16 172 (insert "\n")
yuuji@16 173 (set-buffer dbuf)
yuuji@16 174 (re-search-forward (concat "^" (regexp-quote YaTeX-help-description)))
yuuji@16 175 (forward-line 1)
yuuji@16 176 (setq beg (point))
yuuji@16 177 (setq end (point-max))
yuuji@16 178 (set-buffer hfbuf)
yuuji@16 179 (insert-buffer-substring dbuf beg end)
yuuji@16 180 (insert "\n\n")
yuuji@16 181 (forward-line -1)
yuuji@16 182 (delete-blank-lines)
yuuji@16 183 (let ((make-backup-files t))
yuuji@16 184 (basic-save-buffer))
yuuji@23 185 (bury-buffer hfbuf)
yuuji@23 186 (setq YaTeX-help-entries (cons (list command) YaTeX-help-entries)))
yuuji@16 187 )
yuuji@16 188 (defun YaTeX-help-prepare-entry (command help-file)
yuuji@16 189 "Read help description on COMMAND and add it to HELP-FILE."
yuuji@16 190 (let ((buf (get-buffer-create "**Description**"))
yuuji@16 191 (conf (current-window-configuration)))
yuuji@23 192 (YaTeX-showup-buffer
yuuji@23 193 buf (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@16 194 (pop-to-buffer buf)
yuuji@16 195 (make-local-variable 'YaTeX-help-file-current)
yuuji@16 196 (make-local-variable 'YaTeX-help-command-current)
yuuji@16 197 (make-local-variable 'YaTeX-help-saved-config)
yuuji@16 198 (setq YaTeX-help-file-current help-file
yuuji@16 199 YaTeX-help-command-current command
yuuji@16 200 YaTeX-help-saved-config conf
yuuji@16 201 mode-name "Text"
yuuji@16 202 major-mode 'text)
yuuji@16 203 (erase-buffer)
yuuji@16 204 (insert YaTeX-help-synopsis "\n\n" YaTeX-help-description "\n\n")
yuuji@16 205 (define-key YaTeX-help-entry-map "\r" 'YaTeX-help-newline)
yuuji@16 206 (use-local-map YaTeX-help-entry-map)
yuuji@16 207 (message
yuuji@16 208 (cond (YaTeX-japan "入力を終えたら . のみ入力してRET")
yuuji@16 209 (t "Type only `.' and RET to exit."))))
yuuji@16 210 )
yuuji@16 211 (defun YaTeX-enrich-help (command)
yuuji@16 212 "Add the COMMAND's help to help file."
yuuji@16 213 (if (y-or-n-p (format "No help on `%s'. Create help?" command))
yuuji@16 214 (YaTeX-help-prepare-entry
yuuji@16 215 command
yuuji@16 216 (if (y-or-n-p "Add help to global documentation?")
yuuji@16 217 YaTeX-help-file YaTeX-help-file-private)))
yuuji@16 218 )
yuuji@16 219
yuuji@16 220 (defun YaTeX-help-sort (&optional help-file)
yuuji@16 221 "Sort help file HELP-FILE.
yuuji@16 222 If HELP-FILE is nil or called interactively, sort current buffer
yuuji@16 223 as a help file."
yuuji@16 224 (interactive)
yuuji@16 225 (if help-file (set-buffer (find-file-noselect help-file)))
yuuji@16 226 (sort-regexp-fields
yuuji@16 227 nil "\\(\\sw+\\)\\([^]+\\|\\s'\\)" "\\1" (point-min) (point-max))
yuuji@16 228 )
yuuji@16 229
yuuji@23 230 (defun YaTeX-apropos-file (keyword help-file &optional append)
yuuji@23 231 (let ((hb (find-file-noselect help-file))
yuuji@23 232 (ab (get-buffer-create YaTeX-help-buffer))
yuuji@23 233 (sw (selected-window))
yuuji@23 234 (head (concat "^" (regexp-quote YaTeX-help-delimiter)))
yuuji@23 235 pt command)
yuuji@23 236 (YaTeX-showup-buffer
yuuji@23 237 ab (function (lambda (x) (nth 3 (window-edges x)))))
yuuji@23 238 (select-window (get-buffer-window ab))
yuuji@23 239 (set-buffer ab) ;assertion
yuuji@23 240 (or append (erase-buffer))
yuuji@23 241 (set-buffer hb)
yuuji@23 242 (goto-char (point-min))
yuuji@23 243 (while (re-search-forward keyword nil t)
yuuji@23 244 (setq pt (point))
yuuji@23 245 (re-search-backward head nil t)
yuuji@23 246 (setq command (buffer-substring (match-end 0) (point-end-of-line)))
yuuji@23 247 (switch-to-buffer ab)
yuuji@23 248 (goto-char (point-max))
yuuji@23 249 (insert-char ?- (1- (window-width)))
yuuji@23 250 (insert (format "\n<<%s>>\n" command))
yuuji@23 251 (YaTeX-refer-help command help-file t) ;append mode
yuuji@23 252 (set-buffer hb)
yuuji@23 253 (goto-char pt)
yuuji@23 254 (if (re-search-forward head nil 1)
yuuji@23 255 (goto-char (1- (match-beginning 0)))))
yuuji@23 256 (select-window sw)
yuuji@23 257 pt)
yuuji@23 258 )
yuuji@23 259
yuuji@23 260 ;;;###autoload
yuuji@23 261 (defun YaTeX-apropos (key)
yuuji@23 262 (interactive "sLaTeX apropos (regexp): ")
yuuji@23 263 (or (YaTeX-apropos-file key YaTeX-help-file)
yuuji@23 264 (YaTeX-apropos-file key YaTeX-help-file-private t)
yuuji@23 265 (message "No matches found."))
yuuji@23 266 )
yuuji@23 267
yuuji@16 268 ;;;###autoload
yuuji@16 269 (defun YaTeX-help ()
yuuji@16 270 "Show help buffer of LaTeX/TeX commands or macros."
yuuji@16 271 (interactive)
yuuji@16 272 (let (p beg end command)
yuuji@16 273 (save-excursion
yuuji@16 274 (if (looking-at YaTeX-ec-regexp)
yuuji@16 275 (goto-char (match-end 0)))
yuuji@16 276 (setq p (point)) ;remember current position.
yuuji@16 277 (cond
yuuji@16 278 ((YaTeX-on-begin-end-p)
yuuji@16 279 ;;if on \begin or \end, extract its environment.
yuuji@16 280 (setq command
yuuji@16 281 (cond ((match-beginning 1)
yuuji@16 282 (buffer-substring (match-beginning 1) (match-end 1)))
yuuji@16 283 ((match-beginning 2)
yuuji@16 284 (buffer-substring (match-beginning 2) (match-end 2))))))
yuuji@16 285 ((search-backward YaTeX-ec (point-beginning-of-line) t)
yuuji@16 286 (goto-char (setq beg (match-end 0)))
yuuji@23 287 (re-search-forward YaTeX-TeX-token-regexp (point-end-of-line) t)
yuuji@16 288 (setq end (point))
yuuji@16 289 (if (and (<= beg p) (<= p end))
yuuji@16 290 (setq command (buffer-substring beg end)))))
yuuji@16 291 (if (or (string= command "begin") (string= command "end"))
yuuji@16 292 (progn
yuuji@16 293 (search-forward "{" (point-end-of-line))
yuuji@16 294 (setq beg (point))
yuuji@16 295 (search-forward "}" (point-end-of-line))
yuuji@16 296 (setq command (buffer-substring beg (match-beginning 0)))))
yuuji@16 297 (setq command
yuuji@16 298 (completing-read
yuuji@16 299 "Describe (La)TeX command: "
yuuji@23 300 YaTeX-help-entries nil nil command))
yuuji@16 301 );end excursion
yuuji@16 302 (or (YaTeX-refer-help command YaTeX-help-file)
yuuji@16 303 (YaTeX-refer-help command YaTeX-help-file-private)
yuuji@16 304 (YaTeX-enrich-help command)))
yuuji@16 305 )