emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Fabrice Niessen" <fni-news-TA4HMoP+1wHrZ44/DZwexQ@public.gmane.org>
To: emacs-orgmode-mXXj517/zsQ@public.gmane.org
Subject: Re: Tangle with conditional statement
Date: Sat, 24 Aug 2013 16:04:28 +0200	[thread overview]
Message-ID: <867gfbjic3.fsf@somewhere.org> (raw)
In-Reply-To: CAC=ryke7YTV-YN5gtqvywhVaTU9j8H-m7jjn2uHTaqyeySbPVQ@mail.gmail.com

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

  reply	other threads:[~2013-08-24 14:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-24 11:51 Tangle with conditional statement Andreas
2013-08-24 14:04 ` Fabrice Niessen [this message]
2013-08-24 15:49   ` Eric Schulte
  -- strict thread matches above, loose matches on Subject: below --
2013-10-06 19:50 Andreas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=867gfbjic3.fsf@somewhere.org \
    --to=fni-news-ta4hmop+1whrz44/dzwexq@public.gmane.org \
    --cc=emacs-orgmode-mXXj517/zsQ@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).