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