join_lines: Add "Join lines" command.

The code of existing "Reflow paragraph" command was refactored to
extract the bits required for the new command.
This commit is contained in:
Eugene Arshinov 2012-02-23 12:56:28 +04:00
parent 9fa6cfe47b
commit 6b760b99d4
2 changed files with 76 additions and 30 deletions

View File

@ -394,6 +394,8 @@ static void init_default_kb(void)
add_kb(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL, add_kb(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"), GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
"reflow_lines_block1"); "reflow_lines_block1");
keybindings_set_item(group, GEANY_KEYS_FORMAT_JOINLINES, NULL,
0, 0, "edit_joinlines", _("Join lines"), NULL);
group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT); group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
@ -2060,7 +2062,28 @@ static void join_lines(GeanyEditor *editor)
} }
static void split_lines(GeanyEditor *editor, gint column) static gint get_reflow_column(GeanyEditor *editor)
{
const GeanyEditorPrefs *eprefs = editor_get_prefs(editor);
if (editor->line_breaking)
{
/* use line break column if enabled */
return eprefs->line_break_column;
}
else if (eprefs->long_line_type != 2)
{
/* use long line if enabled */
return eprefs->long_line_column;
}
else
{
/* do nothing if no column is defined */
return -1;
}
}
static void reflow_lines(GeanyEditor *editor, gint column)
{ {
gint start, indent, linescount, i, end; gint start, indent, linescount, i, end;
gchar c; gchar c;
@ -2130,8 +2153,23 @@ static void split_lines(GeanyEditor *editor, gint column)
} }
/* deselect last newline of selection, if any */
static void sci_deselect_last_newline(ScintillaObject *sci)
{
gint start, end;
start = sci_get_selection_start(sci);
end = sci_get_selection_end(sci);
if (end > start && sci_get_col_from_position(sci, end) == 0)
{
end = sci_get_line_end_position(sci, sci_get_line_from_position(sci, end-1));
sci_set_selection(sci, start, end);
}
}
/* if cursor < anchor, swap them */ /* if cursor < anchor, swap them */
static void sci_fix_selection(ScintillaObject *sci) static void sci_fix_selection_anchors(ScintillaObject *sci)
{ {
gint start, end; gint start, end;
@ -2145,45 +2183,49 @@ static void reflow_paragraph(GeanyEditor *editor)
{ {
ScintillaObject *sci = editor->sci; ScintillaObject *sci = editor->sci;
gboolean sel; gboolean sel;
gint column = -1; gint column;
const GeanyEditorPrefs *eprefs = editor_get_prefs(editor);
if (editor->line_breaking) column = get_reflow_column(editor);
if (column == -1)
{ {
/* use line break column if enabled */
column = eprefs->line_break_column;
}
else if (eprefs->long_line_type != 2)
{
/* use long line if enabled */
column = eprefs->long_line_column;
}
else
{
/* do nothing if no column is defined */
utils_beep(); utils_beep();
return; return;
} }
sci_start_undo_action(sci); sci_start_undo_action(sci);
sel = sci_has_selection(sci); sel = sci_has_selection(sci);
if (!sel) if (!sel)
{
gint line, pos;
editor_select_indent_block(editor); editor_select_indent_block(editor);
sci_deselect_last_newline(sci);
sci_fix_selection_anchors(sci);
reflow_lines(editor, column);
if (!sel)
sci_set_anchor(sci, -1);
/* deselect last line break */ sci_end_undo_action(sci);
pos = sci_get_selection_end(sci); }
line = sci_get_line_from_position(sci, pos);
if (line < sci_get_line_count(sci) - 1)
{ static void join_paragraph(GeanyEditor *editor)
/* not last line */ {
pos = sci_get_line_end_position(sci, line - 1); ScintillaObject *sci = editor->sci;
sci_set_selection_end(sci, pos); gboolean sel;
} gint column;
column = get_reflow_column(editor);
if (column == -1)
{
utils_beep();
return;
} }
sci_fix_selection(sci);
split_lines(editor, column); sci_start_undo_action(sci);
sel = sci_has_selection(sci);
if (!sel)
editor_select_indent_block(editor);
sci_deselect_last_newline(sci);
//sci_fix_selection_anchors(sci);
join_lines(editor);
if (!sel) if (!sel)
sci_set_anchor(sci, -1); sci_set_anchor(sci, -1);
@ -2248,6 +2290,9 @@ static gboolean cb_func_format_action(guint key_id)
case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH: case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
reflow_paragraph(doc->editor); reflow_paragraph(doc->editor);
break; break;
case GEANY_KEYS_FORMAT_JOINLINES:
join_paragraph(doc->editor);
break;
} }
return TRUE; return TRUE;
} }

View File

@ -240,6 +240,7 @@ enum GeanyKeyBindingID
GEANY_KEYS_PROJECT_OPEN, /**< Keybinding. */ GEANY_KEYS_PROJECT_OPEN, /**< Keybinding. */
GEANY_KEYS_PROJECT_NEW, /**< Keybinding. */ GEANY_KEYS_PROJECT_NEW, /**< Keybinding. */
GEANY_KEYS_PROJECT_CLOSE, /**< Keybinding. */ GEANY_KEYS_PROJECT_CLOSE, /**< Keybinding. */
GEANY_KEYS_FORMAT_JOINLINES, /**< Keybinding. */
GEANY_KEYS_COUNT /* must not be used by plugins */ GEANY_KEYS_COUNT /* must not be used by plugins */
}; };