yatex

view yatexhlp.el @ 255:214702e4df54

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