yatex

view yatexhlp.el @ 79:0734be649cb8

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