Author Topic: Set current file as an env var  (Read 4965 times)

moshek

  • Community Member
  • Posts: 13
  • Hero Points: 0
Set current file as an env var
« on: July 29, 2010, 04:57:49 am »
I'm trying to set the current open file to be an env variable. The reason for doing that is because I need to pass that parameter from windows to Linux.
%f gives me H:\branches\mybranch\...\file.txt
And I need to change it to something like: branches/mybranch/.../file.txt (Remove drive letter and use slashes instead of backslashes)

I'm trying to do the following:

#include "slick.sh"
_command vusrmacs() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
      _str pd=absolute (p_buf_name,strip_filename (p_buf_name,'N'));
      pd=strip_filename (pd,'D');

      // bslash -> slash
      pd = stranslate ( pd, "/", "\\", "" );

      // message ( "pd = " pd );
      set_env( "vusrmacs",  pd  );
   }
}

But whenever I use the p_buf_name, load the macro and put it in the project properties SlickEdit crashes with the following error:
This property or method is not allowed on this project
file=vusrmacs.ex offset=34

The Slick-C stack shows:

 This property or method is not allowed on this object
vusrmacs.ex 34 vusrmacs()   p_window_id: 268   p_object: OI_COMMAND_BUTTON   p_name: _ok
project.ex 632 project:_project_run_macro2(C:\SlickEditProjects\kala_dell.vpj)   p_window_id: 268   p_object: OI_COMMAND_BUTTON   p_name: _ok
project.ex 35395 project:doOK()   p_window_id: 268   p_object: OI_COMMAND_BUTTON   p_name: _ok
project.ex 34964 _project_form._ok.\x{15f3}()   p_window_id: 167   p_object: OI_SSTAB   p_name: _proj_prop_sstab
dlgeditv.ex 7420 show(-mdi -modal -xy _project_form,C:\SlickEditProjects\kala_dell.vpj,4,)   p_window_id: 136   p_object: OI_EDITOR   p_name:
project.ex 367 project:project_edit2(,C:\SlickEditProjects\kala_dell.vpj)   p_window_id: 136   p_object: OI_EDITOR   p_name:
project.ex 565 project_edit()   p_window_id: 136   p_object: OI_EDITOR   p_name:

What am I doing wrong?

moshek

  • Community Member
  • Posts: 13
  • Hero Points: 0
Re: Set current file as an env var
« Reply #1 on: July 29, 2010, 07:08:17 am »
Update:
I realized that I need to use:

int temp_view_id=0;
int orig_view_id=_create_temp_view(temp_view_id);
.
.
.
_delete_temp_view(temp_view_id);
activate_window(orig_view_id);

Now I can acess p_buf_name. The problem is that the value of that var is not updated. I get the SlickEdit configuration directory (C:/Documents and Settings/kamerm/My Documents/My SlickEdit Config/15.0.0/) instead of the current file.
I was trying to set 'def_switchbuf_cd' to 1, but it didn't help

What am I missing?

Graeme

  • Senior Community Member
  • Posts: 1978
  • Hero Points: 226
Re: Set current file as an env var
« Reply #2 on: July 29, 2010, 11:37:14 am »
Maybe someone else can understand what you're trying to do but I can't so I'll try asking some questions.

Quote
I need to pass that parameter from windows to Linux
Is this from one machine to another?  Can you describe what you're doing more clearly?

Quote
%f gives me H:\branches\mybranch\...\file.txt
In what context are you using %f  - a project command?

Quote
But whenever I use the p_buf_name
Do you mean that you think that your use of p_buf_name in the macro is giving you the error.  p_buf_name gets you the name of the current edit buffer when that buffer has the focus.  To get the name of the current edit buffer when the edit window doesn't have the focus, you can use _mdi.p_child.p_buf_name.
Quote
I realized that I need to use:

int temp_view_id=0;
int orig_view_id=_create_temp_view(temp_view_id);
What makes you think you need to do that?

Quote
... load the macro and put it in the project properties
Do you mean you put the name of the macro as a pre-build command in the project properties?




moshek

  • Community Member
  • Posts: 13
  • Hero Points: 0
Re: Set current file as an env var
« Reply #3 on: July 29, 2010, 12:10:34 pm »
Let me explain:

I'm using linux version control (bitkeeper). Until now, I had a linux SE and everything was fine.
I configured 'Version control' to run bk edit %f whenever I needed to chekout a file.

Now we switched to Windows SE, and I'm trying to configure version control to run bk commands using Plink.
The files are located under /home/kamerm/branches/mybranch/... and I created a network drive (H:\) for windows to point /home/kamerm/
The checkout command should be something like:
c:\Putty\PLINK.EXE -ssh kamerm@cosma-dev -i c:\Putty\key.ppk "bk edit %f"

But when I do that, the command in linux becomes:   bk edit H:branchesmybranch...file.txt (with drive latter, without slashes at all)
So I need to remove the drive letter and replace backslashes with slashes.

In order to do so, I tried to use a macro - to take the p_buf_name, do dome manipulations, and set an env var which I can use instead of %f.
I read in the forum that p_buf_name should sometimes be used after settings a different window (without using _create_temp_view SE simply crushed).
So I built the following macro:

#include "slick.sh"
_command vusrmacs() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   int temp_view_id=0;
   int orig_view_id=_create_temp_view(temp_view_id);
   _str name = absolute (p_buf_name);
   name = substr(name,4);  // Remove H:\
   _delete_temp_view(temp_view_id);
   activate_window(orig_view_id);
   name = stranslate ( name, "/", "\\", "" );   // bslash -> slash
   set_env( "ff", name);
}


I saved the macro, loaded it (macro->load module...), and added "vusrmacs" in Project properties->Open tab.
But it look like the value of the ff env variable is set only when I change the project properties.
I tried using _mdi.p_child.p_buf_name but it didn't help
I want to get the file name / path which has the focus (In order to checkout it), but the p_buf_name isn't being updated when I switch from one file to another.



shadowbane

  • Community Member
  • Posts: 85
  • Hero Points: 4
Re: Set current file as an env var
« Reply #4 on: July 29, 2010, 03:51:34 pm »
A couple of comments here.

1) Be aware that the project properties -> open tab items will only run once, so if the buffer changes, your macro will not reexecute and create a new path.  Since it doesn't appear that your using the file name in the env variable, perhaps this is okay for you.

2) I believe you were crashing because the current window when opening a project is not an editor control, therefore there is no buffer, so p_buf_name is invalid to use.  I would guess that none of the files of the project are loaded while loading the project, so I don't think using p_buf_name can possibly work.  using the temp view like you are is keeping the crash at bay because it is changing p_buf_id to a real buffer, which makes p_buf_name valid, but it doesn't contain what you want.

3) I would try using
Code: [Select]
absolute(strip(_ProjectGet_WorkingDir (_ProjectHandle (_project_name))))or try using
Code: [Select]
absolute(strip(_workspace_filename))to get to a path that should be more reliable. One gets the working directory configured for the current project, the other gets the path to the workpace file.  I'm not sure which would be more useful to you.

-shad




moshek

  • Community Member
  • Posts: 13
  • Hero Points: 0
Re: Set current file as an env var
« Reply #5 on: July 29, 2010, 04:43:20 pm »
Quote
Be aware that the project properties -> open tab items will only run once, so if the buffer changes, your macro will not reexecute and create a new path.  Since it doesn't appear that your using the file name in the env variable, perhaps this is okay for you

What do I need to do in order to execute the macro whenever I switch to another file? or (even better) whenever I run a version control command?

Quote
believe you were crashing because the current window when opening a project is not an editor control
Actually I need to execute the macro ONLY when the current window is an editor (focus on a file)

Quote
One gets the working directory configured for the current project, the other gets the path to the workpace file.  I'm not sure which would be more useful to you
None of them is good for me. I need the directory of the current open file...

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Set current file as an env var
« Reply #6 on: July 29, 2010, 05:47:11 pm »
When your macro runs while opening a project, there isn't any current open file yet, so it's impossible.

To make a macro function that gets called whenever a file becomes the current file, prefix its name with "_switchbuf_".

For example, I tested the following and it worked for me:

Make a file named something like "LinuxHelpers.e", put the following snippet in it, and then load the module (Menu -> Macro -> Load Module).  I don't recommend using "vusrmacs" as the environment variable name, it's a bit generic, so below I've instead used "lnxfile" as the name.

Code: [Select]
#include "slick.sh"
void _switchbuf_SetEnvVar()
{
   _str pd=absolute(p_buf_name, strip_filename(p_buf_name, 'N'));
   pd=strip_filename(pd, 'D');

   // bslash -> slash
   pd = stranslate(pd, "/", "\\", "");

   // strip leading slash
   if (substr(pd, 1, 1) == "/")
      pd = substr(pd, 2);

   //message("pd=" pd);
   set_env("lnxfile", pd);
}
« Last Edit: July 29, 2010, 05:48:45 pm by chrisant »

shadowbane

  • Community Member
  • Posts: 85
  • Hero Points: 4
Re: Set current file as an env var
« Reply #7 on: July 29, 2010, 06:22:12 pm »
Another few alternatives you might like:

1) You could consider wrapping your command line in a small shell script that fixes the path, and just continue to use %f.
2) you could patch vcs.e so that it allows slickedit internal commands to be used for vcs commands (just tested and it doesn't presently), then it would be a simple effort to write your own command that fixes the %f and calls your ssh line.

Don't know if either of these help, but...

-shad

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Set current file as an env var
« Reply #8 on: July 30, 2010, 12:07:11 am »
Yeah actually I was very surprised that you can't setup version control to run macros for the various commands.  It would be a welcome enhancement.

moshek

  • Community Member
  • Posts: 13
  • Hero Points: 0
Re: Set current file as an env var
« Reply #9 on: July 30, 2010, 06:16:09 am »
Problem solved.
The trick was to use _switchbuf in the macro name, and replace the keyword "_command" with "void" in the method declaration

Thanks everyone !

Graeme

  • Senior Community Member
  • Posts: 1978
  • Hero Points: 226
Re: Set current file as an env var
« Reply #10 on: July 30, 2010, 07:07:10 am »
Not sure about this but you might need to check that your _switchbuf function gets called when you open a new file.  If it doesn't, I think there's a _buffer_add prefix you can use to get notified of a new buffer being opened.