Author Topic: Edit Like  (Read 2411 times)

MindprisM

  • Senior Community Member
  • Posts: 127
  • Hero Points: 8
Edit Like
« on: September 23, 2009, 09:04:12 am »
'Edit Like'

The following macro- Edit Like, or _command el(...) name_info(','VSARG2_CMDLINE|VSARG2_REQUIRES_MDI)

The premise of el is that no one actually uses their working directory, at least no one of any importance (namely me)..
So e filename.ext from the command line will only create/edit files relative
to the working directory and its usefulness is constricted by having to point
your working directory to where your, um, working.

Who does that?

Regardless, what el does is use the current files location and extension to
enable you to quickly edit/create a new file in the same location, optionally defaulting to the same extension.

If you are editing C:\huge-far-away-path\I-make-a-lot-of-files-like-this.txt then
from the command line: el another-file   (notice no extension specified)
will create/edit C:\huge-far-away-path\another-file.txt (notice automatic extension added)

or-
from the command line: el another-file.csv (notice extension specified)
will create/edit C:\huge-far-away-path\another-file.csv (notice automatic extension override)


That is what I call a high-utility-braindead-macro. If anyone has other macros of this sort lets have a roundup.
I think this type of thing is very interesting.



Ok, so the macro described above is an easy write. But in going to write it
I noticed that I wanted it to also have the functionality of _command e() - where, from the command
line you are hot-prompted with filenames from the working directory as you type.

The only way to do this AFAIK is to have a two-stage macro, because we have to change
the working directory to get the hot-prompt correct, then change it back when we are done.

So command line el will not give hot-prompts, but if you do not provide a filename then
the command el_ will be invoked, at which point the working directory has been temporarily
set so hot-prompts work.

What you will see on the command line will be: el_ <newfile[.ext]>
with the second part of that text selected '<newfile[.ext]>' which is intended
to show you where you should type your filename.


Note: At this point, should you decide to back out, simply delete '<newfile[.ext]>' (leaving el_ on the command line),
otherwise the command line will retain its prompting label. Not only that,
your working directory will not be restored to what it was. To correct this, simply run el_ (not el) and
back out properly.


el will also work from execute-selection as expected, and binding it to a key with no parms will invoke
el_ as expected.

Code: [Select]
_str file_path(_str s)
{
   return strip_filename(s,'ne');
};

_str file_ext(_str s)
{
   //strip_names(
   //pos('.',s,p,'');
   if (pos('.',s)==0) {
      return '';
   }
   ss=str_reverse(s);
   p=pos('.',ss,1,'');
   sss=substr(ss,1,p-1);
   sss=str_reverse(sss);
   return sss;
   ss=file_nameext(s);
   parse ss with 1 d +1 n (d)'.'e;
   return e;
   return strip_filename(ss,'n');
};

_str file_nameext(_str s)
{
   return strip_filename(s,'dp');
};

_str str_reverse(_str s='')
{
   //for () {}
   ss='';
   for (x=1;x<length(s)+1;x++) {
      ss=substr(s,x,1)ss;
   }
   return ss;
}

_str _el_working_dir='';
//el x.x
_command void el(...) name_info(','VSARG2_CMDLINE|VSARG2_REQUIRES_MDI){
   if (p_window_id==_cmdline) {
      if (arg(1)=='') {
         d=file_path(p_buf_name);
         fex=file_ext(p_buf_name);
         wd=getcwd();
         _el_working_dir=wd;
         cd(d);
         s2='<newfile[.'fex']>';
         s='el_ ' s2;
         p="Create new *."fex" file in "d"\\ ";
         _cmdline.set_command(s,5,5+length(s2),p);
         command_toggle();
      }else{
         a1=arg(1);
         fexn=file_ext(a1);
         d=file_path(p_buf_name);
         fex=file_ext(p_buf_name);
         if (fexn=='') {
            a1=a1 '.'fex
         }
         e('"'d a1'"');
         //Msg('e:'d a1);
      }
   }else{
      if (arg(1)=='') {
         d=file_path(p_buf_name);
         fex=file_ext(p_buf_name);
         wd=getcwd();
         _el_working_dir=wd;
         cd(d);
         s2='<newfile[.'fex']>';
         s='el_ ' s2;
         p="Create new *."fex" file in "d"\\ ";
         _cmdline.set_command(s,5,5+length(s2),p);
         command_toggle();
      }else{
         a1=arg(1);
         fexn=file_ext(a1);
         d=file_path(p_buf_name);
         fex=file_ext(p_buf_name);
         if (fexn=='') {
            a1=a1 '.'fex
         }
         e('"'d a1'"');
         //Msg('e:'d a1);
      }
   }
}

_command void el_(...) name_info(FILE_MAYBE_LIST_BINARIES_ARG'*,'VSARG2_CMDLINE|VSARG2_REQUIRES_MDI)
{
   _str filenameArg='';
   if (arg(1)=='') {
      _cmdline.set_command('',0,0,'');
      cd(_el_working_dir);
      return;
   }
   d=file_path(p_buf_name);
   fex=file_ext(p_buf_name);
   a1=arg(1);
   //a1=prompt(arg(1),"create new *."fex" file in "d);
   if (a1!='') {
      fexn=file_ext(a1);
      if (fexn=='') {
         a1=a1 '.'fex
      }
      e('"'d a1'"');
      //Msg('e:'d a1);
      //e(maybe_quote_filename(d a1));
   }
   _cmdline.set_command('',0,0,'');
   cd(_el_working_dir);
}
« Last Edit: September 23, 2009, 10:34:35 am by MindprisM »