2

I can define a custom option with:

(transient-define-infix my-option-infix ()
  :description "My Option"
  :class 'transient-option
  :shortarg "-o"
  :argument "--my-option=")

But how can I initialize the value depending of the current buffer ?

Balaïtous
  • 173
  • 7

1 Answers1

4

SECOND EDIT

If you have saved the prefix its state using C-x s or C-x C-s, then the prefix value is taken from transient-values variable or from the transient-values-file. To set the value as explained in this answer, make sure that you clear the value for the prefix from this variable and/or file.

FIRST EDIT

In the original code, the value of the :value keyword gets evaluated when the prefix is defined, so it sets the value to a fixed buffer.

To set the value at the moment of running the prefix, replace the :value keyword (line) in the prefix definition with the following line:

:init-value 'my-prefix-init

and define the my-prefix-init function as follows:

(defun my-prefix-init (obj)
  (oset obj value `(,(concat "--foo=" (buffer-name)))))

END EDIT

It is, not very clearly, described in this reddit post.

I guess this is best answered with an example. I have commented out the simple way of setting the value and replaced it by a backquote syntax method to show how to set the current buffer name:

(transient-define-prefix test ()
  ;; :value '("--foo=bar")
  :value `(,(concat "--foo=" (buffer-name)))
  [(test-option-infix)]
  [("p" "print value" print-essentials)])

(transient-define-infix test-option-infix ()
  :class 'transient-option
  :key "-f"
  :description "My option"
  :argument "--foo="
  :choices '("some-alt-value" "etc"))

You can retrieve the value directly from the infix-object as follows.

(defun print-essentials ()
  (interactive)
  (pp (transient-args 'test))
  (pp (transient-arg-value "--foo=" (transient-args 'test))))

To read more about this solution I refer to the reddit post. And the documentation of EIEIO (as I am not very familiar with neither transient nor EIEIO).

dalanicolai
  • 6,108
  • 7
  • 23