diff options
author | Mark Oteiza <mvoteiza@udel.edu> | 2017-09-09 11:55:09 -0400 |
---|---|---|
committer | Mark Oteiza <mvoteiza@udel.edu> | 2017-09-09 12:26:50 -0400 |
commit | 4131f9785e30f2a31745125c714e922892113c62 (patch) | |
tree | 56858ce56448c799997008648b23e647cebd526d /lisp | |
parent | 68b7ecbac1dcb5bfcace5505a4d354777147dd54 (diff) | |
download | emacs-4131f9785e30f2a31745125c714e922892113c62.tar.gz |
Add function to read all entries in a group
Use that to extend xdg-desktop-read-file. Also fix a bug where all
entries in all groups were read and returned by xdg-desktop-read-file.
* lisp/xdg.el (xdg-desktop-read-group): New function.
(xdg-desktop-read-file): Use it.
* test/data/xdg/malformed.desktop: New file.
* test/data/xdg/test.desktop: Add another section.
* test/lisp/xdg-tests.el (xdg-desktop-parsing): Test presence of a key
in another group. Test reading a prescribed group. Test detecting a
malformed key=value.
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/xdg.el | 58 |
1 files changed, 33 insertions, 25 deletions
diff --git a/lisp/xdg.el b/lisp/xdg.el index c7000219487..102e34cb0ce 100644 --- a/lisp/xdg.el +++ b/lisp/xdg.el @@ -167,33 +167,41 @@ This should be called at the beginning of a line." (group-n 2 (* nonl))) "Regexp matching desktop file entry key-value pairs.") -(defun xdg--desktop-parse-line () - (skip-chars-forward "[:blank:]") - (when (/= (following-char) ?#) - (cond - ((looking-at xdg-desktop-entry-regexp) - (cons (match-string 1) (match-string 2))) - ((looking-at xdg-desktop-group-regexp) - (match-string 1))))) - -(defun xdg-desktop-read-file (filename) - "Return \"Desktop Entry\" contents of desktop file FILENAME as a hash table." - (let ((res (make-hash-table :test #'equal)) - elt group) - (with-temp-buffer - (insert-file-contents-literally filename) - (goto-char (point-min)) - (while (or (= (following-char) ?#) - (string-blank-p (buffer-substring (point) (point-at-eol)))) - (forward-line)) - (unless (equal (setq group (xdg--desktop-parse-line)) "Desktop Entry") - (error "Wrong first section: %s" group)) - (while (not (eobp)) - (when (consp (setq elt (xdg--desktop-parse-line))) - (puthash (car elt) (cdr elt) res)) - (forward-line))) +(defun xdg-desktop-read-group () + "Return hash table of group of desktop entries in the current buffer." + (let ((res (make-hash-table :test #'equal))) + (while (not (or (eobp) (looking-at xdg-desktop-group-regexp))) + (skip-chars-forward "[:blank:]") + (cond + ((eolp)) + ((= (following-char) ?#)) + ((looking-at xdg-desktop-entry-regexp) + (puthash (match-string 1) (match-string 2) res)) + (t (error "Malformed line: %s" + (buffer-substring (point) (point-at-eol))))) + (forward-line)) res)) +(defun xdg-desktop-read-file (filename &optional group) + "Return group contents of desktop file FILENAME as a hash table. +Optional argument GROUP defaults to the string \"Desktop Entry\"." + (with-temp-buffer + (insert-file-contents-literally filename) + (goto-char (point-min)) + (while (and (skip-chars-forward "[:blank:]" (line-end-position)) + (or (eolp) (= (following-char) ?#))) + (forward-line)) + (unless (looking-at xdg-desktop-group-regexp) + (error "Expected group name! Instead saw: %s" + (buffer-substring (point) (point-at-eol)))) + (unless (equal (match-string 1) "Desktop Entry") + (error "Wrong first group: %s" (match-string 1))) + (when group + (while (and (re-search-forward xdg-desktop-group-regexp nil t) + (not (equal (match-string 1) group))))) + (forward-line) + (xdg-desktop-read-group))) + (defun xdg-desktop-strings (value) "Partition VALUE into elements delimited by unescaped semicolons." (let (res) |