summary refs log tree commit diff stats
path: root/Makefile
Commit message (Expand)AuthorAgeFilesLines
* Makefile, setup.py: Fixed `make doc`hut2010-09-111-1/+0
* Makefile: simplificationhut2010-06-241-1/+1
* make doc: minor cleanup and fixhut2010-06-211-2/+2
* commited pydocs are author agnosticshpelda2010-06-211-1/+2
* INSTALL: changed logname from uninstall_info to install_log.txthut2010-06-181-1/+1
* Removed built-in support for uninstalling.hut2010-06-181-5/+4
* Rewrote INSTALL and "make (un)install"hut2010-06-181-2/+12
* Makefile: removed functions which are now handled by setup.pyhut2010-06-181-51/+4
* make snapshot: prefix ranger-$VERSION/ in archiveshut2010-06-141-1/+1
* make install: Fixed script for finding PYTHON_SITE_DESThut2010-06-141-2/+6
* Makefile: dynamically get the version numberhut2010-06-091-1/+1
* simplified all_tests.py, moved to test/hut2010-06-071-2/+2
* make bm, make test: make use of PYTHON variablehut2010-05-251-2/+2
* make: replace literal python with $(PYTHON)hut2010-05-171-1/+1
* make: added DESTDIR to Makefilehut2010-05-171-9/+9
* Makefile: updatedhut2010-05-101-9/+12
* Added license header to a few fileshut2010-05-041-0/+15
* Makefile: added `make bm` to run all benchmarks BMCOUNT timeshut2010-05-031-2/+5
* Makefile: cleaned up, added automatic uninstallhut2010-05-031-58/+57
* make clean: fixedhut2010-04-071-1/+1
* Makefile: improvementshut2010-04-031-2/+2
* clean uphut2010-03-311-12/+0
* make test: reduce verbosityhut2010-03-261-1/+1
* fixed make cleanhut2010-03-251-1/+1
* make clean: safer method of cleaninghut2010-03-221-1/+1
* make compile: create pyc files toohut2010-03-221-1/+2
* make test: don't use "clean" before "test"hut2010-03-211-1/+1
* make install: chmod after copyinghut2010-03-141-0/+2
* incremented verison number v1.0.4hut2010-03-121-1/+1
* make install: catch an exceptionhut2010-03-121-0/+4
* make push: removed "hut" repohut2010-03-121-1/+0
* misc changes, make installhut2010-03-121-7/+34
* make snapshot: changed the format of the filenamehut2010-03-081-2/+3
* make minimal_snapshot: snapshot with only neccessary fileshut2010-03-081-1/+13
* make clean: fixedhut2010-02-281-2/+2
* Makefile: added make info + some improvementshut2010-02-271-7/+22
* added make snapshothut2010-02-251-0/+4
* make clean: delete pyo files as well.hut2010-02-251-1/+1
* added Makefilehut2010-02-251-0/+39
bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Copyright (c) 2012 Elias Norberg <xyzzy@kudzu.se>
 * Copyright (c) 2012 Josh Rickmar <jrick@devio.us>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <xombrero.h>

#if WEBKIT_CHECK_VERSION(1, 5, 0)
	/* we got the DOM API we need */

struct edit_src_cb_args {
		WebKitWebFrame		*frame;
		WebKitWebDataSource	*data_src;
};

struct external_editor_args {
		GPid		child_pid;
		char		*path;
		time_t		mtime;
		struct tab	*tab;
		int		(*callback)(const char *,gpointer);
		gpointer	cb_data;
};

int
update_contents(struct external_editor_args *args)
{
	struct stat		st;
	int			fd = -1;
	int			rv, nb;
	GString			*contents = NULL;
	char			buf[XT_EE_BUFSZ];

	rv = stat(args->path, &st);
	if (rv == -1 && errno == ENOENT)
		return (1);
	else if (rv == 0 && st.st_mtime > args->mtime) {
		DPRINTF("File %s has been modified\n", args->path);
		args->mtime = st.st_mtime;

		contents = g_string_sized_new(XT_EE_BUFSZ);
		fd = open(args->path, O_RDONLY);
		if (fd == -1) {
			DPRINTF("open_external_editor_cb, open error, %s\n",
			    strerror(errno));
			goto done;
		}

		for (;;) {
			nb = read(fd, buf, XT_EE_BUFSZ);
			if (nb < 0) {
				g_string_free(contents, TRUE);
				show_oops(args->tab, strerror(errno));
				goto done;
			}

			buf[nb] = '\0';
			contents = g_string_append(contents, buf);

			if (nb < XT_EE_BUFSZ)
				break;
		}
		close(fd);

		DPRINTF("external_editor_cb: contents updated\n");
		if (args->callback)
			args->callback(contents->str, args->cb_data);

		g_string_free(contents, TRUE);

		return (0);
	}

done:
	if (fd != -1)
		close(fd);
	return (0);
}

void
external_editor_closed(GPid pid, gint status, gpointer data)
{
	struct external_editor_args	*args;
	struct tab			*t;
	int				found_tab = 0;

	args = (struct external_editor_args *)data;

	TAILQ_FOREACH(t, &tabs, entry)
		if (t == args->tab) {
			found_tab = 1;
			break;
		}

	/* Tab was deleted */
	if (!found_tab)
		goto done;

	/*
	 * unfortunately we can't check the exit status in glib < 2.34,
	 * otherwise a check and warning would be nice here
	 */
	update_contents(args);

done:
	unlink(args->path);
	g_spawn_close_pid(pid);	
}

int
open_external_editor_cb(gpointer data)
{
	struct external_editor_args	*args;
	struct tab			*t;
	int				found_tab = 0;

	args = (struct external_editor_args*)data;

	/* Check if tab is still open */
	TAILQ_FOREACH(t, &tabs, entry)
		if (t == args->tab) {
			found_tab = 1;
			break;
		}

	/* Tab was deleted */
	if (!found_tab)
		goto done;

	if (update_contents(args))
		goto done;

	return (1);
done:
	/* cleanup and remove from event loop */
	g_free(args->path);
	g_free(args->cb_data);
	g_free(args);

	return (0);
}

int
open_external_editor(struct tab *t, const char *contents,
    int (*callback)(const char *, gpointer), gpointer cb_data)
{
	struct stat			st;
	struct external_editor_args	*a;
	GPid				pid;
	char				*cmdstr;
	char				filename[PATH_MAX];
	char				**sv;
	int				fd;
	int				nb, rv;
	int				cnt;

	if (external_editor == NULL)
		return (0);

	snprintf(filename, sizeof filename, "%s" PS "xombreroXXXXXX", temp_dir);

	/* Create a temporary file */
	fd = g_mkstemp(filename);
	if (fd == -1) {
		show_oops(t, "Cannot create temporary file");
		return (1);
	}

	nb = 0;
	while (contents && nb < strlen(contents)) {
		if (strlen(contents) - nb > XT_EE_BUFSZ)
			cnt = XT_EE_BUFSZ;
		else
			cnt = strlen(contents) - nb;

		rv = write(fd, contents+nb, cnt);
		if (rv < 0) {
			close(fd);
			show_oops(t,strerror(errno));
			return (1);
		}

		nb += rv;
	}

	rv = fstat(fd, &st);
	if (rv == -1) {
		close(fd);
		show_oops(t,"Cannot stat file: %s\n", strerror(errno));
		return (1);
	}
	close(fd);

	DPRINTF("edit_src: external_editor: %s\n", external_editor);

	sv = g_strsplit(external_editor, "<file>", -1);
	cmdstr = g_strjoinv(filename, sv);
	g_strfreev(sv);
	sv = g_strsplit_set(cmdstr, " \t", -1);
	if (!g_spawn_async(NULL, sv, NULL,
	    (G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), NULL, NULL, &pid,
	    NULL)) {
		show_oops(t, "%s: could not spawn process");
		g_strfreev(sv);
		g_free(cmdstr);
		return (1);
	}

	g_strfreev(sv);
	g_free(cmdstr);

	a = g_malloc(sizeof(struct external_editor_args));
	a->child_pid = pid;
	a->path = g_strdup(filename);
	a->tab = t;
	a->mtime = st.st_mtime;
	a->callback = callback;
	a->cb_data = cb_data;

	/* Check every 100 ms if file has changed */
	g_timeout_add(100, (GSourceFunc)open_external_editor_cb,
	    (gpointer)a);

	/* Stop loop  child has terminated */
	g_child_watch_add(pid, external_editor_closed, (gpointer)a);

	return (0);
}

int
edit_src_cb(const char *contents, gpointer data)
{
	struct edit_src_cb_args *args;

	args = (struct edit_src_cb_args *)data;

	webkit_web_frame_load_string(args->frame, contents, NULL,
	    webkit_web_data_source_get_encoding(args->data_src),
	    webkit_web_frame_get_uri(args->frame));
	return (0);
}

int
edit_src(struct tab *t, struct karg *args)
{
	WebKitWebFrame		*frame;
	WebKitWebDataSource	*ds;
	GString			*contents;
	struct edit_src_cb_args	*ext_args;

	if (external_editor == NULL){
		show_oops(t,"Setting external_editor not set");
		return (1);
	}

	frame = webkit_web_view_get_focused_frame(t->wv);
	ds = webkit_web_frame_get_data_source(frame);
	if (webkit_web_data_source_is_loading(ds)) {
		show_oops(t,"Webpage is still loading.");
		return (1);
	}

	contents = webkit_web_data_source_get_data(ds);
	if (!contents)
		show_oops(t,"No contents - opening empty file");

	ext_args = g_malloc(sizeof(struct edit_src_cb_args));
	ext_args->frame = frame;
	ext_args->data_src = ds;

	/* Check every 100 ms if file has changed */
	open_external_editor(t, contents ? contents->str : "", &edit_src_cb,
	    ext_args);
	return (0);
}

struct edit_element_cb_args {
	WebKitDOMElement	*active;
	struct tab		*tab;
};

int
edit_element_cb(const char *contents, gpointer data)
{
	struct				edit_element_cb_args *args;
	WebKitDOMHTMLTextAreaElement	*ta;
	WebKitDOMHTMLInputElement	*el;

	args = (struct edit_element_cb_args*)data;

	if (!args || !args->active)
		return (0);

	el = (WebKitDOMHTMLInputElement*)args->active;
	ta = (WebKitDOMHTMLTextAreaElement*)args->active;

	if (WEBKIT_DOM_IS_HTML_INPUT_ELEMENT(el))
		webkit_dom_html_input_element_set_value(el, contents);
	else if (WEBKIT_DOM_IS_HTML_TEXT_AREA_ELEMENT(ta))
		webkit_dom_html_text_area_element_set_value(ta, contents);

	return (0);
}

int
edit_element(struct tab *t, struct karg *a)
{
	WebKitDOMHTMLDocument		*doc;
	WebKitDOMElement		*active_element;
	WebKitDOMHTMLTextAreaElement	*ta;
	WebKitDOMHTMLInputElement	*el;
	char				*contents;
	struct edit_element_cb_args	*args;

	if (external_editor == NULL){
		show_oops(t,"Setting external_editor not set");
		return (0);
	}

	doc = (WebKitDOMHTMLDocument*)webkit_web_view_get_dom_document(t->wv);
	active_element = webkit_dom_html_document_get_active_element(doc);
	el = (WebKitDOMHTMLInputElement*)active_element;
	ta = (WebKitDOMHTMLTextAreaElement*)active_element;

	if (doc == NULL || active_element == NULL ||
	    (WEBKIT_DOM_IS_HTML_INPUT_ELEMENT(el) == 0 &&
	    WEBKIT_DOM_IS_HTML_TEXT_AREA_ELEMENT(ta) == 0)) {
		show_oops(t, "No active text element!");
		return (1);
	}

	contents = "";
	if (WEBKIT_DOM_IS_HTML_INPUT_ELEMENT(el))
		contents = webkit_dom_html_input_element_get_value(el);
	else if (WEBKIT_DOM_IS_HTML_TEXT_AREA_ELEMENT(ta))
		contents = webkit_dom_html_text_area_element_get_value(ta);

	if ((args = g_malloc(sizeof(struct edit_element_cb_args))) == NULL)
		return (1);

	args->tab = t;
	args->active = active_element;

	open_external_editor(t, contents, &edit_element_cb,  args);

	return (0);
}

#else /* Just to make things compile. */

int
edit_element(struct tab *t, struct karg *a)
{
	show_oops(t, "external editor feature requires webkit >= 1.5.0");
	return (1);
}

int
edit_src(struct tab *t, struct karg *args)
{
	show_oops(t, "external editor feature requires webkit >= 1.5.0");
	return (1);
}

#endif