yatex

view yatexhlp.el @ 47:d7e7b4654058

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