-
Notifications
You must be signed in to change notification settings - Fork 7
/
recently-opened.rkt
63 lines (52 loc) · 2.04 KB
/
recently-opened.rkt
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
#lang racket/base
(provide read-recently-opened-files ; return list of recently opened files
write-recently-opened-files ; write value of current-recenly-opened-files to disk
add-recently-opened-file ; add a single file to parameter and write disk
reset-recently-opened-files) ; make an empty data file
;;;
;;; Recently Opened Files
;;;
(require (for-syntax racket/base)
racket/file
racket/format
racket/runtime-path
"parameters.rkt")
(define-runtime-path .remacs-dir ".remacs")
(define-runtime-path recently-opened.rktd ".remacs/recently.rktd")
(define no-recent-files '())
(define (read-recently-opened-files)
(with-handlers ([exn:fail? (λ () no-recent-files)])
(maybe-create-recently-opened-data-file)
(define fs (file->lines recently-opened.rktd))
(filter file-exists? fs)))
(define (write-recently-opened-files)
(define error-msg "write-recently-opened-files: error while writing")
(with-handlers ([exn:fail? (log-error error-msg)])
(with-output-to-file recently-opened.rktd
(λ ()
(for ([f (current-recently-opened-files)]
[_ 20])
(displayln f)))
#:exists 'truncate/replace)))
(define (add-recently-opened-file file)
(log-debug (~a `(add-recently-opened-file ,file)))
(when (path? file)
(set! file (path->string file)))
(define c current-recently-opened-files)
(when (and (or (string? file) (path? file)))
(c (cons file (remove* (list file) (c)))))
(write-recently-opened-files)
((current-update-recent-files-menu)))
(define (reset-recently-opened-files)
(parameterize ([current-recently-opened-files '()])
(write-recently-opened-files)))
;;; Helpers
; The helpers will throw an exception on any error
(define (maybe-create-remacs-dir)
(unless (directory-exists? .remacs-dir)
(make-directory .remacs-dir)))
(define (maybe-create-recently-opened-data-file)
(maybe-create-remacs-dir)
(unless (file-exists? recently-opened.rktd)
(with-output-to-file recently-opened.rktd
(λ () (void)))))