aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Oteiza2017-09-06 13:17:05 -0400
committerMark Oteiza2017-09-06 13:17:05 -0400
commit9604f9cd33bcbc921fd18e894fdd8a98012fd09d (patch)
treee8c152189ae6f439fa57496dfc76596418543e08
parentda3e1016349b2f552f149ccf577b60e377c3095a (diff)
downloademacs-9604f9cd33bcbc921fd18e894fdd8a98012fd09d.tar.gz
emacs-9604f9cd33bcbc921fd18e894fdd8a98012fd09d.zip
Add XDG desktop file parsing and tests
* lisp/xdg.el: Add support for Desktop Entry Specification. (xdg--user-dirs-parse-line): Check if file is readable. (xdg-desktop-group-regexp, xdg-desktop-entry-regexp): New variables. (xdg--desktop-parse-line, xdg-desktop-read-file, xdg-desktop-strings): New functions. * test/lisp/xdg-tests.el: * test/data/xdg/test.desktop: * test/data/xdg/wrong.desktop: New files.
-rw-r--r--lisp/xdg.el73
-rw-r--r--test/data/xdg/test.desktop3
-rw-r--r--test/data/xdg/wrong.desktop2
-rw-r--r--test/lisp/xdg-tests.el71
4 files changed, 142 insertions, 7 deletions
diff --git a/lisp/xdg.el b/lisp/xdg.el
index 916de00d5e2..4b255429db4 100644
--- a/lisp/xdg.el
+++ b/lisp/xdg.el
@@ -29,9 +29,13 @@
29;; - XDG Base Directory Specification 29;; - XDG Base Directory Specification
30;; - Thumbnail Managing Standard 30;; - Thumbnail Managing Standard
31;; - xdg-user-dirs configuration 31;; - xdg-user-dirs configuration
32;; - Desktop Entry Specification
32 33
33;;; Code: 34;;; Code:
34 35
36(eval-when-compile
37 (require 'subr-x))
38
35 39
36;; XDG Base Directory Specification 40;; XDG Base Directory Specification
37;; https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html 41;; https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
@@ -128,13 +132,14 @@ This should be called at the beginning of a line."
128(defun xdg--user-dirs-parse-file (filename) 132(defun xdg--user-dirs-parse-file (filename)
129 "Return alist of xdg-user-dirs from FILENAME." 133 "Return alist of xdg-user-dirs from FILENAME."
130 (let (elt res) 134 (let (elt res)
131 (with-temp-buffer 135 (when (file-readable-p filename)
132 (insert-file-contents filename) 136 (with-temp-buffer
133 (goto-char (point-min)) 137 (insert-file-contents filename)
134 (while (not (eobp)) 138 (goto-char (point-min))
135 (setq elt (xdg--user-dirs-parse-line)) 139 (while (not (eobp))
136 (when (consp elt) (push elt res)) 140 (setq elt (xdg--user-dirs-parse-line))
137 (forward-line))) 141 (when (consp elt) (push elt res))
142 (forward-line))))
138 res)) 143 res))
139 144
140(defun xdg-user-dir (name) 145(defun xdg-user-dir (name)
@@ -147,6 +152,60 @@ This should be called at the beginning of a line."
147 (let ((dir (cdr (assoc name xdg-user-dirs)))) 152 (let ((dir (cdr (assoc name xdg-user-dirs))))
148 (when dir (expand-file-name dir)))) 153 (when dir (expand-file-name dir))))
149 154
155
156;; Desktop Entry Specification
157;; https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.1.html
158
159(defconst xdg-desktop-group-regexp
160 (rx "[" (group-n 1 (+? (in " -Z\\^-~"))) "]")
161 "Regexp matching desktop file group header names.")
162
163;; TODO Localized strings left out intentionally, as Emacs has no
164;; notion of l10n/i18n
165(defconst xdg-desktop-entry-regexp
166 (rx (group-n 1 (+ (in "A-Za-z0-9-")))
167 (* blank) "=" (* blank)
168 (group-n 2 (* nonl)))
169 "Regexp matching desktop file entry key-value pairs.")
170
171(defun xdg--desktop-parse-line ()
172 (skip-chars-forward "[:blank:]")
173 (when (/= (following-char) ?#)
174 (cond
175 ((looking-at xdg-desktop-entry-regexp)
176 (cons (match-string 1) (match-string 2)))
177 ((looking-at xdg-desktop-group-regexp)
178 (match-string 1)))))
179
180(defun xdg-desktop-read-file (filename)
181 "Return \"Desktop Entry\" contents of desktop file FILENAME as a hash table."
182 (let ((res (make-hash-table :test #'equal))
183 elt group)
184 (with-temp-buffer
185 (save-match-data
186 (insert-file-contents-literally filename)
187 (goto-char (point-min))
188 (while (or (= (following-char) ?#)
189 (string-blank-p (buffer-substring (point) (point-at-eol))))
190 (forward-line))
191 (unless (equal (setq group (xdg--desktop-parse-line)) "Desktop Entry")
192 (error "Wrong first section: %s" group))
193 (while (not (eobp))
194 (when (consp (setq elt (xdg--desktop-parse-line)))
195 (puthash (car elt) (cdr elt) res))
196 (forward-line))))
197 res))
198
199(defun xdg-desktop-strings (value)
200 "Partition VALUE into elements delimited by unescaped semicolons."
201 (let (res)
202 (save-match-data
203 (setq value (string-trim-left value))
204 (dolist (x (split-string (replace-regexp-in-string "\\\\;" "\0" value) ";"))
205 (push (replace-regexp-in-string "\0" ";" x) res)))
206 (when (null (string-match-p "[^[:blank:]]" (car res))) (pop res))
207 (nreverse res)))
208
150(provide 'xdg) 209(provide 'xdg)
151 210
152;;; xdg.el ends here 211;;; xdg.el ends here
diff --git a/test/data/xdg/test.desktop b/test/data/xdg/test.desktop
new file mode 100644
index 00000000000..b6dda62774a
--- /dev/null
+++ b/test/data/xdg/test.desktop
@@ -0,0 +1,3 @@
1# this is a comment
2[Desktop Entry]
3Name=Test
diff --git a/test/data/xdg/wrong.desktop b/test/data/xdg/wrong.desktop
new file mode 100644
index 00000000000..e0b4c221cf9
--- /dev/null
+++ b/test/data/xdg/wrong.desktop
@@ -0,0 +1,2 @@
1# the first section must be "Desktop Entry"
2[Why]
diff --git a/test/lisp/xdg-tests.el b/test/lisp/xdg-tests.el
new file mode 100644
index 00000000000..e7e122b54ee
--- /dev/null
+++ b/test/lisp/xdg-tests.el
@@ -0,0 +1,71 @@
1;;; xdg-tests.el --- tests for xdg.el -*- lexical-binding: t -*-
2
3;; Copyright (C) 2017 Free Software Foundation, Inc.
4
5;; Maintainer: emacs-devel@gnu.org
6
7;; Author: Mark Oteiza <mvoteiza@udel.edu>
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24;;; Commentary:
25
26;;; Code:
27
28(require 'ert)
29(require 'xdg)
30
31(defconst xdg-tests-data-dir
32 (expand-file-name "test/data/xdg" source-directory))
33
34(ert-deftest xdg-match-data ()
35 "Ensure public functions do not mangle match data."
36 (let ((data '(1 9)))
37 (save-match-data
38 (set-match-data data)
39 (xdg-user-dir "DOCUMENTS")
40 (should (equal (match-data) data))))
41 (let ((data '(2 9)))
42 (save-match-data
43 (set-match-data data)
44 (xdg-desktop-read-file (expand-file-name "test.desktop" xdg-tests-data-dir))
45 (should (equal (match-data) data))))
46 (let ((data '(3 9)))
47 (save-match-data
48 (set-match-data data)
49 (xdg-desktop-strings "a;b")
50 (should (equal (match-data) data)))))
51
52(ert-deftest xdg-desktop-parsing ()
53 "Test `xdg-desktop-read-file' parsing of .desktop files."
54 (let ((tab (xdg-desktop-read-file
55 (expand-file-name "test.desktop" xdg-tests-data-dir))))
56 (should (equal (gethash "Name" tab) "Test")))
57 (should-error
58 (xdg-desktop-read-file
59 (expand-file-name "wrong.desktop" xdg-tests-data-dir))))
60
61(ert-deftest xdg-desktop-strings-type ()
62 "Test desktop \"string(s)\" type: strings delimited by \";\"."
63 (should (equal (xdg-desktop-strings " a") '("a")))
64 (should (equal (xdg-desktop-strings "a;b") '("a" "b")))
65 (should (equal (xdg-desktop-strings "a;b;") '("a" "b")))
66 (should (equal (xdg-desktop-strings "\\;") '(";")))
67 (should (equal (xdg-desktop-strings ";") '("")))
68 (should (equal (xdg-desktop-strings " ") nil))
69 (should (equal (xdg-desktop-strings "a; ;") '("a" " "))))
70
71;;; xdg-tests.el ends here