From fd70791218936da56a622707d3e09efc33feb16d Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Wed, 14 Sep 2022 10:15:26 +0200 Subject: [PATCH] Add new function xdg-current-desktop to xdg.el * lisp/xdg.el (xdg-current-desktop): New function. * test/lisp/xdg-tests.el (xdg-current-desktop): New test. --- etc/NEWS | 8 ++++++++ lisp/xdg.el | 12 ++++++++++++ test/lisp/xdg-tests.el | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index ae3f84c1b97..4053e39b773 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -3458,6 +3458,14 @@ version 0.8 (8th May 2021), "contains state data that should persist between (application) restarts, but that is not important or portable enough to the user that it should be stored in $XDG_DATA_HOME". +--- +*** New function 'xdg-current-desktop'. +It returns a list of strings, corresponding to the colon-separated +list of names in the 'XDG_CURRENT_DESKTOP' environment variable, which +identify the current desktop environment. +(This variable was introduced in XDG Desktop Entry Specification +version 1.2.) + +++ ** New macro 'with-delayed-message'. This macro is like 'progn', but will output the specified message if diff --git a/lisp/xdg.el b/lisp/xdg.el index dd0d51290dc..5d60aa2f283 100644 --- a/lisp/xdg.el +++ b/lisp/xdg.el @@ -281,6 +281,18 @@ Optional argument GROUP defaults to the string \"Desktop Entry\"." (when (null (string-match-p "[^[:blank:]]" (car res))) (pop res)) (nreverse res))) +(defun xdg-current-desktop () + "Return a list of strings identifying the current desktop environment. + +According to the XDG Desktop Entry Specification version 0.5: + + If $XDG_CURRENT_DESKTOP is set then it contains a + colon-separated list of strings ... $XDG_CURRENT_DESKTOP + should have been set by the login manager, according to the + value of the DesktopNames found in the session file." + (when-let ((ret (getenv "XDG_CURRENT_DESKTOP"))) + (string-split ret ":"))) + ;; MIME apps specification ;; https://standards.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html diff --git a/test/lisp/xdg-tests.el b/test/lisp/xdg-tests.el index e8e103348b7..882160743a2 100644 --- a/test/lisp/xdg-tests.el +++ b/test/lisp/xdg-tests.el @@ -59,6 +59,16 @@ (should (equal (xdg-desktop-strings " ") nil)) (should (equal (xdg-desktop-strings "a; ;") '("a" " ")))) +(ert-deftest xdg-current-desktop () + (let ((env (getenv "XDG_CURRENT_DESKTOP"))) + (unwind-protect + (progn + (setenv "XDG_CURRENT_DESKTOP" "KDE") + (should (equal (xdg-current-desktop) '("KDE"))) + (setenv "XDG_CURRENT_DESKTOP" "ubuntu:GNOME") + (should (equal (xdg-current-desktop) '("ubuntu" "GNOME")))) + (setenv "XDG_CURRENT_DESKTOP" env)))) + (ert-deftest xdg-mime-associations () "Test reading MIME associations from files." (let* ((apps (ert-resource-file "mimeapps.list")) -- 2.39.2