Author Topic: Here's a macro to strip trailing whitespace from modified lines on save  (Read 8945 times)

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Here's a macro to strip whitespace from modified lines on save.  I don't want to strip whitespace from all lines, because that introduces unnecessary diff churn in the source control system since many other users don't have editors that strip whitespace.  So I figure a nice low impact compromise is to only strip whitespace from lines that I've actually modified (or added).

It would be handy if the stock save_file had a _cbbeforesave_ type of hook so that multiple macros could inject pre-save logic, instead of only one being able to replace the save.

Code: [Select]
#include "slick.sh"
#pragma option(strict,on)
_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
   // chrisant: Strip trailing whitespace only from modified/inserted lines,
   // except during auto-saves.
   _str as_dir=def_as_directory;
   if (as_dir=='') {
      as_dir=_config_path():+'autosave':+FILESEP;
   }
   if (pos(as_dir,filename,1,'I')!=1) {
      typeless p;
      _save_pos2(p);
      top();
      do {
         if (_lineflags()&(MODIFY_LF|INSERTED_LINE_LF)) {
            _str line,stripped;
            get_line(line);
            stripped=strip(line,'T');
            if (line:!=stripped) {
               replace_line(stripped);
            }
         }
      } while (down()==0);
      _restore_pos2(p);
   }
   typeless status=_save_file(options " "filename);
   if (!status && file_eq(strip(filename,'B','"'),p_buf_name)) {
      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();
      }
   }
   return(status);
}

Graeme

  • Senior Community Member
  • Posts: 2432
  • Hero Points: 322
Quote
It would be handy if the stock save_file had a _cbbeforesave_ type of hook so that multiple macros could inject pre-save logic, instead of only one being able to replace the save.

You could add the callback to your own version of save

int proceed_with_save;
proceed_with_save = 0;
call_list('_cbbeforesave_', &proceed_with_save)
if (proceed_with_save != 0)
    return;
...

All functions that start with _cbbeforesave_ get called by the call_list.  Dunno if you want 'proceed_with_save' - I just added that for illustration.

Graeme

hs2

  • Senior Community Member
  • Posts: 2744
  • Hero Points: 288
I'm also using this mechanism to strip trailing blank lines :)
Quote
   call_list('_presavefile_',filename,options);
HS2

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Yep, I do like that we can add our own hooks, but I meant it would be nice for an "out of the box" installation of SlickEdit to include a before-save hook.  That way for example people can share multiple macros that all inject before-save actions.  As opposed to how today it is guaranteed that one or more of the macros will need manual editing first.

hs2

  • Senior Community Member
  • Posts: 2744
  • Hero Points: 288
BTW I think stripping SPACEs with macro code takes longer than letting SE doing that.
I prefer diff tools wich alllow to 'ignore' SPACES (leading/trailing/all).
HS2

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Yes, letting SE strip whitespace from all lines would be faster.  But it would cause extra churn, and in fact when working in large teams it introduces gratuitous merge conflicts (in my experience as a developer of a large scale source control system, stripping whitespace from otherwise-unmodified lines causes around a 4-12x increase in the rate of complex merge conflicts).  I work on a team of several hundred developers, though, and the issues only start to emerge at large scale -- for most users it would just come down to a matter of personal preference.   :)

hs2

  • Senior Community Member
  • Posts: 2744
  • Hero Points: 288
Ok, I understand. This scale requires some much more strict dev. rules... HS2