Skip to content

Instantly share code, notes, and snippets.

@GitMurf
Created January 8, 2022 04:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitMurf/c52dfef13162d88375975e4eefedf3a7 to your computer and use it in GitHub Desktop.
Save GitMurf/c52dfef13162d88375975e4eefedf3a7 to your computer and use it in GitHub Desktop.
Send the current active line of text or if text is selected, send the selection... to the pane next to the active one
<%*
//OPTIONS: TOP / BOTTOM
const whereToWrite = "TOP";
//OPTIONS: COPY / MOVE
const copyOrMove = "COPY";
//Find leaf next door
const thisLeaf = app.workspace.activeLeaf;
const thisFile = thisLeaf.view.file;
let leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "right");
if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "left");}
if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "bottom");}
if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "top");}
if(leafToUse) {
//Find selected text or text on current line
const editor = app.workspace.activeLeaf.view.editor;
if(editor.somethingSelected() === false) {
const curLineNum = editor.getCursor().line;
editor.setSelection({ line: curLineNum, ch: 0 }, { line: curLineNum, ch: editor.getLine(curLineNum).length });
}
let curLineStr = editor.getSelection();
if(curLineStr) {
curLineStr = curLineStr.trim();
const nextEditor = leafToUse.view.editor;
const foundFile = leafToUse.view.file;
let lineNo = 0;
switch(whereToWrite.toUpperCase()) {
case "TOP":
lineNo = 0;
const mdCache = app.metadataCache.getFileCache(foundFile);
if(mdCache) {
if(mdCache.frontmatter) {
lineNo = mdCache.frontmatter.position.end.line;
}
}
break;
case "BOTTOM":
lineNo = nextEditor.lastLine();
break;
}
const curText = nextEditor.getLine(lineNo);
nextEditor.setLine(lineNo,`${curText}\n${curLineStr}`);
nextEditor.setCursor(lineNo + 1);
switch(copyOrMove.toUpperCase()) {
case "COPY":
tR = curLineStr;
break;
case "MOVE":
tR = "";
break;
}
}
}
%>
@GitMurf
Copy link
Author

GitMurf commented Jan 8, 2022

If you only have 2 panes open then it will automatically send to the next pane (whichever side it is on). But if you have 3 or more panes open, it will first look right, then left, then below, and finally top. As soon as it finds a valid pane it will use that. Of course you could copy the script and make it into multiple where it switches the ordering. For example a "Send to left" script you could put the line to look to the left first in the code.

At the top you will notice 2 variables to set: whereToWrite and copyOrMove. They should be pretty self explanatory.

image

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