Cocoa NSTextContainer: How to maintain whitespace after gap? - cocoa

I am creating a gap in text by overriding NSTextContainer's "lineFragmentRectForProposedRect" and returning a remainingRect.
This works overall, but it is undesirable to me that the "remainingRect" portion of the text container hides whitespace at the beginning of the rect. Anybody know a means of stopping this behaviour?
For example:
"Hello World!" as the literal text when separated by a gap beginning anywhere between "Hello" and "World!", then the right side of the gap will have no space before "World!", such that, let's say, if the gap was the width of a character and begins right after "Hello", then the result would look like "Hello World!".

Related

Opposite of backspace?

Is there a ASCII character that does the 'opposite' of backspace, i.e., that moves the cursor one position forward? Id like a character X such that
print("bad\rXbc")
prints bcd (the example is python, but this should be rather a subject depending on the terminal emulator). X cannot be a white space, for the white space overwrites:
print("bad\r bc")
prints cd. In princible, \t does what I want, but the sought character should move by only one character forward. Does it exist?
Edit. To make my question clear: the BS does not delete printed characters, in the sense that they are still visible unless overwritten. Of course this depends on the terminal emulator. For the macOS terminal, this is the case:

GREP for end of text symbol

I need to go through and join hundreds of separate text frames into a unified text thread. The problem is that when I link the frames the style of the first paragraph of the frame I'm linking to ofter changes to the style of the last paragraph in the text frame I'm linking from. If I could put a frame break into the end of every text frame I could avoid this problem. The hidden character for 'end of text' looks like a hashtag. Is there a GREP for this?
Just found on the Adobe forums that \z is the character I want, and it does find all the instances, but when I try to change them for page breaks I crash my programme. Every damn time.
You can't only search for a single location placeholder with InDesign's GREP (it has its wings clipped) – although it should not have made it crash. Change the Keep Settings for every first paragraph in your stories to start in a new frame, it'll have the same result when joining.
You can use GREP for this as well. Search for
\A.
and leave the Change To field blank. In the Change To formatting field, set your new Keep options. \A matches Start of Story, and the single . is so you actually can find a (any) character so it won't crash on you again.
So I did this in the end which achieved what I wanted:
Find: (.)(\z)
Change to: $1~R
This way when I linked the text frames the styles didn't get changed.
Thanks for everyone's help, esp. user1754036 for the link to InDesign Secrets which really solved the puzzle for me.

Tabs "can be displayed completely differently on different types of systems and editors"?

While reading arguments against the use of Tabs, I came across this (source):
...it can be a good idea to avoid tabs alltogether, because the semantics of tabs are not very well-defined in the computer world, and they can be displayed completely differently on different types of systems and editors.
I am relatively new to programming, and have never experienced any issues with tabs in my code, and I've used a number of editors including Notepad++, Programmer's Nodepad, Gedit, Kate, Sublime Text, etc. I may not have done enough coding to get to that point, hence the question:
Can someone please explain, in simple terms, what the quote states? And is the problem with tabs still relevant?
Please note that I am not asking you whether I should use tabs or spaces in my code. I am only after a rational explanation for a specific argument against tabs that I've come across.
I assume you want to see some examples so bellow I've listed some of the most common.
Problem #1: Tab width is not consistent
This is for "displayed completely differently on different types of systems and editors" part.
Even assuming all systems and editors (that your code will be displayed on) agrees on same tab semantics: "move to the right until the current column is a multiple of N", the N is arbitrary.
Historically "standard" for this N is 8 but now most people configure their editors it to be 4 or 2 to "look better".
This is where tab width inconsistency problems come from.
I will use tab widths 2 and 8 in my examples to make differences more visual but same applies to other widths as well.
Indent
Lets say someone is using tab width: 2 in their editor. They see the code like this:
class Foo:
def doSomething(a):
if test(a):
// some nice comment
// about this
bar(a)
Now someone else reads this code in say terminal that uses tab width 8. They see the code like this:
class Foo:
def doSomething(a):
if test(a):
// some nice comment
// about this
bar(a)
That someone may see this as not very pleasant.
Align
So far we have seen inconsistency in indent. But some people like to also align code e.g. assignments, comments.
Again with tab width 2:
class Foo:
def do_something(a):
if test(a):
foo = a.foo // some nice comment
foo_bar = bar(foo) // about this
bar(a)
Again someone else reads this in environment where tab width is 8. Lets say they need to post this snippet to the web and uses <pre> tag. Browsers by default use "standard" tab width 8 and the code looks like this:
class Foo:
def do_something(a):
if test(a):
foo = a.foo // some nice comment
foo_bar = bar(foo) // about this
bar(a)
They can't post it as is. They have to modify the code to replace tabs to spaces.
Line length
Most coding standards define maximum line width.
Lets take max line width 80 for example.
Someone using tab width 2 may see this code completely standard conforming. For them the longest line width is 74 (visible width as opposed to line length in bytes which is 72).
class Foo:
def do_something(a):
// Some very nice comment about code bellow using more then few words.
Someone else using tab width 8 (in a terminal for example) will see the same line as non-conforming because now longest line width is 86:
class Foo:
def do_something(a):
// Some very nice comment about code bellow using more then few words.
Since tab width is inconsistent, line with is now inconsistent too.
Problem #2: Tab is not same thing everywhere
This is for the "semantics of tabs are not very well-defined" part.
So far we assumed that everyone uses tab character as "move to the right until the current column is a multiple of N".
But in some contexts tab character may be used for something a bit different. For example in word processors tab means "move to the the next tab stop", where tab stops are completely arbitrary (and most likely not event the same width).
For example lets say someone is writing a document that uses tab stops:
Now lets say they need to paste some code snippet in it. If code is using tabs, following happens:
They can't leave it as is. They have to modify the code to replace tabs to spaces.
Conclusion
As you can see tabs in different contexts may render code from slightly to completely unreadable.
Spaces have none of the above problems.
I like #Giedrius’ answer (+1), and am going to toss in some vastly over-simplified history just because.
What is a space? It is a blank stretch between letters used to identify words.
How big is a space? It depends on the letters—specifically, on the font. There are thousands of fonts, and they can all be tagged as either proportional or non-proportional. In proportional fonts, character width varies (compare i and M); in non-proportional fonts, character width is always the same (compare i and M). How big is a space? It’s as big as whoever designed the font determined it should be. (Typesetting has been around since Guttenberg, which means the typesetting industry has had a few centuries to figure what works and what doesn’t.)
Skipping a few hundred hears, probably relevant stuff happened that I don’t know about.
Along comes the typewriter (hey kids, ever actually seen one?) These allowed you to type out anything you want to. However, to make it work (totally mechanical, all gears and pulleys, no electricity required, but not cool enough for Steampunk) every character has to be the same width, including the spaces.
Free-form words are all nice and well, but quite frequently people wanted to type up numbers in nicely formatted columns—such as for invoices, where you list items followed by their costs lined up by decimal point with a total at the bottom. (Can you imagine doing that by hand? Congratulations, you just imagined my summer job waaay back when. << Insert Liquid Paper reference here.>>) To make this feasible, someone came up with the TAB key. If you do X on the typewriter, it would remember that “hey, there’s a tab stop here”, and if you hit tab and had not yet reached that column on the row you were typing the print head would jump to that point. How big was it? You guessed it, it depended on what the user needed. (What "X" was totally depended on the typewriter manufacturer.)
Thus and so: no real-world correlation between tabs and spaces, unless there's something obscure from the linotype industry. On typewriters a tab stop would line up with a “regular” character position, whereas on computers there’s no such limitation.

Ruby, Files, Tab Characters

I am parsing a csv file and the file has "\t" characters after every column. Why is it that when I print out the individual lines in terminal or open the file in my text editor that the tab spacing between each of the columns is different?
When you use tab, you're essentially moving to the next tab location, not moving over a specific distance. To see the difference, try using 4 spaces instead of tab. Or, alternatively, run the following code, and I think it may become clear to you.
puts "Hel\tlo world!"
puts "H\tello world!"
puts "Hell\to world!"
Hope that helps.
Do you mean something like
1 1
12345678 1
as a result of
puts "1\t1"
puts "12345678\t1"
A tab jumps to the next position in 8-space steps (8 spaces is a common distance, but it depends on settings of your editor. For ruby often is 2-space distance is used).
If the previous text is longer then 8 characters, then you jump to the next position and you have the impression of different tab spacing.

Deleting lines of code in a text editor

Edit: This question had been tagged "Tolstoy" in appreciation of the quality and length of my writing:) Just reading the first and the last paragraph should be enough:) If you tend to select and move code with the mouse, the stuff in middle could be interesting to you.
This question is about how you use text editors in general. I’m looking for the best way to delete a plurality of lines of code (no intent to patent it:) This extends to transposing lines, i.e. deleting and adding them somewhere else. Most importantly, I don’t want to be creating any blank lines that I have to delete separately. Sort of like Visual Studio's SHIFT+DELETE feature, but working for multiple lines at once.
Say you want to delete line 3 from following code (tabs and newlines visualized as well). The naïve way would be to select the text between angle brackets:
if (true) {\n
\t int i = 1;\n
\t <i *= 2;>\n
\t i += 3;\n
}\n
Then hit backspace. This creates a blank line. Hit backspace twice more to delete \t and \n.
You end up with:
if (true) {\n
\t int i = 1;\n
\t i += 3;\n
}\n
When you try to select a whole line, Visual Studio doesn't let you select the trailing newline character. For example, placing the cursor on a line and hitting SHIFT+END will not select the newline at the end. Neither will you select the newline if you use your mouse, i.e. clicking in the middle of a line and dragging the cursor all the way to the right. You only select the trailing newline characters if you make a selection that spans at least two lines. Most editors I use do it this way; Microsoft WordPad and Word are counter-examples (and I frequently get newlines wrong when deleting text there; at least Word has a way to display end-of-line and end-of-paragraph characters explicitly).
When using Visual Studio and other editors in general, here’s the solution that currently works best for me:
Using the mouse, I select the characters that I put between angle brackets:
if (true) {\n
\t int i = 1;<\n
\t i *= 2;>\n
\t i += 3;\n
}\n
Hitting backspace now, you delete the line in one go without having to delete any other characters. This works for several contiguous lines at once. Additionally, it can be used for transposing lines. You could drag the selection between the angle brackets to the point marked with a caret:
if (true) {\n
\t int i = 1;<\n
\t i *= 2;>\n
\t i += 3;^\n
}\n
This leaves you with:
if (true) {\n
\t int i = 1;\n
\t i += 3;<\n
\t i *= 2;>\n
}\n
where lines 3 and 4 have switched place.
There are variations on this theme. When you want to delete line 3, you could also select the following characters:
if (true) {\n
\t int i = 1;\n
<\t i *= 2;\n
>\t i += 3;\n
}\n
In fact, this is what Visual Studio does if you tell it to select a complete line. You do this by clicking in the margin between your code and the column where the red circles go which indicate breakpoints. The mouse pointer is mirrored in that area to distinguish it a little better, but I think it's too narrow and physically too far removed from the code I want to select.
Maybe this method is useful to other people as well, even if it only serves to make them aware of how newlines are handled when selecting/deleting text:) It works nicely for most non-specialized text editors. However, given the vast amount of features and plugins for Visual Studio (which I use most), I'm sure there is better way to use it to delete and move lines of code. Getting the indentation right automatically when moving code between different blocks would be nice (i.e. without hitting "Format Document/Selection"). I'm looking forward to suggestions; no rants on micro-optimization, please:)
Summary of Answers
With respect to Visual Studio: Navigating well with the cursor keys.
The solution that would best suit my style of going over and editing code is the Eclipse way:
You can select several consecutive lines of code, where the first and the last selected line may be selected only partially. Pressing ALT+{up,down} moves the complete lines (not just the selection) up and down, fixing indentation as you go. Hitting CTRL+D deletes the lines completely (not just the selection) without leaving any unwanted blank lines. I would love to see this in Visual Studio!
In Emacs:
kill-line C-k
transpose-lines C-x C-t
C-a C-k C-k -- kill whole line including newline (or kill-whole-line by C-S-backspace).
C-u <number> C-k -- kill <number> of lines (including newlines).
C-y -- yank back the most recently killed text (aka paste)
In VIM:
Delete the whole line including the newline: dd
Transpose lines: dd p
You can always prefix any command with a number to repeat it, so to delete 10 lines do:
10 dd
You can also specify a range of lines to delete. For instance, to delete lines 10-15:
:10,15d
Or you can move the lines, for instance move lines 10-15 below line 20:
:10,15m20
Or you can copy the lines:
:10,15t20
What I do is, starting with the cursor at the start of the line (in some editors you have to press home twice to do this), hold shift and press down until all lines that I want to delete are selected. Then I press delete.
In Eclipse you can ALT-↓ or ALT-↑ to move a line. I find this incredibly useful, as well as ALT-SHIFT-{↓, ↑} to copy a line. In addition, it doesn't wreck your clipboard. It even corrects indentation as the line is moving!
Adding to the existing vim answer, you can use d along with any cursor movement command to delete from the cursor's current position to the new position. For example, to delete...
...to end-of-paragraph (usually meaning "to the next blank line"): d}
...the line containing the cursor and the next 5 lines: d5j
...a set of parentheses, braces, etc. and its contents: d% (with the cursor on the opening or closing paren/brace/etc.)
...to the third appearance of the word "foo": d3/foo
It's quite flexible.
Learn to use your cursor keys.
For moving lines I do the following:
Use ↑/↓to move to the line you want to copy.
Hit Home if not there already, and again if it places the cursor after whitespace.
Then press Shift+↓ to select the line (or lines) you want to move
Ctrl+X to cut the line.
Move Up/Down to the line you want to insert
Ctrl+V
This should work in pretty much any text editor on Windows.
When deleting lines I still tend to use Ctrl+X (although I guess I also use backspace) as the above is so ingrained in how I edit, and it's also more forgiving.
(Although I find them disorienting on the occasions I use Macs, I think Apple might have been on to something with the way they've set up the Home/End, skip word shortcuts on Macs)
Ctrl+Shift+L removes line without copying it to the buffer.
in Eclipse i use CTRL+ D to delete a single line (or a couple)
for many lines i'll select them with the mouse or with SHIFT + ARROW then press the DEL key.
In addition to the above, use Resharper for Visual Studio to do what you want. Best VS plugin you will find ever. It provides a bunch of different commands that help with moving/deleting/copying code here there and everywhere. Not to mention refactor/generate/etc code.
Ctrl-Shift-Alt ↑or ↓ will move a method up or down, line up or down, etc.
Shift-Del - deletes the current line, but puts it in the clipboard (unless you modify your settings to not do this - I'm trying to recall if this is a VS standard shortcut, not just Resharper - been too long).
Ctrl-C, Ctrl-X without selecting copies/cuts the current line.
And on and on...
See http://www.jetbrains.com/resharper/docs/ReSharper40DefaultKeymap2.pdf for a full list.
Using the Brief keyboard mapping this is done using the Alt+L to mark the line and the - key on the numeric keypad (or Alt+D) to cut the line to clipboard. The cut will remove the line entirely, including the newline character.
Hitting the Ins key on the numeric keypad would put the line back into the document including the newline character.
IMHO Brief is a really well designed keyboard mapping.
PS: I think MSVC has an option to emulate the Brief keyboard mapping.
In Emacs, in addition to C-k (and C-k with a numeric prefix arg, to kill N lines), you can just use the mouse:
To kill one line: triple-click it, then right-click twice
To kill multiple lines: triple-click the first line, then right-click on the last line ("first" line can be after the "last" line -- "first" here means first one you click)

Resources