Author Topic: How do you convert the current selection into a string  (Read 9416 times)

pmsteinm

  • Senior Community Member
  • Posts: 114
  • Hero Points: 3
How do you convert the current selection into a string
« on: July 11, 2006, 03:45:27 PM »
I'm trying to write a macro to open the file indicated by the current selected text (NEdit has this feature, called "Open Selection").  Is there a function that will take the current selection and convert it to a string so I can pass it to the open function?

Zak

  • Community Member
  • Posts: 5
  • Hero Points: 0
Re: How do you convert the current selection into a string
« Reply #1 on: July 11, 2006, 07:27:34 PM »
use the filter_* functions... e.g.:
   filter_init();
   filter_get_string(mystr);
   edit_file(mystr);

Dr. Who

  • New Community Member
  • Posts: 1
  • Hero Points: 0
Re: How do you convert the current selection into a string
« Reply #2 on: July 18, 2006, 11:34:38 AM »
I, too, had trouble figuring out this. Thanks Zak for steering us in the right direction. Here's what I end up with:

_command OpenFile() blah...blah...blah
{
   _macro('R',1);
      filter_init();
      filter_get_string(myfname);
      if (maybe_find_file("",myfname) == 0) {
         e(myfname);
         execute('view-line-numbers-toggle');
      }
}

This version issues an error if the file is not found, otherwise opens the file for editing and toggles line numbers on. Pretty simple but I had a heck of a time trying to figure out how to do it.
« Last Edit: July 18, 2006, 11:36:23 AM by Dr. Who »

pmsteinm

  • Senior Community Member
  • Posts: 114
  • Hero Points: 3
Re: How do you convert the current selection into a string
« Reply #3 on: July 18, 2006, 03:47:37 PM »
Thank you Zak and Dr. Who.  This has been a feature I've wanted for a long time.  Here's what I ended up with thanks to your help:

_command open_selected() {

  // See if anything is selected
  if(_isnull_selection()){
    _message_box("Nothing Selected!");
    return 0;
  }

  // Get Current Selection
  filter_init();
  filter_get_string(filename);
  filter_restore_pos();

  // Parse out first "filename" (this guards against the user having weird selections)
  filename = parse_file(filename);

  // Check if file exists
  if(maybe_find_file("",filename)==0) {
    if(e(filename)!=0) {
      _message_box("Unable to open file: " :+ filename);
    }
  }
} // open_selected()

This prints a mesage if nothing is selected, and restores the cursor position in the original file.