 
 (defun enh-browsing-open-note (widget &rest ignored)
   "Open a note in browsing mode"
-  (let ((guid (widget-value widget)))
-    (enh-base-open-note-common
-     (enh-get-note-attr guid))))
-
-
-(defun enh-browsing-create-page (type description &optional attr-list)
+  (enh-command-with-auth
+   (let ((guid (widget-value widget)))
+     (enh-base-open-note-common
+      (enh-get-note-attr guid)))))
+
+
+(defun enh-browsing-push-page (page)
+  "Push a new page to the browsing mode"
+  (enutil-push page evernote-browsing-page-list)
+  (setq evernote-browsing-current-page page)
+  (switch-to-buffer page)
+  (funcall enh-browsing-page-setup-func))
+
+
+(defun enh-browsing-create-page (type description &optional note-attrs)
   "Create a page structure of the attr-list"
-  (list
-   (cons 'type type)
-   (cons 'description description)
-   (cons 'guid-list
-         (mapcar
-          (lambda (attr)
-            (enutil-aget 'guid attr))
-          attr-list))))
-
-
-(defun enh-browsing-reflesh ()
-  "Refresh browsing buffer"
-  (let ((buf (get-buffer evernote-browsing-mode-buffer-name)))
-    (when buf
-      (save-excursion
-        (set-buffer buf)
-        (enh-browsing-redraw-current-page)))))
-
-
-(defun enh-browsing-redraw-current-page ()
-  "Draw browsing buffer"
-  (let (current-page type)
+  (let ((buf (generate-new-buffer (format "*ENB %s* " description))))
     (save-excursion
-      (if enh-browsing-current-page
-          (progn
-            (setq type (enutil-aget 'type enh-browsing-current-page))
-            (cond
-             ((eq type 'tag-list)
-              (enh-browsing-draw-tag-list-page))
-             ((eq type 'search-list)
-              (enh-browsing-draw-search-list-page))
-             ((eq type 'note-list)
-              (enh-browsing-draw-note-list-page))))
-        (enutil-erase-buffer-forcibly)))))
-
-
-(defun enh-browsing-draw-tag-list-page ()
+      (set-buffer buf)
+      (setq enh-browsing-page-type type
+            enh-browsing-page-description description
+            enh-browsing-page-data
+            (mapcar
+             (lambda (attr)
+               (enutil-aget 'guid attr))
+             note-attrs))
+      (cond
+       ((eq type 'tag-list)
+        (setq enh-browsing-page-setup-func
+              'enh-browsing-setup-tag-list-page))
+       ((eq type 'search-list)
+        (setq enh-browsing-page-setup-func
+              'enh-browsing-setup-search-list-page))
+       ((eq type 'note-list)
+        (setq enh-browsing-page-setup-func
+              'enh-browsing-setup-note-list-page)))
+      (evernote-browsing-mode))
+    buf))
+
+
+(defun enh-browsing-setup-tag-list-page ()
   "Create a page structure of the attr-list"
-  (erase-buffer)
-  (widget-insert (format "Tag List\n\ntotal %d\n" (hash-table-count enh-tag-info)))
+  (when enh-browsing-page-widget-root
+    (widget-delete enh-browsing-page-widget-title)
+    (widget-delete enh-browsing-page-widget-root)
+    (setq enh-browsing-page-widget-title nil)
+    (setq enh-browsing-page-widget-root nil))
   (let ((guid-children-hash (make-hash-table :test #'equal)))
     (maphash
      (lambda (guid attr)
        (let* ((parent (enutil-aget 'parent attr))
               (children (gethash parent guid-children-hash)))
-       (if children
-           (puthash parent (cons guid children) guid-children-hash)
-         (puthash parent (list guid) guid-children-hash))))
+         (if children
+             (puthash parent (cons guid children) guid-children-hash)
+           (puthash parent (list guid) guid-children-hash))))
      enh-tag-info)
-    (message "test")
-    (apply 'widget-create
-           (enh-browsing-get-tag-tree nil)))
+    (setq enh-browsing-page-widget-title
+          (widget-create 'item (format "Tag List\n\ntotal %d\n" (hash-table-count enh-tag-info))))
+    (setq enh-browsing-page-widget-root
+          (apply 'widget-create
+                 (enh-browsing-get-tag-tree nil))))
   (widget-setup))
 
 

(defun evernote-browser ()
  "Open an evernote browser"
  (interactive)
  (let ((buf (get-buffer evernote-browsing-mode-buffer-name)))
    (if buf
        (enutil-move-cursor-to-window buf)
      (switch-to-buffer (generate-new-buffer evernote-browsing-mode-buffer-name))
      (evernote-browsing-mode)
      (evernote-browsing-list-tags))))


(defun evernote-browsing-list-tags ()
  "List tags"
  (interactive)
  (when (eq major-mode 'evernote-browsing-mode)
    (enh-command-with-auth
     (enh-init-tag-info)
     (enh-browsing-push-page
      (enh-browsing-create-page 'tag-list "All Tags")))))


(defun evernote-browsing-list-searches ()
  "List saved searches"
  (interactive)
  (when (eq major-mode 'evernote-browsing-mode)
    (enh-command-with-auth
     (enh-init-search-info)
     (enh-browsing-push-page
      (enh-browsing-create-page 'search-list "All Saved Searches")))))


(defun evernote-browsing-search-notes ()
  "Search notes"
  (interactive)
  (when (eq major-mode 'evernote-browsing-mode)
    (let (note-attrs (query (read-string "Query:")))
      (enh-command-with-auth
       (setq note-attrs
             (enh-command-get-note-attrs-from-query query))
       (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)
    (let ((prev-page (enh-browsing-get-prev-page)))
      (if prev-page
          (progn
            (setq enh-browsing-current-page prev-page)
            (enh-browsing-redraw-current-page))
        (message "[No more previous page]")))))



;; Helper functions for evernote-browsing-mode (enh-browsing-xxx)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar enh-browsing-page-type nil)
(make-variable-buffer-local 'enh-browsing-page-type)

(defvar enh-browsing-page-description nil)
(make-variable-buffer-local 'enh-browsing-page-description)

(defvar enh-browsing-page-data nil)
(make-variable-buffer-local 'enh-browsing-page-data)

(defvar enh-browsing-page-widget-title nil)
(make-variable-buffer-local 'enh-browsing-page-widget-title)

(defvar enh-browsing-page-widget-root nil)
(make-variable-buffer-local 'enh-browsing-page-widget-root)

(defvar enh-browsing-page-setup-func nil)
(make-variable-buffer-local 'enh-browsing-page-setup-func)


(defun enh-browsing-open-tag (widget &rest ignored)
  "Open a tag in browsing mode"
  (let ((guid (widget-value widget)) note-attrs)
    (enh-command-with-auth
     (setq note-attrs
           (enh-command-get-note-attrs-from-tag-guids
            (if guid
                (list guid)
              nil))))
    (enh-set-note-attrs note-attrs)
    (enh-browsing-update-page-list)
    (enh-browsing-push-page
     (enh-browsing-create-page 'note-list
                               (format "Notes with tag %s"
