[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [elisp] Look for function that create safe file name from string...
From: |
Oleksandr Gavenko |
Subject: |
Re: [elisp] Look for function that create safe file name from string... |
Date: |
Tue, 10 Jul 2012 17:34:29 +0300 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) |
>From Eli Zaretskii:
> I think you will find a few ideas in make-auto-save-file-name.
>
I look sources and most useful thing I find (for me) is replacement of '/'
with '!'. Also this piece are good:
;; Restrict the characters used in the file name to those which
;; are known to be safe on all filesystems, url-encoding the
;; rest.
...
(while (string-match "[^A-Za-z0-9-_.~#+]" buffer-name limit)
...
But I lose Russian chars...
On 2012-07-09, Pascal J. Bourguignon wrote:
> Oleksandr Gavenko <gavenkoa@gmail.com> writes:
>
>> I decide store my message after sending it to mail/news.
>>
>> As file name I use date and message header.
>>
>> But message header may contain danger symbol. One of errors I get when '/'
>> char present in file name...
>>
>> So I look for function that safely escape string to get file name in portable
>> for Emacs way...
>
> (defun alphanumericp (ch)
> (find ch "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))
>
> (defun clean-filename (name)
> (remove-if-not (lambda (ch) (or (alphanumericp ch) (find ch "-_.")))
> (substitute ?- 32 name)))
>
I like your solution. But as I need Russian/Ukrainian chars I write another
code (without cl dependency):
(defconst my-safe-filename-char-regex "[[:alnum:]-_!.@]"
"Safe file names.")
(defun my-clean-filename (filename)
(mapconcat
(lambda (ch) (or (when (string-match my-safe-filename-char-regex
(char-to-string ch)) (char-to-string ch)) "-"))
filename "") )
Resulted file name does not contain spaces, so I can easy write sh scripts on
these files.
Thanks for help to both!
--
Best regards!