From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrey Smirnov Subject: [org-babel] [PATCH] Improve ditta.jar finding heuristics Date: Sun, 20 Nov 2011 20:37:11 +0700 Message-ID: <87sjlij4oo.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([140.186.70.92]:37806) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RS7aC-0005v0-30 for emacs-orgmode@gnu.org; Sun, 20 Nov 2011 08:37:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RS7aA-00060V-MK for emacs-orgmode@gnu.org; Sun, 20 Nov 2011 08:37:20 -0500 Received: from mail-bw0-f41.google.com ([209.85.214.41]:36246) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RS7aA-00060R-DW for emacs-orgmode@gnu.org; Sun, 20 Nov 2011 08:37:18 -0500 Received: by bke17 with SMTP id 17so6369586bke.0 for ; Sun, 20 Nov 2011 05:37:17 -0800 (PST) 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: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hi everybody, I've been using org-mode for quite a while but only recently found myself in need of using ditaa to draw some simple diagram. As it turns out my installation of emacs(Ubuntu 10.10, emacs-snapshot from https://launchpad.net/~cassou/+archive/emacs) doesn't come with ditta.jar pre-bundled, although I'm not sure if it should. Anyway, despite my installation of ditta with help of apt-get, org-babel kept unsuccessfully trying to locate /usr/share/emacs/24.0.91/lisp/contrib/ditta.jar, leaving me without any diagrams produced. So to alleviate the problem I cloned git repository and wrote a small patch implementing very crude algorithm, which is, nonetheless, in my opinion, still an improvement on default behavior. For more details see commit message. Andrey Smirnov --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-org-babel-Add-simple-ditaa.jar-searching-heuristics.patch Content-Description: Add simple ditta.jar finding heuristics >From 03b434347e02c2fc95f38a7a4e87850eb5f87f56 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Sun, 20 Nov 2011 19:40:32 +0700 Subject: [PATCH] org-babel: Add simple ditaa.jar searching heuristics lisp/ob-ditaa.el: Add two functions `org-ditaa-try-find-file-in' and `org-ditaa-delete-if-not' and more complicated algorithm for setting the value of `org-ditaa-jar-path'. Prior to this the algorithm used to locate ditaa.jar was to look for it in ${ob-ditaa.el path}/../contrib/ but that approach fails if said 'jar' doesn't come pre-bundled with user's emacs install and even if it does it precludes user from using system-wide installed(via apt-get or any such tool) instance of ditaa in favor of pre-bundled one. New heuristics does the following: 1. Looks in the predefined set of locations, right now it is - /usr/share/ditaa/ (Location where it is installed in Ubuntu) - ${ob-ditaa.el path}/../contrib/ 2. If previous set yeilds no results it tryies to locate said ditta.jar in either /usr/share/ or /usr/lib/ --- lisp/ob-ditaa.el | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 71 insertions(+), 1 deletions(-) diff --git a/lisp/ob-ditaa.el b/lisp/ob-ditaa.el index 0aba9a6..15b3fbe 100644 --- a/lisp/ob-ditaa.el +++ b/lisp/ob-ditaa.el @@ -42,7 +42,77 @@ '((:results . "file") (:exports . "results") (:java . "-Dfile.encoding=UTF-8")) "Default arguments for evaluating a ditaa source block.") -(defvar org-ditaa-jar-path) +;;; Not having delete-if-not from cl package is rather annoying, +;;; but, alright, we'll do it live. +(defun org-ditaa-delete-if-not (pred seq) + "Destructively remove all elements of SEQ that do not satisfy predicat PRED" + (dolist (elt seq seq) + (when (not (apply pred (list elt))) + (setq seq (delete elt seq))))) + +(defun org-ditaa-try-find-file-in (dir filename) + "Traverse directory tree supplied in DIR and search for FILENAME. +Return full path to FILENAME if found." + (let ((candidate-file (expand-file-name filename dir))) + (cond ((file-exists-p candidate-file) + candidate-file) + ((file-directory-p dir) + (do ((path-to-file nil) + ;; List of sub-directories with . , .. and all + ;; items that are not directories filtered out + (subdir-list + (org-ditaa-delete-if-not + (lambda (e) (file-directory-p + (file-name-as-directory + (expand-file-name e dir)))) + (delete + ".." + (delete + "." + ;; Access to some directories might result in + ;; "Permission denied" file error. Wrap the call + ;; in condition-case to avoid that + (condition-case ex + (directory-files dir) + ('file-error))))) + (setq subdir-list (cdr subdir-list)))) + ((or (not subdir-list) + path-to-file) path-to-file) + (when subdir-list + (let ((subdir (file-name-as-directory + (expand-file-name (car subdir-list) dir)))) + (setq path-to-file (when (and subdir + (file-directory-p subdir)) + (org-ditaa-try-find-file-in subdir filename))))))) + (t + nil)))) + +;;; When looking for ditaa.jar go through predefined list of most +;;; likely places to have it, then if else fails try to find it +;;; somwhere in /usr/share or /usr/lib +(defvar org-ditaa-jar-path + (let* ((potential-path-list + (list "/usr/share/ditaa/ditaa.jar" ; Ubuntu 10.10 installed via apt-get + (expand-file-name ; Bundled with emacs + "ditaa.jar" + (file-name-as-directory + (expand-file-name + "scripts" + (file-name-as-directory + (expand-file-name + "../contrib" + (file-name-directory (or load-file-name + buffer-file-name))))))))) + (actual-path (car potential-path-list))) + (while (and actual-path + (not (file-exists-p actual-path))) + (setq potential-path-list (cdr potential-path-list)) + (setq actual-path (car potential-path-list))) + (when (not actual-path) + (setq actual-path (or (org-ditaa-try-find-file-in "/usr/share" "ditaa.jar") + (org-ditaa-try-find-file-in "/usr/lib" "ditaa.jar")))) + actual-path)) + (defun org-babel-execute:ditaa (body params) "Execute a block of Ditaa code with org-babel. This function is called by `org-babel-execute-src-block'." -- 1.7.5.4 --=-=-=--