emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Babel: tangling questions
@ 2019-12-03  9:53 Jarmo Hurri
  2019-12-03 13:28 ` Diego Zamboni
  0 siblings, 1 reply; 3+ messages in thread
From: Jarmo Hurri @ 2019-12-03  9:53 UTC (permalink / raw)
  To: emacs-orgmode


Greetings.

I am trying to tangle code to a specific directory.

1. I want to tangle to the default filename (derived from name of org
   file and programming language). However, the :tangle header argument
   only takes either "yes" or "filename." If the value is "yes", then
   the filename is deduced automatically. How can I specify a tangling
   directory and still have the default filename?

   There was a related question on stack exchange, but it did not use
   the default filename:
   https://emacs.stackexchange.com/questions/41806/org-babel-set-root-dir-for-tangled-files

2. The question above implies a feature request: third possible value
   for :tangle, a directory. In this case code would be tangled into the
   specified directory into a file with default filename. Any support?

3. Is it possible to tangle automatically on (every) export? I always
   export, but I might forget to tangle, in which case export and tangle
   would be out of sync.

   Someone asked about this a while ago on stack exchange, but maybe
   things have changed:
   https://emacs.stackexchange.com/questions/24645/exporting-and-tangling-simultaneously-in-org-mode

Thanks a bunch in advance.

Jarmo

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Babel: tangling questions
  2019-12-03  9:53 Babel: tangling questions Jarmo Hurri
@ 2019-12-03 13:28 ` Diego Zamboni
  2019-12-03 15:22   ` Jarmo Hurri
  0 siblings, 1 reply; 3+ messages in thread
From: Diego Zamboni @ 2019-12-03 13:28 UTC (permalink / raw)
  To: Org-mode; +Cc: Jarmo Hurri

[-- Attachment #1: Type: text/plain, Size: 1449 bytes --]

Hi Jarmo,

1. I want to tangle to the default filename (derived from name of org
>    file and programming language). However, the :tangle header argument
>    only takes either "yes" or "filename." If the value is "yes", then
>    the filename is deduced automatically. How can I specify a tangling
>    directory and still have the default filename?
>
>    There was a related question on stack exchange, but it did not use
>    the default filename:
>
> https://emacs.stackexchange.com/questions/41806/org-babel-set-root-dir-for-tangled-files


You can specify a full path as the value of :tangle, but I don't know of a
way to specify only the directory.

As a workaround, you can also use emacs-lisp code as the value for :tangle,
so you could do something like this:

 :tangle (concat "/some/dir/" (file-name-base (file-name-sans-extension
(buffer-file-name))) ".ext")

3. Is it possible to tangle automatically on (every) export? I always
>    export, but I might forget to tangle, in which case export and tangle
>    would be out of sync.
>

I use the following hook to tangle on every save. I find it indispensable :)

  (add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook
'org-babel-tangle
                                   'run-at-end 'only-in-org-mode)))

Also, check out my free book "Literate Config", where I discuss some more
tips about Literate Programming: https://leanpub.com/lit-config/read

Hope this helps,
--Diego

[-- Attachment #2: Type: text/html, Size: 2382 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Babel: tangling questions
  2019-12-03 13:28 ` Diego Zamboni
@ 2019-12-03 15:22   ` Jarmo Hurri
  0 siblings, 0 replies; 3+ messages in thread
From: Jarmo Hurri @ 2019-12-03 15:22 UTC (permalink / raw)
  To: emacs-orgmode


Greetings Diego.

Diego Zamboni <diego@zzamboni.org> writes:

>> 1. I want to tangle to the default filename (derived from name of org
>>    file and programming language). However, the :tangle header argument
>>    only takes either "yes" or "filename." If the value is "yes", then
>>    the filename is deduced automatically. How can I specify a tangling
>>    directory and still have the default filename?
>
> You can specify a full path as the value of :tangle, but I don't know
> of a way to specify only the directory.

For my purposes this would be a nice new feature. Specifying a directory
would imply the default filename.

> As a workaround, you can also use emacs-lisp code as the value for
> :tangle, so you could do something like this:
>
>  :tangle (concat "/some/dir/" (file-name-base (file-name-sans-extension
> (buffer-file-name))) ".ext")

Nice! For this to be practical, I need two additional features.

1. Ability to set "/some/dir/" on a per-file basis.
2. Ability to infer ".ext" from the source block.

I was actually extend your suggestion to a solution with both of these
properties. In my emacs init file I define:

(defun org-default-tangle-file-with-dir-from-property ()
  (concat (or (org-entry-get nil "ORG_TANGLE_DIRECTORY" t) ".")
          "/"
          (file-name-base (file-name-sans-extension (buffer-file-name)))
          "."
          (let ((lang (car (org-babel-get-src-block-info t))))
            (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang))))

Then I set on the top of my org file, for example

#+property: ORG_TANGLE_DIRECTORY ../docs

After this the following works like a charm (at least so far):

:tangle (org-default-tangle-file-with-dir-from-property)

>> 3. Is it possible to tangle automatically on (every) export? I always
>>    export, but I might forget to tangle, in which case export and tangle
>>    would be out of sync.
>>
>
> I use the following hook to tangle on every save. I find it indispensable :)
>
>   (add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook
> 'org-babel-tangle
>                                    'run-at-end 'only-in-org-mode)))

Good idea, I will explore this.

> Also, check out my free book "Literate Config", where I discuss some
> more tips about Literate Programming:
> https://leanpub.com/lit-config/read

Will do.

> Hope this helps,

Sure did. Thanks!

Jarmo

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-12-03 15:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-12-03  9:53 Babel: tangling questions Jarmo Hurri
2019-12-03 13:28 ` Diego Zamboni
2019-12-03 15:22   ` Jarmo Hurri

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).