* gnus-util.el (gnus-truncate-string): Abolished.
[elisp/gnus.git-] / lisp / dgnushack.el
1 ;;; dgnushack.el --- a hack to set the load path for byte-compiling
2 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Version: 4.19
8 ;; Keywords: news, path
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;; Set coding priority of Shift-JIS to the bottom.
32 (if (featurep 'xemacs)
33     (defalias 'set-coding-priority 'ignore)
34   (defalias 'coding-priority-list 'ignore)
35   (defalias 'set-coding-priority-list 'ignore))
36 (cond ((and (featurep 'xemacs) (featurep 'mule))
37        (if (memq 'shift-jis (coding-priority-list))
38            (set-coding-priority-list
39             (append (delq 'shift-jis (coding-priority-list)) '(shift-jis)))))
40       ((boundp 'MULE)
41        (put '*coding-category-sjis* 'priority (length *predefined-category*)))
42       ((featurep 'mule)
43        (if (memq 'coding-category-sjis coding-category-list)
44            (set-coding-priority
45             (append (delq 'coding-category-sjis
46                           (copy-sequence coding-category-list))
47                     '(coding-category-sjis))))))
48
49 (defalias 'facep 'ignore)
50
51 (require 'cl)
52 (unless (dolist (var nil t))
53   ;; Override the macro `dolist' which may have been defined in egg.el.
54   (load "cl-macs" nil t))
55
56 (defvar srcdir (or (getenv "srcdir") "."))
57
58 (let ((urldir (getenv "URLDIR")))
59   (unless (zerop (length urldir))
60     (push (file-name-as-directory urldir) load-path)))
61
62 (defvar dgnushack-w3-directory (let ((w3dir (getenv "W3DIR")))
63                                  (unless (zerop (length w3dir))
64                                    (file-name-as-directory w3dir))))
65 (when dgnushack-w3-directory
66   (push dgnushack-w3-directory load-path))
67
68 ;; If we are building w3 in a different directory than the source
69 ;; directory, we must read *.el from source directory and write *.elc
70 ;; into the building directory.  For that, we define this function
71 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
72 (defun byte-compile-dest-file (filename)
73   "Convert an Emacs Lisp source file name to a compiled file name.
74  In addition, remove directory name part from FILENAME."
75   (setq filename (byte-compiler-base-file-name filename))
76   (setq filename (file-name-sans-versions filename))
77   (setq filename (file-name-nondirectory filename))
78   (if (memq system-type '(win32 w32 mswindows windows-nt))
79       (setq filename (downcase filename)))
80   (cond ((eq system-type 'vax-vms)
81          (concat (substring filename 0 (string-match ";" filename)) "c"))
82         ((string-match emacs-lisp-file-regexp filename)
83          (concat (substring filename 0 (match-beginning 0)) ".elc"))
84         (t (concat filename ".elc"))))
85
86 (require 'bytecomp)
87
88 (when (boundp 'MULE)
89   (let (current-load-list)
90     ;; Make the function to be silent at compile-time.
91     (defun locate-library (library &optional nosuffix)
92       "Show the full path name of Emacs library LIBRARY.
93 This command searches the directories in `load-path' like `M-x load-library'
94 to find the file that `M-x load-library RET LIBRARY RET' would load.
95 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
96 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
97       (interactive "sLocate library: ")
98       (catch 'answer
99         (mapcar
100          '(lambda (dir)
101             (mapcar
102              '(lambda (suf)
103                 (let ((try (expand-file-name (concat library suf) dir)))
104                   (and (file-readable-p try)
105                        (null (file-directory-p try))
106                        (progn
107                          (or noninteractive
108                              (message "Library is file %s" try))
109                          (throw 'answer try)))))
110              (if nosuffix '("") '(".elc" ".el" ""))))
111          load-path)
112         (or noninteractive
113             (message "No library %s in search path" library))
114         nil))
115     (byte-compile 'locate-library)))
116
117 (unless (fboundp 'si:byte-optimize-form-code-walker)
118   (byte-optimize-form nil);; Load `byte-opt' or `byte-optimize'.
119   (setq max-specpdl-size 3000)
120   (defalias 'si:byte-optimize-form-code-walker
121     (symbol-function 'byte-optimize-form-code-walker))
122   (defun byte-optimize-form-code-walker (form for-effect)
123     (if (and for-effect (memq (car-safe form) '(and or)))
124         ;; Fix bug in and/or forms.
125         (let ((fn (car form))
126               (backwards (reverse (cdr form))))
127           (while (and backwards
128                       (null (setcar backwards
129                                     (byte-optimize-form (car backwards) t))))
130             (setq backwards (cdr backwards)))
131           (if (and (cdr form) (null backwards))
132               (byte-compile-log
133                "  all subforms of %s called for effect; deleted" form))
134           (if backwards
135               (let ((head backwards))
136                 (while (setq backwards (cdr backwards))
137                   (setcar backwards (byte-optimize-form (car backwards)
138                                                         nil)))
139                 (cons fn (nreverse head)))))
140       (si:byte-optimize-form-code-walker form for-effect)))
141   (byte-compile 'byte-optimize-form-code-walker))
142
143 (condition-case nil
144     (char-after)
145   (wrong-number-of-arguments
146    ;; Optimize byte code for `char-after'.
147    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
148    (defun byte-optimize-char-after (form)
149      (if (null (cdr form))
150          '(char-after (point))
151        form))))
152
153 (condition-case nil
154     (char-before)
155   (wrong-number-of-arguments
156    ;; Optimize byte code for `char-before'.
157    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
158    (defun byte-optimize-char-before (form)
159      (if (null (cdr form))
160          '(char-before (point))
161        form))))
162
163 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
164
165 (condition-case err
166     (load "~/.lpath.el" t nil t)
167   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
168
169 ;; Don't load path-util until `char-after' and `char-before' have been
170 ;; optimized because it requires `poe' and then modify the functions.
171
172 ;; If the APEL modules are installed under the non-standard directory,
173 ;; for example "/var/home/john/lisp/apel-VERSION/", you should add that
174 ;; name using the configure option "--with-addpath=".
175 ;; And also the directory where the EMU modules are installed, for
176 ;; example "/usr/local/share/mule/19.34/site-lisp/", it should be
177 ;; included in the standard `load-path' or added by the configure
178 ;; option "--with-addpath=".
179 (let ((path (or (locate-library "path-util")
180                 (locate-library "apel/path-util")));; backward compat.
181       parent lpath)
182   (if path
183       (progn
184         (when (string-match "/$" (setq path (file-name-directory path)))
185           (setq path (substring path 0 (match-beginning 0))))
186         ;; path == "/var/home/john/lisp/apel-VERSION"
187         (when (string-match "/$" (setq parent (file-name-directory path)))
188           (setq parent (substring path 0 (match-beginning 0))))
189         ;; parent == "/var/home/john/lisp"
190         (if (setq lpath (or (member path load-path)
191                             (member (file-name-as-directory path) load-path)))
192             (unless (or (member parent load-path)
193                         (member (file-name-as-directory parent) load-path))
194               (push parent (cdr lpath)))
195           (push path load-path)
196           (unless (or (member parent load-path)
197                       (member (file-name-as-directory parent) load-path))
198             (push parent (cdr load-path))))
199         (require 'path-util))
200     (error "
201 APEL modules are not found in %s.
202 Try to re-configure with --with-addpath=APEL_PATH and run make again.
203 "
204            load-path)))
205
206 (unless (locate-library "mel")
207   (add-path "flim"))
208 (unless (module-installed-p 'mel)
209   ;; FLIM 1.14 may have installed in two "flim" subdirectories.
210   (push (expand-file-name "flim"
211                           (file-name-directory (get-latest-path "^apel$" t)))
212         load-path)
213   (unless (module-installed-p 'mel)
214     (error "
215 FLIM modules does not found in %s.
216 Try to re-configure with --with-addpath=FLIM_PATH and run make again.
217 "
218            load-path)))
219 (add-path "semi")
220
221 (push srcdir load-path)
222 (load (expand-file-name "lpath.el" srcdir) nil t t)
223
224 (load (expand-file-name "gnus-clfns.el" srcdir) nil t t)
225
226 (require 'custom)
227
228 ;; Bind functions defined by `defun-maybe'.
229 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
230 (defun byte-compile-file-form-defun-maybe (form)
231   (if (and (not (fboundp (nth 1 form)))
232            (memq 'unresolved byte-compile-warnings))
233       (setq byte-compile-function-environment
234             (cons (cons (nth 1 form)
235                         (cons 'lambda (cdr (cdr form))))
236                   byte-compile-function-environment)))
237   form)
238
239 (condition-case nil
240     :symbol-for-testing-whether-colon-keyword-is-available-or-not
241   (void-variable
242    (defun dgnushack-bind-colon-keywords ()
243      "Bind all the colon keywords for old Emacsen."
244      (let ((cache (expand-file-name "dgnuskwds.el" srcdir))
245            (makefile (expand-file-name "Makefile" srcdir))
246            (buffer (get-buffer-create " *colon keywords*"))
247            keywords ignores files file dirs dir form elem make-backup-files)
248        (save-excursion
249          (set-buffer buffer)
250          (let (buffer-file-format
251                format-alist
252                insert-file-contents-post-hook
253                insert-file-contents-pre-hook
254                jam-zcat-filename-list
255                jka-compr-compression-info-list)
256            (if (and (file-exists-p cache)
257                     (file-exists-p makefile)
258                     (file-newer-than-file-p cache makefile))
259                (progn
260                  (insert-file-contents cache nil nil nil t)
261                  (setq keywords (read buffer)))
262              (setq
263               ignores
264               '(:symbol-for-testing-whether-colon-keyword-is-available-or-not
265                 ;; The following keywords will be bound by CUSTOM.
266                 :get :group :initialize :link :load :options :prefix
267                 :require :set :tag :type)
268               files (list (locate-library "semi-def")
269                           (locate-library "mailcap")
270                           (locate-library "mime-def")
271                           (locate-library "path-util")
272                           (locate-library "poem"))
273               dirs (list (file-name-as-directory (expand-file-name srcdir))))
274              (while files
275                (when (setq file (pop files))
276                  (setq dir (file-name-directory file))
277                  (unless (member dir dirs)
278                    (push dir dirs))))
279              (message "Searching for all the colon keywords in:")
280              (while dirs
281                (setq dir (pop dirs))
282                (message " %s..." dir)
283                (setq files (directory-files dir t
284                                             "\\.el\\(\\.gz\\|\\.bz2\\)?$"))
285                (while files
286                  (setq file (pop files))
287                  (if (string-match "\\(\\.gz$\\)\\|\\.bz2$" file)
288                      (let ((temp (expand-file-name "dgnustemp.el" srcdir)))
289                        (when
290                            (let* ((binary (if (boundp 'MULE)
291                                               '*noconv*
292                                             'binary))
293                                   (coding-system-for-read binary)
294                                   (coding-system-for-write binary)
295                                   (input-coding-system binary)
296                                   (output-coding-system binary)
297                                   (default-process-coding-system
298                                     (cons binary binary))
299                                   call-process-hook)
300                              (insert-file-contents file nil nil nil t)
301                              (when
302                                  (condition-case code
303                                      (progn
304                                        (if (match-beginning 1)
305                                            (call-process-region
306                                             (point-min) (point-max)
307                                             "gzip" t buffer nil "-cd")
308                                          (call-process-region
309                                           (point-min) (point-max)
310                                           "bzip2" t buffer nil "-d"))
311                                        t)
312                                    (error
313                                     (erase-buffer)
314                                     (message "In file %s: %s" file code)
315                                     nil))
316                                (write-region (point-min) (point-max) temp
317                                              nil 'silent)
318                                t))
319                          (unwind-protect
320                              (insert-file-contents temp nil nil nil t)
321                            (delete-file temp))))
322                    (insert-file-contents file nil nil nil t))
323                  (while (setq form (condition-case nil
324                                        (read buffer)
325                                      (error nil)))
326                    (while form
327                      (setq elem (pop form))
328                      (unless (memq (car-safe elem)
329                                    '(\` backquote
330                                      defcustom defface defgroup
331                                      define-widget quote))
332                        (while (consp elem)
333                          (push (car elem) form)
334                          (setq elem (cdr elem)))
335                        (when (and elem
336                                   (symbolp elem)
337                                   (not (eq ': elem))
338                                   (eq ?: (aref (symbol-name elem) 0))
339                                   (not (memq elem ignores))
340                                   (not (memq elem keywords)))
341                          (push elem keywords)))))))
342              (setq keywords (sort keywords
343                                   (lambda (a b)
344                                     (string-lessp (symbol-name a)
345                                                   (symbol-name b)))))
346              (erase-buffer)
347              (insert (format "%s" keywords))
348              (write-region (point-min) (point) cache nil 'silent)
349              (message
350               "The following colon keywords will be bound at run-time:\n %s"
351               keywords))))
352        (kill-buffer buffer)
353        (defconst dgnushack-colon-keywords keywords)
354        (while keywords
355          (set (car keywords) (car keywords))
356          (setq keywords (cdr keywords)))))
357    (byte-compile 'dgnushack-bind-colon-keywords)
358    (dgnushack-bind-colon-keywords)))
359
360 (if (boundp 'MULE)
361     (progn
362       (setq :version ':version
363             :set-after ':set-after)
364       (require 'custom)
365       (defadvice custom-handle-keyword
366         (around dont-signal-an-error-even-if-unsupported-keyword-is-given
367                 activate)
368         "Don't signal an error even if unsupported keyword is given."
369         (if (not (memq (ad-get-arg 1) '(:version :set-after)))
370             ad-do-it))))
371
372 (when (boundp 'MULE)
373   (put 'custom-declare-face 'byte-optimizer
374        'byte-optimize-ignore-unsupported-custom-keywords)
375   (put 'custom-declare-group 'byte-optimizer
376        'byte-optimize-ignore-unsupported-custom-keywords)
377   (defun byte-optimize-ignore-unsupported-custom-keywords (form)
378     (if (or (memq ':version (nthcdr 4 form))
379             (memq ':set-after (nthcdr 4 form)))
380         (let ((newform (list (car form) (nth 1 form)
381                              (nth 2 form) (nth 3 form)))
382               (args (nthcdr 4 form)))
383           (while args
384             (or (memq (car args) '(:version :set-after))
385                 (setq newform (nconc newform (list (car args)
386                                                    (car (cdr args))))))
387             (setq args (cdr (cdr args))))
388           newform)
389       form))
390
391   (put 'custom-declare-variable 'byte-hunk-handler
392        'byte-compile-file-form-custom-declare-variable)
393   (defun byte-compile-file-form-custom-declare-variable (form)
394     ;; Bind defcustom'ed variables.
395     (if (memq 'free-vars byte-compile-warnings)
396         (setq byte-compile-bound-variables
397               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
398     (if (memq ':version (nthcdr 4 form))
399         ;; Make the variable uncustomizable.
400         `(defvar ,(nth 1 (nth 1 form)) ,(nth 1 (nth 2 form))
401            ,(substring (nth 3 form) (if (string-match "^[\t *]+" (nth 3 form))
402                                         (match-end 0)
403                                       0)))
404       ;; Ignore unsupported keyword(s).
405       (if (memq ':set-after (nthcdr 4 form))
406           (let ((newform (list (car form) (nth 1 form)
407                                (nth 2 form) (nth 3 form)))
408                 (args (nthcdr 4 form)))
409             (while args
410               (or (eq (car args) ':set-after)
411                   (setq newform (nconc newform (list (car args)
412                                                      (car (cdr args))))))
413               (setq args (cdr (cdr args))))
414             newform)
415         form))))
416
417 ;; Unknown variables and functions.
418 (unless (boundp 'buffer-file-coding-system)
419   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
420 (unless (featurep 'xemacs)
421   (defalias 'Custom-make-dependencies 'ignore)
422   (defalias 'update-autoloads-from-directory 'ignore))
423
424 (defalias 'device-sound-enabled-p 'ignore)
425 (defalias 'play-sound-file 'ignore)
426 (defalias 'nndb-request-article 'ignore)
427 (defalias 'efs-re-read-dir 'ignore)
428 (defalias 'ange-ftp-re-read-dir 'ignore)
429 (defalias 'define-mail-user-agent 'ignore)
430
431 (defconst dgnushack-unexporting-files
432   (append '("dgnushack.el" "dgnuspath.el" "dgnuskwds.el" "lpath.el")
433           (condition-case nil
434               (progn (require 'shimbun) nil)
435             (error '("nnshimbun.el")))
436           (unless (or (condition-case code
437                           (require 'w3-forms)
438                         (error
439                          (message "No w3: %s %s retrying..." code
440                                   (locate-library "w3-forms"))
441                          nil))
442                       ;; Maybe mis-configured Makefile is used (e.g.
443                       ;; configured for FSFmacs but XEmacs is running).
444                       (let ((lp (delete dgnushack-w3-directory
445                                         (copy-sequence load-path))))
446                         (if (let ((load-path lp))
447                               (condition-case nil
448                                   (require 'w3-forms)
449                                 (error nil)))
450                             ;; If success, fix `load-path' for compiling.
451                             (progn
452                               (setq load-path lp)
453                               (message " => fixed; W3DIR=%s"
454                                        (file-name-directory
455                                         (locate-library "w3-forms")))
456                               t)
457                           (message " => ignored")
458                           nil)))
459             '("nnweb.el" "nnlistserv.el" "nnultimate.el"
460               "nnslashdot.el" "nnwarchive.el" "webmail.el"
461               "nnwfm.el" "nnrss.el"))
462           (condition-case nil
463               (progn (require 'bbdb) nil)
464             (error '("gnus-bbdb.el")))
465           (unless (featurep 'xemacs)
466             '("gnus-xmas.el" "gnus-picon.el" "messagexmas.el"
467               "nnheaderxm.el" "smiley.el"))
468           (when (or (featurep 'xemacs) (<= emacs-major-version 20))
469             '("smiley-ems.el"))
470           (when (and (fboundp 'base64-decode-string)
471                      (subrp (symbol-function 'base64-decode-string)))
472             '("base64.el"))
473           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
474             '("md5.el")))
475   "Files which will not be installed.")
476
477 (defconst dgnushack-exporting-files
478   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
479     (dolist (file dgnushack-unexporting-files)
480       (setq files (delete file files)))
481     (sort files 'string-lessp))
482   "Files which will be compiled and installed.")
483
484 (defun dgnushack-exporting-files ()
485   "Print name of files which will be installed."
486   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
487
488 (defun dgnushack-compile (&optional warn)
489   ;;(setq byte-compile-dynamic t)
490   (unless warn
491     (setq byte-compile-warnings
492           '(free-vars unresolved callargs redefine)))
493   (unless (locate-library "cus-edit")
494     (error "You do not seem to have Custom installed.
495 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
496 You also then need to add the following to the lisp/dgnushack.el file:
497
498      (push \"~/lisp/custom\" load-path)
499
500 Modify to suit your needs."))
501
502   ;; Show `load-path'.
503   (message "load-path=(\"%s\")"
504            (mapconcat 'identity load-path "\"\n           \""))
505
506   (dolist (file dgnushack-exporting-files)
507     (setq file (expand-file-name file srcdir))
508     (when (and (file-exists-p
509                 (setq elc (concat (file-name-nondirectory file) "c")))
510                (file-newer-than-file-p file elc))
511       (delete-file elc)))
512
513   (let ((files dgnushack-exporting-files)
514         ;;(byte-compile-generate-call-tree t)
515         file elc)
516     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
517     ;; installed.
518     (when (featurep 'xemacs)
519       (setq gnus-xmas-glyph-directory "dummy"))
520     (while (setq file (pop files))
521       (setq file (expand-file-name file srcdir))
522       (when (or (not (file-exists-p
523                       (setq elc (concat (file-name-nondirectory file) "c"))))
524                 (file-newer-than-file-p file elc))
525         (ignore-errors
526           (byte-compile-file file))))))
527
528 (defun dgnushack-recompile ()
529   (require 'gnus)
530   (byte-recompile-directory "." 0))
531
532 (defvar dgnushack-gnus-load-file (expand-file-name "gnus-load.el" srcdir))
533 (defvar dgnushack-cus-load-file (expand-file-name "cus-load.el" srcdir))
534 (defvar dgnushack-auto-load-file (expand-file-name "auto-autoloads.el" srcdir))
535
536 (defun dgnushack-make-cus-load ()
537   (when (condition-case nil
538             (load "cus-dep")
539           (error nil))
540     (let ((cusload-base-file dgnushack-cus-load-file))
541       (if (fboundp 'custom-make-dependencies)
542           (custom-make-dependencies)
543         (Custom-make-dependencies)))))
544
545 (defun dgnushack-make-auto-load ()
546   (require 'autoload)
547   (unless (make-autoload '(define-derived-mode child parent name
548                             "docstring" body)
549                          "file")
550     (defadvice make-autoload (around handle-define-derived-mode activate)
551       "Handle `define-derived-mode'."
552       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
553           (setq ad-return-value
554                 (list 'autoload
555                       (list 'quote (nth 1 (ad-get-arg 0)))
556                       (ad-get-arg 1)
557                       (nth 4 (ad-get-arg 0))
558                       t nil))
559         ad-do-it))
560     (put 'define-derived-mode 'doc-string-elt 3))
561   (let ((generated-autoload-file dgnushack-gnus-load-file)
562         (make-backup-files nil)
563         (autoload-package-name "gnus"))
564     (if (featurep 'xemacs)
565         (progn
566           (if (file-exists-p generated-autoload-file)
567               (delete-file generated-autoload-file))
568           (if (file-exists-p dgnushack-auto-load-file)
569               (delete-file dgnushack-auto-load-file)))
570       (with-temp-file generated-autoload-file
571         (insert ?\014)))
572     (if (featurep 'xemacs)
573         (let ((si:message (symbol-function 'message)))
574           (defun message (fmt &rest args)
575             (cond ((and (string-equal "Generating autoloads for %s..." fmt)
576                         (file-exists-p (file-name-nondirectory (car args))))
577                    (funcall si:message
578                             fmt (file-name-nondirectory (car args))))
579                   ((string-equal "No autoloads found in %s" fmt))
580                   ((string-equal "Generating autoloads for %s...done" fmt))
581                   (t (apply si:message fmt args))))
582           (unwind-protect
583               (batch-update-autoloads)
584             (fset 'message si:message)))
585       (batch-update-autoloads))))
586
587 (defun dgnushack-make-load ()
588   (message (format "Generating %s..." dgnushack-gnus-load-file))
589   (with-temp-file dgnushack-gnus-load-file
590     (if (file-exists-p dgnushack-cus-load-file)
591         (progn
592           (insert-file-contents dgnushack-cus-load-file)
593           (delete-file dgnushack-cus-load-file)
594           (goto-char (point-min))
595           (search-forward ";;; Code:")
596           (forward-line)
597           (delete-region (point-min) (point))
598           (unless (re-search-forward "\
599 ^[\t ]*(autoload[\t\n ]+\\('\\|(quote[\t\n ]+\\)custom-add-loads[\t\n ]"
600                                      nil t)
601             (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))
602           (goto-char (point-min))
603           (insert "\
604 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
605 ;;
606 ;;; Code:
607 ")
608           (goto-char (point-max))
609           (if (search-backward "custom-versions-load-alist" nil t)
610               (forward-line -1)
611             (forward-line -1)
612             (while (eq (char-after) ?\;)
613               (forward-line -1))
614             (forward-line))
615           (delete-region (point) (point-max))
616           (insert "\n"))
617       (insert "\
618 ;;; gnus-load.el --- automatically extracted autoload
619 ;;
620 ;;; Code:
621 "))
622     ;; smiley-* are duplicated. Remove them all.
623     (let ((point (point)))
624       (insert-file-contents dgnushack-gnus-load-file)
625       (goto-char point)
626       (while (search-forward "smiley-" nil t)
627         (beginning-of-line)
628         (if (looking-at "(autoload ")
629             (delete-region (point) (progn (forward-sexp) (point)))
630           (forward-line))))
631     ;;
632     (goto-char (point-max))
633     (when (search-backward "\n(provide " nil t)
634       (forward-line -1)
635       (delete-region (point) (point-max)))
636     (insert "\
637
638 \(provide 'gnus-load)
639
640 ;;; Local Variables:
641 ;;; version-control: never
642 ;;; no-byte-compile: t
643 ;;; no-update-autoloads: t
644 ;;; End:
645 ;;; gnus-load.el ends here\n"))
646   (message (format "Compiling %s..." dgnushack-gnus-load-file))
647   (byte-compile-file dgnushack-gnus-load-file))
648
649 \f
650 (defun dgnushack-compose-package ()
651   "Re-split the file gnus-load.el into custom-load.el and
652 auto-autoloads.el.  It is silly, should be improved!"
653   (message "
654 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...")
655   (let ((customload (expand-file-name "custom-load.el" srcdir))
656         (autoloads (expand-file-name "auto-autoloads.el" srcdir))
657         start)
658     (with-temp-buffer
659       (insert-file-contents dgnushack-gnus-load-file)
660       (delete-file dgnushack-gnus-load-file)
661       (when (file-exists-p (concat dgnushack-gnus-load-file "c"))
662         (delete-file (concat dgnushack-gnus-load-file "c")))
663       (while (prog1
664                  (looking-at "[\t ;]")
665                (forward-line 1)))
666       (setq start (point))
667       (insert "\
668 ;;; custom-load.el --- automatically extracted custom dependencies\n
669 ;;; Code:\n\n")
670       (goto-char (point-max))
671       (while (progn
672                (forward-line -1)
673                (not (looking-at "[\t ]*(custom-add-loads[\t\n ]"))))
674       (forward-list 1)
675       (forward-line 1)
676       (insert "\n;;; custom-load.el ends here\n")
677       (write-region start (point) customload)
678       (while (looking-at "[\t ]*$")
679         (forward-line 1))
680       (setq start (point))
681       (if (re-search-forward "^[\t\n ]*(if[\t\n ]+(featurep[\t\n ]" nil t)
682           (let ((from (goto-char (match-beginning 0))))
683             (delete-region from (progn
684                                   (forward-list 1)
685                                   (forward-line 1)
686                                   (point))))
687         (while (looking-at "[\t ;]")
688           (forward-line 1)))
689       (insert "(if (featurep 'gnus-autoloads) (error \"Already loaded\"))\n")
690       (goto-char (point-max))
691       (while (progn
692                (forward-line -1)
693                (not (looking-at "[\t ]*(provide[\t\n ]"))))
694       (insert "(provide 'gnus-autoloads)\n")
695       (write-region start (point) autoloads))
696     (byte-compile-file customload)
697     (byte-compile-file autoloads))
698   (message "\
699 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...done
700 \n"))
701
702 \f
703 (defconst dgnushack-info-file-regexp-en
704   (let ((names '("gnus" "message" "emacs-mime"))
705         regexp name)
706     (while (setq name (pop names))
707       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
708                            (when names "\\|"))))
709     regexp)
710   "Regexp matching English info files.")
711
712 (defconst dgnushack-info-file-regexp-ja
713   (let ((names '("gnus-ja" "message-ja"))
714         regexp name)
715     (while (setq name (pop names))
716       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
717                            (when names "\\|"))))
718     regexp)
719   "Regexp matching Japanese info files.")
720
721 (defun dgnushack-remove-extra-files-in-package ()
722   "Remove extra files in the lisp directory of the XEmacs package."
723   (let ((lisp-dir (expand-file-name (concat "lisp/"
724                                             ;; GNUS_PRODUCT_NAME
725                                             (cadr command-line-args-left)
726                                             "/")
727                                     ;; PACKAGEDIR
728                                     (car command-line-args-left))))
729     (when (file-directory-p lisp-dir)
730       (let (files)
731         (dolist (file dgnushack-exporting-files)
732           (setq files (nconc files (list file (concat file "c")))))
733         (dolist (file (directory-files lisp-dir nil nil t t))
734           (unless (member file files)
735             (setq file (expand-file-name file lisp-dir))
736             (message "Removing %s..." file)
737             (condition-case nil
738                 (delete-file file)
739               (error nil))))))))
740
741 (defun dgnushack-install-package-manifest ()
742   "Install MANIFEST file as an XEmacs package."
743   (let* ((package-dir (car command-line-args-left))
744          (product-name (cadr command-line-args-left))
745          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
746                                  package-dir))
747          make-backup-files)
748     (message "Generating %s..." name)
749     (with-temp-file name
750       (insert "pkginfo/MANIFEST." product-name "\n")
751       (let ((lisp-dir (concat "lisp/" product-name "/"))
752             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
753             file)
754         (while (setq file (pop files))
755           (unless (member file dgnushack-unexporting-files)
756             (insert lisp-dir file "\n")))
757         (setq files
758               (sort (directory-files "../texi/" nil
759                                      (concat dgnushack-info-file-regexp-en
760                                              "\\|"
761                                              dgnushack-info-file-regexp-ja)
762                                      t)
763                     'string-lessp))
764         (while (setq file (pop files))
765           (insert "info/" file "\n"))))))
766
767 \f
768 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
769   (if (condition-case nil
770           (progn
771             (describe-key-briefly '((())) nil)
772             t)
773         (wrong-number-of-arguments nil);; Old Emacsen.
774         (error t))
775       form
776     (if insert
777         `(if ,insert
778              (insert (funcall 'describe-key-briefly ,key))
779            (funcall 'describe-key-briefly ,key))
780       `(funcall 'describe-key-briefly ,key))))
781
782 ;;; dgnushack.el ends here