 
 (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))
 
 

          (enh-command-get-note-attrs-from-tag-guids
           (enh-read-tag-guids
            "Tags used for search (comma separated form. default search all tags):"))))
     (enh-update-note-attrs-external note-attrs)
     (enh-base-open-note-common (enh-base-read-note-attr note-attrs)))))



                    ,guid))))


(defun enh-browsing-setup-search-list-page ()
  "Insert saved search list into the browsing buffer"
  (when enh-browsing-page-widget-root
    (widget-delete enh-browsing-page-widget-root)
    (setq enh-browsing-page-widget-root nil))
  (let ((search-list nil))
    (maphash
     (lambda (guid attr)
       (let ((attr (enh-get-search-attr guid)))
         (enutil-push `(push-button
                        :tag ,(format "%-30s   %s"
                                      (enutil-aget 'name attr)
                                      (enutil-aget 'query attr))
                        :format "%[%t%]\n"
                        :notify enh-browsing-open-search
                        ,guid)
                      search-list)))
     enh-search-info)
    (setq enh-browsing-page-widget-root
          (apply 'widget-create
                 `(group
                   (item ,(format "%s\n\ntotal %d\n%-30s   %s\n\n"
                                  enh-browsing-page-description
                                  (hash-table-count enh-search-info)
                                  "Name"
                                  "Query"))
                   ,@(nreverse search-list)))))
  (widget-setup))


(defun enh-browsing-setup-note-list-page ()
  "Insert note list into the browsing buffer"
  (when enh-browsing-page-widget-root
    (widget-delete enh-browsing-page-widget-root)
    (setq enh-browsing-page-widget-root nil))
  (let ((guid-list enh-browsing-page-data)
        (note-attrs (make-hash-table :test #'equal))
        (note-list nil))
    (mapc
     (lambda (guid)
       (puthash guid (gethash guid enh-note-info) note-attrs))
     guid-list)
    (maphash
     (lambda (guid attr)
       (enutil-push `(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-list))
     note-attrs)
    (setq enh-browsing-page-widget-root
          (apply 'widget-create
                 `(group
                   (item ,(format "%s\n\ntotal %d\n%-30s   %-15s   %s\n\n"
                                  enh-browsing-page-description
                                  (hash-table-count note-attrs)
                                  "Last Modified"
                                  "Tags"
                                  "Name"))
                   ,@(nreverse note-list)))))
  (widget-setup))


(defun enh-browsing-update-page-list ()
  (setq evernote-browsing-page-list
        (delete nil
                (mapcar (lambda (page)
                          (if (buffer-live-p page) page nil))
                        evernote-browsing-page-list)))
  (unless (memq evernote-browsing-current-page evernote-browsing-page-list)
    (setq evernote-browsing-current-page (car evernote-browsing-page-list))))


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


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


(defun enh-browsing-get-page-of-type (type)
  "Get a page of the type in the browsing mode"
  (let ((result nil))
    (save-excursion
      (mapc
       (lambda (page)
         (set-buffer page)
         (when (eq enh-browsing-page-type type)
           (setq result page)))
       evernote-browsing-page-list))
    result))


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