2
* ao_blanklines.c - this file is part of Addons, a Geany plugin
4
* Copyright 2009 Eugene Arshinov <earshinov@gmail.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include "geanyplugin.h"
25
#include "ao_blanklines.h"
27
static gboolean enabled = FALSE;
29
static void editor_strip_trailing_newlines(GeanyEditor *editor)
31
const gint maxline = sci_get_line_count(editor->sci) - 1;
32
const gint maxpos = sci_get_line_end_position(editor->sci, maxline);
34
gint line, start, end, pos;
38
* Store index of the last non-empty line in `line' and position of the first
39
* of its trailing spaces in `pos'. If all lines are empty, `line' will
40
* contain -1, and `pos' will be undefined. If `line' does not contain
41
* trailing spaces, `pos' will be its end position.
43
for (line = maxline; line >= 0; line--)
45
start = sci_get_position_from_line(editor->sci, line);
46
end = sci_get_line_end_position(editor->sci, line);
49
* We can't be sure that `geany_data->file_prefs->strip_trailing_spaces'
50
* setting is set, so we should check for trailing spaces manually.
51
* Performance overhead of the for loop below is very small anyway,
52
* so it does not worth checking the setting manually and writing
56
for (pos = end-1; pos >= start; pos--)
58
ch = sci_get_char_at(editor->sci, pos);
59
if (ch != ' ' && ch != '\t')
67
if (line == -1 || geany_data->file_prefs->final_new_line)
69
/* leave one newline */
71
pos = sci_get_position_from_line(editor->sci, line);
76
/* there are some lines to be removed */
77
sci_set_target_start(editor->sci, pos);
78
sci_set_target_end(editor->sci, maxpos);
79
sci_replace_target(editor->sci, "", FALSE);
83
void ao_blanklines_set_enable(gboolean enabled_)
88
void ao_blanklines_on_document_before_save(GObject *object, GeanyDocument *doc, gpointer data)
91
editor_strip_trailing_newlines(doc->editor);