Kaydet (Commit) b77b71fd authored tarafından Enrico Tröger's avatar Enrico Tröger

Add option to insert line numbers (closes #3197150).

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5576 ea778897-0a13-0410-b9d1-a72fbfd435f5
üst 4f6354d4
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
Cleanup. Cleanup.
Respect saved state of 'Case sensitive' option when using Respect saved state of 'Case sensitive' option when using
the 'Regular expressions' option as well. the 'Regular expressions' option as well.
* plugins/export.c:
Add option to insert line numbers (closes #3197150).
2011-03-05 Colomban Wendling <colomban(at)geany(dot)org> 2011-03-05 Colomban Wendling <colomban(at)geany(dot)org>
......
...@@ -100,7 +100,8 @@ enum ...@@ -100,7 +100,8 @@ enum
DATE_TYPE_HTML DATE_TYPE_HTML
}; };
typedef void (*ExportFunc) (GeanyDocument *doc, const gchar *filename, gboolean use_zoom); typedef void (*ExportFunc) (GeanyDocument *doc, const gchar *filename,
gboolean use_zoom, gboolean insert_line_numbers);
typedef struct typedef struct
{ {
GeanyDocument *doc; GeanyDocument *doc;
...@@ -109,8 +110,10 @@ typedef struct ...@@ -109,8 +110,10 @@ typedef struct
} ExportInfo; } ExportInfo;
static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data); static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean use_zoom); static void write_html_file(GeanyDocument *doc, const gchar *filename,
static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean use_zoom); gboolean use_zoom, gboolean insert_line_numbers);
static void write_latex_file(GeanyDocument *doc, const gchar *filename,
gboolean use_zoom, gboolean insert_line_numbers);
/* converts a RGB colour into a LaTeX compatible representation, taken from SciTE */ /* converts a RGB colour into a LaTeX compatible representation, taken from SciTE */
...@@ -149,7 +152,7 @@ static gchar *get_tex_style(gint style) ...@@ -149,7 +152,7 @@ static gchar *get_tex_style(gint style)
static void create_file_save_as_dialog(const gchar *extension, ExportFunc func, static void create_file_save_as_dialog(const gchar *extension, ExportFunc func,
gboolean show_zoom_level_checkbox) gboolean show_zoom_level_checkbox)
{ {
GtkWidget *dialog; GtkWidget *dialog, *vbox;
GeanyDocument *doc; GeanyDocument *doc;
ExportInfo *exi; ExportInfo *exi;
...@@ -175,20 +178,31 @@ static void create_file_save_as_dialog(const gchar *extension, ExportFunc func, ...@@ -175,20 +178,31 @@ static void create_file_save_as_dialog(const gchar *extension, ExportFunc func,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
/* file chooser extra widget */
vbox = gtk_vbox_new(FALSE, 0);
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox);
{
GtkWidget *check_line_numbers;
check_line_numbers = gtk_check_button_new_with_mnemonic(_("_Insert line numbers"));
ui_widget_set_tooltip_text(check_line_numbers,
_("Insert line numbers before each line in the exported document"));
gtk_box_pack_start(GTK_BOX(vbox), check_line_numbers, FALSE, FALSE, 0);
gtk_widget_show_all(vbox);
ui_hookup_widget(dialog, check_line_numbers, "check_line_numbers");
}
if (show_zoom_level_checkbox) if (show_zoom_level_checkbox)
{ {
GtkWidget *vbox, *check_zoom_level; GtkWidget *check_zoom_level;
vbox = gtk_vbox_new(FALSE, 0);
check_zoom_level = gtk_check_button_new_with_mnemonic(_("_Use current zoom level")); check_zoom_level = gtk_check_button_new_with_mnemonic(_("_Use current zoom level"));
ui_widget_set_tooltip_text(check_zoom_level, ui_widget_set_tooltip_text(check_zoom_level,
_("Renders the font size of the document together with the current zoom level")); _("Renders the font size of the document together with the current zoom level"));
gtk_box_pack_start(GTK_BOX(vbox), check_zoom_level, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), check_zoom_level, FALSE, FALSE, 0);
gtk_widget_show_all(vbox); gtk_widget_show_all(vbox);
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox);
ui_hookup_widget(dialog, check_zoom_level, "check_zoom_level"); ui_hookup_widget(dialog, check_zoom_level, "check_zoom_level");
exi->have_zoom_level_checkbox = TRUE; exi->have_zoom_level_checkbox = TRUE;
} }
...@@ -299,6 +313,7 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin ...@@ -299,6 +313,7 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin
{ {
gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gchar *utf8_filename; gchar *utf8_filename;
gboolean insert_line_numbers;
gboolean use_zoom_level = FALSE; gboolean use_zoom_level = FALSE;
if (exi->have_zoom_level_checkbox) if (exi->have_zoom_level_checkbox)
...@@ -306,6 +321,8 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin ...@@ -306,6 +321,8 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin
use_zoom_level = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( use_zoom_level = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
ui_lookup_widget(GTK_WIDGET(dialog), "check_zoom_level"))); ui_lookup_widget(GTK_WIDGET(dialog), "check_zoom_level")));
} }
insert_line_numbers = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
ui_lookup_widget(GTK_WIDGET(dialog), "check_line_numbers")));
utf8_filename = utils_get_utf8_from_locale(new_filename); utf8_filename = utils_get_utf8_from_locale(new_filename);
...@@ -318,7 +335,7 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin ...@@ -318,7 +335,7 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin
return; return;
} }
exi->export_func(exi->doc, new_filename, use_zoom_level); exi->export_func(exi->doc, new_filename, use_zoom_level, insert_line_numbers);
g_free(utf8_filename); g_free(utf8_filename);
g_free(new_filename); g_free(new_filename);
...@@ -328,10 +345,29 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin ...@@ -328,10 +345,29 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin
} }
static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean use_zoom) /* returns the "width" (count of needed characters) for the given number */
static gint get_line_numbers_arity(gint line_number)
{
gint a = 0;
while ((line_number /= 10) != 0)
a++;
return a;
}
static gint get_line_number_width(GeanyDocument *doc)
{
gint line_count = sci_get_line_count(doc->editor->sci);
return get_line_numbers_arity(line_count);
}
static void write_latex_file(GeanyDocument *doc, const gchar *filename,
gboolean use_zoom, gboolean insert_line_numbers)
{ {
GeanyEditor *editor = doc->editor; GeanyEditor *editor = doc->editor;
gint i, doc_len, style = -1, old_style = 0, column = 0; gint i, doc_len, style = -1, old_style = 0, column = 0;
gint k, line_number, line_number_width, line_number_max_width = 0, pad;
gchar c, c_next, *tmp, *date; gchar c, c_next, *tmp, *date;
/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */ /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
gint styles[STYLE_MAX + 1][MAX_TYPES]; gint styles[STYLE_MAX + 1][MAX_TYPES];
...@@ -351,6 +387,9 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean ...@@ -351,6 +387,9 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean
styles[i][USED] = 0; styles[i][USED] = 0;
} }
if (insert_line_numbers)
line_number_max_width = get_line_number_width(doc);
/* read the document and write the LaTeX code */ /* read the document and write the LaTeX code */
body = g_string_new(""); body = g_string_new("");
doc_len = sci_get_length(doc->editor->sci); doc_len = sci_get_length(doc->editor->sci);
...@@ -360,6 +399,20 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean ...@@ -360,6 +399,20 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean
c = sci_get_char_at(doc->editor->sci, i); c = sci_get_char_at(doc->editor->sci, i);
c_next = sci_get_char_at(doc->editor->sci, i + 1); c_next = sci_get_char_at(doc->editor->sci, i + 1);
/* line numbers */
if (insert_line_numbers && column == 0)
{
line_number = sci_get_line_from_position(doc->editor->sci, i) + 1;
line_number_width = get_line_numbers_arity(line_number);
/* padding */
pad = line_number_max_width - line_number_width;
for (k = 0; k < pad; k++)
{
g_string_append(body, " ");
}
g_string_append_printf(body, "%d ", line_number);
}
if (style != old_style || ! block_open) if (style != old_style || ! block_open)
{ {
old_style = style; old_style = style;
...@@ -536,10 +589,12 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean ...@@ -536,10 +589,12 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean
} }
static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean use_zoom) static void write_html_file(GeanyDocument *doc, const gchar *filename,
gboolean use_zoom, gboolean insert_line_numbers)
{ {
GeanyEditor *editor = doc->editor; GeanyEditor *editor = doc->editor;
gint i, doc_len, style = -1, old_style = 0, column = 0; gint i, doc_len, style = -1, old_style = 0, column = 0;
gint k, line_number, line_number_width, line_number_max_width = 0, pad;
gchar c, c_next, *date; gchar c, c_next, *date;
/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */ /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
gint styles[STYLE_MAX + 1][MAX_TYPES]; gint styles[STYLE_MAX + 1][MAX_TYPES];
...@@ -571,6 +626,9 @@ static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean ...@@ -571,6 +626,9 @@ static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean
if (use_zoom) if (use_zoom)
font_size += scintilla_send_message(doc->editor->sci, SCI_GETZOOM, 0, 0); font_size += scintilla_send_message(doc->editor->sci, SCI_GETZOOM, 0, 0);
if (insert_line_numbers)
line_number_max_width = get_line_number_width(doc);
/* read the document and write the HTML body */ /* read the document and write the HTML body */
body = g_string_new(""); body = g_string_new("");
doc_len = sci_get_length(doc->editor->sci); doc_len = sci_get_length(doc->editor->sci);
...@@ -581,6 +639,20 @@ static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean ...@@ -581,6 +639,20 @@ static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean
/* sci_get_char_at() takes care of index boundaries and return 0 if i is too high */ /* sci_get_char_at() takes care of index boundaries and return 0 if i is too high */
c_next = sci_get_char_at(doc->editor->sci, i + 1); c_next = sci_get_char_at(doc->editor->sci, i + 1);
/* line numbers */
if (insert_line_numbers && column == 0)
{
line_number = sci_get_line_from_position(doc->editor->sci, i) + 1;
line_number_width = get_line_numbers_arity(line_number);
/* padding */
pad = line_number_max_width - line_number_width;
for (k = 0; k < pad; k++)
{
g_string_append(body, "&nbsp;");
}
g_string_append_printf(body, "%d&nbsp;", line_number);
}
if ((style != old_style || ! span_open) && ! isspace(c)) if ((style != old_style || ! span_open) && ! isspace(c))
{ {
old_style = style; old_style = style;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment