Skip to content

Instantly share code, notes, and snippets.

@MakotoE
Last active April 7, 2023 09:39
Show Gist options
  • Save MakotoE/d74d75ade47f57c1c9396488bada8347 to your computer and use it in GitHub Desktop.
Save MakotoE/d74d75ade47f57c1c9396488bada8347 to your computer and use it in GitHub Desktop.
// Words to exclude
var excludeWords = ['a', 'an', 'and', 'for', 'or', 'as', 'at', 'of'];
/*
Choose between:
eeCaseLowerCase
eeCaseUpperCase
eeCaseCapitalize
*/
var convertTo = eeCaseCapitalize;
function coords() {
return {
x: document.selection.GetActivePointX(eePosLogical),
y: document.selection.GetActivePointY(eePosLogical),
};
}
function includes(arr, find) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === find) {
return true;
}
}
return false;
}
var matchNonwhitespace = new RegExp('\\S');
var left = {
x: document.selection.GetTopPointX(eePosLogical),
y: document.selection.GetTopPointY(eePosLogical),
};
var selected = {
x: document.selection.GetBottomPointX(eePosLogical),
y: document.selection.GetBottomPointY(eePosLogical),
};
if (left.x > 1) {
document.selection.SetActivePoint(eePosLogical, left.x, left.y);
document.selection.SetActivePoint(eePosLogical, left.x - 1, left.y, true);
if (document.selection.Text !== ' ') {
document.selection.WordRight();
left = coords();
}
}
while (left.y < selected.y || left.x < selected.x) {
document.selection.SetActivePoint(eePosLogical, left.x, left.y);
document.selection.WordRight(true);
var right = coords();
if (includes(['’', '\''], document.selection.Text)) { // Skip apostrophe
document.selection.WordRight(true);
right = coords();
} else if (matchNonwhitespace.test(document.selection.Text)) {
if (document.selection.Text.slice(-1) === ' ') {
right = coords();
document.selection.SetActivePoint(eePosLogical, left.x, left.y);
document.selection.SetActivePoint(eePosLogical, right.x - 1, right.y, true);
}
if (!includes(excludeWords, document.selection.Text)) {
document.selection.ChangeCase( convertTo );
}
}
left = right;
}
document.selection.CharRight();
@Thomanji
Copy link

Thomanji commented Apr 7, 2023

That worked very well. That you very much for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment