anchor.diff:  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}, documents = Documents} = State) ->
  +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),
  -	wxFrame:refresh(Window),
  -	{noreply, State#state{buffer = Buffer, documents = [DocPid|Documents]}};
  -handle_info({ee_pubsub, {document_deleted, DocPid}, _From}, #state{documents = Documents} = State) ->
  -	{noreply, State#state{documents = lists:delete(DocPid, Documents)}};
  -handle_info({ee_pubsub, {buffer_update, Buffer}, _From}, #state{win = #main_window{window = Window}} = State) ->
  -	wxFrame:refresh(Window),
  -	{noreply, State#state{buffer = Buffer}};
  +	%% 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()}};
  
positive.code: 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)),
  	%% 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)),
  	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 = KeyCode}, State) ->
  	io:format("Ignored key: ~p~n", [KeyCode]),
  	State.
  
negative.code: 	wx:destroy(),
  	ok.
  
  handle_call({get_buffer}, _From, #state{doc_set = #doc_set{focus_doc = undefined}} = State) ->
  	{reply, {undefined, #ee_caret{}}, State};
  handle_call({get_buffer}, _From, #state{doc_set = #doc_set{focus_doc = #doc_view{buffer = Buffer, caret = Caret}}} = State) ->
  	{reply, {Buffer, Caret}, State};
  handle_call(Msg, _From, _State) ->
  	erlang:error({bad_call, Msg}).
  