Author Topic: Looking for macro to delete to start of next word  (Read 12922 times)

Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Looking for macro to delete to start of next word
« on: May 08, 2009, 08:50:55 PM »
I am looking for a macro that will allow me to delete all characters up to the start of a new word. In MS Word and other editors, it is what happens when you press Ctrl+Delete. I'm guessing I might need some type of loop, looking at the current character, but could not find the appropriate built in commands, or how to know what is the current character at the cursor.

Thanks in advance for all help.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Looking for macro to delete to start of next word
« Reply #1 on: May 09, 2009, 12:43:59 AM »
Record a macro that does Ctrl-Shift-Right  delete.

Code: [Select]
_command last_recorded_macro() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _macro('R',1);
   deselect();
   _select_char('','E');
   next_word();
   select_it('CHAR','','E');
   delete_selection();
}

or if the cursor is not at the start of a word and you want to delete the whole word (not just from the cursor) you can do this
- select word, cursor left, ctrl-shift-right, delete

Code: [Select]
_command last_recorded_macro() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _macro('R',1);
   execute('select-whole-word');
   _begin_select('',false,false);
   _deselect();
   deselect();
   _select_char('','E');
   next_word();
   select_it('CHAR','','E');
   delete_selection();
}

Graeme

Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #2 on: May 11, 2009, 03:41:42 PM »
This seems to work, Thanks for the input.

Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #3 on: May 13, 2009, 03:17:39 PM »
In general, the macro works, but deletes through punctuation, such as , .  which are not identified as words. If I change the opitons for Word chars in Tools | Options | Languages, then word selection gets messed up.

Any other ideas?

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Looking for macro to delete to start of next word
« Reply #4 on: May 13, 2009, 03:44:33 PM »
cut-word and delete-word commands are used to remove the characters up until the end of the current word from the cursor postion.  Is that what your looking for or something else?  Also see cut-whole-word and delete-whole-word which will remove the entire word at cursor.

Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #5 on: May 13, 2009, 03:56:50 PM »
That has similar problems. If a line ends with punctuation characters, such as ()[]{}, . ; they are not recognized as words, so the deleting goes right through them.

For example, if I have the following code:
AsyncReset_n                : in  std_logic;                      -- asynchronous reset

and my cursor is positioned after the d in std_logic, delete-word gives:
  AsyncReset_n                : in  std;                      -- asynchronous reset

which is fine.
Execute delete-word 1 more time and you get:
  AsyncReset_n                : in  std reset
because delete-word deleted the ; the white space, the -- and the word asynchronous.

In this case, I wanted it to only delete the ; and/or the white space.
In other words, I want to delete the current word if I'm in the middle of a word, otherwise delete to the start of the next word. I'm okay with the macro not deleting enough (for instance, stopping at the white space), but the commands I have found delete too much, often pulling and deleting text from the subsequent line.

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Looking for macro to delete to start of next word
« Reply #6 on: May 13, 2009, 04:11:43 PM »
Ahh, I see, the issue is that your on a non-word character there, and the default behavior is to jump to the end of the next word. 

There are three def-vars you can set which do change the behavior of the select-word op.  First there is def-brief-word (default for Brief emulation), def-vcpp-word (default for .Net, Visual C++ emulations), and def_word_delim.  You can toggle this on/off on the SlickEdit command line with set-var def_brief_word 1 (1 for on or 0 for off).

Both def-brief-word and def-vcpp-word should handles cursor on blanks, cursor on non-word-char, and trailing blanks.  def_word_delim should handle the non-word-char case, but not the trailing blanks.  Try one of these settings, if these don't work better for you, I'm sure it that we come up with some other solution.  What is Word chars set to in this language?




« Last Edit: May 13, 2009, 04:53:18 PM by Lee »

Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #7 on: May 13, 2009, 05:14:58 PM »
Can you explain def-vars or where I can read about this? Is this a setting somewhere?

I tried to find help on these words in the index, but only found a brief explanation for def_brief_word:
boolean def_brief_word
If true, use Brief style select word and delete word.
Default:
false, except for Brief emulation

This doesn't explain how it works. I could not find any reference to def-vcpp-word or def_word_delim.

My word chars is set to:
A-Za-z0-9_$

I tried experimenting with adding punctuation to the list, but they caused other problems.

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Looking for macro to delete to start of next word
« Reply #8 on: May 13, 2009, 05:27:41 PM »
def-vars are global Slick-C macro variables.  When you make changes to any changes in the Options, you are usually modifying a Slick-C variable.  You can also modify them directly by name, either using the Slick-C command line (set-var def-whatever) or you can use the main menu Macro > Set Macro Variable... .  The three specific word select def-vars I referenced aren't directly set in the options unless you change emulations, but you can experiment with them to see if one of these is better suited to your needs without having to switch emulations.  We've tried to document as many of the def-vars over the years, but some have slipped through over the years.  I'll file documentation requests for those.  I'll also make a note in our feature tracker that we might want to expose this as a configuration option in the future without having to change emulations.

Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #9 on: May 13, 2009, 06:02:19 PM »
The def-vcpp-word seems to work better than def-brief-word.
What happens if both have been turned on? I wasn't sure, so I turned off def-brief-word before turning on def-vcpp-word.

Is this stored in my configuration somewhere, or do I need to execute this command every time I start up? Will it be exported if I use the Export All Options?

How would I know how these emulations are supposed to work? Is it just the familiarity with vcpp or brief?

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Looking for macro to delete to start of next word
« Reply #10 on: May 13, 2009, 09:20:15 PM »
The actual implementation is in macros\stdcmds.e in pselect_word (~line 6440).  Unless I'm mistaken, I'd say def_brief_word and def_vcpp_word are identical for this operation.  I know there are also differences in next_word/prev_word that also use those same def-vars, I'd have to look at those implementations to say for sure.

def-vars are saved in your configuration, so you will not have to worry about execute the set-var command every session.   The only time it would get reset is if you switch emulations.  And since the def-vars were always implied from the emulation and have no direct configuration in the Options menus, they are not exported with Export All Options.  Like I said, this is probably something we can refactor into it's own configuration option.

Unless you've worked in other editors and environments, you probably wouldn't know anything about those other emulations.  As much as possible, we try to accomodate users who want SlickEdit to work the way the want it to or the way they are used to.  If you find something that doesn't work the way you expect, there is probably already an option or alternate command already implemented that changes it.  Just ask, thats what these boards are for.

Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #11 on: May 13, 2009, 09:27:56 PM »
Thanks for all your help and the time you took to respond!

steve-o

  • Community Member
  • Posts: 8
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #12 on: May 15, 2009, 10:59:33 PM »
Here's a kind of cool one I find very powerful. It prompts for a character, then deletes everything between the cursor and the next instance of that character.

_command boolean delete_to_char()
{
   typeless keytab_used='';
   typeless keyname='';
   typeless k='';
   typeless status=prompt_for_key(nls('Delete To:')' ',keytab_used,k,keyname,'','','',1);

   int mark_id = _alloc_selection();
   begin_select(mark_id);
   select_char();

   int x = search(k, "E");
   delete_selection();
   end_select(mark_id);

   _free_selection(mark_id);
   return true;
}


Gardener

  • Community Member
  • Posts: 15
  • Hero Points: 0
Re: Looking for macro to delete to start of next word
« Reply #13 on: May 15, 2009, 11:01:19 PM »
Thanks, I will try to keep yours in mind.