emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Tangle with conditional statement
@ 2013-08-24 11:51 Andreas
  2013-08-24 14:04 ` Fabrice Niessen
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas @ 2013-08-24 11:51 UTC (permalink / raw)
  To: emacs-orgmode

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

Hello mailing list,

First time poster to this mailing list.
I was hoping i could get some pointers to a question I have not yet found a
solution or example of.

I use orgmode and tangled elisp src blocks to initiate emacs. I also use
el-get to install packages, however sometimes these packages fail for
various reasons and I would like to skip them so that the hall emacs
initailisation doesn't brake. However for important packages i have them
hardcoded in my init files so that the settings I have doesn't complain.
But as I use org-mode to tangle this I can just flip the :tangle to "no" to
to not tangle a specific section.

my question: Is it possible to have a conditional statement for a tangled
block so that only if a package is installed or a directory exist does the
block tangle? Meaning that I wouldn't need to hard code all the el-get
fetches in my init file.
Is this a good idea? or should I work in conditions in the emacs src blocks
instead?
I realize now when I'm typing it out that it might come with some other
issues like first time running it I would have to reinitialize emacs after
package installs to get my settings but after that I guess it would be fine.

Hope to get some pointers,
Regards,
Andreas

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

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

* Re: Tangle with conditional statement
  2013-08-24 11:51 Tangle with conditional statement Andreas
@ 2013-08-24 14:04 ` Fabrice Niessen
  2013-08-24 15:49   ` Eric Schulte
  0 siblings, 1 reply; 4+ messages in thread
From: Fabrice Niessen @ 2013-08-24 14:04 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hello Andreas,

> First time poster to this mailing list.
> I was hoping i could get some pointers to a question I have not yet found a
> solution or example of.
>
> I use orgmode and tangled elisp src blocks to initiate emacs. I also use
> el-get to install packages, however sometimes these packages fail for
> various reasons and I would like to skip them so that the hall emacs
> initailisation doesn't brake. However for important packages i have them
> hardcoded in my init files so that the settings I have doesn't complain.
> But as I use org-mode to tangle this I can just flip the :tangle to "no" to
> to not tangle a specific section.
>
> my question: Is it possible to have a conditional statement for a tangled
> block so that only if a package is installed or a directory exist does the
> block tangle? Meaning that I wouldn't need to hard code all the el-get
> fetches in my init file.
> Is this a good idea? or should I work in conditions in the emacs src blocks
> instead?
> I realize now when I'm typing it out that it might come with some other
> issues like first time running it I would have to reinitialize emacs after
> package installs to get my settings but after that I guess it would be fine.

Instead of trying to put the "look if package is present" constraint on
Babel's side (and have a configuration file which you cannot exchange), I'd do
it on the Emacs (Lisp) side, as I did in my configuration file [1] with a
"try-require" function:

--8<---------------cut here---------------start------------->8---
  (defvar leuven--missing-packages nil
    "List of packages that `try-require' or `locate-library' can't find.")

  ;; require a feature/library if available; if not, fail silently
  (defun try-require (feature)
    "Attempt to load a library or module. Return true if the
  library given as argument is successfully loaded. If not, instead
  of an error, just add the package to a list of missing packages."
    (let (time-start)
      (condition-case err
          ;; protected form
          (progn
            (when leuven-load-verbose
              (message "(info) Checking for `%s'..." feature))
            (if (stringp feature)
                (load-library feature)
              (setq time-start (float-time))
              (require feature))
            t)
        ;; error handler
        (file-error ;; condition
         (progn
           (when leuven-load-verbose
             (message "(info) Checking for `%s'... missing" feature))
           (add-to-list 'leuven--missing-packages feature 'append))
         nil))))
--8<---------------cut here---------------end--------------->8---

Best regards,
Fabrice

[1] https://github.com/fniessen/emacs-leuven/blob/master/emacs-leuven.el

-- 
Fabrice Niessen
Leuven, Belgium

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

* Re: Tangle with conditional statement
  2013-08-24 14:04 ` Fabrice Niessen
@ 2013-08-24 15:49   ` Eric Schulte
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Schulte @ 2013-08-24 15:49 UTC (permalink / raw)
  To: Fabrice Niessen; +Cc: emacs-orgmode

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

"Fabrice Niessen" <fni-news@pirilampo.org> writes:

> Hello Andreas,
>
>> First time poster to this mailing list.
>> I was hoping i could get some pointers to a question I have not yet found a
>> solution or example of.
>>
>> I use orgmode and tangled elisp src blocks to initiate emacs. I also use
>> el-get to install packages, however sometimes these packages fail for
>> various reasons and I would like to skip them so that the hall emacs
>> initailisation doesn't brake. However for important packages i have them
>> hardcoded in my init files so that the settings I have doesn't complain.
>> But as I use org-mode to tangle this I can just flip the :tangle to "no" to
>> to not tangle a specific section.
>>
>> my question: Is it possible to have a conditional statement for a tangled
>> block so that only if a package is installed or a directory exist does the
>> block tangle? Meaning that I wouldn't need to hard code all the el-get
>> fetches in my init file.

Yes, this is possible, e.g.,


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: tangle-example.org --]
[-- Type: text/x-org, Size: 514 bytes --]

* Conditional Tangle Example

To setup, run this block to ensure foo exists and bar doesn't.
#+begin_src sh :tangle no :results silent
  date > foo
  rm -f bar
#+end_src

Then tangle the buffer with =C-c C-v t=.

#+begin_src emacs-lisp :tangle (if (file-exists-p "foo") "yes" "no")
  (message "only tangled if %s exists %s" "foo" (file-exists-p "foo"))
#+end_src

#+begin_src emacs-lisp :tangle (if (file-exists-p "bar") "yes" "no")
  (message "only tangled if %s exists %s" "bar" (file-exists-p "bar"))
#+end_src

[-- Attachment #3: Type: text/plain, Size: 2211 bytes --]


>> 
>> Is this a good idea?

Probably not.  The emacs-lisp embedded in your Org-mode file is only
re-tangled when the Org-mode file changes, so it is better to have the
conditional in the tangled Emacs Lisp file so it is run every time you
start Emacs.

>> or should I work in conditions in the emacs src blocks instead?  I
>> realize now when I'm typing it out that it might come with some other
>> issues like first time running it I would have to reinitialize emacs
>> after package installs to get my settings but after that I guess it
>> would be fine.
>
> Instead of trying to put the "look if package is present" constraint on
> Babel's side (and have a configuration file which you cannot exchange), I'd do
> it on the Emacs (Lisp) side, as I did in my configuration file [1] with a
> "try-require" function:
>
> --8<---------------cut here---------------start------------->8---
>   (defvar leuven--missing-packages nil
>     "List of packages that `try-require' or `locate-library' can't find.")
>
>   ;; require a feature/library if available; if not, fail silently
>   (defun try-require (feature)
>     "Attempt to load a library or module. Return true if the
>   library given as argument is successfully loaded. If not, instead
>   of an error, just add the package to a list of missing packages."
>     (let (time-start)
>       (condition-case err
>           ;; protected form
>           (progn
>             (when leuven-load-verbose
>               (message "(info) Checking for `%s'..." feature))
>             (if (stringp feature)
>                 (load-library feature)
>               (setq time-start (float-time))
>               (require feature))
>             t)
>         ;; error handler
>         (file-error ;; condition
>          (progn
>            (when leuven-load-verbose
>              (message "(info) Checking for `%s'... missing" feature))
>            (add-to-list 'leuven--missing-packages feature 'append))
>          nil))))
> --8<---------------cut here---------------end--------------->8---
>
> Best regards,
> Fabrice
>
> [1] https://github.com/fniessen/emacs-leuven/blob/master/emacs-leuven.el

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

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

* Re: Tangle with conditional statement
@ 2013-10-06 19:50 Andreas
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas @ 2013-10-06 19:50 UTC (permalink / raw)
  To: emacs-orgmode

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

Hi Fabrice and Eric,

A long over due response. Apologies.

I got a unanimous answer about not putting the conditional statements in
the code block header, that was reassuring =). I guess the use case here is
if you have different configurations for different users or machines or
something similar.

I looked at the code I was shown. I noticed there was some pieces missing
but even though adding them I could not get the code to work properly. I
figured it was due to me using el-get for my packages and no packages are
actually required, but somehow autoloaded instead, by the el-get setup, or
it's just a matter of me being a noob elisper.

Anyway,  when I was fiddling around with the code I stumbled upon el-gets
built in functions. I noticed that, of course, el-get has functions for
checking install status of packages. My current solution therefore looks
like this:
(if (el-get-package-exists-p "<package>")
      (do stuff) )

which seems to work well for now but is not optimal. Of course it's only
works with el-get installed packages but I'll have to live with it for now.

Thanks Fabrice and Eric for your swift responses.

Regards

Andreas

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

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

end of thread, other threads:[~2013-10-06 19:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-24 11:51 Tangle with conditional statement Andreas
2013-08-24 14:04 ` Fabrice Niessen
2013-08-24 15:49   ` Eric Schulte
  -- strict thread matches above, loose matches on Subject: below --
2013-10-06 19:50 Andreas

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).