+;; for the menu i learned quite a lot from buff-menu.el
+;; (http://www.mit.edu/afs.new/athena/astaff/source/src-8.2/third/emacs/lisp/buff-menu.el)
+
 (require 'json)
+(require 'widget)
 
-(setq debug-on-error t)
+(setq path-seperator '"/")
+(setq settings-file-name '"settings.json")
 
+;; setup the keymap
+(setq rc-menu-mode-map (make-keymap))
+(suppress-keymap rc-menu-mode-map t)
+(define-key rc-menu-mode-map "q" 'rc-menu-quit)
+(define-key rc-menu-mode-map "\C-m" 'rc-menu-entry)
+
+
+(defun rc-menu-quit ()
+  "Quit the menu."
+  (interactive)
+  (let ((buffer (current-buffer)))
+    ;; Switch away from the buffer menu and bury it.
+    (switch-to-buffer (other-buffer))
+    (bury-buffer buffer)))
+
+
+(defun rc-menu-entry ()
+  "Select this line's buffer in this window."
+  (interactive)
+  (message (current-menu-entry)))
+
+
+(defun current-menu-entry ()
+  "Return buffer described by this line of buffer menu."
+(buffer-substring (point-at-bol) (point-at-eol)))
+
+;; simply prints a list
 (defun print-elements-of-list (list)
   "Print each element of LIST on a line of its own."
   (if list


  (dolist (new-file new-files)
    (message (concat "path: " dirname ", file:" new-file)))
;    (add-to-list 'all-files-and-dirs (concat (file-name-as-directory dirname) new-file)))
;    (add-to-list 'all-files-and-dirs (concat 
    (add-to-list 'all-files-and-dirs `((dir . ,dirname) (file . ,new-files)))

;(message "stored everything")


;    (message all-files-and-dirs)
  all-files-and-dirs))





(setq dir-project-root (find-next-settings-file))
(setq dir-inbox (concat dir-project-root "inbox/"))
;(print-elements-of-list (find-files-and-directories dir-inbox))
(setq dirs-and-files (find-files-and-directories dir-inbox))

;(print-elements-of-list (cdr (car dirs-and-files)))






    (setq list (cdr list)))
  (message "the list is empty.")))


;; function to find the path to the next (upwards in the directory tree)
;; settings file.
(defun find-next-settings-file ()
  (setq cur-dir (buffer-file-name))

  (setq idx-sep  (length cur-dir))
  (setq b-stop-looping nil)
  (while (and (> idx-sep 0) (not b-stop-looping))
    ;; find the last path seperator in the path string
    (setq idx-sep 0)
    (setq read-pos 0)
    
    (while (< read-pos (- (length cur-dir) 1))
      (if (string= (substring cur-dir read-pos (+ read-pos 1)) path-seperator)
	  (setq idx-sep read-pos))

      (setq read-pos (1+ read-pos)))

    (setq cur-pos idx-sep)
    (setq cur-dir (substring cur-dir 0 (1+ idx-sep)))
    (setq candidate-path (concat cur-dir settings-file-name))
    (when (file-exists-p candidate-path)
      (setq b-stop-looping t)))

  (if b-stop-looping cur-dir nil))


(defun read-settings-file (filepath)
  (setq settings (json-read-file filepath)))

(defun print-alist (list)
  (while list
    (setq cur-element (car list))
    (setq cur-key (car cur-element))
    (setq cur-value (cdr cur-element))
    (message (format "%10s: %s" cur-key cur-value))
    (setq list (cdr list))))


(defun find-all-inbox-entries ()
  ;; get the project directory
  (setq dir-project-root (find-next-settings-file))
  (setq dir-inbox (concat dir-project-root "inbox/"))
  (message dir-inbox)

  ;; traverse through the subdirectories
  (message (directory-files-and-attributes dir-inbox)))


(defun find-files-and-directories (dirname)
  (let (cur-files '() all-files-and-dirs '())
