File: swi_preferences.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 (149 lines) | stat: -rw-r--r-- 4,890 bytes parent folder | download | duplicates (2)
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
/*  Part of XPCE --- The SWI-Prolog GUI toolkit

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        J.Wielemaker@cs.vu.nl
    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    Copyright (c)  2002-2015, University of Amsterdam
                              VU University 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.
*/

:- module(prolog_preferences,
          [ prolog_edit_preferences/1   % +What
          ]).
:- use_module(library(pce)).
:- use_module(library(pce_tick_box)).
:- autoload(library(lists), [member/2]).
:- autoload(library(pce_emacs), [start_emacs/0]).
:- autoload(library(swi_compatibility), [auto_call/1]).

/** <module> Edit preferences files

This  module  provides  prolog_edit_preferences/1,  which   is  used  to
simplify locating the preference files and provide a default if the user
has no such file.

@see    library(win_menu) binds this to the Settings menu of the console on
        the MS-Windows version.
*/

%!  prolog_edit_preferences(+What) is det.
%
%   Edit the specified user preference file.  What is one of
%
%       * =xpce=
%       * =prolog=
%
%   The UI components are started asynchronously in the XPCE thread.

prolog_edit_preferences(What) :-
    in_pce_thread(pce_edit_preferences(What)).

pce_edit_preferences(What) :-
    locate_preferences(What, File),
    auto_call(start_emacs),
    (   \+ access_file(File, exist)
    ->  send(@display, confirm,
             'Preferences file %s doesn''t exist.\nCreate it?', File),
        (   default_preferences(What, DefFile)
        ->  copy_file(DefFile, File)
        ;   true
        )
    ;   access_file(File, write)
    ->  true
    ;   send(@display, inform,
             'You cannot modify the preferences file %s', File)
    ),
    send(@emacs, goto_source_location, File).

locate_preferences(xpce, File) :-
    ensure_xpce_config_dir(Dir),
    get(string('%s/Defaults', Dir), value, File).
locate_preferences(prolog, File) :-
    prolog_init_file(Base),
    member(Access, [read,write]),
    absolute_file_name(user_app_config(Base), File,
                       [ access(Access),
                         file_errors(fail)
                       ]),
    !.

%!  prolog_init_file(-Base)
%
%   Get the base-name of the Prolog user initialization file.
%
%   @tbd This should have a public interface.

:- if(current_predicate('$cmd_option_val'/2)).
prolog_init_file(Base) :-
    '$cmd_option_val'(init_file, Base).
:- else.
prolog_init_file(Base) :-
    '$option'(init_file, Base).
:- endif.
prolog_init_file('init.pl').


%!  default_preferences(+Id, -File)
%
%   If there is a default file for the preferences, return a path to
%   it, so the user can be presented a starting point.

default_preferences(prolog, File) :-
    member(Location,
           [ swi('customize/swipl.ini'),
             swi('customize/dotswiplrc')
           ]),
    absolute_file_name(Location, File,
                       [ access(read),
                         file_errors(fail)
                       ]),
    !.
default_preferences(xpce, File) :-
    absolute_file_name(pce('Defaults.user'), File,
                       [ access(read),
                         file_errors(fail)
                       ]),
    !.


%!  ensure_xpce_config_dir(-Dir:atom)
%
%   Ensure existence of the personal XPCE config directory.

ensure_xpce_config_dir(Dir) :-
    get(@pce, application_data, AppDir),
    (   send(AppDir, exists)
    ->  true
    ;   send(AppDir, make)
    ),
    get(AppDir, path, Dir).


copy_file(From, To) :-
    send(file(To), copy, From).