Merge semi21-D20010129.
[elisp/lemi.git] / emacs-lisp / filename.el
1 ;;; filename.el --- file name filter
2
3 ;; Copyright (C) 1996,1997,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: file name, string
7
8 ;; This file is part of APEL (A Portable Emacs Library).
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 'path-util)
28
29 (or (fboundp 'char-int)
30     (defalias 'char-int 'identity))
31
32 ;; Emacs 20.1/XEmacs 20.3 (but first appeared in Epoch?): (functionp OBJECT)
33 (or (fboundp 'functionp)
34     (defun functionp (object)
35       "Non-nil if OBJECT is a type of object that can be called as a function."
36       (or (subrp object) (byte-code-function-p object)
37           (eq (car-safe object) 'lambda)
38           (and (symbolp object) (fboundp object)))))
39
40
41 (defsubst poly-funcall (functions argument)
42   "Apply initial ARGUMENT to sequence of FUNCTIONS.
43 FUNCTIONS is list of functions.
44
45 (poly-funcall '(f1 f2 .. fn) arg) is as same as
46 (fn .. (f2 (f1 arg)) ..).
47
48 For example, (poly-funcall '(car number-to-string) '(100)) returns
49 \"100\"."
50   (while functions
51     (setq argument (funcall (car functions) argument)
52           functions (cdr functions)))
53   argument)
54
55
56 ;;; @ variables
57 ;;;
58
59 (defvar filename-limit-length 21 "Limit size of file-name.")
60
61 (defvar filename-replacement-alist
62   '(((?\  ?\t) . "_")
63     ((?! ?\" ?# ?$ ?% ?& ?' ?\( ?\) ?* ?/
64          ?: ?\; ?< ?> ?? ?\[ ?\\ ?\] ?` ?{ ?| ?}) . "_")
65     (filename-control-p . ""))
66   "Alist list of characters vs. string as replacement.
67 List of characters represents characters not allowed as file-name.")
68
69 (defvar filename-filters
70   (let ((filters '(filename-special-filter
71                    filename-eliminate-top-low-lines
72                    filename-canonicalize-low-lines
73                    filename-maybe-truncate-by-size
74                    filename-eliminate-bottom-low-lines
75                    )))
76     (if (exec-installed-p "kakasi")
77         (cons 'filename-japanese-to-roman-string filters)
78       filters))
79   "List of functions for file-name filter.")
80
81
82 ;;; @ filters
83 ;;;
84
85 (defun filename-japanese-to-roman-string (str)
86   (save-excursion
87     (set-buffer (get-buffer-create " *temp kakasi*"))
88     (erase-buffer)
89     (insert str)
90     (call-process-region
91      (point-min)(point-max)
92      "kakasi" t t t "-Ha" "-Ka" "-Ja" "-Ea" "-ka")
93     (buffer-string)))
94
95 (defun filename-control-p (character)
96   (let ((code (char-int character)))
97     (or (< code 32)(= code 127))))
98
99 (defun filename-special-filter (string)
100   (let ((len (length string))
101         (b 0)(i 0)
102         (dest ""))
103     (while (< i len)
104       (let ((chr (sref string i))
105             (lst filename-replacement-alist)
106             ret)
107         (while (and lst (not ret))
108           (if (if (functionp (car (car lst)))
109                   (setq ret (funcall (car (car lst)) chr))
110                 (setq ret (memq chr (car (car lst)))))
111               t                         ; quit this loop.
112             (setq lst (cdr lst))))
113         (if ret
114             (setq dest (concat dest (substring string b i)(cdr (car lst)))
115                   i (1+ i)
116                   ;; i (+ i (char-length chr))
117                   b i)
118           (setq i (1+ i))
119           ;; (setq i (+ i (char-length chr)))
120           )))
121     (concat dest (substring string b))))
122
123 (defun filename-eliminate-top-low-lines (string)
124   (if (string-match "^_+" string)
125       (substring string (match-end 0))
126     string))
127
128 (defun filename-canonicalize-low-lines (string)
129   (let ((dest ""))
130     (while (string-match "__+" string)
131       (setq dest (concat dest (substring string 0 (1+ (match-beginning 0)))))
132       (setq string (substring string (match-end 0))))
133     (concat dest string)))
134
135 (defun filename-maybe-truncate-by-size (string)
136   (if (and (> (length string) filename-limit-length)
137            (string-match "_" string filename-limit-length))
138       (substring string 0 (match-beginning 0))
139     string))
140
141 (defun filename-eliminate-bottom-low-lines (string)
142   (if (string-match "_+$" string)
143       (substring string 0 (match-beginning 0))
144     string))
145
146
147 ;;; @ interface
148 ;;;
149
150 (defun replace-as-filename (string)
151   "Return safety filename from STRING.
152 It refers variable `filename-filters' and default filters refers
153 `filename-limit-length', `filename-replacement-alist'."
154   (and string
155        (poly-funcall filename-filters string)))
156
157
158 ;;; @ end
159 ;;;
160
161 (require 'product)
162 (product-provide (provide 'filename) (require 'apel-ver))
163
164 ;;; filename.el ends here