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)
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.
25 ;; A simple wrapper around the command line "idn" utility in GNU
26 ;; Libidn to make punycode operations available in Emacs.
30 ;; (punycode-encode "räksmörgås")
31 ;; => "rksmrgs-5wao1o"
33 ;; (punycode-encode "foo")
36 ;; (punycode-decode "rksmrgs-5wao1o")
39 ;; (punycode-decode "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.
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."
57 (defcustom punycode-environment
'("CHARSET=UTF-8")
58 "List of environment variable definitions prepended to `process-environment'."
59 :type
'(repeat string
)
62 (defcustom punycode-encode-parameters
'("--quiet" "--punycode-encode")
63 "Parameters passed to `punycode-program' to invoke punycode encoding mode."
64 :type
'(repeat string
)
67 (defcustom punycode-decode-parameters
'("--quiet" "--punycode-decode")
68 "Parameters passed to `punycode-program' to invoke punycode decoding mode."
69 :type
'(repeat string
)
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
97 (kill-process punycode-encode-process
)
99 (when (setq punycode-encode-process
100 (let ((process-environment (append process-environment
101 punycode-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
132 (kill-process punycode-decode-process
)
134 (when (setq punycode-decode-process
135 (let ((process-environment (append process-environment
136 punycode-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))
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
)))
159 (defun punycode-decode (str)
160 "Returns a possibly multibyte string which is the punycode decoding of STR."
161 (let ((proc (punycode-decode-process))
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
)))
174 ;;; punycode.el ends here