Subject: [PATCH 4/6] gnu: Add emacs-emacsql.

From: Oleg Pykhalov <[email-masked]>
Date: Tue, 20 Mar 2018 19:33:08 +0300

* gnu/packages/emacs.scm (emacs-emacsql): New public variable.
---
 gnu/packages/emacs.scm | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)

diff --git a/gnu/packages/emacs.scm b/gnu/packages/emacs.scm
index 41bb69914..83deb256f 100644
--- a/gnu/packages/emacs.scm
+++ b/gnu/packages/emacs.scm
@@ -64,6 +64,7 @@
   #:use-module (gnu packages audio)
   #:use-module (gnu packages bash)
   #:use-module (gnu packages code)
+  #:use-module (gnu packages databases)
   #:use-module (gnu packages guile)
   #:use-module (gnu packages gtk)
   #:use-module (gnu packages gnome)
@@ -7478,3 +7479,96 @@ after its registered lisp object has been garbage collected.  This allows for
 extra resources, such as buffers and processes, to be cleaned up after the
 object has been freed.")
   (license license:unlicense)))
+
+(define-public emacs-emacsql
+  (package
+    (name "emacs-emacsql")
+    (version "2.0.3")
+    (source
+     (origin
+       (method url-fetch)
+       (uri (string-append "https://github.com/skeeto/emacsql/archive/"
+                           version ".tar.gz"))
+       (file-name (string-append name "-" version ".tar.gz"))
+       (sha256
+        (base32
+         "04hfjdgl1zc7jysgjc7d7d3xqpr7q1q9gsmzffjd91ii3hpqjgx6"))))
+    (build-system gnu-build-system)
+    (arguments
+     `(#:modules ((ice-9 match)
+                  (srfi srfi-26)
+                  ,@%gnu-build-system-modules
+                  (guix build emacs-utils))
+       #:imported-modules (,@%gnu-build-system-modules
+                           (guix build emacs-utils))
+       #:phases
+       (modify-phases %standard-phases
+         (delete 'configure)
+         (add-before 'build 'patch-elisp-shell-shebangs
+           (lambda _
+             (substitute* (find-files "." "\\.el")
+               (("/bin/sh") (which "sh")))
+             #t))
+         (add-after 'patch-elisp-shell-shebangs 'setenv-emacsloadpath
+           (lambda* (#:key inputs #:allow-other-keys)
+             (define (el-dir store-dir)
+               (match (find-files store-dir "\\.el$")
+                 ((f1 f2 ...) (dirname f1))
+                 (_ "")))
+             (define emacs-prefix? (cut string-prefix? "emacs-" <>))
+             (let* ((emacs-load-paths
+                     (map (match-lambda
+                            (((? emacs-prefix? name) . dir)
+                             (string-append (el-dir dir) ":"))
+                            (_ ""))
+                          inputs))
+                    (emacs-load-path-value
+                     (string-concatenate emacs-load-paths)))
+               (format #t "environment variable `EMACSLOADPATH' set to ~a\n"
+                       emacs-load-path-value)
+               (setenv "EMACSLOADPATH" emacs-load-path-value))
+             #t))
+         (add-after 'setenv-emacsloadpath 'setenv-shell
+           (lambda _
+             (setenv "SHELL" "sh")))
+         (add-after 'setenv-shell 'build-emacsql-sqlite
+           (lambda _
+             (invoke "make" "binary" "CC=gcc")))
+         (add-after 'build-emacsql-sqlite 'install-emacsql-sqlite
+           (lambda* (#:key outputs #:allow-other-keys)
+             (install-file "sqlite/emacsql-sqlite"
+                           (string-append (assoc-ref outputs "out")
+                                          "/bin"))
+             #t))
+         (add-after 'install-emacsql-sqlite 'patch-emacsql-sqlite.el
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let ((file "emacsql-sqlite.el"))
+               (chmod file #o644)
+               (emacs-substitute-sexps file
+                 ("(defvar emacsql-sqlite-user-prompted" 't)
+                 ("(executable-find" (which "gcc"))
+                 ("(defvar emacsql-sqlite-executable"
+                  (string-append (assoc-ref outputs "out")
+                                 "/bin/emacsql-sqlite"))))))
+         (delete 'check) ; No ‘check’ target in ‘./Makefile’.
+         (replace 'install ; No ‘install’ target in ‘./Makefile’.
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let* ((out (assoc-ref outputs "out")))
+               (install-file "sqlite/emacsql-sqlite"
+                             (string-append out "/bin"))
+               (for-each (cut install-file <>
+                              (string-append out "/share/emacs/site-lisp"))
+                         (find-files "." "\\.elc*$")))
+             #t)))))
+    (inputs
+     `(("emacs-minimal" ,emacs-minimal)
+       ("mysql" ,mysql)
+       ("postgresql" ,postgresql)))
+    (propagated-inputs
+     `(("emacs-finalize" ,emacs-finalize)
+       ("emacs-pg" ,emacs-pg)))
+    (home-page "https://github.com/skeeto/emacsql")
+    (synopsis "Emacs high-level SQL database front-end")
+    (description "EmacSQL provides a high-level Emacs Lisp front-end for
+SQLite (primarily), PostgreSQL, MySQL, and potentially other SQL databases.")
+  (license license:gpl3+)))
-- 
2.16.2