.
[libidn.git] / src / punycode.el
bloba77ade51496bd2d12be4354618fe403cce8a2ec8
1 ;;; punycode.el --- An ASCII compatible Unicode encoding format.
3 ;; Copyright (C) 2003 Simon Josefsson
4 ;; Keywords: punycode, idna, idn, unicode, encoding
6 ;; This file is part of GNU Libidn.
8 ;; GNU Libidn is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
13 ;; GNU Libidn is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Libidn; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
23 ;;; Commentary:
25 ;; A simple wrapper around the command line "idn" utility in GNU
26 ;; Libidn to make punycode operations available in Emacs.
28 ;; Example:
30 ;; (punycode-encode "räksmörgås")
31 ;; => "rksmrgs-5wao1o"
33 ;; (punycode-encode "foo")
34 ;; => "foo-"
36 ;; (punycode-decode "rksmrgs-5wao1o")
37 ;; => "räksmörgås"
39 ;; (punycode-decode "foo-")
40 ;; => "foo"
42 ;; This package is useless unless your emacs has at least partial
43 ;; support for the UTF-8 coding system.
45 ;; Report bugs to bug-libidn@gnu.org.
47 ;;; Code:
49 (defgroup punycode nil
50 "Punycode: An ASCII compatible Unicode encoding format.")
52 (defcustom punycode-program "idn"
53 "Name of the GNU Libidn \"idn\" application."
54 :type 'string
55 :group 'punycode)
57 (defcustom punycode-environment '("CHARSET=UTF-8")
58 "List of environment variable definitions prepended to `process-environment'."
59 :type '(repeat string)
60 :group 'punycode)
62 (defcustom punycode-encode-parameters '("--quiet" "--punycode-encode")
63 "Parameters passed to `punycode-program' to invoke punycode encoding mode."
64 :type '(repeat string)
65 :group 'punycode)
67 (defcustom punycode-decode-parameters '("--quiet" "--punycode-decode")
68 "Parameters passed to `punycode-program' to invoke punycode decoding mode."
69 :type '(repeat string)
70 :group 'punycode)
72 ;; Internal process handling:
74 (defvar punycode-encode-process nil
75 "Internal variable holding process for punycode encoding.")
76 (defvar punycode-encode-response nil
77 "Internal variable holding response data received from punycode process.")
79 (defun punycode-encode-response-clear ()
80 (setq punycode-encode-response nil))
82 (defun punycode-encode-response ()
83 (while (and (eq (process-status punycode-encode-process) 'run)
84 (null punycode-encode-response))
85 (accept-process-output punycode-encode-process 1))
86 punycode-encode-response)
88 (defun punycode-encode-filter (process string)
89 (setq punycode-encode-response (concat punycode-encode-response string)))
91 (defun punycode-encode-process ()
92 (if (and punycode-encode-process
93 (eq (process-status punycode-encode-process) 'run))
94 punycode-encode-process
95 (if punycode-encode-process
96 (condition-case ()
97 (kill-process punycode-encode-process)
98 (error)))
99 (when (setq punycode-encode-process
100 (let ((process-environment (append punycode-environment
101 process-environment)))
102 (apply 'start-process "punycode" nil punycode-program
103 punycode-encode-parameters)))
104 (set-process-filter punycode-encode-process 'punycode-encode-filter)
105 (set-process-coding-system punycode-encode-process 'utf-8 'utf-8)
106 (process-kill-without-query punycode-encode-process))
107 punycode-encode-process))
109 (defvar punycode-decode-process nil
110 "Internal variable holding process for punycode encoding.")
111 (defvar punycode-decode-response nil
112 "Internal variable holding response data received from punycode process.")
114 (defun punycode-decode-response-clear ()
115 (setq punycode-decode-response nil))
117 (defun punycode-decode-response ()
118 (while (and (eq (process-status punycode-decode-process) 'run)
119 (null punycode-decode-response))
120 (accept-process-output punycode-decode-process 1))
121 punycode-decode-response)
123 (defun punycode-decode-filter (process string)
124 (setq punycode-decode-response (concat punycode-decode-response string)))
126 (defun punycode-decode-process ()
127 (if (and punycode-decode-process
128 (eq (process-status punycode-decode-process) 'run))
129 punycode-decode-process
130 (if punycode-decode-process
131 (condition-case ()
132 (kill-process punycode-decode-process)
133 (error)))
134 (when (setq punycode-decode-process
135 (let ((process-environment (append punycode-environment
136 process-environment)))
137 (apply 'start-process "punycode" nil punycode-program
138 punycode-decode-parameters)))
139 (set-process-filter punycode-decode-process 'punycode-decode-filter)
140 (set-process-coding-system punycode-decode-process 'utf-8 'utf-8)
141 (process-kill-without-query punycode-decode-process))
142 punycode-decode-process))
144 ;; Punycode Elisp API:
146 (defun punycode-encode (str)
147 "Returns a Punycode encoding of STR."
148 (let ((proc (punycode-encode-process))
149 string)
150 (if (null proc)
151 (error "Cannot start idn application")
152 (punycode-encode-response-clear)
153 (process-send-string proc (concat str "\n"))
154 (setq string (punycode-encode-response))
155 (if (and string (string= (substring string (1- (length string))) "\n"))
156 (substring string 0 (1- (length string)))
157 string))))
159 (defun punycode-decode (str)
160 "Returns a possibly multibyte string which is the punycode decoding of STR."
161 (let ((proc (punycode-decode-process))
162 string)
163 (if (null proc)
164 (error "Cannot start idn application")
165 (punycode-decode-response-clear)
166 (process-send-string proc (concat str "\n"))
167 (setq string (punycode-decode-response))
168 (if (and string (string= (substring string (1- (length string))) "\n"))
169 (substring string 0 (1- (length string)))
170 string))))
172 (defun punycode-shutdown ()
173 "Kill the punycode related process."
174 (interactive)
175 (if (and punycode-decode-process
176 (eq (process-status punycode-decode-process) 'run))
177 (kill-process punycode-decode-process))
178 (if (and punycode-encode-process
179 (eq (process-status punycode-encode-process) 'run))
180 (kill-process punycode-encode-process)))
182 (provide 'punycode)
184 ;;; punycode.el ends here