This file is indexed.

/usr/share/doc/gnat-gps/html/Hooks.html is in gnat-gps-doc 5.0-16.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 2002-2010 AdaCore.

This document is free; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This document is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, see http://www.gnu.org/licenses/.

A copy of the license is included in the section entitled
"GNU General Public License". -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Using the GNAT Programming Studio: Hooks</title>

<meta name="description" content="Using the GNAT Programming Studio: Hooks">
<meta name="keywords" content="Using the GNAT Programming Studio: Hooks">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Index-table.html#Index-table" rel="index" title="Index table">
<link href="Scripting-GPS.html#Scripting-GPS" rel="up" title="Scripting GPS">
<link href="Adding-support-for-new-Version-Control-Systems.html#Adding-support-for-new-Version-Control-Systems" rel="next" title="Adding support for new Version Control Systems">
<link href="Creating-custom-graphical-interfaces.html#Creating-custom-graphical-interfaces" rel="previous" title="Creating custom graphical interfaces">
<style type="text/css">
<!--
   

a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
pre.smallexample {background-color:rgb(240,240,240);
                     font-family: courier new,courier,fixed;
                     font-size: 14px;
                     margin: 0px 40px 0px 40px;
                     border-width: 1px 2px 2px 1px;
                     border-top-style: dotted;
                     border-left-style: dotted;
                     border-right-style: solid;
                     border-bottom-style: solid;
                     border-color: black;}
   code             {color:black;
                     font-family: courier new,courier,fixed;
                     font-size: 14px;}
   body             {font-family: arial,helvetica,sans-serif;
                     font-size: 16px;
                     max-width: 800px;
                     text-align: justify}
   samp             {font-family: courier new,courier,fixed;
                     font-size: 14px}
                    

-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Hooks"></a>
<div class="header">
<p>
Previous: <a href="Python-FAQ.html#Python-FAQ" accesskey="p" rel="previous">Python FAQ</a>, Up: <a href="Scripting-GPS.html#Scripting-GPS" accesskey="u" rel="up">Scripting GPS</a> &nbsp; [<a href="Index-table.html#Index-table" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Hooks-1"></a>
<h4 class="subsection">16.8.8 Hooks</h4>
<a name="index-hooks"></a>

<p>A <b>hook</b> is a named set of commands to be executed on particular occasions
as a result of user actions in GPS.
</p>
<p>GPS and its various modules define a number of standard hooks, which are
called for instance when a new project is loaded, when a file is edited, and
so on. You can define your own commands to be executed in such cases.
</p>
<a name="index-hooks_002c-Hook_002elist"></a>
<a name="index-hooks_002c-Hook_002edescribe"></a>
<p>You can find out the list of hooks that GPS currently knows about by calling
the <b>Hook.list</b> function, which takes no argument, and returns a list of
hook names that you can use. More advanced description for each hook is
available through the <code>Help-&gt;Python Extensions</code>.
</p>
<div class="smallexample">
<pre class="smallexample">GPS&gt; Hook.list
project_changed
open_file_action_hook
preferences_changed
[...]

Python&gt; GPS.Hook.list()
</pre></div>

<a name="index-hooks_002c-type"></a>
<p>The description of each hooks includes a pointer to the type of the hook, that
is what parameters the subprograms in this hook will receive. For instance:
</p>
<a name="index-hooks_002c-Hook_002elist_005ftypes"></a>
<p>The list of all known hook types can be found through the <b>Hook.list_types</b>
command. This takes no argument and returns a list of all known types of hooks.
As before, you can more information for each of these type through a call to
<b>Hook.describe_type</b>.
</p>
<a name="Adding-commands-to-hooks"></a>
<h4 class="subsubsection">16.8.8.1 Adding commands to hooks</h4>

<p>You can add your own command to existing hooks through a call to the
<b>Hook.add</b> command. Whenever the hook is executed by GPS or another script,
your command will also be executed, and will be given the parameters that
were specified when the hook is run. The first parameter is always the name
of the hook being executed.
</p>
<p>This <b>Hook.add</b> applies to an instance of the hook class, and takes one
parameter, the command to be executed. This is a subprogram parameter (see <a href="Subprogram-parameters.html#Subprogram-parameters">Subprogram parameters</a>).
</p>
<ul>
<li> GPS shell

<p>The command can be any GPS action (see <a href="Defining-Actions.html#Defining-Actions">Defining Actions</a>). The arguments for
the hook will be passed to the action, and are available as $1, $2, &hellip;;
In the following example, the message &quot;Just executed
the hook: project_changed&quot; will be printed in the Shell console. Note that we
are defining the action to be executed inline, but this could in fact be
defined in a separate XML customization file for convenience.
</p>
<div class="smallexample">
<pre class="smallexample">GPS&gt; parse_xml &quot;&quot;&quot;&lt;action name=&quot;my_action&quot;&gt;&lt;shell&gt;echo &quot;Just executed the hook&quot;&lt;/shell&gt;&lt;/action_name&gt;&quot;&quot;&quot;
GPS&gt; Hook project_changed
GPS&gt; Hook.add %1 &quot;my_action&quot;
</pre></div>

</li><li> Python

<p>The command must be a subprogram to execute. The arguments for the
hook will be passed to this subprogram. In the following example, the
message &quot;The hook project_changed was executed by GPS&quot; will be displayed in the
Python console whenever the project changes.
</p>
<div class="smallexample">
<pre class="smallexample">def my_callback (name):
    print &quot;The hook &quot; + name + &quot; was executed by GPS&quot;
GPS.Hook (&quot;project_changed&quot;).add (my_callback)
</pre></div>

</li></ul>

<p>The example above shows the simplest type of hook, which doesn&rsquo;t take any
argument. However, most hooks receive several parameters. For instance, the
hook &quot;file_edited&quot; receives the file name as a parameter.
</p>
<ul>
<li> GPS shell

<p>The following code will print the name of the hook (&quot;file_edited&quot;) and the
name of the file in the shell console every time a file is open by GPS.
</p><div class="smallexample">
<pre class="smallexample">GPS&gt; parse_xml &quot;&quot;&quot;&lt;action name=&quot;my_action&quot;&gt;&lt;shell&gt;echo name=$1 file=$2&lt;/shell&gt;&lt;/action&gt;&quot;&quot;&quot;
GPS&gt; Hook &quot;file_edited&quot;
GPS&gt; Hook.add %1 &quot;my_action&quot;
</pre></div>

</li><li> Python

<p>The following code prints the name of the file being edited by GPS in the
python console whenever a new editor is opened. The second argument is of type
GPS.File.
</p>
<div class="smallexample">
<pre class="smallexample">def my_file_callback (name, file):
    print &quot;Editing &quot; + file.name()
GPS.Hook (&quot;file_edited&quot;).add (my_file_callback)
</pre></div>

</li></ul>

<a name="Action-hooks"></a>
<h4 class="subsubsection">16.8.8.2 Action hooks</h4>
<a name="index-hooks_002c-action_005fhooks"></a>
<a name="index-hooks_002c-open_005ffile_005faction_005fhook"></a>

<p>Some hooks have a special use in GPS. Their name always ends with
&quot;_action_hook&quot;.
</p>
<p>As opposed to the standard hooks described in the previous section, the
execution of the action hooks stops as soon as one of the subprograms returns
a True value (&quot;1&quot; or &quot;true&quot;). The subprograms associated with that hook are
executed one after the other. If any such subprogram knows how to act for that
hook, it should do the appropriate action and return &quot;1&quot;.
</p>
<p>Other action hooks expect a string as a return value instead of a boolean. The
execution will stop when a subprogram returns a non-empty string.
</p>
<p>This mechanism is used extensively by GPS internally. For instance, whenever
a file needs to be opened in an editor, GPS executes the
&quot;open_file_action_hook&quot; hook to request its editing. Several modules are
connected to that hook.
</p>
<p>One of the first modules to be executed is the external editor module. If the
user has chosen to use an external editor, this module will simply spawn
Emacs or the external editor that the user has selected, and return 1. This
immediately stops the execution of the &quot;open_file_action_hook&quot;.
</p>
<p>However, if the user doesn&rsquo;t want to use external editors, this module will
return 0. This will keep executing the hook, and in particular will execute the
source editor module, which will always act and open an editor internally in
GPS.
</p>
<p>This is a very flexible mechanism. In your own script, you could choose to
have some special handling for files with a &quot;.foo&quot; extension for instance. If
the user wants to open such a file, you would spawn for instance an external
command (say &quot;my_editor&quot;) on this file, instead of opening it in GPS.
</p>
<p>This is done with a code similar to the following
</p>
<div class="smallexample">
<pre class="smallexample">from os.path import *
import os
def my_foo_handler (name, file, line, column, \
                    column_end, enable_nav, new_file, reload):
    if splitext (file.name())[1] == &quot;.foo&quot;:
        os.spawnv \
           (os.P_NOWAIT, &quot;/usr/bin/emacs&quot;, (&quot;emacs&quot;, file.name()))
        return 1   ## Prevent further execution of the hook
    return 0  ## Let other subprograms in the hook do their job

GPS.Hook (&quot;open_file_action_hook&quot;).add (my_foo_handler)
</pre></div>

<a name="Running-hooks"></a>
<h4 class="subsubsection">16.8.8.3 Running hooks</h4>
<a name="index-hooks_002c-Hook_002erun"></a>

<p>Any module in GPS is responsible for running the hooks when appropriate.
Most of the time, the subprograms exported by GPS to the scripting languages
will properly run the hook. But you might also need to run them in your own
scripts.
</p>
<p>As usual, this will result in the execution of all the functions bound to
that hook, whether they are defined in Ada or in any of the scripting
languages.
</p>
<p>This is done through the <b>Hook.run</b> command. This applies to an instance
of the Hook class, and a variable number of arguments
These must be in the right order and of the right type
for that specific type of hook.
</p>
<p>If you are running an action hook, the execution will stop as usual as soon
as one of the subprograms return a True value.
</p>
<p>The following example shows how to run a simple hook with no parameter, and
a more complex hook with several parameters. The latter will in fact request
the opening of an editor for the file in GPS, and thus has an immediately
visible effect on the interface. The file is opened at line 100. See the
description of the hook for more information on the other parameters.
</p>
<div class="smallexample">
<pre class="smallexample">GPS.Hook (&quot;project_changed&quot;).run()
GPS.Hook (&quot;open_file_action_hook&quot;).run \
              (GPS.File (&quot;test.adb&quot;), 100, 1, 0, 1, 1, 1)
</pre></div>

<a name="Creating-new-hooks"></a>
<h4 class="subsubsection">16.8.8.4 Creating new hooks</h4>
<a name="index-hooks_002c-creating"></a>
<a name="index-hooks_002c-Hook_002eregister"></a>

<p>The list of hooks known to GPS is fully dynamic. GPS itself declares a
number of hooks, mostly for its internal use although of course you can
also connect to them.
</p>
<p>But you can also create your own hooks to report events happening in your
own modules and programs. This way, any other script or GPS module can
react to these events.
</p>
<p>Such hooks can either be of a type exported by GPS, which constraints the
list of parameters for the callbacks, but make such hooks more portable and
secure; or they can be of a general type, which allows basically any kind
of parameters. In the latter case, checks are done at runtime to ensure that
the subprogram that is called as a result of running the hook has the right
number of parameters. If this isn&rsquo;t the case, GPS will complain and display
error messages. Such general hooks will also not pass their parameters to
other scripting languages.
</p>
<p>Creating new hooks is done through a call to <b>Hook.register</b>. This function
takes two arguments: the name of the hook you are creating,
and the type of the hook.
</p>
<p>The name of the hook is left to you. Any character is allowed in that name,
although using only alphanumerical characters is recommended.
</p>
<p>The type of the hook must be one of the following:
</p>
<ul>
<li> &quot;&quot; (the empty string)

<p>This indicates that the hook doesn&rsquo;t take any argument. None should be given
to <b>Hook.run</b>, and none should be
expected by the various commands connected to that hook, apart from
the hook name itself.
</p>
</li><li> one of the values returned by <b>Hook.list_types</b>

<p>This indicates that the hook is of one of the types exported by GPS itself. The
advantage of using such explicit types as opposed to &quot;general&quot; is that GPS
is able to make more tests for the validity of the parameters. Such hooks can
also be connected to from other scripting languages.
</p>
</li><li> &quot;general&quot;

<p>This indicates that the hook is of the general type that allows any number of
parameter, of any type. Other scripts will be able to connect to it, but will
not be executed when the hook is run if they do not expect the same number of
parameters that was given to <b>Hook.run</b>. Other scripts in other language will
only receive the hook name in parameter, not the full list of parameters.
</p>
</li></ul>

<p>A small trick worth noting: if the command bound to a hook doesn&rsquo;t have the
right number of parameters that this hook provide, the command will not be
executed and GPS will report an error. You can make sure that your command
will always be executed by either giving default values for its parameter, or
by using python&rsquo;s syntax to indicate a variable number of arguments.
</p>
<p>This is especially useful if you are connecting to a &quot;general&quot; hook, since you
do not really know in advance how many parameters the call of <b>Hook.run</b> will
provide.
</p>
<div class="smallexample">
<pre class="smallexample">## This callback can be connected to any type of hook
def trace (name, *args):
   print &quot;hook=&quot; + name

## This callback can be connected to hooks with one or two parameters
def trace2 (name, arg1, arg2=100):
   print &quot;hook=&quot; + str (arg1) + str (arg2)

Hook.register (&quot;my_custom_hook&quot;, &quot;general&quot;)
Hook (&quot;my_custom_hook&quot;).add (trace2)
Hook (&quot;my_custom_hook&quot;).run (1, 2) ## Prints 1 2
Hook (&quot;my_custom_hook&quot;).run (1)    ## Prints 1 100
</pre></div>

<hr>
<div class="header">
<p>
Previous: <a href="Python-FAQ.html#Python-FAQ" accesskey="p" rel="previous">Python FAQ</a>, Up: <a href="Scripting-GPS.html#Scripting-GPS" accesskey="u" rel="up">Scripting GPS</a> &nbsp; [<a href="Index-table.html#Index-table" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>