*** empty log message ***
[libidn.git] / src / idna.el
blobda162e4c0fa6d4054c92163d8670f5ccbaf51c46
1 ;;; idna.el --- Internationalizing Domain Names in Applications.
3 ;; Copyright (C) 2003, 2005 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)
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., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
23 ;;; Commentary:
25 ;; A simple wrapper around the command line "idn" utility in GNU
26 ;; Libidn to make IDNA ToASCII and ToUnicode operations available in
27 ;; Emacs.
29 ;; Example:
31 ;; (idna-to-ascii "räksmörgås.gnu.org")
32 ;; => "xn--rksmrgs-5wao1o.gnu.org"
34 ;; (idna-to-ascii "www.gnu.org")
35 ;; => "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")
41 ;; => "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.
50 ;;; Code:
52 (defgroup idna nil
53 "Internationalizing Domain Names in Applications.")
55 (defcustom idna-program "idn"
56 "Name of the GNU Libidn \"idn\" application."
57 :type 'string
58 :group 'idna)
60 (defcustom idna-environment '("CHARSET=UTF-8")
61 "List of environment variable definitions prepended to `process-environment'."
62 :type '(repeat string)
63 :group 'idna)
65 (defcustom idna-to-ascii-parameters '("--quiet"
66 "--idna-to-ascii"
67 "--usestd3asciirules")
68 "Parameters passed to `idna-program' to invoke IDNA ToASCII mode."
69 :type '(repeat string)
70 :group 'idna)
72 (defcustom idna-to-unicode-parameters '("--quiet"
73 "--idna-to-unicode"
74 "--usestd3asciirules")
75 "Parameters passed `idna-program' to invoke IDNA ToUnicode mode."
76 :type '(repeat string)
77 :group 'idna)
79 ;; Internal process handling:
81 (defvar idna-to-ascii-process nil
82 "Internal variable holding process for ToASCII.")
83 (defvar idna-to-ascii-response nil
84 "Internal variable holding response data received from ToASCII process.")
86 (defun idna-to-ascii-response-clear ()
87 (setq idna-to-ascii-response nil))
89 (defun idna-to-ascii-response ()
90 (while (and (eq (process-status idna-to-ascii-process) 'run)
91 (null idna-to-ascii-response))
92 (accept-process-output idna-to-ascii-process 1))
93 idna-to-ascii-response)
95 (defun idna-to-ascii-filter (process string)
96 (setq idna-to-ascii-response (concat idna-to-ascii-response string)))
98 (defun idna-to-ascii-process ()
99 (if (and idna-to-ascii-process
100 (eq (process-status idna-to-ascii-process) 'run))
101 idna-to-ascii-process
102 (if idna-to-ascii-process
103 (condition-case ()
104 (kill-process idna-to-ascii-process)
105 (error)))
106 (when (setq idna-to-ascii-process
107 (let ((process-environment (append idna-environment
108 process-environment)))
109 (apply 'start-process "idna" nil idna-program
110 idna-to-ascii-parameters)))
111 (set-process-filter idna-to-ascii-process 'idna-to-ascii-filter)
112 (set-process-coding-system idna-to-ascii-process 'utf-8 'utf-8)
113 (process-kill-without-query idna-to-ascii-process))
114 idna-to-ascii-process))
116 (defvar idna-to-unicode-process nil
117 "Internal variable holding process for ToASCII.")
118 (defvar idna-to-unicode-response nil
119 "Internal variable holding response data received from ToASCII process.")
121 (defun idna-to-unicode-response-clear ()
122 (setq idna-to-unicode-response nil))
124 (defun idna-to-unicode-response ()
125 (while (and (eq (process-status idna-to-unicode-process) 'run)
126 (null idna-to-unicode-response))
127 (accept-process-output idna-to-unicode-process 1))
128 idna-to-unicode-response)
130 (defun idna-to-unicode-filter (process string)
131 (setq idna-to-unicode-response (concat idna-to-unicode-response string)))
133 (defun idna-to-unicode-process ()
134 (if (and idna-to-unicode-process
135 (eq (process-status idna-to-unicode-process) 'run))
136 idna-to-unicode-process
137 (if idna-to-unicode-process
138 (condition-case ()
139 (kill-process idna-to-unicode-process)
140 (error)))
141 (when (setq idna-to-unicode-process
142 (let ((process-environment (append idna-environment
143 process-environment)))
144 (apply 'start-process "idna" nil idna-program
145 idna-to-unicode-parameters)))
146 (set-process-filter idna-to-unicode-process 'idna-to-unicode-filter)
147 (set-process-coding-system idna-to-unicode-process 'utf-8 'utf-8)
148 (process-kill-without-query idna-to-unicode-process))
149 idna-to-unicode-process))
151 ;; IDNA Elisp API:
153 (defun idna-to-ascii (str)
154 "Returns an ASCII Compatible Encoding (ACE) of STR.
155 It is computed by the IDNA ToASCII operation, after converting the
156 input to UTF-8."
157 (let ((proc (idna-to-ascii-process))
158 string)
159 (if (null proc)
160 (error "Cannot start idn application (to-ascii)")
161 (idna-to-ascii-response-clear)
162 (process-send-string proc (concat str "\n"))
163 (setq string (idna-to-ascii-response))
164 (if (and string (string= (substring string (1- (length string))) "\n"))
165 (substring string 0 (1- (length string)))
166 string))))
168 (defun idna-to-unicode (str)
169 "Returns a possibly multibyte string after decoding STR.
170 It is computed by the IDNA ToUnicode operation."
171 (let ((proc (idna-to-unicode-process))
172 string)
173 (if (null proc)
174 (error "Cannot start idn application (to-unicode)")
175 (idna-to-unicode-response-clear)
176 (process-send-string proc (concat str "\n"))
177 (setq string (idna-to-unicode-response))
178 (if (and string (string= (substring string (1- (length string))) "\n"))
179 (substring string 0 (1- (length string)))
180 string))))
182 (defun idna-shutdown ()
183 "Kill the IDNA related processes."
184 (interactive)
185 (if (and idna-to-ascii-process
186 (eq (process-status idna-to-ascii-process) 'run))
187 (kill-process idna-to-ascii-process))
188 (if (and idna-to-unicode-process
189 (eq (process-status idna-to-unicode-process) 'run))
190 (kill-process idna-to-unicode-process)))
192 (provide 'idna)
194 ;;; idna.el ends here