static bool fmt_is_supported_ft(GeanyDocument *doc) { int id; if (!DOC_VALID(doc)) doc = document_get_current(); if (!DOC_VALID(doc)) return false; id = doc->file_type->id; return (id == GEANY_FILETYPES_C || id == GEANY_FILETYPES_CPP || id == GEANY_FILETYPES_OBJECTIVEC); }
static gboolean check_lines(gpointer data) { GeanyDocument *doc = check_line_data.doc; /* since we're in an timeout callback, the document may have been closed */ if (DOC_VALID (doc)) { gchar *line; gint line_number = check_line_data.line_number; gint line_count = check_line_data.line_count; gint i; for (i = 0; i < line_count; i++) { line = sci_get_line(doc->editor->sci, line_number); indicator_clear_on_line(doc, line_number); if (sc_speller_process_line(doc, line_number, line) != 0) { if (sc_info->use_msgwin) msgwin_switch_tab(MSG_MESSAGE, FALSE); } g_free(line); } } check_line_data.check_while_typing_idle_source_id = 0; return FALSE; }
static PyObject* Document_new_file(PyObject *self, PyObject *args, PyObject *kwargs) { gchar *filename = NULL, *initial_text = NULL; Filetype *filetype = NULL; PyObject *py_ft = NULL; GeanyDocument *doc; GeanyFiletype *ft = NULL; static gchar *kwlist[] = { "filename", "filetype", "initial_text", NULL }; if (PyArg_ParseTupleAndKeywords(args, kwargs, "|zOz", kwlist, &filename, &py_ft, &initial_text)) { if (py_ft != NULL && py_ft != Py_None) { filetype = (Filetype *) py_ft; if (filetype->ft != NULL) ft = filetype->ft; } doc = document_new_file(filename, ft, initial_text); if (DOC_VALID(doc)) return (PyObject *) Document_create_new_from_geany_document(doc); } Py_RETURN_NONE; }
static void reindent (GeanyDocument *doc, GrindIndenter *indenter) { if (DOC_VALID (doc)) { int start; int end; ScintillaObject *sci = doc->editor->sci; if (sci_has_selection (sci)) { start = sci_get_line_from_position (sci, sci_get_selection_start (sci)); end = sci_get_line_from_position (sci, sci_get_selection_end (sci)); } else { start = 0; end = sci_get_line_count (sci); } if (start != end) { g_debug ("Using indenter \"%s\" by \"%s\"", grind_indenter_get_name (indenter), grind_indenter_get_author (indenter)); sci_start_undo_action (sci); if (grind_indenter_indent (indenter, doc, sci_get_position_from_line (sci, start), sci_get_line_end_position (sci, end))) { msgwin_status_add ("Reindented \"%s\"", DOC_FILENAME (doc)); } sci_end_undo_action (sci); } } }
static PyObject* Document_open_file(PyObject *self, PyObject *args, PyObject *kwargs) { gint read_only = 0; gchar *filename = NULL, *forced_encoding = NULL; GeanyDocument *doc; GeanyFiletype *ft = NULL; Filetype *filetype = NULL; PyObject *py_ft = NULL; static gchar *kwlist[] = { "filename", "read_only", "filetype", "forced_enc", NULL }; if (PyArg_ParseTupleAndKeywords(args, kwargs, "s|iOz", kwlist, &filename, &read_only, &py_ft, &forced_encoding)) { if (py_ft != NULL && py_ft != Py_None) { filetype = (Filetype *) py_ft; if (filetype->ft != NULL) ft = filetype->ft; } doc = document_open_file(filename, read_only, ft, forced_encoding); if (DOC_VALID(doc)) return (PyObject *) Document_create_new_from_geany_document(doc); } Py_RETURN_NONE; }
/* Save when focus out * * @param pointer ref to the current doc (struct GeanyDocument *) * * @return always FALSE = Just a one shot execution * */ static gboolean save_on_focus_out_idle(gpointer p_cur_doc) { GeanyDocument *cur_doc = p_cur_doc; if (DOC_VALID(cur_doc) && (cur_doc->file_name != NULL)) document_save_file(cur_doc, FALSE); return FALSE; }
static void on_open_config_item_map(GtkWidget *wid, gpointer user_data) { GeanyDocument *doc = document_get_current(); if (!DOC_VALID(doc) || !doc->file_name) return; gtk_widget_set_sensitive(wid, fmt_can_find_clang_format_dot_file(doc->file_name)); }
static PyObject* Document_get_current(PyObject *self) { GeanyDocument *doc; doc = document_get_current(); if (DOC_VALID(doc)) return (PyObject *) Document_create_new_from_geany_document(doc); Py_RETURN_NONE; }
static inline OverviewScintilla * overview_scintilla_from_document (GeanyDocument *doc) { if (DOC_VALID (doc)) { ScintillaObject *src_sci = doc->editor->sci; if (IS_SCINTILLA (src_sci)) return g_object_get_data (G_OBJECT (src_sci), "overview"); } return NULL; }
static void item_activate(GtkMenuItem * menuitem, gpointer gdata) { GeanyDocument *doc = document_get_current(); if(!DOC_VALID(doc)) { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("There are no opened documents. Open one and retry.\n")); return; } paste(doc, websites_api[website_selected]); }
/* tries to insert comments for all the document */ static void insert_all_comments (void) { GeanyDocument *doc; doc = document_get_current (); if (DOC_VALID (doc)) { /* try to ensure tags corresponds to the actual state of the file */ refresh_tag_list (doc->tm_file, doc->editor->sci, doc); ggd_insert_all_comments (doc, ggd_plugin_get_doctype (doc->file_type->id)); } }
static gboolean automark(gpointer user_data) { GeanyDocument *doc = (GeanyDocument *)user_data; GeanyEditor *editor = doc->editor; static GeanyEditor *editor_cache = NULL; ScintillaObject *sci = editor->sci; gchar text[GEANY_MAX_WORD_LENGTH]; static gchar text_cache[GEANY_MAX_WORD_LENGTH] = {0}; gint match_flag = SCFIND_MATCHCASE | SCFIND_WHOLEWORD; struct Sci_TextToFind ttf; source_id = 0; /* during timeout document could be destroyed so check everything again */ if (!DOC_VALID(doc)) return FALSE; /* Do not highlight while selecting text and allow other markers to work */ if (sci_has_selection(sci)) return FALSE; get_current_word(editor->sci, text, sizeof(text)); if (!*text) { editor_indicator_clear(editor, AUTOMARK_INDICATOR); return FALSE; } if (editor_cache != editor || strcmp(text, text_cache) != 0) { editor_indicator_clear(editor, AUTOMARK_INDICATOR); strcpy(text_cache, text); editor_cache = editor; } gint vis_first = SSM(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); gint doc_first = SSM(sci, SCI_DOCLINEFROMVISIBLE, vis_first, 0); gint vis_last = SSM(sci, SCI_LINESONSCREEN, 0, 0) + vis_first; gint doc_last = SSM(sci, SCI_DOCLINEFROMVISIBLE, vis_last, 0); gint start = SSM(sci, SCI_POSITIONFROMLINE, doc_first, 0); gint end = SSM(sci, SCI_GETLINEENDPOSITION, doc_last, 0); ttf.chrg.cpMin = start; ttf.chrg.cpMax = end; ttf.lpstrText = text; search_mark_in_range(editor, match_flag, &ttf); return FALSE; }
static GeanyDocument * ZenEditor_get_context(ZenEditor *self) { GeanyDocument *doc; print_called(); if (self->context == NULL) return NULL; doc = (GeanyDocument *) PyLong_AsVoidPtr(self->context); return DOC_VALID(doc) ? doc : NULL; }
static PyObject * Navqueue_goto_line(PyObject *module, PyObject *args, PyObject *kwargs) { gint line = 1; PyObject *py_old = NULL, *py_new = NULL; Document *py_doc_old, *py_doc_new; GeanyDocument *old_doc, *new_doc; static gchar *kwlist[] = { "old_doc", "new_doc", "line", NULL }; if (PyArg_ParseTupleAndKeywords(args, kwargs, "OOi", kwlist, &py_old, &py_new, &line)) { if (!py_old || py_old == Py_None) old_doc = NULL; else { py_doc_old = (Document *) py_old; old_doc = py_doc_old->doc; } if (!py_new || py_new == Py_None) Py_RETURN_NONE; else { py_doc_new = (Document *) py_new; new_doc = py_doc_new->doc; } if ( (old_doc != NULL && !DOC_VALID(old_doc)) || !DOC_VALID(new_doc) ) Py_RETURN_NONE; if (navqueue_goto_line(old_doc, new_doc, line)) Py_RETURN_TRUE; else Py_RETURN_FALSE; } Py_RETURN_NONE; }
static gboolean on_update_overview_later (gpointer user_data) { GeanyDocument *doc; doc = document_get_current (); if (DOC_VALID (doc)) { OverviewScintilla *overview; overview = g_object_get_data (G_OBJECT (doc->editor->sci), "overview"); if (OVERVIEW_IS_SCINTILLA (overview)) overview_scintilla_sync (overview); } return FALSE; }
static PyObject* Document_get_from_index(PyObject *self, PyObject *args, PyObject *kwargs) { gint idx; GeanyDocument *doc; static gchar *kwlist[] = { "index", NULL }; if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &idx)) { doc = document_index(idx); if (DOC_VALID(doc)) return (PyObject *) Document_create_new_from_geany_document(doc); } Py_RETURN_NONE; }
static PyObject* Document_find_by_real_path(PyObject *self, PyObject *args, PyObject *kwargs) { gchar *fn; GeanyDocument *doc; static gchar *kwlist[] = { "real_path", NULL }; if (PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &fn)) { doc = document_find_by_real_path(fn); if (DOC_VALID(doc)) return (PyObject *) Document_create_new_from_geany_document(doc); } Py_RETURN_NONE; }
static void on_open_config_file(GtkMenuItem *item, gpointer user_data) { char *fn; GeanyDocument *doc = document_get_current(); if (!DOC_VALID(doc) || !doc->file_name) return; fn = fmt_lookup_clang_format_dot_file(doc->file_name); if (fn) { document_open_file(fn, false, filetypes[GEANY_FILETYPES_YAML], NULL); g_free(fn); } }
void zen_controller_run_action(ZenController *zen, const char *action_name) { PyObject *addr, *result; GeanyDocument *doc; g_return_if_fail(zen != NULL); g_return_if_fail(action_name != NULL); ui_set_statusbar(FALSE, _("Zen Coding: Running '%s' action"), action_name); doc = document_get_current(); if (!DOC_VALID(doc)) { g_warning("No valid document detected."); return; } addr = PyLong_FromVoidPtr((void *) doc); if (addr == NULL) { if (PyErr_Occurred()) PyErr_Print(); g_warning("Unable to convert document pointer to Python object."); return; } result = PyObject_CallMethod(zen->editor, "set_context", "O", addr); Py_DECREF(addr); if (result == NULL) { if (PyErr_Occurred()) PyErr_Print(); g_warning("Unable to call set_context() function."); return; } Py_XDECREF(result); result = PyObject_CallFunction(zen->run_action, "sO", action_name, zen->editor); if (result == NULL) { if (PyErr_Occurred()) PyErr_Print(); g_warning("Call to run_action() failed."); return; } Py_XDECREF(result); }
gboolean msgwin_goto_messages_file_line(gboolean focus_editor) { GtkTreeIter iter; GtkTreeModel *model; GtkTreeSelection *selection; gboolean ret = FALSE; selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg)); if (gtk_tree_selection_get_selected(selection, &model, &iter)) { gint line; gchar *string; GeanyDocument *doc; GeanyDocument *old_doc = document_get_current(); gtk_tree_model_get(model, &iter, 0, &line, 1, &doc, 3, &string, -1); /* doc may have been closed, so check doc->index: */ if (line >= 0 && DOC_VALID(doc)) { ret = navqueue_goto_line(old_doc, doc, line); if (ret && focus_editor) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); } else if (line < 0 && string != NULL) { gchar *filename; /* try with a file:line parsing */ msgwin_parse_generic_line(string, &filename, &line); if (filename != NULL) { /* use document_open_file to find an already open file, or open it in place */ doc = document_open_file(filename, FALSE, NULL, NULL); if (doc != NULL) { ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line); if (ret && focus_editor) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); } } g_free(filename); } g_free(string); } return ret; }
static PyObject* Document_rename_file(Document *self, PyObject *args, PyObject *kwargs) { gchar *new_fn = NULL; static gchar *kwlist[] = { "new_filename", NULL }; if (PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &new_fn)) { if (new_fn != NULL) document_rename_file(self->doc, new_fn); } if (DOC_VALID(self->doc) && self->doc->file_name == new_fn) Py_RETURN_TRUE; else Py_RETURN_FALSE; Py_RETURN_NONE; }
static PyObject * Document_get_documents_list(PyObject *module) { guint i; GeanyDocument *doc; PyObject *list; list = PyList_New(0); for (i = 0; i < geany_data->documents_array->len; i++) { doc = g_ptr_array_index(geany_data->documents_array, i); if (DOC_VALID(doc)) { PyList_Append(list, (PyObject *) Document_create_new_from_geany_document(doc)); } } return list; }
static PyObject * ZenEditor_set_context(ZenEditor *self, PyObject *args) { PyObject *context = NULL, *tmp; print_called(); if (PyArg_ParseTuple(args, "O", &context)) { py_return_none_if_null(context); if (DOC_VALID((GeanyDocument *) PyLong_AsVoidPtr(context))) { tmp = self->context; Py_INCREF(context); self->context = context; Py_XDECREF(tmp); } } Py_RETURN_NONE; }
static PyObject * Document_get_property(Document *self, const gchar *prop_name) { g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(prop_name != NULL, NULL); if (!self->doc) { PyErr_SetString(PyExc_RuntimeError, "Document instance not initialized properly"); return NULL; } if (!DOC_VALID(self->doc)) { PyErr_SetString(PyExc_RuntimeError, "Document is invalid"); return NULL; } if (g_str_equal(prop_name, "basename_for_display")) { PyObject *py_str = NULL; gchar *res_string; res_string = document_get_basename_for_display(self->doc, -1); if (!res_string) { PyErr_SetString(PyExc_RuntimeError, "Geany API failed to return a string"); Py_RETURN_NONE; } py_str = PyString_FromString(res_string); g_free(res_string); return py_str; } else if (g_str_equal(prop_name, "notebook_page")) return Py_BuildValue("i", document_get_notebook_page(self->doc)); else if (g_str_equal(prop_name, "status_color")) { const GdkColor *color = document_get_status_color(self->doc); if (!color) Py_RETURN_NONE; return Py_BuildValue("iii", color->red, color->green, color->blue); } else if (g_str_equal(prop_name, "editor") && self->doc->editor) { Editor *editor; if (self->doc->editor != NULL) { editor = Editor_create_new_from_geany_editor(self->doc->editor); return (PyObject *) editor; } Py_RETURN_NONE; } else if (g_str_equal(prop_name, "encoding") && self->doc->encoding) return PyString_FromString(self->doc->encoding); else if (g_str_equal(prop_name, "file_name") && self->doc->file_name) return PyString_FromString(self->doc->file_name); else if (g_str_equal(prop_name, "file_type") && self->doc->file_type) return (PyObject *) Filetype_create_new_from_geany_filetype(self->doc->file_type); else if (g_str_equal(prop_name, "has_bom")) { if (self->doc->has_bom) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "has_tags")) { if (self->doc->has_tags) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "index")) return Py_BuildValue("i", self->doc->index); else if (g_str_equal(prop_name, "is_valid")) { if (self->doc->is_valid) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "readonly")) { if (self->doc->readonly) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "real_path")) { if (self->doc->real_path) return PyString_FromString(self->doc->real_path); Py_RETURN_NONE; } else if (g_str_equal(prop_name, "text_changed")) { if (self->doc->changed) Py_RETURN_NONE; else Py_RETURN_NONE; } }
static void do_format(GeanyDocument *doc, bool entire_doc, bool autof) { GString *formatted; ScintillaObject *sci; size_t offset = 0, length = 0, sci_len; size_t cursor_pos, old_first_line, new_first_line, line_delta; const char *sci_buf; bool changed = true; bool was_changed; if (doc == NULL) doc = document_get_current(); if (!DOC_VALID(doc)) { g_warning("Cannot format with no documents open"); return; } sci = doc->editor->sci; was_changed = doc->changed; // FIXME: instead of failing, ask user to save the document once if (!doc->real_path) { g_warning("Cannot format document that's never been saved"); return; } if (!entire_doc) { if (sci_has_selection(sci)) { // format selection offset = sci_get_selection_start(sci); length = sci_get_selection_end(sci) - offset; } else { // format current line size_t cur_line = sci_get_current_line(sci); offset = sci_get_position_from_line(sci, cur_line); length = sci_get_line_end_position(sci, cur_line) - offset; } } else { // format entire document offset = 0; length = sci_get_length(sci); } cursor_pos = sci_get_current_position(sci); sci_len = sci_get_length(sci); sci_buf = (const char *)scintilla_send_message(sci, SCI_GETCHARACTERPOINTER, 0, 0); formatted = fmt_clang_format(doc->file_name, sci_buf, sci_len, &cursor_pos, offset, length, false); // FIXME: handle better if (formatted == NULL) return; if (!autof) { changed = (formatted->len != sci_len) || (g_strcmp0(formatted->str, sci_buf) != 0); } old_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); // Replace document text and move cursor to new position scintilla_send_message(sci, SCI_BEGINUNDOACTION, 0, 0); scintilla_send_message(sci, SCI_CLEARALL, 0, 0); scintilla_send_message(sci, SCI_ADDTEXT, formatted->len, (sptr_t)formatted->str); scintilla_send_message(sci, SCI_GOTOPOS, cursor_pos, 0); new_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); line_delta = new_first_line - old_first_line; scintilla_send_message(sci, SCI_LINESCROLL, 0, -line_delta); scintilla_send_message(sci, SCI_ENDUNDOACTION, 0, 0); document_set_text_changed(doc, (was_changed || changed)); g_string_free(formatted, true); }
/* handler that opens the current filetype's configuration file */ static void open_current_filetype_conf_handler (GtkWidget *widget, gpointer data) { GeanyDocument *doc; (void)widget; (void)data; doc = document_get_current (); if (DOC_VALID (doc)) { gchar *path_read; gchar *path_write; GError *err = NULL; path_write = ggd_file_type_manager_get_conf_path (doc->file_type->id, GGD_PERM_W | GGD_PERM_NOCREAT, &err); if (! path_write) { msgwin_status_add (_("Failed to find configuration file " "for file type \"%s\": %s"), doc->file_type->name, err->message); g_error_free (err); } else { gchar *text = NULL; gchar *path_write_u8; path_read = ggd_file_type_manager_get_conf_path (doc->file_type->id, GGD_PERM_R, &err); if (! path_read) { text = g_strdup (_( "# Configuration for this file type doesn't exist yet.\n" "# To create it, just write it in this file and save it. For the description\n" "# of the syntax of this file, please refer to the manual.\n" )); } else { gchar *content = NULL; gsize length; if (! g_file_get_contents (path_read, &content, &length, &err)) { gchar *display_path_read; display_path_read = g_filename_display_name (path_read); g_warning (_("Failed to load file \"%s\": %s"), display_path_read, err->message); g_free (display_path_read); g_error_free (err); } else { text = encodings_convert_to_utf8 (content, length, NULL); g_free (content); } g_free (path_read); } path_write_u8 = utils_get_utf8_from_locale (path_write); /* It's no Ruby, but it is the closest one I've found. It has: * - # comments * - multi-line double-quoted strings */ document_new_file (path_write_u8, filetypes[GEANY_FILETYPES_RUBY], text); g_free (path_write_u8); g_free (text); g_free (path_write); } } }
static int ZenEditor_init(ZenEditor *self, PyObject *args, PyObject *kwds) { PyObject *context = NULL; PyObject *profile = NULL; PyObject *tmp, *mod, *caret_ph; const gchar *ph; static gchar *kwlist[] = { "profile", "context", NULL }; print_called(); self->active_profile = PyString_FromString("xhtml"); self->context = NULL; if (PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &context, &profile)) { if (context != NULL) { if (!DOC_VALID((GeanyDocument *) PyLong_AsVoidPtr(context))) return -1; tmp = self->context; Py_INCREF(context); self->context = context; Py_XDECREF(tmp); } if (profile != NULL) { tmp = self->active_profile; Py_INCREF(profile); self->active_profile = profile; Py_XDECREF(tmp); } } mod = PyImport_ImportModule("zencoding.utils"); if (mod == NULL) { if (PyErr_Occurred()) PyErr_Print(); return -1; } caret_ph = PyObject_GetAttrString(mod, "caret_placeholder"); if (caret_ph == NULL) { if (PyErr_Occurred()) PyErr_Print(); Py_XDECREF(mod); return -1; } Py_XDECREF(mod); ph = (const gchar *)PyString_AsString(caret_ph); if (ph == NULL) { if (PyErr_Occurred()) PyErr_Print(); Py_XDECREF(caret_ph); return -1; } self->caret_placeholder = g_strstrip(g_strdup(ph)); return 0; }