2

For my current config, I'm tangling an org-mode buffer into my init.el, and I have a few sections where I'd like to insert rich text between segments of Emacs lisp code, like the following:

...

#+BEGIN_SRC elisp
(use-package exwm
  :config
  (exwm-enable)
#+END_SRC

I find that four workspaces is enough to start out with, and I like being able
to use my X windows across workspaces.

#+BEGIN_SRC elisp
  :init
  (setq exwm-workspace-number 4
        exwm-workspace-show-all-buffers t
        exwm-layout-show-all-buffers t)
#+END_SRC

...

The issue is, if I C-c ' to edit the second SRC block, it reindents to the beginning of the line. Smartparens is also unhappy about any unmatched closing parentheses I have in subsequent blocks.

Is there anything in org-mode that would support something like this? Perhaps some marker I can tag each block with as a hint that they should all be pulled into the same Org Src buffer when I C-c ' on one of them?

4
  • 1
    I would not split a single expression between multiple src blocks: it's like having y = x + in one block and x*4 in another: they cannot be evaluated independently and it is very confusing. Why not add comments to the src block? They will appear in init.el but so what? You can never have enough documentation.
    – NickD
    Apr 6, 2020 at 0:24
  • 1
    Note also that you can use <<noweb>> syntax. One (use-package block with multiple <<subsections>>, and you can define the subsections elsewhere. Would that help?
    – mankoff
    Apr 21, 2020 at 20:25
  • Thank you for the suggestion, @mankoff. This is the closest to what I was looking for, but it's unfortunately still a bit too much overhead for my tastes. It seems what I'm trying to do doesn't make much sense in Org, so I've deferred to a plain init.el with comments.
    – Jakob
    May 14, 2020 at 23:45
  • What about multiple use-package statements as I suggest in the answer below? What about that doesn't work for you?
    – mankoff
    May 15, 2020 at 14:30

2 Answers 2

1

You have two issues here and probably should have 2 questions. First issue is the indenting; second is the bracket matching for smartparens.

You can indent the whole tangled source file after saving it. This idea is taken from Grant Rettke's page.

The relevant code is

(defun help/org-babel-post-tangle-hook-fn ()
  (interactive)
  (indent-region (point-min) (point-max) nil)
  (save-buffer))
(add-hook 'org-babel-post-tangle-hook #'help/org-babel-post-tangle-hook-fn)

To deal with the bracket issue, I would look at weaving and literate programming and Grant Rettke's link above, so that each code block is balanced, e.g.:

#+BEGIN_SRC elisp
(use-package exwm
  :config
  (exwm-enable)
:init
<<exwm>>
) ; close use-package
#+END_SRC

and

#+BEGIN_SRC elisp :tangle no :noweb-ref exwm
  (setq exwm-workspace-number 4
        exwm-workspace-show-all-buffers t
        exwm-layout-show-all-buffers t) ; parens balanced in code block
#+END_SRC

By placing the :init in the top level block, the code in the other block is valid elisp and so, when editing the block in a buffer, you can execute it easily.

0

You can use (use-package exwm ... ) multiple times. That might help.

2
  • 1
    Maybe but doesn't that defeat the purpose of use-package? I thought it was to keep the configuration of the package all in one place (but I don't use it, so this may be a wee bit off...)
    – NickD
    Apr 6, 2020 at 0:27
  • 1
    I think use-package is more general than that. There are other purposes, like tighter syntax. And there may be logical reasons to not keep things in one place, and this supports that elegantly.
    – mankoff
    Apr 7, 2020 at 16:19

Your Answer

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

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