summaryrefslogtreecommitdiff
path: root/misc/desktop-entry-mode.el
blob: 618ad051a37aaab64bd72bb7df5c302fd5b3e0f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
;;; desktop-entry-mode.el --- freedesktop.org desktop entry editing

;; Copyright (C) 2003-2004, 2006-2007 Ville Skyttä, <scop at xemacs.org>

;; Author:   Ville Skyttä, <scop at xemacs.org>
;; Keywords: unix, desktop entry

;; This file is part of XEmacs.

;; XEmacs is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; XEmacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301 USA.

;;; Commentary:

;; This mode provides basic functionality, eg. syntax highlighting and
;; validation for freedesktop.org desktop entry files.
;;
;; To install it:
;;
;;   In XEmacs:
;;   Just install the XEmacs `text-modes' package, this mode is included.
;;   See <http://www.xemacs.org/Documentation/packageGuide.html>.
;;
;;   In GNU Emacs:
;;   Place this file in your load path somewhere (eg. site-lisp), and add
;;   the following to your .emacs:
;;
;;   (autoload 'desktop-entry-mode "desktop-entry-mode" "Desktop Entry mode" t)
;;   (add-to-list 'auto-mode-alist
;;                '("\\.desktop\\(\\.in\\)?$" . desktop-entry-mode))
;;   (add-hook 'desktop-entry-mode-hook 'turn-on-font-lock)
;;
;; For more information about desktop entry files, see
;;   <http://www.freedesktop.org/Standards/desktop-entry-spec>
;;
;; This version is up to date with version 1.0 of the specification.

;;; Code:

(eval-when-compile
  (require 'regexp-opt))

(defconst desktop-entry-mode-version "1.0 (spec 1.0)"
  "Version of `desktop-entry-mode'.")

(defgroup desktop-entry nil
  "Support for editing freedesktop.org desktop entry files."
  :group 'languages)

(defcustom desktop-entry-validate-command "desktop-file-validate"
  "*Command for validating desktop entry files."
  :type 'string
  :group 'desktop-entry)

(defgroup desktop-entry-faces nil
  "Font lock faces for `desktop-entry-mode'."
  :prefix "desktop-entry-"
  :group 'desktop-entry
  :group 'faces)

(defface desktop-entry-group-header-face
  '((((class color) (background light)) (:foreground "mediumblue" :bold t))
    (((class color) (background dark)) (:foreground "lightblue" :bold t))
    (t (:bold t)))
  "*Face for highlighting desktop entry group headers."
  :group 'desktop-entry-faces)

(defface desktop-entry-deprecated-keyword-face
  '((((class color)) (:background "yellow" :foreground "black" :strikethru t))
    )
  "*Face for highlighting deprecated desktop entry keys."
  :group 'desktop-entry-faces)

(defface desktop-entry-unknown-keyword-face
  '((((class color)) (:foreground "red3" :underline t))
    (t (:underline t))
    )
  "*Face for highlighting unknown desktop entry keys."
  :group 'desktop-entry-faces)

(defface desktop-entry-value-face
  '((((class color) (background light)) (:foreground "darkgreen"))
    (((class color) (background dark)) (:foreground "lightgreen"))
    )
  "*Face for highlighting desktop entry values."
  :group 'desktop-entry-faces)

(defface desktop-entry-locale-face
  '((((class color) (background light)) (:foreground "dimgray"))
    (((class color) (background dark)) (:foreground "lightgray"))
    )
  "*Face for highlighting desktop entry locales."
  :group 'desktop-entry-faces)

(defconst desktop-entry-keywords
  (eval-when-compile
    (concat
     "\\(?:"
     (regexp-opt
      '(
        "Type"
        "Version"
        "Name"
        "GenericName"
        "NoDisplay"
        "Comment"
        "Icon"
        "Hidden"
        "OnlyShowIn"
        "NotShowIn"
        "TryExec"
        "Exec"
        "Path"
        "Terminal"
        "MimeType"
        "Categories"
        "StartupNotify"
        "StartupWMClass"
        "URL"
        ;; Reserved for use with KDE
        "ServiceTypes"
        "DocPath"
        "KeyWords"
        "InitialPreference"
        ;; Used by KDE for entries of the FSDevice type
        "Dev"
        "FSType"
        "MountPoint"
        "ReadOnly"
        "UnmountIcon"
        ) 'words)
     "\\|X-[A-Za-z0-9-]+\\)"))
  "Expression for matching desktop entry keys.")

(defconst desktop-entry-deprecated-keywords
  (eval-when-compile
    (concat
     "\\(\\<Type\\s-*=\\s-*MimeType\\>\\|"
     (regexp-opt
      '(
        "Patterns"
        "DefaultApp"
        "Encoding"
        "MiniIcon"
        "TerminalOptions"
        "Protocols"
        "Extensions"
        "BinaryPattern"
        "MapNotify"
        "SwallowTitle"
        "SwallowExec"
        "SortOrder"
        "FilePattern"
        ) 'words)
     "\\)"))
  "Expression for matching deprecated desktop entry keys.")

(defconst desktop-entry-group-header-re
  "^\\[\\(X-[^\][]+\\|\\(?:Desktop \\(?:Entry\\|Action [a-zA-Z-]+\\)\\)\\)\\]"
  "Regular expression for matching desktop entry group headers.")

(defconst desktop-entry-font-lock-keywords
  (list
   (cons "^\\s-*#.*$" '(0 'font-lock-comment-face))
   (cons (concat "^" desktop-entry-deprecated-keywords)
         '(0 'desktop-entry-deprecated-keyword-face))
   (cons (concat "^" desktop-entry-keywords) '(0 'font-lock-keyword-face))
   (cons "^[A-Za-z0-9-]+" '(0 'desktop-entry-unknown-keyword-face))
   (cons desktop-entry-group-header-re '(1 'desktop-entry-group-header-face))
   (cons "^[A-Za-z0-9-]+?\\s-*=\\s-*\\(.*\\)"
         '(1 'desktop-entry-value-face))
   (cons "^[A-Za-z0-9-]+?\\[\\([^\]]+\\)\\]\\s-*=\\s-*\\(.*\\)"
         '((1 'desktop-entry-locale-face)
           (2 'desktop-entry-value-face)))
   )
  "Highlighting rules for `desktop-entry-mode' buffers.")

(defvar desktop-entry-imenu-generic-expression
  `((nil ,desktop-entry-group-header-re 1))
  "Imenu generic expression for `desktop-entry-mode'.
See `imenu-generic-expression'.")

;;;###autoload
(defun desktop-entry-mode ()
  "Major mode for editing freedesktop.org desktop entry files.
See <http://www.freedesktop.org/Standards/desktop-entry-spec> for more
information.  See `desktop-entry-mode-version' for information about which
version of the specification this mode is up to date with.

Turning on desktop entry mode calls the value of the variable
`desktop-entry-mode-hook' with no args, if that value is non-nil."
  (interactive)
  (set (make-local-variable 'imenu-generic-expression)
       '((nil "^\\s-*\\(.*\\)\\s-*=" 1)))
  (set (make-local-variable 'compile-command)
       (concat desktop-entry-validate-command " " buffer-file-name))
  (set (make-local-variable 'compilation-buffer-name-function)
       (lambda (x) (concat "*desktop-file-validate "
                           (file-name-nondirectory buffer-file-name) "*")))
  (set (make-local-variable 'comment-start) "# ")
  (set (make-local-variable 'comment-end) "")
  (set (make-local-variable 'comment-start-skip) "#+ *")
  (setq major-mode 'desktop-entry-mode mode-name "Desktop Entry")
  (set (make-local-variable 'imenu-generic-expression)
       desktop-entry-imenu-generic-expression)
  (unless (featurep 'xemacs) ;; font-lock support for GNU Emacs
    (set (make-local-variable 'font-lock-defaults)
         '(desktop-entry-font-lock-keywords)))
  (run-hooks 'desktop-entry-mode-hook))

(defun desktop-entry-validate ()
  "Validate desktop entry in the current buffer."
  (interactive)
  (require 'compile)
  (compile compile-command))

;;;###autoload(add-to-list 'auto-mode-alist '("\\.desktop\\(\\.in\\)?$" . desktop-entry-mode))

(provide 'desktop-entry-mode)

;;; desktop-entry-mode.el ends here