1

I know this may sound silly but what does defvar do in the code below? Does it define a variable that hold a capture template? or we say that the template is bound to the variable capture-template-contact?

I tried C-h f defvar for help but still don't get it being a non-native speaker of English.

   (defvar my/capture-template-contact
      (concat "* %^{Name}\n"
              ":PROPERTIES:\n"
              ":Created: %U\n"
              ":Birthday: yyyy-mm-dd\n"
              ":Email:\n"
              ":Mobile:\n"
              ":Skype:\n"
              ":Address:\n"
              ":City:\n"
              ":State:\n"
              ":Country:\n"
              ":PostalCode:\n"
              ":Website:\n"
              ":Note:\n"
              ":END:\n") "Contact")
3
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Feb 19, 2023 at 16:56
  • Removed the extraneous code after the defvar expression.
    – Drew
    Commented Feb 19, 2023 at 17:01
  • 1
    maybe it is defining a format string
    – shynur
    Commented Feb 19, 2023 at 17:11

1 Answer 1

1
  1. defvar declares a global variable, more precisely, a variable that is dynamically bound. And it can define that variable, that is, give it an initial value.

  2. If only one value (the variable symbol) is passed to defvar then it does not give the variable a value; it just declares it.

  3. defvar does nothing if the variable it defines already has a value.

Does it define a variable that hold a capture template? or we say that the template is bound to the variable capture-template-contact?

It defines a variable my/capture-template-contact if that's not already defined. It gives it the string value that's the result of that (concat ...) expression. It gives the variable the doc string "Contact". That's all it does. As for your second question, the defvar doesn't answer that question - it says nothing about what the variable it defines means, or how it might be used in some code somewhere. And the variable name is my/capture-template-contact, not capture-template-contact.

The Elisp manual, node Defining Variables, tells you what you need to know about defvar.

3
  • Thank you. I moved all my capture templates in such variables. If you were to write a git log message how would you caption such a change? Use variables for each capture template or Define variables to hold capture templates?
    – Zoli
    Commented Feb 19, 2023 at 17:20
  • That's up to you; I have no advice about that. I'd imagine that you're the one who most needs to understand what your message means.
    – Drew
    Commented Feb 19, 2023 at 19:41
  • Why have you chosen not to customize org-capture-templates? Commented Feb 21, 2023 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.