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
let g:logname = "$TEMP/VIM/backup.log"
" ログファイル名...変更してください

function! Backup2UsbMemory(dir, compress, ...)
    if a:dir =~ '/$'
       let l:dirroot = a:dir
    else
       let l:dirroot = a:dir .'/'
    endif
    let l:dirname = l:dirroot .strftime("%Y/%Y_%m/%d")
    if isdirectory(l:dirname) == 0
        if isdirectory(l:dirroot .strftime("%Y/%Y_%m")) == 0
            if isdirectory(l:dirroot .strftime("%Y/")) == 0
                call mkdir(l:dirroot .strftime("%Y/"))
            endif
            call mkdir(l:dirroot .strftime("%Y/%Y_%m"))
        endif
        call mkdir(l:dirname)
        if expand('%') == ''
            echo 'Vim only made a directory named "' .l:dirname .'/"'
            return
        endif
    elseif expand('%') == ''
        echohl ErrorMsg
        echo 'Error: No file name'
        echo 'Directory ' .l:dirname ." is already made."
        echohl None
        return
    endif
    let l:compformat = ''
    if a:compress == 'gzip'
        let l:compformat = '.gz'
    elseif a:compress == 'bzip2'
        let l:compformat = '.bz2'
    endif
    let l:extend = expand("%:e")
    if l:extend != ""
        let l:extend = "." .l:extend
    endif

    let l:partsign = (len(a:000) > 0) ? "_part" : ""

    let l:filename = expand("%:t:r")
    let l:filepath = expand("%:p:h")
    if l:filepath != getcwd() && l:filepath != "/"
        let l:filename = substitute(substitute(l:filepath,'\','_','g'),'^\D:.','','g') ."_" .filename
    endif
    let l:filenamefull = l:dirname .'/' .l:filename .l:partsign ."_" .strftime("%y%m%d_%H%M") .l:extend .l:compformat
    if filereadable(l:filenamefull)
        return "filename is already exists."
    endif

    let l:tmp = tempname()

    let l:lnum = (len(a:000) > 0) ? a:1 : '^'
    let l:end  = (len(a:000) > 1) ? a:2 : '$'

    call writefile(getline(l:lnum, l:end), l:tmp)
    call system(a:compress ." " .l:tmp)
    let l:fsize = ArrangeNumber(getfsize(l:tmp .l:compformat))
    call rename(l:tmp .l:compformat, l:filenamefull)

    " add information to logfile
    let l:inform = ["@ " .strftime("%Y-%m-%d %a %H:%M :%S"), "Name: " .l:filenamefull, "Size: " .l:fsize]
    call AppendBackupLog(l:inform, g:logname)
    " ----
    return '"' .split(l:filenamefull,'/')[-1] .'" Size: ' .l:fsize .' saved'
endfunction

function! AppendBackupLog(inform, logname)
    let l:loginform = []
    if filereadable(a:logname)
        let l:loginform = readfile(a:logname)
    endif
    call writefile(l:loginform + a:inform, a:logname)
endfunction

" flag... ''ならログファイル表示, 'promptdel'なら削除ダイアログ, 'del'なら確認せず削除
function! ViewBackupLog(logname, flag)
    let l:blankmax = 10
    if filereadable(a:logname)
        let l:loginform = readfile(a:logname)
    else
        echo "logfile not found."
        return
    endif
    if a:flag =~ "del"
        if a:flag =~ "promptdel"
            while 1
                let l:answer = input("delete logfile? (y/n):")
                if l:answer =~ '^y\>'
                    break
                elseif l:answer =~ '^n\>'
                    return
                endif
            endwhile
        endif
        call delete(a:logname)
        echo "deleted."
        return
    endif
    echohl Title
    echo "--- Backup Log --- "
    echohl None
    for l:i in range(0, len(loginform) -1, 3)
        let l:date = split(l:loginform[l:i])[1]
        let l:time = split(l:loginform[l:i])[3]
        let l:filename = split(l:loginform[l:i +1], '/')[-1]
        let l:fsize = split(l:loginform[l:i +2])[1]
        echo l:date ." " .l:time .Blank(l:blankmax - len(l:fsize)) .l:fsize ." " .l:filename
    endfor
        return
endfunction

function! ArrangeNumber(num)
    if a:num < 1
        return a:num
    endif
    let l:len = strlen(a:num)
    if l:len <= 3
        return a:num
    endif
    let l:result = ""
    let l:modulo = l:len % 3
    if l:modulo != 0
        let l:result .= a:num[0:(l:modulo -1)] .','
    endif
    for l:i in range(l:modulo, l:len -1, 3)
        let l:result .= a:num[(l:i):(l:i +2)] .','
    endfor
    return l:result[:-2]
endfunction

function! Blank(limit)
    if a:limit < 1
        return ""
    endif
    let l:blank = ""
    for l:count in range(a:limit)
        let l:blank .= " "
    endfor
    return l:blank
endfunction
inserted by FC2 system