comparison lisp/efs/efs-ovwrt.el @ 22:8fc7fe29b841 r19-15b94

Import from CVS: tag r19-15b94
author cvs
date Mon, 13 Aug 2007 08:50:29 +0200
parents
children 7e54bd776075
comparison
equal deleted inserted replaced
21:b88636d63495 22:8fc7fe29b841
1 ;; -*-Emacs-Lisp-*-
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;
4 ;; File: efs-ovwrt.el
5 ;; Release: $efs release: 1.15 $
6 ;; Version: $Revision: 1.1 $
7 ;; RCS:
8 ;; Description: Utilities for overwriting functions with new definitions.
9 ;; Author: Andy Norman <ange@hplb.hpl.hp.com>
10 ;; Modified: Sun Nov 27 18:40:20 1994 by sandy on gandalf
11 ;;
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
14 ;;; Although used by efs, these utilities could be of general use to other
15 ;;; packages too. Keeping them separate from the main efs program
16 ;;; makes it easier for other programs to require them.
17
18 (provide 'efs-ovwrt)
19
20 (defconst efs-ovwrt-version
21 (concat (substring "$efs release: 1.15 $" 14 -2)
22 "/"
23 (substring "$Revision: 1.1 $" 11 -2)))
24
25 (defvar efs-overwrite-fmt
26 "Note: This function has been modified to work with %s.")
27
28 ;; Make the byte compiler happy.
29 (defvar file-name-handler-alist)
30 (defvar inhibit-file-name-handlers)
31 (defvar inhibit-file-name-operation)
32
33 (defun efs-safe-documentation (fun)
34 "A documentation function that isn't quite as fragile."
35 (condition-case ()
36 (documentation fun)
37 (error nil)))
38
39 (defun efs-overwrite-fn (package fun &optional newfun)
40 "Overwrites a function with a new definition from PACKAGE.
41 PACKAGE should be a string. The the function to be overwritten is FUN.
42 The new definition is obtained from the optional NEWFUN. If ommitted,
43 NEWFUN is taken to be PACKAGE-FUN. The original definition is stored in
44 PACKAGE-real-FUN. The original documentation is placed on the new
45 definition suitably augmented."
46 (let* ((name (symbol-name fun))
47 (saved (intern (concat package "-real-" name)))
48 (new (or newfun (intern (concat package "-" name))))
49 (nfun (symbol-function new))
50 (exec-directory (if (or (equal (nth 3 command-line-args) "dump")
51 (equal (nth 4 command-line-args) "dump"))
52 "../etc/"
53 exec-directory)))
54
55 (while (symbolp nfun)
56 (setq nfun (symbol-function nfun)))
57
58 ;; Interpose the new function between the function symbol and the
59 ;; original definition of the function symbol AT TIME OF FIRST LOAD.
60 ;; We must only redefine the symbol-function of FUN the very first
61 ;; time, to avoid blowing away stuff that overloads FUN after this.
62
63 ;; We direct the function symbol to the new function symbol
64 ;; rather than function definition to allow reloading of this file or
65 ;; redefining of the individual function (e.g., during debugging)
66 ;; later after some other code has been loaded on top of our stuff.
67
68 (or (fboundp saved)
69 (progn
70 (fset saved (symbol-function fun))
71 (fset fun new)))
72
73 ;; Rewrite the doc string on the new function. This should
74 ;; be done every time the file is loaded (or a function is redefined),
75 ;; because the underlying overloaded function may have changed its doc
76 ;; string.
77
78 (let* ((doc-str (efs-safe-documentation saved))
79 (ndoc-str (concat doc-str (and doc-str "\n")
80 (format efs-overwrite-fmt package))))
81
82 (cond ((listp nfun)
83 ;; Probe to test whether function is in preloaded read-only
84 ;; memory, and if so make writable copy:
85 (condition-case nil
86 (setcar nfun (car nfun))
87 (error
88 (setq nfun (copy-sequence nfun)) ; shallow copy only
89 (fset new nfun)))
90 (let ((ndoc-cdr (nthcdr 2 nfun)))
91 (if (stringp (car ndoc-cdr))
92 ;; Replace the existing docstring.
93 (setcar ndoc-cdr ndoc-str)
94 ;; There is no docstring. Insert the overwrite msg.
95 (setcdr ndoc-cdr (cons (car ndoc-cdr) (cdr ndoc-cdr)))
96 (setcar ndoc-cdr (format efs-overwrite-fmt package)))))
97 (t
98 ;; it's an emacs19 compiled-code object
99 (let ((new-code (append nfun nil))) ; turn it into a list
100 (if (nthcdr 4 new-code)
101 (setcar (nthcdr 4 new-code) ndoc-str)
102 (setcdr (nthcdr 3 new-code) (cons ndoc-str nil)))
103 (fset new (apply 'make-byte-code new-code))))))))
104
105
106 ;;; end of efs-ovwrt.el