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