isearch-forward/backward: docstrings
[conkeror.git] / modules / clicks-in-new-buffer.js
blob40b5f99a931d0a3f6fd1d45d54aa48c573e9f8bf
1 /**
2  * (C) Copyright 2008 Nicholas Zigarovich
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 define_variable("clicks_in_new_buffer_button", 1,
9                 "Which mouse button should open links in a new buffer. " +
10                 "0 = left, 1 = middle, 2 = right. Default is 1.");
12 define_variable("clicks_in_new_buffer_target", OPEN_NEW_BUFFER,
13                 "How to open links in a new buffer, in the foreground or " +
14                 "the background. Set to one the constants OPEN_NEW_BUFFER " +
15                 "or OPEN_NEW_BUFFER_BACKGROUND. Default is OPEN_NEW_BUFFER.");
17 // Should mouse click event propagation be stopped?
18 var clicks_in_new_buffer_ev_stop_prop = true;
20 function find_tag_in_parents (tag, element) {
21     // FIXME If tag names will always be upper-case, toLowerCase() can
22     //       be eliminated. Also not sure that p will ever be null.
23     tag = tag.toLowerCase();
24     for (let p = element.parentNode;
25          p != null && p.tagName.toLowerCase() != "html";
26          p = p.parentNode)
27     {
28         if (p.tagName.toLowerCase() == tag)
29             return p;
30     }
31     return null;
34 function open_link_in_new_buffer (event) {
35     if (event.button != clicks_in_new_buffer_button)
36         return;
37     let element = event.target;
38     let anchor = null;
39     if (element instanceof Ci.nsIDOMHTMLAnchorElement ||
40         element instanceof Ci.nsIDOMHTMLAreaElement)
41     {
42         anchor = element;
43     } else
44         anchor = find_tag_in_parents("a", element);
45     if (anchor == null)
46         return;
47     event.preventDefault();
48     if (clicks_in_new_buffer_ev_stop_prop)
49         event.stopPropagation();
50     let spec = load_spec(anchor);
51     let window = this.ownerDocument.defaultView;
52     let buffer = window.buffers.current;
53     create_buffer(window,
54                   buffer_creator(content_buffer,
55                                  $opener = buffer,
56                                  $load = spec),
57                   clicks_in_new_buffer_target);
60 function clicks_in_new_buffer_add_listener (buffer) {
61     buffer.browser.addEventListener("click",
62                                     open_link_in_new_buffer,
63                                     true);
66 function clicks_in_new_buffer_remove_listener (buffer) {
67     buffer.browser.removeEventListener("click",
68                                        open_link_in_new_buffer,
69                                        true);
72 function clicks_in_new_buffer_mode_enable () {
73     add_hook("create_buffer_hook",
74              clicks_in_new_buffer_add_listener);
75     for_each_buffer(clicks_in_new_buffer_add_listener);
78 function clicks_in_new_buffer_mode_disable () {
79     remove_hook("create_buffer_hook",
80                 clicks_in_new_buffer_add_listener);
81     for_each_buffer(clicks_in_new_buffer_remove_listener);
84 define_global_mode("clicks_in_new_buffer_mode",
85                    clicks_in_new_buffer_mode_enable,
86                    clicks_in_new_buffer_mode_disable);
88 clicks_in_new_buffer_mode(true);
90 provide("clicks-in-new-buffer");