1 /****************************************************************************
4 * Copyright 2006 Naba Kumar <naba@gnome.org>
5 ****************************************************************************/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <libanjuta/anjuta-debug.h>
23 #include <libanjuta/anjuta-preferences.h>
24 #include <libanjuta/interfaces/ianjuta-file.h>
25 #include <libanjuta/interfaces/ianjuta-file-savable.h>
26 #include <libanjuta/interfaces/ianjuta-editor.h>
29 #include "anjuta-vim.h"
36 struct _AnjutaVimPrivate
42 static void anjuta_vim_class_init(AnjutaVimClass
*klass
);
43 static void anjuta_vim_instance_init(AnjutaVim
*av
);
44 static void anjuta_vim_finalize(GObject
*object
);
46 static GObjectClass
*parent_class
= NULL
;
50 /* Update Monitor on load/save */
53 anjuta_vim_instance_init(AnjutaVim
* av
)
55 av
->priv
= g_new0(AnjutaVimPrivate
, 1);
59 anjuta_vim_class_init(AnjutaVimClass
*klass
)
61 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
63 parent_class
= g_type_class_peek_parent(klass
);
64 object_class
->finalize
= anjuta_vim_finalize
;
68 anjuta_vim_finalize(GObject
*object
)
71 cobj
= ANJUTA_VIM(object
);
73 /* Free private members, etc. */
74 g_free (cobj
->priv
->uri
);
75 g_free (cobj
->priv
->filename
);
78 G_OBJECT_CLASS(parent_class
)->finalize(object
);
81 /* Create a new anjuta_vim instance. If uri is valid,
82 the file will be loaded in the buffer */
85 anjuta_vim_new (const gchar
* uri
, const gchar
* filename
, AnjutaPlugin
* plugin
)
89 AnjutaVim
*av
= ANJUTA_VIM (g_object_new (ANJUTA_TYPE_VIM
, NULL
));
91 if (uri
!= NULL
&& strlen(uri
) > 0)
93 ianjuta_file_open (IANJUTA_FILE(av
), uri
, NULL
);
95 else if (filename
!= NULL
&& strlen(filename
) > 0)
96 av
->priv
->filename
= g_strdup(filename
);
101 /* IAnjutaFile interface */
103 /* Open uri in Editor */
105 ifile_open (IAnjutaFile
* file
, const gchar
*uri
, GError
** e
)
107 AnjutaVim
* av
= ANJUTA_VIM(file
);
108 g_free (av
->priv
->uri
);
109 g_free (av
->priv
->filename
);
110 av
->priv
->uri
= g_strdup (uri
);
111 av
->priv
->filename
= g_path_get_basename (uri
);
112 gtk_vim_edit (GTK_VIM (av
), uri
, NULL
);
115 /* Return the currently loaded uri */
118 ifile_get_uri (IAnjutaFile
* file
, GError
** e
)
120 AnjutaVim
* av
= ANJUTA_VIM(file
);
121 return g_strdup (av
->priv
->uri
);
124 /* IAnjutaFileSavable interface */
128 ifile_savable_save (IAnjutaFileSavable
* file
, GError
** e
)
130 AnjutaVim
* av
= ANJUTA_VIM(file
);
135 ifile_savable_save_as (IAnjutaFileSavable
* file
, const gchar
*uri
, GError
** e
)
137 AnjutaVim
* av
= ANJUTA_VIM(file
);
141 ifile_savable_set_dirty (IAnjutaFileSavable
* file
, gboolean dirty
, GError
** e
)
143 AnjutaVim
* av
= ANJUTA_VIM(file
);
147 ifile_savable_is_dirty (IAnjutaFileSavable
* file
, GError
** e
)
149 AnjutaVim
* av
= ANJUTA_VIM(file
);
154 isavable_iface_init (IAnjutaFileSavableIface
*iface
)
156 iface
->save
= ifile_savable_save
;
157 iface
->save_as
= ifile_savable_save_as
;
158 iface
->set_dirty
= ifile_savable_set_dirty
;
159 iface
->is_dirty
= ifile_savable_is_dirty
;
163 ifile_iface_init (IAnjutaFileIface
*iface
)
165 iface
->open
= ifile_open
;
166 iface
->get_uri
= ifile_get_uri
;
169 /* IAnjutaEditor interface */
172 static void ieditor_goto_line(IAnjutaEditor
*editor
, gint line
, GError
**e
)
174 AnjutaVim
* av
= ANJUTA_VIM(editor
);
177 /* Scroll to position */
178 static void ieditor_goto_position(IAnjutaEditor
*editor
, gint position
, GError
**e
)
180 AnjutaVim
* av
= ANJUTA_VIM(editor
);
183 /* Return a newly allocated pointer containing the whole text */
184 static gchar
* ieditor_get_text(IAnjutaEditor
* editor
, gint start
,
185 gint end
, GError
**e
)
187 AnjutaVim
* av
= ANJUTA_VIM(editor
);
191 /* TODO! No documentation, so we do nothing */
192 static gchar
* ieditor_get_attributes(IAnjutaEditor
* editor
,
193 gint start
, gint end
, GError
**e
)
198 /* Get cursor position */
199 static gint
ieditor_get_position(IAnjutaEditor
* editor
, GError
**e
)
201 AnjutaVim
* av
= ANJUTA_VIM(editor
);
206 /* Return line of cursor */
207 static gint
ieditor_get_lineno(IAnjutaEditor
*editor
, GError
**e
)
209 AnjutaVim
* av
= ANJUTA_VIM(editor
);
213 /* Return the length of the text in the buffer */
214 static gint
ieditor_get_length(IAnjutaEditor
*editor
, GError
**e
)
216 AnjutaVim
* av
= ANJUTA_VIM(editor
);
220 /* Return word on cursor position */
221 static gchar
* ieditor_get_current_word(IAnjutaEditor
*editor
, GError
**e
)
223 AnjutaVim
* av
= ANJUTA_VIM(editor
);
227 /* Insert text at position */
228 static void ieditor_insert(IAnjutaEditor
*editor
, gint position
,
229 const gchar
* text
, gint length
, GError
**e
)
231 AnjutaVim
* av
= ANJUTA_VIM(editor
);
234 /* Append text to buffer */
235 static void ieditor_append(IAnjutaEditor
*editor
, const gchar
* text
,
236 gint length
, GError
**e
)
238 AnjutaVim
* av
= ANJUTA_VIM(editor
);
241 static void ieditor_erase_all(IAnjutaEditor
*editor
, GError
**e
)
243 AnjutaVim
* av
= ANJUTA_VIM(editor
);
246 /* Return true if editor can redo */
247 static gboolean
ieditor_can_redo(IAnjutaEditor
*editor
, GError
**e
)
249 AnjutaVim
* av
= ANJUTA_VIM(editor
);
253 /* Return true if editor can undo */
254 static gboolean
ieditor_can_undo(IAnjutaEditor
*editor
, GError
**e
)
256 AnjutaVim
* av
= ANJUTA_VIM(editor
);
260 /* Return column of cursor */
261 static gint
ieditor_get_column(IAnjutaEditor
*editor
, GError
**e
)
263 AnjutaVim
* av
= ANJUTA_VIM(editor
);
267 /* Return TRUE if editor is in overwrite mode */
268 static gboolean
ieditor_get_overwrite(IAnjutaEditor
*editor
, GError
**e
)
270 AnjutaVim
* av
= ANJUTA_VIM(editor
);
275 /* Set the editor popup menu */
276 static void ieditor_set_popup_menu(IAnjutaEditor
*editor
,
277 GtkWidget
* menu
, GError
**e
)
279 AnjutaVim
* av
= ANJUTA_VIM(editor
);
282 /* Return the opened filename */
283 static const gchar
* ieditor_get_filename(IAnjutaEditor
*editor
, GError
**e
)
285 AnjutaVim
* av
= ANJUTA_VIM(editor
);
286 return av
->priv
->filename
;
289 /* Convert from position to line */
290 static gint
ieditor_get_line_from_position(IAnjutaEditor
*editor
,
291 gint position
, GError
**e
)
293 AnjutaVim
* av
= ANJUTA_VIM(editor
);
298 ieditor_undo(IAnjutaEditor
* edit
, GError
** ee
)
300 AnjutaVim
* av
= ANJUTA_VIM(edit
);
304 ieditor_redo(IAnjutaEditor
* edit
, GError
** ee
)
306 AnjutaVim
* av
= ANJUTA_VIM(edit
);
310 ieditor_iface_init (IAnjutaEditorIface
*iface
)
312 iface
->goto_line
= ieditor_goto_line
;
313 iface
->goto_position
= ieditor_goto_position
;
314 iface
->get_text
= ieditor_get_text
;
315 iface
->get_attributes
= ieditor_get_attributes
;
316 iface
->get_position
= ieditor_get_position
;
317 iface
->get_lineno
= ieditor_get_lineno
;
318 iface
->get_length
= ieditor_get_length
;
319 iface
->get_current_word
= ieditor_get_current_word
;
320 iface
->insert
= ieditor_insert
;
321 iface
->append
= ieditor_append
;
322 iface
->erase_all
= ieditor_erase_all
;
323 iface
->get_filename
= ieditor_get_filename
;
324 iface
->can_undo
= ieditor_can_undo
;
325 iface
->can_redo
= ieditor_can_redo
;
326 iface
->get_column
= ieditor_get_column
;
327 iface
->get_overwrite
= ieditor_get_overwrite
;
328 iface
->set_popup_menu
= ieditor_set_popup_menu
;
329 iface
->get_line_from_position
= ieditor_get_line_from_position
;
330 iface
->undo
= ieditor_undo
;
331 iface
->redo
= ieditor_redo
;
334 ANJUTA_TYPE_BEGIN(AnjutaVim
, anjuta_vim
, GTK_TYPE_VIM
);
335 ANJUTA_TYPE_ADD_INTERFACE(ifile
, IANJUTA_TYPE_FILE
);
336 ANJUTA_TYPE_ADD_INTERFACE(isavable
, IANJUTA_TYPE_FILE_SAVABLE
);
337 ANJUTA_TYPE_ADD_INTERFACE(ieditor
, IANJUTA_TYPE_EDITOR
);