emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [org-babel] [PATCH] Improve ditta.jar finding heuristics


From: Andrey Smirnov
Subject: [O] [org-babel] [PATCH] Improve ditta.jar finding heuristics
Date: Sun, 20 Nov 2011 20:37:11 +0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.91 (gnu/linux)

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

>From 03b434347e02c2fc95f38a7a4e87850eb5f87f56 Mon Sep 17 00:00:00 2001
From: Andrey Smirnov <address@hidden>
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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]