Author Topic: customizing save command  (Read 19831 times)

vineetvijay12

  • Community Member
  • Posts: 29
  • Hero Points: 0
customizing save command
« on: July 16, 2014, 09:14:56 AM »
I have a requirement customizing save command so i can copy file to shared location once i save the file using save button in slickedit..
Quick help will be highly appreciated.

Thanks
Vineet


Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: customizing save command
« Reply #1 on: July 16, 2014, 10:16:47 AM »
Copy the code below to a file of your own called mysave.e or something  - in your slick configuration folder.  I added the following lines to the official save_file function.

   int result = copy_file(filename, 'H:\slick-backups\' :+ strip_filename(filename,'P'));
   if (result != 0) {
      _message_box("slick backup main copy failed " :+ (_str)(result));
   }

Modify the location you want the file to be copied to.  If you want to replicate the directory structure on the server according to where the file comes from then you need to do a bit more work (using make_path).  Let me know if you need that.  To load the code, open the mysave.e file in slick and use the load module command on the macro menu.

Code: [Select]
#include "slick.sh"

#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)
#import "tagform.e"

/**
 * Writes current buffer to filename.  This function is a hook function
 * that the user may replace.  Options allowed by <b>_save_file</b>
 * built-in may be specified.
 * @param filename parameter should not contain options.
 *
 * @appliesTo Edit_Window
 *
 * @categories File_Functions
 *
 */
_str save_file(_str filename,_str options)
{
#if 0
   int renumber_flags=numbering_options();
   if (renumber_flags&VSRENUMBER_AUTO) {
      if (renumber_flags&VSRENUMBER_COBOL) {
         renumber_lines(1,6,'0',false,true);
      }
      if (renumber_flags&VSRENUMBER_STD) {
         renumber_lines(73,80,'0',false,true);
      }
   }
#endif
   typeless status=_save_file(options " "maybe_quote_filename(filename));
   if (!status && file_eq(strip(filename,'B','"'),p_buf_name)) {
      //_cbsave_filewatch();
#if 1
      call_list('_cbsave_');
      //10:51am 7/3/1997
      //Dan modified for auto-tagging
      if (def_autotag_flags2&AUTOTAG_ON_SAVE) {
         //messageNwait(nls('got here'));
         TagFileOnSave();
      }
#endif
   }

   int result = copy_file(filename, 'H:\slick-backups\' :+ strip_filename(filename,'P'));

   if (result != 0) {
      _message_box("slick backup main copy failed " :+ (_str)(result));
   }

   return(status);

}


vineetvijay12

  • Community Member
  • Posts: 29
  • Hero Points: 0
Re: customizing save command
« Reply #2 on: July 16, 2014, 10:39:42 AM »
yes that works with static path.
i want to get source path and create a destination path.
can you help me on this?

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: customizing save command
« Reply #3 on: July 16, 2014, 11:58:54 AM »


sure.  If you have autosave enabled you'll find it uses this function, so your autosave files will get copied to the server too.

Code: [Select]
   _str pa = 'H:\slick-backup\' :+ strip_filename(filename,'DN');
   if (!path_exists(pa)) {
      int result = make_path(pa);
      if (result) {
         _message_box("slick backup make path failed : " :+ result :+ pa);
         return status;
      }
   }

   int result2 = copy_file(filename, pa :+ strip_filename(filename,'P'));

   if (result2 != 0) {
      _message_box("slick backup main copy failed " :+ (_str)(result2));
   }

vineetvijay12

  • Community Member
  • Posts: 29
  • Hero Points: 0
Re: customizing save command
« Reply #4 on: July 16, 2014, 12:14:54 PM »
autosave is not enabeled, how to enable that.
even i want to make path dynamically as below, can you please help?
m:\vvijay_test_protocols_test_lbx_wtl\vv-test\makedoc, the SAVE function of SlikEdit will trigger a program to copy the file directly to \\home\vvijay\stage_area\vvijay_test_protocols_test_lbx_wtl\vv-test\makedoc

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: customizing save command
« Reply #5 on: July 16, 2014, 12:26:10 PM »
Do you mean you didn't understand what to do with the extra code I posted?
I don't use Linux so I can't test your paths.
Regarding autosave, in tools -> options, type autosave in the search text box at the top left.

Code: [Select]
#include "slick.sh"

#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)
#import "tagform.e"

/**
 * Writes current buffer to filename.  This function is a hook function
 * that the user may replace.  Options allowed by <b>_save_file</b>
 * built-in may be specified.
 * @param filename parameter should not contain options.
 *
 * @appliesTo Edit_Window
 *
 * @categories File_Functions
 *
 */
_str save_file(_str filename,_str options)
{
#if 0
   int renumber_flags=numbering_options();
   if (renumber_flags&VSRENUMBER_AUTO) {
      if (renumber_flags&VSRENUMBER_COBOL) {
         renumber_lines(1,6,'0',false,true);
      }
      if (renumber_flags&VSRENUMBER_STD) {
         renumber_lines(73,80,'0',false,true);
      }
   }
#endif
   typeless status=_save_file(options " "maybe_quote_filename(filename));
   if (!status && file_eq(strip(filename,'B','"'),p_buf_name)) {
      //_cbsave_filewatch();
#if 1
      call_list('_cbsave_');
      //10:51am 7/3/1997
      //Dan modified for auto-tagging
      if (def_autotag_flags2&AUTOTAG_ON_SAVE) {
         //messageNwait(nls('got here'));
         TagFileOnSave();
      }
#endif
   }

 _str pa = '\\home\vvijay\stage_area\' :+ strip_filename(filename,'DN');
   if (!path_exists(pa)) {
      int result = make_path(pa);
      if (result) {
         _message_box("slick backup make path failed : " :+ result :+ pa);
         return status;
      }
   }

   int result2 = copy_file(filename, pa :+ strip_filename(filename,'P'));

   if (result2 != 0) {
      _message_box("slick backup main copy failed " :+ (_str)(result2));
   }

   return(status);

}


vineetvijay12

  • Community Member
  • Posts: 29
  • Hero Points: 0
Re: customizing save command
« Reply #6 on: July 16, 2014, 02:29:01 PM »
almost near to get it.
I am also doing the changes for windows path
can you please help me getting path like this.

m:\vvijay_test_protocols_test_lbx_wtl \vv-test\makedoc to copy to \\home\vvijay\stage_area\vvijay_test_protocols_test_lbx_wtl\vv-test\makedoc

i want to get value vvijay from some windows variable $USER or get the value "vvijay" from vvijay_test_protocols_test_lbx_wtl assigened to a variable $USER and appned in in \home\$USER\stage_area\vvijay_test_protocols_test_lbx_wtl\vv-test\makedoc

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: customizing save command
« Reply #7 on: July 16, 2014, 10:40:07 PM »
Try e.g. 'get_env("User")' to query this env.var. Probably the easiest way for now. Alt. 'parse' the given original file name to extract the ,user' relying on your fixed naming rule. IHMO it seems a bit less robust than querying the user env.var.
Good luck, HS2

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: customizing save command
« Reply #8 on: July 16, 2014, 11:45:21 PM »
I'd propose to use SE callback mechanism for '_cbsave_' and to avoid patching product macro sources.
Add this e.g. to your 'vusrmacs.e' (see this post for some further details regarding 'vusrmacs.e'):
Code: [Select]
void _cbsave_vvijay(...)
{
   src := strip_filename(p_buf_name, 'DN');
   dst := get_env("HOME") :+ "\\stage_area" :+ src;
   // say("dst path:" dst );
   if ( !path_exists(dst) )
   {
      int err = make_path(dst);
      if (err)
      {
         _message_box("make_path '" dst "' failed - rc =" err);
         return;
      }
   }

   dst :+= strip_filename(p_buf_name, 'DP');
   // say("dst file:" dst );
   err := copy_file(p_buf_name, dst);
   if ( err )
   {
      _message_box("copy_file '" dst "' failed - rc = " err);
   }
}

I guess you're using a Unix probably Linux system. So the 'HOME' env.var should be fine, right ?
(Hint: It's a good idea to tell SE version incl. hotfix pack and OS platform when asking for assistance/support.)

However, it's an example (thanks Graeme) and as a starter.
You should add a filter for backing up the files you really want to...

Good luck, HS2
« Last Edit: July 17, 2014, 08:01:01 AM by hs2 »

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: customizing save command
« Reply #9 on: July 17, 2014, 12:25:06 AM »
I'd propose to use SE callback mechanism for '_cbsave_' and to avoid patching product macro sources.

Actually the save_file function is sanctioned as a "hook function" that can be replaced by a user
Code: [Select]
/**
 * Writes current buffer to filename.  This function is a hook function
 * that the user may replace.  Options allowed by <b>_save_file</b>
 * built-in may be specified.

but of course, the _cbsave callback is fine too.


vineetvijay12

  • Community Member
  • Posts: 29
  • Hero Points: 0
Re: customizing save command
« Reply #10 on: July 17, 2014, 08:43:38 AM »
I am able to copy file but getting attached error during make_path code, can you please check.
 below is my code.
_str save_file(_str filename,_str options)
{
#if 0
   int renumber_flags=numbering_options();
   if (renumber_flags&VSRENUMBER_AUTO) {
      if (renumber_flags&VSRENUMBER_COBOL) {
         renumber_lines(1,6,'0',false,true);
      }
      if (renumber_flags&VSRENUMBER_STD) {
         renumber_lines(73,80,'0',false,true);
      }
   }
#endif
   typeless status=_save_file(options " "filename);
   if (!status && file_eq(strip(filename,'B','"'),p_buf_name)) {
   //_cbsave_filewatch();
   #if 1
      call_list('_cbsave_');
      //10:51am 7/3/1997
      //Dan modified for auto-tagging
      if (def_autotag_flags2&AUTOTAG_ON_SAVE) {
         //messageNwait(nls('got here'));
         TagFileOnSave();
      }
     #endif
   }

   src := strip_filename(p_buf_name, 'DN');
   dst := \\home-1 :+ get_env("USERNAME") :+ "\stage_area" :+ src;
    //say("dst path:" dst );
   if ( !path_exists(dst) )
   {
      int err = make_path(dst);
      if (err)
      {
         _message_box("make_path '" dst "' failed - rc =" err);
         return;
      }
   }

   dst :+= strip_filename(p_buf_name, 'DP');
   // say("dst file:" dst );
   err := copy_file(p_buf_name, dst);
   if ( err )
   {
      _message_box("copy_file '" dst "' failed - rc = " err);
   }

   }
   return(status);

}

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: customizing save command
« Reply #11 on: July 17, 2014, 08:56:25 AM »
1. Which OS platform / SE version ?
2. What's the intention of this ? This doesn't seem right.
Code: [Select]
\\home-1
Try
Code: [Select]
'\\home\' :+ get_env("USERNAME")
HS2

« Last Edit: July 17, 2014, 09:21:55 AM by hs2 »

vineetvijay12

  • Community Member
  • Posts: 29
  • Hero Points: 0
Re: customizing save command
« Reply #12 on: July 17, 2014, 09:02:42 AM »
SE 2007 and 2010.
OS is windows 7

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: customizing save command
« Reply #13 on: July 17, 2014, 09:18:59 AM »
Yes - I've also seen the attachment file path right now ;)
Again beware that if you're modifying an installed ie. product macro it might get overwritten e.g. when installing an SE upgrade or hotfix pack containing a fixed version of 'saveload.e'. You should either follow the advise Graeme posted before
Quote
Copy the code below to a file of your own called mysave.e or something  - in your slick configuration folder
or this this posting to use the default 'vusrmacs.e' user macro module.

HS2

vineetvijay12

  • Community Member
  • Posts: 29
  • Hero Points: 0
Re: customizing save command
« Reply #14 on: July 17, 2014, 09:34:55 AM »
i chnaged my saveload.e to what came with installation and added new file but same error
#pragma option(strict,on)
#include 'slick.sh'

void _cbsave_vvijay(...)
{
   src := strip_filename(p_buf_name, 'DN');
   dst := '\\home-1\' :+ get_env("USERNAME") :+ "\stage_area" :+ src;
   // say("dst path:" dst );
   if ( !path_exists(dst) )
   {
      int err = make_path(dst);
      if (err)
      {
         _message_box("make_path '" dst "' failed - rc =" err);
         return;
      }
   }

   dst :+= strip_filename(p_buf_name, 'DP');
   // say("dst file:" dst );
   err := copy_file(p_buf_name, dst);
   if ( err )
   {
      _message_box("copy_file '" dst "' failed - rc = " err);
   }
}