yatex

view yatexhlp.el @ 34:c61405ef1bd1

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