diff -ru a/src/document.c b/src/document.c --- a/src/document.c 2019-01-04 18:26:24.000000000 +0300 +++ b/src/document.c 2019-02-23 00:58:33.019469539 +0300 @@ -2143,7 +2143,7 @@ editor_ensure_final_newline(doc->editor); /* ensure newlines are consistent */ if (fp->ensure_convert_new_lines) - sci_convert_eols(doc->editor->sci, sci_get_eol_mode(doc->editor->sci)); + editor_convert_eols(doc->editor); /* notify plugins which may wish to modify the document before it's saved */ g_signal_emit_by_name(geany_object, "document-before-save", doc); Only in b/src: document.c.orig diff -ru a/src/editor.c b/src/editor.c --- a/src/editor.c 2019-01-04 18:26:24.000000000 +0300 +++ b/src/editor.c 2019-02-23 00:58:33.020469540 +0300 @@ -4543,6 +4543,141 @@ } } +struct Position +{ + gint pos; + gint virt; +}; + +struct Location +{ + gint line; + gint col; +}; + +struct Selection +{ + struct Location anchor; + struct Location caret; +}; + +struct Selections +{ + gint count; + struct Selection *array; +}; + +struct Location pos2loc(ScintillaObject *sci, struct Position const *pos) +{ + struct Location loc; + loc.line = sci_get_line_from_position(sci, pos->pos); + loc.col = sci_get_column_from_position(sci, pos->pos) + pos->virt; + return loc; +} + +struct Position loc2pos(ScintillaObject *sci, struct Location const *loc) +{ + struct Position pos; + pos.pos = sci_find_column(sci, loc->line, loc->col); + pos.virt = loc->col - sci_get_column_from_position(sci, pos.pos); + return pos; +} + +struct Position get_anchor_pos(ScintillaObject *sci, gint i) +{ + struct Position pos; + pos.pos = sci_get_selection_n_anchor(sci, i); + pos.virt = sci_get_selection_n_anchor_virtual_space(sci, i); + return pos; +} + +void set_anchor_pos(ScintillaObject *sci, int i, struct Position const *pos) +{ + sci_set_selection_n_anchor(sci, i, pos->pos); + sci_set_selection_n_anchor_virtual_space(sci, i, pos->virt); +}; + +struct Location get_anchor_loc(ScintillaObject *sci, gint i) +{ + struct Position pos = get_anchor_pos(sci, i); + struct Location loc = pos2loc(sci, &pos); + return loc; +} + +void set_anchor_loc(ScintillaObject *sci, gint i, struct Location const *loc) +{ + struct Position pos = loc2pos(sci, loc); + set_anchor_pos(sci, i, &pos); +} + +struct Position get_caret_pos(ScintillaObject *sci, gint i) +{ + struct Position pos; + pos.pos = sci_get_selection_n_caret(sci, i); + pos.virt = sci_get_selection_n_caret_virtual_space(sci, i); + return pos; +} + +void set_caret_pos(ScintillaObject *sci, gint i, struct Position const *pos) +{ + sci_set_selection_n_caret(sci, i, pos->pos); + sci_set_selection_n_caret_virtual_space(sci, i, pos->virt); +} + +struct Location get_caret_loc(ScintillaObject *sci, gint i) +{ + struct Position pos = get_caret_pos(sci, i); + struct Location loc = pos2loc(sci, &pos); + return loc; +} + +void set_caret_loc(ScintillaObject *sci, gint i, struct Location const *loc) +{ + struct Position pos = loc2pos(sci, loc); + set_caret_pos(sci, i, &pos); +} + +struct Selection get_selection_loc(ScintillaObject *sci, gint i) +{ + struct Selection sel; + sel.anchor = get_anchor_loc(sci, i); + sel.caret = get_caret_loc(sci, i); + return sel; +} + +void set_selection_loc(ScintillaObject *sci, gint i, struct Selection const *sel) +{ + set_anchor_loc(sci, i, &sel->anchor); + set_caret_loc(sci, i, &sel->caret); +} + +struct Selections get_selections_loc(ScintillaObject *sci) +{ + struct Selections sels = { 0, NULL }; + gint mask = SCVS_RECTANGULARSELECTION | SCVS_USERACCESSIBLE; + if (sci_get_virtual_space_options(sci) & mask) + { + gint n = sci_get_selections(sci); + sels.array = g_malloc(n * sizeof(struct Selection)); + for (gint i = 0; i < n; ++ i) + { + sels.array[ i ] = get_selection_loc(sci, i); + } + sels.count = n; + } + return sels; +}; + +void set_selections_loc(ScintillaObject *sci, struct Selections *sels) +{ + for (gint i = 0; i < sels->count; ++ i) + { + set_selection_loc(sci, i, &sels->array[i]); + } + sels->count = 0; + g_free(sels->array); + sels->array = NULL; +} void editor_strip_trailing_spaces(GeanyEditor *editor, gboolean ignore_selection) { @@ -4568,15 +4703,24 @@ } sci_start_undo_action(editor->sci); - + struct Selections sels = get_selections_loc(editor->sci); for (line = start_line; line < end_line; line++) { editor_strip_line_trailing_spaces(editor, line); } + set_selections_loc(editor->sci, &sels); sci_end_undo_action(editor->sci); } +void editor_convert_eols(GeanyEditor *editor) +{ + struct Selections sels = get_selections_loc(editor->sci); + sci_convert_eols(editor->sci, sci_get_eol_mode(editor->sci)); + set_selections_loc(editor->sci, &sels); +} + + void editor_ensure_final_newline(GeanyEditor *editor) { gint max_lines = sci_get_line_count(editor->sci); Only in b/src: editor.c.orig diff -ru a/src/editor.h b/src/editor.h --- a/src/editor.h 2019-01-04 18:26:24.000000000 +0300 +++ b/src/editor.h 2019-02-23 00:58:33.021469540 +0300 @@ -312,6 +312,8 @@ void editor_strip_trailing_spaces(GeanyEditor *editor, gboolean ignore_selection); +void editor_convert_eols(GeanyEditor *editor); + void editor_ensure_final_newline(GeanyEditor *editor); void editor_insert_color(GeanyEditor *editor, const gchar *colour);