diff options
| author | Mark Oteiza | 2017-09-06 13:17:05 -0400 |
|---|---|---|
| committer | Mark Oteiza | 2017-09-06 13:17:05 -0400 |
| commit | 9604f9cd33bcbc921fd18e894fdd8a98012fd09d (patch) | |
| tree | e8c152189ae6f439fa57496dfc76596418543e08 /lisp | |
| parent | da3e1016349b2f552f149ccf577b60e377c3095a (diff) | |
| download | emacs-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.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/xdg.el | 73 |
1 files changed, 66 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 |