EmEditor's indent behavior on blank lines - emeditor

Currently EmEditor inserts indent characters (spaces or tabs) as below:
[indent]for (i = 0; i < 10; ++i)
[indent][indent]printf("%d\n", i);[cursor]
If I press Enter twice, the text will become:
[indent]for (i = 0; i < 10; ++i)
[indent][indent]printf("%d\n", i);
[indent]
[indent][cursor]
As you see, the 3rd line is blank, but has an indent character which most people want to eliminate. Is there an option to let EmEditor not insert that indent character?

You can set the Delete Spaces at End of Lines option in the Save Details dialog box (configuration properties -> File -> Saving... button).

Related

Ace editor - how do I remove/reset indent?

I want to change the behavior of the editor such that when the user presses enter on an empty list bullet, their cursor position is reset to the start of the line (rather than leaving them at the indented amount).
I've tried:
aceEdit.moveCursorTo(rowToUpdate, 0)
aceEdit.getSession().indentRows(rowToUpdate, rowToUpdate, "")
aceEdit.getSession().replace(range(rowToUpdate, 0, rowToUpdate, 0), "")
However, all three of these leave the cursor at the previous indent level. How do I reset the indent level for the line?
Update: adding example.
* list
* list
* list
* <- user presses enter here
_
Cursor is where I placed the underscore above, and can't be reset programmatically to the start of the line using what I listed above. (User can backspace the indents to get back to the start.)
You can use editor.setOption("enableAutoIndent", false) to disable automatic indentation.
If you want a way to keep autoIndent in all cases except the list, try creating an issue on ace's github page
If you want to remove the indentation on particular line you can do
var range = ace.Range
var cursor = editor.getCursorPosition()
var line = editor.session.getLine(cursor.row)
var match = /^\s*\*\s*/
if (match) {
editor.session.replace(new Range(cursor.row, 0, cursor.row, cursor.column), "")
}
If you are making a custom mode, you can give it a custom $outdent and related methods - see MatchingBraceOutdent, for example. I think the full set of indentation-related Mode methods is:
getNextLineIndent
checkOutdent
autoOutdent

Conditionally apply Paragraph Style to text in a Table

I have this code that works:
var myTable = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for(i=0; i<myTable.length; i++) {
myTable[i].cells[0].insertionPoints[0].appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item("Wycena").paragraphStyles.item("Nazwa A. Numeracja2");
}
This code applies one Paragraph Style to all rows in a Table, however what I need is as follows:
If I paste text that does begin with a bullet symbol (•) into a Table then apply "Paragraph Style 1"
If I paste text that does not begin with a bullet symbol (•) into a Table then apply "Paragraph Style 2".
What I think I need is an if statement somewhere, but I cant work it out.
You can apply Paragraph Style 2 to all the text and then run the script to format only the text with the bullet symbol:
var myDocument = app.documents.item(0);
var allCells = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
for ( var i = 0; i < allCells.length; i++)
{
if ( allCells[i].contents.indexOf('•') > -1)
{
allCells[i].appliedCellStyle = 'Paragraph Style 1';
allCells[i].clearCellStyleOverrides();
}
}
You can change the "•" to any other text you want a specific format and that cell will be formatted accordingly.

inDesign JSX scripted add of heading and content into textFrame

I'm attempting to use inDesign JSX scripts to insert the following data into a document:
data = [{heading:"Heading 1", content: ["Some content"]},
{heading:"Heading 2", content: ["Some other content with", "Multiple paragraphs"]}]
The data has to be placed into a single TextFrame, but have different styling on the heading and content.
The only way I can see to add the text is in one go via the textFrame.contents variable:
allContent = "";
headingParagraphs = []; // keep track of which paragraphs are headings
paragraph = 0;
for (var i = 0; i < data.length; i++) {
allContent += data.heading + "\r"; // Use a newline to split the paragraph
headingParagraphs.push(paragraph);
paragraph++;
for (var j = 0; j < data.content.length; j++) {
allContent += data.content[j] + "\r"; // Use a newline to split the paragraph
paragraph++;
}
}
textFrame.contents = allContent; // all data is in, but all text is styled the same
Then once the data is in, I iterate the paragraphs and add some style to the headings:
for (var i = 0; i < textFrame.paragraphs.count(); i++) {
if (headingParagraphs.indexOf(i) != -1) { // this is a heading paragraph
textFrame.paragraphs[i].pointSize = 20;
}
}
This works fine for small data sets that fit on one page, but once the contents gets bigger than the frame, paragraphs only returns visible paragraphs. And if I follow on to a new textFrame, paragraphs get split and the headingParagraphs[] array no longer lines up.
Ideally I'd like to append to the contents and set styles before I append the next content - but the API docs aren't very clear on how you might do that (if at all)
// Pseudo code:
for all sections:
append the heading to the frame, split to next page if needed
style all the *new* paragraphs as headings
for all section contents
append the content to the frame, split to next page if needed
style any *new* paragraphs as normal content
Is there a way to achieve this using either an append function or some other way to assign headings to the right place after content has been added? Perhaps special characters in the content to define style?
Your longer text gets messed up because currently you are working inside a single text frame. As soon as the text runs out of this one frame, you can't refer to them as this frame's "owned" paragraphs anymore. Use parentStory instead, as it points to the whole story, inside one text frame or spanning more than one. It also keeps on working if the text gets overset.
So if you have a starting frame called textFrame, set a new variable story to textFrame.parentStory and use that to add text.
As for adding text to this frame(/story): indeed, there is no fast way to add formatted text. Setting contents only works for long swathes with the same formatting. One way I've used is to write INX formatted text to a temporary file and importing that. It's slow for short fragments, but larger stories (up to several hundreds of pages) can be created very efficiently in Javascript itself, and then importing it into ID is .. well, it aint fast but faster than trying to do it "manually".
The other way is to add contents one paragraph at a time. The trick is to set formatting and add your text to story.insertionPoints[-1]. This, in a particularly handy notation, refers to the very last text insertion point of the story. You can think of an insertion point as "the text cursor"; you can 'apply' formatting to it, and any text added will then have this formatting as well.
Your code snippet reworked to add one data item at a time:
for (var i = 0; i < data.length; i++)
{
story.insertionPoints[-1].pointSize = 20;
story.insertionPoints[-1].contents = data[i].heading + "\r"; // Use a newline to split the paragraph
story.insertionPoints[-1].pointSize = 10;
for (var j = 0; j < data[i].content.length; j++)
{
story.insertionPoints[-1].contents = data[i].content[j] + "\r"; // Use a newline to split the paragraph
}
}
One thing to note is that you cannot temporarily override the pointSize here. If you set it to your larger size, you must also set it back to the original size again (the '10' in my snippet).
Can I convince you to look in to using paragraph styles? With paragraph styles, you'd have something like
hdrStyle = app.activeDocument.paragraphStyles.item("Header");
textStyle = app.activeDocument.paragraphStyles.item("Text");
for (var i = 0; i < data.length; i++)
{
story.insertionPoints[-1].contents = data[i].heading + "\r"; // Use a newline to split the paragraph
story.insertionPoints[-2].appliedParagraphStyle = hdrStyle;
for (var j = 0; j < data[i].content.length; j++)
{
story.insertionPoints[-1].contents = data[i].content[j] + "\r"; // Use a newline to split the paragraph
story.insertionPoints[-2].appliedParagraphStyle = textStyle;
}
}
Note that it's worth here to invert inserting contents and applying formatting. This is so any previous 'temporary' formatting gets cleared; applying a paragraph style this way overrides any and all local overrides. As you have to apply the style to the previous paragraph (the one before the hard return you just inserted), you would use insertionPoints[-2] here.
The advantages of using styles over local formatting are countless. You can apply all formatting you want with a single command, safely remove all local overridden formatting, and change any part of the formatting globally if you are not satisfied with it, rather than having to re-run your script with slightly different settings.

Can we paste lines into visual studio "without carriage return"?

Lets say we have these lines in the editor
int a = 10;
print(a,b);
string b = "hello";
So what i would like to do is.. shift the 3rd line to the 2nd position similar to
int a = 10;
string b = "hello";
print(a,b);
I use ctrl+x or Shift + del to cut the line to the clipboard.
But on pasting the line at 2nd position i get
int a = 10;
string b = "hello";
print(a,b);
The extra blank line at the 3rd line. is there any way to paste without that 3rd blank line. or for the matter an easier way to move and cut and paste lines. ?
Shift+Alt+T swaps the current line with the line below it. So you'd put the caret (or cursor) on the second line, then use that keyboard shortcut to swap it with the third line.
Thank you so much for the answer!.
i just stumbled on to a easier method and more flexible.
With a Addon - Pro tools
http://visualstudiogallery.msdn.microsoft.com/en-us/d0d33361-18e2-46c0-8ff2-4adea1e34fef
we can now use Alt + Arrow keys to move the lines up or down.

"New Scope" Macro for Visual Studio

I'm trying to create a new macro that takes the currently selected text and puts curly braces around it (after making a newline), while, of course, indenting as needed.
So, for example, if the user selects the code x = 0; and runs the macro in the following code:
if (x != 0) x = 0;
It should turn into:
if (x != 0)
{
x = 0;
}
(Snippets don't help here, because this also needs to work for non-supported source code.)
Could someone help me figure out how to do the indentation and the newlines correctly? This is what I have:
Public Sub NewScope()
Dim textDoc As TextDocument = _
CType(DTE.ActiveDocument.Object("TextDocument"), TextDocument)
textDoc.???
End Sub
but how do I figure out the current indentation and make a newline?
Sub BracketAndIndent()
Dim selection = CType(DTE.ActiveDocument.Selection, TextSelection)
' here's the text we want to insert
Dim text As String = selection.Text
' bracket the selection;
selection.Delete()
' remember where we start
Dim start As Integer = selection.ActivePoint.AbsoluteCharOffset
selection.NewLine()
selection.Text = "{"
selection.NewLine()
selection.Insert(text)
selection.NewLine()
selection.Text = "}"
' this is the position after the bracket
Dim endPos As Integer = selection.ActivePoint.AbsoluteCharOffset
' select the whole thing, including the brackets
selection.CharLeft(True, endPos - start)
' reformat the selection according to the language's rules
DTE.ExecuteCommand("Edit.FormatSelection")
End Sub
textDoc.Selection.Text = "\n{\n\t" + textDoc.Selection.Text + "\n}\n"
Of course the amount of \t before the { and } and Selection depend on the current indention.
Since there is a difference between selected Text and Document data, it's hard to find out where the cursor is within the document's data (at least in Outlook it is).
The only way I figured out how to do this in Outlook is to actually move the selection backwards until I got text that I needed, but that resulted in undesirable effects.
Try taking the selection start, and using that position in the document text, looking at that line and getting the amount of tabs.
I would think there wouldn't be formatting characters in VStudio.

Resources