Author Topic: Macro to create commented backup-history-entry of all source files  (Read 356 times)

Jmathew

  • Community Member
  • Posts: 10
  • Hero Points: 0
I would like to create a macro that could be manually triggered that would:
1. Popup a prompt where a user could enter a comment.
2. create a backup-history-entry for every file in the "Source Files" folder (no matter if the file has been changed or not) setting that comment
The macro should work regardless of the "File Options -> Backup -> Make Backup Files" setting being enabled or not.
But I have no idea where to start.
Would that even be possible and easy to do?

Graeme

  • Senior Community Member
  • Posts: 1910
  • Hero Points: 217
Re: Macro to create commented backup-history-entry of all source files
« Reply #1 on: April 25, 2017, 09:25:29 am »
Possible I guess.  This code adds a comment to every open buffer - for_each_buffer iterates through all visible open buffers.  The comment gets added to the "newest version" of the file in the backup history.  If you want to create a new entry in the backup history you'll probably have to make a change to the file and save it.  I think you can "touch" a file with   _save_file()  - but this doesn't get you a new entry in the backup history.  If you need to add a multi-line comment instead of a single line you'll need to make your own dialog  - select "new form" in the Macro menu.  Let me know if you need help with that.

Dunno how to get a list of all "source files".  You can get a list of all project files like this, then iterate through them selectively.  You can use the function get_extension(filename) to return the file extension.

Code: [Select]
   _str files_to_comment [];
   _getProjectFiles( _workspace_filename, _project_get_filename(), files_to_comment, 1);


Code: [Select]
int xdelta_add_a_comment()
{
   _str versionList[];
   DSListVersions(p_buf_name, versionList);
   if ( versionList._length() == 0 ) {
      return 0;
   }
   DSSetVersionComment(p_buf_name, versionList._length() - 1, _param1);
   return 0;
}

_command void xdelta_add_comment_to_all_buffers() name_info(',')
{
   int result = textBoxDialog(
      "blah", // Form caption
      TB_RETRIEVE,      // Flags
      11000,             // textbox width
      "",               // Help item
      "\t Enter a comment",
      "blahblah1",
      "Comment :" );

   if (result == COMMAND_CANCELLED_RC) {
      return;
   }
   if (_param1 == '') {
         return;
   }

   for_each_buffer("xdelta_add_a_comment");
}

Jmathew

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: Macro to create commented backup-history-entry of all source files
« Reply #2 on: April 25, 2017, 08:11:25 pm »
This is great, thank you! Single line comment is fine. Doing it with all project files is also fine.
How do you let each buffers execute commands in a foreach loop?
Code: [Select]
_str file_name = "";
 foreach (file_name in files_to_comment) {
???
}

Then maybe i could insert 1 character in each buffer then remove that character again then save the buffer to create a history entry, right?

Graeme

  • Senior Community Member
  • Posts: 1910
  • Hero Points: 217
Re: Macro to create commented backup-history-entry of all source files
« Reply #3 on: April 26, 2017, 11:58:05 am »
Unfortunately no, you can't create a new history entry if the file doesn't actually change.  I guess the diff mechanism finds nothing has changed so doesn't create a new entry.  If you un-comment the calls to delete_line in the code below you'll see you don't get a new history entry.

Code: [Select]
_command void xdelta_add_comment_to_project_files() name_info(',')
{
   int result = textBoxDialog(
      "blah", // Form caption
      TB_RETRIEVE,      // Flags
      11000,             // textbox width
      "",               // Help item
      "\t Enter a comment",
      "blahblah1",
      "Comment :" );

   if (result == COMMAND_CANCELLED_RC) {
      return;
   }
   if (_param1 == '') {
         return;
   }

   _str save_buf = _mdi.p_child.p_buf_name;

   _str files_to_comment [];
   _getProjectFiles( _workspace_filename, _project_get_filename(), files_to_comment, 1);

   int k;
   for ( k = 0; k < files_to_comment._length(); ++k  ) {
      _str versionList[];

      if (edit("+B " :+ files_to_comment[k]) == 0) {
         bottom();
         insert_line("");
         //_delete_line();
         _save_file("+DD");
      }
      else
      {
         edit(files_to_comment[k]);
         bottom();
         insert_line("");
         //_delete_line();
         _save_file("+DD");
         quit();
      }

      DSListVersions(files_to_comment[k], versionList);
      if ( versionList._length() > 0 )
         DSSetVersionComment(files_to_comment[k], versionList._length() - 1, _param1);
   }
   if ( save_buf != "" ) {
      edit(save_buf);
   }
}

Jmathew

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: Macro to create commented backup-history-entry of all source files
« Reply #4 on: April 26, 2017, 08:20:37 pm »
Thanks alot Graeme! Now, it does exactly what i imagined and i'll probably be using that macro alot.
I added a little "comment line hack" to prevent the files from getting bigger and bigger. And I also restore the position:

Code: [Select]
_command void xdelta_add_comment_to_project_files() name_info(',')
{
   int result = textBoxDialog(
      "blah", // Form caption
      TB_RETRIEVE,      // Flags
      11000,             // textbox width
      "",               // Help item
      "\t Enter a comment",
      "blahblah1",
      "Comment :" );

   if (result == COMMAND_CANCELLED_RC) {
      return;
   }
   if (_param1 == '') {
         return;
   }

   _str save_buf = _mdi.p_child.p_buf_name;

   _str files_to_comment [];
   _getProjectFiles( _workspace_filename, _project_get_filename(), files_to_comment, 1);

   int k;
   for ( k = 0; k < files_to_comment._length(); ++k  ) {
      _str versionList[];

      if (edit("+B " :+ files_to_comment[k]) == 0) { 
         typeless p;
         save_pos(p);
         bottom();       
         _str temp_line = "";
         get_line(temp_line);
         if (temp_line == "(**)") {
            _delete_line();
            insert_line("(***)");
         }
         else if (temp_line == "(***)") {
            _delete_line();
            insert_line("(**)");         
         }
         else {
            insert_line("(**)");
         }
         restore_pos(p);
         _save_file("+DD");
      }
      else
      {
         edit(files_to_comment[k]);
         bottom();
         _str temp_line = "";
         get_line(temp_line);
         if (temp_line == "(**)") {
            _delete_line();
            insert_line("(***)");
         }
         else if (temp_line == "(***)") {
            _delete_line();
            insert_line("(**)");         
         }
         else {
            insert_line("(**)");
         }
         
         _save_file("+DD");
         quit();

      }

      DSListVersions(files_to_comment[k], versionList);
      if ( versionList._length() > 0 )
         DSSetVersionComment(files_to_comment[k], versionList._length() - 1, _param1);
   }
   if ( save_buf != "" ) {
      edit(save_buf);
   }
}
« Last Edit: April 26, 2017, 08:22:43 pm by Jmathew »

Graeme

  • Senior Community Member
  • Posts: 1910
  • Hero Points: 217
Re: Macro to create commented backup-history-entry of all source files
« Reply #5 on: April 26, 2017, 10:39:27 pm »
ok, nice work.  I should have written this but I was too lazy

Code: [Select]
   _str save_buf = "";
   if ( !  _no_child_windows() ) {
      save_buf = _mdi.p_child.p_buf_name;
   }
I didn't do any testing of what happens when backup history is disabled.  _save_file("+DD") might create a delta file if one doesn't exist.

Jmathew

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: Macro to create commented backup-history-entry of all source files
« Reply #6 on: April 27, 2017, 06:36:22 pm »
Replaced what you posted... allthough I don't know what difference it made.  :D I disabled backup history and the macro seems to also work correctly. Then I came up with a better idea to make the mechanism that is tricking the diff mechanism invisible:

Code: [Select]
_command void xdelta_add_comment_to_project_files() name_info(',')
{
   int result = textBoxDialog(
      "Create Backup History entry in all buffers", // Form caption
      TB_RETRIEVE,      // Flags
      11000,             // textbox width
      "",               // Help item
      "\t Enter a comment",
      "blahblah1",
      "Comment :" );

   if (result == COMMAND_CANCELLED_RC) {
      return;
   }
   if (_param1 == '') {
         return;
   }

      _str save_buf = "";
   if ( !  _no_child_windows() ) {
      save_buf = _mdi.p_child.p_buf_name;
   }

   _str files_to_comment [];
   _getProjectFiles( _workspace_filename, _project_get_filename(), files_to_comment, 1);

   int k;
   for ( k = 0; k < files_to_comment._length(); ++k  ) {
      _str versionList[];

      if (edit("+B " :+ files_to_comment[k]) == 0) { 
          typeless p;
         save_pos(p);
         bottom();       
         _str temp_line = "";
         get_line(temp_line); 
         
         if (temp_line == "") {
            end_line();
            if (p_col == 1) {
               insert_literal(" ");
            }
            else {
               linewrap_rubout();
            }
         }
         else {
            insert_line("");
         }   
         
         restore_pos(p);
         _save_file("+DD");
      }
      else
      {
         edit(files_to_comment[k]);
                  bottom();       
         _str temp_line = "";
         get_line(temp_line); 
         
         if (temp_line == "") {
            end_line();
            if (p_col == 1) {
               insert_literal(" ");
            }
            else {
               linewrap_rubout();
            }
         }
         else {
            insert_line("");
         }   
         
         _save_file("+DD");
         quit();
      }

      DSListVersions(files_to_comment[k], versionList);
      if ( versionList._length() > 0 )
         DSSetVersionComment(files_to_comment[k], versionList._length() - 1, _param1);
   }
   if ( save_buf != "" ) {
      edit(save_buf);
   }
}

Graeme

  • Senior Community Member
  • Posts: 1910
  • Hero Points: 217
Re: Macro to create commented backup-history-entry of all source files
« Reply #7 on: April 28, 2017, 06:53:11 am »
The call to _no_child_windows checks if there are any edit buffers open in the _mdi window.  p_buf_name isn't valid if there aren't any edit buffers open.

Jmathew

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: Macro to create commented backup-history-entry of all source files
« Reply #8 on: April 28, 2017, 08:43:23 am »
Ah, after some research, I think I got it. Anyways, thank you again!