Author Topic: Way to wrap selection?  (Read 7579 times)

jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Way to wrap selection?
« on: February 23, 2010, 08:26:54 PM »
Hi folks,

Is there a way to highlight a line or lines and run a command that will wrap those lines, inserting carriage returns as needed to wrap at my "vertical line column" (or some other configurable setting), and inserting spaces as needed to indent each line of the new block as far in as the first line of my original selection was indented?  I think this might be the last feature I still miss from Codewright.

Thanks!

Jim

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Way to wrap selection?
« Reply #1 on: February 24, 2010, 09:55:18 AM »
This code doesn't do exactly what you want but it might give you a start.  The idea of calling next-word prev-word is so it doesn't break a word in half.  The column the line gets wrapped to is determined by slick_enter().

Code: [Select]
_command void shorten_line() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
   strip_trailing_spaces();
   _end_line();
   if (p_col <= 64)
   {
      return;
   }
   goto_col(63);
   next_word();
   prev_word();
   slick_enter();
   cursor_down();
   begin_line_text_toggle();
}

ScottW, VP of Dev

  • Senior Community Member
  • Posts: 1471
  • Hero Points: 64
Re: Way to wrap selection?
« Reply #2 on: February 24, 2010, 03:55:05 PM »
Can I ask what you're editing? It doesn't seem like you would want to do this to code. We have a comment wrapping feature for editing comments that will do this as you type. If you're editing text, there's also settings for that.

I'd like to understand what you're doing so I can better assess how well we support that activity.  Thanks.

jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Re: Way to wrap selection?
« Reply #3 on: February 24, 2010, 04:19:02 PM »
Hi Scott,

I'm editing HTML.  In this case, long paragraphs that I like indented properly, with each line no longer than 80 characters.  I prefer not to rely on editor wrapping (where the lines are really unbroken in the underlying file) because anyone using an editor that doesn't support that feature then has to scroll way out to the right to see all the text.  Or maybe their editor does wrapping poorly such that the paragraph is not all indented correctly.  I want the formatting to be this in the file itself:

Code: [Select]
   <p>
      Sing, O goddess, the anger of Achilles son of Peleus, that
      brought countless ills upon the Achaeans. Many a brave soul
      did it send hurrying down to Hades, and many a hero did it
      yield a prey to dogs and vultures, for so were the counsels
      of Jove fulfilled ...
   </p>

Right now I have a file that is not formatted the way I'd like, and in CodeWright there was a "wrap" command that I could map to a keyboard shortcut, and I could go through and quickly highlight the badly formatted paragraphs and wrap them like I described in my original post.

It's a small thing, thanks for thinking about it!

Thanks!

Jim

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Way to wrap selection?
« Reply #4 on: February 24, 2010, 04:24:12 PM »
I should imagine he's trying to wrap code lines that are too long to fit on the screen.  Softwrap is better than nothing but it's still hard to read because it breaks at a fixed column and starts at column one on the next line.  How about having an intelligent softwrap that breaks at whole words and aligns with the start of the previous line plus an indent margin.

Where I work, people use different size fonts in their editor so some people have lines that are too long for others.

Graeme

jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Re: Way to wrap selection?
« Reply #5 on: February 24, 2010, 04:31:48 PM »
Graeme, thanks!  If I have to roll my own I'll use your code as a starting point.

ScottW, VP of Dev

  • Senior Community Member
  • Posts: 1471
  • Hero Points: 64
Re: Way to wrap selection?
« Reply #6 on: February 24, 2010, 04:50:41 PM »
We do have a feature to help with HTML and XML, called "XML/HTML Formatting". It allows you to define schemes that direct SlickEdit how to wrap specific tags. To turn it on, select Document > XML/HTML Formatting > Enable HTML Formatting, while you have an HTML document open. That enables it for this one document. Turn it on for all HTML documents using the language settings, Document > HTML Options > Formatting. At the bottom of the screen are checkboxes for "Enable content wrapping", which will automatically wordwrap the specified tags and "Enable tag layout", which will help position the tags.

This feature requires a properly defined scheme to work well. If the formatting directions don't match what you want it to do, you will fight with this a lot. You may just want to define a scheme that does content wrapping on the <p> tag and leaves everything else alone. It's better to start small and build up the formatting rules you need. You will need to select the "Content Wrap" tab and set the "Tag content width" to "Fixed right margin" for the effect you have requested.

I use this system every day to edit docbook files for our User Guide. It works very well and saves me a lot of time. There are occasional hiccups where it gets confused about where to wrap, but it saves me more time than it costs me.

You can read more about this feature in the User Guide under Editing Features > Language-Specific Editing > XML/HTML Formatting.

I tried to find something else that might help, but all of the other word wrap stuff is based on a left and right margin, rather than wrapping based on the indentation in the code. Please let me know if this works for you.

jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Re: Way to wrap selection?
« Reply #7 on: February 24, 2010, 05:09:07 PM »
Thanks Scott, good stuff, although I don't really think either approach fits the bill for me.  The times when I want this feature are random enough that an automatic solution isn't really going to work, and while I can work with a wrap that respects a right margin, I really need the left margin to be set to wherever the first line of the selection is indented to.  That will rarely be in a predictable place.

If I can find the time to create a macro I'll post it, but I'm not sure that will happen.

Thanks again!

Jim

ScottW, VP of Dev

  • Senior Community Member
  • Posts: 1471
  • Hero Points: 64
Re: Way to wrap selection?
« Reply #8 on: February 24, 2010, 07:39:37 PM »
The XML/HTML Formatting does wrap based on the code's location, not a fixed left margin. It can be easily togged on and off using the Document menu or the html_formatting_toggle command. After toggling it on, you can then reformat a paragraph by positioning the cursor in the paragraph, press backspace or delete to remove a space, and retype the space. The paragraph will then be reformatted according to the spec.

Before:
Code: [Select]
  <p>
    Sing, O goddess, the anger of Achilles son of Peleus, that brought countless ills upon the Achaeans. Many a brave soul did it send hurrying down to Hades, and many a hero did it yield a prey to dogs and vultures, for so were the counsels of Jove fulfilled ...
  </p>

After:
Code: [Select]
  <p>
    Sing, O goddess, the anger of Achilles son of Peleus, that brought countless
    ills upon the Achaeans. Many a brave soul did it send hurrying down to
    Hades, and many a hero did it yield a prey to dogs and vultures, for so were
    the counsels of Jove fulfilled ...
  </p>




jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Re: Way to wrap selection?
« Reply #9 on: February 24, 2010, 08:35:24 PM »
Hmm, maybe, thanks Scott.  Defining a scheme, hitting a menu, toggling it, backspacing and re-spacing doesn't really appeal to me the way a keyboard shortcut macro would, but on the other hand this feature exists whereas the macro doesn't unless I write it, so we'll see.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Way to wrap selection?
« Reply #10 on: February 24, 2010, 09:44:26 PM »
Hmm, maybe, thanks Scott.  Defining a scheme, hitting a menu, toggling it, backspacing and re-spacing doesn't really appeal to me the way a keyboard shortcut macro would, but on the other hand this feature exists whereas the macro doesn't unless I write it, so we'll see.
Try recording a macro when you do that - then see if replaying it gives the right result.

ScottW, VP of Dev

  • Senior Community Member
  • Posts: 1471
  • Hero Points: 64
Re: Way to wrap selection?
« Reply #11 on: February 24, 2010, 10:42:51 PM »
The scheme only has to be defined once to tell the system what and how to format.

Then you can record a command that toggles on the formatting, inserts and deletes a space, and toggles off the formatting.

For me, hitting backspace followed by space is as fast as typing a key sequence, so you might have the overhead of a separate sequence to toggle the command. Seems like there ought to be a function to reflow the context, but I couldn't find one.

jim

  • Community Member
  • Posts: 30
  • Hero Points: 1
Re: Way to wrap selection?
« Reply #12 on: February 25, 2010, 12:05:05 AM »
[edited because I wrote "adaptive formatting" by mistake]

Turns out all the XML/HTML Formatting menu items are disabled for me, even when I open a file with an *.html extension.  I'm guessing this is because I have a "Jim" user-defined language to which I have assigned all file extensions I work with (including htm/html), which allowed me to turn off auto-complete, syntax highlighting, and various other features I don't care for across the board.
« Last Edit: February 25, 2010, 01:43:30 AM by jim »