マクロでdocstringを評価する

Sep 27 2020

のようなdefun(またはdefmacroそれ自体の)マクロにはオプションのdocstring引数がありますが、それらはマクロであり関数でdocstringはないため、評価されず、そのまま使用されます。

docstringこのようなものの代わりに-を評価する必要があります

"does a, then b, then c" ; b is hardcoded

代わりにこのようなものを置くことができます-

(concat "does a, then " (getenv "<some-shell-variable>") " , then c") ; b isn't hardcoded

それは、の定義を微調整することなく、これを行うことは可能ですdefunとdefmacro?

微調整することで、私は定義を上書きしますが、同じように新しいマクロを書くわけではないprefix-defunとprefix-defmacroの既存の定義に基づくdefunと、demacroドキュメント文字列は、のすべてのインスタンスを置き換えることによって評価され、除いて、docstringと(eval docstring)。

回答

1 Drew Sep 28 2020 at 03:33

(動的に生成されるかどうかに関係なく)任意の関数、変数、または面のドキュメント文字列を任意の文字列に設定できます。

関数の場合、プロパティの値としてfoo必要な文字列をシンボルfooに配置しますfunction-documentation。

(defun foo () ; `foo' gets no doc string from this defun.
  (interactive)
  (message "FOOOOOOOO"))

(put 'foo 'function-documentation "I'm `foo'.") ; Give function `foo' a doc string.
(put 'foo 'function-documentation "I'm NEW `foo' doc.") ; Redefine the doc string.

defvarまたはdefcustomについても同じです-シンボルプロパティを使用しますvariable-documentation:

(defvar foo 42) ; Variable `foo' has no doc string.

(put 'foo 'variable-documentation "I'm `foo'.") ; Give var `foo' a doc string.
(put 'foo 'variable-documentation "I'm NEW `foo' .") ; Redefine the doc string.

Elispのマニュアル、ノードのドキュメントの基本とそのサブノードを参照してください。

同じことdefface-シンボルプロパティを使用しますvariable-documentation。ただしdefface、ドキュメント文字列が必要です。それでも、関数や変数の場合と同様に、動的に変更できます。

(defface foo '((t :foreground "Red")) "0000") ; Doc string "0000".

(put 'foo 'face-documentation "11111") ; Change doc string to "1111".
2 phils Sep 27 2020 at 19:47
(defun foo ()
  (:documentation (concat "does a, then " (getenv "<some-shell-variable>") " , then c"))
  ...BODY...)

これは事実上複製です生成されたドキュメント文字列をラムダに添付することは可能ですか?ただし、質問は非常にlambda指向的であるため、Stefanの回答では、defun。を使用して同じことができるとは指摘されていません。

docstringをevalオンデマンドで(つまり、要求するたびに)実行する必要がある場合は、それも実行できますが、:documentation。を使用しないようです。それを行うためのいくつかのオプションについては、前の質問に対する私の答えを参照してください。