NicoEdit text editor

I recently reported on Nicolas's superb text editor, that I tentatively named "NicEdit", however it turns out that name is taken, so I have renamed it to "NicoEdit".

Nicolas has released version 1.5, which saves settings in ~/.nicoedit.rc and has automatic syntax discovery.

I have played with the code and made some small mods, in particular added a bit more detection of the file type, checking the "shebang" line in the file. I have assigned it version 1.5.1. Here is the source:

/*Nicolas's Editor, NicoEdit, version 1.5, 2009 GPL licence*/

/*BK Build with ./bk_compile */
/*BK some minor mods, 23 June 2009*/
/*BK tab indent: make sure your text editor is not set to convert tabs to spaces!*/

uses
Gtk
Pango

init
Gtk.init (ref args)
Intl.textdomain(Config.GETTEXT_PACKAGE)
Intl.bindtextdomain(Config.GETTEXT_PACKAGE, Config.LOCALEDIR)
Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "UTF-8")
Environment.set_application_name(Config.GETTEXT_PACKAGE)
var test = new NicoEdit ()
test.open_file(args[1])
test.show_all ()
test.restore_stats ()
Gtk.main ()

class NicoEdit : Window
toolbar : Toolbar
text_view : SourceView
status_bar : Statusbar
status_bar2 : Statusbar
status_hbox : HBox
scrool : ScrolledWindow
document_modified : bool = false
separator_menu_item : SeparatorMenuItem
image_menu_item : ImageMenuItem
image_menu_item1 : ImageMenuItem
image_menu_item2 : ImageMenuItem
image_menu_item3 : ImageMenuItem
check_menu_item : CheckMenuItem
check_menu_item2 : CheckMenuItem
check_menu_item3 : CheckMenuItem
check_menu_item4 : CheckMenuItem
check_menu_item5 : CheckMenuItem
image_menu_item4 : ImageMenuItem
image_menu_item5 : ImageMenuItem
radio_menu_item : RadioMenuItem
radio_menu_item1 : RadioMenuItem
radio_menu_item2 : RadioMenuItem
radio_menu_item3 : RadioMenuItem
radio_menu_item4 : RadioMenuItem
radio_menu_item5 : RadioMenuItem
radio_menu_item6 : RadioMenuItem
radio_menu_item7 : RadioMenuItem
radio_menu_item8 : RadioMenuItem
radio_menu_item9 : RadioMenuItem
radio_menu_item10 : RadioMenuItem
radio_menu_item11 : RadioMenuItem
radio_menu_item12 : RadioMenuItem
radio_menu_item13 : RadioMenuItem
radio_menu_item14 : RadioMenuItem
radio_menu_item15 : RadioMenuItem
radio_menu_item16 : RadioMenuItem
radio_menu_item17 : RadioMenuItem
radio_menu_item18 : RadioMenuItem
radio_menu_item19 : RadioMenuItem
radio_menu_item20 : RadioMenuItem
radio_menu_item21 : RadioMenuItem
radio_menu_item22 : RadioMenuItem
radio_menu_item23 : RadioMenuItem
radio_menu_item24 : RadioMenuItem
fullscreen_button : ToolButton
bar : MenuBar
mylist : SList
goto_window : Dialog
save : Dialog
search_dialog : Dialog
menu_item : MenuItem
messagedialog : MessageDialog
gototext : Entry
buf : SourceBuffer
thefile : string = ""
info : Dialog
search_entry : Entry
match_case : CheckButton
find_backwards : CheckButton
find_button : Widget
search_flags : SourceSearchFlags
color : Gdk.Color
newfont : FontSelectionDialog
newcolor : ColorSelectionDialog
font_desc : FontDescription
manager : SourceLanguageManager
thedefaultsyntax : string
default_window_height : int
default_window_width : int
thebackgroundcolor : string
thetextcolor : string
thefont : string
viewtoolbar : string
linenumber : string
insertspace : string
highlightcurrentline : string
highlightsyntax : string
activatedfullscreen : bool = false
cancelclicked : bool
context : PrintContext
op : PrintOperation
thecompositor : SourcePrintCompositor

init
restore_window_size()
title = "NicoEdit"
default_height = default_window_height
default_width = default_window_width
window_position = WindowPosition.CENTER
delete_event += exit_program

/* Menus */
bar = new MenuBar()
var menu1 = new Menu()
image_menu_item = new ImageMenuItem.from_stock(STOCK_NEW, null)
image_menu_item.activate += new_text
menu1.append(image_menu_item)
image_menu_item = new ImageMenuItem.from_stock(STOCK_OPEN, null)
image_menu_item.activate += on_open_clicked
menu1.append(image_menu_item)
separator_menu_item = new SeparatorMenuItem()
menu1.append(separator_menu_item)
image_menu_item = new ImageMenuItem.from_stock(STOCK_SAVE, null)
image_menu_item.activate += save_clicked
menu1.append(image_menu_item)
image_menu_item = new ImageMenuItem.from_stock(STOCK_SAVE_AS, null)
image_menu_item.activate += on_save_as_clicked
menu1.append(image_menu_item)
separator_menu_item = new SeparatorMenuItem()
menu1.append(separator_menu_item)
image_menu_item = new ImageMenuItem.from_stock(STOCK_PRINT_PREVIEW, null)
image_menu_item.activate += def (s)
initialize_printer("preview")
menu1.append(image_menu_item)
image_menu_item = new ImageMenuItem.from_stock(STOCK_PRINT, null)
image_menu_item.activate += def (s)
initialize_printer("print")
menu1.append(image_menu_item)
separator_menu_item = new SeparatorMenuItem()
menu1.append(separator_menu_item)
image_menu_item = new ImageMenuItem.from_stock(STOCK_QUIT, null)
image_menu_item.activate += exit_clicked
menu1.append(image_menu_item)
var launcher = new MenuItem.with_mnemonic(_("_File"))
launcher.set_submenu(menu1)
bar.append(launcher)
/* *************** */
var menu2 = new Menu()
image_menu_item2 = new ImageMenuItem.from_stock(STOCK_UNDO, null)
image_menu_item2.activate += undo_clicked
menu2.append(image_menu_item2)
image_menu_item2 = new ImageMenuItem.from_stock(STOCK_REDO, null)
image_menu_item2.activate += redo_clicked
menu2.append(image_menu_item2)
separator_menu_item = new SeparatorMenuItem()
menu2.append(separator_menu_item)
image_menu_item2 = new ImageMenuItem.from_stock(STOCK_CUT, null)
image_menu_item2.activate += cut_clipboard
menu2.append(image_menu_item2)
image_menu_item2 = new ImageMenuItem.from_stock(STOCK_COPY, null)
image_menu_item2.activate += copy_clipboard
menu2.append(image_menu_item2)
image_menu_item2 = new ImageMenuItem.from_stock(STOCK_PASTE, null)
image_menu_item2.activate += paste_clipboard
menu2.append(image_menu_item2)
separator_menu_item = new SeparatorMenuItem()
menu2.append(separator_menu_item)
image_menu_item2 = new ImageMenuItem.from_stock(STOCK_SELECT_ALL, null)
image_menu_item2.activate += select_all
menu2.append(image_menu_item2)
var launcher2 = new MenuItem.with_mnemonic(_("E_dit"))
launcher2.set_submenu(menu2)
bar.append(launcher2)
/* *************** */
var menu3 = new Menu()
check_menu_item = new CheckMenuItem.with_label(_("Show Toolbar"))
menu3.append(check_menu_item)
check_menu_item.set_active(true)
check_menu_item.toggled += on_check
separator_menu_item = new SeparatorMenuItem()
menu3.append(separator_menu_item)
check_menu_item2 = new CheckMenuItem.with_label(_("Insert spaces instead of tabs"))
menu3.append(check_menu_item2)
check_menu_item2.toggled += on_check2
separator_menu_item = new SeparatorMenuItem()
menu3.append(separator_menu_item)
check_menu_item3 = new CheckMenuItem.with_label(_("Highlight current line"))
menu3.append(check_menu_item3)
check_menu_item3.toggled += on_check3
separator_menu_item = new SeparatorMenuItem()
menu3.append(separator_menu_item)
check_menu_item4 = new CheckMenuItem.with_label(_("Show line number"))
menu3.append(check_menu_item4)
check_menu_item4.toggled += on_check4
separator_menu_item = new SeparatorMenuItem()
menu3.append(separator_menu_item)
check_menu_item5 = new CheckMenuItem.with_label(_("Highlight syntax"))
menu3.append(check_menu_item5)
check_menu_item5.set_active(true)
check_menu_item5.toggled += on_check5
/* *************** */
/* Extended Menu */
/* *************** */
var cmenu = new Menu()
var colormenu = new MenuItem.with_label(_("Coloration mode"))
colormenu.set_submenu(cmenu)
radio_menu_item = new RadioMenuItem.with_label(mylist, _("Text"))
cmenu.append(radio_menu_item)
radio_menu_item.toggled += def (s)
buf.language = manager.get_language ("text")
thedefaultsyntax = "text"
separator_menu_item = new SeparatorMenuItem()
cmenu.append(separator_menu_item)
radio_menu_item1 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Asp")
cmenu.append(radio_menu_item1)
radio_menu_item1.toggled += def (s)
buf.language = manager.get_language ("asp")
thedefaultsyntax = "asp"
radio_menu_item1 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "C")
cmenu.append(radio_menu_item1)
radio_menu_item1.toggled += def (s)
buf.language = manager.get_language ("c")
thedefaultsyntax = "c"
radio_menu_item2 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "C++")
cmenu.append(radio_menu_item2)
radio_menu_item2.toggled += def (s)
buf.language = manager.get_language ("cpp")
thedefaultsyntax = "cpp"
radio_menu_item3 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "C#")
cmenu.append(radio_menu_item3)
radio_menu_item3.toggled += def (s)
buf.language = manager.get_language ("csharp")
thedefaultsyntax = "csharp"
radio_menu_item18 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "CSS")
cmenu.append(radio_menu_item18)
radio_menu_item18.toggled += def (s)
buf.language = manager.get_language ("css")
thedefaultsyntax = "css"
radio_menu_item19 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Diff")
cmenu.append(radio_menu_item19)
radio_menu_item19.toggled += def (s)
buf.language = manager.get_language ("diff")
thedefaultsyntax = "diff"
radio_menu_item4 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Genie")
cmenu.append(radio_menu_item4)
radio_menu_item4.toggled += def (s)
buf.language = manager.get_language ("genie")
thedefaultsyntax = "genie"
radio_menu_item14 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "HTML")
cmenu.append(radio_menu_item14)
radio_menu_item14.toggled += def (s)
buf.language = manager.get_language ("html")
thedefaultsyntax = "html"
radio_menu_item5 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Java")
cmenu.append(radio_menu_item5)
radio_menu_item5.toggled += def (s)
buf.language = manager.get_language ("java")
thedefaultsyntax = "java"
radio_menu_item15 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Java Script")
cmenu.append(radio_menu_item15)
radio_menu_item15.toggled += def (s)
buf.language = manager.get_language ("javascript")
thedefaultsyntax = "javascript"
radio_menu_item16 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "LaTex")
cmenu.append(radio_menu_item16)
radio_menu_item16.toggled += def (s)
buf.language = manager.get_language ("latex")
thedefaultsyntax = "latex"
radio_menu_item22 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Lua")
cmenu.append(radio_menu_item22)
radio_menu_item22.toggled += def (s)
buf.language = manager.get_language ("lua")
thedefaultsyntax = "lua"
radio_menu_item6 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Pascal")
cmenu.append(radio_menu_item6)
radio_menu_item6.toggled += def (s)
buf.language = manager.get_language ("pascal")
thedefaultsyntax = "pascal"
radio_menu_item7 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Perl")
cmenu.append(radio_menu_item7)
radio_menu_item7.toggled += def (s)
buf.language = manager.get_language ("perl")
thedefaultsyntax = "perl"
radio_menu_item8 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "PHP")
cmenu.append(radio_menu_item8)
radio_menu_item8.toggled += def (s)
buf.language = manager.get_language ("php")
thedefaultsyntax = "php"
radio_menu_item24 = new RadioMenuItem.with_label(radio_menu_item.get_group(), _("Lang File"))
cmenu.append(radio_menu_item24)
radio_menu_item24.toggled += def (s)
buf.language = manager.get_language ("gettext-translation")
thedefaultsyntax = "gettext-translation"
radio_menu_item9 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Python")
cmenu.append(radio_menu_item9)
radio_menu_item9.toggled += def (s)
buf.language = manager.get_language ("python")
thedefaultsyntax = "python"
radio_menu_item21 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "R")
cmenu.append(radio_menu_item21)
radio_menu_item21.toggled += def (s)
buf.language = manager.get_language ("r")
thedefaultsyntax = "r"
radio_menu_item10 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Sh") /*BK*/
cmenu.append(radio_menu_item10)
radio_menu_item10.toggled += def (s)
buf.language = manager.get_language ("sh")
thedefaultsyntax = "sh"
radio_menu_item11 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "SQL")
cmenu.append(radio_menu_item11)
radio_menu_item11.toggled += def (s)
buf.language = manager.get_language ("sql")
thedefaultsyntax = "sql"
radio_menu_item17 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Tcl")
cmenu.append(radio_menu_item17)
radio_menu_item17.toggled += def (s)
buf.language = manager.get_language ("tcl")
thedefaultsyntax = "tcl"
radio_menu_item12 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "Vala")
cmenu.append(radio_menu_item12)
radio_menu_item12.toggled += def (s)
buf.language = manager.get_language ("vala")
thedefaultsyntax = "vala"
radio_menu_item23 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "VB.NET")
cmenu.append(radio_menu_item23)
radio_menu_item23.toggled += def (s)
buf.language = manager.get_language ("vbnet")
thedefaultsyntax = "vbnet"
radio_menu_item13 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "XML")
cmenu.append(radio_menu_item13)
radio_menu_item13.toggled += def (s)
buf.language = manager.get_language ("xml")
thedefaultsyntax = "xml"
radio_menu_item20 = new RadioMenuItem.with_label(radio_menu_item.get_group(), "XSLT")
cmenu.append(radio_menu_item20)
radio_menu_item20.toggled += def (s)
buf.language = manager.get_language ("xslt")
thedefaultsyntax = "xslt"
menu3.append(colormenu)
/* ***************** */
/* End Extended Menu */
/* ***************** */
var launcher3 = new MenuItem.with_mnemonic(_("_View"))
launcher3.set_submenu(menu3)
bar.append(launcher3)
/* *************** */
var menu4 = new Menu()
image_menu_item4 = new ImageMenuItem.from_stock(STOCK_FIND, null)
image_menu_item4.activate += searchdialog
menu4.append(image_menu_item4)
separator_menu_item = new SeparatorMenuItem()
menu4.append(separator_menu_item)
image_menu_item4 = new ImageMenuItem.from_stock(STOCK_JUMP_TO, null)
image_menu_item4.activate += go_to_line_window
menu4.append(image_menu_item4)
var launcher4 = new MenuItem.with_mnemonic(_("_Search"))
launcher4.set_submenu(menu4)
bar.append(launcher4)
/* *************** */
var menu5 = new Menu()
menu_item = new MenuItem.with_label(_("Change Background Color"))
menu_item.activate += change_background_color
menu5.append(menu_item)
menu_item = new MenuItem.with_label(_("Change Text Color"))
menu_item.activate += change_text_color
menu5.append(menu_item)
menu_item = new MenuItem.with_label(_("Change Font"))
menu_item.activate += change_font
menu5.append(menu_item)
separator_menu_item = new SeparatorMenuItem()
menu5.append(separator_menu_item)
menu5.append(menu_item = new MenuItem.with_label(_("Document Stats")))
menu_item.activate += info_clicked
var launcher5 = new MenuItem.with_mnemonic(_("_Tools"))
launcher5.set_submenu(menu5)
bar.append(launcher5)
/* *************** */
var menu6 = new Menu()
image_menu_item5 = new ImageMenuItem.from_stock(STOCK_ABOUT, null)
image_menu_item5.activate += about_clicked
menu6.append(image_menu_item5)
var launcher6 = new MenuItem.with_label("About") /*BK("?")*/
launcher6.set_submenu(menu6)
bar.append(launcher6)

/* Toolbar */
toolbar = new Toolbar ()
var separator = new SeparatorToolItem ()
var separator2 = new SeparatorToolItem ()
var separator3 = new SeparatorToolItem ()
var separator4 = new SeparatorToolItem ()
var separator5 = new SeparatorToolItem ()
var new_button = new ToolButton.from_stock (STOCK_NEW)
var open_button = new ToolButton.from_stock (STOCK_OPEN)
var save_button = new ToolButton.from_stock (STOCK_SAVE)
var print_button = new ToolButton.from_stock (STOCK_PRINT)
var undo_button = new ToolButton.from_stock (STOCK_UNDO)
var redo_button = new ToolButton.from_stock (STOCK_REDO)
var cut_button = new ToolButton.from_stock (STOCK_CUT)
var copy_button = new ToolButton.from_stock (STOCK_COPY)
var paste_button = new ToolButton.from_stock (STOCK_PASTE)
var find_button = new ToolButton.from_stock (STOCK_FIND)
fullscreen_button = new ToolButton.from_stock (STOCK_FULLSCREEN)
toolbar.add (new_button)
toolbar.add (open_button)
toolbar.add (save_button)
toolbar.add (separator)
toolbar.add (print_button)
toolbar.add (separator2)
toolbar.add (undo_button)
toolbar.add (redo_button)
toolbar.add (separator3)
toolbar.add (cut_button)
toolbar.add (copy_button)
toolbar.add (paste_button)
toolbar.add (separator4)
toolbar.add (find_button)
toolbar.add (separator5)
/*BKtoolbar.add (fullscreen_button)*/
new_button.clicked += new_text
open_button.clicked += on_open_clicked
save_button.clicked += save_clicked
print_button.clicked += def (s)
initialize_printer("print")
undo_button.clicked += undo_clicked
redo_button.clicked += redo_clicked
cut_button.clicked += cut_clipboard
copy_button.clicked += copy_clipboard
paste_button.clicked += paste_clipboard
find_button.clicked += searchdialog
/*BKfullscreen_button.clicked += fullscreenmode*/

/* Textview */
manager = new SourceLanguageManager()
buf = new SourceBuffer(null)
text_view = new SourceView.with_buffer(buf)
text_view.set_insert_spaces_instead_of_tabs(false)
text_view.buffer.changed += text_changed
buf.set_highlight_syntax (true)

/* Scrolled Window */
var scroll = new ScrolledWindow (null, null)
scroll.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC)
scroll.add (text_view)

/* Status Bar */
status_bar = new Statusbar ()
status_bar.set_has_resize_grip (false)
status_bar.pop(0)
status_bar.push(0, (_("New file")))
status_bar2 = new Statusbar ()
status_bar2.set_has_resize_grip (false)
status_bar2.pop(0)

/* HBox */
status_hbox = new HBox (false, 0)
status_hbox.pack_start (status_bar, true, true, 0)
status_hbox.pack_start (status_bar2, true, true, 0)

/* VBox */
var vbox = new VBox (false, 0)
vbox.pack_start (bar, false, true, 0)
vbox.pack_start (toolbar, false, true, 0)
vbox.pack_start (scroll, true, true, 0)
vbox.pack_start (status_hbox, false, true, 0)
add (vbox)

/* ********************************* */
/* Save Settings Function */
/* ********************************* */

def save_stats ()
width : int
height : int
get_size (out width, out height)
try
var file = File.new_for_path(Environment.get_home_dir() + "/.nicoedit.rc")
var renamed = file.set_display_name ("samplefile.bak", null)
var thefile_stream = file.create (FileCreateFlags.NONE, null)
var data_stream = new DataOutputStream (thefile_stream)
data_stream.put_string ("default_window_height=" + height.to_string() + "|default_window_width=" + width.to_string() + "|viewtoolbar=" + viewtoolbar + "|linenumber=" + linenumber + "|insertspace=" + insertspace + "|highlightcurrentline=" + highlightcurrentline +"|highlightsyntax=" + highlightsyntax + "|defaultsyntax=" + thedefaultsyntax + "|backgroundcolor=" + thebackgroundcolor + "|textcolor=" + thetextcolor + "|font=" + thefont, null)
renamed.delete(null)
except e : GLib.Error
print "Unable to save stats"

/* ********************************* */
/* Restore Settings Function */
/* ********************************* */

def restore_stats ()
try
text : string
length : ulong
FileUtils.get_contents (Environment.get_home_dir() + "/.nicoedit.rc", out text, out length)
var textlist = text.split("|")
for var s in textlist
if s.contains("insertspace")
var s1 = s.split("=")
var value = s1[1].strip()
if value == "true"
check_menu_item2.set_active(true)
text_view.set_insert_spaces_instead_of_tabs(true)
else
check_menu_item2.set_active(false)
text_view.set_insert_spaces_instead_of_tabs(false)
insertspace = value
if s.contains("highlightsyntax")
var s2 = s.split("=")
var value = s2[1].strip()
if value == "true"
check_menu_item5.set_active(true)
buf.set_highlight_syntax (true)
else
check_menu_item5.set_active(false)
buf.set_highlight_syntax (false)
highlightsyntax = value
if s.contains("viewtoolbar")
var s3 = s.split("=")
var value = s3[1].strip()
if value == "true"
check_menu_item.set_active(true)
toolbar.show()
else
check_menu_item.set_active(false)
toolbar.hide()
viewtoolbar = value
if s.contains("linenumber")
var s4 = s.split("=")
var value = s4[1].strip()
if value == "true"
check_menu_item4.set_active(true)
text_view.set_show_line_numbers(true)
else
check_menu_item4.set_active(false)
text_view.set_show_line_numbers(false)
linenumber = value
if s.contains("highlightcurrentline")
var s5 = s.split("=")
var value = s5[1].strip()
if value == "true"
check_menu_item3.set_active(true)
text_view.set_highlight_current_line(true)
else
check_menu_item3.set_active(false)
text_view.set_highlight_current_line(false)
highlightcurrentline = value
if s.contains("backgroundcolor")
var s6 = s.split("=")
var value = s6[1].strip()
thebackgroundcolor = value
Gdk.Color.parse (value, out color)
text_view.modify_base (Gtk.StateType.NORMAL, color)
if s.contains("textcolor")
var s7 = s.split("=")
var value = s7[1].strip()
thetextcolor = value
Gdk.Color.parse (value, out color)
text_view.modify_text (Gtk.StateType.NORMAL, color)
if s.contains("font")
var s8 = s.split("=")
var value = s8[1].strip()
thefont = value
font_desc = FontDescription.from_string(value)
text_view.modify_font(font_desc)
if s.contains("defaultsyntax")
var s9 = s.split("=")
var value = s9[1].strip()
thedefaultsyntax = value
check_correct_lang(value)
except e : GLib.Error
thebackgroundcolor = "#ffffffffffff"
thetextcolor = "#000000000000"
thefont = "Sans 12"
linenumber = "false"
insertspace = "false"
highlightcurrentline = "false"
highlightsyntax = "true"
viewtoolbar = "true"
thedefaultsyntax = "text"
var file = File.new_for_path(Environment.get_home_dir() + "/.nicoedit.rc")
try
var thefile_stream = file.create (FileCreateFlags.NONE, null)
var data_stream = new DataOutputStream (thefile_stream)
try
data_stream.put_string ("default_window_height=560|default_window_width=792|viewtoolbar=true|insertspace=false|highlightcurrentline=false|linenumber=false|highlightsyntax=false|defaultsyntax=text|backgroundcolor=#ffffffffffff|textcolor=#000000000000|font= Sans 12", null)
except e : GLib.Error
print "Unable to put string in text file"
except e : GLib.Error
print "Unable to create stream for file"

def restore_window_size ()
try
text : string
length : ulong
FileUtils.get_contents (Environment.get_home_dir() + "/.nicoedit.rc", out text, out length)
var textlist = text.split("|")
for var s in textlist
if s.contains("window_height=")
var s1 = s.split("=")
default_window_height = s1[1].strip().to_int()
if s.contains("window_width")
var s2 = s.split("=")
default_window_width = s2[1].strip().to_int()
except e : GLib.Error
default_window_height = 560
default_window_width = 792 /*BK1080*/

/* ********************************* */
/* Autodetect Syntax Function */
/* ********************************* */

def auto_detect_syntax (filename : string)
theextension : string = filename.rchr (-1, '.')
if theextension is not null
theextension = theextension.offset (1).down()
if theextension is "gs"
check_correct_lang ("genie")
if theextension is "h"
check_correct_lang ("c")
if theextension is "hpp"
check_correct_lang ("cpp")
if theextension is "php3"
check_correct_lang ("php")
if theextension is "htm"
check_correct_lang ("html")
if theextension is "patch"
check_correct_lang ("diff")
if theextension is "R"
check_correct_lang ("r")
if theextension is "sh"
check_correct_lang ("sh")
if theextension is "vb"
check_correct_lang ("vb.net")
if theextension is "po"
check_correct_lang ("gettext-translation")
if theextension is "pot"
check_correct_lang ("gettext-translation")
if theextension is "py"
check_correct_lang ("python")
else
check_correct_lang (theextension)

/*BK 'else' for above gave syntax error, do it this way...*/
if theextension is null
var f = FileStream.open(filename,"r")
var a = new array of char[4096]
f.gets(a)
a[a.length - 1] = 0 /*make it null-terminated*/
var s = (string)a /*caste array-of-char to a string*/
if s.contains("bin/sh") do check_correct_lang ("sh")
if s.contains("bin/ash") do check_correct_lang ("sh")
if s.contains("bin/bash") do check_correct_lang ("sh")
if s.contains("bin/perl") do check_correct_lang ("perl")
if s.contains("bin/python") do check_correct_lang ("python")

def check_correct_lang (value : string)
a : string = value
case a
when "asp"
radio_menu_item.set_active(true)
when "c"
radio_menu_item1.set_active(true)
when "cpp"
radio_menu_item2.set_active(true)
when "csharp"
radio_menu_item3.set_active(true)
when "css"
radio_menu_item18.set_active(true)
when "diff"
radio_menu_item19.set_active(true)
when "genie"
radio_menu_item4.set_active(true)
when "html"
radio_menu_item14.set_active(true)
when "java"
radio_menu_item5.set_active(true)
when "javascript"
radio_menu_item15.set_active(true)
when "latext"
radio_menu_item16.set_active(true)
when "lua"
radio_menu_item22.set_active(true)
when "pascal"
radio_menu_item6.set_active(true)
when "perl"
radio_menu_item7.set_active(true)
when "php"
radio_menu_item8.set_active(true)
when "gettext-translation"
radio_menu_item24.set_active(true)
when "python"
radio_menu_item9.set_active(true)
when "r"
radio_menu_item21.set_active(true)
when "sh" /*BK*/
radio_menu_item10.set_active(true)
when "sql"
radio_menu_item11.set_active(true)
when "tcl"
radio_menu_item17.set_active(true)
when "vala"
radio_menu_item12.set_active(true)
when "vb.net"
radio_menu_item23.set_active(true)
when "xml"
radio_menu_item13.set_active(true)
when "xslt"
radio_menu_item20.set_active(true)

/* ********************************* */
/* Some Functions */
/* ********************************* */

def on_check ()
checked : bool = check_menu_item.get_active()
if checked is true
toolbar.show()
viewtoolbar = "true"
else
toolbar.hide()
viewtoolbar = "false"

def on_check2 ()
checked : bool = check_menu_item2.get_active()
if checked is true
text_view.set_insert_spaces_instead_of_tabs(true)
insertspace = "true"
else
text_view.set_insert_spaces_instead_of_tabs(false)
insertspace = "false"

def on_check3 ()
checked : bool = check_menu_item3.get_active()
if checked is true
buf.language = manager.get_language (thedefaultsyntax)
text_view.set_highlight_current_line(true)
highlightcurrentline = "true"
else
text_view.set_highlight_current_line(false)
highlightcurrentline = "false"

def on_check4 ()
checked : bool = check_menu_item4.get_active()
if checked is true
text_view.set_show_line_numbers(true)
linenumber = "true"
else
text_view.set_show_line_numbers(false)
linenumber = "false"

def on_check5 ()
checked : bool = check_menu_item5.get_active()
if checked is true
buf.set_highlight_syntax (true)
highlightsyntax = "true"
else
buf.set_highlight_syntax (false)
highlightsyntax = "false"

/* ********************************* */
/* Fullscreen Function */
/* ********************************* */

def fullscreenmode ()
if activatedfullscreen is false
fullscreen ()
bar.hide()
activatedfullscreen = true
fullscreen_button.set_stock_id (STOCK_LEAVE_FULLSCREEN)
else
unfullscreen ()
bar.show()
activatedfullscreen = false
fullscreen_button.set_stock_id (STOCK_FULLSCREEN)

/* ********************************* */
/* Text Change Function */
/* ********************************* */

def text_changed ()
document_modified = true
status_bar2.push(0, (_("Unregistered")))

/* ********************************* */
/* New Text Function */
/* ********************************* */

def new_text ()
if document_modified is true
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, (_("The file is unregistered, do you want to save it ?")))
messagedialog.add_button (STOCK_CANCEL, ResponseType.CANCEL)
var response = messagedialog.run()
if response is ResponseType.YES
messagedialog.destroy()
save_clicked ()
clear_text()
else if response is ResponseType.NO
clear_text()
messagedialog.destroy()
else if response is ResponseType.CANCEL
messagedialog.destroy()
else
clear_text()

/* ********************************* */
/* Clear Text Function */
/* ********************************* */

def clear_text()
text_view.buffer.set_text ("", 0)
status_bar.push(0, _("New file"))
status_bar2.push(0, "")
document_modified = false
thefile = ""

/* ********************************* */
/* Some Functions */
/* ********************************* */

def select_all ()
text_view.select_all (true)

def undo_clicked ()
text_view.undo()

def redo_clicked ()
text_view.redo()

def cut_clipboard ()
text_view.cut_clipboard()

def copy_clipboard ()
text_view.copy_clipboard()

def paste_clipboard ()
text_view.paste_clipboard()

def current_line () : int
iter : TextIter
text_view.buffer.get_iter_at_mark (out iter, text_view.buffer.get_insert ())
return iter.get_line()

def line_count () : int
return text_view.buffer.get_line_count ()

def info_clicked ()
var messagedialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, (_("Your text have ")) + line_count ().to_string() + (_(" line(s)")))
messagedialog.run()
messagedialog.destroy()

/* ********************************* */
/* Search Function */
/* ********************************* */

def searchdialog()
search_dialog = new Dialog()
search_dialog.title = "Find"
search_dialog.set_default_size (300, 50)
search_dialog.position = WindowPosition.CENTER
search_dialog.has_separator = false
search_dialog.border_width = 5
search_dialog.set_deletable(false)
search_dialog.set_resizable(false)
search_dialog.set_skip_taskbar_hint(true)
search_entry = new Entry ()
var search_label = new Label.with_mnemonic (_("_Search for:"))
search_label.mnemonic_widget = this.search_entry
this.match_case = new CheckButton.with_mnemonic (_("_Match case"))
this.find_backwards = new CheckButton.with_mnemonic (_("Find _backwards"))
var hbox = new HBox (false, 20)
hbox.pack_start (search_label, false, true, 0)
hbox.pack_start (this.search_entry, true, true, 0)
search_dialog.vbox.pack_start (hbox, false, true, 0)
search_dialog.vbox.pack_start (this.match_case, false, true, 0)
search_dialog.vbox.pack_start (this.find_backwards, false, true, 0)
search_dialog.vbox.spacing = 10
search_dialog.add_button (STOCK_HELP, ResponseType.HELP)
search_dialog.add_button (STOCK_CLOSE, ResponseType.CLOSE)
search_dialog.add_button (STOCK_FIND, ResponseType.APPLY)
search_dialog.response += on_response
search_dialog.show_all ()
search_dialog.run()

def on_response(response_id : int)
if response_id is ResponseType.CLOSE
search_dialog.hide()
if response_id is ResponseType.APPLY
findtext()
if response_id is ResponseType.HELP
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE, (_("Type word and click 'Find'")))
messagedialog.run()
messagedialog.destroy()

def findtext ()
match_start : TextIter
match_end : TextIter
start_iter : TextIter
end_iter : TextIter
iter : TextIter
found : bool = false
text_view.buffer.get_start_iter (out start_iter)
text_view.buffer.get_end_iter (out end_iter)
text_view.buffer.get_selection_bounds (null, out iter)
if match_case.get_active() is true
search_flags |= SourceSearchFlags.CASE_INSENSITIVE | SourceSearchFlags.TEXT_ONLY
if find_backwards.get_active() is true
found = source_iter_backward_search (iter , search_entry.get_text(), search_flags, out match_start, out match_end, null)
text_view.buffer.place_cursor (iter)
text_view.scroll_to_mark (text_view.buffer.get_insert (), 0.25, false, 0.0, 0.0)
else
found = source_iter_forward_search (iter , search_entry.get_text(), search_flags, out match_start, out match_end, null)
text_view.buffer.place_cursor (iter)
text_view.scroll_to_mark (text_view.buffer.get_insert (), 0.25, false, 0.0, 0.0)
if found is true
text_view.buffer.select_range(match_start , match_end)
else if found is false
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, (_("Unable to find this word")))
messagedialog.run()
messagedialog.destroy()
search_entry.set_text("")

/* ********************************* */
/* GoToLine Function */
/* ********************************* */

def go_to_line_window ()
goto_window = new Dialog()
goto_window.title = (_("Go to line :"))
goto_window.set_default_size (300, 50)
goto_window.position = WindowPosition.CENTER
goto_window.has_separator = false
goto_window.border_width = 5
goto_window.set_deletable(false)
goto_window.set_resizable(false)
goto_window.set_skip_taskbar_hint(true)
gototext = new Entry()
var okbutton = new Button.with_label (_("Go!"))
okbutton.clicked += def (s)
go_to_line(gototext.get_text().to_int())
goto_window.hide()
var hbox = new HBox (false, 0)
hbox.pack_start (gototext, true, true, 0)
hbox.pack_start (okbutton, false, true, 0)
goto_window.vbox.pack_start (hbox, false, true, 0)
goto_window.show_all()
goto_window.run()

def go_to_line (line_number : int)
iter : TextIter
text_view.buffer.get_iter_at_line (out iter, line_number)
iter.set_line (line_number - 1)
text_view.buffer.place_cursor (iter)
text_view.scroll_to_iter (iter, 0, false, 0, 0)

/* ********************************* */
/* Change Background Color Function */
/* ********************************* */

def change_background_color ()
newcolor = new ColorSelectionDialog (_("Choose your prefered background color:"))
var response = newcolor.run()
if response is ResponseType.OK
var colorsel = (ColorSelection) newcolor.colorsel
colorsel.get_current_color (out color)
thebackgroundcolor = color.to_string()
text_view.modify_base (Gtk.StateType.NORMAL, color)
newcolor.destroy()
if response is ResponseType.CANCEL
newcolor.destroy()
if response is ResponseType.HELP
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE, (_("Choose your color and click 'Ok'")))
messagedialog.run()
messagedialog.destroy()

/* ********************************* */
/* Change Text Color Function */
/* ********************************* */

def change_text_color ()
newcolor = new ColorSelectionDialog (_("Choose your prefered text color:"))
var response = newcolor.run()
if response is ResponseType.OK
var colorsel = (ColorSelection) newcolor.colorsel
colorsel.get_current_color (out color)
thetextcolor = color.to_string()
text_view.modify_text (Gtk.StateType.NORMAL, color)
newcolor.destroy()
if response is ResponseType.CANCEL
newcolor.destroy()
if response is ResponseType.HELP
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE, (_("Choose your color and click 'Ok'")))
messagedialog.run()
messagedialog.destroy()

/* ********************************* */
/* Change Font Function */
/* ********************************* */

def change_font ()
newfont = new FontSelectionDialog(_("Select your prefered font:"))
var response = newfont.run()
if response is ResponseType.OK
font_desc = FontDescription.from_string(newfont.get_font_name())
thefont = font_desc.to_string()
text_view.modify_font(font_desc)
newfont.destroy()
if response is ResponseType.CANCEL
newfont.destroy()

/* ********************************* */
/* Print Function */
/* ********************************* */

def initialize_printer (theaction : string)
op = new PrintOperation ()
thecompositor = new SourcePrintCompositor.from_view(text_view)
context = new PrintContext ()
var paper_size = new PaperSize(PAPER_NAME_A4)
var setup = new PageSetup()
setup.set_paper_size(paper_size)
op.set_default_page_setup(setup)
op.set_unit(Unit.MM)
thecompositor.set_body_font_name("Mono 9")
thecompositor.set_left_margin(0.5, Unit.MM)
thecompositor.set_right_margin(0.5, Unit.MM)
thecompositor.set_top_margin(0.5, Unit.MM)
thecompositor.set_bottom_margin(0.5, Unit.MM)
if linenumber is "true"
thecompositor.set_print_line_numbers(1)
if highlightsyntax is "true"
thecompositor.set_highlight_syntax(true)
else if highlightsyntax is "false"
thecompositor.set_highlight_syntax(false)
op.paginate += paginate
op.draw_page += draw_page
if theaction is "preview"
op.run (PrintOperationAction.PREVIEW, null)
else if theaction is "print"
op.run (PrintOperationAction.PRINT_DIALOG, null)

def draw_page (context : PrintContext, page_nr : int)
thecompositor.draw_page (context, page_nr)

def paginate (context : PrintContext) : bool
if (thecompositor.paginate (context))
n_pages : int
n_pages = thecompositor.get_n_pages()
op.set_n_pages (n_pages)
return true
return false


/* ********************************* */
/* Exit Function */
/* ********************************* */

def exit_clicked ()
exit_program()

def exit_program () : bool
if document_modified is true
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, (_("The file is unregistered, do you want to save it ?")))
messagedialog.add_button (STOCK_CANCEL, ResponseType.CANCEL)
var response = messagedialog.run()
if response is ResponseType.YES
messagedialog.destroy()
save_clicked ()
if cancelclicked is false
save_stats ()
Gtk.main_quit()
else if response is ResponseType.NO
messagedialog.destroy()
save_stats ()
Gtk.main_quit()
else if response is ResponseType.CANCEL
messagedialog.destroy()
return true
else
save_stats ()
Gtk.main_quit()
return true

def save_clicked ()
if thefile is ""
on_save_as_clicked ()
else
save_file()

/* ********************************* */
/* Save Function */
/* ********************************* */

def on_save_as_clicked ()
var file_chooser = new FileChooserDialog ((_("Save File")), this, FileChooserAction.SAVE, STOCK_CANCEL, ResponseType.CANCEL, STOCK_SAVE, ResponseType.ACCEPT, null)
var response = file_chooser.run ()
if response is ResponseType.ACCEPT
save_new_file (file_chooser.get_filename ())
cancelclicked = false
else if response is ResponseType.CANCEL
cancelclicked = true
file_chooser.destroy ()

def save_new_file (filename : string)
end, start : TextIter
text_view.buffer.get_bounds (out start, out end)
try
var file = File.new_for_path(filename)
var thefile_stream = file.create (FileCreateFlags.NONE, null)
var data_stream = new DataOutputStream (thefile_stream)
data_stream.put_string (text_view.buffer.get_text(start, end, false), null)
thefile = filename
open_file(filename)
except e : GLib.Error
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, (_("Unable to save file !")))
messagedialog.run()
messagedialog.destroy()

def save_file()
filename : string = thefile
end, start : TextIter
text_view.buffer.get_bounds (out start, out end)
try
var file = File.new_for_path(thefile)
var renamed = file.set_display_name ("samplefile.bak", null)
var thefile_stream = file.create (FileCreateFlags.NONE, null)
var data_stream = new DataOutputStream (thefile_stream)
data_stream.put_string (text_view.buffer.get_text(start, end, false), null)
renamed.delete(null)
thefile = filename
open_file(filename)
except e : GLib.Error
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, (_("Unable to save file !")))
messagedialog.run()
messagedialog.destroy()

def on_open_clicked ()
if document_modified is true
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, (_("The file is unregistered, do you want to save it ?")))
var response = messagedialog.run()
if response is ResponseType.YES
save_clicked ()
messagedialog.destroy()
else if response is ResponseType.NO
open_file_now()
messagedialog.destroy()
else
open_file_now()

/* ********************************* */
/* Open Function */
/* ********************************* */

def open_file_now ()
var file_chooser = new FileChooserDialog ((_("Open File")), this, FileChooserAction.OPEN, STOCK_CANCEL, ResponseType.CANCEL, STOCK_OPEN, ResponseType.ACCEPT, null)
if (file_chooser.run () == ResponseType.ACCEPT)
open_file (file_chooser.get_filename ())
file_chooser.destroy ()

def open_file (filename : string)
try
text : string
length : ulong
FileUtils.get_contents (filename, out text, out length)
test1 : int = length.to_string().to_int()
text_view.buffer.set_text (text, test1)
go_to_line(1)
status_bar.push(0, (_("You are editing: ")) + filename)
document_modified = false
thefile = filename
auto_detect_syntax(filename)
status_bar2.push(0, "")
except e : GLib.Error
messagedialog = new MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, (_("Unable to open file !")))
messagedialog.run()
messagedialog.destroy()

/* ********************************* */
/* About Dialog */
/* ********************************* */

def about_clicked ()
var about = new AboutDialog ()
about.set_version(Config.VERSION)
about.set_program_name("NicoEdit")
about.set_comments(_("Edit your text file with syntax highlighting !"))
about.set_copyright("Nicolas alias nikobordx, 2009, GPL licence")
about.run()
about.hide()


There is something wrong with the automatic sysntax selection though -- I opened a Perl file and it did not get set to Perl syntax -- there was an error message on the console -- there seems to be a problem with how gtksourceview is being used.

Nicholas has moved the project to the 'waf' build system, but I have created a script 'bk_compile' to compile. That script and all other required files are in /usr/local/NicoEdit. I have uploaded the PET package 'nicoedit-1.5.1-pup4.pet' to here:

http://distro.ibiblio.org/pub/linux/distributions/puppylinux/pet_packages-woof/


Posted on 23 Jun 2009, 14:27


Comments:

Posted on 25 Jun 2009, 4:36 by Nicolas
NicoEdit
New version of NicoEdit !

http://www.murga-linux.com/puppy/viewtopic.php?mode=attach&id=19522

Nicolas.


Posted on 6 Jul 2009, 17:41 by Nicolas
NicoEdit
Hi All,

New little version.

News:
Added a script for building in Puppy Linux (see README)
Added a .desktop file
If your file is executable, when you save it, the file keep this permission

Download it here: http://www.murga-linux.com/puppy/viewtopic.php?mode=attach&id=19803

See you,
Nicolas.