Mercurial > emacs
annotate lisp/mail/binhex.el @ 108632:d38b0dd2bdbe
Provide a simple generic indentation engine and use it for Prolog.
* emacs-lisp/smie.el: New file.
* progmodes/prolog.el (prolog-smie-op-levels)
(prolog-smie-indent-rules): New var.
(prolog-mode-variables): Use them to configure SMIE.
(prolog-indent-line, prolog-indent-level): Remove.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Mon, 17 May 2010 15:27:26 -0400 |
parents | c0d13767677a |
children | 8d09094063d0 |
rev | line source |
---|---|
86959 | 1 ;;; binhex.el --- elisp native binhex decode |
2 | |
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, | |
106815 | 4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
86959 | 5 |
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
7 ;; Keywords: binhex news | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
94674
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
11 ;; GNU Emacs is free software: you can redistribute it and/or modify |
86959 | 12 ;; it under the terms of the GNU General Public License as published by |
94674
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; the Free Software Foundation, either version 3 of the License, or |
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; (at your option) any later version. |
86959 | 15 |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
94674
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
86959 | 23 |
24 ;;; Commentary: | |
25 | |
26 ;;; Code: | |
27 | |
28 (eval-when-compile (require 'cl)) | |
29 | |
30 (eval-and-compile | |
31 (defalias 'binhex-char-int | |
32 (if (fboundp 'char-int) | |
33 'char-int | |
34 'identity))) | |
35 | |
86960 | 36 (defgroup binhex nil |
37 "Decoding of BinHex (binary-to-hexadecimal) data." | |
38 :group 'mail | |
39 :group 'news) | |
40 | |
86959 | 41 (defcustom binhex-decoder-program "hexbin" |
42 "*Non-nil value should be a string that names a binhex decoder. | |
43 The program should expect to read binhex data on its standard | |
44 input and write the converted data to its standard output." | |
45 :type 'string | |
86960 | 46 :group 'binhex) |
86959 | 47 |
48 (defcustom binhex-decoder-switches '("-d") | |
49 "*List of command line flags passed to the command `binhex-decoder-program'." | |
86960 | 50 :group 'binhex |
86959 | 51 :type '(repeat string)) |
52 | |
53 (defcustom binhex-use-external | |
54 (executable-find binhex-decoder-program) | |
55 "*Use external binhex program." | |
56 :version "22.1" | |
86960 | 57 :group 'binhex |
86959 | 58 :type 'boolean) |
59 | |
60 (defconst binhex-alphabet-decoding-alist | |
61 '(( ?\! . 0) ( ?\" . 1) ( ?\# . 2) ( ?\$ . 3) ( ?\% . 4) ( ?\& . 5) | |
62 ( ?\' . 6) ( ?\( . 7) ( ?\) . 8) ( ?\* . 9) ( ?\+ . 10) ( ?\, . 11) | |
63 ( ?\- . 12) ( ?0 . 13) ( ?1 . 14) ( ?2 . 15) ( ?3 . 16) ( ?4 . 17) | |
64 ( ?5 . 18) ( ?6 . 19) ( ?8 . 20) ( ?9 . 21) ( ?@ . 22) ( ?A . 23) | |
65 ( ?B . 24) ( ?C . 25) ( ?D . 26) ( ?E . 27) ( ?F . 28) ( ?G . 29) | |
66 ( ?H . 30) ( ?I . 31) ( ?J . 32) ( ?K . 33) ( ?L . 34) ( ?M . 35) | |
67 ( ?N . 36) ( ?P . 37) ( ?Q . 38) ( ?R . 39) ( ?S . 40) ( ?T . 41) | |
68 ( ?U . 42) ( ?V . 43) ( ?X . 44) ( ?Y . 45) ( ?Z . 46) ( ?\[ . 47) | |
69 ( ?\` . 48) ( ?a . 49) ( ?b . 50) ( ?c . 51) ( ?d . 52) ( ?e . 53) | |
70 ( ?f . 54) ( ?h . 55) ( ?i . 56) ( ?j . 57) ( ?k . 58) ( ?l . 59) | |
71 ( ?m . 60) ( ?p . 61) ( ?q . 62) ( ?r . 63))) | |
72 | |
73 (defun binhex-char-map (char) | |
74 (cdr (assq char binhex-alphabet-decoding-alist))) | |
75 | |
76 ;;;###autoload | |
77 (defconst binhex-begin-line | |
78 "^:...............................................................$") | |
79 (defconst binhex-body-line | |
80 "^[^:]...............................................................$") | |
81 (defconst binhex-end-line ":$") | |
82 | |
83 (defvar binhex-temporary-file-directory | |
84 (cond ((fboundp 'temp-directory) (temp-directory)) | |
85 ((boundp 'temporary-file-directory) temporary-file-directory) | |
86 ("/tmp/"))) | |
87 | |
88 (eval-and-compile | |
89 (defalias 'binhex-insert-char | |
90 (if (featurep 'xemacs) | |
91 'insert-char | |
92 (lambda (char &optional count ignored buffer) | |
93 "Insert COUNT copies of CHARACTER into BUFFER." | |
94 (if (or (null buffer) (eq buffer (current-buffer))) | |
95 (insert-char char count) | |
96 (with-current-buffer buffer | |
97 (insert-char char count))))))) | |
98 | |
99 (defvar binhex-crc-table | |
100 [0 4129 8258 12387 16516 20645 24774 28903 | |
101 33032 37161 41290 45419 49548 53677 57806 61935 | |
102 4657 528 12915 8786 21173 17044 29431 25302 | |
103 37689 33560 45947 41818 54205 50076 62463 58334 | |
104 9314 13379 1056 5121 25830 29895 17572 21637 | |
105 42346 46411 34088 38153 58862 62927 50604 54669 | |
106 13907 9842 5649 1584 30423 26358 22165 18100 | |
107 46939 42874 38681 34616 63455 59390 55197 51132 | |
108 18628 22757 26758 30887 2112 6241 10242 14371 | |
109 51660 55789 59790 63919 35144 39273 43274 47403 | |
110 23285 19156 31415 27286 6769 2640 14899 10770 | |
111 56317 52188 64447 60318 39801 35672 47931 43802 | |
112 27814 31879 19684 23749 11298 15363 3168 7233 | |
113 60846 64911 52716 56781 44330 48395 36200 40265 | |
114 32407 28342 24277 20212 15891 11826 7761 3696 | |
115 65439 61374 57309 53244 48923 44858 40793 36728 | |
116 37256 33193 45514 41451 53516 49453 61774 57711 | |
117 4224 161 12482 8419 20484 16421 28742 24679 | |
118 33721 37784 41979 46042 49981 54044 58239 62302 | |
119 689 4752 8947 13010 16949 21012 25207 29270 | |
120 46570 42443 38312 34185 62830 58703 54572 50445 | |
121 13538 9411 5280 1153 29798 25671 21540 17413 | |
122 42971 47098 34713 38840 59231 63358 50973 55100 | |
123 9939 14066 1681 5808 26199 30326 17941 22068 | |
124 55628 51565 63758 59695 39368 35305 47498 43435 | |
125 22596 18533 30726 26663 6336 2273 14466 10403 | |
126 52093 56156 60223 64286 35833 39896 43963 48026 | |
127 19061 23124 27191 31254 2801 6864 10931 14994 | |
128 64814 60687 56684 52557 48554 44427 40424 36297 | |
129 31782 27655 23652 19525 15522 11395 7392 3265 | |
130 61215 65342 53085 57212 44955 49082 36825 40952 | |
131 28183 32310 20053 24180 11923 16050 3793 7920]) | |
132 | |
133 (defun binhex-update-crc (crc char &optional count) | |
134 (if (null count) (setq count 1)) | |
135 (while (> count 0) | |
136 (setq crc (logxor (logand (lsh crc 8) 65280) | |
137 (aref binhex-crc-table | |
138 (logxor (logand (lsh crc -8) 255) | |
139 char))) | |
140 count (1- count))) | |
141 crc) | |
142 | |
143 (defun binhex-verify-crc (buffer start end) | |
144 (with-current-buffer buffer | |
145 (let ((pos start) (crc 0) (last (- end 2))) | |
146 (while (< pos last) | |
147 (setq crc (binhex-update-crc crc (char-after pos)) | |
148 pos (1+ pos))) | |
149 (if (= crc (binhex-string-big-endian (buffer-substring last end))) | |
150 nil | |
151 (error "CRC error"))))) | |
152 | |
153 (defun binhex-string-big-endian (string) | |
154 (let ((ret 0) (i 0) (len (length string))) | |
155 (while (< i len) | |
156 (setq ret (+ (lsh ret 8) (binhex-char-int (aref string i))) | |
157 i (1+ i))) | |
158 ret)) | |
159 | |
160 (defun binhex-string-little-endian (string) | |
161 (let ((ret 0) (i 0) (shift 0) (len (length string))) | |
162 (while (< i len) | |
163 (setq ret (+ ret (lsh (binhex-char-int (aref string i)) shift)) | |
164 i (1+ i) | |
165 shift (+ shift 8))) | |
166 ret)) | |
167 | |
168 (defun binhex-header (buffer) | |
169 (with-current-buffer buffer | |
170 (let ((pos (point-min)) len) | |
171 (vector | |
172 (prog1 | |
173 (setq len (binhex-char-int (char-after pos))) | |
174 (setq pos (1+ pos))) | |
175 (buffer-substring pos (setq pos (+ pos len))) | |
176 (prog1 | |
177 (setq len (binhex-char-int (char-after pos))) | |
178 (setq pos (1+ pos))) | |
179 (buffer-substring pos (setq pos (+ pos 4))) | |
180 (buffer-substring pos (setq pos (+ pos 4))) | |
181 (binhex-string-big-endian | |
182 (buffer-substring pos (setq pos (+ pos 2)))) | |
183 (binhex-string-big-endian | |
184 (buffer-substring pos (setq pos (+ pos 4)))) | |
185 (binhex-string-big-endian | |
186 (buffer-substring pos (setq pos (+ pos 4)))))))) | |
187 | |
188 (defvar binhex-last-char) | |
189 (defvar binhex-repeat) | |
190 | |
191 (defun binhex-push-char (char &optional count ignored buffer) | |
192 (cond | |
193 (binhex-repeat | |
194 (if (eq char 0) | |
195 (binhex-insert-char (setq binhex-last-char 144) 1 | |
196 ignored buffer) | |
197 (binhex-insert-char binhex-last-char (- char 1) | |
198 ignored buffer) | |
199 (setq binhex-last-char nil)) | |
200 (setq binhex-repeat nil)) | |
201 ((= char 144) | |
202 (setq binhex-repeat t)) | |
203 (t | |
204 (binhex-insert-char (setq binhex-last-char char) 1 ignored buffer)))) | |
205 | |
206 ;;;###autoload | |
207 (defun binhex-decode-region-internal (start end &optional header-only) | |
208 "Binhex decode region between START and END without using an external program. | |
209 If HEADER-ONLY is non-nil only decode header and return filename." | |
210 (interactive "r") | |
211 (let ((work-buffer nil) | |
212 (counter 0) | |
213 (bits 0) (tmp t) | |
214 (lim 0) inputpos | |
215 (non-data-chars " \t\n\r:") | |
216 file-name-length data-fork-start | |
217 header | |
218 binhex-last-char binhex-repeat) | |
219 (unwind-protect | |
220 (save-excursion | |
221 (goto-char start) | |
222 (when (re-search-forward binhex-begin-line end t) | |
92783
b8de2f698e11
(binhex-decode-region-internal): Use set-buffer-multibyte rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
223 (setq work-buffer (generate-new-buffer " *binhex-work*")) |
108287
c0d13767677a
Synch with Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
106815
diff
changeset
|
224 (unless (featurep 'xemacs) |
c0d13767677a
Synch with Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
106815
diff
changeset
|
225 (with-current-buffer work-buffer (set-buffer-multibyte nil))) |
86959 | 226 (beginning-of-line) |
227 (setq bits 0 counter 0) | |
228 (while tmp | |
229 (skip-chars-forward non-data-chars end) | |
230 (setq inputpos (point)) | |
231 (end-of-line) | |
232 (setq lim (point)) | |
233 (while (and (< inputpos lim) | |
234 (setq tmp (binhex-char-map (char-after inputpos)))) | |
235 (setq bits (+ bits tmp) | |
236 counter (1+ counter) | |
237 inputpos (1+ inputpos)) | |
238 (cond ((= counter 4) | |
239 (binhex-push-char (lsh bits -16) 1 nil work-buffer) | |
240 (binhex-push-char (logand (lsh bits -8) 255) 1 nil | |
241 work-buffer) | |
242 (binhex-push-char (logand bits 255) 1 nil | |
243 work-buffer) | |
244 (setq bits 0 counter 0)) | |
245 (t (setq bits (lsh bits 6))))) | |
246 (if (null file-name-length) | |
247 (with-current-buffer work-buffer | |
248 (setq file-name-length (char-after (point-min)) | |
249 data-fork-start (+ (point-min) | |
250 file-name-length 22)))) | |
251 (when (and (null header) | |
252 (with-current-buffer work-buffer | |
253 (>= (buffer-size) data-fork-start))) | |
254 (binhex-verify-crc work-buffer | |
255 (point-min) data-fork-start) | |
256 (setq header (binhex-header work-buffer)) | |
257 (when header-only (setq tmp nil counter 0))) | |
258 (setq tmp (and tmp (not (eq inputpos end))))) | |
259 (cond | |
260 ((= counter 3) | |
261 (binhex-push-char (logand (lsh bits -16) 255) 1 nil | |
262 work-buffer) | |
263 (binhex-push-char (logand (lsh bits -8) 255) 1 nil | |
264 work-buffer)) | |
265 ((= counter 2) | |
266 (binhex-push-char (logand (lsh bits -10) 255) 1 nil | |
267 work-buffer)))) | |
268 (if header-only nil | |
269 (binhex-verify-crc work-buffer | |
270 data-fork-start | |
271 (+ data-fork-start (aref header 6) 2)) | |
272 (or (markerp end) (setq end (set-marker (make-marker) end))) | |
273 (goto-char start) | |
274 (insert-buffer-substring work-buffer | |
275 data-fork-start (+ data-fork-start | |
276 (aref header 6))) | |
277 (delete-region (point) end))) | |
278 (and work-buffer (kill-buffer work-buffer))) | |
279 (if header (aref header 1)))) | |
280 | |
281 ;;;###autoload | |
282 (defun binhex-decode-region-external (start end) | |
283 "Binhex decode region between START and END using external decoder." | |
284 (interactive "r") | |
285 (let ((cbuf (current-buffer)) firstline work-buffer status | |
286 (file-name (expand-file-name | |
287 (concat (binhex-decode-region-internal start end t) | |
288 ".data") | |
289 binhex-temporary-file-directory))) | |
290 (save-excursion | |
291 (goto-char start) | |
292 (when (re-search-forward binhex-begin-line nil t) | |
293 (let ((cdir default-directory) default-process-coding-system) | |
294 (unwind-protect | |
295 (progn | |
296 (set-buffer (setq work-buffer | |
297 (generate-new-buffer " *binhex-work*"))) | |
298 (buffer-disable-undo work-buffer) | |
299 (insert-buffer-substring cbuf firstline end) | |
300 (cd binhex-temporary-file-directory) | |
301 (apply 'call-process-region | |
302 (point-min) | |
303 (point-max) | |
304 binhex-decoder-program | |
305 nil | |
306 nil | |
307 nil | |
308 binhex-decoder-switches)) | |
309 (cd cdir) (set-buffer cbuf))) | |
310 (if (and file-name (file-exists-p file-name)) | |
311 (progn | |
312 (goto-char start) | |
313 (delete-region start end) | |
314 (let (format-alist) | |
315 (insert-file-contents-literally file-name))) | |
316 (error "Can not binhex"))) | |
317 (and work-buffer (kill-buffer work-buffer)) | |
318 (ignore-errors | |
319 (if file-name (delete-file file-name)))))) | |
320 | |
321 ;;;###autoload | |
322 (defun binhex-decode-region (start end) | |
323 "Binhex decode region between START and END." | |
324 (interactive "r") | |
325 (if binhex-use-external | |
326 (binhex-decode-region-external start end) | |
327 (binhex-decode-region-internal start end))) | |
328 | |
329 (provide 'binhex) | |
330 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
92783
diff
changeset
|
331 ;; arch-tag: 8476badd-1e76-4f1d-a640-f9a38c72eed8 |
86959 | 332 ;;; binhex.el ends here |