2013-03-23 00:43:44 +01:00
|
|
|
|
/* Copyright (C) 2013 Tom Willemsen <tom at ryuslash dot org>
|
2013-03-23 00:20:27 +01:00
|
|
|
|
|
|
|
|
|
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
|
2013-03-24 13:47:56 +01:00
|
|
|
|
// CLark.
|
2013-03-23 00:20:27 +01:00
|
|
|
|
|
|
|
|
|
/// Code:
|
|
|
|
|
|
|
|
|
|
define_variable("clark_program", "clark",
|
|
|
|
|
"The location of the clark executable.");
|
2013-03-24 21:24:31 +01:00
|
|
|
|
define_variable("clark_bookmarked_string", "+",
|
|
|
|
|
"Text to show if the current url has been bookmarked.");
|
|
|
|
|
define_variable("clark_not_bookmarked_string", "-",
|
|
|
|
|
"Text to show it the current url has not been"
|
|
|
|
|
+ " bookmarked.");
|
2013-03-23 00:20:27 +01:00
|
|
|
|
|
2013-08-26 23:47:29 +02:00
|
|
|
|
function clark_url_completer () {
|
|
|
|
|
keywords(arguments, $sort_order = "visitcount_descending");
|
|
|
|
|
var use_webjumps = arguments.$use_webjumps;
|
|
|
|
|
var use_history = arguments.$use_history;
|
|
|
|
|
var use_bookmarks = arguments.$use_bookmarks;
|
|
|
|
|
var sort_order = arguments.$sort_order;
|
|
|
|
|
var completers = [];
|
|
|
|
|
completers.push(file_path_completer());
|
|
|
|
|
if (url_completion_use_webjumps)
|
|
|
|
|
completers.push(webjump_completer());
|
|
|
|
|
completers.push(clark_complete);
|
|
|
|
|
if (url_completion_use_history)
|
|
|
|
|
completers.push(history_completer($use_history = true,
|
|
|
|
|
$sort_order = sort_order));
|
|
|
|
|
return merge_completers(completers);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 00:20:27 +01:00
|
|
|
|
define_browser_object_class(
|
|
|
|
|
"clark-bookmark", null,
|
|
|
|
|
function (I, prompt) {
|
|
|
|
|
check_buffer(I.buffer, content_buffer);
|
|
|
|
|
var result = yield I.buffer.window.minibuffer.read(
|
2013-08-26 23:47:29 +02:00
|
|
|
|
$prompt = prompt, $completer = clark_url_completer()
|
2013-03-23 00:20:27 +01:00
|
|
|
|
);
|
|
|
|
|
yield co_return(result);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2013-06-06 22:51:34 +02:00
|
|
|
|
// Add URL to clark, ask for a title (provide TITLE as a
|
|
|
|
|
// default), description and any number of tags.
|
2013-03-23 00:20:27 +01:00
|
|
|
|
function clark_add_url(I, url, title)
|
2013-06-06 22:51:34 +02:00
|
|
|
|
{ let url_string = load_spec_uri_string(load_spec(url));
|
2013-03-23 00:20:27 +01:00
|
|
|
|
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): "
|
|
|
|
|
);
|
2013-03-24 12:49:43 +01:00
|
|
|
|
let command = clark_program + ' add "' + url_string + '" "' + title
|
2013-03-23 00:20:27 +01:00
|
|
|
|
+ '" "' + description + '" \''
|
|
|
|
|
+ tags.split(',').map(function (str)
|
|
|
|
|
{ return str.trim(); }).join("' '")
|
|
|
|
|
+ "'";
|
2013-06-06 22:51:34 +02:00
|
|
|
|
let result = yield clark_shell_command(command);
|
|
|
|
|
|
|
|
|
|
yield co_return(result);
|
2013-03-23 00:20:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clark_add(I) {
|
|
|
|
|
check_buffer(I.buffer, content_buffer);
|
|
|
|
|
let result = yield clark_add_url(I, I.buffer.top_frame,
|
|
|
|
|
I.buffer.title);
|
|
|
|
|
|
2013-06-06 22:51:34 +02:00
|
|
|
|
if (result === "")
|
2013-03-23 00:20:27 +01:00
|
|
|
|
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);
|
2013-03-24 15:55:36 +01:00
|
|
|
|
var bo = yield read_browser_object(I);
|
2013-03-23 00:20:27 +01:00
|
|
|
|
let result = yield clark_add_url(I, encodeURIComponent(bo),
|
|
|
|
|
bo.textContent);
|
|
|
|
|
|
2013-06-06 22:51:34 +02:00
|
|
|
|
if (result === "")
|
2013-03-23 00:20:27 +01:00
|
|
|
|
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);
|
|
|
|
|
|
2013-03-24 21:24:31 +01:00
|
|
|
|
function clark_bookmarked_widget(window) {
|
|
|
|
|
this.class_name = "clark-bookmark-widget";
|
|
|
|
|
text_widget.call(this, window);
|
|
|
|
|
this.add_hook("current_content_buffer_location_change_hook");
|
|
|
|
|
this.add_hook("select_buffer_hook");
|
|
|
|
|
}
|
|
|
|
|
clark_bookmarked_widget.prototype = {
|
|
|
|
|
constructor: clark_bookmarked_widget,
|
|
|
|
|
__proto__: text_widget.prototype,
|
|
|
|
|
update: function () {
|
|
|
|
|
var obj = this;
|
|
|
|
|
function doit () {
|
|
|
|
|
var result = yield clark_exists_p(
|
|
|
|
|
obj.window.buffers.current.description
|
|
|
|
|
);
|
|
|
|
|
obj.view.text = result == "yes"
|
|
|
|
|
? clark_bookmarked_string
|
|
|
|
|
: clark_not_bookmarked_string;
|
|
|
|
|
}
|
|
|
|
|
co_call(doit());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-03-23 00:20:27 +01:00
|
|
|
|
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("") },
|
2013-03-24 21:24:31 +01:00
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) data += s || "") },
|
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) error += s || "") }]);
|
2013-03-23 00:20:27 +01:00
|
|
|
|
else
|
|
|
|
|
result = yield shell_command_with_argument(
|
|
|
|
|
clark_program + " --script search {}", str,
|
|
|
|
|
$fds = [{ output: async_binary_string_writer("") },
|
2013-03-24 21:24:31 +01:00
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) data += s || "") },
|
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) error += s || "") }]);
|
2013-03-23 00:20:27 +01:00
|
|
|
|
|
|
|
|
|
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,
|
2013-03-28 00:52:48 +01:00
|
|
|
|
get_string: function (i) ret[i][1],
|
|
|
|
|
get_description: function (i) ret[i][2],
|
|
|
|
|
get_input_state: function (i) [ret[i][0]] };
|
2013-03-23 00:20:27 +01:00
|
|
|
|
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);
|
|
|
|
|
|
2013-03-24 15:55:36 +01:00
|
|
|
|
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 + "' ";
|
|
|
|
|
|
2013-03-27 21:48:02 +01:00
|
|
|
|
var result = yield clark_shell_command(command);
|
|
|
|
|
|
2013-06-06 22:51:34 +02:00
|
|
|
|
if (result === "")
|
2013-03-27 21:48:02 +01:00
|
|
|
|
I.window.minibuffer.message("CLark done");
|
|
|
|
|
else
|
|
|
|
|
I.window.minibuffer.message("Error during CLark operation.");
|
2013-03-24 15:55:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
interactive("clark-edit", "Edit information for the current URL.",
|
|
|
|
|
clark_edit);
|
|
|
|
|
|
2013-03-24 21:24:31 +01:00
|
|
|
|
function clark_exists_p(url) {
|
|
|
|
|
let command = clark_program + ' exists "' + url + '"';
|
2013-03-24 13:42:20 +01:00
|
|
|
|
var data = "", error = "";
|
|
|
|
|
|
2013-03-24 15:55:36 +01:00
|
|
|
|
var result = yield shell_command(
|
2013-03-24 13:42:20 +01:00
|
|
|
|
command,
|
|
|
|
|
$fds = [{ output: async_binary_string_writer("") },
|
2013-03-24 21:24:31 +01:00
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) data += s || "") },
|
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) error += s || "") }]
|
2013-03-24 13:42:20 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (error != "")
|
|
|
|
|
throw new Error("Error occurred with CLark: " + error);
|
|
|
|
|
|
2013-03-24 21:24:31 +01:00
|
|
|
|
yield co_return(data.trim());
|
2013-03-24 13:42:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interactive("clark-exists-p", "Check to see if the current url"
|
2013-03-24 21:24:31 +01:00
|
|
|
|
+ " exists in the database.",
|
|
|
|
|
function (I) {
|
|
|
|
|
check_buffer(I.buffer, content_buffer);
|
|
|
|
|
let data = yield clark_exists_p(
|
|
|
|
|
load_spec_uri_string(load_spec(I.buffer.top_frame))
|
|
|
|
|
);
|
|
|
|
|
I.window.minibuffer.message(data);
|
|
|
|
|
});
|
2013-03-24 13:42:20 +01:00
|
|
|
|
|
2013-03-24 16:30:52 +01:00
|
|
|
|
function clark_remove(I) {
|
|
|
|
|
check_buffer(I.buffer, content_buffer);
|
|
|
|
|
|
|
|
|
|
let url_string =
|
|
|
|
|
load_spec_uri_string(load_spec(I.buffer.top_frame));
|
|
|
|
|
let command = clark_program + ' remove "' + url_string + '"';
|
2013-03-27 21:48:02 +01:00
|
|
|
|
let result = yield clark_shell_command(command);
|
|
|
|
|
|
2013-06-06 22:51:34 +02:00
|
|
|
|
if (result === "")
|
2013-03-27 21:48:02 +01:00
|
|
|
|
I.window.minibuffer.message("CLark done");
|
|
|
|
|
else
|
|
|
|
|
I.window.minibuffer.message("Error during CLark operation.");
|
2013-03-24 16:30:52 +01:00
|
|
|
|
}
|
|
|
|
|
interactive("clark-remove", "Remove the bookmark of the current URL"
|
|
|
|
|
+ " from the database.", clark_remove);
|
|
|
|
|
|
2013-03-24 16:43:32 +01:00
|
|
|
|
function clark_set_tags(I) {
|
|
|
|
|
check_buffer(I.buffer, content_buffer);
|
|
|
|
|
|
|
|
|
|
let url_string =
|
|
|
|
|
load_spec_uri_string(load_spec(I.buffer.top_frame));
|
|
|
|
|
let tags = yield I.minibuffer.read(
|
|
|
|
|
$prompt="tags (comma delimited): "
|
|
|
|
|
);
|
|
|
|
|
let command = clark_program + ' set-tags "' + url_string + '" '
|
|
|
|
|
+ tags.split(',').map(
|
|
|
|
|
function (str) { return "'" + str.trim() + "'"; }
|
|
|
|
|
).join(" ");
|
2013-03-27 21:48:02 +01:00
|
|
|
|
let result = yield clark_shell_command(command);
|
|
|
|
|
|
2013-06-06 22:51:34 +02:00
|
|
|
|
if (result === "")
|
2013-03-27 21:48:02 +01:00
|
|
|
|
I.window.minibuffer.message("CLark done");
|
|
|
|
|
else
|
|
|
|
|
I.window.minibuffer.message("Error during CLark operation.");
|
2013-03-24 16:43:32 +01:00
|
|
|
|
}
|
|
|
|
|
interactive("clark-set-tags", "Replace the tags for the bookmark of"
|
|
|
|
|
+ " the current URL.", clark_set_tags);
|
|
|
|
|
|
2013-06-04 00:35:52 +02:00
|
|
|
|
function clark_random(I)
|
|
|
|
|
{
|
|
|
|
|
check_buffer(I.buffer, content_buffer);
|
|
|
|
|
|
|
|
|
|
let tag = yield I.minibuffer.read($prompt="tag: ");
|
|
|
|
|
let command = clark_program + ' random';
|
|
|
|
|
if (tag != "")
|
|
|
|
|
command += ' ' + tag;
|
|
|
|
|
|
|
|
|
|
let result = yield clark_shell_command(command);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
I.buffer.load(result);
|
|
|
|
|
}
|
|
|
|
|
interactive("clark-random", "Open a random bookmark", clark_random);
|
|
|
|
|
|
2013-03-27 21:48:02 +01:00
|
|
|
|
function clark_shell_command(command)
|
|
|
|
|
{
|
|
|
|
|
let data = "", error = "";
|
2013-06-06 22:51:34 +02:00
|
|
|
|
let res = yield shell_command(
|
2013-03-27 21:48:02 +01:00
|
|
|
|
command,
|
|
|
|
|
$fds = [{ output: async_binary_string_writer("") },
|
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) data += s || ""
|
|
|
|
|
)},
|
|
|
|
|
{ input: async_binary_reader(
|
|
|
|
|
function (s) error += s || ""
|
|
|
|
|
)}]
|
|
|
|
|
);
|
|
|
|
|
|
2013-06-06 22:51:34 +02:00
|
|
|
|
yield co_return(res == 0 && error == "" && data);
|
2013-03-27 21:48:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 13:04:28 +01:00
|
|
|
|
define_keymap("clark_keymap");
|
2013-03-24 15:55:36 +01:00
|
|
|
|
|
2013-06-04 00:35:52 +02:00
|
|
|
|
define_key(clark_keymap, "#", "clark-random");
|
2013-03-24 15:55:36 +01:00
|
|
|
|
define_key(clark_keymap, "?", "clark-exists-p");
|
2013-03-24 13:04:28 +01:00
|
|
|
|
define_key(clark_keymap, "a", "clark-add");
|
2013-03-24 13:45:16 +01:00
|
|
|
|
define_key(clark_keymap, "A", "clark-add-link");
|
2013-03-24 15:55:36 +01:00
|
|
|
|
define_key(clark_keymap, "e", "clark-edit");
|
2013-03-24 13:45:16 +01:00
|
|
|
|
define_key(clark_keymap, "f", "clark-find-url");
|
|
|
|
|
define_key(clark_keymap, "F", "clark-find-url-new-buffer");
|
2013-03-24 16:30:52 +01:00
|
|
|
|
define_key(clark_keymap, "r", "clark-remove");
|
2013-03-24 16:43:32 +01:00
|
|
|
|
define_key(clark_keymap, "t", "clark-set-tags");
|
2013-03-23 00:20:27 +01:00
|
|
|
|
|
|
|
|
|
provide("clark");
|