From cae98ebdfbf9a1a852a810dc7d43a9717f7c7a9a Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Sun, 16 Sep 2012 14:09:56 +0200 Subject: Make file loading a bit more robust Don't fail when a file can't be found. --- undone/main.scm | 72 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 37 deletions(-) (limited to 'undone') 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)) -- cgit v1.2.3-54-g00ecf