// vim:syntax=javascript

//! Helper function to toggle frame
function toggle_frame(map)
{
    if (map.getFrameType() == "NoFrame" ) {
        map.setFrameType ("RoundedRectangle");
    } else
        map.setFrameType ("NoFrame");
}
function colorSubtreeWithQuickColor(n)
{
    map = vym.currentMap();
    vym.selectQuickColor(n);
    c = vym.currentColor();
    map.colorSubtree (c);
}

//! Macro F1: Color subtree red
function macro_f1()
{
    colorSubtreeWithQuickColor(0);
}

//! Macro F2: Color subtree orange
function macro_f2()
{
    colorSubtreeWithQuickColor(1);

    // Or if you prefer to edit the heading of a branch using "F2"-key,
    // you can use below instead of above:
    // vym.editHeading();
}

//! Macro F3: Color subtree green
function macro_f3()
{
    colorSubtreeWithQuickColor(2);
}


//! Macro F4: Color subtree purple
function macro_f4()
{
    colorSubtreeWithQuickColor(3);
}

//! Macro F5: Color subtree blue
function macro_f5()
{
    colorSubtreeWithQuickColor(4);
}

//! Macro F6: Color subtree blue
function macro_f6()
{
    colorSubtreeWithQuickColor(5);
}

//! Macro F7: Color subtree black
function macro_f7()
{
    colorSubtreeWithQuickColor(6);
}

//! Macro F8: Color subtree dark gray
function macro_f8()
{
    colorSubtreeWithQuickColor(7);
}

//! Macro F9: Color subtree light gray
function macro_f9()
{
    colorSubtreeWithQuickColor(8);
}

//! Macro F10: Color subtree light white
function macro_f10()
{
    colorSubtreeWithQuickColor(9);
}

//! Macro F11: Repeat last command
function macro_f11()
{
    map = vym.currentMap();
    map.repeatLastCommand();
}

//! Macro F12: toggle high prio task
function macro_f12()
{
	// Assuming 3 states, which are cycled:
	// 0 - nothing set
	// 1 - high prio task with arrows
	// 2 - done task without arrows, but green hook
    map = vym.currentMap();
    if (map.hasTask() ) {
        // Switch to state 2 
        map.toggleTask();
        map.unsetFlagByName("2arrow-up");
        map.unsetFlagByName("stopsign");
        map.setFlagByName("hook-green");
        map.colorBranch("#0000ff");
    } else {
        if (map.hasActiveFlag("hook-green")) {
            // Switch to state 0
            map.unsetFlagByName("hook-green");
        } else {
            // Switch to state 1
            map.setFlagByName("2arrow-up");
            map.toggleTask();
        }
    }
}


//! Macro Shift + F1: Frame background light red
function macro_shift_f1()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light red";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#ffb3b4");
    statusMessage(status);
}

//! Macro Shift + F2: Frame background light green
function macro_shift_f2()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light green";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#bdffd6");
    statusMessage(status);
}

//! Macro Shift + F3: Frame background light yellow
function macro_shift_f3()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light yellow";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#efefb3");
    statusMessage(status);
}

//! Macro Shift + F4: Frame background light blue
function macro_shift_f4()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light blue";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#e2e6ff");
    statusMessage(status);
}

//! Macro Shift + F5: Frame background light grey
function macro_shift_f5()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background light grey";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#d6d6d6");
    statusMessage(status);
}

//! Macro Shift + F6: Frame background purple
function macro_shift_f6()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background purple";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#ffaaff");
    statusMessage(status);
}

//! Macro Shift + F7: Frame background white
function macro_shift_f7()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background white";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#ffffff");
    statusMessage(status);
}

//! Macro Shift + F8: Frame background black
function macro_shift_f8()
{
    map = vym.currentMap();
    status = "Background off";
    if (map.getFrameType() == "NoFrame") {
        status = "Background black";
    }
    toggle_frame ( map );
    map.setFrameBrushColor("#000000");
    statusMessage(status);
}

//! Macro Shift + F9: Toggle frame to include children
function macro_shift_f9()
{
    map = vym.currentMap();
    map.toggleFrameIncludeChildren();
}

//! Macro Shift + F10: 
// Useful for timestamps created on last entry
function macro_shift_f10()
{   
}

//! Macro Shift + F11: Replace "@..." by mutt aliases
function macro_shift_f11()
{
	map     = vym.currentMap();
	lines   = vym.loadFile("/home/uwe/.mutt/eb-aliases").split('\n');


	if (map.getHeadingPlainText() == "parts") {
		map.setHeadingPlainText("Participants");
	}

	map.initIterator("foobar");

	while (map.nextIterator("foobar") ) {
		heading = map.getHeadingPlainText();

		// Search for "@alias" at beginning of heading
		if (heading.search(/^@\w/) == 0) {
			alias_map = heading.replace(/@/,"");

			for(var i = 0;i < lines.length;i++){
				if (lines[i].search(/^alias/) == 0) {
					if (lines[i].search(alias_map) == 6) {
						name = lines[i].replace(/^.+?\"/, "");
						name = name.replace(/\".+/, "");

						email = lines[i].replace(/^.+?</, "");
						email = email.replace(/>.*/, "");

						map.setHeadingPlainText(name);
					}
				}
			}
		} else {
			print ("No alias found");
        }
	}
}

//! Macro Shift + F12:
function macro_shift_f12()
{
    statusMessage("Macro F12 + Shift triggered");
}


// New /////////////////////////////////////
//! Macro Ctrl + F1:
function macro_ctrl_f1()
{
    statusMessage("Macro F1 + Ctrl triggered");
}

//! Macro Ctrl + F2:
function macro_ctrl_f2()
{
    statusMessage("Macro F2 + Ctrl triggered");
}

//! Macro Ctrl + F3:
function macro_ctrl_f3()
{
    statusMessage("Macro F3 + Ctrl triggered");
}

//! Macro Ctrl + F4:
function macro_ctrl_f4()
{
    statusMessage("Macro F4 + Ctrl triggered");
}

//! Macro Ctrl + F5:
function macro_ctrl_f5()
{
    statusMessage("Macro F5 + Ctrl triggered");
}

//! Macro Ctrl + F6:
function macro_ctrl_f6()
{
    statusMessage("Macro F6 + Ctrl triggered");
}

//! Macro Ctrl + F7:
function macro_ctrl_f7()
{
    statusMessage("Macro F7 + Ctrl triggered");
}

//! Macro Ctrl + F8:
function macro_ctrl_f8()
{
    statusMessage("Macro F8 + Ctrl triggered");
}

//! Macro Ctrl + F9:
function macro_ctrl_f9()
{
    statusMessage("Macro F9 + Ctrl triggered");
}

//! Macro Ctrl + F10: 
function macro_ctrl_f10()
{
    statusMessage("Macro F10 + Ctrl triggered");
}

//! Macro Ctrl + F11:
function macro_ctrl_f11()
{
    statusMessage("Macro F11 + Ctrl triggered");
}

//! Macro Ctrl + F12:
function macro_ctrl_f12()
{
    statusMessage("Macro F12 + Ctrl triggered");
}

//! Macro Ctrl + Shift + F1:
function macro_ctrl_shift_f1()
{
    statusMessage("Macro F1 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F2:
function macro_ctrl_shift_f2()
{
    statusMessage("Macro F2 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F3:
function macro_ctrl_shift_f3()
{
    statusMessage("Macro F3 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F4:
function macro_ctrl_shift_f4()
{
    statusMessage("Macro F4 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F5:
function macro_ctrl_shift_f5()
{
    statusMessage("Macro F5 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F6:
function macro_ctrl_shift_f6()
{
    statusMessage("Macro F6 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F7:
function macro_ctrl_shift_f7()
{
    statusMessage("Macro F7 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F8:
function macro_ctrl_shift_f8()
{
    statusMessage("Macro F8 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F9:
function macro_ctrl_shift_f9()
{
    statusMessage("Macro F9 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F10: 
function macro_ctrl_shift_f10()
{
    statusMessage("Macro F10 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F11:
function macro_ctrl_shift_f11()
{
    statusMessage("Macro F11 + Ctrl + Shift triggered");
}

//! Macro Ctrl + Shift + F12:
function macro_ctrl_shift_f12()
{
    statusMessage("Macro F12 + Ctrl + Shift triggered");
}