aboutsummaryrefslogtreecommitdiffstats
path: root/clark.js
blob: 3620f7ed3d8e62892b60ccc84e3a5a44312505d8 (plain)
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
/* Copyright (C) 2013  Tom Willemsen <tom at ryuslash dot org>

   This file is part of CLark

   CLark is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   CLark is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with CLark. If not, see <http://www.gnu.org/licenses/>.
 */
/// Commentary:

// A wrapper script that should allow conkeror to interface with
// CLark.

/// Code:

define_variable("clark_program", "clark",
                "The location of the clark executable.");

define_browser_object_class(
    "clark-bookmark", null,
    function (I, prompt) {
        check_buffer(I.buffer, content_buffer);
        var result = yield I.buffer.window.minibuffer.read(
            $prompt = prompt, $completer = clark_complete
        );
        yield co_return(result);
    }
);

function clark_add_url(I, url, title)
{   // Add URL to clark, ask for a title (provide TITLE as a
    // default), description and any number of tags.
    let url_string = load_spec_uri_string(load_spec(url));
    let title = yield I.minibuffer.read($prompt="name (required): ",
                                        $initial_value=title);
    let description = yield I.minibuffer.read(
        $prompt="extended description: "
    );
    let tags = yield I.minibuffer.read(
        $prompt="tags (comma delimited): "
    );
    let command = clark_program + ' add "' + url_string + '" "' + title
            + '" "' + description + '" \''
            + tags.split(',').map(function (str)
                                  { return str.trim(); }).join("' '")
            + "'";
    yield shell_command(command);
}

function clark_add(I) {
    check_buffer(I.buffer, content_buffer);
    let result = yield clark_add_url(I, I.buffer.top_frame,
                                        I.buffer.title);

    if (!result)
        I.window.minibuffer.message('Added to clark');
    else
        I.window.minibuffer.message('Couldn\'t add to clark');
}
interactive("clark-add",
            "Bookmark the current page in clark",
            clark_add);

function clark_add_link(I) {
    check_buffer(I.buffer, content_buffer);
    var bo = yield read_browser_object(I);
    let result = yield clark_add_url(I, encodeURIComponent(bo),
                                        bo.textContent);

    if (!result)
        I.window.minibuffer.message('Added to clark');
    else
        I.window.minibuffer.message('Couldn\'t add to clark');
}
interactive("clark-add-link",
            "Select and bookmark a link in clark",
            clark_add_link);

function clark_complete(input, pos, conservative)
{
    if (pos == 0 && conservative)
        yield co_return(undefined);

    let str = input.substring(0, pos);

    var data = "", error = "", ret = [];
    var result = 0;

    if (str == "")
        result = yield shell_command(
            clark_program + " --script",
            $fds = [{ output: async_binary_string_writer("") },
                    { input: async_binary_reader(function (s) data += s || "") },
                    { input: async_binary_reader(function (s) error += s || "") }]);
    else
        result = yield shell_command_with_argument(
            clark_program + " --script search {}", str,
            $fds = [{ output: async_binary_string_writer("") },
                    { input: async_binary_reader(function (s) data += s || "") },
                    { input: async_binary_reader(function (s) error += s || "") }]);

    if (result != 0 || error != "")
        throw new Error("result: " + result + ", error: " + error);
    else if (data != "") {
        data.split('').forEach(function (row) {
            ret.push(row.split(''));
        });

        let c = { count: ret.length,
                  get_string: function (i) ret[i][0],
                  get_description: function (i) ret[i][1],
                  get_input_state: function (i) [ret[i][2]] };
        yield co_return(c);
    }
}

interactive("clark-find-url",
            "Find a page from clark in the current buffer",
            "find-url",
            $browser_object = browser_object_clark_bookmark);
interactive("clark-find-url-new-buffer",
            "Find a page from clark in a new buffer",
            "find-url-new-buffer",
            $browser_object = browser_object_clark_bookmark);

function clark_edit(I) {
    check_buffer(I.buffer, content_buffer);

    let url_string =
            load_spec_uri_string(load_spec(I.buffer.top_frame));
    let title = yield I.minibuffer.read(
        $prompt="name (leave empty to skip): ",
        $initial_value=I.buffer.title
    );
    let description = yield I.minibuffer.read(
        $prompt="description (leave empty to skip): "
    );
    let command = clark_program + ' edit "' + url_string + '" ';

    if (title == "" && description == "")
        I.window.minibuffer.message('Nothing to change');
    else {
        if (title != "")
            command += "--name '" + title + "' ";
        if (description != "")
            command += "--description '" + description + "' ";

        var result = yield shell_command(command);
        I.window.minibuffer.message("CLark done");
    }
}
interactive("clark-edit", "Edit information for the current URL.",
            clark_edit);

function clark_exists_p(I) {
    check_buffer(I.buffer, content_buffer);

    let url_string =
            load_spec_uri_string(load_spec(I.buffer.top_frame));
    let command = clark_program + ' exists "' + url_string + '"';
    var data = "", error = "";

    var result = yield shell_command(
        command,
        $fds = [{ output: async_binary_string_writer("") },
                { input: async_binary_reader(function (s) data += s || "") },
                { input: async_binary_reader(function (s) error += s || "") }]
    );

    if (error != "")
        throw new Error("Error occurred with CLark: " + error);

    I.window.minibuffer.message(data);
}

interactive("clark-exists-p", "Check to see if the current url"
            + " exists in the database.", clark_exists_p);

define_keymap("clark_keymap");

define_key(clark_keymap, "?", "clark-exists-p");
define_key(clark_keymap, "a", "clark-add");
define_key(clark_keymap, "A", "clark-add-link");
define_key(clark_keymap, "e", "clark-edit");
define_key(clark_keymap, "f", "clark-find-url");
define_key(clark_keymap, "F", "clark-find-url-new-buffer");

provide("clark");