add emms
[emacs.git] / .emacs.d / elisp / emms / lisp / emms-i18n.el
1 ;;; emms-i18n.el --- functions for handling coding systems
2
3 ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 ;; Author: Ye Wenbin <wenbinye@163.com>
6
7 ;; This file is part of EMMS.
8
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation; either version 3, or (at
12 ;; your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; if not, write to the Free Software
21 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; When reading from process, first check the car part of
26 ;; `emms-i18n-default-coding-system'; if non-nil, use this for
27 ;; decoding, and never detect coding system; if nil, first call
28 ;; `emms-i18n-coding-detect-functions' to get coding system, if
29 ;; success, decode the result, otherwise, use
30 ;; `emms-i18n-detect-coding-function', the Emacs detect coding
31 ;; function, if the coding detected is not in
32 ;; `emms-i18n-never-used-coding-system', decode it, otherwise use
33 ;; locale-coding-system.
34
35 ;; When writing/sending data to process, first check the cdr part of
36 ;; `emms-i18n-default-coding-system', if non-nil, use this to encode
37 ;; data, otherwise do nothing, that means use
38 ;; `default-process-coding-system' or `process-coding-system-alist' to
39 ;; encode data.
40
41 ;; Put this file into your load-path and the following into your
42 ;; ~/.emacs:
43
44 ;; (require 'emms-i18n)
45
46 ;;; Code:
47
48 (provide 'emms-i18n)
49 (eval-when-compile
50 (require 'cl))
51
52 ;; TODO: Use defcustom.
53 (defvar emms-i18n-never-used-coding-system
54 '(raw-text undecided)
55 "If the `emms-i18n-coding-detect-functions' return a coding
56 system in this list, use `emms-i18n-default-coding-system'
57 instead.")
58
59 ;; TODO: Use defcustom.
60 (defvar emms-i18n-coding-system-for-read
61 'utf-8
62 "If coding detect fails, use this for decoding.")
63
64 ;; TODO: Use defcustom.
65 (defvar emms-i18n-default-coding-system
66 '(no-conversion . no-conversion)
67 "If non-nil, use this for decoding and encoding.")
68
69 ;; TODO: Use defcustom.
70 (defvar emms-i18n-coding-detect-functions
71 nil
72 "A list of functions to call to detect codings.")
73
74 ;; TODO: Use defcustom.
75 (defvar emms-i18n-detect-max-size
76 10000
77 "Maximum amount of bytes to detect the coding system. nil
78 means to scan the whole buffer.")
79
80 (defun emms-i18n-iconv (from to str)
81 "Convert string STR from FROM coding to TO coding."
82 (if (and from to)
83 (decode-coding-string
84 (encode-coding-string str to)
85 from)
86 str))
87
88 (defun emms-i18n-iconv-region (beg end from to)
89 (when (and from to)
90 (save-restriction
91 (narrow-to-region beg end)
92 (encode-coding-region (point-min) (point-max) to)
93 (decode-coding-region (point-min) (point-max) from))))
94
95 (defun emms-i18n-iconv-buffer (from to &optional buf)
96 "Convert buffer BUF from FROM coding to TO coding. BUF
97 defaults to the current buffer."
98 (save-excursion
99 (and buf (set-buffer buf))
100 (emms-i18n-iconv-region (point-min) (point-max) from to)))
101
102 (defun emms-i18n-set-default-coding-system (read-coding write-coding)
103 "Set `emms-i18n-default-coding-system'."
104 (interactive "zSet coding system for read: \nzSet coding system for write: ")
105 (setq emms-i18n-default-coding-system
106 (cons
107 (and (coding-system-p read-coding) read-coding)
108 (and (coding-system-p write-coding) write-coding)))
109 (message (concat
110 (if (car emms-i18n-default-coding-system)
111 (format "The coding system for reading is %S." (car emms-i18n-default-coding-system))
112 "Good, you want me to detect the coding system!")
113 (format " The coding system for writing is %S."
114 (or (cdr emms-i18n-default-coding-system)
115 (cdr default-process-coding-system))))))
116
117 (defun emms-i18n-call-process-simple (&rest args)
118 "Run a program and return the program result.
119 If the car part of `emms-i18n-default-coding-system' is non-nil,
120 the program result will be decoded using the car part of
121 `emms-i18n-default-coding-system'. Otherwise, use
122 `emms-i18n-coding-detect-functions' to detect the coding system
123 of the result. If the `emms-i18n-coding-detect-functions'
124 failed, use `emms-i18n-detect-coding-function' to detect coding
125 system. If all the coding systems are nil or in
126 `emms-i18n-never-used-coding-system', decode the result using
127 `emms-i18n-coding-system-for-read'.
128
129 ARGS are the same as in `call-process', except BUFFER should
130 always have the value t. Otherwise the coding detection will not
131 be performed."
132 (let ((default-process-coding-system (copy-tree default-process-coding-system))
133 (process-coding-system-alist nil) exit pos)
134 (when (eq (nth 2 args) 't)
135 (setcar default-process-coding-system (car emms-i18n-default-coding-system))
136 (setq pos (point)))
137 (setq exit (apply 'call-process args))
138 (when (and (eq (nth 2 args) 't)
139 (eq (car emms-i18n-default-coding-system) 'no-conversion))
140 (save-restriction
141 (narrow-to-region pos (point))
142 (decode-coding-region (point-min) (point-max) (emms-i18n-detect-buffer-coding-system))))
143 exit))
144
145 ;; TODO: Is this function useful?
146 (defun emms-i18n-call-process (&rest args)
147 "Run the program like `call-process'. If the cdr part of
148 `emms-i18n-default-coding-system' is non-nil, the string in ARGS
149 will be encoded by the cdr part of
150 `emms-i18n-default-coding-system'; otherwise, all parameters are
151 simply passed to `call-process'."
152 (with-temp-buffer
153 (if (cdr emms-i18n-default-coding-system)
154 (let ((default-process-coding-system emms-i18n-default-coding-system)
155 (process-coding-system-alist nil))
156 (apply 'call-process args))
157 (apply 'call-process args))))
158
159 (defun emms-i18n-detect-coding-function (size)
160 (detect-coding-region (point)
161 (+ (if (null emms-i18n-detect-max-size)
162 size
163 (min size emms-i18n-detect-max-size))
164 (point)) t))
165
166 (defun emms-i18n-detect-buffer-coding-system (&optional buf)
167 "Before calling this function, make sure the buffer is literal."
168 (let ((size (- (point-max) (point-min)))
169 (func (append emms-i18n-coding-detect-functions 'emms-i18n-detect-coding-function))
170 coding)
171 (save-excursion
172 (and buf (set-buffer buf))
173 (goto-char (point-min))
174 (when (> size 0)
175 (setq coding (run-hook-with-args-until-success 'func size))
176 (if (member (coding-system-base coding) emms-i18n-never-used-coding-system)
177 (setq coding (emms-i18n-detect-coding-function size))))
178 (if (or (null coding) (member (coding-system-base coding) emms-i18n-never-used-coding-system))
179 emms-i18n-coding-system-for-read
180 coding))))
181
182 ;;; emms-i18n.el ends here