Sync up with Pterodactyl Gnus v0.99.
[elisp/gnus.git-] / lisp / dgnushack.el
1 ;;; dgnushack.el --- a hack to set the load path for byte-compiling
2 ;; Copyright (C) 1994,95,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
6 ;; Version: 4.19
7 ;; Keywords: news, path
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; Set coding priority of Shift-JIS to the bottom.
31 (defvar *predefined-category*)
32 (defvar coding-category-list)
33 (if (featurep 'xemacs)
34     (fset 'set-coding-priority 'ignore)
35   (fset 'coding-priority-list 'ignore)
36   (fset 'set-coding-priority-list 'ignore))
37 (cond ((and (featurep 'xemacs) (featurep 'mule))
38        (if (memq 'shift-jis (coding-priority-list))
39            (set-coding-priority-list
40             (nconc (delq 'shift-jis (coding-priority-list)) '(shift-jis)))))
41       ((boundp 'MULE)
42        (put '*coding-category-sjis* 'priority (length *predefined-category*)))
43       ((featurep 'mule)
44        (if (memq 'coding-category-sjis coding-category-list)
45            (set-coding-priority
46             (nconc (delq 'coding-category-sjis coding-category-list)
47                    '(coding-category-sjis))))))
48
49 (fset 'facep 'ignore)
50
51 (require 'cl)
52
53 ;; cl functions.
54 (define-compiler-macro mapc (&whole form fn seq &rest rest)
55   (if (and (fboundp 'mapc)
56            (subrp (symbol-function 'mapc)))
57       form
58     (if rest
59         `(let* ((fn ,fn)
60                 (seq ,seq)
61                 (args (cons seq ,rest))
62                 (m (apply (function min) (mapcar (function length) args)))
63                 (n 0))
64            (while (< n m)
65              (apply fn (mapcar (function (lambda (arg) (nth n arg))) args))
66              (setq n (1+ n)))
67            seq)
68       `(let ((seq ,seq))
69          (mapcar ,fn seq)
70          seq))))
71
72 (define-compiler-macro last (&whole form x &optional n)
73   (if (and (fboundp 'last)
74            (subrp (symbol-function 'last)))
75       form
76     (if n
77         `(let* ((x ,x)
78                 (n ,n)
79                 (m 0)
80                 (p x))
81            (while (consp p)
82              (incf m)
83              (pop p))
84            (if (<= n 0)
85                p
86              (if (< n m)
87                  (nthcdr (- m n) x)
88                x)))
89       `(let ((x ,x))
90          (while (consp (cdr x))
91            (pop x))
92          x))))
93
94 ;; If we are building w3 in a different directory than the source
95 ;; directory, we must read *.el from source directory and write *.elc
96 ;; into the building directory.  For that, we define this function
97 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
98 (defun byte-compile-dest-file (filename)
99   "Convert an Emacs Lisp source file name to a compiled file name.
100  In addition, remove directory name part from FILENAME."
101   (setq filename (byte-compiler-base-file-name filename))
102   (setq filename (file-name-sans-versions filename))
103   (setq filename (file-name-nondirectory filename))
104   (if (memq system-type '(win32 w32 mswindows windows-nt))
105       (setq filename (downcase filename)))
106   (cond ((eq system-type 'vax-vms)
107          (concat (substring filename 0 (string-match ";" filename)) "c"))
108         ((string-match emacs-lisp-file-regexp filename)
109          (concat (substring filename 0 (match-beginning 0)) ".elc"))
110         (t (concat filename ".elc"))))
111
112 (require 'bytecomp)
113
114 (defvar srcdir (or (getenv "srcdir") "."))
115
116 (push srcdir load-path)
117
118 ;; Attempt to pickup the additional load-path(s).
119 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
120 (condition-case err
121     (load "~/.lpath.el" t nil t)
122   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
123
124 (condition-case nil
125     (char-after)
126   (wrong-number-of-arguments
127    ;; Optimize byte code for `char-after'.
128    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
129    (defun byte-optimize-char-after (form)
130      (if (null (cdr form))
131          '(char-after (point))
132        form))
133    (byte-defop-compiler char-after 0-1)))
134
135 (condition-case nil
136     (char-before)
137   (wrong-number-of-arguments
138    ;; Optimize byte code for `char-before'.
139    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
140    (defun byte-optimize-char-before (form)
141      (if (null (cdr form))
142          '(char-before (point))
143        form))))
144
145 ;; `char-after' and `char-before' must be well-behaved before lpath.el
146 ;; is loaded.  Because it requires `poe' via `path-util'.
147 (load (expand-file-name "lpath.el" srcdir) nil t t)
148
149 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
150   ;; Bind defcustom'ed variables.
151   (put 'custom-declare-variable 'byte-hunk-handler
152        'byte-compile-file-form-custom-declare-variable)
153   (defun byte-compile-file-form-custom-declare-variable (form)
154     (if (memq 'free-vars byte-compile-warnings)
155         (setq byte-compile-bound-variables
156               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
157     form))
158
159 ;; Bind functions defined by `defun-maybe'.
160 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
161 (defun byte-compile-file-form-defun-maybe (form)
162   (if (and (not (fboundp (nth 1 form)))
163            (memq 'unresolved byte-compile-warnings))
164       (setq byte-compile-function-environment
165             (cons (cons (nth 1 form)
166                         (cons 'lambda (cdr (cdr form))))
167                   byte-compile-function-environment)))
168   form)
169
170 (condition-case nil
171     :symbol-for-testing-whether-colon-keyword-is-available-or-not
172   (void-variable
173    ;; Bind keywords.
174    (mapcar (lambda (keyword) (set keyword keyword))
175            '(:button-keymap :data :file :mime-handle))))
176
177 ;; Unknown variables and functions.
178 (unless (boundp 'buffer-file-coding-system)
179   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
180 (autoload 'font-lock-set-defaults "font-lock")
181 (unless (fboundp 'coding-system-get)
182   (defalias 'coding-system-get 'ignore))
183 (when (boundp 'MULE)
184   (defalias 'find-coding-system 'ignore))
185 (unless (fboundp 'get-charset-property)
186   (defalias 'get-charset-property 'ignore))
187 (unless (featurep 'xemacs)
188   (defalias 'Custom-make-dependencies 'ignore)
189   (defalias 'toolbar-gnus 'ignore)
190   (defalias 'update-autoloads-from-directory 'ignore))
191 (autoload 'texinfo-parse-line-arg "texinfmt")
192
193 (unless (fboundp 'with-temp-buffer)
194   ;; Pickup some macros.
195   (require 'emu))
196
197 (defalias 'device-sound-enabled-p 'ignore)
198 (defalias 'play-sound-file 'ignore)
199 (defalias 'nndb-request-article 'ignore)
200 (defalias 'efs-re-read-dir 'ignore)
201 (defalias 'ange-ftp-re-read-dir 'ignore)
202 (defalias 'define-mail-user-agent 'ignore)
203
204 (eval-and-compile
205   (unless (string-match "XEmacs" emacs-version)
206     (fset 'get-popup-menu-response 'ignore)
207     (fset 'event-object 'ignore)
208     (fset 'x-defined-colors 'ignore)
209     (fset 'read-color 'ignore)))
210
211 (defun dgnushack-compile (&optional warn)
212   ;;(setq byte-compile-dynamic t)
213   (unless warn
214     (setq byte-compile-warnings
215           '(free-vars unresolved callargs redefine)))
216   (unless (locate-library "cus-edit")
217     (error "You do not seem to have Custom installed.
218 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
219 You also then need to add the following to the lisp/dgnushack.el file:
220
221      (push \"~/lisp/custom\" load-path)
222
223 Modify to suit your needs."))
224   (let ((files (delete "dgnuspath.el"
225                        (directory-files srcdir nil "^[^=].*\\.el$")))
226         (xemacs (string-match "XEmacs" emacs-version))
227         ;;(byte-compile-generate-call-tree t)
228         file elc)
229     (condition-case ()
230         (require 'w3-forms)
231       (error
232        (dolist (file '("nnweb.el" "nnlistserv.el" "nnultimate.el"
233                        "nnslashdot.el" "nnwarchive.el" "webmail.el"))
234          (setq files (delete file files)))))
235     (condition-case ()
236         (require 'bbdb)
237       (error (setq files (delete "gnus-bbdb.el" files))))
238     (while (setq file (pop files))
239       (unless (or (and (not xemacs)
240                        (member file
241                                '("gnus-xmas.el" "gnus-picon.el"
242                                  "messagexmas.el" "nnheaderxm.el"
243                                  "smiley.el" "x-overlay.el")))
244                   (and (string-equal file "md5.el")
245                        (not (and (fboundp 'md5)
246                                  (subrp (symbol-function 'md5))))))
247         (setq file (expand-file-name file srcdir))
248         (when (or (not (file-exists-p (setq elc (concat file "c"))))
249                   (file-newer-than-file-p file elc))
250           (ignore-errors
251             (byte-compile-file file)))))))
252
253 (defun dgnushack-recompile ()
254   (require 'gnus)
255   (byte-recompile-directory "." 0))
256
257 \f
258 ;; Avoid byte-compile warnings.
259 (defvar gnus-product-name)
260 (defvar early-package-load-path)
261 (defvar early-packages)
262 (defvar last-package-load-path)
263 (defvar last-packages)
264 (defvar late-package-load-path)
265 (defvar late-packages)
266
267 (defconst dgnushack-info-file-regexp
268   (concat "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)"
269           "\\.info\\(-[0-9]+\\)?$"))
270
271 (defconst dgnushack-texi-file-regexp
272   "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)\\.texi$")
273
274 (defun dgnushack-make-package ()
275   (require 'gnus)
276   (let* ((product-name (downcase gnus-product-name))
277          (lisp-dir (concat "lisp/" product-name "/"))
278          make-backup-files)
279
280     (message "Updating autoloads for directory %s..." default-directory)
281     (let ((generated-autoload-file "auto-autoloads.el")
282           noninteractive
283           (omsg (symbol-function 'message)))
284       (defun message (fmt &rest args)
285         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
286                     (file-exists-p (file-name-nondirectory (car args))))
287                (funcall omsg fmt (file-name-nondirectory (car args))))
288               ((string-equal "No autoloads found in %s" fmt))
289               ((string-equal "Generating autoloads for %s...done" fmt))
290               (t (apply omsg fmt args))))
291       (unwind-protect
292           (update-autoloads-from-directory default-directory)
293         (fset 'message omsg)))
294     (byte-compile-file "auto-autoloads.el")
295
296     (with-temp-buffer
297       (let ((standard-output (current-buffer)))
298         (Custom-make-dependencies "."))
299       (message (buffer-string)))
300     (require 'cus-load)
301     (byte-compile-file "custom-load.el")
302
303     (message "Generating MANIFEST.%s for the package..." product-name)
304     (with-temp-buffer
305       (insert "pkginfo/MANIFEST." product-name "\n"
306               lisp-dir
307               (mapconcat
308                'identity
309                (sort (delete "dgnuspath.el"
310                              (delete "patchs.elc"
311                                      (directory-files "." nil "\\.elc?$")))
312                      'string-lessp)
313                (concat "\n" lisp-dir))
314               "\ninfo/"
315               (mapconcat
316                'identity
317                (sort (directory-files "../texi/"
318                                       nil dgnushack-info-file-regexp)
319                      'string-lessp)
320                "\ninfo/")
321               "\n")
322       (write-file (concat "../MANIFEST." product-name)))))
323
324 (defun dgnushack-install-package ()
325   (let ((package-dir (car command-line-args-left))
326         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
327     (unless package-dir
328       (when (boundp 'early-packages)
329         (setq dirs (delq nil (append (when early-package-load-path
330                                        early-packages)
331                                      (when late-package-load-path
332                                        late-packages)
333                                      (when last-package-load-path
334                                        last-packages))))
335         (while (and dirs (not package-dir))
336           (when (file-exists-p (car dirs))
337             (setq package-dir (car dirs)
338                   dirs (cdr dirs))))))
339     (unless package-dir
340       (error "%s" "
341 You must specify the name of the package path as follows:
342
343 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
344 "
345              ))
346     (setq info-dir (expand-file-name "info/" package-dir)
347           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
348     (require 'gnus)
349     (setq product-name (downcase gnus-product-name)
350           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
351                                      package-dir)
352           manifest (concat "MANIFEST." product-name))
353
354     (unless (file-directory-p lisp-dir)
355       (make-directory lisp-dir t))
356     (unless (file-directory-p info-dir)
357       (make-directory info-dir))
358     (unless (file-directory-p pkginfo-dir)
359       (make-directory pkginfo-dir))
360
361     (setq files
362           (sort (delete "dgnuspath.el"
363                         (delete "dgnuspath.elc"
364                                 (directory-files "." nil "\\.elc?$")))
365                 'string-lessp))
366     (mapcar
367      (lambda (file)
368        (unless (member file files)
369          (setq file (expand-file-name file lisp-dir))
370          (message "Removing %s..." file)
371          (condition-case nil
372              (delete-file file)
373            (error nil))))
374      (directory-files lisp-dir nil nil nil t))
375     (mapcar
376      (lambda (file)
377        (message "Copying %s to %s..." file lisp-dir)
378        (copy-file file (expand-file-name file lisp-dir) t t))
379      files)
380
381     (mapcar
382      (lambda (file)
383        (message "Copying ../texi/%s to %s..." file info-dir)
384        (copy-file (expand-file-name file "../texi/")
385                   (expand-file-name file info-dir)
386                   t t))
387      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
388            'string-lessp))
389
390     (message "Copying ../%s to %s..." manifest pkginfo-dir)
391     (copy-file (expand-file-name manifest "../")
392                (expand-file-name manifest pkginfo-dir) t t)
393
394     (message "Done")))
395
396 (defun dgnushack-texi-add-suffix-and-format ()
397   (dgnushack-texi-format t))
398
399 (defun dgnushack-texi-format (&optional addsuffix)
400   (if (not noninteractive)
401       (error "batch-texinfo-format may only be used -batch."))
402   (require 'texinfmt)
403   (let ((auto-save-default nil)
404         (find-file-run-dired nil)
405         coding-system-for-write)
406     (let ((error 0)
407           file
408           (files ()))
409       (while command-line-args-left
410         (setq file (expand-file-name (car command-line-args-left)))
411         (cond ((not (file-exists-p file))
412                (message ">> %s does not exist!" file)
413                (setq error 1
414                      command-line-args-left (cdr command-line-args-left)))
415               ((file-directory-p file)
416                (setq command-line-args-left
417                      (nconc (directory-files file)
418                             (cdr command-line-args-left))))
419               (t
420                (setq files (cons file files)
421                      command-line-args-left (cdr command-line-args-left)))))
422       (while files
423         (setq file (car files)
424               files (cdr files))
425         (condition-case err
426             (progn
427               (if buffer-file-name (kill-buffer (current-buffer)))
428               (find-file file)
429               (setq coding-system-for-write buffer-file-coding-system)
430               (when (and addsuffix
431                          (re-search-forward
432                           "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
433                          (not (string-match "\\.info$" (match-string 1))))
434                 (insert ".info"))
435               (buffer-disable-undo (current-buffer))
436               ;; process @include before updating node
437               ;; This might produce some problem if we use @lowersection or
438               ;; such.
439               (let ((input-directory default-directory)
440                     (texinfo-command-end))
441                 (while (re-search-forward "^@include" nil t)
442                   (setq texinfo-command-end (point))
443                   (let ((filename (concat input-directory
444                                           (texinfo-parse-line-arg))))
445                     (re-search-backward "^@include")
446                     (delete-region (point) (save-excursion
447                                              (forward-line 1)
448                                              (point)))
449                     (message "Reading included file: %s" filename)
450                     (save-excursion
451                       (save-restriction
452                         (narrow-to-region
453                          (point)
454                          (+ (point)
455                             (car (cdr (insert-file-contents filename)))))
456                         (goto-char (point-min))
457                         ;; Remove `@setfilename' line from included file,
458                         ;; if any, so @setfilename command not duplicated.
459                         (if (re-search-forward "^@setfilename"
460                                                (save-excursion
461                                                  (forward-line 100)
462                                                  (point))
463                                                t)
464                             (progn
465                               (beginning-of-line)
466                               (delete-region (point) (save-excursion
467                                                        (forward-line 1)
468                                                        (point))))))))))
469               (texinfo-mode)
470               (texinfo-every-node-update)
471               (set-buffer-modified-p nil)
472               (message "texinfo formatting %s..." file)
473               (texinfo-format-buffer nil)
474               (if (buffer-modified-p)
475                   (progn (message "Saving modified %s" (buffer-file-name))
476                          (save-buffer))))
477           (error
478            (message ">> Error: %s" (prin1-to-string err))
479            (message ">>  point at")
480            (let ((s (buffer-substring (point)
481                                       (min (+ (point) 100)
482                                            (point-max))))
483                  (tem 0))
484              (while (setq tem (string-match "\n+" s tem))
485                (setq s (concat (substring s 0 (match-beginning 0))
486                                "\n>>  "
487                                (substring s (match-end 0)))
488                      tem (1+ tem)))
489              (message ">>  %s" s))
490            (setq error 1))))
491       (kill-emacs error))))
492
493 ;;; dgnushack.el ends here