Change Current Working Directory to Current File Directory

Friday, September 26, 2008 1 comments

The Current Working Directory vs. The Current File Directory

The macros discussed in this article are not as important as the discussion around them.

At any given time, Slickedit has (1) a current working directory and (2) a location of the file being edited. The current working directory is the directory that is displayed when you launch the Open, Save, Save as, Insert File, Export to HTML and similar dialog windows. In MS Windows, these dialogs are generally called the "common open dialog" or "common file dialog." Note: this discussion is based on Slickedit under MS Windows. Results might vary on other platforms.

Purpose of switchbuf_cd

The switchbuf_cd macro changes Slickedit's current working directory to the same directory as the file you're currently editing. One reason I find this useful is because I usually want to save my files in the same directory as the original file and I often want to open additional files in the same directory as well.

For example, if I'm editing util.e which resides in "c:\program files\Slickedit 2008\macros", Slickedit itself might be pointing to a different directory, such as "g:\documents\tmp". When I open, let's say, the Save as dialog, the directory shown will be g:\documents\tmp. But I don't want to save my file there. I want to save my file in the Slickedit macros directory (c:\program files\Slickedit 2008\macros). And I don't want to do a lot of typing or fumbling around to get there.

Directory Aliases and Change dir Checkbox

Sure, I can create a directory alias to help with the typing, but I'm often too impatient for that. I want instant gratification! Another option is to (1) open the common file dialog to perform an Open, Save, Save As, etc., (2) put a check in the check box next to Change dir (in the lower left corner of the dialog) and diddle around in the gui file tree until I get to the folder I want and, finally, (3) double click on that folder in order to make it my current working directory.

If the Change dir box is checked, the next time I open the common file dialog it should open in the folder I just picked. It's easy to toggle this functionality by checking or unchecking the Change dir box as needed. If I uncheck the box, the file dialog will not change the working directory even if I change it in the dialog.

The def_change_dir Configuration Variable

Slickedit comes with a Configuration Variable, def_change_dir, to set the default state of the Change dir check box. Toggling the value of this variable will toggle the state of the check box in the common dialog.

The gui_cd Command

Yet another option for changing the editor's current directory is to use gui_cd, which prompts you for the directory to which you want to change. According to the documentation: this command supports directory aliases and may change the current directory in the build window.

The cd Command

From the docs: Changes the current working directory to the drive and path if given. A current directory message is displayed. By default, this command supports specifying directory aliases for driveNpath and will change directory in the build window. Use the Change Directory dialog box ("File", "Change Directory...") to change these defaults and press the save settings button.

The cdd Command

This command changes the current working directory to the drive and path if given. A current directory message is displayed. This command will also change directory in the build window if a path is given.

The chdir Command

Changes to drive and directory specified. If a path is given the current working drive is changed to drive specified. For some reason I can't get this to work.

Drawbacks of existing methods

With the exception of switchbuf_cd, all these methods share a common drawback. If the fully qualified directory of the current file is not readily apparent, you'll have to spend time identifying it, copying it and pasting it (or copying it from memory) before you can switch to it. This can happen when the file's path is too long to see on Slickedit's title bar or when you can see the path in a tool window like the file/buffer list (list_buffers command) but you can't easily copy and paste it from there. This drawback doesn't exist when you use switchbuf_cd.

If you assign a key to the switchbuf_cd macro, it will force Slickedit to make it's current working directory the same as your current file's directory, regardless of the length of the path and of whether you actually know the precise location of the file. This saves time. Be aware, however, that if you later use switchbuf_cd while you're in a different open file located in a different directory, you'll change the editor's current working directory again, to match the current directory of the now-current file.

Copy_path_filename_to_clipboard Macro

Here's a macro worth seeing before considering switchbuf_cd. This macro copies the full path of the current file to the clipboard, making it easy to paste the file's location wherever you want to. You should find the macro in the Slickedit Macro Programming Community Forum at http://community.slickedit.com/index.php?topic=190.0. I've modified the macro slightly to display the path in the message line while it copies the path to the clipboard. If the current file has not yet been saved, the macro will return "Path not found".

// copy_fileid_to_clipboard: Quickly copy the path and filename to your clipboard

// Posted by Mike, Slickedit Team Member
on: July 27, 2006, 07:04:19 PM
_command void copy_fileid_to_clipboard() name_info(',')

{
_str buf_name=p_buf_name;
int temp_view_id;
int orig_view_id=_create_temp_view(temp_view_id);
// say('copy_filename_to_clipboard temp_view_id='temp_view_id);
p_view_id=temp_view_id;
fileid=buf_name;
_insert_text(buf_name);
copy_to_clipboard();
p_view_id=orig_view_id;
_delete_temp_view(temp_view_id);
message('Filename: ' fileid);

}
// switchbuf_cd: change current working directory to current file directory 


// change current directory to that of the current buffer

_command switchbuf_cd()
{
path=strip_filename(p_buf_name,'n');
status=chdir(path,1);
if (!status) {
message('Current working is 'path);
} else message get_message(status);
}





Transpose Lines

0 comments

Another simple macro. If you're sometimes lazy like me, you might find this command saves some time and saves some time thinking.

Assign a key combination to the macro. Whenever you want to swap a line of text with the line above it (or vice versa), simply place the cursor on the bottom line of the two lines. Press your key combo. The line the cursor is on is moved up one line, leaving the line that was above it, below it.

// Exchange current line with the line above
_command void transpose_lines() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
if ( _on_line0() ) {
return;
}
markid=_alloc_selection();
if (markid<0) {
// This should not happen.
return;
}
_select_line(markid);
up();
special_case=0;
if (_on_line0()) {
status=down(2);
if (status) return;
special_case=1;
} else {
up();
}
_move_to_cursor(markid);
if (special_case) {
down;
} else {
down(2);
}
_free_selection(markid);
}

Showkey

Wednesday, September 24, 2008 0 comments

I've used one version or another of the following macro, Showkey, in the macro languages of most of the different editors I've used over the years. Prior to Slickedit my editor was Kedit for a long time. My love of Kedit stemmed from my enjoyment of using Xedit on the IBM mainframe VM/CMS system.

If you asked me now, "What's the usefulness of this Showkey macro?" I don't know that I could remember. I remember that it came in very handy at times. Execute showkey and it will report the exact key or key combination you press. To stop showing your keypresses, press the ESC key.

One example of a use that I had for the showkey macro was to test keystrokes and key combos to make sure the computer understood the keyboard input the same way I thought I was issuing it. For instance, I've had several keyboards that, for some reason, didn't "get" the Ctrl keypress from the Ctrl key on the right side of my keyboard when used in certain key combinations. The Ctrl key on the left side of the keyboard worked fine.

By trying key combinations with the right Ctrl key while showkey was active, I could see that the computer didn't register the right Ctrl key when pressed in conjunction with certain other keys.

Showkey doesn't work on the Print Screen, Pause/Break, or modifier keys (Shift, Ctrl, Alt, or Windows keys) when they are pressed alone. I hope you find a good way to use this macro one day.

// show which key was pressed
_command showkey()
{
message('Reading keystrokes to message line; press ESC to stop');
for (;;) {
binary_key=get_event();
key_name=event2name(binary_key);
if (key_name == 'ESC') {
message('Keypress ' key_name'; reading keystrokes ended');
break;
} else {
message('Keypress: 'key_name);
//return(key_name);
}
}
}
}

Count occurrences matching search_string

, , , , , Sunday, September 21, 2008 0 comments

I'm sure this command could be improved, but it has served my needs just fine
for years. If you find problems or can suggest improvements, please post them.
Remember I'm not a professional programmer, so please take that into consideration.

dh

// Searches current buffer for search_string specified on the command line
// and returns a
total count of occurrences and a count of the number of lines
// containing the occurrences. Will accept all command line arguments that
// the "search" command accepts, including regular expressions
// in search_string. Can be used on visible lines in a selective display.
// Case insensitive search by default.

_command int cou,count(...) name_info(','VSARG2_REQUIRES_EDITORCTL)
{
int strcount;
int linecount;
_str search_string=arg(1);
parse search_string with delim +1 sstring(delim)opts

save_pos(p);
top(); up();

status=search(sstring, '@i>':+opts);
if (!status) {
strcount=1;
curline=p_line;
linecount=1;
} else {
restore_pos(p);
message get_message(status);
stop;
}

for (;;) {
status=search(sstring, '@i>':+opts);
if (status !=0) {
break
} else {
++strcount;

if (!(curline==p_line)) {
++linecount;
curline=p_line;
}

}
}
restore_pos(p);
message(strcount' occurrence(s) in ' linecount ' lines');
return(strcount);
}

Delall: Delete Visible Lines from a Selective Display

Thursday, September 18, 2008 0 comments

Here are 3 more Slickedit macros I've been using regularly for several years. Actually, the first one is more of a test macro than it is useful. The other two are stalwarts.

The 3 macros are:

  1. count_hidden_lines(): show count of hidden lines
  2. dat(): delete all visible lines in the selective display, without deleting any hidden lines
  3. delall(): delete all lines containing the specified search string
Have fun. Let me know if you find any significant problems or have comments.

dh
// count_hidden_lines

_command count_hidden_lines()
{
NoflinesShown0=((p_Noflines)-p_Nofhidden);
message( NoflinesShown0' visible (selected) lines, p_Noflines= 'p_Noflines'; p_Nofhidden = 'p_Nofhidden);
}


// dat(): delete all visible lines in selective display
// Example: to remove all lines containing "Bookmark this site",
// execute 'all /bookmark this site/' and then execute 'dat.'
// The 'dat' command will delete all of the visible lines and leave the
// rest untouched.

_command dat() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
// get line counts for verification and be sure a selective display is active
NoflinesShown0=((p_Noflines)-p_Nofhidden);
if (NoflinesShown0==p_Noflines) {
_message_box("Error: be sure selective display is active before running ''dat''");
return(0);
}
old_line_cou=p_Noflines;
// make sure nothing is marked--we don't want to delete invisible lines
if (select_active()) {
_message_box('Error: Deselect the marked block');
return(0);
} else {
bottom();
while (p_line>=1) {
hid=_lineflags() & (HIDDEN_LF);
if (hid!=HIDDEN_LF) {
// probably also need to exclude or prompt for

// deletion of pbm "minus" lines. A pbm minus
// line is a hidden line that has been expanded
status=_delete_line();
} else {
cursor_up();
}
}
}
NoflinesShown1=((p_Noflines)-p_Nofhidden);
message(old_line_cou'-'old_line_cou-p_Noflines' lines deleted; 'p_Noflines' remain');
if (p_Noflines!=(old_line_cou-NoflinesShown0)) {
_message_box('Error: Number of lines deleted does not match original number of visible lines');
}
show_all();
}


// delall(): Delete all lines containing the specified search string.
//
Locates lines containing matches and then runs 'dat' to delete
// all matching lines in one step.

// Example: "delall /^Subtotal$/r"
// Delall is a timesaver ... and easily reversible, thanks to Undo.
// You can use any arguments
to 'delall' that you can use with the
// ordinary 'search', 'find', 'all', 'more', or 'less'
commands,
// including those luscious regular expressions we love so much.


_command d,delall(...) name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_READ_ONLY)
{
_str searchstr=arg(1);
_str delim;
_str sstring;
_str opts;
// strip delimiters
parse searchstr with delim +1 sstring(delim)opts
save_pos(p);
all(sstring,'i':+opts);
dat();
show_all();
restore_pos(p);
}

Show Longest Line and Lines > or < Specified Column Number

Tuesday, September 9, 2008 1 comments

I guess you could say I write alot of "utility" macros. Short and sweet, but they get the job done. Here are three that come in handy once in a rare while.

// Find longest line in buffer; display line number and length
// It would be good if the macro took you to the longest line.
// I tried a couple of things that didn't work.
// Maybe you can provide the missing code.
_command longest_line() name_info(','READ_ONLY_ARG2)
{
save_pos(p);
top();up();max=0;
while (!down()) {
get_line(line);
len=length(line);
if (len > max) {
max=len;
max_linenum=p_line;
}
}
message('Longest line is ' max_linenum ' with ' max ' bytes');
restore_pos(p);
}

// The following two macros could be modified to display 'greater
// than or equal to" or "less than or equal to" the specified
// column number, if that is more to your liking.

// Show only lines exceeding the column number you specify.
// This command currently provides a selective display, but doesn't
// show the usual "+" and "-" marks that you expect in a
// selective display, to enable you to
expand and compress the lines.
// Usage: show_lines_over 128. This will display only lines longer than 128 columns
_command show_lines_over(...) name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_READ_ONLY)
{
typeless maxlength=arg(1);
if (arg(1)=='' arg(1)<=0) {
message('Error: a column number argument is required');
stop;
}
top();up();
for (;;) {
status=down();
get_line(line);
if (length(line)<=maxlength) {
_lineflags(HIDDEN_LF,HIDDEN_LF);
}
if (status) {
break;
}
}
}

// show lines shorter than specified column length; hide lines longer than that length
_command show_lines_under(...) name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_READ_ONLY)
{
typeless minlength=arg(1);
if (arg(1)=='' arg(1)<=0) {
message('Error: a column number argument is required');
stop;
}
top();up();
for (;;) {
status=down();
get_line(line);
if (length(line)>minlength) {
_lineflags(HIDDEN_LF,HIDDEN_LF);
}
if (status) {
break;
}
}
}
dh

GlossyBlue Blogger by Black Quanta. Theme & Icons by N.Design Studio
Entries RSS Comments RSS