Function: move-to-column

move-to-column is an interactive function defined in indent.c.

Signature

(move-to-column COLUMN &optional FORCE)

Documentation

Move point to column COLUMN in the current line.

Interactively, COLUMN is the value of prefix numeric argument. The column of a character is calculated by adding together the widths as displayed of the previous characters in the line. This function ignores line-continuation; there is no upper limit on the column number a character can have and horizontal scrolling has no effect. Text that has an invisible property is considered as having width 0, unless buffer-invisibility-spec specifies that it is replaced by an ellipsis.

If specified column is within a character, point goes after that character. If it's past end of line, point goes to end of line.

Optional second argument FORCE non-nil means if COLUMN is in the middle of a tab character, either change it to spaces (when indent-tabs-mode(var)/indent-tabs-mode(fun) is nil), or insert enough spaces before it to reach COLUMN (otherwise). In addition, if FORCE is t, and the line is too short to reach COLUMN, add spaces/tabs to get there.

The return value is the current column.

View in manual

Probably introduced at or before Emacs version 19.29.

Key Bindings

Source Code

// Defined in /usr/src/emacs/src/indent.c
{
  ptrdiff_t pos, prev_pos, prev_bpos, prev_col;
  EMACS_INT col;
  EMACS_INT goal;

  CHECK_FIXNAT (column);
  goal = XFIXNUM (column);

  col = goal;
  pos = ZV;
  scan_for_column (&pos, &col, &prev_pos, &prev_bpos, &prev_col);

  SET_PT (pos);

  /* If a tab char made us overshoot, change it to spaces
     and scan through it again.  */
  if (!NILP (force) && col > goal)
    {
      int c;

      c = FETCH_CHAR (prev_bpos);
      if (c == '\t' && prev_col < goal && prev_bpos < PT_BYTE)
	{
	  ptrdiff_t goal_pt, goal_pt_byte;

	  /* Insert spaces in front of the tab to reach GOAL.  Do this
	     first so that a marker at the end of the tab gets
	     adjusted.  */
	  SET_PT_BOTH (prev_pos, prev_bpos);
	  Finsert_char (make_fixnum (' '), make_fixnum (goal - prev_col), Qt);

	  /* Now delete the tab, and indent to COL.  */
	  del_range (PT, PT + 1);
	  goal_pt = PT;
	  goal_pt_byte = PT_BYTE;
	  Findent_to (make_fixnum (col), Qnil);
	  SET_PT_BOTH (goal_pt, goal_pt_byte);

	  /* Set the last_known... vars consistently.  */
	  col = goal;
	}
    }

  /* If line ends prematurely, add space to the end.  */
  if (col < goal && EQ (force, Qt))
    Findent_to (make_fixnum (col = goal), Qnil);

  last_known_column = col;
  last_known_column_point = PT;
  last_known_column_modified = MODIFF;

  return make_fixnum (col);
}