File: registry.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (239 lines) | stat: -rw-r--r-- 8,886 bytes parent folder | download | duplicates (3)
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
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2011-2020, University of Amsterdam
                              CWI, Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This module requires plregtry.ddl, for  which   the  sources  are in the
dlldemo directory.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- module(win_registry,
          [ registry_get_key/2,         % +Path, -Value
            registry_get_key/3,         % +Path, +Name, -Value
            registry_set_key/2,         % +Path, +Value
            registry_set_key/3,         % +Path, +Name, +Value
            registry_delete_key/1,      % +Path
            registry_lookup_key/3,      % +Path, +Access, -Key
            win_flush_filetypes/0,      % Flush changes filetypes to shell

            shell_register_file_type/4, % +Ext, +Type, +Name, +Open
            shell_register_file_type/5, % +Ext, +Type, +Name, +Open, +Icon
            shell_register_dde/6,       % +Type, +Action,
                                        % +Service, Topic, +DDECommand
                                        % +IfNotRunning
            shell_register_prolog/1     % +Extension
          ]).
:- autoload(library(lists),[member/2]).

:- use_foreign_library(foreign(plregtry)).      % load plregtry.ddl

                 /*******************************
                 *       REGISTER PROLOG        *
                 *******************************/

shell_register_prolog(Ext) :-
    current_prolog_flag(executable, Me),
    atomic_list_concat(['"', Me, '" "%1"'], OpenCommand),
    atom_concat(Me, ',0', Icon),
    shell_register_file_type(Ext, 'prolog.type', 'Prolog Source',
                             OpenCommand, Icon),
    shell_register_dde('prolog.type', consult,
                       prolog, control, 'consult(''%1'')', Me),
    shell_register_dde('prolog.type', edit,
                       prolog, control, 'edit(''%1'')', Me),
    win_flush_filetypes.


                 /*******************************
                 *     WINDOWS SHELL STUFF      *
                 *******************************/

%!  shell_register_file_type(+Ext, +Type, +Name, +Open) is det.
%!  shell_register_file_type(+Ext, +Type, +Name, +Open, +Icon) is det.
%
%   Register an extension to a type.  The open command for the type
%   is defined and files with this extension will be given Name as
%   their description in the explorer.  For example:
%
%   ==
%   ?- shell_register_file_type(pl, 'prolog.type', 'Prolog Source',
%                               '"c:\\pl\\bin\\plwin.exe" "%1"').
%   ==
%
%   The icon command is of the form File.exe,N or File.ico,0

shell_register_file_type(Ext, Type, Name, Open) :-
    ensure_dot(Ext, DExt),
    registry_set_key(classes_root/DExt, Type),
    registry_set_key(classes_root/Type, Name),
    registry_set_key(classes_root/Type/shell/open/command, Open),
    win_flush_filetypes.
shell_register_file_type(Ext, Type, Name, Open, Icon) :-
    shell_register_file_type(Ext, Type, Name, Open),
    registry_set_key(classes_root/Type/'DefaultIcon', Icon),
    win_flush_filetypes.

ensure_dot(Ext, Ext) :-
    atom_concat('.', _, Ext),
    !.
ensure_dot(Ext, DExt) :-
    atom_concat('.', Ext, DExt).

%!  shell_register_dde(+Type, +Action, +Service,
%!                     +Topic, +DDECommand, +IfNotRunning) is det.
%
%   Register a DDE command for the type.  The example below will
%   send DDE_EXECUTE command `consult('<File>') to the service
%   prolog, given the topic control.
%
%   ==
%   shell_register_dde('prolog.type', consult,
%                      prolog, control, 'consult(''%1'')',
%                      'c:\\pl\\bin\\plwin.exe -g "edit(''%1'')"').
%   ==

shell_register_dde(Type, Action, Service, Topic, DDECommand, IfNotRunning) :-
    registry_make_key(classes_root/Type/shell/Action/ddeexec,
                      all_access, EKey),
    registry_set_key(classes_root/Type/shell/Action/command, IfNotRunning),
    reg_set_value(EKey, '', DDECommand),
    registry_set_key(EKey/'Application', Service),
    registry_set_key(EKey/ifexec, ''),
    registry_set_key(EKey/topic, Topic),
    reg_close_key(EKey).

                 /*******************************
                 *        REGISTRY STUFF        *
                 *******************************/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
In the commands below, Path refers to   the path-name of the registry. A
path is a '/' separated description, where   the / should be interpreted
as a Prolog operator. For example, classes_root/'prolog.type'/shell. The
components should be atoms.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

%!  registry_set_key(+Path, +Value) is det.
%!  registry_set_key(+Path, +Name, +Value) is det.
%
%   Associate a (string) value with the key described by Path.  If
%   part of the path does not exist, the required keys will be created.

registry_set_key(Path, Value) :-
    registry_set_key(Path, '', Value).
registry_set_key(Path, Name, Value) :-
    registry_make_key(Path, write, Key, Close),
    reg_set_value(Key, Name, Value),
    Close.

%!  registry_get_key(+Path, -Value) is semidet.
%!  registry_get_key(+Path, +Name, -Value) is semidet.
%
%   Get the value associated with the given key.  If the key does not
%   exists, the predicate fails silently.

registry_get_key(Path, Value) :-
    registry_get_key(Path, '', Value).
registry_get_key(Path, Name, Value) :-
    registry_lookup_key(Path, read, Key, Close),
    (   reg_value(Key, Name, RawVal)
    ->  Close,
        Value = RawVal
    ;   Close,
        fail
    ).

%!  registry_delete_key(+Path)
%
%   Delete the gven key and all its subkeys and values.  Note that
%   the root-keys cannot be deleted.

registry_delete_key(Parent/Node) :-
    !,
    registry_lookup_key(Parent, all_access, PKey),
    (   reg_open_key(PKey, Node, all_access, Key)
    ->  delete_subkeys(Key),
        reg_close_key(Key),
        reg_delete_key(PKey, Node)
    ),
    reg_close_key(PKey).

delete_subkeys(Parent) :-
    reg_subkeys(Parent, Subs),
    forall(member(Sub, Subs),
           delete_subkey(Parent, Sub)).

delete_subkey(Parent, Sub) :-
    reg_open_key(Parent, Sub, all_access, Key),
    delete_subkeys(Key),
    reg_close_key(Key),
    reg_delete_key(Parent, Sub).

%!  registry_make_key(+Path, +Access, -Key)
%
%   Open the given key and create required keys if the path does not
%   exist.

registry_make_key(Path, Access, Key) :-
    registry_make_key(Path, Access, Key, _).

registry_make_key(A/B, Access, Key, Close) :-
    !,
    registry_make_key(A, Access, Parent, CloseParent),
    (   reg_open_key(Parent, B, Access, RawKey)
    ->  true
    ;   reg_create_key(Parent, B, '', [], Access, RawKey)
    ),
    CloseParent,
    Close = reg_close_key(RawKey),
    Key = RawKey.
registry_make_key(Key, _, Key, true).

%!  registry_lookup_key(+Path, +Access, -Key)
%
%   Open the given key, fail silently if the key doesn't
%   exist.

registry_lookup_key(Path, Access, Key) :-
    registry_lookup_key(Path, Access, Key, _).

registry_lookup_key(A/B, Access, Key, Close) :-
    !,
    registry_lookup_key(A, Access, Parent, CloseParent),
    reg_open_key(Parent, B, Access, RawKey),
    CloseParent,
    Close = reg_close_key(RawKey),
    Key = RawKey.
registry_lookup_key(Key, _, Key, true).