16.4.2 Macro arguments
When an action is defined, you can use macro arguments to pass to your
shell or external commands. Macro arguments
are special parameters that are transformed every time the command is executed.
The following macro arguments are provided.
The equivalent python command is given for all tests. These commands are useful
when you are writing a full python script, and want to test for yourself
whether the context is properly defined.
%f
- Base name of the currently selected file.
Python equivalent:
import os.path
os.path.basename (GPS.current_context().file().name())
%F
- Absolute name of the currently opened file.
Python equivalent:
GPS.current_context().file().name()
%fk
- Krunched base name of the currently selected file.
This is the same as %f, except that long names are shortened, and their middle
letters are replaced by "[...]". This should be used in particular in menu
labels, to keep the menus narrow.
%e
- Name of the entity the user clicked on.
Python equivalent:
GPS.current_context().entity().name()
%ek
- Krunched name of the entity the user clicked on.
This is the same as
%e
, except long names are shorted as in %fk
.
%d
- The current directory.
Python equivalent:
GPS.current_context().directory()
%dk
- The krunched name of the current directory.
%l
- This is the line number on which the user clicked.
Python equivalent:
GPS.current_context().line()
%c
- This is the column number on which the user clicked.
Python equivalent:
GPS.current_context().column()
%a
- If the user clicked within the Locations Window, this is the name of the
category to which the current line belongs
%i
- If the user clicked within the Project View, this is the name of the parent
project, ie the one that is importing the one the user clicked on. Note that
with this definition of parent project, a given project might have multiple
parents. The one that is returned is read from the Project View itself.
%s
- This is the text selected by the user, if a single line was selected. When
multiple lines were selected, this returns the empty string
%p
- The current project. This is the name of the project, not the project file, ie
the .gpr extension is not included in this name, and the casing is the
one found inside the project file, not the one of the file name itself.
If the current context is an editor, this is the name of the project to which
the source file belongs.
Python equivalent:
GPS.current_context().project().name()
%P
- The root project. This is the name of the project, not the project file.
Python equivalent:
GPS.Project.root().name()
%pp
- The current project file pathname. If a file is selected, this is the project
file to which the source file belongs.
Python equivalent:
GPS.current_context().project().file().name()
%PP
- The root project pathname.
Python equivalent:
GPS.Project.root().file().name()
%pps
- This is similar to
%pp
, except it returns the project name prepended
with -P
, or an empty string if there is no project file selected and the
current source file doesn't belong to any project. This is mostly for use with
the GNAT command line tools.
Python equivalent:
if GPS.current_context().project():
return "-P" & GPS.current_context().project().path()
%PPs
- This is similar to
%PP
, except it returns the project name prepended
with -P
, or an empty string if the root project is the default project.
This is mostly for use with the GNAT command line tools.
%(p|P)[r](d|s)[f]
- Substituted by the list of sources or directories of a given project. This list
is a list of space-separated, quoted names (all names are surrounded by double
quotes, for proper handling of spaces in directories or file names).
P
- the root project.
p
- the selected project, or the root project if there is no project selected.
r
- recurse through the projects: sub projects will be listed as well as their
sub projects, etc...
d
- list the source directories.
Python equivalent:
GPS.current_context().project().source_dirs()
s
- list the source files.
Python equivalent:
GPS.current_context().project().sources()
f
- output the list into a file and substitute the parameter with the
name of that file. This file is never deleted by GPS, it is your responsibility
to do so.
Examples:
%Ps
- Replaced by a list of source files in the root project.
%prs
- Replaced by a list of files in the current project, and all imported
sub projects, recursively.
%prdf
- Replaced by the name of a file that contains a list of source
directories in the current project, and all imported sub projects,
recursively.
%%
- Replaced by the % sign.