Skip to content

Commit 5d66f21

Browse files
committed
Update how eval cntxt is retrieved and represented
1 parent 487ad5d commit 5d66f21

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

evalator-context.el

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
(require 'eieio)
2929

30+
(defvar evalator-context-to-use nil)
3031
(defvar evalator-context-special-arg-default "") ;; Unicode character x24ba
3132

3233
;; References to data types in the docstrings below are assumed to be elisp types.

evalator-state.el

+10-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@
3838

3939
(defun evalator-state-init (&optional mode)
4040
"Set `evalator-state' to the value of `evalator-state-default'.
41+
Sets the state's context if user has defined a value for evalator-context-to-use.
4142
Sets the state's MODE if necessary and performs any context initialization"
4243
(setq evalator-state (copy-sequence evalator-state-default))
43-
(when mode (evalator-utils-put! evalator-state :mode mode))
44-
(funcall (slot-value (plist-get evalator-state :context) :init)))
44+
(when evalator-context-to-use ;; might have a global or buffer local value
45+
(evalator-utils-put! evalator-state :context evalator-context-to-use))
46+
(when mode
47+
(evalator-utils-put! evalator-state :mode mode))
48+
(funcall (slot-value (evalator-state-context) :init)))
49+
50+
(defun evalator-state-context ()
51+
"Return the state's context object."
52+
(plist-get evalator-state :context))
4553

4654
(provide 'evalator-state)
4755

evalator.el

+7-7
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ Elisp function from within an evalator session that uses a different
7575
evaluation context. This action does not transform the candidates."
7676
(interactive)
7777
(let* ((spec-arg-elisp (evalator-context-get-special-arg evalator-context-elisp))
78-
(spec-arg-curr (evalator-context-get-special-arg (plist-get evalator-state :context)))
78+
(spec-arg-curr (evalator-context-get-special-arg (evalator-state-context)))
7979
(expr-str (if (equal spec-arg-elisp spec-arg-curr)
8080
helm-pattern
8181
(replace-regexp-in-string spec-arg-curr spec-arg-elisp helm-pattern))))
8282
(condition-case err
83-
(message
83+
(message
8484
(prin1-to-string
8585
(evalator-context-elisp-transform-candidates (evalator-get-candidates) expr-str nil)))
8686
(error
@@ -109,15 +109,15 @@ to the entire candidate selection. Second, the current expression is
109109
evaluated only once to produce a single candidate. This action is
110110
used for when you need to produce an aggregate result."
111111
(interactive)
112-
(let* ((f (slot-value (plist-get evalator-state :context) :transform-candidates))
112+
(let* ((f (slot-value (evalator-state-context) :transform-candidates))
113113
(expr-str helm-pattern)
114114
(args (list (evalator-get-candidates) expr-str t)))
115115
(evalator-action-confirm-make-or-transform (list f args))))
116116

117117
(defun evalator-action-insert-special-arg ()
118118
"Insert the evalator special arg into the expression prompt."
119119
(interactive)
120-
(insert (evalator-context-get-special-arg (plist-get evalator-state :context))))
120+
(insert (evalator-context-get-special-arg (evalator-state-context))))
121121

122122
(defun evalator-message (msg)
123123
"Output MSG and append a newline and an instruction to continue."
@@ -192,8 +192,8 @@ accept's an optional ERR-HANDLER to pass to `evalator-try-context-f'."
192192
(with-helm-current-buffer
193193
(if f-and-args
194194
(apply 'evalator-try-context-f (append f-and-args (list err-handler)))
195-
(let* ((make-f (slot-value (plist-get evalator-state :context) :make-candidates))
196-
(transform-f (slot-value (plist-get evalator-state :context) :transform-candidates))
195+
(let* ((make-f (slot-value (evalator-state-context) :make-candidates))
196+
(transform-f (slot-value (evalator-state-context) :transform-candidates))
197197
(expr-str helm-pattern)
198198
(mode (plist-get evalator-state :mode))
199199
(f-and-args (if (equal 0 (evalator-history-index))
@@ -227,7 +227,7 @@ accept's an optional ERR-HANDLER to pass to `evalator-try-context-f'."
227227
(interactive)
228228
(if (equal :explicit (plist-get evalator-state :mode))
229229
(insert (funcall
230-
(slot-value (plist-get evalator-state :context) :make-equiv-expr)
230+
(slot-value (evalator-state-context) :make-equiv-expr)
231231
(evalator-history-expression-chain)))
232232
(message "Error: This command is only allowed when the last evalator session was started with `evalator-explicit'.")))
233233

test/evalator-state-test.el

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727

2828
(require 'ert)
29+
(require 'el-mock)
2930
(require 'evalator-state)
3031

3132
(ert-deftest evalator-state-init-test ()
@@ -41,5 +42,9 @@
4142
(should-not (equal evalator-state-default evalator-state))
4243
(should (equal :explicit (plist-get evalator-state :mode))))))
4344

45+
(ert-deftest evalator-state-context-test ()
46+
(let* ((evalator-state (list :context "foo")))
47+
(should (equal "foo"
48+
(evalator-state-context)))))
4449
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4550
;;; evalator-state-test.el ends here

0 commit comments

Comments
 (0)