Author Topic: Persistent variable  (Read 12191 times)

wconley

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Persistent variable
« on: January 14, 2019, 02:10:54 PM »
I would like to create a macro to insert a function header comment block that includes the author's name. Not sure how to globally set the authors name that I can access each time the macro is executed. Any suggestions?

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2896
  • Hero Points: 153
Re: Persistent variable
« Reply #1 on: January 14, 2019, 02:37:22 PM »
You should be able to call the function getAuthor() if the userid will do.

wconley

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Re: Persistent variable
« Reply #2 on: January 14, 2019, 02:48:34 PM »
Hi Dan,

I'm looking for the full name string. I created a simple form with a text box and button. The idea is the user can invoke the form, type in his name and press the button. I'd like to store what he typed in a place that is persistent so he only has to do this once and in a way that I can grab it from the 'create function header' macro. Not sure if I'm approaching this right or where I can store the name string.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2896
  • Hero Points: 153
Re: Persistent variable
« Reply #3 on: January 14, 2019, 02:51:58 PM »
Are you ok with them typing it in the first time each instance of the editor, or want them to do it just once?

wconley

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Re: Persistent variable
« Reply #4 on: January 14, 2019, 02:56:17 PM »
Would rather be able to do it just once but first time for each instance of the editor would be okay too.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Persistent variable
« Reply #5 on: January 15, 2019, 10:29:11 AM »
You can save it to a file with the code below.
On Windows you can read the system environment variable USERNAME with get_env('USERNAME');

Code: [Select]
_command void xsave_user_name(_str username = 'fred')
{
   _str filename = _ConfigPath() :+ 'mydata.ini';

   int temp_view_id;
   int orig_view_id = _create_temp_view(temp_view_id);
   insert_line(username);
   p_window_id = orig_view_id;
   int status = _ini_put_section(filename, 'SectionUserName', temp_view_id);
   if (status) {
      _message_box('Error writing file : ' :+ filename);
   }
}


_command _str xload_user_name()
{
   _str filename = _ConfigPath() :+ 'mydata.ini';
   int tempWID;
   status := _ini_get_section(filename, 'SectionUserName', tempWID);
   if (status)
   {
      //_message_box('Error reading file : ' :+ filename);
      return '';
   }

   origWID := p_window_id;
   p_window_id = tempWID;
   top();
   _str line = '';
   get_line(line);
   p_window_id = origWID;
   // message(line);
   return line;
}




Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2896
  • Hero Points: 153
Re: Persistent variable
« Reply #6 on: January 15, 2019, 10:46:05 AM »
Graeme for the win.  This could be improved slightly by calling textBoxDialog to get the first value... but if they're just entering it once, the ini file should be fine.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Persistent variable
« Reply #7 on: January 15, 2019, 11:32:54 AM »
Hi Dan
You just lost me slightly there.  Do you mean it's easier just to use textBoxDialog and not have the ini file at all?  I did think about textBoxDialog but I wasn't sure if it kept its data through an upgrade and the OP said he already made a form so I thought he would probably want to keep it.


Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2896
  • Hero Points: 153
Re: Persistent variable
« Reply #8 on: January 15, 2019, 12:50:48 PM »
Hi Dan
You just lost me slightly there.  Do you mean it's easier just to use textBoxDialog and not have the ini file at all?  I did think about textBoxDialog but I wasn't sure if it kept its data through an upgrade and the OP said he already made a form so I thought he would probably want to keep it.

No, just that if the ini isn't found, you could use it to prompt the first time.

wconley

  • Junior Community Member
  • Posts: 5
  • Hero Points: 0
Re: Persistent variable
« Reply #9 on: January 15, 2019, 01:13:10 PM »
Dan and Graeme, this sounds perfect. Thank you both very much!!!

Dennis

  • Senior Community Member
  • Posts: 3954
  • Hero Points: 515
Re: Persistent variable
« Reply #10 on: January 16, 2019, 03:50:31 PM »
If you pass a retrieve_name to textBoxDialog(), along with the TB_RETRIEVE_INIT flag, then you can take advantage of dialog box retrieval to do the heavy lifting for you.  In fact, this will allow you to use F7/F8 to flip through previous results you input:

Code: [Select]
   result := textBoxDialog("I have a question",     // Form caption
                                        TB_RETRIEVE_INIT,             // Flags
                                        0,             // Use default textbox width
                                        "",            // Help item
                                        "42,Cancel",            // Buttons and captions
                                        "the_answer_to_life_the_universe_and_everything", // Retrieve Name
                                        "What is the answer:");

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Persistent variable
« Reply #11 on: January 16, 2019, 09:43:28 PM »
ok, do you know if the information persists through an upgrade?

Dennis

  • Senior Community Member
  • Posts: 3954
  • Hero Points: 515
Re: Persistent variable
« Reply #12 on: January 16, 2019, 09:50:00 PM »
It is stored in vrestore.slk.  Also, we are looking at adding dialog retrieval support to parameterized alias expansion, so for a case like the OP's request, you wouldn't need to write any code at all.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2896
  • Hero Points: 153
Re: Persistent variable
« Reply #13 on: January 17, 2019, 11:04:22 AM »
If you pass a retrieve_name to textBoxDialog(), along with the TB_RETRIEVE_INIT flag, then you can take advantage of dialog box retrieval to do the heavy lifting for you.  In fact, this will allow you to use F7/F8 to flip through previous results you input:

Code: [Select]
   result := textBoxDialog("I have a question",     // Form caption
                                        TB_RETRIEVE_INIT,             // Flags
                                        0,             // Use default textbox width
                                        "",            // Help item
                                        "42,Cancel",            // Buttons and captions
                                        "the_answer_to_life_the_universe_and_everything", // Retrieve Name
                                        "What is the answer:");
I think this is the best solution.  They are prompted every time but will generally just hit enter after the first time because the answer is the same.

Dennis gets a hero point, not for the idea, but for the Hitchhiker's Guide reference.