1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 Copyright (C) 2005 Sebastien Granjoux
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.
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.
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, MA 02110-1301 USA
22 * Keeps variables used by the tool plugin. This includes values defined
23 * by Anjuta in several flavour (by example file uri, path or just name) and
24 * other values ask at run time with a dialog.
25 * Each variable includes a short help message.
27 *---------------------------------------------------------------------------*/
33 #include <libanjuta/anjuta-utils.h>
34 #include <libanjuta/interfaces/ianjuta-document-manager.h>
35 #include <libanjuta/interfaces/ianjuta-file-manager.h>
36 #include <libanjuta/interfaces/ianjuta-project-manager.h>
37 #include <libanjuta/interfaces/ianjuta-editor.h>
38 #include <libanjuta/interfaces/ianjuta-editor-selection.h>
39 #include <libanjuta/interfaces/ianjuta-file.h>
41 #include <glib/gi18n.h>
46 /* List of variables understood by the tools editor and their values.
47 *---------------------------------------------------------------------------*/
49 /* enum and following variable_list table must be in the same order */
52 ATP_PROJECT_ROOT_URI
= 0,
53 ATP_PROJECT_ROOT_DIRECTORY
,
54 ATP_FILE_MANAGER_CURRENT_GFILE
,
55 ATP_FILE_MANAGER_CURRENT_URI
,
56 ATP_FILE_MANAGER_CURRENT_DIRECTORY
,
57 ATP_FILE_MANAGER_CURRENT_FULL_FILENAME
,
58 ATP_FILE_MANAGER_CURRENT_FULL_FILENAME_WITHOUT_EXT
,
59 ATP_FILE_MANAGER_CURRENT_FILENAME
,
60 ATP_FILE_MANAGER_CURRENT_FILENAME_WITHOUT_EXT
,
61 ATP_FILE_MANAGER_CURRENT_EXTENSION
,
62 ATP_PROJECT_MANAGER_CURRENT_URI
,
63 ATP_PROJECT_MANAGER_CURRENT_DIRECTORY
,
64 ATP_PROJECT_MANAGER_CURRENT_FULL_FILENAME
,
65 ATP_PROJECT_MANAGER_CURRENT_FULL_FILENAME_WITHOUT_EXT
,
66 ATP_PROJECT_MANAGER_CURRENT_FILENAME
,
67 ATP_PROJECT_MANAGER_CURRENT_FILENAME_WITHOUT_EXT
,
68 ATP_PROJECT_MANAGER_CURRENT_EXTENSION
,
69 ATP_EDITOR_CURRENT_FILENAME
,
70 ATP_EDITOR_CURRENT_FILENAME_WITHOUT_EXT
,
71 ATP_EDITOR_CURRENT_DIRECTORY
,
72 ATP_EDITOR_CURRENT_SELECTION
,
73 ATP_EDITOR_CURRENT_WORD
,
74 ATP_EDITOR_CURRENT_LINE
,
86 {IANJUTA_PROJECT_MANAGER_PROJECT_ROOT_URI
, N_("Project root URI"), ATP_DEFAULT_VARIABLE
},
87 {"project_root_directory", N_("Project root path"), ATP_DIRECTORY_VARIABLE
},
88 {IANJUTA_FILE_MANAGER_SELECTED_FILE
, "File Manager file", ATP_NO_VARIABLE
},
89 {"file_manager_current_uri", N_("Selected URI in the file manager plugin"), ATP_DEFAULT_VARIABLE
},
90 {"file_manager_current_directory", N_("Selected directory in the file manager plugin"), ATP_DIRECTORY_VARIABLE
},
91 {"file_manager_current_full_filename", N_("Selected full file name in the file manager plugin"), ATP_FILE_VARIABLE
},
92 {"file_manager_current_full_filename_without_ext", N_("Selected full file name without extension in the file manager plugin"), ATP_FILE_VARIABLE
},
93 {"file_manager_current_filename", N_("Selected file name in the file manager plugin"), ATP_FILE_VARIABLE
},
94 {"file_manager_current_filename_without_ext", N_("Selected file name without extension in the file manager plugin"), ATP_FILE_VARIABLE
},
95 {"file_manager_current_extension", N_("Selected file's extension in the file manager plugin"), ATP_DEFAULT_VARIABLE
},
96 {"project_manager_current_uri", N_("Selected URI in the project manager plugin"), ATP_DEFAULT_VARIABLE
},
97 {"project_manager_current_directory", N_("Selected directory in the project manager plugin"), ATP_DIRECTORY_VARIABLE
},
98 {"project_manager_current_full_filename", N_("Selected full file name in the project manager plugin"), ATP_FILE_VARIABLE
},
99 {"project_manager_current_full_filename_without_ext", N_("Selected full file name without extension in the project manager plugin"), ATP_FILE_VARIABLE
},
100 {"project_manager_current_filename", N_("Selected file name in the project manager plugin"), ATP_FILE_VARIABLE
},
101 {"project_manager_current_filename_without_ext", N_("Selected file name without extension in the project manager plugin"), ATP_FILE_VARIABLE
},
102 {"project_manager_current_extension", N_("Selected file extension in the project manager plugin"), ATP_DEFAULT_VARIABLE
},
103 {"editor_current_filename", N_("Currently edited file name"), ATP_FILE_VARIABLE
},
104 {"editor_current_filename_without_ext", N_("Currently edited file name without extension"), ATP_FILE_VARIABLE
},
105 {"editor_current_directory", N_("Currently edited file directory"), ATP_DIRECTORY_VARIABLE
},
106 {"editor_current_selection", N_("Currently selected text in editor"), ATP_FILE_VARIABLE
},
107 {"editor_current_word", N_("Current word in editor"), ATP_FILE_VARIABLE
},
108 {"editor_current_line", N_("Current line in editor"), ATP_FILE_VARIABLE
},
109 {"ask_user_string", N_("Ask the user to get additional parameters"), ATP_INTERACTIVE_VARIABLE
}
113 *---------------------------------------------------------------------------*/
115 /* Get path from uri */
117 get_path_from_uri (char* uri
)
122 if (uri
== NULL
) return NULL
;
124 file
= g_file_new_for_uri (uri
);
126 val
= g_file_get_path (file
);
127 g_object_unref (file
);
132 /* Remove file name from a path */
134 remove_filename (char* path
)
138 if (path
== NULL
) return NULL
;
140 val
= g_path_get_dirname (path
);
146 /* Remove directory from a path */
148 remove_directory (char* path
)
152 if (path
== NULL
) return NULL
;
154 val
= g_path_get_basename (path
);
161 /* Remove directory and base filename from a path */
163 remove_all_but_extension (char* path
)
168 if (path
== NULL
) return NULL
;
170 dir
= strrchr (path
, G_DIR_SEPARATOR
);
171 ext
= strrchr (path
, '.');
172 if ((ext
!= NULL
) && ((dir
== NULL
) || (dir
< ext
)))
174 strcpy(path
, ext
+ 1);
184 /* Remove extension from a filename */
186 remove_extension (char* path
)
191 if (path
== NULL
) return NULL
;
193 dir
= strrchr (path
, G_DIR_SEPARATOR
);
194 ext
= strrchr (path
, '.');
195 if ((ext
!= NULL
) && ((dir
== NULL
) || (dir
< ext
)))
203 /* Get variable index from name
204 *---------------------------------------------------------------------------*/
207 atp_variable_get_id (const ATPVariable
* this, const gchar
* name
)
211 for (i
= 0; i
< ATP_VARIABLE_COUNT
; ++i
)
213 if (strcmp (variable_list
[i
].name
, name
) == 0) break;
220 atp_variable_get_id_from_name_part (const ATPVariable
* this, const gchar
* name
, gsize length
)
224 for (i
= 0; i
< ATP_VARIABLE_COUNT
; ++i
)
226 if ((strncmp (variable_list
[i
].name
, name
, length
) == 0)
227 && (variable_list
[i
].name
[length
] == '\0')) break;
233 /* Get Anjuta variables
234 *---------------------------------------------------------------------------*/
237 atp_variable_get_anjuta_variable (const ATPVariable
*this, guint id
)
242 anjuta_shell_get_value (this->shell
, variable_list
[id
].name
, &value
, &err
);
245 /* Value probably does not exist */
254 if (G_VALUE_HOLDS (&value
, G_TYPE_STRING
))
256 ret
= g_value_dup_string (&value
);
258 else if (G_VALUE_HOLDS (&value
, G_TYPE_FILE
))
260 ret
= g_value_dup_object (&value
);
266 g_value_unset (&value
);
273 atp_variable_get_project_manager_variable (const ATPVariable
*this, guint id
)
275 IAnjutaProjectManager
*prjman
;
280 prjman
= anjuta_shell_get_interface (this->shell
, IAnjutaProjectManager
, NULL
);
282 if (prjman
== NULL
) return NULL
;
286 case ATP_PROJECT_MANAGER_CURRENT_URI
:
287 file
= ianjuta_project_manager_get_selected (prjman
, &err
);
290 val
= g_file_get_uri (file
);
291 g_object_unref (file
);
295 g_return_val_if_reached (NULL
);
310 static IAnjutaEditor
*
311 get_current_editor(IAnjutaDocumentManager
* docman
)
315 IAnjutaDocument
* document
= ianjuta_document_manager_get_current_document(docman
, NULL
);
316 if (IANJUTA_IS_EDITOR(document
))
317 return IANJUTA_EDITOR(document
);
323 atp_variable_get_editor_variable (const ATPVariable
*this, guint id
)
325 IAnjutaDocumentManager
*docman
;
332 docman
= anjuta_shell_get_interface (this->shell
, IAnjutaDocumentManager
, NULL
);
333 ed
= get_current_editor(docman
);
335 if (ed
== NULL
) return NULL
;
338 case ATP_EDITOR_CURRENT_SELECTION
:
339 val
= ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (ed
),
342 case ATP_EDITOR_CURRENT_WORD
:
343 val
= ianjuta_editor_get_current_word (ed
, &err
);
345 case ATP_EDITOR_CURRENT_LINE
:
346 val
= g_strdup_printf ("%d", ianjuta_editor_get_lineno (ed
, &err
));
348 case ATP_EDITOR_CURRENT_FILENAME
:
349 val
= g_strdup (ianjuta_document_get_filename (IANJUTA_DOCUMENT(ed
), &err
));
351 case ATP_EDITOR_CURRENT_DIRECTORY
:
352 file
= ianjuta_file_get_file (IANJUTA_FILE (ed
), &err
);
353 path
= g_file_get_path (file
);
354 val
= remove_filename(path
);
355 g_object_unref (file
);
358 g_return_val_if_reached (NULL
);
374 *---------------------------------------------------------------------------*/
377 atp_variable_get_count (const ATPVariable
* this)
379 return ATP_VARIABLE_COUNT
;
383 atp_variable_get_name (const ATPVariable
* this, guint id
)
385 return id
>= ATP_VARIABLE_COUNT
? NULL
: variable_list
[id
].name
;
389 atp_variable_get_help (const ATPVariable
* this, guint id
)
391 return id
>= ATP_VARIABLE_COUNT
? NULL
: _(variable_list
[id
].help
);
395 atp_variable_get_flag (const ATPVariable
* this, guint id
)
397 return id
>= ATP_VARIABLE_COUNT
? ATP_NO_VARIABLE
: variable_list
[id
].flag
;
401 atp_variable_get_value (const ATPVariable
* this, const gchar
* name
)
405 id
= atp_variable_get_id (this, name
);
407 return atp_variable_get_value_from_id (this, id
);
411 atp_variable_get_value_from_name_part (const ATPVariable
* this, const gchar
* name
, gsize length
)
415 id
= atp_variable_get_id_from_name_part (this, name
, length
);
417 return atp_variable_get_value_from_id (this, id
);
420 /* Return NULL only if the variable doesn't exist
421 * If not NULL, return value must be freed when not used */
423 atp_variable_get_value_from_id (const ATPVariable
* this, guint id
)
430 case ATP_PROJECT_ROOT_URI
:
431 val
= atp_variable_get_anjuta_variable (this, id
);
433 case ATP_PROJECT_ROOT_DIRECTORY
:
434 val
= atp_variable_get_anjuta_variable (this, ATP_PROJECT_ROOT_URI
);
435 val
= get_path_from_uri (val
);
437 case ATP_FILE_MANAGER_CURRENT_URI
:
438 file
= atp_variable_get_anjuta_variable (this, ATP_FILE_MANAGER_CURRENT_GFILE
);
441 val
= g_file_get_uri (file
);
442 g_object_unref (file
);
445 case ATP_FILE_MANAGER_CURRENT_DIRECTORY
:
446 file
= atp_variable_get_anjuta_variable (this, ATP_FILE_MANAGER_CURRENT_GFILE
);
449 val
= g_file_get_path (file
);
450 g_object_unref (file
);
451 val
= remove_filename (val
);
454 case ATP_FILE_MANAGER_CURRENT_FULL_FILENAME
:
455 file
= atp_variable_get_anjuta_variable (this, ATP_FILE_MANAGER_CURRENT_GFILE
);
458 val
= g_file_get_path (file
);
459 g_object_unref (file
);
462 case ATP_FILE_MANAGER_CURRENT_FULL_FILENAME_WITHOUT_EXT
:
463 file
= atp_variable_get_anjuta_variable (this, ATP_FILE_MANAGER_CURRENT_GFILE
);
466 val
= g_file_get_path (file
);
467 g_object_unref (file
);
468 val
= remove_extension (val
);
471 case ATP_FILE_MANAGER_CURRENT_FILENAME
:
472 file
= atp_variable_get_anjuta_variable (this, ATP_FILE_MANAGER_CURRENT_GFILE
);
475 val
= g_file_get_path (file
);
476 g_object_unref (file
);
477 val
= remove_directory (val
);
480 case ATP_FILE_MANAGER_CURRENT_FILENAME_WITHOUT_EXT
:
481 file
= atp_variable_get_anjuta_variable (this, ATP_FILE_MANAGER_CURRENT_GFILE
);
484 val
= g_file_get_path (file
);
485 g_object_unref (file
);
486 val
= remove_directory (val
);
487 val
= remove_extension (val
);
490 case ATP_FILE_MANAGER_CURRENT_EXTENSION
:
491 file
= atp_variable_get_anjuta_variable (this, ATP_FILE_MANAGER_CURRENT_GFILE
);
494 val
= g_file_get_path (file
);
495 g_object_unref (file
);
496 val
= remove_all_but_extension (val
);
499 case ATP_PROJECT_MANAGER_CURRENT_URI
:
500 val
= atp_variable_get_project_manager_variable (this, id
);
502 case ATP_PROJECT_MANAGER_CURRENT_DIRECTORY
:
503 val
= atp_variable_get_project_manager_variable (this, ATP_PROJECT_MANAGER_CURRENT_URI
);
504 val
= get_path_from_uri (val
);
505 val
= remove_filename (val
);
507 case ATP_PROJECT_MANAGER_CURRENT_FULL_FILENAME
:
508 val
= atp_variable_get_project_manager_variable (this, ATP_PROJECT_MANAGER_CURRENT_URI
);
509 val
= get_path_from_uri (val
);
511 case ATP_PROJECT_MANAGER_CURRENT_FULL_FILENAME_WITHOUT_EXT
:
512 val
= atp_variable_get_project_manager_variable (this, ATP_PROJECT_MANAGER_CURRENT_URI
);
513 val
= get_path_from_uri (val
);
514 val
= remove_extension (val
);
516 case ATP_PROJECT_MANAGER_CURRENT_FILENAME
:
517 val
= atp_variable_get_anjuta_variable (this, ATP_PROJECT_MANAGER_CURRENT_URI
);
518 val
= get_path_from_uri (val
);
519 val
= remove_directory (val
);
521 case ATP_PROJECT_MANAGER_CURRENT_FILENAME_WITHOUT_EXT
:
522 val
= atp_variable_get_anjuta_variable (this, ATP_PROJECT_MANAGER_CURRENT_URI
);
523 val
= get_path_from_uri (val
);
524 val
= remove_directory (val
);
525 val
= remove_extension (val
);
527 case ATP_PROJECT_MANAGER_CURRENT_EXTENSION
:
528 val
= atp_variable_get_anjuta_variable (this, ATP_PROJECT_MANAGER_CURRENT_URI
);
529 val
= get_path_from_uri (val
);
530 val
= remove_all_but_extension (val
);
532 case ATP_EDITOR_CURRENT_FILENAME_WITHOUT_EXT
:
533 val
= atp_variable_get_editor_variable (this, ATP_EDITOR_CURRENT_FILENAME
);
534 val
= remove_extension (val
);
536 case ATP_EDITOR_CURRENT_FILENAME
:
537 case ATP_EDITOR_CURRENT_DIRECTORY
:
538 case ATP_EDITOR_CURRENT_SELECTION
:
539 case ATP_EDITOR_CURRENT_WORD
:
540 case ATP_EDITOR_CURRENT_LINE
:
541 val
= atp_variable_get_editor_variable (this, id
);
543 case ATP_ASK_USER_STRING
:
545 anjuta_util_dialog_input (GTK_WINDOW (this->shell
),
546 _("Command line parameters"), NULL
, &val
);
549 /* Variable does not exist */
554 return val
== NULL
? g_new0 (gchar
, 1) : val
;
557 /* Creation and Destruction
558 *---------------------------------------------------------------------------*/
561 atp_variable_construct (ATPVariable
* this, AnjutaShell
* shell
)
569 atp_variable_destroy (ATPVariable
* this)