From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: Tangle with conditional statement Date: Sat, 24 Aug 2013 09:49:13 -0600 Message-ID: <87eh9jay2u.fsf@gmail.com> References: <867gfbjic3.fsf@somewhere.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:40009) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VDG5i-00072C-1q for emacs-orgmode@gnu.org; Sat, 24 Aug 2013 11:49:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VDG5c-0003x8-NO for emacs-orgmode@gnu.org; Sat, 24 Aug 2013 11:49:29 -0400 Received: from mail-pd0-x230.google.com ([2607:f8b0:400e:c02::230]:36502) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VDG5c-0003wo-D6 for emacs-orgmode@gnu.org; Sat, 24 Aug 2013 11:49:24 -0400 Received: by mail-pd0-f176.google.com with SMTP id q10so1798518pdj.7 for ; Sat, 24 Aug 2013 08:49:23 -0700 (PDT) In-Reply-To: <867gfbjic3.fsf@somewhere.org> (Fabrice Niessen's message of "Sat, 24 Aug 2013 16:04:28 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Fabrice Niessen Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain "Fabrice Niessen" 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., --=-=-= Content-Type: text/x-org Content-Disposition: inline; filename=tangle-example.org * 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 --=-=-= Content-Type: text/plain >> >> 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 --=-=-=--