1:- module(ejockey2, []).    2
    3:- use_module(pac(basic)).    4:- use_module(pac(reduce)).    5:- use_module(pac(meta)).    6:- use_module(misc(misc)).    7:- use_module(misc(file)).    8:- use_module(misc('prolog-elisp')).    9:- use_module(misc('emacs-handler')).   10:- use_module(misc(obj)).   11:- use_module(pac(op)).   12
   13term_expansion --> pac:expand_pac.
   14
   15:- discontiguous handle/3.   16
   17			/***********************
   18			*    YA Emacs Handler  *
   19			***********************/
   20%
   21handle([test1]) --> region,
   22	peek("Hello World!\n"),
   23	overwrite.
   24
   25handle([skk, user, jisyo]) -->
   26	{	Input = '~/Dropbox/skkdict/math-symbol.plist',
   27		Output = '~/Desktop/math-symbol-dummy.utf8',
   28		expand_file_name(Input, [In]),
   29		expand_file_name(Output, [Out]),
   30		plist_to_dict(In, Out)
   31	}.
   32%
   33plist_to_dict(In, Out):-
   34	load_structure(In, XML, []),
   35	parse_xml_structure(Dict0, [], XML, []),
   36	convert_to_skk_dict(Dict0, Dict),
   37	setup_call_cleanup(open(Out, write, S),
   38					  	(	write_head_lines(S),
   39							write_dict(S, Dict)
   40						),
   41					   close(S)).
   42
   43%
   44parse_xml_structure([key(A,B)|Out], Out0)-->[element(key, A,B)], !,
   45	parse_xml_structure(Out, Out0).
   46parse_xml_structure([string(A,B)|Out], Out0)-->[element(string, A, B)], !,
   47	parse_xml_structure(Out, Out0).
   48parse_xml_structure(Out, Out0)-->[element(_, _, X)], !,
   49	{ phrase(parse_xml_structure(Out, Out1), X, []) },
   50	parse_xml_structure(Out1, Out0).
   51parse_xml_structure(Out, Out0)-->[_], !, parse_xml_structure(Out, Out0).
   52parse_xml_structure(Out, Out)-->[].
   53
   54%
   55convert_to_skk_dict([], []).
   56convert_to_skk_dict([key(_, [K]), string(_, [S])|D], [A|D0]):- A=..[K, S],
   57	convert_to_skk_dict(D, D0).
   58%
   59head_lines(
   60	  [		";; -*- mode: fundamental; coding: utf-8 -*-\n",
   61			";;\n",
   62			";; okuri-ari entries.\n",
   63			";; okuri-nasi entries.\n"
   64	  ]).
   65%
   66write_head_lines(S):- head_lines(Hs), maplist(write(S), Hs).
   67%
   68write_dict(_, []).
   69write_dict(S, [phrase(P), shortcut(C)|R]):-
   70	format(S, "~w /~w/~n", [C, P]),
   71	write_dict(S, R).
   72%