* liece.el (liece-refresh-buffer-window): Simplified.
[elisp/liece.git] / lisp / liece-q-ccl.el
1 ;;; liece-q-ccl.el --- CTCP binary data quotation in CCL.
2 ;; Copyright (C) 1998-2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1999-01-31
6 ;; Revised: 1999-01-31
7 ;; Keywords: IRC, liece, CTCP
8
9 ;; This file is part of Liece.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program 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
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (require 'broken)
33 (require 'pccl)
34
35 (eval-and-compile
36   (defconst liece-quote-ccl-256-table
37     '(  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
38        16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
39        32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47
40        48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63
41        64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79
42        80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95
43        96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111
44       112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
45       128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
46       144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
47       160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
48       176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
49       192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
50       208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
51       224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
52       240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255)))
53
54 (broken-facility ccl-cascading-write
55   "Emacs CCL write command does not accept more than 2 arguments."
56   (condition-case nil
57       (progn
58         (define-ccl-program cascading-read-test
59           '(1
60             (write r0 r1 r2)))
61         t)
62     (error nil)))
63  
64 (define-ccl-program liece-quote-ccl-decode
65   `(1
66     (loop
67       (read-if
68        (r0 == ?\\)
69        ((read-if
70          (r1 == ?\\)
71          (write r1)
72          (branch
73           r1
74           ,@(mapcar
75              (lambda (r1)
76                (cond
77                 ((= r1 (char-int ?a))
78                  `(write ?\x01))
79                 ((= r1 (char-int ?n))
80                  `(write ?\n))
81                 ((= r1 (char-int ?r))
82                  `(write ?\r))
83                 (t
84                  (if-broken ccl-cascading-write
85                      `((write r0)
86                        (write r1))
87                    `(write r0 r1)))))
88              liece-quote-ccl-256-table))))
89        (write r0))
90       (repeat))))
91
92 (define-ccl-program liece-quote-ccl-encode
93   `(2
94     (loop
95       (read-branch
96        r0
97        ,@(mapcar
98           (lambda (r0)
99             (cond
100              ((= r0 (char-int ?\\))
101               `(write-repeat "\\\\"))
102              ((= r0 (char-int ?\x01))
103               `(write-repeat "\\a"))
104              ((= r0 (char-int ?\n))
105               `(write-repeat "\\n"))
106              ((= r0 (char-int ?\r))
107               `(write-repeat "\\r"))
108              (t
109               `(write-repeat r0))))
110           liece-quote-ccl-256-table)))))
111
112 (make-ccl-coding-system
113  'liece-quote-ccl-cs ?Q "CTCP Quote Decoder/Encoder"
114  'liece-quote-ccl-decode 'liece-quote-ccl-encode)
115
116 (defun liece-quote-ccl-decode-string (string-to-decode)
117   (decode-coding-string string-to-decode 'liece-quote-ccl-cs))
118
119 (defun liece-quote-ccl-encode-string (string-to-encode)
120   (encode-coding-string string-to-encode 'liece-quote-ccl-cs))
121
122 (defun liece-quote-ccl-decode-region (min max)
123   (decode-coding-region min max 'liece-quote-ccl-cs))
124
125 (defun liece-quote-ccl-encode-region (min max)
126   (encode-coding-region min max 'liece-quote-ccl-cs))
127
128 (defalias 'liece-quote-decode-string 'liece-quote-ccl-decode-string)
129 (defalias 'liece-quote-encode-string 'liece-quote-ccl-encode-string)
130
131 (defalias 'liece-quote-decode-region 'liece-quote-ccl-decode-region)
132 (defalias 'liece-quote-encode-region 'liece-quote-ccl-encode-region)
133
134 (provide 'liece-q-ccl)
135
136 ;;; liece-q-ccl.el ends here