f9be5f61ca799d489cc64fa9355a7baafd43e76d
[elisp/apel.git] / emu-nemacs.el
1 ;;; emu-nemacs.el --- emu API implementation for NEmacs
2
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, NEmacs, mule
7
8 ;; This file is part of emu.
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'poem)
28
29
30 ;;; @ coding system
31 ;;;
32
33 ;;; @@ for old MULE emulation
34 ;;;
35
36 (defconst *noconv*    0)
37 (defconst *sjis*      1)
38 (defconst *junet*     2)
39 (defconst *ctext*     2)
40 (defconst *internal*  3)
41 (defconst *euc-japan* 3)
42
43 (defun code-convert-string (str ic oc)
44   "Convert code in STRING from SOURCE code to TARGET code,
45 On successful converion, returns the result string,
46 else returns nil. [emu-nemacs.el; Mule emulating function]"
47   (if (not (eq ic oc))
48       (convert-string-kanji-code str ic oc)
49     str))
50
51 (defun code-convert-region (beg end ic oc)
52   "Convert code of the text between BEGIN and END from SOURCE
53 to TARGET. On successful conversion returns t,
54 else returns nil. [emu-nemacs.el; Mule emulating function]"
55   (if (/= ic oc)
56       (save-excursion
57         (save-restriction
58           (narrow-to-region beg end)
59           (convert-region-kanji-code beg end ic oc)))
60     ))
61
62
63 ;;; @ without code-conversion
64 ;;;
65
66 (fset 'insert-binary-file-contents 'insert-file-contents-as-binary)
67
68 (defun insert-binary-file-contents-literally (filename
69                                               &optional visit beg end replace)
70   "Like `insert-file-contents-literally', q.v., but don't code conversion.
71 A buffer may be modified in several ways after reading into the buffer due
72 to advanced Emacs features, such as file-name-handlers, format decoding,
73 find-file-hooks, etc.
74   This function ensures that none of these modifications will take place.
75 \[emu-nemacs.el]"
76   (as-binary-input-file
77    ;; Returns list absolute file name and length of data inserted.
78    (insert-file-contents-literally filename visit beg end replace)))
79
80
81 ;;; @ MIME charset
82 ;;;
83
84 (defvar charsets-mime-charset-alist
85   '(((ascii) . us-ascii)))
86
87 (defvar default-mime-charset 'iso-2022-jp)
88
89 (defvar mime-charset-coding-system-alist
90   '((iso-2022-jp     . 2)
91     (shift_jis       . 1)
92     ))
93
94 (defun mime-charset-to-coding-system (charset)
95   (if (stringp charset)
96       (setq charset (intern (downcase charset)))
97     )
98   (cdr (assq charset mime-charset-coding-system-alist)))
99
100 (defun detect-mime-charset-region (start end)
101   "Return MIME charset for region between START and END.
102 \[emu-nemacs.el]"
103   (if (save-excursion
104         (save-restriction
105           (narrow-to-region start end)
106           (goto-char start)
107           (re-search-forward "[\200-\377]" nil t)))
108       default-mime-charset
109     'us-ascii))
110
111 (defun encode-mime-charset-region (start end charset)
112   "Encode the text between START and END as MIME CHARSET.
113 \[emu-nemacs.el]"
114   (let ((cs (mime-charset-to-coding-system charset)))
115     (and (numberp cs)
116          (or (= cs 3)
117              (save-excursion
118                (save-restriction
119                  (narrow-to-region start end)
120                  (convert-region-kanji-code start end 3 cs))))
121          )))
122
123 (defun decode-mime-charset-region (start end charset &optional lbt)
124   "Decode the text between START and END as MIME CHARSET.
125 \[emu-nemacs.el]"
126   (let ((cs (mime-charset-to-coding-system charset))
127         (nl (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")
128                              (dos . "\r\n") (mac . "\r"))))))
129     (and (numberp cs)
130          (or (= cs 3)
131              (save-excursion
132                (save-restriction
133                  (narrow-to-region start end)
134                  (convert-region-kanji-code start end cs 3)
135                  (if nl
136                      (progn
137                        (goto-char (point-min))
138                        (while (search-forward nl nil t)
139                          (replace-match "\n")))
140                    )))
141              ))))
142
143 (defun encode-mime-charset-string (string charset)
144   "Encode the STRING as MIME CHARSET. [emu-nemacs.el]"
145   (let ((cs (mime-charset-to-coding-system charset)))
146     (if cs
147         (convert-string-kanji-code string 3 cs)
148       string)))
149
150 (defun decode-mime-charset-string (string charset &optional lbt)
151   "Decode the STRING as MIME CHARSET. [emu-nemacs.el]"
152   (with-temp-buffer
153     (insert string)
154     (decode-mime-charset-region (point-min)(point-max) charset lbt)
155     (buffer-string)))
156
157 (defun write-region-as-mime-charset (charset start end filename)
158   "Like `write-region', q.v., but code-convert by MIME CHARSET.
159 \[emu-nemacs.el]"
160   (let ((kanji-fileio-code
161          (or (mime-charset-to-coding-system charset)
162              *noconv*)))
163     (write-region start end filename)))
164
165
166 ;;; @ end
167 ;;;
168
169 (provide 'emu-nemacs)
170
171 ;;; emu-nemacs.el ends here