]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-maint.el
Style cleanup; don't put closing parens on their
[gnu-emacs] / lisp / calc / calc-maint.el
1 ;; Calculator for GNU Emacs, maintenance routines
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
13
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
21
22
23
24
25 (defun calc-compile ()
26 "Compile all parts of Calc.
27 Unix usage:
28 emacs -batch -l calc-maint -f calc-compile"
29 (interactive)
30 (if (equal (user-full-name) "David Gillespie")
31 (load "~/lisp/newbytecomp"))
32 (setq byte-compile-verbose t)
33 (if noninteractive
34 (let ((old-message (symbol-function 'message))
35 (old-write-region (symbol-function 'write-region))
36 (comp-was-func nil)
37 (comp-len 0))
38 (unwind-protect
39 (progn
40 (fset 'message (symbol-function 'calc-compile-message))
41 (fset 'write-region (symbol-function 'calc-compile-write-region))
42 (calc-do-compile))
43 (fset 'message old-message)
44 (fset 'write-region old-write-region)))
45 (calc-do-compile)))
46
47 (defun calc-do-compile ()
48 (let ((make-backup-files nil)
49 (changed-rules nil)
50 (changed-units nil)
51 (message-bug (string-match "^18.\\([0-4][0-9]\\|5[0-6]\\)"
52 emacs-version)))
53 (setq max-lisp-eval-depth (max 400 max-lisp-eval-depth))
54 ;; Enable some irrelevant warnings to avoid compiler bug in 19.29:
55 (setq byte-compile-warnings (and (string-match "^19.29" emacs-version)
56 '(obsolete)))
57
58 ;; Make sure we're in the right directory.
59 (find-file "calc.el")
60 (if (= (buffer-size) 0)
61 (error "This command must be used in the Calc source directory."))
62
63 ;; Make sure current directory is in load-path.
64 (setq load-path (cons default-directory load-path))
65 (load "calc-macs.el" nil t t)
66 (provide 'calc)
67 (provide 'calc-ext)
68
69 ;; Compile all the source files.
70 (let ((files (append
71 '("calc.el" "calc-ext.el")
72 (sort (directory-files
73 default-directory nil
74 "\\`calc-.[^x].*\\.el\\'")
75 'string<))))
76 (while files
77 (if (file-newer-than-file-p (car files) (concat (car files) "c"))
78 (progn
79 (if (string-match "calc-rules" (car files))
80 (setq changed-rules t))
81 (if (string-match "calc-units" (car files))
82 (setq changed-units t))
83 (or message-bug (message ""))
84 (byte-compile-file (car files)))
85 (message "File %s is up to date." (car files)))
86 (if (string-match "calc\\(-ext\\)?.el" (car files))
87 (load (concat (car files) "c") nil t t))
88 (setq files (cdr files))))
89
90 (if (or changed-units changed-rules)
91 (condition-case err
92 (progn
93
94 ;; Pre-build the units table.
95 (if (and changed-units
96 (not (string-match "Lucid" emacs-version)))
97 (progn
98 (or message-bug (message ""))
99 (save-excursion
100 (calc-create-buffer)
101 (math-build-units-table))
102 (find-file "calc-units.elc")
103 (goto-char (point-max))
104 (insert "\n(setq math-units-table '"
105 (prin1-to-string math-units-table)
106 ")\n")
107 (save-buffer)))
108
109 ;; Pre-build rewrite rules for j D, j M, etc.
110 (if (and changed-rules (not (string-match "^19" emacs-version)))
111 (let ((rules nil))
112 (or message-bug (message ""))
113 (find-file "calc-rules.elc")
114 (goto-char (point-min))
115 (while (re-search-forward "defun calc-\\([A-Za-z]*Rules\\)"
116 nil t)
117 (setq rules (cons (buffer-substring (match-beginning 1)
118 (match-end 1))
119 rules)))
120 (goto-char (point-min))
121 (re-search-forward "\n(defun calc-[A-Za-z]*Rules")
122 (beginning-of-line)
123 (delete-region (point) (point-max))
124 (mapcar (function
125 (lambda (v)
126 (let* ((vv (intern (concat "var-" v)))
127 (val (save-excursion
128 (calc-create-buffer)
129 (calc-var-value vv))))
130 (insert "\n(defun calc-" v " () '"
131 (prin1-to-string val) ")\n"))))
132 (sort rules 'string<))
133 (save-buffer))))
134 (error (message "Unable to pre-build tables %s" err))))
135 (message "Done. Don't forget to install with \"make public\" or \"make private\".")))
136
137 (defun calc-compile-message (fmt &rest args)
138 (cond ((and (= (length args) 2)
139 (stringp (car args))
140 (string-match ".elc?\\'" (car args))
141 (symbolp (nth 1 args)))
142 (let ((name (symbol-name (nth 1 args))))
143 (princ (if comp-was-func ", " " "))
144 (if (and comp-was-func (eq (string-match comp-was-func name) 0))
145 (setq name (substring name (1- (length comp-was-func))))
146 (setq comp-was-func (if (string-match "\\`[a-zA-Z]+-" name)
147 (substring name 0 (match-end 0))
148 " ")))
149 (if (> (+ comp-len (length name)) 75)
150 (progn
151 (princ "\n ")
152 (setq comp-len 0)))
153 (princ name)
154 (send-string-to-terminal "") ; cause an fflush(stdout)
155 (setq comp-len (+ comp-len 2 (length name)))))
156 ((and (setq comp-was-func nil
157 comp-len 0)
158 (= (length args) 1)
159 (stringp (car args))
160 (string-match ".elc?\\'" (car args)))
161 (or (string-match "Saving file %s..." fmt)
162 (funcall old-message fmt (file-name-nondirectory (car args)))))
163 ((string-match "\\(Preparing\\|Building\\).*\\.\\.\\.$" fmt)
164 (send-string-to-terminal (apply 'format fmt args)))
165 ((string-match "\\(Preparing\\|Building\\).*\\.\\.\\. *done$" fmt)
166 (send-string-to-terminal "done\n"))
167 (t (apply old-message fmt args))))
168
169 (defun calc-compile-write-region (start end filename &optional append visit &rest rest)
170 (if (eq visit t)
171 (set-buffer-auto-saved))
172 (if (and (string-match "\\.elc" filename)
173 (= start (point-min))
174 (= end (point-max)))
175 (save-excursion
176 (goto-char (point-min))
177 (if (search-forward "\n(require (quote calc-macs))\n" nil t)
178 (replace-match ""))
179 (setq end (point-max))))
180 (apply old-write-region start end filename append 'quietly rest)
181 (message "Wrote %s" filename)
182 nil)
183
184
185
186 (defun calc-split-tutorial (&optional force)
187 (interactive "P")
188 (calc-split-manual force 1))
189
190
191 (defun calc-split-reference (&optional force)
192 (interactive "P")
193 (calc-split-manual force 2))
194
195
196 (defun calc-split-manual (&optional force part)
197 "Split the Calc manual into separate Tutorial and Reference manuals.
198 Use this if your TeX installation is too small-minded to handle
199 calc.texinfo all at once.
200 Usage: C-x C-f calc.texinfo RET
201 M-x calc-split-manual RET"
202 (interactive "P")
203 (or (let ((case-fold-search t))
204 (string-match "calc\\.texinfo" (buffer-name)))
205 force
206 (error "This command should be used in the calc.texinfo buffer."))
207 (let ((srcbuf (current-buffer))
208 tutpos refpos endpos (maxpos (point-max)))
209 (goto-char 1)
210 (search-forward "@c [tutorial]")
211 (beginning-of-line)
212 (setq tutpos (point))
213 (search-forward "@c [reference]")
214 (beginning-of-line)
215 (setq refpos (point))
216 (search-forward "@c [end]")
217 (beginning-of-line)
218 (setq endpos (point))
219 (or (eq part 2)
220 (progn
221 (find-file "calctut.tex")
222 (erase-buffer)
223 (insert-buffer-substring srcbuf 1 refpos)
224 (insert-buffer-substring srcbuf endpos maxpos)
225 (calc-split-volume "I" "ref" "Tutorial" "Reference")
226 (save-buffer)))
227 (or (eq part 1)
228 (progn
229 (find-file "calcref.tex")
230 (erase-buffer)
231 (insert-buffer-substring srcbuf 1 tutpos)
232 (insert "\n@tex\n\\global\\advance\\chapno by 1\n@end tex\n")
233 (insert-buffer-substring srcbuf refpos maxpos)
234 (calc-split-volume "II" "tut" "Reference" "Tutorial")
235 (save-buffer)))
236 (switch-to-buffer srcbuf)
237 (goto-char 1))
238 (message (cond ((eq part 1) "Wrote file calctut.tex")
239 ((eq part 2) "Wrote file calcref.tex")
240 (t "Wrote files calctut.tex and calcref.tex"))))
241
242 (defun calc-split-volume (number fix name other-name)
243 (goto-char 1)
244 (search-forward "@c [title]\n")
245 (search-forward "Manual")
246 (delete-backward-char 6)
247 (insert name)
248 (search-forward "@c [volume]\n")
249 (insert "@sp 1\n@center Volume " number ": " name "\n")
250 (let ((pat (format "@c \\[fix-%s \\(.*\\)\\]\n" fix)))
251 (while (re-search-forward pat nil t)
252 (let ((topic (buffer-substring (match-beginning 1) (match-end 1))))
253 (re-search-forward "@\\(p?xref\\){[^}]*}")
254 (let ((cmd (buffer-substring (match-beginning 1) (match-end 1))))
255 (delete-region (match-beginning 0) (match-end 0))
256 (insert (if (equal cmd "pxref") "see" "See")
257 " ``" topic "'' in @emph{the Calc "
258 other-name "}")))))
259 (goto-char 1)
260 (while (search-forward "@c [when-split]\n" nil t)
261 (while (looking-at "@c ")
262 (delete-char 3)
263 (forward-line 1)))
264 (goto-char 1)
265 (while (search-forward "@c [not-split]\n" nil t)
266 (while (not (looking-at "@c"))
267 (insert "@c ")
268 (forward-line 1))))
269
270
271 (defun calc-inline-summary ()
272 "Make a special \"calcsum.tex\" file to be used with main manual."
273 (calc-split-summary nil t))
274
275 (defun calc-split-summary (&optional force in-line)
276 "Make a special \"calcsum.tex\" file with just the Calc summary."
277 (interactive "P")
278 (or (let ((case-fold-search t))
279 (string-match "calc\\.texinfo" (buffer-name)))
280 force
281 (error "This command should be used in the calc.texinfo buffer."))
282 (let ((srcbuf (current-buffer))
283 begpos sumpos endpos midpos)
284 (goto-char 1)
285 (search-forward "{Calc Manual}")
286 (backward-char 1)
287 (delete-backward-char 6)
288 (insert "Summary")
289 (search-forward "@c [begin]")
290 (beginning-of-line)
291 (setq begpos (point))
292 (search-forward "@c [summary]")
293 (beginning-of-line)
294 (setq sumpos (point))
295 (search-forward "@c [end-summary]")
296 (beginning-of-line)
297 (setq endpos (point))
298 (find-file "calcsum.tex")
299 (erase-buffer)
300 (insert-buffer-substring srcbuf 1 begpos)
301 (insert "@tex\n"
302 "\\global\\advance\\appendixno2\n"
303 "\\gdef\\xref#1.{See ``#1.''}\n")
304 (setq midpos (point))
305 (insert "@end tex\n")
306 (insert-buffer-substring srcbuf sumpos endpos)
307 (insert "@bye\n")
308 (goto-char 1)
309 (if (search-forward "{. a b c" nil t)
310 (replace-match "{... a b c"))
311 (goto-char 1)
312 (if in-line
313 (let ((buf (current-buffer))
314 (page nil))
315 (find-file "calc.aux")
316 (if (> (buffer-size) 0)
317 (progn
318 (goto-char 1)
319 (re-search-forward "{Summary-pg}{\\([0-9]+\\)}")
320 (setq page (string-to-int (buffer-substring (match-beginning 1)
321 (match-end 1))))))
322 (switch-to-buffer buf)
323 (if page
324 (progn
325 (message "Adjusting starting page number to %d" page)
326 (goto-char midpos)
327 (insert (format "\\global\\pageno=%d\n" page)))
328 (message "Unable to find page number from calc.aux")))
329 (if (search-forward "@c smallbook" nil t)
330 (progn ; activate "smallbook" format for compactness
331 (beginning-of-line)
332 (forward-char 1)
333 (delete-char 2))))
334 (let ((buf (current-buffer)))
335 (find-file "calc.ky")
336 (if (> (buffer-size) 0)
337 (let ((ibuf (current-buffer)))
338 (message "Mixing in page numbers from Key Index (calc.ky)")
339 (switch-to-buffer buf)
340 (goto-char 1)
341 (search-forward "notes at the end")
342 (insert "; the number in italics is\n"
343 "the page number where the command is described")
344 (while (re-search-forward
345 "@r{.*@: *\\([^ ]\\(.*[^ ]\\)?\\) *@:.*@:.*@:\\(.*\\)@:.*}"
346 nil t)
347 (let ((key (buffer-substring (match-beginning 1) (match-end 1)))
348 (pos (match-beginning 3))
349 num)
350 (set-buffer ibuf)
351 (goto-char 1)
352 (let ((p '( ( "I H " . "H I " ) ; oops!
353 ( "@@ ' \"" . "@@" ) ( "h m s" . "@@" )
354 ( "\\\\" . "{\\tt\\indexbackslash }" )
355 ( "_" . "{\\_}" )
356 ( "\\^" . "{\\tt\\hat}" )
357 ( "<" . "{\\tt\\less}" )
358 ( ">" . "{\\tt\\gtr}" )
359 ( "\"" ) ( "@{" ) ( "@}" )
360 ( "~" ) ( "|" ) ( "@@" )
361 ( "\\+" . "{\\tt\\char43}" )
362 ( "# l" . "# L" )
363 ( "I f I" . "f I" ) ( "I f Q" . "f Q" )
364 ( "V &" . "&" ) ( "C-u " . "" ) ))
365 (case-fold-search nil))
366 (while p
367 (if (string-match (car (car p)) key)
368 (setq key (concat (substring key 0 (match-beginning 0))
369 (or (cdr (car p))
370 (format "{\\tt\\char'%03o}"
371 (aref key (1- (match-end
372 0)))))
373 (substring key (match-end 0)))))
374 (setq p (cdr p)))
375 (setq num (and (search-forward (format "\\entry {%s}{" key)
376 nil t)
377 (looking-at "[0-9]+")
378 (buffer-substring (point) (match-end 0)))))
379 (set-buffer buf)
380 (goto-char pos)
381 (insert "@pgref{" (or num "") "}")))
382 (goto-char midpos)
383 (insert "\\gdef\\pgref#1{\\hbox to 2em{\\indsl\\hss#1}\\ \\ }\n"))
384 (message
385 "Unable to find Key Index (calc.ky); no page numbers inserted"))
386 (switch-to-buffer buf))
387 (save-buffer))
388 (message "Wrote file calcsum.tex"))
389
390
391
392 (defun calc-public-autoloads ()
393 "Modify the public \"default\" file to contain the necessary autoload and
394 global-set-key commands for Calc."
395 (interactive)
396 (let ((home default-directory)
397 (p load-path)
398 instbuf name)
399 (while (and p
400 (not (file-exists-p
401 (setq name (expand-file-name "default" (car p)))))
402 (not (file-exists-p
403 (setq name (expand-file-name "default.el" (car p))))))
404 (setq p (cdr p)))
405 (or p (error "Unable to find \"default\" file. Create one and try again."))
406 (find-file name)
407 (if buffer-read-only (error "No write permission for \"%s\"" buffer-file-name))
408 (goto-char (point-max))
409 (calc-add-autoloads home "calc-public-autoloads")))
410
411 (defun calc-private-autoloads ()
412 "Modify the user's \".emacs\" file to contain the necessary autoload and
413 global-set-key commands for Calc."
414 (interactive)
415 (let ((home default-directory))
416 (find-file "~/.emacs")
417 (goto-char (point-max))
418 (calc-add-autoloads home "calc-private-autoloads")))
419
420 (defun calc-add-autoloads (home cmd)
421 (barf-if-buffer-read-only)
422 (let (top)
423 (if (and (re-search-backward ";;; Commands added by calc-.*-autoloads"
424 nil t)
425 (setq top (point))
426 (search-forward ";;; End of Calc autoloads" nil t))
427 (progn
428 (forward-line 1)
429 (message "(Removing previous autoloads)")
430 (delete-region top (point)))
431 (insert "\n\n")
432 (backward-char 1)))
433 (insert ";;; Commands added by " cmd " on "
434 (current-time-string) ".
435 \(autoload 'calc-dispatch \"calc\" \"Calculator Options\" t)
436 \(autoload 'full-calc \"calc\" \"Full-screen Calculator\" t)
437 \(autoload 'full-calc-keypad \"calc\" \"Full-screen X Calculator\" t)
438 \(autoload 'calc-eval \"calc\" \"Use Calculator from Lisp\")
439 \(autoload 'defmath \"calc\" nil t t)
440 \(autoload 'calc \"calc\" \"Calculator Mode\" t)
441 \(autoload 'quick-calc \"calc\" \"Quick Calculator\" t)
442 \(autoload 'calc-keypad \"calc\" \"X windows Calculator\" t)
443 \(autoload 'calc-embedded \"calc\" \"Use Calc inside any buffer\" t)
444 \(autoload 'calc-embedded-activate \"calc\" \"Activate =>'s in buffer\" t)
445 \(autoload 'calc-grab-region \"calc\" \"Grab region of Calc data\" t)
446 \(autoload 'calc-grab-rectangle \"calc\" \"Grab rectangle of data\" t)
447 \(setq load-path (nconc load-path (list \"" (directory-file-name home) "\")))
448 \(global-set-key \"\\e#\" 'calc-dispatch)
449 ;;; End of Calc autoloads.\n")
450 (let ((trim-versions-without-asking t))
451 (save-buffer)))
452
453 ;;; calc-maint.el ends here