GETL: Variation on GET Displays Number of Lines "Gotten"

Tuesday, November 25, 2008 0 comments

// Here's another of my rinky-dink macros:
// GETL: same as 'get' but displays the number of lines imported into the
// current buffer. I missed having this functionality when I switched over from
// Mansfield's Kedit. It acts as a visual confirmation of the number of
// lines imported and tells you how much bigger you're making the current
// (i.e., destination) file.

_command getl(...) name_info(FILE_ARG'*,'VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
filename=arg(1)
old_noflines=p_noflines;
get(filename);
new_noflines=p_noflines;
noflines_added=(new_noflines - old_noflines)
message(noflines_added' lines added');
}

// direct complaints, suggestions or improvements to me

Fun with Line Flags!

, , , , , , , , Sunday, October 26, 2008 0 comments

Lineflags, as defined below and in the macros that follow, are an important
element driving the attributes, behavior and display of specific types of lines
of text in a Slickedit editing session. Lineflags are used in such modules as
mouse.e, seldisp.e, util.e, builtins.e, surround.e, markfilt.e, guireplace.e,
javadoc.e and many more.

Some of the more common examples of commands that use lineflags include 'all',
'less', 'hide_all_comments', 'plusminus', 'show_braces', and 'show_paragraphs.'
A few of the uses of lineflags include the setting of levels of nested comments
and selectively displayed lines, the setting of 'no save' lines in Diffzilla,
the 'plus' or 'minus' bitmaps shown in the left margins of selective displays,
and even in the color or box appearance of the current line.

Additionally, lineflags account for the differently colored labels used to
identify 'inserted' and 'modified' lines in the lefthand margins or linenumbers
area of speacific lines in a file. Some types of lineflags set the visible
appearance of lines and some are used unseen in the background.

The basic function for manipulating lineflags, taken from Slickedit Help, is
'_lineflags', as follows. (For added flavor, I've included the bit values in
hex and decimal that are set by a few of the lineflags. I gathered these bits
from a source file in which the constants were set, in an earlier version of
Slickedit. I haven't been able to find them in the current Slickedit 2008 and
recent versions.)

Following this Help information, I present several of my 'Fun With Lineflags'
macros.)

int _lineflags(int newflags=0, int mask=newflags)

Gets or sets the line status flags for the current line. If the flags argument
is given, the line status flags for the current line are modified. mask
defaults to the same value as flags if it is not specified. The mask indicates
which bits will be set according to flags.

MLCOMMENTINDEX_LF
Indicates which multi-line comment. Only two are allowed. Must know which
multi-line comment we are in so we know what will terminate it.

MLCOMMENTLEVEL_LF
Indicates multi-line comment nest level.
Used by Difference Editor and Merge Editor. Lines with the NOSAVE_LF flag set
are not saved in the file. VIMARK_LF Used by VI emulation to mark lines.

MODIFY_LF
Line has been modified.

INSERTED_LINE_LF
Line was inserted.

HIDDEN_LF 0x00001000 4096
Indicates that this line should not be displayed.

PLUSBITMAP_LF 0x00004000 16384
Display "+" bitmap to left of this line.

MINUSBITMAP_LF 0x00002000 8192
Display "-" bitmap to left of this line.

CURLINEBITMAP_LF
Display current line bitmap.

LEVEL_LF 0x001F8000 2064384
Bits used to store selective display nest level.

NEXTLEVEL_LF 0x00008000 32768

The MLCOMMENT flags can not be modified.

Returns: int
The new current line status flags for the current line.


Examples:

if (_lineflags() & INSERTED_LINE_LF) {
messageNwait("This line was inserted");
}
if (_lineflags() & MODIFY_LF) {
messageNwait("This line was modified");
}
// Turn on hidden flag
_lineflags(HIDDEN_LF,HIDDEN_LF);
if (_lineflags() & HIDDEN_LF) {
messageNwait("HIDDEN flag is on");

}
// Turn off HIDDEN flag
_lineflags(0,HIDDEN_LF);
if (!(_lineflags() & HIDDEN_LF)) {
messageNwait("hidden flag is off");
}

Applies To:
Edit Window, Editor Control

Categories:
CursorMovement Functions, Edit Window Methods, Editor Control Methods


FUN WITH LINEFLAGS--GETTING DOWN TO BUSINESS

Below are several macros I created to explore possible novel uses of lineflags.
I've refrained from including a couple related commands because they are not
ready for prime time.

Try these on sample files that you can afford to mess up. Generally the
operations are easily reversible (as in the toggle commands), but be
particularly careful applying the NOSAVE_LF. In some situations NOSAVE Lines
may be deleted permanently when you close a file containing them.

// clf_toggle: this command toggles the 'current line' flag on any line you
// choose. 'clf' in the command name means 'current line flag.' In this
// particular implementation, the 'current line' flag appears as a green triangle
// in the left margin. You can toggle the current line flag on and off easily if
// you bind the command to a key. Each time the command is issued, the existing
// flag is toggled and the cursor moves down one line.

// toggle CLF (current line flag) for current line
_command clf_toggle()
{
if (!(_lineflags() & CURLINEBITMAP_LF)) {
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
} else {
_lineflags(0,CURLINEBITMAP_LF);
}
down();
}


// turn ON ALL CLF indicators and flags in a file
// turn CLF ON for ALL lines
_command clf_all_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
for (;;) {
if (down()) break;
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
}
restore_pos(p);
}
}


// turn OFF ALL CLF indicators and flags in a file
// turn CLF OFF for ALL lines
_command clf_no_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(0,CURLINEBITMAP_LF);
for (;;) {
if (down()) break;
_lineflags(0,CURLINEBITMAP_LF);
}
restore_pos(p);
}
}

// turn CLF ON for lines matching search string in the current buffer
// for example, 'all_clf /VSARG2/' will add the current line flag to all
// lines containg 'VSARG2'--like the 'all' command. You might want to change the
// command name, since it can be confused with 'clf_all_lines.
_command all_clf(...) name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
int strcount;
int linecount;
_str searchstr=arg(1);
parse searchstr 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;
_lineflags(CURLINEBITMAP_LF,CURLINEBITMAP_LF);
} else {
restore_pos(p);
message get_message(status);
stop;
}

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

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

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


// 'NO-SAVE LINE' FLAG (displys the Slickedit default or user-set background
// color for 'no save' lines. 'NLF' means NOSAVE lineflag.)
// toggle NLF for current line
_command nlf_toggle() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
if (!(_lineflags() & 2)) {
_lineflags(NOSAVE_LF,NOSAVE_LF);
} else {
_lineflags(0,NOSAVE_LF);
}
cursor_down();
}

// turn NLF ON for ALL lines
_command nlf_all_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(NOSAVE_LF,NOSAVE_LF);
for (;;) {
if (down()) break;
_lineflags(NOSAVE_LF,NOSAVE_LF);
}
restore_pos(p);
}
}

// turn NLF OFF for ALL lines
_command nlf_no_lines()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(0,NOSAVE_LF);
for (;;) {
if (down()) break;
_lineflags(0,NOSAVE_LF);
}
restore_pos(p);
}
}

// turn NLF ON for lines matching search string in current buffer
_command all_nlf(...) name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
int strcount;
int linecount;
_str searchstr=arg(1);
parse searchstr 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;
_lineflags(NOSAVE_LF,NOSAVE_LF);
} else {
restore_pos(p);
message get_message(status);
stop;
}

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

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

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

// turn NLF AND CLF OFF for ALL lines
_command untag_all_nlfclf()
{
if (p_mode_name=='Fileman') {
deselect_all();
} else {
save_pos(p)
top();up();
_lineflags(0,CURLINEBITMAP_LF);
_lineflags(0,NOSAVE_LF);
for (;;) {
if (down()) break;
_lineflags(0,CURLINEBITMAP_LF);
_lineflags(0,NOSAVE_LF);
}
restore_pos(p);
}
}

// Setting PLUS and MINUS BITMAPS

// If a 'plus' and 'minus' bitmap is shown in left margin of the current line,
// running this command on that line will remove the bitmap and its associated
// lineflag.
_command set_plusminus_lf_off()
name_info(','VSARG2_REQUIRES_EDITORCTL) {
if (_lineflags() & (PLUSBITMAP_LF|MINUSBITMAP_LF)) {
_lineflags(0,PLUSBITMAP_LF|MINUSBITMAP_LF);
cursor_down();
}
}

// sets hidden lineflag on and hides the line
_command set_hidden_lf_on() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
_lineflags(HIDDEN_LF,HIDDEN_LF);
if (!(_lineflags() & HIDDEN_LF)) {
messageNwait("hidden flag is off");
}
}

// hides lines in the numeric range specified in the argument
// For example, 'hide_lines 30 50' will hide file lines 30-50 and
// indicate them by showing a 'plus' mark (collapsed/hidden lines) in the margin

_command hide_lines(...) name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY|VSARG2_CMDLINE)
{
parse arg(1) with first_line last_line

if (first_line>last_line) {
p_line=last_line;
return(1);
}
p_line=first_line;
// IF we are on line#0 and we are not displaying tof line
if (_on_line0() && !_default_option('t')) {
++first_line;
status=down();
if (status || first_line>=last_line) {
p_line=last_line;
return(-1);
}
}
cur_lineflags=_lineflags();
up();
prev_lineflags=_lineflags();
// IF this is case 1 or 3
if (!(prev_lineflags & (PLUSBITMAP_LF|MINUSBITMAP_LF)) &&
(prev_lineflags & LEVEL_LF)==(cur_lineflags & LEVEL_LF)) {
new_level=(prev_lineflags& LEVEL_LF) + NEXTLEVEL_LF;
// messageNwait("hide_selection: case 1 or 3");
// IF this is case 2
} else if ((prev_lineflags & (PLUSBITMAP_LF|MINUSBITMAP_LF)) &&
!(cur_lineflags & (PLUSBITMAP_LF|MINUSBITMAP_LF))
) {
p_line=last_line;
return(0);
// IF this is case 4 or 5
} else {
p_line=last_line;
return(0);
}

_lineflags(PLUSBITMAP_LF,PLUSBITMAP_LF|MINUSBITMAP_LF);
down();
start_level=new_level-NEXTLEVEL_LF;
doEndLastLevel=true;
for (;;) {
level=(_lineflags() & LEVEL_LF);
if (levellast_line) {
break;
}
}
if (doEndLastLevel) {
for (;;) {
new_level= (_lineflags() & LEVEL_LF)+NEXTLEVEL_LF;
if (new_level /*(_lineflags() & LEVEL_LF)*/<=start_level+NEXTLEVEL_LF) { break; } _lineflags(HIDDEN_LF|new_level,HIDDEN_LF|LEVEL_LF); status=down(); if (status) break; } } p_line=last_line;
}


Flush Left All Lines in Buffer

Tuesday, October 14, 2008 0 comments

I use this dinky little macro all the time. Offered for your consideration ... and, as always, perhaps you can build on it

// flush left all lines in current buffer
// TODO: Add option to affect line, selection, or all
// depending on select_active, etc

_command fl,flush_left_all() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
save_pos(p);
top();
search('^:b','r*','');
restore_pos(p);
}

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);
}

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