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