Add.
[libidn.git] / idna.el
blob9b0cbdb38f594ddcac01b4df940b0df3a5f26b71
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)
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 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 "env CHARSET=UTF-8 idn"
56 "Name of the GNU Libidn \"idn\" application."
57 :type 'string
58 :group 'idna)
60 (defcustom idna-to-ascii-parameters "--quiet --idna-to-ascii"
61 "Parameters passed to `idna-program' to invoke IDNA ToASCII mode."
62 :type 'string
63 :group 'idna)
65 (defcustom idna-to-unicode-parameters "--quiet --idna-to-unicode"
66 "Parameters passed `idna-program' to invoke IDNA ToUnicode mode."
67 :type 'string
68 :group 'idna)
70 ;; Internal process handling:
72 (defvar idna-to-ascii-process nil
73 "Internal variable holding process for ToASCII.")
74 (defvar idna-to-ascii-response nil
75 "Internal variable holding response data received from ToASCII process.")
77 (defun idna-to-ascii-response-clear ()
78 (setq idna-to-ascii-response nil))
80 (defun idna-to-ascii-response ()
81 (while (and (eq (process-status idna-to-ascii-process) 'run)
82 (null idna-to-ascii-response))
83 (accept-process-output idna-to-ascii-process 1))
84 idna-to-ascii-response)
86 (defun idna-to-ascii-filter (process string)
87 (setq idna-to-ascii-response (concat idna-to-ascii-response string)))
89 (defun idna-to-ascii-process ()
90 (if (and idna-to-ascii-process
91 (eq (process-status idna-to-ascii-process) 'run))
92 idna-to-ascii-process
93 (if idna-to-ascii-process
94 (condition-case ()
95 (kill-process idna-to-ascii-process)
96 (error)))
97 (when (setq idna-to-ascii-process
98 (start-process "idna" nil
99 shell-file-name shell-command-switch
100 (concat idna-program " "
101 idna-to-ascii-parameters)))
102 (set-process-filter idna-to-ascii-process 'idna-to-ascii-filter)
103 (set-process-coding-system idna-to-ascii-process 'utf-8 'utf-8)
104 (process-kill-without-query idna-to-ascii-process))
105 idna-to-ascii-process))
107 (defvar idna-to-unicode-process nil
108 "Internal variable holding process for ToASCII.")
109 (defvar idna-to-unicode-response nil
110 "Internal variable holding response data received from ToASCII process.")
112 (defun idna-to-unicode-response-clear ()
113 (setq idna-to-unicode-response nil))
115 (defun idna-to-unicode-response ()
116 (while (and (eq (process-status idna-to-unicode-process) 'run)
117 (null idna-to-unicode-response))
118 (accept-process-output idna-to-unicode-process 1))
119 idna-to-unicode-response)
121 (defun idna-to-unicode-filter (process string)
122 (setq idna-to-unicode-response (concat idna-to-unicode-response string)))
124 (defun idna-to-unicode-process ()
125 (if (and idna-to-unicode-process
126 (eq (process-status idna-to-unicode-process) 'run))
127 idna-to-unicode-process
128 (if idna-to-unicode-process
129 (condition-case ()
130 (kill-process idna-to-unicode-process)
131 (error)))
132 (when (setq idna-to-unicode-process
133 (start-process "idna" nil
134 shell-file-name shell-command-switch
135 (concat idna-program " "
136 idna-to-unicode-parameters)))
137 (set-process-filter idna-to-unicode-process 'idna-to-unicode-filter)
138 (set-process-coding-system idna-to-unicode-process 'utf-8 'utf-8)
139 (process-kill-without-query idna-to-unicode-process))
140 idna-to-unicode-process))
142 ;; IDNA Elisp API:
144 (defun idna-to-ascii (str)
145 "Returns an ASCII Compatible Encoding (ACE) of STR.
146 It is computed by the IDNA ToASCII operation, after converting the
147 input to UTF-8."
148 (let ((proc (idna-to-ascii-process))
149 string)
150 (if (null proc)
151 (error "Cannot start idn application")
152 (idna-to-ascii-response-clear)
153 (process-send-string proc (concat str "\n"))
154 (setq string (idna-to-ascii-response))
155 (if (string= (substring string (1- (length string))) "\n")
156 (substring string 0 (1- (length string)))
157 string))))
159 (defun idna-to-unicode (str)
160 "Returns a possibly multibyte string after decoding STR.
161 It is computed by the IDNA ToUnicode operation."
162 (let ((proc (idna-to-unicode-process))
163 string)
164 (if (null proc)
165 (error "Cannot start idn application")
166 (idna-to-unicode-response-clear)
167 (process-send-string proc (concat str "\n"))
168 (setq string (idna-to-unicode-response))
169 (if (string= (substring string (1- (length string))) "\n")
170 (substring string 0 (1- (length string)))
171 string))))
173 (provide 'idna)
175 ;;; idna.el ends here