Add id to each item

* undone/main.scm (get-field-value): Allow specification of default
  return if no key is found.
  (next-id): New function.
  (add): Add `id' to the todo item.
This commit is contained in:
Tom Willemsen 2012-09-10 21:56:16 +02:00
parent 62cde9b1c8
commit 271b33f910

View file

@ -12,8 +12,13 @@
(define-syntax get-field-value (define-syntax get-field-value
(lambda (x) (lambda (x)
(syntax-case x () (syntax-case x ()
((_ key alist default)
#'(let ((field (get-field key alist)))
(if field
(cdr field)
default)))
((_ key alist) ((_ key alist)
#'(cdr (get-field key alist)))))) #'(get-field-value key alist #f)))))
(define-syntax define-view (define-syntax define-view
(lambda (x) (lambda (x)
@ -126,6 +131,7 @@
(mkdir path))) (mkdir path)))
(define (save) (define (save)
"Save the list." "Save the list."
(mkdirs (dirname todo-list-file)) (mkdirs (dirname todo-list-file))
@ -133,13 +139,20 @@
(write todo-list port) (write todo-list port)
(close-port port))) (close-port port)))
(define (next-id)
"Look through all known todo items and get the next id."
(1+ (apply max (map (lambda (elm)
(get-field-value id elm 0))
todo-list))))
(define (add args) (define (add args)
"Add item to the list." "Add item to the list."
(set! todo-list (set! todo-list
(append todo-list (append todo-list
(list (list
(append (append
`((content . ,(string-trim-right (read-delimited "")))) `((id . ,(next-id))
(content . ,(string-trim-right (read-delimited ""))))
(map (lambda (arg) (map (lambda (arg)
(let* ((pair (string-split arg #\=)) (let* ((pair (string-split arg #\=))
(var (string->symbol (car pair))) (var (string->symbol (car pair)))