1

I am new to (doom) Emacs, and I have almost no understanding of Emacs-Lisp yet. I have a simple function I copied from system-crafters blog to quickly insert an org-roam-capture to an inbox file. I am trying to modify it to include my system hostname in the file name, and in the file's tile. The following is the slightly modified and working snippet I use:

(defun my/org-roam-capture-inbox ()
  (interactive)
  (org-roam-capture- :node (org-roam-node-create)
                     :templates '(("i" "inbox" plain "* %?"
                     :if-new (file+head "inbox/main.org" "#+title: Main Inbox\n")))))

I found that the (system-name) function could be used to retrieve the system hostname, and that %var can be used in a string to reference the var value. So, I tried the following:

(defun my/org-roam-capture-inbox ()
  (defvar my-host nil)
  (interactive)
  (setq my-host (system-name))
  (org-roam-capture- :node (org-roam-node-create)
                     :templates '(("i" "inbox" plain "* %?"
                     :if-new (file+head "inbox/%my-host.org" "#+title: %my-host Inbox\n")))))

I also tried to set the defvar part globally meaning, outside of my/org-roam-capture-inbox function. But this attempt simply uses %my-host as a litteral string value.

8
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Apr 29 at 15:06
  • Where did you find that %var can be used in a string to reference the var value? That is generally not true.
    – NickD
    Commented Apr 29 at 15:54
  • I think I should delete this question. First, the comment from Drew seems to indicate that I posted in the wrong place. Second, after giving it more thoughts, I figure I am going all wrong about it. In any other language I kind of know of, I would first build the 'filename' or 'title' string with something like concat, and then try and use it as a variable where it is needed. But above all, I think I should study the basics of elisp like declaring variables or calling functions before I ask for help. I am sorry I asked too quickly, can someone let me know how I should proceed ?
    – lyndhurst
    Commented Apr 30 at 11:38
  • No, @Drew's comment was about the elisp tag: it tends to be misused frequently to the point of meaninglessness, so he deleted that tag and replaced it with org-roam. If you click on the link in his comment, you can read more about what the tag really means. So you posted to the right place. Second, the question seems reasonable: you posted what you tried and asked a question about it. The problem might be that the intersection of the set of people who might be willing to answer and the set of people who know something about org-roam is probably small ...
    – NickD
    Commented Apr 30 at 12:13
  • ... (actually, empty so far - but that might change). So let the question stew for a while: it might take a while but somebody might be motivated or bored by other work to pick it up. However, learning more about Emacs Lisp is definitely a good idea. I would recommend that you start by reading the "Intro" book: in Emacs, type C-h i g(eintr) RET and start reading. It is also available on the Web, but learning to use Info in Emacs is very much worth your while.
    – NickD
    Commented Apr 30 at 12:20

1 Answer 1

1

I finally managed to make it work with org-roam-capture-templates (not with org-capture-templates yet, but this is out of the scope of this thread).

It was actually much less complicated than I believed; I could just inject the string produced by (system-name) using the template expansion syntax %(func) directly where I need it. Using a variable would prevent repeating the code, but I still could not figure out how to do that...

(defun my/org-roam-capture-inbox ()
   (interactive)
   (org-roam-capture- :node (org-roam-node-create)
                      :templates '(("i" "inbox" plain "* %?"
                      :target (file+head "inbox/%(system-name)-inbox.org" "#+title: %(system-name) Inbox")
                      :empty-lines 1))))

2
  • You can use a variable, but you have to worry about interpolating the value in a quoted expression. You can do that with backquote, but you don't need to worry about that just yet: having somebody else (in this case, org-roam-capture-) worry about when the evaluation should happen is much easier. However, backquote is going to bite you eventually: it bites everybody. But if you know about it beforehand, you might be able to take the sting more easily: do C-h i g(elisp) RET i backquote RET.
    – NickD
    Commented May 1 at 15:33
  • You are right NickD, I made a first simple stew myself ! Anyway thanks for pointer toward the backquote help page. I should go and read those nodes more often when I am trying to decipher some elisp symbols I really do not understand. I got to say that Elisp seems to be a very elegant language, I had already kind of guessed (or read somewhere maybe) the quote usage, but the level of subtlety that backquote brings would have been beyond my deduction skills. All I got to do now is some more trial and error...
    – lyndhurst
    Commented May 3 at 6:38

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.