1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
2
3    Author:        Jan Wielemaker and Anjo Anjewierden
4    E-mail:        jan@swi.psy.uva.nl
5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
6    Copyright (c)  1985-2002, University of Amsterdam
7    All rights reserved.
8
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12
13    1. Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15
16    2. Redistributions in binary form must reproduce the above copyright
17       notice, this list of conditions and the following disclaimer in
18       the documentation and/or other materials provided with the
19       distribution.
20
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32    POSSIBILITY OF SUCH DAMAGE.
33*/
34
35:- module(emacs_buffer_menu, []).
36:- use_module(library(pce)).
37:- use_module(library(persistent_frame)).
38:- require([ send_list/3
39           ]).
40
41:- pce_autoload(tool_bar, library(toolbar)).
42
43resource(open,      image, image('16x16/open.xpm')).
44resource(saveall,   image, image('16x16/saveall.xpm')).
45resource(help,      image, image('16x16/help.xpm')).
46resource(bookmarks, image, image('16x16/bookmarks.xpm')).
47resource(buffers,   image, image('32x32/buffers.xpm')).
48
49:- pce_begin_class(emacs_buffer_menu, persistent_frame,
50                   "List showing all PceEmacs buffers").
51
52class_variable(geometry,        geometry,       '211x190+0+125').
53
54initialise(BM, Emacs:emacs) :->
55    "Create menu for buffer-list"::
56    send(BM, send_super, initialise,
57         'PCE Emacs Buffers', application := Emacs),
58    send(BM, icon, resource(buffers)),
59    send(BM, name, buffer_menu),
60    send(BM, append, new(D, dialog)),
61    send(D, pen, 0),
62    send(D, gap, size(0, 3)),
63    send(D, append, new(TB, tool_bar(Emacs))),
64    send_list(TB, append,
65              [ tool_button(find_file,
66                            resource(open),
67                            'Open file for editing'),
68                tool_button(save_some_buffers,
69                            resource(saveall),
70                            'Save all modified buffers'),
71                tool_button(show_bookmarks,
72                            resource(bookmarks),
73                            'Show bookmarks'),
74                tool_button(help,
75                            resource(help),
76                            'Help on PceEmacs')
77              ]),
78
79    send(new(B, emacs_buffer_browser(Emacs)), below, D),
80    send(new(report_dialog), below, B).
81
82selection(BM, B:emacs_buffer*) :->
83    "Select emacs buffer"::
84    get(BM, member, browser, Browser),
85    (   B == @nil
86    ->  send(Browser, selection, @nil)
87    ;   get(B, name, Name),
88        get(Browser, member, Name, DictItem),
89        send(Browser, insert_after, DictItem, @nil), % move to top
90        send(Browser, selection, DictItem)
91    ).
92
93:- pce_end_class(emacs_buffer_menu).
94
95:- pce_begin_class(emacs_buffer_browser, browser,
96                   "Browse the emacs buffers").
97
98initialise(B, Emacs:emacs) :->
99    "Create for Emacs"::
100    send_super(B, initialise, 'Emacs buffers'),
101    send(B, name, browser),
102    send(B, open_message, message(@arg1?object, open)),
103    send(B, tab_stops, vector(150)),
104    send(B, attach_popup),
105    send(B, dict, Emacs?buffer_list).
106
107typed(B, Ev:event, Delegate:[bool]) :->
108    "Map DEL and backspace to kill selected buffer"::
109    (   (   get(Ev, id, 'DEL')
110        ;   get(Ev, id, backspace)
111        ),
112        get(B, selection, _)
113    ->  send(B, kill_selection)
114    ;   send_super(B, typed, Ev, Delegate)
115    ).
116
117kill_selection(B) :->
118    get(B, selection, DI),
119    send(DI?object, kill).
120
121attach_popup(B) :->
122    "Attach the popup menu"::
123    send(B, popup, new(P, popup)),
124
125    new(Buffer, @arg1?object),
126    send(P, update_message,
127         message(B, selection, @arg1)),
128    send_list(P, append,
129              [ menu_item(open_buffer,
130                          message(Buffer, open)),
131                menu_item(open_new_window,
132                          message(Buffer, open, @on),
133                          @default, @on),
134                menu_item(identify,
135                          message(Buffer, identify),
136                          @default, @on),
137                menu_item(kill_buffer,
138                          message(Buffer, kill))
139              ]).
140
141
142drop_files(B, Files:chain, _At:point) :->
143    "Drag-and-drop interface"::
144    get(B, application, Emacs),
145    send(Files, for_all,
146         message(Emacs, open_file, @arg1)).
147
148:- pce_end_class(emacs_buffer_browser).
149
150
151
152
153