;; Test Package for Parameters
;; emacs-p : emacs variants, combined with parameters

(define-module (test-packages)
               #:use-module (gnu packages version-control)
               #:use-module (gnu packages emacs)
               #:use-module (gnu packages sqlite)
               #:use-module (gnu packages)
               #:use-module (guix parameters)
               #:use-module (guix packages)
               #:use-module (guix utils)
               #:use-module (guix git-download)
               #:use-module (guix build-system gnu))

(define-global-parameter
  (package-parameter
    (name 'x11)))

(define-public emacs-p
               (package-with-parameters
                 [parameter-spec
                   (local
                     (list
                       (package-parameter 
                         (name 'next)
                         (variants
                           (parameter-variant-match
                             (_ #:lambda (lambda (pkg)
                                           (package
                                             (inherit pkg)
                                             (version "29.0.92")
                                             (source
                                               (origin
                                                 (inherit (package-source pkg))
                                                 (method git-fetch)
                                                 (uri (git-reference
                                                        (url "https://git.savannah.gnu.org/git/emacs.git/")
                                                        (commit (string-append "emacs-" version))))
                                                 (file-name (git-file-name (package-name pkg) version))
                                                 (patches
                                                   (parameter-if #:package pkg (pgtk)
                                                                 (search-patches
                                                                   "emacs-exec-path.patch"
                                                                   "emacs-fix-scheme-indent-function.patch"
                                                                   "emacs-native-comp-driver-options.patch"
                                                                   "emacs-pgtk-super-key-fix.patch")
                                                                 (search-patches
                                                                   "emacs-exec-path.patch"
                                                                   "emacs-fix-scheme-indent-function.patch"
                                                                   "emacs-native-comp-driver-options.patch")))
                                                 (sha256
                                                   (base32
                                                     "1h3p325859svcy43iv7wr27dp68049j9d44jq5akcynqdkxz4jjn"))))))))))
                       (package-parameter (name 'tree-sitter)
                                          (dependencies '(tree-sitter)))
                       (package-parameter
                         (name 'pgtk)
                         (variants
                           (parameter-variant-match
                             (_ #:transform (with-configure-flag
                                              #:package-name "=--with-pgtk"))))
                         (dependencies '(tree-sitter x11)))
                       (package-parameter
                         (name 'xwidgets)
                         (variants
                           (parameter-variant-match
                             (_ #:transform (with-configure-flag
                                              #:package-name "=--with-xwidgets")))))
                       (package-parameter
                         (name 'wide-int)
                         (variants
                           (parameter-variant-match
                             (_ #:transform (with-configure-flag
                                              #:package-name "=--with-wide-int")))))))
                   (one-of '((_ (x11 #:off) pgtk)
                             (_ (x11 #:off) xwidgets)))]
                 (inherit emacs)
                 (name "emacs-p")
                 (arguments
                   (parameter-substitute-keyword-arguments 
                     (package-arguments emacs)
                     [((x11 #:off))
                      '(((#:configure-flags flags #~'())
                         #~(delete "--with-cairo" #$flags))
                        ((#:modules _) (%emacs-modules build-system))
                        ((#:phases phases)
                         #~(modify-phases #$phases
                                          (delete 'restore-emacs-pdmp)
                                          (delete 'strip-double-wrap))))]
                     [(#:all (xwidgets on) (pgtk #:off))
                      '(((#:configure-flags flags #~'())
                         #~(cons "--with-xwidgets" #$flags))
                        ((#:modules _) (%emacs-modules build-system))
                        ((#:phases phases)
                         #~(modify-phases #$phases
                                          (delete 'restore-emacs-pdmp)
                                          (delete 'strip-double-wrap))))]))
                 (inputs
                   (parameter-modify-inputs (package-inputs emacs)
                                            ((next) (prepend sqlite))
                                            (((tree-sitter on)) (prepend tree-sitter))
                                            (((xwidgets on)) (prepend gsettings-desktop-schemas
                                                                      webkitgtk-with-libsoup2))
                                            (((x11 #:off))
                                             (delete "libx11" "gtk+" "libxft" "libtiff" "giflib" "libjpeg"
                                                     "imagemagick" "libpng" "librsvg" "libxpm" "libice" "libsm"
                                                     "cairo" "pango" "harfbuzz" "libotf" "m17n-lib" "dbus"))))))

