Add NULL description
[clinton/lisp-on-lines.git] / src / standard-descriptions / edit.lisp
CommitLineData
4358148e 1(in-package :lisp-on-lines)
2
4358148e 3(define-description editable ()
4 ()
5 (:mixinp t))
6
f4efa7ff 7(define-layered-class standard-attribute
8 :in-layer #.(defining-description 'editable)
9 ()
10 ((edit-attribute-p
11 :initform :inherit
12 :layered-accessor attribute-editp
13 :initarg :editp
14 :layered t)
15 (setter
16 :initarg :setter
17 :layered t
18 :accessor attribute-setter
19 :initform nil)
20 (attribute-editor
2548f054 21 :initarg :editor
f4efa7ff 22 :layered t
23 :accessor attribute-editor
2548f054 24 :initform (make-instance 'attribute-editor)
f4efa7ff 25 :documentation "This ones a bit odd")))
26
2548f054 27(defmethod shared-initialize :after ((object standard-attribute)
28 slots &rest args &key input &allow-other-keys)
29
30 (when input
31 (setf (attribute-editor object)
32 (apply #'make-instance (find-editor-class input)
33 input))))
34
35
36(defun find-editor-class (spec)
37 (let ((class (getf spec :class))
38 (type (getf spec :type)))
39 (or class (when
40 (and type (symbolp type))
41 (let ((name (format nil "~A-~A" type 'attribute-editor)))
42 (or (find-class (intern name (symbol-package type)) nil)
43 (find-class (intern name) nil)
44 'string-attribute-editor))))))
f4efa7ff 45
46(defclass attribute-editor ()
a5a635a2 47 ((class :initarg :class)
48 (type :initarg :type
2548f054 49 :initform 'string
50 :accessor attribute-editor-type)
f4efa7ff 51 (parser :initarg :parse-using
52 :initform 'identity
53 :accessor attribute-editor-parsing-function)
54 (prompt :initarg :prompt
2548f054 55 :initform nil)
56 (unbound-value
57 :initarg :unbound-value
58 :initform "")))
59
60
f4efa7ff 61
62(defclass string-attribute-editor (attribute-editor) ())
63(defclass text-attribute-editor (string-attribute-editor) ())
2548f054 64
65(deftype password () 'string)
66
f4efa7ff 67(defclass password-attribute-editor (string-attribute-editor) ())
68
69(defclass number-attribute-editor (attribute-editor) ()
70 (:default-initargs
71 :parse-using 'parse-number:PARSE-NUMBER
72 :type 'number))
73
74(defun parse-attribute-value (attribute value)
75 (funcall (attribute-editor-parsing-function
76 (attribute-editor attribute))
77 value))
78
79(define-layered-function display-attribute-editor (attribute)
80 (:method (attribute)
81 (setf (attribute-value attribute)
82 (funcall (attribute-editor-parsing-function
83 (attribute-editor attribute))
84 (read-line)))))
85
4358148e 86(define-description T ()
87 ((editp :label "Edit by Default?"
88 :value nil
89 :editp nil)
90 (identity :editp nil)
91 (type :editp nil)
92 (class :editp nil))
93 (:in-description editable))
94
b7657b86 95(define-layered-method (setf attribute-value-using-object)
96 :in-layer #.(defining-description 'editable)(value object attribute)
97
98 (let ((setter (attribute-setter attribute)))
99 (if setter
100 (funcall setter value object)
101 (error "No setter in ~A for ~A" attribute object))))
4358148e 102
4358148e 103
f4efa7ff 104(define-layered-function attribute-editp (attribute)
105 (:method (attribute) nil))
4358148e 106
107(define-layered-method attribute-editp
108 :in-layer #.(defining-description 'editable)
f4efa7ff 109 ((attribute standard-attribute))
2548f054 110 (let ((value (attribute-value attribute)))
111 (unless (or (unbound-slot-value-p value)
112 (typep value
113 (attribute-editor-type
114 (attribute-editor attribute))))
115 (return-from attribute-editp nil)))
f4efa7ff 116 (let ((edit? (call-next-method)))
117 (if (eq :inherit edit?)
118 (attribute-value (find-attribute
119 (attribute-description attribute)
120 'editp))
121 edit?)))
4358148e 122
123
f4efa7ff 124(define-layered-method display-attribute-value
4358148e 125 :in-layer #.(defining-description 'editable)
f4efa7ff 126 ((attribute standard-attribute))
127 (if (attribute-editp attribute)
128 (display-attribute-editor attribute)
e8d4fa45 129 (call-next-method)))
4358148e 130
131
f4efa7ff 132
133
134
4358148e 135