1 /*
2 * utils.c
3 *
4 * Copyright 2010 Alexander Petukhov <devel(at)apetukhov.ru>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 /*
23 * Miscellaneous functions.
24 */
25
26 #include <stdlib.h>
27 #include <memory.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <gtk/gtk.h>
31
32 #include "breakpoint.h"
33 #include "debug_module.h"
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #include <geanyplugin.h>
39
40 #include "utils.h"
41
42 /*
43 * opens position in a editor
44 */
editor_open_position(const gchar * filename,int line)45 void editor_open_position(const gchar *filename, int line)
46 {
47 GeanyDocument* doc = NULL;
48 gboolean already_open = (doc = document_get_current()) && !strcmp(DOC_FILENAME(doc), filename);
49
50 if (!already_open)
51 doc = document_open_file(filename, FALSE, NULL, NULL);
52
53 if (doc)
54 {
55 /* temporarily set debug caret policy */
56 scintilla_send_message(doc->editor->sci, SCI_SETYCARETPOLICY, CARET_SLOP | CARET_JUMPS | CARET_EVEN, 3);
57
58 sci_goto_line(doc->editor->sci, line - 1, TRUE);
59
60 /* revert to default edit caret policy */
61 scintilla_send_message(doc->editor->sci, SCI_SETYCARETPOLICY, CARET_EVEN, 0);
62
63 scintilla_send_message(doc->editor->sci, SCI_SETFOCUS, TRUE, 0);
64 }
65 else
66 {
67 dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Can't find a source file \"%s\""), filename);
68 }
69 }
70
71 /*
72 * get word at "position" in Scintilla document
73 */
get_word_at_position(ScintillaObject * sci,int position)74 GString* get_word_at_position(ScintillaObject *sci, int position)
75 {
76 GString *word = g_string_new("");
77
78 gchar gc;
79
80 /* first, move to the beginning of a word */
81 do
82 {
83 gc = sci_get_char_at(sci, position - 1);
84 if (isalpha(gc) || '.' == gc || '_' == gc)
85 {
86 position--;
87 continue;
88 }
89 else if ('>' == gc)
90 {
91 if('-' == sci_get_char_at(sci, position - 2))
92 {
93 position -= 2;
94 continue;
95 }
96 }
97 break;
98 }
99 while(TRUE);
100
101 /* move to the end of a word */
102 do
103 {
104 gc = sci_get_char_at(sci, position);
105 if (isalpha(gc) || '.' == gc || '_' == gc)
106 {
107 word = g_string_append_c(word, gc);
108 position++;
109 continue;
110 }
111 else if ('-' == gc)
112 {
113 gchar next = sci_get_char_at(sci, position + 1);
114 if('>' == next)
115 {
116 word = g_string_append(word, "->");
117 position += 2;
118 continue;
119 }
120 }
121 break;
122 }
123 while (TRUE);
124
125 return word;
126 }
127