home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 March / PCWELT_3_2006.ISO / base / 04_xap_libs.mo / usr / share / emacs / site-lisp / punycode.el < prev   
Encoding:
Text File  |  2005-06-09  |  6.2 KB  |  185 lines

  1. ;;; punycode.el --- An ASCII compatible Unicode encoding format.
  2.  
  3. ;; Copyright (C) 2003, 2005  Simon Josefsson
  4. ;; Keywords: punycode, idna, idn, unicode, encoding
  5.  
  6. ;; This file is part of GNU Libidn.
  7.  
  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.
  12.  
  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.
  17.  
  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.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; A simple wrapper around the command line "idn" utility in GNU
  26. ;; Libidn to make punycode operations available in Emacs.
  27.  
  28. ;; Example:
  29. ;;
  30. ;; (punycode-encode "rΣksm÷rgσs")
  31. ;; => "rksmrgs-5wao1o"
  32. ;;
  33. ;; (punycode-encode "foo")
  34. ;; => "foo-"
  35. ;;
  36. ;; (punycode-decode "rksmrgs-5wao1o")
  37. ;; => "rΣksm÷rgσs"
  38. ;;
  39. ;; (punycode-decode "foo-")
  40. ;; => "foo"
  41.  
  42. ;; This package is useless unless your emacs has at least partial
  43. ;; support for the UTF-8 coding system.
  44.  
  45. ;; Report bugs to bug-libidn@gnu.org.
  46.  
  47. ;;; Code:
  48.  
  49. (defgroup punycode nil
  50.   "Punycode: An ASCII compatible Unicode encoding format.")
  51.  
  52. (defcustom punycode-program "idn"
  53.   "Name of the GNU Libidn \"idn\" application."
  54.   :type 'string
  55.   :group 'punycode)
  56.  
  57. (defcustom punycode-environment '("CHARSET=UTF-8")
  58.   "List of environment variable definitions prepended to `process-environment'."
  59.   :type '(repeat string)
  60.   :group 'punycode)
  61.  
  62. (defcustom punycode-encode-parameters '("--quiet" "--punycode-encode")
  63.   "Parameters passed to `punycode-program' to invoke punycode encoding mode."
  64.   :type '(repeat string)
  65.   :group 'punycode)
  66.  
  67. (defcustom punycode-decode-parameters '("--quiet" "--punycode-decode")
  68.   "Parameters passed to `punycode-program' to invoke punycode decoding mode."
  69.   :type '(repeat string)
  70.   :group 'punycode)
  71.  
  72. ;; Internal process handling:
  73.  
  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.")
  78.  
  79. (defun punycode-encode-response-clear ()
  80.   (setq punycode-encode-response nil))
  81.  
  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)
  87.  
  88. (defun punycode-encode-filter (process string)
  89.   (setq punycode-encode-response (concat punycode-encode-response string)))
  90.  
  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
  96.     (condition-case ()
  97.         (kill-process punycode-encode-process)
  98.       (error)))
  99.     (when (setq punycode-encode-process
  100.         (let ((process-environment (append punycode-environment
  101.                            process-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))
  108.  
  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.")
  113.  
  114. (defun punycode-decode-response-clear ()
  115.   (setq punycode-decode-response nil))
  116.  
  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)
  122.  
  123. (defun punycode-decode-filter (process string)
  124.   (setq punycode-decode-response (concat punycode-decode-response string)))
  125.  
  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
  131.     (condition-case ()
  132.         (kill-process punycode-decode-process)
  133.       (error)))
  134.     (when (setq punycode-decode-process
  135.         (let ((process-environment (append punycode-environment
  136.                            process-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))
  143.  
  144. ;; Punycode Elisp API:
  145.  
  146. (defun punycode-encode (str)
  147.   "Returns a Punycode encoding of STR."
  148.   (let ((proc (punycode-encode-process))
  149.     string)
  150.     (if (null proc)
  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)))
  157.     string))))
  158.  
  159. (defun punycode-decode (str)
  160.   "Returns a possibly multibyte string which is the punycode decoding of STR."
  161.   (let ((proc (punycode-decode-process))
  162.     string)
  163.     (if (null proc)
  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)))
  170.     string))))
  171.  
  172. (defun punycode-shutdown ()
  173.   "Kill the punycode related process."
  174.   (interactive)
  175.   (if (and punycode-decode-process
  176.        (eq (process-status punycode-decode-process) 'run))
  177.       (kill-process punycode-decode-process))
  178.   (if (and punycode-encode-process
  179.        (eq (process-status punycode-encode-process) 'run))
  180.       (kill-process punycode-encode-process)))
  181.  
  182. (provide 'punycode)
  183.  
  184. ;;; punycode.el ends here
  185.