-
Notifications
You must be signed in to change notification settings - Fork 10
/
filename.el
159 lines (124 loc) · 4.54 KB
/
filename.el
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
;;; filename.el --- file name filter -*- lexical-binding: t -*-
;; Copyright (C) 1996,1997 MORIOKA Tomohiko
;; Author: MORIOKA Tomohiko <[email protected]>
;; Version: $Id$
;; Keywords: file name, string
;; This file is part of APEL (A Portable Emacs Library).
;; 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 GNU Emacs; 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 'path-util)
(defsubst poly-funcall (functions argument)
"Apply initial ARGUMENT to sequence of FUNCTIONS.
FUNCTIONS is list of functions.
(poly-funcall \\='(f1 f2 .. fn) arg) is as same as
(fn .. (f2 (f1 arg)) ..).
For example, (poly-funcall \\='(car number-to-string) \\='(100)) returns
\"100\"."
(while functions
(setq argument (funcall (car functions) argument)
functions (cdr functions)))
argument)
;;; @ variables
;;;
(defvar filename-limit-length 21 "Limit size of file-name.")
(defvar filename-replacement-alist
'(((?\ ?\t) . "_")
((?! ?\" ?# ?$ ?% ?& ?' ?\( ?\) ?* ?/
?: ?\; ?< ?> ?? ?\[ ?\\ ?\] ?` ?{ ?| ?}) . "_")
(filename-control-p . ""))
"Alist list of characters vs. string as replacement.
List of characters represents characters not allowed as file-name.")
(defvar filename-filters nil
"List of functions for file-name filter.
Example:
(setq filename-filters \\='(filename-special-filter
filename-eliminate-top-low-lines
filename-canonicalize-low-lines
filename-maybe-truncate-by-size
filename-eliminate-bottom-low-lines))
Moreover, if you want to convert Japanese filename to roman string by kakasi,
(if (exec-installed-p \"kakasi\")
(setq filename-filters
(append \\='(filename-japanese-to-roman-string) filename-filters)))")
;;; @ filters
;;;
(defun filename-japanese-to-roman-string (str)
(with-current-buffer (get-buffer-create " *temp kakasi*")
(erase-buffer)
(insert str)
(call-process-region
(point-min)(point-max)
"kakasi" t t t "-Ha" "-Ka" "-Ja" "-Ea" "-ka")
(buffer-string)))
(defun filename-control-p (character)
(or (< character 32) (= character 127)))
(eval-when-compile
(defmacro filename-special-filter-1 (string)
(let (sref inc-i)
(setq sref 'aref
inc-i '(1+ i))
`(let ((len (length ,string))
(b 0)(i 0)
(dest ""))
(while (< i len)
(let ((chr (,sref ,string i))
(lst filename-replacement-alist)
ret)
(while (and lst (not ret))
(if (if (functionp (car (car lst)))
(setq ret (funcall (car (car lst)) chr))
(setq ret (memq chr (car (car lst)))))
t ; quit this loop.
(setq lst (cdr lst))))
(if ret
(setq dest (concat dest (substring ,string b i)
(cdr (car lst)))
i ,inc-i
b i)
(setq i ,inc-i))))
(concat dest (substring ,string b))))))
(defun filename-special-filter (string)
(filename-special-filter-1 string))
(defun filename-eliminate-top-low-lines (string)
(if (string-match "^_+" string)
(substring string (match-end 0))
string))
(defun filename-canonicalize-low-lines (string)
(let ((dest ""))
(while (string-match "__+" string)
(setq dest (concat dest (substring string 0 (1+ (match-beginning 0)))))
(setq string (substring string (match-end 0))))
(concat dest string)))
(defun filename-maybe-truncate-by-size (string)
(if (and (> (length string) filename-limit-length)
(string-match "_" string filename-limit-length))
(substring string 0 (match-beginning 0))
string))
(defun filename-eliminate-bottom-low-lines (string)
(if (string-match "_+$" string)
(substring string 0 (match-beginning 0))
string))
;;; @ interface
;;;
(defun replace-as-filename (string)
"Return safety filename from STRING.
It refers variable `filename-filters' and default filters refers
`filename-limit-length', `filename-replacement-alist'."
(and string
(poly-funcall filename-filters string)))
;;; @ end
;;;
(require 'product)
(product-provide (provide 'filename) (require 'apel-ver))
;;; filename.el ends here