File: mel-u.el

package info (click to toggle)
flim 1%3A1.14.9%2B0.20201117-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 864 kB
  • sloc: lisp: 7,300; makefile: 86; sh: 86
file content (164 lines) | stat: -rw-r--r-- 5,376 bytes parent folder | download | duplicates (2)
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
;;; mel-u.el --- uuencode encoder/decoder.  -*- lexical-binding: t -*-

;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.

;; Author: MORIOKA Tomohiko <tomo@m17n.org>
;; Created: 1995/10/25
;; Keywords: uuencode

;; This file is part of FLIM (Faithful Library about Internet Message).

;; This program 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.

;; This program 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 this program; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Code:

(require 'mime-def)
(require 'path-util)


(mel-define-backend "x-uue")


;;; @ variables
;;;

(defvar uuencode-external-encoder '("uuencode" "-")
  "*list of uuencode encoder program name and its arguments.")

(defvar uuencode-external-decoder '("sh" "-c" "uudecode")
  "*list of uuencode decoder program name and its arguments.")


;;; @ uuencode encoder/decoder for region
;;;

(defun uuencode-external-encode-region (start end)
  "Encode current region by unofficial uuencode format.
This function uses external uuencode encoder which is specified by
variable `uuencode-external-encoder'."
  (interactive "*r")
  (save-excursion
    (let ((coding-system-for-read  'binary)
	  (coding-system-for-write 'binary))
      (apply (function call-process-region)
	     start end (car uuencode-external-encoder)
	     t t nil
	     (cdr uuencode-external-encoder)))
    ;; for OS/2
    ;;   regularize line break code
    (goto-char (point-min))
    (while (re-search-forward "\r$" nil t)
      (replace-match ""))))

(defun uuencode-external-decode-region (start end)
  "Decode current region by unofficial uuencode format.
This function uses external uuencode decoder which is specified by
variable `uuencode-external-decoder'."
  (interactive "*r")
  (save-excursion
    (let ((filename (make-temp-file "x-uue")))
      (save-excursion
	(save-restriction
	  (set-mark end)
	  (narrow-to-region start end)
	  (goto-char start)
	  (when (and (re-search-forward "^begin [0-9]+ " nil t)
		     (looking-at ".+$"))
	    (replace-match filename)
	    (let ((coding-system-for-read  'binary)
		  (coding-system-for-write 'binary))
	      (apply (function call-process-region)
		     start (mark) (car uuencode-external-decoder)
		     t nil nil
		     (cdr uuencode-external-decoder)))
	    (insert-file-contents filename)
	    ;; The previous line causes the buffer to be made read-only, I
	    ;; do not pretend to understand the control flow leading to this
	    ;; but suspect it has something to do with image-mode. -slb
	    ;;	Use `inhibit-read-only' to avoid to force
	    ;;	buffer-read-only nil. - tomo.
	    (let ((inhibit-read-only t))
	      (delete-file filename))))))))

(mel-define-method-function (mime-encode-region start end (nil "x-uue"))
			    'uuencode-external-encode-region)
(mel-define-method-function (mime-decode-region start end (nil "x-uue"))
			    'uuencode-external-decode-region)


;;; @ encoder/decoder for string
;;;

(mel-define-method mime-encode-string (string (nil "x-uue"))
  (with-temp-buffer
    (insert string)
    (uuencode-external-encode-region (point-min)(point-max))
    (buffer-string)))

(mel-define-method mime-decode-string (string (nil "x-uue"))
  (with-temp-buffer
    (insert string)
    (uuencode-external-decode-region (point-min)(point-max))
    (buffer-string)))


;;; @ uuencode encoder/decoder for file
;;;

(mel-define-method mime-insert-encoded-file (filename (nil "x-uue"))
  "Insert file encoded by unofficial uuencode format.
This function uses external uuencode encoder which is specified by
variable `uuencode-external-encoder'."
  (interactive "*fInsert encoded file: ")
  (call-process (car uuencode-external-encoder)
		filename t nil
		(file-name-nondirectory filename)))

(mel-define-method mime-write-decoded-region (start end filename
						    (nil "x-uue"))
  "Decode and write current region encoded by uuencode into FILENAME.
START and END are buffer positions."
  (interactive "*r\nFWrite decoded region to file: ")
  (save-excursion
    (let ((clone-buf (clone-buffer " *x-uue*"))
	  (file (make-temp-file "x-uue")))
      (save-excursion
	(save-restriction
	  (set-buffer clone-buf)
	  (narrow-to-region start end)
	  (setq buffer-read-only nil)
	  (goto-char start)
	  (when (and (re-search-forward "^begin [0-9]+ " nil t)
		   (looking-at ".+$"))
	    (replace-match file)
	    (let ((coding-system-for-read  'binary)
		  (coding-system-for-write 'binary))
	      (apply (function call-process-region)
		     (point-min) (point-max) (car uuencode-external-decoder)
		     nil nil nil
		     (cdr uuencode-external-decoder))
	      (rename-file file filename 'overwrites)
	      (message (concat "Wrote " filename))))))
      (kill-buffer clone-buf))))

;;; @ end
;;;

(provide 'mel-u)

(mel-define-backend "x-uuencode" ("x-uue"))

;;; mel-u.el ends here.