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
|
|
70 (defvar xrdb-mode-hook nil
|
|
71 "*Hook to be run when `xrdb-mode' is entered.")
|
|
72
|
|
73 (defvar xrdb-subdivide-by 'paragraph
|
|
74 "*Extent of alignment calculations.
|
|
75 Can be one of `buffer', `paragraph', `page', or `line'. Do a
|
|
76 \\[describe-function] xrdb-indent-buffer RET for more information.")
|
|
77
|
|
78
|
|
79
|
|
80 ;; no need to customize anything below this line
|
|
81 (defconst xrdb-comment-re "^[ \t]*[!]"
|
|
82 "Character which starts a comment.")
|
|
83 (defconst xrdb-separator-char ?:
|
|
84 "Character which separates resource specs from values.")
|
|
85
|
|
86
|
|
87 ;; utilities
|
|
88 (defsubst xrdb-point (position)
|
|
89 ;; Returns the value of point at certain commonly referenced POSITIONs.
|
|
90 ;; POSITION can be one of the following symbols:
|
|
91 ;;
|
|
92 ;; bol -- beginning of line
|
|
93 ;; eol -- end of line
|
|
94 ;; bod -- beginning of defun
|
|
95 ;; boi -- back to indentation
|
|
96 ;; ionl -- indentation of next line
|
|
97 ;; iopl -- indentation of previous line
|
|
98 ;; bonl -- beginning of next line
|
|
99 ;; bopl -- beginning of previous line
|
|
100 ;; bop -- beginning of paragraph
|
|
101 ;; eop -- end of paragraph
|
|
102 ;; bopg -- beginning of page
|
|
103 ;; eopg -- end of page
|
|
104 ;;
|
|
105 ;; This function does not modify point or mark.
|
|
106 (let ((here (point)))
|
|
107 (cond
|
|
108 ((eq position 'bol) (beginning-of-line))
|
|
109 ((eq position 'eol) (end-of-line))
|
|
110 ((eq position 'boi) (back-to-indentation))
|
|
111 ((eq position 'bonl) (forward-line 1))
|
|
112 ((eq position 'bopl) (forward-line -1))
|
|
113 ((eq position 'bop) (forward-paragraph -1))
|
|
114 ((eq position 'eop) (forward-paragraph 1))
|
|
115 ((eq position 'bopg) (forward-page -1))
|
|
116 ((eq position 'eopg) (forward-page 1))
|
|
117 (t
|
|
118 (error "unknown buffer position requested: %s" position)))
|
|
119 (prog1
|
|
120 (point)
|
|
121 (goto-char here))
|
|
122 ))
|
|
123
|
|
124 (defsubst xrdb-skip-to-separator ()
|
|
125 ;; skip forward from the beginning of the line to the separator
|
|
126 ;; character as given by xrdb-separator-char. Returns t if the
|
|
127 ;; char was found, otherwise, nil.
|
|
128 (beginning-of-line)
|
|
129 (skip-chars-forward
|
|
130 (concat "^" (char-to-string xrdb-separator-char))
|
|
131 (xrdb-point 'eol))
|
|
132 (= (following-char) xrdb-separator-char))
|
|
133
|
|
134
|
|
135
|
|
136 ;; commands
|
|
137 (defun xrdb-electric-separator (arg)
|
|
138 "Insert the separator character.
|
|
139 Re-align the line unless an argument is given."
|
|
140 (interactive "P")
|
|
141 ;; first insert the character
|
|
142 (self-insert-command (prefix-numeric-value arg))
|
|
143 ;; only do electric behavior if arg is not given
|
|
144 (if (not arg)
|
|
145 (xrdb-align-to (xrdb-point 'bol)
|
|
146 (xrdb-point 'bonl)
|
|
147 (save-excursion
|
|
148 (beginning-of-line)
|
|
149 (forward-comment (- (point-max)))
|
|
150 (beginning-of-line)
|
|
151 (xrdb-skip-to-separator)
|
|
152 (current-column)))))
|
|
153
|
|
154 (defun xrdb-align-to (start end goalcolumn)
|
|
155 (interactive "r\nnAlign to column: ")
|
|
156 (save-excursion
|
|
157 (save-restriction
|
|
158 (narrow-to-region start end)
|
|
159 (beginning-of-buffer)
|
|
160 (while (< (point) (point-max))
|
|
161 (if (and (not (looking-at xrdb-comment-re))
|
|
162 (xrdb-skip-to-separator)
|
|
163 goalcolumn)
|
|
164 (indent-code-rigidly (xrdb-point 'bol)
|
|
165 (xrdb-point 'bonl)
|
|
166 (- goalcolumn (current-column))))
|
|
167 (forward-line 1)))))
|
|
168
|
|
169 (defun xrdb-indent-line (arg)
|
|
170 "Re-align current line."
|
|
171 (interactive "P")
|
|
172 ;; narrow to the region specified by xrdb-subdivide-by
|
|
173 (save-excursion
|
|
174 (save-restriction
|
|
175 (widen)
|
|
176 (cond
|
|
177 ((eq xrdb-subdivide-by 'buffer))
|
|
178 ((eq xrdb-subdivide-by 'page)
|
|
179 (narrow-to-page))
|
|
180 ((eq xrdb-subdivide-by 'paragraph)
|
|
181 (narrow-to-region (xrdb-point 'bop) (xrdb-point 'eop)))
|
|
182 (t
|
|
183 (narrow-to-region (xrdb-point 'bopl) (xrdb-point 'bonl))
|
|
184 ))
|
|
185 ;; indent line
|
|
186 (xrdb-align-to (xrdb-point 'bol) (xrdb-point 'bonl)
|
|
187 (xrdb-region-goal-column))
|
|
188 )))
|
|
189
|
|
190 (defun xrdb-indent-region (start end)
|
|
191 "Re-align region."
|
|
192 (interactive "r")
|
|
193 ;; narrow to region
|
|
194 (save-excursion
|
|
195 (save-restriction
|
|
196 (narrow-to-region start end)
|
|
197 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
198 )))
|
|
199
|
|
200 (defun xrdb-indent-page ()
|
|
201 "Re-align the current page."
|
|
202 (interactive)
|
|
203 (save-excursion
|
|
204 (save-restriction
|
|
205 (narrow-to-page)
|
|
206 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
207 )))
|
|
208
|
|
209 (defun xrdb-indent-paragraph ()
|
|
210 "Re-align the current paragraph."
|
|
211 (interactive)
|
|
212 (save-excursion
|
|
213 (save-restriction
|
|
214 (narrow-to-region (xrdb-point 'bop) (xrdb-point 'eop))
|
|
215 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
216 )))
|
|
217
|
|
218 (defun xrdb-indent-buffer (arg)
|
|
219 "Re-align the entire buffer.
|
|
220 Alignment calculations are controlled by the variable
|
|
221 `xrdb-subdivide-by', which can take the values `buffer', `paragraph',
|
|
222 `page', or `line', with the following meanings:
|
|
223
|
|
224 buffer - all non-comment lines are aligned with the longest line in
|
|
225 the buffer. Since every line must be scanned, this will
|
|
226 take the longest to perform.
|
|
227
|
|
228 paragraph - alignment of lines spanning paragraphs. A paragraph is
|
|
229 defined as all contiguous lines between blank or comment
|
|
230 lines.
|
|
231
|
|
232 page - alignment of lines spanning pages (i.e. separated by
|
|
233 page-delimiter, usually ^L).
|
|
234
|
|
235 none - alignment of lines based on the previous line.
|
|
236
|
|
237 With optional \\[universal-argument], queries for alignment subdivision."
|
|
238 (interactive "P")
|
|
239 (let ((align-by (if (not arg)
|
|
240 xrdb-subdivide-by
|
|
241 (completing-read
|
|
242 "Align by: "
|
|
243 '(("buffer" . buffer)
|
|
244 ("paragraph" . paragraph)
|
|
245 ("page" . page)
|
|
246 ("line" . line))
|
|
247 nil t (format "%s" xrdb-subdivide-by)))))
|
|
248 (message "Aligning by %s..." align-by)
|
|
249 (save-excursion
|
|
250 (save-restriction
|
|
251 (widen)
|
|
252 (cond
|
|
253 ;; by buffer
|
|
254 ((eq align-by 'buffer)
|
|
255 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column)))
|
|
256 ;; by paragraph
|
|
257 ((eq align-by 'paragraph)
|
|
258 (beginning-of-buffer)
|
|
259 (while (< (point) (point-max))
|
|
260 (narrow-to-region (point) (xrdb-point 'eop))
|
|
261 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
262 (beginning-of-buffer)
|
|
263 (widen)
|
|
264 (forward-paragraph 1)
|
|
265 ))
|
|
266 ;; by page
|
|
267 ((eq align-by 'page)
|
|
268 (beginning-of-buffer)
|
|
269 (while (< (point) (point-max))
|
|
270 (narrow-to-region (point) (xrdb-point 'eopg))
|
|
271 (xrdb-align-to (point-min) (point-max) (xrdb-region-goal-column))
|
|
272 (beginning-of-buffer)
|
|
273 (widen)
|
|
274 (forward-page 1)
|
|
275 ))
|
|
276 ;; by line
|
|
277 (t
|
|
278 (beginning-of-buffer)
|
|
279 (let ((prev-goalcol 0))
|
|
280 (while (< (point) (point-max))
|
|
281 ;; skip comments and blank lines
|
|
282 (if (not (looking-at paragraph-start))
|
|
283 (progn
|
|
284 (xrdb-align-to (xrdb-point 'bol) (xrdb-point 'bonl)
|
|
285 prev-goalcol)
|
|
286 (xrdb-skip-to-separator)
|
|
287 (setq prev-goalcol (- (point) (xrdb-point 'boi)))
|
|
288 ))
|
|
289 (forward-line 1))))
|
|
290 )))
|
|
291 (message "Aligning by %s... done." align-by)
|
|
292 ))
|
|
293
|
|
294
|
|
295 ;; major-mode stuff
|
|
296 (defvar xrdb-mode-abbrev-table nil
|
|
297 "Abbrev table used in `xrdb-mode' buffers.")
|
|
298 (define-abbrev-table 'xrdb-mode-abbrev-table ())
|
|
299
|
|
300
|
|
301 (defvar xrdb-mode-syntax-table nil
|
|
302 "Syntax table used in `xrdb-mode' buffers.")
|
|
303 (if xrdb-mode-syntax-table
|
|
304 ()
|
|
305 (setq xrdb-mode-syntax-table (make-syntax-table))
|
|
306 (modify-syntax-entry ?! "<" xrdb-mode-syntax-table)
|
|
307 (modify-syntax-entry ?\n ">" xrdb-mode-syntax-table))
|
|
308
|
|
309
|
|
310 (defvar xrdb-mode-map ()
|
|
311 "Keymap used in `xrdb-mode' buffers.")
|
|
312 (if xrdb-mode-map
|
|
313 ()
|
|
314 (setq xrdb-mode-map (make-sparse-keymap))
|
|
315 (let ((ekey (char-to-string xrdb-separator-char)))
|
|
316 ;; make the separator key electric
|
|
317 (define-key xrdb-mode-map ekey 'xrdb-electric-separator)
|
|
318 (define-key xrdb-mode-map "\t" 'xrdb-indent-line)
|
|
319 (define-key xrdb-mode-map "\C-c\C-a" 'xrdb-indent-paragraph)
|
|
320 (define-key xrdb-mode-map "\C-c\C-b" 'xrdb-submit-bug-report)
|
|
321 (define-key xrdb-mode-map "\C-c\C-p" 'xrdb-indent-page)
|
|
322 (define-key xrdb-mode-map "\C-c\C-r" 'xrdb-indent-region)
|
|
323 (define-key xrdb-mode-map "\C-c\C-u" 'xrdb-indent-buffer)
|
|
324 (define-key xrdb-mode-map "\C-c>" 'xrdb-align-to)
|
|
325 ))
|
|
326
|
|
327 (defun xrdb-mode ()
|
|
328 "Major mode for editing xrdb config files"
|
|
329 (interactive)
|
|
330 (kill-all-local-variables)
|
|
331 (set-syntax-table xrdb-mode-syntax-table)
|
|
332 (setq major-mode 'xrdb-mode
|
|
333 mode-name "xrdb"
|
|
334 local-abbrev-table xrdb-mode-abbrev-table)
|
|
335 (use-local-map xrdb-mode-map)
|
|
336 ;; local variables
|
|
337 (make-local-variable 'parse-sexp-ignore-comments)
|
|
338 (make-local-variable 'comment-start)
|
|
339 (make-local-variable 'comment-end)
|
|
340 (make-local-variable 'paragraph-start)
|
|
341 (make-local-variable 'paragraph-separate)
|
|
342 (make-local-variable 'paragraph-ignore-fill-prefix)
|
|
343 ;; now set their values
|
|
344 (setq parse-sexp-ignore-comments t
|
|
345 comment-start "! "
|
|
346 comment-end "")
|
|
347 (setq indent-region-function 'xrdb-indent-region
|
|
348 paragraph-ignore-fill-prefix t
|
|
349 paragraph-start (concat "^[ \t]*$\\|^[ \t]*[!]\\|" page-delimiter)
|
|
350 paragraph-separate paragraph-start)
|
|
351 (run-hooks 'xrdb-mode-hook))
|
|
352
|
|
353
|
|
354
|
|
355 ;; faces and font-locking
|
26
|
356 (defvar xrdb-option-name-face 'xrdb-option-name-face
|
|
357 "Face for option name on a line in an X resource db file")
|
|
358 (defvar xrdb-option-value-face 'xrdb-option-value-face
|
|
359 "Face for option value on a line in an X resource db file")
|
|
360
|
24
|
361 (make-face 'xrdb-option-name-face)
|
|
362 (make-face 'xrdb-option-value-face)
|
26
|
363
|
|
364 (defun xrdb-font-lock-mode-hook ()
|
|
365 (or (face-differs-from-default-p 'xrdb-option-name-face)
|
|
366 (copy-face 'font-lock-keyword-face 'xrdb-option-name-face))
|
|
367 (or (face-differs-from-default-p 'xrdb-option-value-face)
|
|
368 (copy-face 'font-lock-string-face 'xrdb-option-value-face))
|
|
369 (remove-hook 'font-lock-mode-hook 'xrdb-font-lock-mode-hook))
|
|
370 (add-hook 'font-lock-mode-hook 'xrdb-font-lock-mode-hook)
|
24
|
371
|
|
372 (defvar xrdb-font-lock-keywords
|
|
373 (list '("^[ \t]*\\([^\n:]*:\\)[ \t]*\\(.*\\)$"
|
|
374 (1 xrdb-option-name-face)
|
|
375 (2 xrdb-option-value-face)))
|
26
|
376 "Additional expressions to highlight in X resource db mode.")
|
|
377 (put 'xrdb-mode 'font-lock-defaults '(xrdb-font-lock-keywords))
|
24
|
378
|
|
379
|
|
380
|
|
381 ;; commands
|
|
382 (defun xrdb-region-goal-column ()
|
|
383 ;; Returns the goal column of the current region. Assumes the
|
|
384 ;; buffer has been narrowed to the region to scan.
|
|
385 (save-excursion
|
|
386 (beginning-of-buffer)
|
|
387 (let ((goalcol -1)
|
|
388 linecol)
|
|
389 (while (< (point) (point-max))
|
|
390 ;; skip any comments
|
|
391 (if (and (not (looking-at xrdb-comment-re))
|
|
392 (xrdb-skip-to-separator)
|
|
393 (< goalcol (setq linecol (current-column)))
|
|
394 )
|
|
395 (setq goalcol linecol))
|
|
396 (forward-line 1))
|
|
397 (if (< goalcol 0)
|
|
398 nil
|
|
399 goalcol))))
|
|
400
|
|
401
|
|
402
|
|
403 ;; submitting bug reports
|
|
404
|
26
|
405 (defconst xrdb-version "1.21"
|
24
|
406 "xrdb-mode version number.")
|
|
407
|
|
408 (defconst xrdb-mode-help-address "tools-help@python.org"
|
|
409 "Address for xrdb-mode bug reports.")
|
|
410
|
|
411 (eval-when-compile
|
|
412 (require 'reporter))
|
|
413
|
|
414 (defun xrdb-submit-bug-report ()
|
|
415 "Submit via mail a bug report on xrdb-mode."
|
|
416 (interactive)
|
|
417 ;; load in reporter
|
|
418 (let ((reporter-prompt-for-summary-p t)
|
|
419 (varlist '(xrdb-subdivide-by
|
|
420 xrdb-mode-hook
|
|
421 )))
|
|
422 (and (if (y-or-n-p "Do you want to submit a report on xrdb-mode? ")
|
|
423 t
|
|
424 (message "")
|
|
425 nil)
|
|
426 (require 'reporter)
|
|
427 (reporter-submit-bug-report
|
|
428 xrdb-mode-help-address "xrdb-mode" varlist nil nil "Dear Barry,")
|
|
429 )))
|
|
430
|
|
431
|
|
432 (provide 'xrdb-mode)
|
|
433 ;; xrdb-mode.el ends here
|