Skip to content

Instantly share code, notes, and snippets.

@kalbasit
Last active March 9, 2017 08:34
Show Gist options
  • Save kalbasit/359ed6744bda93fd4a70 to your computer and use it in GitHub Desktop.
Save kalbasit/359ed6744bda93fd4a70 to your computer and use it in GitHub Desktop.
kalbasit's cvimrc
"""""""""""""""""""
" Settings
let mapleader = ","
let barposition = "top"
let hintcharacters = "arstneio"
let completionengines = ["google", "google-image", "amazon", "imdb", "youtube"]
" blacklists prefixed by '@' act as a whitelist
let blacklists = ["https://inbox.google.com/*", "https://mail.google.com/*", "https://kapollo.us.quickconnect.to/*", "https://apollo.nasreddine.com:56902/*"]
let searchalias g = "google" " Create a shortcut for search engines.
" For example, typing ':tabnew g example'
" would act the same way as ':tabnew google example'
set nosmoothscroll
set autoupdategist
set noautofocus " The opposite of autofocus; this setting stops
" sites from focusing on an input box when they load
"""""""""""""""""""
" Code blocks
getIP() -> {{
httpRequest({url: "http://api.ipify.org/?format=json", json: true},
function(res) { Status.setMessage("IP: " + res.ip); });
}}
copyLastElementInPath() -> {{
var locationParts = window.location.href.split("/");
var lastElement = locationParts[locationParts.length-1].split("#")[0].split("?")[0];
window.Clipboard.copy(lastElement);
Status.setMessage("Copied " + lastElement + " to the clipboard.");
}}
" openAlternate opens the alternate page:
" - When on Github viewing a PR, open the build for this PR (DONE)
" - When on Github viewing a source file, open the test file (DONE)
" - When on Github viewing a test file, open the source file (DONE)
" - When on Github viewing anything else, go the Travis repo (DONE)
" - When on Travis viewing a build for a PR, go to Github PR (DONE)
" - When on Travis viewing a build for branch, go to Github with branch selected (TODO)
" - When on Travis viewing the main build, go to Github repo (DONE)
"
" openAlternate requires the username and the api_token in order to work.
" The username and the api_token must be stored in the chrome storage. To set
" them in the storage, execute the following in the chrome console on cVim's
" background page:
" chrome.storage.sync.set({'github_api_username': '...'});
" chrome.storage.sync.set({'github_api_token': '...'});
openAlternate() -> {{
// start by splitting the path by slashes, ignoring the first slash to
// prevent having the first element an empty string
var pp = window.location.pathname.substring(1).split("/");
// extract the anchor from the last element
if (pp[pp.length - 1].indexOf("#") !== -1) {
var tmp = pp[pp.length - 1].split("#");
pp[pp.length - 1] = tmp[0];
var anchor = tmp[1];
}
// extract any query params from the last element
if (pp[pp.length - 1].indexOf("?") !== -1) {
var tmp = pp[pp.length - 1].split("?");
pp[pp.length - 1] = tmp[0];
var query = tmp[1];
}
// isGithubPrivateRepo() returns true if the current page is a private repo
function isGithubPrivateRepo() {
return document.getElementsByClassName("label-private").length > 0;
}
// isGithubPR returns true if we are viewing a PR
function isGithubPR() {
return pp[2] === "pull";
}
// getGithubPullRequestURL() returns a promise that returns the URL of the PR (if
// successful) or rejects it with a string error.
function getGithubPullRequestURL(username, password) {
return new Promise(function(resolve, reject) {
// compute the XHR URL
var api_pulls_url = "https://api.github.com/repos" + repoPath() + "/pulls";
// find the branch name
var commitBranches = window.document.getElementsByClassName("commit-branch");
if (commitBranches.length === 1) {
var commitBranch = commitBranches[0].title;
// make an XHR Request to Github to find the URL
var xhr = new XMLHttpRequest();
xhr.open("GET", api_pulls_url, true);
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 299) {
var pulls = JSON.parse(xhr.responseText);
for (var i = 0; i < pulls.length; i++) {
var pull = pulls[i];
if (pull.head.ref === commitBranch) {
resolve(pull.html_url);
return;
}
}
reject("did not find a pull request for branch " + commitBranch);
} else {
reject("got " + xhr.statusText + " in the HTTP request to " + api_pulls_url);
return;
}
}
}
xhr.send();
} else {
console.log("found " + commitBranches.length + " elements with class commit-branch, was expecting only one");
reject("");
}
});
}
// getTravisURL returns a promise that returns the URL of the Travis
// push build (if successful) or rejects it with a string error.
function getTravisURL(username, password) {
return new Promise(function(resolve, reject) {
var api_pull_url = "https://api.github.com/repos" + repoPath() + "/pulls/" + pp[3];
// make an XHR Request to Github to find the URL
var xhr = new XMLHttpRequest();
xhr.open("GET", api_pull_url, true);
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 299) {
var pr = JSON.parse(xhr.responseText);
if (!pr || !pr._links || !pr._links.statuses || !pr._links.statuses.href) {
reject("statuses href not found in " + xhr.responseText);
return;
}
var innerXhr = new XMLHttpRequest();
innerXhr.open("GET", pr._links.statuses.href, true);
innerXhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
innerXhr.onreadystatechange = function() {
if (innerXhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 299) {
var statuses = JSON.parse(innerXhr.responseText);
for (var i = 0; i < statuses.length; i++) {
var status = statuses[i];
if (status.context === "continuous-integration/travis-ci/push") {
resolve(status.target_url);
return;
}
}
reject("did not find the status continuous-integration/travis-ci/push in " + JSON.stringify(statuses));
} else {
reject("got " + xhr.statusText + " in the HTTP request to " + pr._links.statuses.href);
return;
}
}
}
innerXhr.send();
} else {
reject("got " + xhr.statusText + " in the HTTP request to " + api_pull_url);
return;
}
}
}
xhr.send();
})
}
// repoPath returns the repo path taken from the URL, repoPath returns an
// empty string if no repo was found in the URL.
function repoPath() {
if (pp.length < 2) {
return "";
}
return "/" + pp[0] + "/" + pp[1];
}
function openTravis() {
var targetURL = "https://travis-ci." + (isGithubPrivateRepo() ? "com" : "org" ) + rp;
chrome.storage.sync.get(["github_api_username", "github_api_token"], function(data) {
if (typeof data.github_api_username === "string" && data.github_api_username !== "" && typeof data.github_api_token === "string" && data.github_api_token !== "") {
if (isGithubPR() === true) {
getTravisURL(data.github_api_username, data.github_api_token)
.then(function(url) {
window.location.href = url;
})
.catch(function(error) {
if (error !== "") {
Status.setMessage(error);
}
setTimeout(function() {
window.location.href = targetURL;
}, 1000);
});
} else {
window.location.href = targetURL;
}
} else {
Status.setMessage("API username and password were not found, follow the documentation in the config");
setTimeout(function() {
window.location.href = targetURL;
}, 1000);
}
});
}
function openGithub() {
chrome.storage.sync.get(["github_api_username", "github_api_token"], function(data) {
if (typeof data.github_api_username === "string" && data.github_api_username !== "" && typeof data.github_api_token === "string" && data.github_api_token !== "") {
getGithubPullRequestURL(data.github_api_username, data.github_api_token)
.then(function(url) {
window.location.href = url;
})
.catch(function(error) {
if (error !== "") {
Status.setMessage(error);
}
setTimeout(function() {
window.location.href = "https://github.com" + rp;
}, 1000);
});
} else {
Status.setMessage("API username and password were not found, follow the documentation in the config");
setTimeout(function() {
window.location.href = "https://github.com" + rp;
}, 1000);
}
});
}
// get the repository path, works on both travis and Github
var rp = repoPath();
if (rp === "") {
Status.setMessage("You must be viewing a repository");
return;
}
function openAlternateFile() {
var file = pp[pp.length - 1];
if (file.match(/_test\.go$/) !== null) {
window.location.href = window.location.href.replace('_test.go', '.go');
} else if (file.match(/\.go$/) !== null) {
window.location.href = window.location.href.replace('.go', '_test.go');
}
}
// are we on a source file?
if (window.location.hostname == "github.com") {
// are we viewing a source file?
if (pp[2] === "blob") {
openAlternateFile();
} else {
openTravis();
}
} else if (window.location.hostname == "travis-ci.com" || window.location.hostname == "travis-ci.org") {
openGithub();
}
}}
"""""""""""""""""""
" Mappings
" Colemak bindings
map A toggleVisualLineMode
map E previousTab
map I nextTab
map K previousSearchResult
map N goBack
map O goForward
map P createHintWindow
map V openPasteTab
map W :tabnew @%
map Y :open @%
map a toggleVisualMode
map cC yankFrameUrl
map cc yankDocumentUrl
map ch yankHighlight
map d closeTab
map e scrollDown
map gc yankUrl
map gs previousTab
map i scrollUp
map k nextSearchResult
map n scrollLeft
map o scrollRight
map p :open github.com/publica-project/
map s insertMode
map t :tabnew google<Space>
map u lastClosedTab
map v openPaste
map w :tabnew<Space>
map y :open<Space>
map <Leader>d :restore<Space>
map <Leader>r reloadTabUncached
map gS :viewsource&<CR>
" You can use <Space>, which is interpreted as a
" literal " " character, to enter buffer completion mode
map ,b :buffer<Space>
" Toggle the current HUD display value
map <C-h> :set hud!<CR>
" Switch between alphabetical hint characters and numeric hints
map <C-i> :set numerichints!<CR>
map <C-u> rootFrame
map <C-d> scrollPageDown
map <C-e> scrollPageUp
" Displays your public IP address in the status bar
map ci :call getIP<CR>
" Copy the last element of the path, useful for copying the story ID in Jira
map cs :call copyLastElementInPath<CR>
map ,. :call openAlternate<CR>
"""""""""""""""""""
" Sites
site "https://inbox.google.com/*" {
call :pintab
}
site "https://mail.google.com/*" {
call :pintab
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment