1 ;;; idna.el --- Internationalizing Domain Names in Applications.
3 ;; Copyright (C) 2003 Simon Josefsson
4 ;; Keywords: idna, idn, domain name, internationalization
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 IDNA ToASCII and ToUnicode operations available in
31 ;; (idna-to-ascii "räksmörgås.gnu.org")
32 ;; => "xn--rksmrgs-5wao1o.gnu.org"
34 ;; (idna-to-ascii "www.gnu.org")
37 ;; (idna-to-unicode "xn--rksmrgs-5wao1o.gnu.org")
38 ;; => "räksmörgås.gnu.org"
40 ;; (idna-to-unicode "www.gnu.org")
43 ;; Todo: Support AllowUnassigned and UseSTD3ASCIIRules somehow?
45 ;; This package is useless unless your emacs has at least partial
46 ;; support for the UTF-8 coding system.
48 ;; Report bugs to bug-libidn@gnu.org.
53 "Internationalizing Domain Names in Applications.")
55 (defcustom idna-program
"idn"
56 "Name of the GNU Libidn \"idn\" application."
60 (defcustom idna-environment
'("CHARSET=UTF-8")
61 "List of environment variable definitions prepended to `process-environment'."
62 :type
'(repeat string
)
65 (defcustom idna-to-ascii-parameters
'("--quiet" "--idna-to-ascii")
66 "Parameters passed to `idna-program' to invoke IDNA ToASCII mode."
67 :type
'(repeat string
)
70 (defcustom idna-to-unicode-parameters
'("--quiet" "--idna-to-unicode")
71 "Parameters passed `idna-program' to invoke IDNA ToUnicode mode."
72 :type
'(repeat string
)
75 ;; Internal process handling:
77 (defvar idna-to-ascii-process nil
78 "Internal variable holding process for ToASCII.")
79 (defvar idna-to-ascii-response nil
80 "Internal variable holding response data received from ToASCII process.")
82 (defun idna-to-ascii-response-clear ()
83 (setq idna-to-ascii-response nil
))
85 (defun idna-to-ascii-response ()
86 (while (and (eq (process-status idna-to-ascii-process
) 'run
)
87 (null idna-to-ascii-response
))
88 (accept-process-output idna-to-ascii-process
1))
89 idna-to-ascii-response
)
91 (defun idna-to-ascii-filter (process string
)
92 (setq idna-to-ascii-response
(concat idna-to-ascii-response string
)))
94 (defun idna-to-ascii-process ()
95 (if (and idna-to-ascii-process
96 (eq (process-status idna-to-ascii-process
) 'run
))
98 (if idna-to-ascii-process
100 (kill-process idna-to-ascii-process
)
102 (when (setq idna-to-ascii-process
103 (let ((process-environment (append idna-environment
104 process-environment
)))
105 (apply 'start-process
"idna" nil idna-program
106 idna-to-ascii-parameters
)))
107 (set-process-filter idna-to-ascii-process
'idna-to-ascii-filter
)
108 (set-process-coding-system idna-to-ascii-process
'utf-8
'utf-8
)
109 (process-kill-without-query idna-to-ascii-process
))
110 idna-to-ascii-process
))
112 (defvar idna-to-unicode-process nil
113 "Internal variable holding process for ToASCII.")
114 (defvar idna-to-unicode-response nil
115 "Internal variable holding response data received from ToASCII process.")
117 (defun idna-to-unicode-response-clear ()
118 (setq idna-to-unicode-response nil
))
120 (defun idna-to-unicode-response ()
121 (while (and (eq (process-status idna-to-unicode-process
) 'run
)
122 (null idna-to-unicode-response
))
123 (accept-process-output idna-to-unicode-process
1))
124 idna-to-unicode-response
)
126 (defun idna-to-unicode-filter (process string
)
127 (setq idna-to-unicode-response
(concat idna-to-unicode-response string
)))
129 (defun idna-to-unicode-process ()
130 (if (and idna-to-unicode-process
131 (eq (process-status idna-to-unicode-process
) 'run
))
132 idna-to-unicode-process
133 (if idna-to-unicode-process
135 (kill-process idna-to-unicode-process
)
137 (when (setq idna-to-unicode-process
138 (let ((process-environment (append idna-environment
139 process-environment
)))
140 (apply 'start-process
"idna" nil idna-program
141 idna-to-unicode-parameters
)))
142 (set-process-filter idna-to-unicode-process
'idna-to-unicode-filter
)
143 (set-process-coding-system idna-to-unicode-process
'utf-8
'utf-8
)
144 (process-kill-without-query idna-to-unicode-process
))
145 idna-to-unicode-process
))
149 (defun idna-to-ascii (str)
150 "Returns an ASCII Compatible Encoding (ACE) of STR.
151 It is computed by the IDNA ToASCII operation, after converting the
153 (let ((proc (idna-to-ascii-process))
156 (error "Cannot start idn application (to-ascii)")
157 (idna-to-ascii-response-clear)
158 (process-send-string proc
(concat str
"\n"))
159 (setq string
(idna-to-ascii-response))
160 (if (and string
(string= (substring string
(1- (length string
))) "\n"))
161 (substring string
0 (1- (length string
)))
164 (defun idna-to-unicode (str)
165 "Returns a possibly multibyte string after decoding STR.
166 It is computed by the IDNA ToUnicode operation."
167 (let ((proc (idna-to-unicode-process))
170 (error "Cannot start idn application (to-unicode)")
171 (idna-to-unicode-response-clear)
172 (process-send-string proc
(concat str
"\n"))
173 (setq string
(idna-to-unicode-response))
174 (if (and string
(string= (substring string
(1- (length string
))) "\n"))
175 (substring string
0 (1- (length string
)))
180 ;;; idna.el ends here