summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemsen2012-09-16 14:09:56 +0200
committerGravatar Tom Willemsen2012-09-16 14:09:56 +0200
commitcae98ebdfbf9a1a852a810dc7d43a9717f7c7a9a (patch)
tree0544f84499534f3b51c867b784d760f61a8ac0de
parent86f48d9fa477e4507505aca3885c3ef7bb471cc0 (diff)
downloadundone-cae98ebdfbf9a1a852a810dc7d43a9717f7c7a9a.tar.gz
undone-cae98ebdfbf9a1a852a810dc7d43a9717f7c7a9a.zip
Make file loading a bit more robust
Don't fail when a file can't be found.
-rw-r--r--undone/main.scm72
1 files changed, 35 insertions, 37 deletions
diff --git a/undone/main.scm b/undone/main.scm
index 55a0de0..2f400e9 100644
--- a/undone/main.scm
+++ b/undone/main.scm
@@ -76,28 +76,38 @@
where #t
sort-by #f nil)))))
+(define (stored-file xdg-env file)
+ "Try to get FILE stored either in XDG-ENV or the home directory."
+ (let ((xdg (getenv xdg-env))
+ (home (getenv "HOME"))
+ (result #f))
+ (cond
+ (xdg
+ (let ((xdg-stored-file (string-append xdg "/undone/" file)))
+ (when (file-exists? xdg-stored-file)
+ (set! result xdg-stored-file))))
+ (home
+ (let ((home-stored-file (string-append home "/.undone/" file)))
+ (when (file-exists? home-stored-file)
+ (set! result home-stored-file)))))
+ result))
+
+(define (config-file file)
+ "Try to get FILE from the configuration file location."
+ (stored-file "XDG_CONFIG_HOME" file))
+
+(define (data-file file)
+ "Try to get FILE from the data file location."
+ (stored-file "XDG_DATA_HOME" file))
+
(define todo-list-file
- (let* ((xdg (getenv "XDG_DATA_HOME"))
- (home (getenv "HOME"))
- (todo-file-name "todo.scm")
- (xdg-todo-file (string-append xdg "/undone/" todo-file-name))
- (home-todo-file (string-append xdg "/.undone/" todo-file-name)))
- (if (and xdg (not (file-exists? home-todo-file)))
- xdg-todo-file
- home-todo-file)))
+ (data-file "todo.scm"))
(define rc-file
- (let* ((xdg (getenv "XDG_CONFIG_HOME"))
- (home (getenv "HOME"))
- (rc-file-name "undonerc.scm")
- (xdg-rc-file (string-append xdg "/undone/" rc-file-name))
- (home-rc-file (string-append home "/.undone/" rc-file-name)))
- (if (and xdg (file-exists? xdg-rc-file))
- xdg-rc-file
- home-rc-file)))
+ (config-file "undonerc.scm"))
(define todo-list
- (if (file-exists? todo-list-file)
+ (if (and todo-list-file (file-exists? todo-list-file))
(let* ((port (open-input-file todo-list-file))
(result (read port)))
(close-port port)
@@ -106,22 +116,6 @@
(define view-list '())
-(define (data-dir)
- "Get the location for data files."
- (let ((xdg (getenv "XDG_DATA_HOME")))
- (string-append (if xdg
- (string-append xdg "/")
- (string-append (getenv "HOME") "/."))
- "undone")))
-
-(define (config-dir)
- "Get the location for configuration files."
- (let ((xdg (getenv "XDG_CONFIG_HOME")))
- (string-append (if xdg
- (string-append xdg "/")
- (string-append (getenv "HOME") "/."))
- "undone")))
-
(define (mkdirs path)
(let ((parent (dirname path)))
(if (not (file-exists? parent))
@@ -131,7 +125,6 @@
(mkdir path)))
(define (save)
-
"Save the list."
(mkdirs (dirname todo-list-file))
@@ -178,12 +171,17 @@
(if (procedure? view-func)
(apply view-func '()))))
-(define (main args)
- ;; Ugly hack, can't think of a better way right now...
+(define (load-rc-file)
+ "Load the RC file containing custom views and such."
+ ;; Ugly hack, can't thinkg of a better way right now, though...
(save-module-excursion
(lambda ()
(set-current-module (resolve-module '(undone main)))
- (load rc-file)))
+ (load rc-file))))
+
+(define (main args)
+ (when (and rc-file (file-exists? rc-file))
+ (load-rc-file))
(if (> (length args) 1)
(case (string->symbol (cadr args))