Page 1 of 1

AB autocompletion: % shows environment variables

Posted: 03 Apr 2017 00:03
by yusef88
thanks

Re: AB autocompletion: % shows environment variables

Posted: 03 Apr 2017 13:15
by RalphM
While I'm sure this "wish" (I guess) makes perfect sense to you, it might help to explain it with a few more words to the rest of us!

Re: AB autocompletion: % shows environment variables

Posted: 03 Apr 2017 13:52
by yusef88
of course :lol:
typing % in AB would suggest available windows variables

Re: AB autocompletion: % shows environment variables

Posted: 03 Apr 2017 20:11
by Filehero
Good idea!

Re: AB autocompletion: % shows environment variables

Posted: 03 Apr 2017 22:51
by PeterH
You could execute !set in the AB.

You could write a script named % that executes

Code: Select all

  $vars = runret 'cmd /c set';
and then handle the output as you want, then display it.
To execute: enter %; in AB.

In anther script you can build a list of lines like

Code: Select all

   $txt = "ALLUSERSPROFILE = %ALLUSERSPROFILE%"
   ."<crlf>APPDATA         = %APPDATA";
and display this. Call the script as shown before.

Advantage of 2nd and 3rd version: you can format the output as you like.

Edit: hope there's no typo in :ninja:

Re: AB autocompletion: % shows environment variables

Posted: 04 Apr 2017 09:11
by bdeshi
Realized PeterH's scriptable suggestion.
The solution uses a script and an alias in tandem.

THE SCRIPT

Code: Select all

/*ab-envvars-dropdown.xys
0. configure $showvalue & $menuwidth below, and save script as <xyscripts>\ab-envvars-dropdown.xys
1. set an alias with this (paste this line in the AB):
@%=::load "<xyscripts>\ab-envvars-dropdown.xys";
2. now ENTER @% in the addressbar and you will get a menu of %variables% under the addressbar
3. pick any item from the menu and it will be placed in the addresbar.
*/
  /****** CONFIGURATION ******/
  $showvalue = 0;
  // $showvalue
  //    0: show only variable names in the menu
  //    1: show value snippets beside variable names
  $menuwidth = 50;
  // $menuwidth
  //    max width of menu if $showvalue==1
  /** END OF  CONFIGURATION **/

  $out   = runret("%ComSpec% /k set", <xydrive>);                                        // get env.vars from output of SET cmd
  $out   = gettoken($out, -2, <crlf>,, 1);                                               // remove extra padding lines at end
  // construct the popupmenu
  $menu  = '';
  if ($showvalue) {                                                                      // include values
    foreach ($line, $out, <crlf>) {                                                      // loop through each %var%=value line
      $var  = gettoken($line, 1, '=');                                                   // get var's name
      $val  = gettoken($line, 2, '=');                                                   // get var's value
      $line = '%' . $var . '%' . <tab> . $val;                                           // build menu item as %varname%<tab>value
      $line = substr($line,, $menuwidth) . ((strlen($line) > $menuwidth) ? ' ...' : ''); // shorten line to max width and add ellipses
      $menu = $menu . <crlf> . $line;
    }
  }
  else {                                                                                 // else skip values
    foreach ($line, $out, <crlf>) {                                                      // loop through each %var%=value line
      $menu = $menu . <crlf> . '%' . gettoken($line, 1, '=') . '%';                      // get var name and built menu item as %varname%
    }
  }
  $menu  = trim($menu, <crlf>, 'L');                                                     // trim extra delimiter line
  $abpos = controlposition('A');                                                         // get addressbar position for popumenu placement
  $x     = gettoken($abpos, 1, '|');                                                     // AB X
  $y     = gettoken($abpos, 2, '|') + gettoken($abpos, 4, '|');                          // AB Y + AB Height
  $pick  = popupmenu($menu, $x, $y,,,, <crlf>, '');                                      // show popupmenu (sep_item='' to show ; in values)
  $pick  = gettoken(regexmatches($pick, '%[^%]+%'), 1, '|');                             // get only the variable name part from picked menu item
  if ($pick) {
    focus 'A';
    sendkeys '^a' . sendkeyesc($pick);                                                   // insert in AB (^a select all replaces orig alias str)
  }
  FUNCTION sendkeyesc($s){return regexreplace($s,'([\{\}\^\~\%\+])','{$1}')}

ab-envvars-dropdown.xys
THE ALIAS
Paste this into the addressbar and press ENTER. Adjust the script path if necessary.

Code: Select all

@%=::load "<xyscripts>\ab-envvars-dropdown.xys";
Now type @% in the addressbar and you will get a menu of available %variables%. Pick any item from the menu and it's placed in the addressbar.

The script has a setting $showvalue to control whether variable values are displayed in the menu. Personally, I like it better with just the variable names (the menu size is much saner), so this setting is off by default.

Although the menu's supposed to be positioned under the addressbar, it's almost never going to be the case unless you have a large display or a low count of variables.

Re: AB autocompletion: % shows environment variables

Posted: 09 Oct 2017 16:52
by admin
I hope everybody is happy with Sammay's script. (I don't plan to add this kind of autocompletion -- it conflicts with the already existing autocompletion.)

Re: AB autocompletion: % shows environment variables

Posted: 09 Oct 2017 19:53
by bdeshi
[don't mind this post, but for what it's worth, I've updated that script above Don's post. There were some violet-hot glaring mistakes.]

Re: AB autocompletion: % shows environment variables

Posted: 14 Nov 2018 22:13
by Dustydog
Sammy - this is a clean, simple, elegant, useful script - along with the optional alias. Thanks for your work! I was using something similar, but I wasn't getting the values from SET (was from a utility), and I wasn't using such a convenient way to call it, nor using a direct insertion in the address bar. Your level of clear commenting is a good example (especially for some of my large regexes, which look more arcane every time I look back at them.), though most suited for sharing something, of course.

I think it might be useful to add a few xy-specific location variables to it at the bottom, though it would be a little less elegant. I do think it would be convenient. I may do it for my own copy if you're not interested. What do you think?

I'm really not sure what Don meant about conflicting(?) with current autocompletion? There seems to be no downside to me...?

Re: AB autocompletion: % shows environment variables

Posted: 02 Dec 2018 06:58
by bdeshi
thanks! and sure, go ahead. (sorry for late reply.)