View source with formatted comments or as raw
   1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
   2
   3    Author:        Jan Wielemaker and Anjo Anjewierden
   4    E-mail:        J.Wielemaker@cs.vu.nl
   5    WWW:           http://www.swi-prolog.nl/projects/xpce/
   6    Copyright (c)  1985-2011, University of Amsterdam
   7                              VU University Amsterdam
   8    All rights reserved.
   9
  10    Redistribution and use in source and binary forms, with or without
  11    modification, are permitted provided that the following conditions
  12    are met:
  13
  14    1. Redistributions of source code must retain the above copyright
  15       notice, this list of conditions and the following disclaimer.
  16
  17    2. Redistributions in binary form must reproduce the above copyright
  18       notice, this list of conditions and the following disclaimer in
  19       the documentation and/or other materials provided with the
  20       distribution.
  21
  22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33    POSSIBILITY OF SUCH DAMAGE.
  34*/
  35
  36:- module(start_emacs,
  37          [ emacs/0
  38          , emacs/1                             % x File
  39          , start_emacs/0
  40          , emacs_server/0
  41          , emacs_toplevel/0
  42          ]).
  43:- use_module(library(pce)).
  44:- require([ append/3
  45           , maplist/3
  46           , unix/1
  47           ]).
  48
  49:- pce_autoload(emacs,      library('emacs/emacs')).
  50:- pce_autoload(emacs_view, library('emacs/emacs')).
  51
  52:- pce_global(@emacs_buffers, new(dict)).
  53:- pce_global(@emacs, new(emacs(@emacs_buffers))).
  54
  55
  56/** <module> PceEmacs toplevel
  57
  58This module provides predicates to start  PceEmacs. PceEmacs is an clone
  59of GNU-Emacs written in  XPCE.  Modes  are   XPCE  classes  that  can be
  60extended in Prolog.
  61
  62@see    Set Prolog flag editor to pce_emacs to make PceEmacs the default
  63        for edit/1.
  64*/
  65
  66%!  start_emacs is det.
  67%
  68%   Create PceEmacs, but no buffers nor windows.
  69
  70start_emacs :-
  71    register_emacs,
  72    (   object(@emacs)
  73    ->  true
  74    ;   in_pce_thread_sync(send(@emacs, start))
  75    ).
  76
  77
  78%!  register_emacs is det.
  79%
  80%   If the user has not specified a specific editor and has started
  81%   PceEmacs, make it the default editor.
  82
  83register_emacs :-
  84    (   current_prolog_flag(editor, '$EDITOR')
  85    ->  set_prolog_flag(editor, pce_emacs)
  86    ;   true
  87    ).
  88
  89
  90%!  emacs_server is det.
  91%
  92%   Create a PceEmacs, ready to run as an unattended background
  93%   server.
  94
  95emacs_server :-
  96    start_emacs,
  97    send(@pce, trap_errors, @off),
  98    send(@pce, console_label, 'PceEmacs Server').
  99
 100%!  emacs is det.
 101%
 102%   Create PceEmacs and open the *scratch* buffer.
 103
 104emacs :-
 105    start_emacs,
 106    in_pce_thread((new(Scratch, emacs_buffer(@nil, '*scratch*')),
 107                   send(Scratch, open, tab))).
 108
 109%!  emacs(+Location) is det.
 110%
 111%   Create PceEmacs and edit  Location.  Location   is  one  of  the
 112%   following, where File must be an atom   and Line and LinePos are
 113%   integers.
 114%
 115%     - File:Line:LinePos
 116%     - File:Line
 117%     - File
 118
 119emacs(File:Line:LinePos) :-
 120    integer(Line),
 121    integer(LinePos),
 122    atom(File),
 123    !,
 124    start_emacs,
 125    new(Loc, source_location(File, Line)),
 126    send(Loc, attribute, linepos, LinePos),
 127    in_pce_thread(send(@emacs, goto_source_location, Loc, tab)).
 128emacs(File:Line) :-
 129    integer(Line),
 130    atom(File),
 131    !,
 132    start_emacs,
 133    in_pce_thread(send(@emacs, goto_source_location,
 134                       source_location(File, Line), tab)).
 135emacs(File) :-
 136    atom(File),
 137    !,
 138    start_emacs,
 139    in_pce_thread(send(@emacs, goto_source_location,
 140                       source_location(File), tab)).
 141emacs(File) :-
 142    domain_error(location, File).
 143
 144
 145%!  emacs_toplevel is det.
 146%
 147%   Prepare to run PceEmacs as a stand-alone executable.
 148
 149emacs_toplevel :-
 150    send(@pce, trap_errors, @off),
 151    current_prolog_flag(argv, Files),
 152    (   Files = [_|_]
 153    ->  start_emacs,
 154        maplist(make_buffer, Files, [B0|_]),
 155        send(B0, open)
 156    ;   emacs
 157    ).
 158
 159make_buffer(File, Buffer) :-
 160    new(Buffer, emacs_buffer(File)).