summaryrefslogtreecommitdiff
path: root/lang/elisp/internals/fset.scm
blob: 249db7c912f222da8e4147b2ac86b0b79605636e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(define-module (lang elisp internals fset)
  #:use-module (lang elisp internals evaluation)
  #:use-module (lang elisp internals lambda)
  #:use-module (lang elisp internals signal)
  #:export (fset
	    fref
	    fref/error-if-void
	    elisp-apply
	    interactive-specification
	    not-subr?
	    elisp-export-module))

(define the-variables-module (resolve-module '(lang elisp variables)))

;; By default, Guile GC's unreachable symbols.  So we need to make
;; sure they stay reachable!
(define syms '())

;; elisp-export-module, if non-#f, holds a module to which definitions
;; should be exported under their normal symbol names.  This is used
;; when importing Elisp definitions into Scheme.
(define elisp-export-module (make-fluid))

;; Store the procedure, macro or alias symbol PROC in SYM's function
;; slot.
(define (fset sym proc)
  (or (memq sym syms)
      (set! syms (cons sym syms)))
  (let ((vcell (symbol-fref sym))
	(vsym #f)
	(export-module (fluid-ref elisp-export-module)))
    ;; Playing around with variables and name properties...  For the
    ;; reasoning behind this, see the commentary in (lang elisp
    ;; variables).
    (cond ((procedure? proc)
	   ;; A procedure created from Elisp will already have a name
	   ;; property attached, with value of the form
	   ;; <elisp-defun:NAME> or <elisp-lambda>.  Any other
	   ;; procedure coming through here must be an Elisp primitive
	   ;; definition, so we give it a name of the form
	   ;; <elisp-subr:NAME>.
	   (or (procedure-name proc)
	       (set-procedure-property! proc
					'name
					(symbol-append '<elisp-subr: sym '>)))
	   (set! vsym (procedure-name proc)))
	  ((macro? proc)
	   ;; Macros coming through here must be defmacros, as all
	   ;; primitive special forms are handled directly by the
	   ;; transformer.
	   (set-procedure-property! (macro-transformer proc)
				    'name
				    (symbol-append '<elisp-defmacro: sym '>))
	   (set! vsym (procedure-name (macro-transformer proc))))
	  (else
	   ;; An alias symbol.
	   (set! vsym (symbol-append '<elisp-defalias: sym '>))))
    ;; This is the important bit!
    (if (variable? vcell)
	(variable-set! vcell proc)
	(begin
	  (set! vcell (make-variable proc))
	  (symbol-fset! sym vcell)
	  ;; Playing with names and variables again - see above.
	  (module-add! the-variables-module vsym vcell)
	  (module-export! the-variables-module (list vsym))))
    ;; Export variable to the export module, if non-#f.
    (if (and export-module
	     (or (procedure? proc)
		 (macro? proc)))
	(begin
	  (module-add! export-module sym vcell)
	  (module-export! export-module (list sym))))))

;; Retrieve the procedure or macro stored in SYM's function slot.
;; Note the asymmetry w.r.t. fset: if fref finds an alias symbol, it
;; recursively calls fref on that symbol.  Returns #f if SYM's
;; function slot doesn't contain a valid definition.
(define (fref sym)
  (let ((var (symbol-fref sym)))
    (if (and var (variable? var))
	(let ((proc (variable-ref var)))
	  (cond ((symbol? proc)
		 (fref proc))
		(else
		 proc)))
	#f)))

;; Same as fref, but signals an Elisp error if SYM's function
;; definition is void.
(define (fref/error-if-void sym)
  (or (fref sym)
      (signal 'void-function (list sym))))

;; Maps a procedure to its (interactive ...) spec.
(define interactive-specification (make-object-property))

;; Maps a procedure to #t if it is NOT a built-in.
(define not-subr? (make-object-property))

(define (elisp-apply function . args)
  (apply apply
	 (cond ((symbol? function)
		(fref/error-if-void function))
	       ((procedure? function)
		function)
	       ((and (pair? function)
		     (eq? (car function) 'lambda))
		(eval (transform-lambda/interactive function '<elisp-lambda>)
		      the-root-module))
	       (else
		(signal 'invalid-function (list function))))
	 args))