           (throw 'mode default-major-mode)))))
 
 
-;;(defun enh-base-all-reflesh ()
-;;  (mapc
-;;   (lambda (buf)
-;;     (save-excursion
-;;       (set-buffer buf)
-;;       (if evernote-mode
-;;           (enh-base-reflesh))))
-;;   (buffer-list)))
-
-
-;;(defun enh-base-reflesh ()
-;;  (if evernote-mode
-;;      (let (note-attr name tag-names edit-mode)
-;;        (setq note-attr (enh-get-note-attr evernote-note-guid))
-;;        (setq name (enutil-aget 'name note-attr))
-;;        (setq tag-names (evernote-tag-guids-to-comma-separated-names
-;;                         (enutil-aget 'tag-list note-attr)))
-;;        (setq edit-mode (enutil-aget 'edit-mode note-attr))
-;;        (rename-buffer name t)
-;;        (enh-base-update-mode-line name tag-names edit-mode))))
-
-
 (defun enh-base-find-opened-buffer (guid)
   "Find a buffer associated with guid"
   (let ((found_buf nil))

                    ,guid))))


(defun enh-browsing-test-open-tag (widget &rest ignored)
  (message (widget-value widget)))


(defun enh-browsing-draw-search-list-page ()
  "Insert saved search list into the browsing buffer"
  (erase-buffer)
  (widget-insert
   (format "%s\n\ntotal %d\n%-30s   %s\n\n"
           (enutil-aget 'description enh-browsing-current-page)
           (hash-table-count enh-search-info)
           "Name"
           "Query"))
  (maphash
   (lambda (guid attr)
     (let ((attr (enh-get-search-attr guid)))
       (widget-create 'push-button
                      :tag (format "%-30s   %s"
                                   (enutil-aget 'name attr)
                                   (enutil-aget 'query attr))
                      :format "%[%t%]\n"
                      :notify enh-browsing-open-search
                      guid)))
   enh-search-info)
  (widget-setup))


(defun enh-browsing-draw-note-list-page ()
  "Insert note list into the browsing buffer"
  (erase-buffer)
  (let ((guid-list (enutil-aget 'guid-list enh-browsing-current-page))
        (note-attrs (make-hash-table :test #'equal)))
    (mapc
     (lambda (guid)
       (puthash guid (gethash guid enh-note-info) note-attrs))
     guid-list)
    (widget-insert
     (format "%s\n\ntotal %d\n%-30s   %-15s   %s\n\n"
             (enutil-aget 'description enh-browsing-current-page)
             (hash-table-count note-attrs)
             "Last Modified"
             "Tags"
             "Name"))
    (maphash
     (lambda (guid attr)
       (widget-create 'push-button
                      :tag (format "%-30s   %-15s   %s"
                                   (enutil-aget 'updated attr)
                                   (enh-tag-guids-to-comma-separated-names
                                    (enutil-aget 'tags attr)
                                    15)
                                   (enutil-aget 'name attr))
                      :format "%[%t%]\n"
                      :notify enh-browsing-open-note
                      guid))
     note-attrs))
  (widget-setup))


(defun enh-browsing-push-page (page)
  "Push a new page to the browsing mode"
  (let ((type (enutil-aget 'type page)))
    (if (or (eq type 'tag-list) (eq type 'search-list))
        (let (same-type-page)
          (setq same-type-page
                (enh-browsing-get-page-of-type type))
          (if same-type-page
              (evernote-browsing-delete-page same-type-page)))))
  (enutil-push page enh-browsing-page-list)
  (setq enh-browsing-current-page page)
  (enh-browsing-redraw-current-page))


(defun enh-browsing-get-page-of-type(type)
  "Get a page of the type in the browsing mode"
  (catch 'result
    (mapc
     (lambda (page)
       (if (eq (enutil-aget 'type page) type)
           (throw 'result page)))
     enh-browsing-page-list)
    nil))


(defun enh-browsing-get-current-page-type ()
  (enutil-aget
   'type
   enh-browsing-current-page))


(defun enh-browsing-get-current-page-guids ()
  (enutil-aget
   'guids
   enh-browsing-current-page))


(defun enh-browsing-get-next-page ()
  (if (enutil-neq enh-browsing-current-page (car enh-browsing-page-list))
      (catch 'next-page
        (let (next-page)
          (mapc
           (lambda (page)
             (when (eq page enh-browsing-current-page)
               (throw 'next-page next-page))
             (setq next-page page))
           enh-browsing-page-list)))))


(defun enh-browsing-get-prev-page ()
  (cadr
   (memq enh-browsing-current-page
         enh-browsing-page-list)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun evernote-browser ()
  "Open an evernote browser"
  (interactive)
  (enh-browsing-update-page-list)
  (if evernote-browsing-current-page
      (enutil-move-cursor-to-window evernote-browsing-current-page)
    (evernote-browsing-list-tags)))


(defun evernote-browsing-list-tags ()
  "List tags"
  (interactive)
  (enh-command-with-auth (enh-init-tag-info))
  (enh-browsing-update-page-list)
  (let ((page (enh-browsing-get-page-of-type 'tag-list)))
    (if page
        (progn
          (setq evernote-browsing-current-page page)
          (switch-to-buffer page))
      (enh-browsing-push-page
       (enh-browsing-create-page 'tag-list "All Tags")))))


(defun evernote-browsing-list-searches ()
  "List saved searches"
  (interactive)
  (enh-command-with-auth (enh-init-search-info))
  (enh-browsing-update-page-list)
  (let ((page (enh-browsing-get-page-of-type 'search-list)))
    (if page
        (progn
          (setq evernote-browsing-current-page page)
          (switch-to-buffer page))
      (enh-browsing-push-page
       (enh-browsing-create-page 'search-list "All Saved Searches")))))


(defun evernote-browsing-search-notes ()
  "Search notes"
  (interactive)
  (let (note-attrs (query (read-string "Query:")))
    (enh-command-with-auth
     (setq note-attrs
           (enh-command-get-note-attrs-from-query query)))
    (enh-browsing-update-page-list)
    (enh-set-note-attrs note-attrs)
    (enh-browsing-push-page
     (enh-browsing-create-page 'note-list
                               (format "Query Result of: %s" query)
                               note-attrs))))


(defun evernote-browsing-prev-page ()
  "Move to the prev page"
  (interactive)
  (when (eq major-mode 'evernote-browsing-mode)
    (enh-browsing-update-page-list)
    (let ((prev-page (enh-browsing-get-prev-page)))
      (if prev-page
          (progn
            (setq evernote-browsing-current-page prev-page)
            (switch-to-buffer prev-page))
        (message "[No more previous page]")))))


