2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "file_history.h"
28 typedef struct _AnFileHistory
32 gboolean history_move
;
35 static AnFileHistory
*s_history
= NULL
;
37 static void an_file_history_init (void)
39 s_history
= g_new(AnFileHistory
, 1);
40 s_history
->items
= NULL
;
41 s_history
->current
= NULL
;
42 s_history
->history_move
= FALSE
;
45 AnHistFile
*an_hist_file_new (GFile
*file
, gint line
)
49 g_return_val_if_fail(file
, NULL
);
51 h_file
= g_new(AnHistFile
, 1);
52 h_file
->file
= g_object_ref (file
);
57 void an_hist_file_free(AnHistFile
*h_file
)
59 g_return_if_fail(h_file
);
60 g_object_unref (h_file
->file
);
64 static void an_hist_items_free(GList
*items
)
68 g_return_if_fail(items
);
69 for (tmp
= items
; tmp
; tmp
= g_list_next(tmp
))
70 an_hist_file_free((AnHistFile
*) tmp
->data
);
74 void an_file_history_reset(void)
76 g_return_if_fail(s_history
&& s_history
->items
);
78 an_hist_items_free(s_history
->items
);
79 s_history
->items
= NULL
;
80 s_history
->current
= NULL
;
83 void an_file_history_push (GFile
*file
, gint line
)
87 g_return_if_fail (file
);
90 an_file_history_init();
92 if (s_history
->current
)
96 /* Only update line number when called by forward/backward */
97 if (s_history
->history_move
)
99 AnHistFile
*h_file
= (AnHistFile
*) s_history
->current
->data
;
101 if (g_file_equal (file
,h_file
->file
))
108 next
= s_history
->current
->next
;
109 s_history
->current
->next
= NULL
;
110 an_hist_items_free(s_history
->items
);
112 s_history
->items
= next
;
113 if (next
) next
->prev
= NULL
;
114 s_history
->current
= NULL
;
115 if (g_list_length(s_history
->items
) > MAX_ENTRIES
)
117 GList
*tmp
= g_list_nth(s_history
->items
, OPT_ENTRIES
);
118 an_hist_items_free(tmp
->next
);
122 h_file
= an_hist_file_new(file
, line
);
123 s_history
->items
= g_list_prepend(s_history
->items
, h_file
);
124 s_history
->current
= NULL
;
127 void an_file_history_back(AnjutaDocman
*docman
)
132 if (!(s_history
&& (!s_history
->current
|| s_history
->current
->next
)))
135 current
= s_history
->current
? s_history
->current
->next
: s_history
->items
;
136 h_file
= (AnHistFile
*) current
->data
;
138 s_history
->history_move
= TRUE
;
139 anjuta_docman_goto_file_line_mark (docman
, h_file
->file
,
140 h_file
->line
, FALSE
);
141 s_history
->history_move
= FALSE
;
143 s_history
->current
= current
;
146 void an_file_history_forward(AnjutaDocman
*docman
)
151 if (!(s_history
&& s_history
->current
&& s_history
->current
->prev
))
154 current
= s_history
->current
->prev
;
155 h_file
= (AnHistFile
*) current
->data
;
157 s_history
->history_move
= TRUE
;
158 anjuta_docman_goto_file_line_mark(docman
, h_file
->file
,
159 h_file
->line
, FALSE
);
160 s_history
->history_move
= FALSE
;
162 s_history
->current
= current
;
165 void an_file_history_dump(void)
170 g_return_if_fail(s_history
&& s_history
->items
);
171 fprintf(stderr
, "--------------------------\n");
172 for (tmp
= s_history
->items
; tmp
; tmp
= g_list_next(tmp
))
175 h_file
= (AnHistFile
*) tmp
->data
;
176 uri
= g_file_get_uri (h_file
->file
);
177 fprintf(stderr
, "%s:%d", uri
, h_file
->line
);
179 if (tmp
== s_history
->current
)
180 fprintf(stderr
, " (*)");
181 fprintf(stderr
, "\n");
183 fprintf(stderr
, "--------------------------\n");
186 void an_file_history_free(void)
188 an_file_history_reset();