annotate lisp/efs/efs-ovwrt.el @ 149:538048ae2ab8 r20-3b1

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