File: punycode.el

package info (click to toggle)
libidn 0.6.5-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,520 kB
  • ctags: 1,515
  • sloc: ansic: 31,525; java: 13,494; cs: 12,385; sh: 9,482; xml: 2,283; perl: 1,868; makefile: 316; lisp: 231; php: 214; sed: 16; python: 9
file content (184 lines) | stat: -rw-r--r-- 6,345 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
;;; punycode.el --- An ASCII compatible Unicode encoding format.

;; Copyright (C) 2003, 2005  Simon Josefsson
;; Keywords: punycode, idna, idn, unicode, encoding

;; This file is part of GNU Libidn.

;; GNU Libidn is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Libidn is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Libidn; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; A simple wrapper around the command line "idn" utility in GNU
;; Libidn to make punycode operations available in Emacs.

;; Example:
;;
;; (punycode-encode "r�ksm�rg�s")
;; => "rksmrgs-5wao1o"
;;
;; (punycode-encode "foo")
;; => "foo-"
;;
;; (punycode-decode "rksmrgs-5wao1o")
;; => "r�ksm�rg�s"
;;
;; (punycode-decode "foo-")
;; => "foo"

;; This package is useless unless your emacs has at least partial
;; support for the UTF-8 coding system.

;; Report bugs to bug-libidn@gnu.org.

;;; Code:

(defgroup punycode nil
  "Punycode: An ASCII compatible Unicode encoding format.")

(defcustom punycode-program "idn"
  "Name of the GNU Libidn \"idn\" application."
  :type 'string
  :group 'punycode)

(defcustom punycode-environment '("CHARSET=UTF-8")
  "List of environment variable definitions prepended to `process-environment'."
  :type '(repeat string)
  :group 'punycode)

(defcustom punycode-encode-parameters '("--quiet" "--punycode-encode")
  "Parameters passed to `punycode-program' to invoke punycode encoding mode."
  :type '(repeat string)
  :group 'punycode)

(defcustom punycode-decode-parameters '("--quiet" "--punycode-decode")
  "Parameters passed to `punycode-program' to invoke punycode decoding mode."
  :type '(repeat string)
  :group 'punycode)

;; Internal process handling:

(defvar punycode-encode-process nil
  "Internal variable holding process for punycode encoding.")
(defvar punycode-encode-response nil
  "Internal variable holding response data received from punycode process.")

(defun punycode-encode-response-clear ()
  (setq punycode-encode-response nil))

(defun punycode-encode-response ()
  (while (and (eq (process-status punycode-encode-process) 'run)
	      (null punycode-encode-response))
    (accept-process-output punycode-encode-process 1))
  punycode-encode-response)

(defun punycode-encode-filter (process string)
  (setq punycode-encode-response (concat punycode-encode-response string)))

(defun punycode-encode-process ()
  (if (and punycode-encode-process
	   (eq (process-status punycode-encode-process) 'run))
      punycode-encode-process
    (if punycode-encode-process
	(condition-case ()
	    (kill-process punycode-encode-process)
	  (error)))
    (when (setq punycode-encode-process
		(let ((process-environment (append punycode-environment
						   process-environment)))
		  (apply 'start-process "punycode" nil punycode-program
			 punycode-encode-parameters)))
      (set-process-filter punycode-encode-process 'punycode-encode-filter)
      (set-process-coding-system punycode-encode-process 'utf-8 'utf-8)
      (process-kill-without-query punycode-encode-process))
    punycode-encode-process))

(defvar punycode-decode-process nil
  "Internal variable holding process for punycode encoding.")
(defvar punycode-decode-response nil
  "Internal variable holding response data received from punycode process.")

(defun punycode-decode-response-clear ()
  (setq punycode-decode-response nil))

(defun punycode-decode-response ()
  (while (and (eq (process-status punycode-decode-process) 'run)
	      (null punycode-decode-response))
    (accept-process-output punycode-decode-process 1))
  punycode-decode-response)

(defun punycode-decode-filter (process string)
  (setq punycode-decode-response (concat punycode-decode-response string)))

(defun punycode-decode-process ()
  (if (and punycode-decode-process
	   (eq (process-status punycode-decode-process) 'run))
      punycode-decode-process
    (if punycode-decode-process
	(condition-case ()
	    (kill-process punycode-decode-process)
	  (error)))
    (when (setq punycode-decode-process
		(let ((process-environment (append punycode-environment
						   process-environment)))
		  (apply 'start-process "punycode" nil punycode-program
			 punycode-decode-parameters)))
      (set-process-filter punycode-decode-process 'punycode-decode-filter)
      (set-process-coding-system punycode-decode-process 'utf-8 'utf-8)
      (process-kill-without-query punycode-decode-process))
    punycode-decode-process))

;; Punycode Elisp API:

(defun punycode-encode (str)
  "Returns a Punycode encoding of STR."
  (let ((proc (punycode-encode-process))
	string)
    (if (null proc)
	(error "Cannot start idn application")
      (punycode-encode-response-clear)
      (process-send-string proc (concat str "\n"))
      (setq string (punycode-encode-response))
      (if (and string (string= (substring string (1- (length string))) "\n"))
	  (substring string 0 (1- (length string)))
	string))))

(defun punycode-decode (str)
  "Returns a possibly multibyte string which is the punycode decoding of STR."
  (let ((proc (punycode-decode-process))
	string)
    (if (null proc)
	(error "Cannot start idn application")
      (punycode-decode-response-clear)
      (process-send-string proc (concat str "\n"))
      (setq string (punycode-decode-response))
      (if (and string (string= (substring string (1- (length string))) "\n"))
	  (substring string 0 (1- (length string)))
	string))))

(defun punycode-shutdown ()
  "Kill the punycode related process."
  (interactive)
  (if (and punycode-decode-process
	   (eq (process-status punycode-decode-process) 'run))
      (kill-process punycode-decode-process))
  (if (and punycode-encode-process
	   (eq (process-status punycode-encode-process) 'run))
      (kill-process punycode-encode-process)))

(provide 'punycode)

;;; punycode.el ends here