Sublime Forum

A better alt+q?

#1

Hi! While I’m overall growing very fond of Sublime Text as a Windows TextMate/BBedit surrogate I’m using it heavily for editing (web)mail, especially from OTRS and I’m using alt+q a lot to fix up quoted text. Unfortunately that function doesn’t seem to let itself be restricted to the selected portion of the text only.

When I have something like

Lorem ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lobortis est in orci varius egestas. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lobortis est in orci varius egestas. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lobortis est in orci varius egestas. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

lorem lorem …

I have to insert a blank line before and after the longer paragraph in order to restrict the reformatting to that (and of course remove the blank lines afterwards again).

If I don’t do that, even if I first select the lines I’d like to reformat them, alt+q will affect the whole quoted text, ie. instead of the desired result

Lorem ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lobortis est in
orci varius egestas. Class aptent taciti sociosqu ad litora torquent per
conubia nostra, per inceptos himenaeos. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Sed lobortis est in orci varius egestas. Class
aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos
himenaeos. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
lobortis est in orci varius egestas. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos.

lorem lorem …

I get

Lorem ipsum Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
lobortis est in orci varius egestas. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. Lorem ipsum dolor sit
amet, consectetur adipiscing elit. Sed lobortis est in orci varius egestas.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
inceptos himenaeos. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed lobortis est in orci varius egestas. Class aptent taciti sociosqu ad
litora torquent per conubia nostra, per inceptos himenaeos. lorem lorem …

Also sometimes a single word will “stick out” too far. The wrap margin is set to 78 and in the above (correct) example the line

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lobortis est in

will look like

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lobortis est
in

instead. Placing the cursor behind the word “in” will show column 79 …

Might this behavior be fixed/changed in a future release of Sublime Text?

Thanks in advance!

0 Likes

#2

Just to let people know, the code is available in the sublime text packages folder, somewhere like

C:\Documents and Settings\Steve\Application Data\Sublime Text\Packages\Default\Paragraph.py

So if anyone fancies putting together a different version, there’s some code to start.

0 Likes

#3

mrdarcy said:

Also sometimes a single word will “stick out” too far. The wrap margin is set to 78…

I found that too. It happens when View> Word Wrap is also turned on. I think it wraps one character too soon, or there’s some off-by-one thing happening between the wrapping code in Paragraph.py and the Python textwrap module. For me, turning off View> Word Wrap was enough to make it look right.

As SteveCooperOrg suggested, a solution for your unique wrapping preference is possible by working with the wrapping code, particularly how it selects text in expandToParagraph. I haven’t dug into it too much but would like to at some point.

Not directly related to your question but more to the subject of the thread, I did change the code to leave a single selection at the end of the paragraph though, instead of leaving everything selected. After the last for loop (for s in view.sel()) but within the previous if (if len(paragraphs) >)) I added this block:

			# TF code to leave cursor at end of wrapped paragraph
			view.sel().clear()
			nEnd = s.end() - 1
			view.sel().add(sublime.Region(nEnd, nEnd))

This clears all selections, then reuses the final selection from the for loop to create a new selection point at the end of the last line, before the final newline. This leaves the cursor in position to continue typing, instead of trying to leave the cursor at its previous position as Jon mentions in his comment in that code.

I imagine there is a better way to put the cursor at the end of the last paragraph, but as a newbie to Sublime Text coding, that’s what I came up with. Suggestions of better ways would be much appreciated.

0 Likes

#4

First of all: Thanks! :smile:

I’m not a programmer but I’ll try to tackle that myself!

0 Likes

#5

FYI, I’ve made some changes to this that will appear in the next beta version: The wrap width will be respected correctly when a per-line prefix (such as "> ") is being used, and the cursor will be left at the end of the paragraph, rather than selecting the entire paragraph.

0 Likes

Alt + Q with LaTeX commands
#6

Thank you, sounds great!

0 Likes

#7

I’m looking forward to these changes Jon, thanks.

Is there a more direct way to leave the cursor at the end of the paragraph than what I coded here?

0 Likes

#8

This is now in 20091108.

I’ve taken a similar approach to leave the cursor at the end of the paragraph, where the selection is adjusted after the rest of the processing.

0 Likes

#9

beautiful! saves much work when revising blocks of comments, and need to reformat it all - cool!

0 Likes