medit/moo/mooedit/mooindenter.c

491 lines
12 KiB
C
Raw Normal View History

2006-05-21 18:11:05 -05:00
/*
* mooindenter.c
2005-09-06 16:21:05 +00:00
*
2010-12-21 20:15:45 -08:00
* Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
2005-09-06 16:21:05 +00:00
*
* This file is part of medit. medit is free software; you can
* redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
2005-09-06 16:21:05 +00:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
2005-09-06 16:21:05 +00:00
*/
#include "mooedit/mooindenter.h"
2005-11-04 01:35:22 +00:00
#include "mooedit/mooedit.h"
#include "marshals.h"
2005-09-06 16:21:05 +00:00
#include <string.h>
/* XXX this doesn't take unicode control chars into account */
2010-11-23 21:54:39 -08:00
static void sync_settings (MooIndenter *indenter);
2005-09-06 16:21:05 +00:00
2010-12-08 01:11:51 -08:00
enum {
SETTING_USE_TABS,
SETTING_INDENT_WIDTH,
LAST_SETTING
};
static guint settings[LAST_SETTING];
2005-09-06 16:21:05 +00:00
/* MOO_TYPE_INDENTER */
G_DEFINE_TYPE (MooIndenter, moo_indenter, G_TYPE_OBJECT)
2010-12-08 01:11:51 -08:00
2005-09-06 16:21:05 +00:00
static void
2010-11-23 21:54:39 -08:00
moo_indenter_class_init (G_GNUC_UNUSED MooIndenterClass *klass)
2005-09-06 16:21:05 +00:00
{
2010-12-08 01:11:51 -08:00
settings[SETTING_USE_TABS] = moo_edit_config_install_setting (
g_param_spec_boolean ("indent-use-tabs", "indent-use-tabs", "indent-use-tabs",
TRUE, (GParamFlags) G_PARAM_READWRITE));
moo_edit_config_install_alias ("indent-use-tabs", "use-tabs");
settings[SETTING_INDENT_WIDTH] = moo_edit_config_install_setting (
g_param_spec_uint ("indent-width", "indent-width", "indent-width",
1, G_MAXUINT, 8, (GParamFlags) G_PARAM_READWRITE));
2005-09-06 16:21:05 +00:00
}
2010-12-08 01:11:51 -08:00
2005-09-06 16:21:05 +00:00
static void
2010-11-23 21:54:39 -08:00
moo_indenter_init (MooIndenter *indent)
2005-09-06 16:21:05 +00:00
{
2010-11-23 21:54:39 -08:00
indent->tab_width = 8;
indent->use_tabs = TRUE;
indent->indent = 8;
2005-09-06 16:21:05 +00:00
}
2010-12-08 01:11:51 -08:00
2010-11-23 21:54:39 -08:00
MooIndenter *
moo_indenter_new (MooEdit *doc)
2005-11-04 01:35:22 +00:00
{
MooIndenter *indent;
2010-11-23 21:54:39 -08:00
g_return_val_if_fail (MOO_IS_EDIT (doc), NULL);
2005-09-06 16:21:05 +00:00
2010-11-23 21:54:39 -08:00
indent = g_object_new (MOO_TYPE_INDENTER, NULL);
indent->doc = doc;
2005-09-06 16:21:05 +00:00
2010-11-23 21:54:39 -08:00
return indent;
2005-09-06 16:21:05 +00:00
}
/*****************************************************************************/
/* Default implementation
*/
2010-11-23 21:54:39 -08:00
char *
2005-09-06 16:21:05 +00:00
moo_indenter_make_space (MooIndenter *indenter,
guint len,
guint start)
{
guint tabs, spaces, delta;
2010-12-19 02:26:03 -08:00
guint tab_width;
2005-09-06 16:21:05 +00:00
char *string;
g_return_val_if_fail (MOO_IS_INDENTER (indenter), NULL);
2010-11-23 21:54:39 -08:00
sync_settings (indenter);
2010-12-19 02:26:03 -08:00
tab_width = indenter->tab_width;
2005-09-06 16:21:05 +00:00
if (!len)
return NULL;
2007-08-06 23:06:42 -05:00
if (!indenter->use_tabs)
2005-09-06 16:21:05 +00:00
return g_strnfill (len, ' ');
delta = start % tab_width;
2005-09-17 12:58:42 +00:00
if (!delta)
{
tabs = len / tab_width;
spaces = len % tab_width;
}
else if (len < tab_width - delta)
{
tabs = 0;
spaces = len;
}
else if (len == tab_width - delta)
{
tabs = 1;
spaces = 0;
}
else
{
len -= tab_width - delta;
tabs = len / tab_width + 1;
spaces = len % tab_width;
}
2005-09-06 16:21:05 +00:00
string = g_new (char, tabs + spaces + 1);
string[tabs + spaces] = 0;
if (tabs)
memset (string, '\t', tabs);
if (spaces)
memset (string + tabs, ' ', spaces);
return string;
}
/* computes amount of leading white space on the given line;
returns TRUE if line contains some non-whitespace chars;
2007-07-31 09:00:28 -05:00
if returns TRUE, then iter points to the first non-white-space char,
otherwise it points to the end of line */
2005-09-06 16:21:05 +00:00
static gboolean
compute_line_offset (GtkTextIter *dest,
2005-09-06 16:21:05 +00:00
guint tab_width,
guint *offsetp)
{
guint offset = 0;
GtkTextIter iter = *dest;
2007-07-31 09:00:28 -05:00
gboolean retval = FALSE;
2005-09-06 16:21:05 +00:00
while (!gtk_text_iter_ends_line (&iter))
2005-09-06 16:21:05 +00:00
{
gunichar c = gtk_text_iter_get_char (&iter);
2005-09-06 16:21:05 +00:00
if (c == ' ')
{
offset += 1;
}
else if (c == '\t')
{
guint add = tab_width - offset % tab_width;
offset += add;
}
else
{
2007-07-31 09:00:28 -05:00
retval = TRUE;
break;
2005-09-06 16:21:05 +00:00
}
gtk_text_iter_forward_char (&iter);
2005-09-06 16:21:05 +00:00
}
2007-07-31 09:00:28 -05:00
*offsetp = offset;
*dest = iter;
return retval;
2005-09-06 16:21:05 +00:00
}
2010-11-23 21:54:39 -08:00
void
moo_indenter_character (MooIndenter *indenter,
const char *inserted_char,
GtkTextIter *where)
2005-09-06 16:21:05 +00:00
{
char *indent_string = NULL;
GtkTextBuffer *buffer = gtk_text_iter_get_buffer (where);
2007-07-31 09:00:28 -05:00
guint offset;
GtkTextIter iter;
2005-09-06 16:21:05 +00:00
2010-11-23 21:54:39 -08:00
g_return_if_fail (MOO_IS_INDENTER (indenter));
g_return_if_fail (inserted_char != NULL);
if (*inserted_char != '\n')
2005-09-06 16:21:05 +00:00
return;
2010-11-23 21:54:39 -08:00
buffer = gtk_text_iter_get_buffer (where);
g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
sync_settings (indenter);
2007-07-31 09:00:28 -05:00
iter = *where;
gtk_text_iter_backward_line (&iter);
compute_line_offset (&iter, indenter->tab_width, &offset);
2005-09-06 16:21:05 +00:00
2007-07-31 09:00:28 -05:00
if (offset)
2005-09-06 16:21:05 +00:00
{
2007-07-31 09:00:28 -05:00
indent_string = moo_indenter_make_space (indenter, offset, 0);
2005-09-06 16:21:05 +00:00
gtk_text_buffer_insert (buffer, where, indent_string, -1);
g_free (indent_string);
}
}
/* computes offset of start and returns offset or -1 if there are
non-whitespace characters before start */
2005-09-17 12:58:42 +00:00
int
moo_iter_get_blank_offset (const GtkTextIter *start,
guint tab_width)
2005-09-06 16:21:05 +00:00
{
GtkTextIter iter;
guint offset;
if (gtk_text_iter_starts_line (start))
return 0;
iter = *start;
gtk_text_iter_set_line_offset (&iter, 0);
offset = 0;
while (gtk_text_iter_compare (&iter, start))
{
gunichar c = gtk_text_iter_get_char (&iter);
if (c == ' ')
{
offset += 1;
}
else if (c == '\t')
{
guint add = tab_width - offset % tab_width;
offset += add;
}
else
{
return -1;
}
gtk_text_iter_forward_char (&iter);
}
return offset;
}
/* computes where cursor should jump when backspace is pressed
<-- result -->
blah blah blah
blah
| offset
*/
2005-09-17 12:58:42 +00:00
guint
moo_text_iter_get_prev_stop (const GtkTextIter *start,
guint tab_width,
guint offset,
gboolean same_line)
2005-09-06 16:21:05 +00:00
{
GtkTextIter iter;
guint indent;
iter = *start;
gtk_text_iter_set_line_offset (&iter, 0);
2006-07-09 08:55:04 -05:00
if (!same_line && !gtk_text_iter_backward_line (&iter))
return 0;
do
2005-09-06 16:21:05 +00:00
{
2006-07-09 08:55:04 -05:00
if (compute_line_offset (&iter, tab_width, &indent) && indent <= offset)
return indent;
else if (!gtk_text_iter_get_line (&iter))
2005-09-06 16:21:05 +00:00
return 0;
}
2006-07-09 08:55:04 -05:00
while (gtk_text_iter_backward_line (&iter));
return 0;
2005-09-06 16:21:05 +00:00
}
/* computes visual offset at iter, amount of white space before the iter,
and makes iter to point to the beginning of the white space, e.g. for
blah wefwefw
|
it would set offset == 7, white_space == 3, and set iter to the first
space after 'blah'
*/
static void
iter_get_visual_offset (GtkTextIter *iter,
guint tab_width,
int *offsetp,
int *white_spacep)
{
2008-01-05 02:08:03 -06:00
GtkTextIter start, white_space_start = {0};
2005-09-06 16:21:05 +00:00
guint offset, white_space;
start = *iter;
gtk_text_iter_set_line_offset (&start, 0);
offset = 0;
white_space = 0;
while (gtk_text_iter_compare (&start, iter))
{
gunichar c = gtk_text_iter_get_char (&start);
if (c == ' ' || c == '\t')
{
if (!white_space)
white_space_start = start;
}
else
{
white_space = 0;
}
if (c == '\t')
{
guint add = tab_width - offset % tab_width;
offset += add;
white_space += add;
}
else if (c == ' ')
{
offset += 1;
white_space += 1;
}
else
{
offset += 1;
}
gtk_text_iter_forward_char (&start);
}
*offsetp = offset;
*white_spacep = white_space;
if (white_space)
*iter = white_space_start;
}
2005-09-17 12:58:42 +00:00
void
moo_indenter_tab (MooIndenter *indenter,
GtkTextBuffer *buffer)
2005-09-06 16:21:05 +00:00
{
GtkTextIter insert, start;
int offset, new_offset, white_space;
2010-12-19 02:26:03 -08:00
guint tab_width;
guint indent;
2005-09-06 16:21:05 +00:00
char *text = NULL;
2010-11-23 21:54:39 -08:00
sync_settings (indenter);
2010-12-19 02:26:03 -08:00
tab_width = indenter->tab_width;
indent = indenter->indent;
2005-09-06 16:21:05 +00:00
gtk_text_buffer_get_iter_at_mark (buffer, &insert, gtk_text_buffer_get_insert (buffer));
start = insert;
iter_get_visual_offset (&start, tab_width, &offset, &white_space);
2007-08-06 23:06:42 -05:00
new_offset = offset + (indent - offset % indent);
2005-09-06 16:21:05 +00:00
text = moo_indenter_make_space (indenter,
new_offset - offset + white_space,
offset - white_space);
gtk_text_buffer_delete (buffer, &start, &insert);
gtk_text_buffer_insert (buffer, &start, text, -1);
g_free (text);
}
static void
shift_line_forward (MooIndenter *indenter,
GtkTextBuffer *buffer,
GtkTextIter *iter)
{
char *text;
guint offset;
GtkTextIter start;
2007-08-06 23:06:42 -05:00
if (!compute_line_offset (iter, indenter->tab_width, &offset))
return;
2005-09-06 16:21:05 +00:00
if (offset)
{
start = *iter;
gtk_text_iter_set_line_offset (&start, 0);
gtk_text_buffer_delete (buffer, &start, iter);
}
2007-08-06 23:06:42 -05:00
text = moo_indenter_make_space (indenter, offset + indenter->indent, 0);
2005-09-06 16:21:05 +00:00
if (text)
gtk_text_buffer_insert (buffer, iter, text, -1);
g_free (text);
}
static void
shift_line_backward (MooIndenter *indenter,
GtkTextBuffer *buffer,
GtkTextIter *iter)
{
GtkTextIter end;
int deleted;
gunichar c;
gtk_text_iter_set_line_offset (iter, 0);
end = *iter;
deleted = 0;
while (TRUE)
{
if (gtk_text_iter_ends_line (&end))
break;
c = gtk_text_iter_get_char (&end);
if (c == ' ')
{
gtk_text_iter_forward_char (&end);
deleted += 1;
}
else if (c == '\t')
{
gtk_text_iter_forward_char (&end);
2007-08-06 23:06:42 -05:00
deleted += indenter->tab_width;
2005-09-06 16:21:05 +00:00
}
else
{
break;
}
2007-08-06 23:06:42 -05:00
if (deleted >= (int) indenter->indent)
2005-09-06 16:21:05 +00:00
break;
}
gtk_text_buffer_delete (buffer, iter, &end);
2007-08-06 23:06:42 -05:00
deleted -= indenter->indent;
2005-09-06 16:21:05 +00:00
if (deleted > 0)
{
char *text = moo_indenter_make_space (indenter, deleted, 0);
gtk_text_buffer_insert (buffer, iter, text, -1);
g_free (text);
}
}
2005-09-17 12:58:42 +00:00
void
moo_indenter_shift_lines (MooIndenter *indenter,
GtkTextBuffer *buffer,
guint first_line,
guint last_line,
int direction)
2005-09-06 16:21:05 +00:00
{
guint i;
GtkTextIter iter;
2010-11-23 21:54:39 -08:00
sync_settings (indenter);
2005-09-06 16:21:05 +00:00
for (i = first_line; i <= last_line; ++i)
{
gtk_text_buffer_get_iter_at_line (buffer, &iter, i);
if (direction > 0)
shift_line_forward (indenter, buffer, &iter);
else
shift_line_backward (indenter, buffer, &iter);
}
}
static void
2010-11-23 21:54:39 -08:00
sync_settings (MooIndenter *indenter)
2005-09-06 16:21:05 +00:00
{
2010-11-23 21:54:39 -08:00
g_return_if_fail (MOO_IS_EDIT (indenter->doc));
indenter->use_tabs = moo_edit_config_get_bool (indenter->doc->config, "indent-use-tabs");
indenter->indent = moo_edit_config_get_uint (indenter->doc->config, "indent-width");
indenter->tab_width = moo_edit_config_get_uint (indenter->doc->config, "tab-width");
2005-09-06 16:21:05 +00:00
}