Home Download Buy Blog Forum Support

A better alt+q?

A better alt+q?

Postby mrdarcy on Tue Nov 03, 2009 1:50 pm

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!
mrdarcy
 
Posts: 8
Joined: Tue Oct 27, 2009 10:39 am

Re: A better alt+q?

Postby SteveCooperOrg on Wed Nov 04, 2009 4:47 pm

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.
Maybe what you're looking for is at the http://www.sublimetextwiki.com
SteveCooperOrg
 
Posts: 193
Joined: Thu Mar 20, 2008 9:53 am

Re: A better alt+q?

Postby ToddFiske on Wed Nov 04, 2009 11:07 pm

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:

Code: Select all
         # 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.
ToddFiske
 
Posts: 38
Joined: Wed Nov 04, 2009 10:43 pm

Re: A better alt+q?

Postby mrdarcy on Fri Nov 06, 2009 8:52 am

First of all: Thanks! :)

I’m not a programmer but I’ll try to tackle that myself!
mrdarcy
 
Posts: 8
Joined: Tue Oct 27, 2009 10:39 am

Re: A better alt+q?

Postby jps on Fri Nov 06, 2009 9:49 am

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.
jps
Site Admin
 
Posts: 3062
Joined: Wed Mar 19, 2008 12:33 pm

Re: A better alt+q?

Postby mrdarcy on Fri Nov 06, 2009 10:37 am

Thank you, sounds great!
mrdarcy
 
Posts: 8
Joined: Tue Oct 27, 2009 10:39 am

Re: A better alt+q?

Postby ToddFiske on Fri Nov 06, 2009 6:31 pm

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?
ToddFiske
 
Posts: 38
Joined: Wed Nov 04, 2009 10:43 pm

Re: A better alt+q?

Postby jps on Sun Nov 08, 2009 11:14 am

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.
jps
Site Admin
 
Posts: 3062
Joined: Wed Mar 19, 2008 12:33 pm

Re: A better alt+q?

Postby vim on Sun Nov 08, 2009 1:14 pm

beautiful! saves much work when revising blocks of comments, and need to reformat it all - cool!
vim
 
Posts: 298
Joined: Tue May 27, 2008 6:51 pm


Return to General Discussion

Who is online

Users browsing this forum: pgrippi and 25 guests