Sync up with Pterodactyl Gnus v0.98.
[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 (require 'bytecomp)
53
54 ;; Attempt to pickup the additional load-path(s).
55 (load (expand-file-name "./dgnuspath.el") nil nil t)
56 (condition-case err
57     (load "~/.lpath.el" t nil t)
58   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
59
60 (push "." load-path)
61
62 (condition-case nil
63     (char-after)
64   (wrong-number-of-arguments
65    ;; Optimize byte code for `char-after'.
66    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
67    (defun byte-optimize-char-after (form)
68      (if (null (cdr form))
69          '(char-after (point))
70        form))
71    (byte-defop-compiler char-after 0-1)))
72
73 (condition-case nil
74     (char-before)
75   (wrong-number-of-arguments
76    ;; Optimize byte code for `char-before'.
77    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
78    (defun byte-optimize-char-before (form)
79      (if (null (cdr form))
80          '(char-before (point))
81        form))))
82
83 ;; `char-after' and `char-before' must be well-behaved before lpath.el
84 ;; is loaded.  Because it requires `poe' via `path-util'.
85 (load "./lpath.el" nil t)
86
87 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
88   ;; Bind defcustom'ed variables.
89   (put 'custom-declare-variable 'byte-hunk-handler
90        'byte-compile-file-form-custom-declare-variable)
91   (defun byte-compile-file-form-custom-declare-variable (form)
92     (if (memq 'free-vars byte-compile-warnings)
93         (setq byte-compile-bound-variables
94               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
95     form))
96
97 ;; Bind functions defined by `defun-maybe'.
98 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
99 (defun byte-compile-file-form-defun-maybe (form)
100   (if (and (not (fboundp (nth 1 form)))
101            (memq 'unresolved byte-compile-warnings))
102       (setq byte-compile-function-environment
103             (cons (cons (nth 1 form)
104                         (cons 'lambda (cdr (cdr form))))
105                   byte-compile-function-environment)))
106   form)
107
108 (condition-case nil
109     :symbol-for-testing-whether-colon-keyword-is-available-or-not
110   (void-variable
111    ;; Bind keywords.
112    (mapcar (lambda (keyword) (set keyword keyword))
113            '(:button-keymap :data :file :mime-handle))))
114
115 ;; Unknown variables and functions.
116 (unless (boundp 'buffer-file-coding-system)
117   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
118 (autoload 'font-lock-set-defaults "font-lock")
119 (unless (fboundp 'coding-system-get)
120   (defalias 'coding-system-get 'ignore))
121 (when (boundp 'MULE)
122   (defalias 'find-coding-system 'ignore))
123 (unless (fboundp 'get-charset-property)
124   (defalias 'get-charset-property 'ignore))
125 (unless (featurep 'xemacs)
126   (defalias 'Custom-make-dependencies 'ignore)
127   (defalias 'toolbar-gnus 'ignore)
128   (defalias 'update-autoloads-from-directory 'ignore))
129 (autoload 'texinfo-parse-line-arg "texinfmt")
130
131 (unless (fboundp 'with-temp-buffer)
132   ;; Pickup some macros.
133   (require 'emu))
134
135 (defalias 'device-sound-enabled-p 'ignore)
136 (defalias 'play-sound-file 'ignore)
137 (defalias 'nndb-request-article 'ignore)
138 (defalias 'efs-re-read-dir 'ignore)
139 (defalias 'ange-ftp-re-read-dir 'ignore)
140 (defalias 'define-mail-user-agent 'ignore)
141
142 (eval-and-compile
143   (unless (string-match "XEmacs" emacs-version)
144     (fset 'get-popup-menu-response 'ignore)
145     (fset 'event-object 'ignore)
146     (fset 'x-defined-colors 'ignore)
147     (fset 'read-color 'ignore)))
148
149 (defun dgnushack-compile (&optional warn)
150   ;;(setq byte-compile-dynamic t)
151   (unless warn
152     (setq byte-compile-warnings
153           '(free-vars unresolved callargs redefine)))
154   (unless (locate-library "cus-edit")
155     (error "You do not seem to have Custom installed.
156 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
157 You also then need to add the following to the lisp/dgnushack.el file:
158
159      (push \"~/lisp/custom\" load-path)
160
161 Modify to suit your needs."))
162   (let ((files (delete "dgnuspath.el"
163                        (directory-files "." nil "^[^=].*\\.el$")))
164         (xemacs (string-match "XEmacs" emacs-version))
165         ;;(byte-compile-generate-call-tree t)
166         file elc)
167     (condition-case ()
168         (require 'w3-forms)
169       (error (setq files (delete "nnweb.el" (delete "nnlistserv.el" files)))))
170     (condition-case ()
171         (require 'bbdb)
172       (error (setq files (delete "gnus-bbdb.el" files))))
173     (while (setq file (pop files))
174       (when (or (and (not xemacs)
175                      (not (member file '("gnus-xmas.el" "gnus-picon.el"
176                                          "messagexmas.el" "nnheaderxm.el"
177                                          "smiley.el" "x-overlay.el"))))
178                 (and xemacs
179                      (not (member file '("md5.el")))))
180         (when (or (not (file-exists-p (setq elc (concat file "c"))))
181                   (file-newer-than-file-p file elc))
182           (ignore-errors
183             (byte-compile-file file)))))))
184
185 (defun dgnushack-recompile ()
186   (require 'gnus)
187   (byte-recompile-directory "." 0))
188
189 \f
190 ;; Avoid byte-compile warnings.
191 (defvar gnus-product-name)
192 (defvar early-package-load-path)
193 (defvar early-packages)
194 (defvar last-package-load-path)
195 (defvar last-packages)
196 (defvar late-package-load-path)
197 (defvar late-packages)
198
199 (defconst dgnushack-info-file-regexp
200   (concat "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)"
201           "\\.info\\(-[0-9]+\\)?$"))
202
203 (defconst dgnushack-texi-file-regexp
204   "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)\\.texi$")
205
206 (defun dgnushack-make-package ()
207   (require 'gnus)
208   (let* ((product-name (downcase gnus-product-name))
209          (lisp-dir (concat "lisp/" product-name "/"))
210          make-backup-files)
211
212     (message "Updating autoloads for directory %s..." default-directory)
213     (let ((generated-autoload-file "auto-autoloads.el")
214           noninteractive
215           (omsg (symbol-function 'message)))
216       (defun message (fmt &rest args)
217         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
218                     (file-exists-p (file-name-nondirectory (car args))))
219                (funcall omsg fmt (file-name-nondirectory (car args))))
220               ((string-equal "No autoloads found in %s" fmt))
221               ((string-equal "Generating autoloads for %s...done" fmt))
222               (t (apply omsg fmt args))))
223       (unwind-protect
224           (update-autoloads-from-directory default-directory)
225         (fset 'message omsg)))
226     (byte-compile-file "auto-autoloads.el")
227
228     (with-temp-buffer
229       (let ((standard-output (current-buffer)))
230         (Custom-make-dependencies "."))
231       (message (buffer-string)))
232     (require 'cus-load)
233     (byte-compile-file "custom-load.el")
234
235     (message "Generating MANIFEST.%s for the package..." product-name)
236     (with-temp-buffer
237       (insert "pkginfo/MANIFEST." product-name "\n"
238               lisp-dir
239               (mapconcat
240                'identity
241                (sort (delete "dgnuspath.el"
242                              (delete "patchs.elc"
243                                      (directory-files "." nil "\\.elc?$")))
244                      'string-lessp)
245                (concat "\n" lisp-dir))
246               "\ninfo/"
247               (mapconcat
248                'identity
249                (sort (directory-files "../texi/"
250                                       nil dgnushack-info-file-regexp)
251                      'string-lessp)
252                "\ninfo/")
253               "\n")
254       (write-file (concat "../MANIFEST." product-name)))))
255
256 (defun dgnushack-install-package ()
257   (let ((package-dir (car command-line-args-left))
258         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
259     (unless package-dir
260       (when (boundp 'early-packages)
261         (setq dirs (delq nil (append (when early-package-load-path
262                                        early-packages)
263                                      (when late-package-load-path
264                                        late-packages)
265                                      (when last-package-load-path
266                                        last-packages))))
267         (while (and dirs (not package-dir))
268           (when (file-exists-p (car dirs))
269             (setq package-dir (car dirs)
270                   dirs (cdr dirs))))))
271     (unless package-dir
272       (error "%s" "
273 You must specify the name of the package path as follows:
274
275 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
276 "
277              ))
278     (setq info-dir (expand-file-name "info/" package-dir)
279           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
280     (require 'gnus)
281     (setq product-name (downcase gnus-product-name)
282           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
283                                      package-dir)
284           manifest (concat "MANIFEST." product-name))
285
286     (unless (file-directory-p lisp-dir)
287       (make-directory lisp-dir t))
288     (unless (file-directory-p info-dir)
289       (make-directory info-dir))
290     (unless (file-directory-p pkginfo-dir)
291       (make-directory pkginfo-dir))
292
293     (setq files
294           (sort (delete "dgnuspath.el"
295                         (delete "dgnuspath.elc"
296                                 (directory-files "." nil "\\.elc?$")))
297                 'string-lessp))
298     (mapcar
299      (lambda (file)
300        (unless (member file files)
301          (setq file (expand-file-name file lisp-dir))
302          (message "Removing %s..." file)
303          (condition-case nil
304              (delete-file file)
305            (error nil))))
306      (directory-files lisp-dir nil nil nil t))
307     (mapcar
308      (lambda (file)
309        (message "Copying %s to %s..." file lisp-dir)
310        (copy-file file (expand-file-name file lisp-dir) t t))
311      files)
312
313     (mapcar
314      (lambda (file)
315        (message "Copying ../texi/%s to %s..." file info-dir)
316        (copy-file (expand-file-name file "../texi/")
317                   (expand-file-name file info-dir)
318                   t t))
319      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
320            'string-lessp))
321
322     (message "Copying ../%s to %s..." manifest pkginfo-dir)
323     (copy-file (expand-file-name manifest "../")
324                (expand-file-name manifest pkginfo-dir) t t)
325
326     (message "Done")))
327
328 (defun dgnushack-texi-add-suffix-and-format ()
329   (dgnushack-texi-format t))
330
331 (defun dgnushack-texi-format (&optional addsuffix)
332   (if (not noninteractive)
333       (error "batch-texinfo-format may only be used -batch."))
334   (require 'texinfmt)
335   (let ((auto-save-default nil)
336         (find-file-run-dired nil)
337         coding-system-for-write)
338     (let ((error 0)
339           file
340           (files ()))
341       (while command-line-args-left
342         (setq file (expand-file-name (car command-line-args-left)))
343         (cond ((not (file-exists-p file))
344                (message ">> %s does not exist!" file)
345                (setq error 1
346                      command-line-args-left (cdr command-line-args-left)))
347               ((file-directory-p file)
348                (setq command-line-args-left
349                      (nconc (directory-files file)
350                             (cdr command-line-args-left))))
351               (t
352                (setq files (cons file files)
353                      command-line-args-left (cdr command-line-args-left)))))
354       (while files
355         (setq file (car files)
356               files (cdr files))
357         (condition-case err
358             (progn
359               (if buffer-file-name (kill-buffer (current-buffer)))
360               (find-file file)
361               (setq coding-system-for-write buffer-file-coding-system)
362               (when (and addsuffix
363                          (re-search-forward
364                           "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
365                          (not (string-match "\\.info$" (match-string 1))))
366                 (insert ".info"))
367               (buffer-disable-undo (current-buffer))
368               ;; process @include before updating node
369               ;; This might produce some problem if we use @lowersection or
370               ;; such.
371               (let ((input-directory default-directory)
372                     (texinfo-command-end))
373                 (while (re-search-forward "^@include" nil t)
374                   (setq texinfo-command-end (point))
375                   (let ((filename (concat input-directory
376                                           (texinfo-parse-line-arg))))
377                     (re-search-backward "^@include")
378                     (delete-region (point) (save-excursion
379                                              (forward-line 1)
380                                              (point)))
381                     (message "Reading included file: %s" filename)
382                     (save-excursion
383                       (save-restriction
384                         (narrow-to-region
385                          (point)
386                          (+ (point)
387                             (car (cdr (insert-file-contents filename)))))
388                         (goto-char (point-min))
389                         ;; Remove `@setfilename' line from included file,
390                         ;; if any, so @setfilename command not duplicated.
391                         (if (re-search-forward "^@setfilename"
392                                                (save-excursion
393                                                  (forward-line 100)
394                                                  (point))
395                                                t)
396                             (progn
397                               (beginning-of-line)
398                               (delete-region (point) (save-excursion
399                                                        (forward-line 1)
400                                                        (point))))))))))
401               (texinfo-mode)
402               (texinfo-every-node-update)
403               (set-buffer-modified-p nil)
404               (message "texinfo formatting %s..." file)
405               (texinfo-format-buffer nil)
406               (if (buffer-modified-p)
407                   (progn (message "Saving modified %s" (buffer-file-name))
408                          (save-buffer))))
409           (error
410            (message ">> Error: %s" (prin1-to-string err))
411            (message ">>  point at")
412            (let ((s (buffer-substring (point)
413                                       (min (+ (point) 100)
414                                            (point-max))))
415                  (tem 0))
416              (while (setq tem (string-match "\n+" s tem))
417                (setq s (concat (substring s 0 (match-beginning 0))
418                                "\n>>  "
419                                (substring s (match-end 0)))
420                      tem (1+ tem)))
421              (message ">>  %s" s))
422            (setq error 1))))
423       (kill-emacs error))))
424
425 ;;; dgnushack.el ends here