 
 (defun evernote-get-command-result ()
   "Get the result of the result of the lately issued command as a string."
-  (let ((outbuf (get-buffer-create evernote-client-output-buffer-name)))
+  (let ((outbuf (get-buffer-create evernote-command-output-buffer-name)))
     (save-excursion
       (set-buffer outbuf)
       (set-buffer-file-coding-system 'utf-8)


(defun evernote-issue-command (inbuf &rest args)
  "Invoke external process to issue an evernote command."
  (let ((outbuf (get-buffer-create evernote-client-output-buffer-name))
        (infile nil)
        (coding-system-for-read 'utf-8)
        (coding-system-for-write 'utf-8))

(defun evernote-open-note ()
  "Open a note"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (evernote-open-note-common
      (evernote-command-get-note-list-from-tags
       (evernote-completing-read-multiple
        "Tags used for search (comma separated form. default search all tags):"
        (evernote-get-tag-alist
         (evernote-command-get-tag-list))
        nil t))))))


(defun evernote-search-notes ()
  "Search notes with query and open a note among them"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (evernote-open-note-common
      (evernote-command-get-note-list-from-query
       (read-string "Query:"))))))


(defun evernote-do-saved-search ()
  "Do a saved search and open a note"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (evernote-open-note-common
      (evernote-command-get-note-list-from-query
       (let ((search-alist
              (evernote-get-search-alist (evernote-command-get-search-list))))
         (evernote-assoc-cdr
          'query
          (evernote-assoc-cdr
           (completing-read
            "Saved search:"
            search-alist
            nil t)
           search-alist))))))))


(defun evernote-open-note-common (note-command-output)
  "Common procedure of opening a note"
  (let (note-attr note-guid note-name note-edit-mode note-tags opened-buf)
    (setq note-attr (evernote-read-note-attr note-command-output))
    (setq note-guid (evernote-assoc-cdr 'guid note-attr)
          note-name (evernote-assoc-cdr 'name note-attr)
          note-edit-mode (evernote-assoc-cdr 'edit-mode note-attr)
          note-tags (evernote-assoc-cdr 'tags note-attr))
    (setq opened-buf (evernote-find-opened-buffer note-guid))
    (if opened-buf
        (evernote-move-cursor-to-window opened-buf)
      (progn
        (evernote-create-note-buffer
         note-guid
         note-name
         note-edit-mode
         note-tags
         (evernote-command-get-note-content note-guid note-edit-mode))))))


(defun evernote-save-note ()
  "Save a note"
  (interactive)
  (if (and evernote-note-guid (buffer-modified-p))
      (evernote-command-with-auth
       (lambda ()
         (progn
           (evernote-command-update-note
            (current-buffer)
            evernote-note-guid
            evernote-note-name
            evernote-note-tags
            evernote-note-edit-mode)
           (set-buffer-modified-p nil))))
    (message "(No changes need to be saved)")))


(defun evernote-create-note ()
  "Create a note"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (let (tags name edit-mode)
       (setq tags
             (evernote-completing-read-multiple
              "Attached Tags (comma separated form):"
              (evernote-get-tag-alist
               (evernote-command-get-tag-list))))
       (setq name (read-string "Note name:"))
       (setq edit-mode "TEXT")
       (switch-to-buffer (generate-new-buffer name))
       (evernote-command-create-note (current-buffer)
                                     name
                                     tags
                                     edit-mode)
       (setq evernote-note-guid (evernote-eval-command-result)
             evernote-note-name name
             evernote-note-tags tags
             evernote-note-edit-mode edit-mode)
       (evernote-mode)
       (evernote-update-mode-line)
       (set-buffer-modified-p nil)))))


(defun evernote-write-note ()
  "Write buffer to a note"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (let (tags name edit-mode)
       (setq tags
             (evernote-completing-read-multiple
              "Attached Tags (comma separated form):"
              (evernote-get-tag-alist
               (evernote-command-get-tag-list))))
       (setq name (read-string "Note name:" (buffer-name)))
       (setq edit-mode (evernote-read-edit-mode "TEXT"))
       (evernote-command-create-note (current-buffer)
                                     name
                                     tags
                                     edit-mode)
       (setq evernote-note-guid (evernote-eval-command-result)
             evernote-note-name name
             evernote-note-tags tags
             evernote-note-edit-mode edit-mode)
       (if (not evernote-mode)
           (evernote-mode))
       (rename-buffer name t)
       (evernote-update-mode-line)
       (set-buffer-modified-p nil)))))


(defun evernote-edit-tags ()
  "Add or remove tags from/to the note"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (if evernote-mode
         (progn
           (setq evernote-note-tags
                 (evernote-completing-read-multiple
                  "Change attached Tags (comma separated form):"
                  (evernote-get-tag-alist (evernote-command-get-tag-list))
                  nil nil (mapconcat #'identity evernote-note-tags ",")))
           (evernote-update-mode-line)
           (set-buffer-modified-p t))))))


(defun evernote-change-edit-mode ()
  "Change edit mode of the note"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (if evernote-mode
         (progn
           (setq evernote-note-edit-mode
                 (evernote-read-edit-mode evernote-note-edit-mode))
           (evernote-update-mode-line)
           (set-buffer-modified-p t))))))


(defun evernote-rename-note ()
  "Rename a note"
  (interactive)
  (if evernote-mode
      (progn
        (setq evernote-note-name
              (read-string "New note name:" evernote-note-name))
        (rename-buffer evernote-note-name t)
        (set-buffer-modified-p t))))


(defun evernote-delete-note ()
  "Delete a note"
  (interactive)
  (if (and evernote-mode
           (y-or-n-p "Do you really want to remove this note? "))
      (evernote-command-with-auth
       (lambda ()
         (evernote-command-delete-note evernote-note-guid)
         (kill-buffer (current-buffer))))))


(defun evernote-create-search ()
  "Create a saved search"
  (interactive)
  (evernote-command-with-auth
   (lambda ()
     (evernote-command-create-search
      (read-string "Saved Search Name:")
      (read-string "Query:")))))


;;
