anchor.diff:  handle_key(#wxKey{type = char, keyCode = ?WXK_F1}, State) ->
   	ee_document_sup:open_document("readme"),
   	State;
  -handle_key(#wxKey{type = char, keyCode = ?WXK_LEFT}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_left(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_RIGHT}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_right(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_UP}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_up(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_DOWN}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_down(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_HOME}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_to_beginning_of_line(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_END}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_to_end_of_line(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_PAGEUP}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_up_one_page(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_PAGEDOWN}, #state{win = #main_window{window = Window}, buffer = Buffer, caret = Caret} = State) ->
  -	wxFrame:refresh(Window),
  -	State#state{caret = ee_caret:move_down_one_page(Caret, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_RETURN}, #state{buffer = Buffer, caret = #ee_caret{line_no = LineNo} = Caret, documents = [Document]} = State) ->
  -	ee_buffer_server:insert_eol(Document, ee_caret:caret_to_buffer_coords(Caret, Buffer)),
  +handle_key(#wxKey{type = char, keyCode = ?WXK_LEFT}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_left_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_RIGHT}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_right_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_UP}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_up_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_DOWN}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_down_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_HOME}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_to_line_start_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_END}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_to_line_end_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_PAGEUP}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_up_one_page_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_PAGEDOWN}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  +	{ok, DocSet1} = move_caret_down_one_page_in_focus_doc(DocSet0),
  +	wxFrame:refresh(Window),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_RETURN}, #state{doc_set = DocSet0} = State) ->
  +	%% TODO: Refactor so that doc_set internals are hidden.
  +	#doc_set{focus_doc = #doc_view{pid = DocPid, buffer = Buffer, caret = #ee_caret{line_no = LineNo} = Caret0} = FocusDoc} = DocSet0,
  +	ee_buffer_server:insert_eol(DocPid, ee_caret:caret_to_buffer_coords(Caret0, Buffer)),
   	%% TODO: Avoid changing caret directly. Waiting for #29.
  -	State#state{caret = Caret#ee_caret{line_no = LineNo + 1, col_no = 1}};
  -handle_key(#wxKey{type = char, keyCode = ?WXK_BACK}, #state{buffer = Buffer, caret = Caret, documents = [Document]} = State) ->
  -	ee_buffer_server:remove_left(Document, ee_caret:caret_to_buffer_coords(Caret, Buffer)),
  -	State#state{caret = ee_caret:move_left(Caret, Buffer)};	
  -handle_key(#wxKey{type = char, keyCode = ?WXK_DELETE}, #state{buffer = Buffer, caret = Caret, documents = [Document]} = State) ->
  -	ee_buffer_server:remove_right(Document, ee_caret:caret_to_buffer_coords(Caret, Buffer)),
  +	Caret1 = Caret0#ee_caret{line_no = LineNo + 1, col_no = 1},
  +	DocSet1 = DocSet0#doc_set{focus_doc = FocusDoc#doc_view{caret = Caret1}},
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_BACK}, #state{doc_set = DocSet0} = State) ->
  +	%% TODO: Refactor so that doc_set internals are hidden.
  +	#doc_set{focus_doc = #doc_view{pid = DocPid, buffer = Buffer, caret = Caret}} = DocSet0,
  +	ee_buffer_server:remove_left(DocPid, ee_caret:caret_to_buffer_coords(Caret, Buffer)),
  +	{ok, DocSet1} = move_caret_left_in_focus_doc(DocSet0),
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_DELETE}, #state{doc_set = DocSet} = State) ->
  +	%% TODO: Refactor so that doc_set internals are hidden.
  +	#doc_set{focus_doc = #doc_view{pid = DocPid, buffer = Buffer, caret = Caret}} = DocSet,
  +	ee_buffer_server:remove_right(DocPid, ee_caret:caret_to_buffer_coords(Caret, Buffer)),
   	State;	
  -handle_key(#wxKey{type = char, keyCode = ?WXK_TAB}, #state{buffer = Buffer, caret = #ee_caret{col_no = ColNo} = Caret, documents = [Document]} = State) ->
  -	ee_buffer_server:insert_text(Document, [?WXK_TAB], ee_caret:caret_to_buffer_coords(Caret, Buffer)),
  -	State#state{caret = ee_caret:move_to(Caret#ee_caret{col_no = ColNo + 4}, Buffer)};
  -handle_key(#wxKey{type = char, keyCode = KeyCode}, #state{buffer = Buffer, caret = #ee_caret{col_no = ColNo} = Caret, documents = [Document]} = State) when KeyCode > 31, KeyCode < 256 ->
  -	ee_buffer_server:insert_text(Document, [KeyCode], ee_caret:caret_to_buffer_coords(Caret, Buffer)),
  -	State#state{caret = ee_caret:move_to(Caret#ee_caret{col_no = ColNo + 1}, Buffer)};
  +handle_key(#wxKey{type = char, keyCode = ?WXK_TAB}, #state{doc_set = DocSet0} = State) ->
  +	%% TODO: Refactor so that doc_set internals are hidden.
  +	#doc_set{focus_doc = #doc_view{pid = DocPid, buffer = Buffer, caret = #ee_caret{col_no = ColNo} = Caret0} = FocusDoc} = DocSet0,
  +	ee_buffer_server:insert_text(DocPid, [?WXK_TAB], ee_caret:caret_to_buffer_coords(Caret0, Buffer)),
  +	%% TODO: Simplify this caret movement.
  +	Caret1 = ee_caret:move_to(Caret0#ee_caret{col_no = ColNo + 4}, Buffer),
  +	DocSet1 = DocSet0#doc_set{focus_doc = FocusDoc#doc_view{caret = Caret1}},
  +	State#state{doc_set = DocSet1};
  +handle_key(#wxKey{type = char, keyCode = KeyCode}, #state{doc_set = DocSet0} = State) when KeyCode > 31, KeyCode < 256 ->
  +	%% TODO: Refactor so that doc_set internals are hidden.
  +	#doc_set{focus_doc = #doc_view{pid = DocPid, buffer = Buffer, caret = #ee_caret{col_no = ColNo} = Caret0} = FocusDoc} = DocSet0,
  +	ee_buffer_server:insert_text(DocPid, [KeyCode], ee_caret:caret_to_buffer_coords(Caret0, Buffer)),
  +	%% TODO: Simplify this caret movement.
  +	Caret1 = ee_caret:move_to(Caret0#ee_caret{col_no = ColNo + 1}, Buffer),
  +	DocSet1 = DocSet0#doc_set{focus_doc = FocusDoc#doc_view{caret = Caret1}},
  +	State#state{doc_set = DocSet1};
   handle_key(#wxKey{type = char, keyCode = KeyCode}, State) ->
   	io:format("Ignored key: ~p~n", [KeyCode]),
   	State.
  
positive.code: -include("ee_caret.hrl").
  
  -record(main_window, {window, status_bar}).
  -record(state, {win = #main_window{}, buffer, caret = #ee_caret{}, documents = []}).
  
  %% ===================================================================
  %% API
  
negative.code: handle_info(#wx{event = #wxKey{} = KeyEvent}, State) ->
  	{noreply, handle_key(KeyEvent, State)};
  %% TODO: replace raw ee_pubsub message with a record.
  handle_info({ee_pubsub, {document_inserted, DocPid}, _From}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  	{ok, DocSet1} = add_focus_document_view(DocPid, DocSet0),
  	ee_buffer_server:add_subscriber(DocPid, self()),
  	{ok, Buffer} = ee_buffer_server:get_buffer(DocPid),
  	%% TODO: Look intoif this is efficient (looking up the doc view by pid again)
  	{ok, DocSet2} = update_document_view_buffer(DocPid, DocSet1, Buffer),
  	wxFrame:refresh(Window),
  	{noreply, State#state{doc_set = DocSet2}};
  handle_info({ee_pubsub, {document_deleted, DocPid}, _From}, #state{doc_set = DocSet0} = State) ->
  	{ok, DocSet1} = remove_document_view(DocPid, DocSet0),
  	{noreply, State#state{doc_set = DocSet1}};
  handle_info({ee_pubsub, {buffer_update, Buffer}, DocPid}, #state{win = #main_window{window = Window}, doc_set = DocSet0} = State) ->
  	{ok, DocSet1} = update_document_view_buffer(DocPid, DocSet0, Buffer),
  	wxFrame:refresh(Window),
  	{noreply, State#state{doc_set = DocSet1}};
  handle_info(timeout, State) ->
  	wx:new(),
  	{noreply, State#state{win = create_window()}};
  