   serialize: ->
     @history?.serialize()
 
-  handleCursorMoved: ({oldBufferPosition, newBufferPosition, cursor}) ->
-    unless @cursorMoveTracking
-      @debug " -- Tracking Skip in CursorMoved"
-      @cursorMoveTracking = true
+  handlePaneChanged: (item) ->
+    return unless item instanceof TextEditor
+    return unless item.getURI()
+
+    # We need to track former active pane to know cursor position when active pane was changed.
+    unless @lastEditor
+      @lastEditor = item
       return
 
+    @debug "# PaneItem Changed: dir = #{@direction}"
+    @debug " From: #{@inspectEditor(@lastEditor)}"
+    @debug " To:   #{@inspectEditor(item)}"
+
+    URI   = @lastEditor.getURI()
+    point = @lastEditor.getCursorBufferPosition()
+    @saveHistory @lastEditor, point, URI, => item.getURI() isnt URI
+    @lastEditor = item
+
+  handleCursorMoved: ({oldBufferPosition, newBufferPosition, cursor}) ->
     editor = cursor.editor
     return if editor.hasMultipleCursors()
 
-    args = [editor, oldBufferPosition, editor.getURI()]
+    # [FIXME] currently buffer without URI is simply ignored.
+    # is there any way to support 'untitled' buffer?
+    return unless URI = editor.getURI()
+
+    @debug "CursorMoved #{oldBufferPosition} -> #{newBufferPosition} #{path.basename(URI)}"
+
+    if @lastEditor.getURI() isnt URI
+      # Pane was changed, its handled by observeActivePaneItem()
+      # And here we should return to avoid duplicate save.
+      @debug "JUST MOVED return on cursorMoved"
+      return
+
+    fn = => @needRemember(oldBufferPosition, newBufferPosition, cursor)
+    @saveHistory editor, oldBufferPosition, URI, fn
+
+  saveHistory: (editor, oldBufferPosition, URI, fn) ->
+    args = [editor, oldBufferPosition, URI]
     if @direction
       if @direction is 'prev' and @history.isNewest()
         @history.pushToHead args...
-
       @direction = null
-
-    else if @needRemember.bind(@)(oldBufferPosition, newBufferPosition, cursor)
+    else
+      return unless fn()
       @history.add args...
 
   needRemember: (oldBufferPosition, newBufferPosition, cursor) ->
-    # [FIXME] currently buffer without URI is simply ignored.
-    # is there any way to support 'untitled' buffer?
-    return false unless cursor.editor.getURI()
-
     # Line number delata exceeds or not.
     if Math.abs(oldBufferPosition.row - newBufferPosition.row) > @rowDeltaToRemember
       return true
-    return false
+    else
+      return false
 
   next:  -> @jump 'next'
   prev:  -> @jump 'prev'


  activate: (state) ->
    @subscriptions = new CompositeDisposable
    @history = new CursorHistory atom.config.get('cursor-history.max')
    @cursorMoveTracking = true

    @rowDeltaToRemember = atom.config.get 'cursor-history.rowDeltaToRemember'
    @subscriptions.add atom.config.onDidChange 'cursor-history.rowDeltaToRemember', ({newValue}) =>

{CompositeDisposable, TextEditor} = require 'atom'
History = require './history'
path = require 'path'

module.exports =
