Commit 4131f978 authored by Mark Oteiza's avatar Mark Oteiza
Browse files

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.
parent 68b7ecba
......@@ -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)
......
# unacceptable key=value format
[Desktop Entry]
Key=value
aowef faoweif of
# this is a comment
[Desktop Entry]
Name=Test
[Another Section]
Exec=frobnicate
......@@ -32,12 +32,20 @@
(ert-deftest xdg-desktop-parsing ()
"Test `xdg-desktop-read-file' parsing of .desktop files."
(let ((tab (xdg-desktop-read-file
(expand-file-name "test.desktop" xdg-tests-data-dir))))
(should (equal (gethash "Name" tab) "Test")))
(let ((tab1 (xdg-desktop-read-file
(expand-file-name "test.desktop" xdg-tests-data-dir)))
(tab2 (xdg-desktop-read-file
(expand-file-name "test.desktop" xdg-tests-data-dir)
"Another Section")))
(should (equal (gethash "Name" tab1) "Test"))
(should (eq 'default (gethash "Exec" tab1 'default)))
(should (equal "frobnicate" (gethash "Exec" tab2))))
(should-error
(xdg-desktop-read-file
(expand-file-name "wrong.desktop" xdg-tests-data-dir))))
(expand-file-name "wrong.desktop" xdg-tests-data-dir)))
(should-error
(xdg-desktop-read-file
(expand-file-name "malformed.desktop" xdg-tests-data-dir))))
(ert-deftest xdg-desktop-strings-type ()
"Test desktop \"string(s)\" type: strings delimited by \";\"."
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment