24
|
1 ;;; xrdb-mode.el --- mode for editing X resource database files
|
|
2
|
|
3 ;; Author: 1994-1997 Barry A. Warsaw
|
|
4 ;; Maintainer: tools-help@python.org
|
|
5 ;; Created: May 1994
|
26
|
6 ;; Version: 1.21
|
|
7 ;; Last Modified: 1997/02/24 03:34:56
|
24
|
8 ;; Keywords: data languages
|
|
9
|
|
10 ;; Copyright (C) 1994 Barry A. Warsaw
|
|
11
|
|
12 ;; This file is not part of GNU Emacs.
|
|
13
|
|
14 ;; This program is free software; you can redistribute it and/or modify
|
|
15 ;; it under the terms of the GNU General Public License as published by
|
|
16 ;; the Free Software Foundation; either version 2 of the License, or
|
|
17 ;; (at your option) any later version.
|
|
18 ;;
|
|
19 ;; This program is distributed in the hope that it will be useful,
|
|
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22 ;; GNU General Public License for more details.
|
|
23 ;;
|
|
24 ;; You should have received a copy of the GNU General Public License
|
|
25 ;; along with this program; if not, write to the Free Software
|
|
26 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
27
|
|
28 ;;; Commentary:
|
|
29 ;;
|
|
30 ;; In 1994 I wrote:
|
|
31 ;;
|
|
32 ;; "I used to be like you. I used to hack on X resource database files
|
|
33 ;; all the time, and when I did, I found this mode to be fairly
|
|
34 ;; useful. It's by no means perfect. At one time I had a collection
|
|
35 ;; of hacks that did some nice indentation of resource lines, but
|
|
36 ;; they were not organized in any way. This mode was my attempt to
|
|
37 ;; congeal this mess into a proper major mode. I release it now, not
|
|
38 ;; because it will change your life, but because I don't plan to do
|
|
39 ;; anything more with it.
|
|
40 ;;
|
|
41 ;; I have since been enlightened and no longer have to cavort with
|
|
42 ;; mere mortal X hackers anymore. I like my brain cells, so now I
|
|
43 ;; use NEXTSTEP where all is glory. Or would you say I traded one
|
|
44 ;; vice for another? Hmm... Anyway, if you are still down in the
|
|
45 ;; trenches and would like to inherit this file, let me know. I
|
|
46 ;; don't intend to do any work on it any more... unless I lose my
|
|
47 ;; place in paradise. I promise to be good, Steve. :-) :-)"
|
|
48 ;;
|
26
|
49 ;; I have fallen from grace and have been kicked out of paradise. So
|
|
50 ;; has Steve Jobs apparently :-)
|
|
51 ;;
|
|
52 ;; To use, put the following in your .emacs:
|
|
53 ;;
|
|
54 ;; (autoload 'xrdb-mode "xrdb-mode" "Mode for editing X resource files" t)
|
|
55 ;;
|
|
56 ;; You may also want something like:
|
|
57 ;;
|
|
58 ;; (setq auto-mode-alist
|
|
59 ;; (append '(("\\.Xdefaults$" . xrdb-mode)
|
|
60 ;; ("\\.Xenvironment$" . xrdb-mode)
|
|
61 ;; ("\\.Xresources$" . xrdb-mode)
|
|
62 ;; )
|
|
63 ;; auto-mode-alist))
|
|
64
|
24
|
65
|
|
66 ;; Code:
|
|
67
|
|
68
|
|
69 ;; These variables are available for your customization
|
134
|
70 (defgroup xrdb nil
|
|
71 "Mode for editing X resource database files."
|
|
72 :group 'languages)
|
24
|
73
|
134
|
74 (defcustom xrdb-mode-hook nil
|
|
75 "*Hook to be run when `xrdb-mode' is entered."
|
|
76 :type 'hook
|
|
77 :group 'xrdb)
|
|
78
|
|
79 (defcustom xrdb-subdivide-by 'paragraph
|
24
|
80 "*Extent of alignment calculations.
|
|
81 Can be one of `buffer', `paragraph', `page', or `line'. Do a
|
134
|
82 \\[describe-function] xrdb-indent-buffer RET for more information."
|
|
83 :type '(radio (const buffer) (const paragraph)
|
|
84 (const page) (const line))
|
|
85 :group 'xrdb)
|
24
|
86
|
|
87
|
|
88
|
|
89 ;; no need to customize anything below this line
|
|
90 (defconst xrdb-comment-re "^[ \t]*[!]"
|
|
91 "Character which starts a comment.")
|
|
92 (defconst xrdb-separator-char ?:
|
|
93 "Character which separates resource specs from values.")
|
|
94
|
|
95
|
|
96 ;; utilities
|
|
97 (defsubst xrdb-point (position)
|
|
98 ;; Returns the value of point at certain commonly referenced POSITIONs.
|
|
99 ;; POSITION can be one of the following symbols:
|
|
100 ;;
|
|
101 ;; bol -- beginning of line
|
|
102 ;; eol -- end of line
|
|
103 ;; bod -- beginning of defun
|
|
104 ;; boi -- back to indentation
|
|
105 ;; ionl -- indentation of next line
|
|
106 ;; iopl -- indentation of previous line
|
|
107 ;; bonl -- beginning of next line
|
|
108 ;; bopl -- beginning of previous line
|
|
109 ;; bop -- beginning of paragraph
|
|
110 ;; eop -- end of paragraph
|
|
111 ;; bopg -- beginning of page
|
|
112 ;; eopg -- end of page
|
|
113 ;;
|
|
114 ;; This function does not modify point or mark.
|
|
115 (let ((here (point)))
|
|
116 (cond
|
|
117 ((eq position 'bol) (beginning-of-line))
|
|
118 ((eq position 'eol) (end-of-line))
|
|
119 ((eq position 'boi) (back-to-indentation))
|
|
120 ((eq position 'bonl) (forward-line 1))
|
|
121 ((eq position 'bopl) (forward-line -1))
|
|
122 ((eq position 'bop) (forward-paragraph -1))
|
|
123 ((eq position 'eop) (forward-paragraph 1))
|
|
124 ((eq position 'bopg) (forward-page -1))
|
|
125 ((eq position 'eopg) (forward-page 1))
|
|
126 (t
|
|
127 (error "unknown buffer position requested: %s" position)))
|
|
128 (prog1
|
|
129 (point)
|
|
130 (goto-char here))
|
|
131 ))
|
|
132
|
|
133 (defsubst xrdb-skip-to-separator ()
|
|
134 ;; skip forward from the beginning of the line to the separator
|
|
135 ;; character as given by xrdb-separator-char. Returns t if the
|
|
136 ;; char was found, otherwise, nil.
|
|
137 (beginning-of-line)
|
|
138 (skip-chars-forward
|
|
139 (concat "^" (char-to-string xrdb-separator-char))
|
|
140 (xrdb-point 'eol))
|
|
141 (= (following-char) xrdb-separator-char))
|
|
142
|
|
143
|
|
144
|
|
145 ;; commands
|
|
146 (defun xrdb-electric-separator (arg)
|
|
147 "Insert the separator character.
|
|
148 Re-align the line unless an argument is given."
|
|
149 (interactive "P")
|
|
150 ;; first insert the character
|
|
151 (self-insert-command (prefix-numeric-value arg))
|
|
152 ;; only do electric behavior if arg is not given
|
|
153 (if (not arg)
|
|
154 (xrdb-align-to (xrdb-point 'bol)
|
|
155 (xrdb-point 'bonl)
|
|
156 (save-excursion
|
|
157 (beginning-of-line)
|
|
158 (forward-comment (- (point-max)))
|
|
159 (beginning-of-line)
|
|
160 (xrdb-skip-to-separator)
|
|
161 (current-column)))))
|
|
162
|
|
163 (defun xrdb-align-to (start end goalcolumn)
|
|
164 (interactive "r\nnAlign to column: ")
|
|
165 (save-excursion
|
|
166 (save-restriction
|
|
167 (narrow-to-region start end)
|
|
168 (beginning-of-buffer)
|
|
169 (while (< (point) (point-max))
|
|
170 (if (and (not (looking-at xrdb-comment-re))
|
|
171 (xrdb-skip-to-separator)
|
|
172 goalcolumn)
|
|
173 (indent-code-rigidly (xrdb-point 'bol)
|
|
174 (xrdb-point 'bonl)
|
|
175 (- goalcolumn (current-column))))
|
|
176 (forward-line 1)))))
|
|
177
|
|
178 (defun xrdb-indent-line (arg)
|
|
179 "Re-align current line."
|
|
180 (interactive "P")
|
|
181 ;; narrow to the region specified by xrdb-subdivide-by
|
|
182 (save-excursion
|
|
183 (save-restriction
|
|
184 (widen)
|
|
185 (cond
|
|
186 ((eq xrdb-subdivide-by 'buffer))
|
|
187 ((eq xrdb-subdivide-by 'page)
|
|
188 (narrow-to-page))
|
|
189 ((eq xrdb-subdivide-by 'paragraph)
|
|
190 (narrow-to-region (xrdb-point 'bop) (xrdb-point 'eop)))
|
|
191 (t
|
|
192 (narrow-to-region (xrdb-point 'bopl) (xrdb-point 'bonl))
|
|
193 ))
|
|
194 ;; indent line
|
|
195 (xrdb-align-to (xrdb-point 'bol) (xrdb-point 'bonl)
|
|
196 (xrdb-region-goal-column))
|
|
197 )))
|
|
198
|
|
199 (defun xrdb-indent-region (start end)
|
|
200 "Re-align region."
|
|
201 (interactive "r")
|
|
202 ;; narrow to region
|
|
203 (save-excursion
|
|
204 (save-restriction
|
|
205 (narrow-to-region start end)
|
|
206 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
207 )))
|
|
208
|
|
209 (defun xrdb-indent-page ()
|
|
210 "Re-align the current page."
|
|
211 (interactive)
|
|
212 (save-excursion
|
|
213 (save-restriction
|
|
214 (narrow-to-page)
|
|
215 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
216 )))
|
|
217
|
|
218 (defun xrdb-indent-paragraph ()
|
|
219 "Re-align the current paragraph."
|
|
220 (interactive)
|
|
221 (save-excursion
|
|
222 (save-restriction
|
|
223 (narrow-to-region (xrdb-point 'bop) (xrdb-point 'eop))
|
|
224 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
225 )))
|
|
226
|
|
227 (defun xrdb-indent-buffer (arg)
|
|
228 "Re-align the entire buffer.
|
|
229 Alignment calculations are controlled by the variable
|
|
230 `xrdb-subdivide-by', which can take the values `buffer', `paragraph',
|
|
231 `page', or `line', with the following meanings:
|
|
232
|
|
233 buffer - all non-comment lines are aligned with the longest line in
|
|
234 the buffer. Since every line must be scanned, this will
|
|
235 take the longest to perform.
|
|
236
|
|
237 paragraph - alignment of lines spanning paragraphs. A paragraph is
|
|
238 defined as all contiguous lines between blank or comment
|
|
239 lines.
|
|
240
|
|
241 page - alignment of lines spanning pages (i.e. separated by
|
|
242 page-delimiter, usually ^L).
|
|
243
|
|
244 none - alignment of lines based on the previous line.
|
|
245
|
|
246 With optional \\[universal-argument], queries for alignment subdivision."
|
|
247 (interactive "P")
|
|
248 (let ((align-by (if (not arg)
|
|
249 xrdb-subdivide-by
|
|
250 (completing-read
|
|
251 "Align by: "
|
|
252 '(("buffer" . buffer)
|
|
253 ("paragraph" . paragraph)
|
|
254 ("page" . page)
|
|
255 ("line" . line))
|
|
256 nil t (format "%s" xrdb-subdivide-by)))))
|
|
257 (message "Aligning by %s..." align-by)
|
|
258 (save-excursion
|
|
259 (save-restriction
|
|
260 (widen)
|
|
261 (cond
|
|
262 ;; by buffer
|
|
263 ((eq align-by 'buffer)
|
|
264 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column)))
|
|
265 ;; by paragraph
|
|
266 ((eq align-by 'paragraph)
|
|
267 (beginning-of-buffer)
|
|
268 (while (< (point) (point-max))
|
|
269 (narrow-to-region (point) (xrdb-point 'eop))
|
|
270 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
271 (beginning-of-buffer)
|
|
272 (widen)
|
|
273 (forward-paragraph 1)
|
|
274 ))
|
|
275 ;; by page
|
|
276 ((eq align-by 'page)
|
|
277 (beginning-of-buffer)
|
|
278 (while (< (point) (point-max))
|
|
279 (narrow-to-region (point) (xrdb-point 'eopg))
|
|
280 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
281 (beginning-of-buffer)
|
|
282 (widen)
|
|
283 (forward-page 1)
|
|
284 ))
|
|
285 ;; by line
|
|
286 (t
|
|
287 (beginning-of-buffer)
|
|
288 (let ((prev-goalcol 0))
|
|
289 (while (< (point) (point-max))
|
|
290 ;; skip comments and blank lines
|
|
291 (if (not (looking-at paragraph-start))
|
|
292 (progn
|
|
293 (xrdb-align-to (xrdb-point 'bol) (xrdb-point 'bonl)
|
|
294 prev-goalcol)
|
|
295 (xrdb-skip-to-separator)
|
|
296 (setq prev-goalcol (- (point) (xrdb-point 'boi)))
|
|
297 ))
|
|
298 (forward-line 1))))
|
|
299 )))
|
|
300 (message "Aligning by %s... done." align-by)
|
|
301 ))
|
|
302
|
|
303
|
|
304 ;; major-mode stuff
|
|
305 (defvar xrdb-mode-abbrev-table nil
|
|
306 "Abbrev table used in `xrdb-mode' buffers.")
|
|
307 (define-abbrev-table 'xrdb-mode-abbrev-table ())
|
|
308
|
|
309
|
|
310 (defvar xrdb-mode-syntax-table nil
|
|
311 "Syntax table used in `xrdb-mode' buffers.")
|
|
312 (if xrdb-mode-syntax-table
|
|
313 ()
|
|
314 (setq xrdb-mode-syntax-table (make-syntax-table))
|
|
315 (modify-syntax-entry ?! "<" xrdb-mode-syntax-table)
|
|
316 (modify-syntax-entry ?\n ">" xrdb-mode-syntax-table))
|
|
317
|
|
318
|
|
319 (defvar xrdb-mode-map ()
|
|
320 "Keymap used in `xrdb-mode' buffers.")
|
|
321 (if xrdb-mode-map
|
|
322 ()
|
|
323 (setq xrdb-mode-map (make-sparse-keymap))
|
|
324 (let ((ekey (char-to-string xrdb-separator-char)))
|
|
325 ;; make the separator key electric
|
|
326 (define-key xrdb-mode-map ekey 'xrdb-electric-separator)
|
|
327 (define-key xrdb-mode-map "\t" 'xrdb-indent-line)
|
|
328 (define-key xrdb-mode-map "\C-c\C-a" 'xrdb-indent-paragraph)
|
|
329 (define-key xrdb-mode-map "\C-c\C-b" 'xrdb-submit-bug-report)
|
|
330 (define-key xrdb-mode-map "\C-c\C-p" 'xrdb-indent-page)
|
|
331 (define-key xrdb-mode-map "\C-c\C-r" 'xrdb-indent-region)
|
|
332 (define-key xrdb-mode-map "\C-c\C-u" 'xrdb-indent-buffer)
|
|
333 (define-key xrdb-mode-map "\C-c>" 'xrdb-align-to)
|
|
334 ))
|
|
335
|
|
336 (defun xrdb-mode ()
|
|
337 "Major mode for editing xrdb config files"
|
|
338 (interactive)
|
|
339 (kill-all-local-variables)
|
|
340 (set-syntax-table xrdb-mode-syntax-table)
|
|
341 (setq major-mode 'xrdb-mode
|
|
342 mode-name "xrdb"
|
|
343 local-abbrev-table xrdb-mode-abbrev-table)
|
|
344 (use-local-map xrdb-mode-map)
|
|
345 ;; local variables
|
|
346 (make-local-variable 'parse-sexp-ignore-comments)
|
|
347 (make-local-variable 'comment-start)
|
|
348 (make-local-variable 'comment-end)
|
|
349 (make-local-variable 'paragraph-start)
|
|
350 (make-local-variable 'paragraph-separate)
|
|
351 (make-local-variable 'paragraph-ignore-fill-prefix)
|
|
352 ;; now set their values
|
|
353 (setq parse-sexp-ignore-comments t
|
|
354 comment-start "! "
|
|
355 comment-end "")
|
|
356 (setq indent-region-function 'xrdb-indent-region
|
|
357 paragraph-ignore-fill-prefix t
|
|
358 paragraph-start (concat "^[ \t]*$\\|^[ \t]*[!]\\|" page-delimiter)
|
|
359 paragraph-separate paragraph-start)
|
|
360 (run-hooks 'xrdb-mode-hook))
|
|
361
|
|
362
|
|
363
|
|
364 ;; faces and font-locking
|
26
|
365 (defvar xrdb-option-name-face 'xrdb-option-name-face
|
|
366 "Face for option name on a line in an X resource db file")
|
|
367 (defvar xrdb-option-value-face 'xrdb-option-value-face
|
|
368 "Face for option value on a line in an X resource db file")
|
|
369
|
24
|
370 (make-face 'xrdb-option-name-face)
|
|
371 (make-face 'xrdb-option-value-face)
|
26
|
372
|
|
373 (defun xrdb-font-lock-mode-hook ()
|
|
374 (or (face-differs-from-default-p 'xrdb-option-name-face)
|
|
375 (copy-face 'font-lock-keyword-face 'xrdb-option-name-face))
|
|
376 (or (face-differs-from-default-p 'xrdb-option-value-face)
|
|
377 (copy-face 'font-lock-string-face 'xrdb-option-value-face))
|
|
378 (remove-hook 'font-lock-mode-hook 'xrdb-font-lock-mode-hook))
|
|
379 (add-hook 'font-lock-mode-hook 'xrdb-font-lock-mode-hook)
|
24
|
380
|
|
381 (defvar xrdb-font-lock-keywords
|
|
382 (list '("^[ \t]*\\([^\n:]*:\\)[ \t]*\\(.*\\)$"
|
|
383 (1 xrdb-option-name-face)
|
|
384 (2 xrdb-option-value-face)))
|
26
|
385 "Additional expressions to highlight in X resource db mode.")
|
|
386 (put 'xrdb-mode 'font-lock-defaults '(xrdb-font-lock-keywords))
|
24
|
387
|
|
388
|
|
389
|
|
390 ;; commands
|
|
391 (defun xrdb-region-goal-column ()
|
|
392 ;; Returns the goal column of the current region. Assumes the
|
|
393 ;; buffer has been narrowed to the region to scan.
|
|
394 (save-excursion
|
|
395 (beginning-of-buffer)
|
|
396 (let ((goalcol -1)
|
|
397 linecol)
|
|
398 (while (< (point) (point-max))
|
|
399 ;; skip any comments
|
|
400 (if (and (not (looking-at xrdb-comment-re))
|
|
401 (xrdb-skip-to-separator)
|
|
402 (< goalcol (setq linecol (current-column)))
|
|
403 )
|
|
404 (setq goalcol linecol))
|
|
405 (forward-line 1))
|
|
406 (if (< goalcol 0)
|
|
407 nil
|
|
408 goalcol))))
|
|
409
|
|
410
|
|
411
|
|
412 ;; submitting bug reports
|
|
413
|
26
|
414 (defconst xrdb-version "1.21"
|
24
|
415 "xrdb-mode version number.")
|
|
416
|
|
417 (defconst xrdb-mode-help-address "tools-help@python.org"
|
|
418 "Address for xrdb-mode bug reports.")
|
|
419
|
|
420 (eval-when-compile
|
|
421 (require 'reporter))
|
|
422
|
|
423 (defun xrdb-submit-bug-report ()
|
|
424 "Submit via mail a bug report on xrdb-mode."
|
|
425 (interactive)
|
|
426 ;; load in reporter
|
|
427 (let ((reporter-prompt-for-summary-p t)
|
|
428 (varlist '(xrdb-subdivide-by
|
|
429 xrdb-mode-hook
|
|
430 )))
|
|
431 (and (if (y-or-n-p "Do you want to submit a report on xrdb-mode? ")
|
|
432 t
|
|
433 (message "")
|
|
434 nil)
|
|
435 (require 'reporter)
|
|
436 (reporter-submit-bug-report
|
|
437 xrdb-mode-help-address "xrdb-mode" varlist nil nil "Dear Barry,")
|
|
438 )))
|
|
439
|
|
440
|
|
441 (provide 'xrdb-mode)
|
|
442 ;; xrdb-mode.el ends here
|