end
  if y < State.top then
    State.old_cursor1 = State.cursor1
    State.old_selection1 = State.selection1
    State.mousepress_shift = App.shift_down()
    State.selection1 = {
        line=State.screen_top1.line,
        pos=State.screen_top1.pos,
    }
    return
  end
  for line_index,line in ipairs(State.lines) do
    if Text.in_line(State, line_index, x,y) then
      -- delicate dance between cursor, selection and old cursor/selection
      -- scenarios:
      --  regular press+release: sets cursor, clears selection
      --  shift press+release:
      --    sets selection to old cursor if not set otherwise leaves it untouched
      --    sets cursor
      --  press and hold to start a selection: sets selection on press, cursor on release
      --  press and hold, then press shift: ignore shift
      --    i.e. mouse_release should never look at shift state
--?       print_and_log(('edit.mouse_press: in line %d'):format(line_index))
      State.old_cursor1 = State.cursor1
      State.old_selection1 = State.selection1
      State.mousepress_shift = App.shift_down()
      State.selection1 = {
          line=line_index,
          pos=Text.to_pos_on_line(State, line_index, x, y),
      }
      return
    end
  end
  -- still here? click is below all screen lines
  State.old_cursor1 = State.cursor1
  State.old_selection1 = State.selection1
  State.mousepress_shift = App.shift_down()
  State.selection1 = {
      line=State.screen_bottom1.line,
      pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
  }
end
function edit.mouse_release(State, x,y, mouse_button)
  if State.search_term then return end
--?   print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
  for line_index,line in ipairs(State.lines) do
    if Text.in_line(State, line_index, x,y) then
--?       print_and_log(('edit.mouse_release: in line %d'):format(line_index))
      State.cursor1 = {
          line=line_index,
          pos=Text.to_pos_on_line(State, line_index, x, y),
      }
--?       print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
      if State.mousepress_shift then
        if State.old_selection1.line == nil then
          State.selection1 = State.old_cursor1
        else
          State.selection1 = State.old_selection1
        end
      end
      State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
      if eq(State.cursor1, State.selection1) then
        State.selection1 = {}
      end
      break
    end
  end
--?   print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos))
end
function edit.mouse_wheel_move(State, dx,dy)
  if dy > 0 then
    State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
    edit.put_cursor_on_next_text_line(State)
    for i=1,math.floor(dy) do
      Text.up(State)
    end
  elseif dy < 0 then
    State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
    edit.put_cursor_on_next_text_line(State)
    for i=1,math.floor(-dy) do
      Text.down(State)
    end
  end
end
function edit.text_input(State, t)
--?   print('text input', t)
  if State.search_term then
    State.search_term = State.search_term..t
    Text.search_next(State)
  else
    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
    Text.text_input(State, t)
  end
  schedule_save(State)
end
function edit.keychord_press(State, chord, key)
  if State.selection1.line and
      -- printable character created using shift key => delete selection
      -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
      (not App.shift_down() or utf8.len(key) == 1) and
      chord ~= 'C-a' and chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and chord ~= 'delete' and chord ~= 'C-z' and chord ~= 'C-y' and not App.is_cursor_movement(chord) then
    Text.delete_selection(State, State.left, State.right)
  end
  if State.search_term then
    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
    if chord == 'escape' then
      State.search_term = nil
      State.cursor1 = State.search_backup.cursor
      State.screen_top1 = State.search_backup.screen_top
      State.search_backup = nil
      Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
    elseif chord == 'return' then
      State.search_term = nil
      State.search_backup = nil
    elseif chord == 'backspace' then
      local len = utf8.len(State.search_term)
      local byte_offset = Text.offset(State.search_term, len)
      State.search_term = string.sub(State.search_term, 1, byte_offset-1)
    elseif chord == 'down' then
      State.cursor1.pos = State.cursor1.pos+1
      Text.search_next(State)
    elseif chord == 'up' then
      Text.search_previous(State)
    end
    return
  elseif chord == 'C-f' then
    State.search_term = ''
    State.search_backup = {
      cursor={line=State.cursor1.line, pos=State.cursor1.pos},
      screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
    }
  -- zoom
  elseif chord == 'C-=' then
    edit.update_font_settings(State, State.font_height+2)
    Text.redraw_all(State)
  elseif chord == 'C--' then
    if State.font_height > 2 then
      edit.update_font_settings(State, State.font_height-2)
      Text.redraw_all(State)
    end
  elseif chord == 'C-0' then
    edit.update_font_settings(State, 20)
    Text.redraw_all(State)
  -- undo
  elseif chord == 'C-z' then
    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
    local event = undo_event(State)
    if event then
      local src = event.before
      State.screen_top1 = deepcopy(src.screen_top)
      State.cursor1 = deepcopy(src.cursor)
      State.selection1 = deepcopy(src.selection)
      patch(State.lines, event.after, event.before)
      patch_placeholders(State.line_cache, event.after, event.before)
      -- if we're scrolling, reclaim all fragments to avoid memory leaks
      Text.redraw_all(State)
      schedule_save(State)
    end
  elseif chord == 'C-y' then
    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
    local event = redo_event(State)
    if event then
      local src = event.after
      State.screen_top1 = deepcopy(src.screen_top)
      State.cursor1 = deepcopy(src.cursor)
      State.selection1 = deepcopy(src.selection)
      patch(State.lines, event.before, event.after)
      -- if we're scrolling, reclaim all fragments to avoid memory leaks
      Text.redraw_all(State)
      schedule_save(State)
    end
  -- clipboard
  elseif chord == 'C-a' then
    State.selection1 = {line=1, pos=1}
    State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
  elseif chord == 'C-c' then
    local s = Text.selection(State)
    if s then
      App.set_clipboard(s)
    end
  elseif chord == 'C-x' then
    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
    local s = Text.cut_selection(State, State.left, State.right)
    if s then
      App.set_clipboard(s)
    end
    schedule_save(State)
  elseif chord == 'C-v' then
    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
    -- We don't have a good sense of when to scroll, so we'll be conservative
    -- and sometimes scroll when we didn't quite need to.
    local before_line = State.cursor1.line
    local before = snapshot(State, before_line)
    local clipboard_data = App.get_clipboard()
    for _,code in utf8.codes(clipboard_data) do
      local c = utf8.char(code)
      if c == '\n' then
        Text.insert_return(State)
      else
        Text.insert_at_cursor(State, c)
      end
    end
    if Text.cursor_out_of_screen(State) then
      Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
    end
    schedule_save(State)
    record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
  -- dispatch to text
  else
    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
    Text.keychord_press(State, chord)