 
 (defvar evernote-mode-map (make-sparse-keymap)
   "Keymap used in evernote mode.")
-
 (define-key evernote-mode-map "\C-x\C-s" 'evernote-save-note)
 (define-key evernote-mode-map "\C-cet"   'evernote-edit-tags)
 (define-key evernote-mode-map "\C-cee"   'evernote-change-edit-mode)
 (define-key evernote-mode-map "\C-cer"   'evernote-rename-note)
 (define-key evernote-mode-map "\C-ced"   'evernote-delete-note)
 
-(defvar evernote-minibuffer-read-note-name-map
+(defvar evernote-read-note-map
   (copy-keymap minibuffer-local-completion-map))
-
-(define-key evernote-minibuffer-read-note-name-map [tab] 'evernote-minibuffer-note-completion)
-(define-key evernote-minibuffer-read-note-name-map "\C-i" 'evernote-minibuffer-note-completion)
-(define-key evernote-minibuffer-read-note-name-map "\C-m" 'evernote-minibuffer-note-input-finish)
+(define-key evernote-read-note-map [tab] 'evernote-read-note-completion)
+(define-key evernote-read-note-map "\C-i" 'evernote-read-note-completion)
+(define-key evernote-read-note-map "\C-m" 'evernote-read-note-finish)
+(define-key evernote-read-note-map  " "   'self-insert-command)
 
 
 (defvar evernote-search-mode-map (copy-keymap global-map)
   "Keymap used in evernote search mode.")
 (define-key evernote-search-mode-map "\C-m" 'evernote-select-note-in-search-mode)
 
-(defvar evernote-search-mode-displayed-name-and-name-map nil
-  "Formatted name list variable used only in evernote-search-mode buffer")
-(make-variable-buffer-local 'evernote-search-mode-displayed-name-and-name-map)
-
-
-;;(defvar evernote-browsing-mode-map (copy-keymap global-map)
-;;  "Keymap used in evernote browsing mode.")
-;;(define-key evernote-browsing-mode-map "l" 'evernote-command-login)
-;;(define-key evernote-browsing-mode-map "t" 'evernote-command-listtags)
-;;(define-key evernote-browsing-mode-map "\C-m" 'evernote-open-guid)
-
-(defconst evernote-client-buffer-name "*Evernote*")
-(defconst evernote-client-output-buffer-name "*Evernote-Client-Output*")
-
+(defvar evernote-search-mode-formatted-name-displayed-name-alist nil
+  "Alist from formatted names to names used only in evernote-search-mode buffer")
+(make-variable-buffer-local 'evernote-search-mode-formatted-name-displayed-name-alist)
+
+
+(defconst evernote-command-output-buffer-name "*Evernote-Client-Output*")
 (defconst evernote-error-ok                  0)
 (defconst evernote-error-fail                100)
 (defconst evernote-error-parse               101)

      (pop-to-buffer buf))))


(defun evernote-string-to-oct (string)
  "Convert the string into quoted backslashed octal edit mode."
  (concat

      (message (evernote-error-message error-code)))))


(defun evernote-get-tag-alist (tag-command-out)
  "Get the alist for completion from command output"
  (let (acc collect-tagname)
    (setq collect-tagname
          (lambda (node)
            (setq acc
                  (cons
                   (list (evernote-assoc-cdr 'name node))
                   acc))
            (mapc collect-tagname (evernote-assoc-cdr 'children node))))
    (mapcar collect-tagname tag-command-out)
    (nreverse acc)))


(defun evernote-get-search-alist (search-command-out)
  "Get the alist for completion from command output"
  (mapcar
   (lambda (elem)
     (cons
      (evernote-assoc-cdr 'name elem)
      elem))
   search-command-out))


(defun evernote-read-note-attr (note-command-out)
  "Prompts a note name and returns a note attribute"
  (let ((name-num-hash (make-hash-table :test #'equal))
        evernote-note-displayed-name-attr-alist ; used in evernote-search-mode
        evenote-note-displayed-name-formatted-name-alist) ; used in evernote-search-mode
    (mapc
     (lambda (attr)
       (let (name displayed-name)
         (setq name (evernote-assoc-cdr 'name attr))
         (setq displayed-name
               (evernote-get-displayed-note-name name name-num-hash))
         (setq evernote-note-displayed-name-attr-alist
               (cons (cons displayed-name attr)
                     evernote-note-displayed-name-attr-alist))
         (setq evenote-note-displayed-name-formatted-name-alist
               (cons (cons displayed-name
                           (format "%s    %s"
                                   (cdr (assoc 'updated attr))
                                   displayed-name))
                     evenote-note-displayed-name-formatted-name-alist))))
     note-command-out)
    (setq evernote-note-displayed-name-attr-alist
          (nreverse evernote-note-displayed-name-attr-alist))
    (setq evenote-note-displayed-name-formatted-name-alist
          (nreverse evenote-note-displayed-name-formatted-name-alist))
    (evernote-assoc-cdr (read-from-minibuffer "Note:"
                                              nil evernote-read-note-map)
                        evernote-note-displayed-name-attr-alist)))


(defun evernote-get-displayed-note-name (name name-hash)
  "Get displayed note name from the read note name"
  (let ((num (gethash name name-num-hash))
        result)
    (if num
        (progn
          (setq num (+ num 1))
          (setq result (format "%s(%d)" name num))
          (puthash name num name-num-hash))
      (setq result (substring name 0))
      (puthash name 1 name-num-hash))
    result))


(defun evernote-read-note-completion ()
  "Complete note name and display completion list"
  (interactive)
  (let (word result start)
    (setq word (evernote-get-minibuffer-string))
    (setq result (try-completion word evernote-note-displayed-name-attr-alist))
    ;(evernote-minibuffer-tmp-message (concat "[" word "]"))
    (cond
     ((eq result t)
      (evernote-minibuffer-tmp-message "[Sole Completion]"))
     ((eq result nil)
      (ding)
      (evernote-minibuffer-tmp-message "[No Match]"))
     ((string= result word)
      (let (formatted-name-displayed-name-alist completion-buf)
        (setq formatted-name-displayed-name-alist
              (mapcar (lambda (displayed-name)
                        (cons
                         (evernote-assoc-cdr
                          displayed-name
                          evenote-note-displayed-name-formatted-name-alist)
                         displayed-name))
                      (all-completions
                       word
                       evenote-note-displayed-name-formatted-name-alist)))
        (setq completion-buf (get-buffer-create "*Evernote-Completions*"))
        (save-excursion
          (set-buffer completion-buf)
          (evernote-display-note-completion-list
           formatted-name-displayed-name-alist)
          (setq evernote-search-mode-formatted-name-displayed-name-alist
                formatted-name-displayed-name-alist)
          (evernote-search-mode))
        (display-buffer completion-buf)))
     (t (evernote-set-minibuffer-string result)
        (end-of-buffer)
        (if (eq t
                (try-completion result
                                evernote-note-displayed-name-attr-alist))
            nil
          (evernote-minibuffer-tmp-message "[Complete, but not unique]"))))))


(defun evernote-read-note-finish ()
  "Finish input note name"
  (interactive)
  (if (assoc
       (evernote-get-minibuffer-string)
       evernote-note-displayed-name-attr-alist)
      (exit-minibuffer)
    (evernote-minibuffer-tmp-message "[No Match]")))


(defun evernote-display-note-completion-list (formatted-name-displayed-name-alist)
  "Display formatted note names on this buffer"
  (setq buffer-read-only nil)
  (erase-buffer)
  (mapc (lambda (elem)
          (insert (car elem) "\n"))
        formatted-name-displayed-name-alist)
  (setq buffer-read-only t))


(defun evernote-select-note-in-search-mode ()
  "Select a note name on this buffer and input it into the mini buffer"
  (interactive)
  (save-excursion
    (let (displayed-name)
      (setq displayed-name
            (evernote-assoc-cdr
             (evernote-get-current-line-string)
             evernote-search-mode-formatted-name-displayed-name-alist))
      (kill-buffer (current-buffer))
      (evernote-set-minibuffer-string displayed-name)
      (exit-minibuffer))))


(defun evernote-find-opened-buffer (guid)
