|
1 | | -#lang scheme/unit |
2 | | - (require "sig.rkt" |
3 | | - racket/list |
4 | | - "../preferences.rkt") |
| 1 | +#lang racket/unit |
| 2 | + |
| 3 | +(require "sig.rkt" |
| 4 | + racket/list |
| 5 | + "../preferences.rkt") |
5 | 6 |
|
6 | | - (import) |
7 | | - (export framework:path-utils^) |
| 7 | +(import) |
| 8 | +(export framework:path-utils^) |
8 | 9 |
|
9 | 10 | ;; preferences initialized in main.rkt |
10 | 11 |
|
|
22 | 23 | (define current-autosave-dir |
23 | 24 | (make-getter/ensure-exists 'path-utils:autosave-dir)) |
24 | 25 |
|
25 | | - ; generate-autosave-name : (or/c #f path-string? path-for-some-system?) -> path? |
26 | | - (define (generate-autosave-name maybe-old-path) |
27 | | - (cond |
28 | | - [maybe-old-path |
29 | | - (let*-values ([(base name dir?) (split-path maybe-old-path)] |
30 | | - [(base) (cond |
31 | | - [(not (path? base)) |
32 | | - (current-directory)] |
33 | | - [(relative-path? base) |
34 | | - (build-path (current-directory) base)] |
35 | | - [else |
36 | | - base])]) |
37 | | - (cond |
38 | | - [(current-autosave-dir) |
39 | | - => |
40 | | - (λ (dir) |
41 | | - (make-unique-autosave-name dir (encode-as-path-element base name)))] |
42 | | - [else |
43 | | - (make-unique-autosave-name base name)]))] |
44 | | - [else |
45 | | - (make-unique-autosave-name (or (current-autosave-dir) |
46 | | - (find-system-path 'doc-dir)) |
47 | | - (bytes->path-element #"mredauto"))])) |
48 | | - |
49 | | - |
50 | | - ; make-unique-autosave-name : dir-path path-element -> path? |
51 | | - (define (make-unique-autosave-name dir name) |
52 | | - (let loop ([n 1]) |
53 | | - (let* ([numb (string->bytes/utf-8 (number->string n))] |
54 | | - [new-name |
55 | | - (build-path dir |
56 | | - (if (eq? (system-type) 'windows) |
57 | | - (bytes->path-element |
58 | | - (bytes-append (regexp-replace #rx#"\\..*$" |
59 | | - (path-element->bytes name) |
60 | | - #"") |
61 | | - #"." |
62 | | - numb)) |
63 | | - (bytes->path-element |
64 | | - (bytes-append #"#" |
65 | | - (path-element->bytes name) |
66 | | - #"#" |
67 | | - numb |
68 | | - #"#"))))]) |
69 | | - (if (file-exists? new-name) |
70 | | - (loop (add1 n)) |
71 | | - new-name)))) |
72 | | - |
73 | | - ;; generate-backup-name : path? -> path? |
74 | | - (define (generate-backup-name full-name) |
75 | | - (let-values ([(pre-base name dir?) (split-path full-name)]) |
76 | | - (let ([base (if (path? pre-base) |
77 | | - pre-base |
78 | | - (current-directory))]) |
79 | | - (define name-element |
80 | | - (let ([name-bytes (path-element->bytes name)]) |
81 | | - (bytes->path-element |
82 | | - (cond |
83 | | - [(and (eq? (system-type) 'windows) |
84 | | - (regexp-match #rx#"(.*)\\.[^.]*" name-bytes)) |
85 | | - => |
86 | | - (λ (m) |
87 | | - (bytes-append (cadr m) #".bak"))] |
88 | | - [(eq? (system-type) 'windows) |
89 | | - (bytes-append name-bytes #".bak")] |
90 | | - [else |
91 | | - (bytes-append name-bytes #"~")])))) |
92 | | - (cond |
93 | | - [(current-backup-dir) |
94 | | - => |
95 | | - (λ (dir) |
96 | | - (build-path dir (encode-as-path-element base name-element)))] |
97 | | - [else |
98 | | - (build-path base name-element)])))) |
99 | | - |
100 | | - |
101 | | - |
| 26 | +; generate-autosave-name : (or/c #f path-string? path-for-some-system?) -> path? |
| 27 | +(define (generate-autosave-name maybe-old-path) |
| 28 | + (cond |
| 29 | + [maybe-old-path |
| 30 | + (let*-values ([(base name dir?) (split-path maybe-old-path)] |
| 31 | + [(base) (cond |
| 32 | + [(not (path? base)) |
| 33 | + (current-directory)] |
| 34 | + [(relative-path? base) |
| 35 | + (build-path (current-directory) base)] |
| 36 | + [else |
| 37 | + base])]) |
| 38 | + (cond |
| 39 | + [(current-autosave-dir) |
| 40 | + => |
| 41 | + (λ (dir) |
| 42 | + (make-unique-autosave-name dir (encode-as-path-element base name)))] |
| 43 | + [else |
| 44 | + (make-unique-autosave-name base name)]))] |
| 45 | + [else |
| 46 | + (make-unique-autosave-name (or (current-autosave-dir) |
| 47 | + (find-system-path 'doc-dir)) |
| 48 | + (bytes->path-element #"mredauto"))])) |
| 49 | + |
| 50 | + |
| 51 | +; make-unique-autosave-name : dir-path path-element -> path? |
| 52 | +(define (make-unique-autosave-name dir name) |
| 53 | + (define sys |
| 54 | + (system-path-convention-type)) |
| 55 | + (let loop ([n 1]) |
| 56 | + (let* ([numb (string->bytes/utf-8 (number->string n))] |
| 57 | + [new-name |
| 58 | + (build-path dir |
| 59 | + (case sys |
| 60 | + [(windows) |
| 61 | + (path-replace-extension name |
| 62 | + (bytes-append #"." |
| 63 | + numb))] |
| 64 | + [else |
| 65 | + (bytes->path-element |
| 66 | + (bytes-append #"#" |
| 67 | + (path-element->bytes name) |
| 68 | + #"#" |
| 69 | + numb |
| 70 | + #"#"))]))]) |
| 71 | + (if (file-exists? new-name) |
| 72 | + (loop (add1 n)) |
| 73 | + new-name)))) |
| 74 | + |
| 75 | + |
| 76 | +;; generate-backup-name : path? -> path? |
| 77 | +(define (generate-backup-name full-name) |
| 78 | + (define-values (pre-base old-name dir?) |
| 79 | + (split-path full-name)) |
| 80 | + (define base |
| 81 | + (if (path? pre-base) |
| 82 | + pre-base |
| 83 | + (current-directory))) |
| 84 | + (define name-element |
| 85 | + (case (system-path-convention-type) |
| 86 | + [(windows) |
| 87 | + (path-replace-extension old-name #".bak")] |
| 88 | + [else |
| 89 | + (bytes->path-element |
| 90 | + (bytes-append (path-element->bytes old-name) #"~"))])) |
| 91 | + (cond |
| 92 | + [(current-backup-dir) |
| 93 | + => |
| 94 | + (λ (dir) |
| 95 | + (build-path dir (encode-as-path-element base name-element)))] |
| 96 | + [else |
| 97 | + (build-path base name-element)])) |
| 98 | + |
| 99 | + |
102 | 100 | (define candidate-separators |
103 | 101 | `(#"!" #"%" #"_" #"|" #":" #">" #"^" #"$" #"@" #"*" #"?")) |
104 | 102 |
|
|
0 commit comments