yatex

annotate yatexhlp.el @ 583:d4831b3672f8

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