summaryrefslogtreecommitdiff
path: root/src/command.c
blob: d4d3502b026fed35637b540c8293135e8535ebde (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
198
199
200
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2014 Daniel Carl
 *
 * This program 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.
 *
 * This program 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 this program. If not, see http://www.gnu.org/licenses/.
 */

/**
 * This file contains functions that are used by normal mode and command mode
 * together.
 */
#include "config.h"
#include "main.h"
#include "command.h"
#include "history.h"
#include "bookmark.h"

extern VbCore vb;

gboolean command_search(const Arg *arg)
{
    static short dir;   /* last direction 1 forward, -1 backward*/
    const char *query;
    static gboolean newsearch = true;
    gboolean forward;

    if (arg->i == 0) {
#ifdef FEATURE_SEARCH_HIGHLIGHT
        webkit_web_view_unmark_text_matches(vb.gui.webview);
#endif
        newsearch = true;
        return true;
    }

    /* copy search query for later use */
    if (arg->s) {
        /* set dearch dir only when the searching is started */
        dir   = arg->i > 0 ? 1 : -1;
        query = arg->s;
    } else {
        /* no search phrase given - continue a previous search */
        query = vb_register_get('/');
    }

    forward = (arg->i * dir) > 0;

    if (query) {
        unsigned int count = abs(arg->i);
        if (newsearch) {
#ifdef FEATURE_SEARCH_HIGHLIGHT
            /* highlight matches if the search is started new or continued
             * after switch to normal mode which calls this function with
             * COMMAND_SEARCH_OFF */
            webkit_web_view_mark_text_matches(vb.gui.webview, query, false, 0);
            webkit_web_view_set_highlight_text_matches(vb.gui.webview, true);
#endif

            newsearch = false;
            /* skip first search because this is done during typing in ex
             * mode, else the search wil mark the next match as active */
            if (count) {
                count -= 1;
            }
        }

        while (count--) {
            if (!webkit_web_view_search_text(vb.gui.webview, query, false, forward, true)) {
                break;
            }
        };
    }

    return true;
}

gboolean command_yank(const Arg *arg, char buf)
{
    static char *tmpl = "Yanked: %s";

    if (arg->i == COMMAND_YANK_SELECTION) {
        char *text = NULL;
        /* copy current selection to clipboard */
        webkit_web_view_copy_clipboard(vb.gui.webview);
        text = gtk_clipboard_wait_for_text(PRIMARY_CLIPBOARD());
        if (!text) {
            text = gtk_clipboard_wait_for_text(SECONDARY_CLIPBOARD());
        }
        if (text) {
            /* put the text into the yank buffer */
            if (strchr(VB_USER_REG, buf)) {
                vb_register_add(buf, text);
            }
            vb_echo(VB_MSG_NORMAL, false, tmpl, text);
            g_free(text);

            return true;
        }

        return false;
    }

    Arg a = {VB_CLIPBOARD_PRIMARY|VB_CLIPBOARD_SECONDARY};
    if (arg->i == COMMAND_YANK_URI) {
        /* yank current uri */
        a.s = (char*)GET_URI();
    } else {
        /* use current arg.s as new clipboard content */
        a.s = arg->s;
    }
    if (a.s) {
        /* put the text into the yank buffer */
        vb_set_clipboard(&a);
        if (strchr(VB_USER_REG, buf)) {
            vb_register_add(buf, a.s);
        }
        vb_echo(VB_MSG_NORMAL, false, tmpl, a.s);

        return true;
    }

    return false;
}

gboolean command_save(const Arg *arg)
{
    WebKitDownload *download;
    const char *uri, *path = NULL;

    if (arg->i == COMMAND_SAVE_CURRENT) {
        uri = GET_URI();
        /* given string is the path to save the download to */
        if (arg->s && *(arg->s) != '\0') {
            path = arg->s;
        }
    } else {
        uri = arg->s;
    }

    if (!uri || !*uri) {
        return false;
    }

    download = webkit_download_new(webkit_network_request_new(uri));
    vb_download(vb.gui.webview, download, path);

    return true;
}

#ifdef FEATURE_QUEUE
gboolean command_queue(const Arg *arg)
{
    gboolean res = false;
    int count = 0;
    char *uri;

    switch (arg->i) {
        case COMMAND_QUEUE_POP:
            if ((uri = bookmark_queue_pop(&count))) {
                res = vb_load_uri(&(Arg){VB_TARGET_CURRENT, uri});
                g_free(uri);
            }
            vb_echo(VB_MSG_NORMAL, false, "Queue length %d", count);
            break;

        case COMMAND_QUEUE_PUSH:
            res = bookmark_queue_push(arg->s ? arg->s : GET_URI());
            if (res) {
                vb_echo(VB_MSG_NORMAL, false, "Pushed to queue");
            }
            break;

        case COMMAND_QUEUE_UNSHIFT:
            res = bookmark_queue_unshift(arg->s ? arg->s : GET_URI());
            if (res) {
                vb_echo(VB_MSG_NORMAL, false, "Pushed to queue");
            }
            break;

        case COMMAND_QUEUE_CLEAR:
            if (bookmark_queue_clear()) {
                vb_echo(VB_MSG_NORMAL, false, "Queue cleared");
            }
            break;
    }

    return res;
}
#endif