Change current file's file extension

Wednesday, March 11, 2009 0 comments

// change fext or prompt for fully qualified buffer name
_command fe, fext() name_info(','VSARG2_EDITORCTL|VSARG2_CMDLINE)
{
fid=p_buf_name;
new_fext=arg(1);
if (new_fext=='') {
name();
return(0);
}
buf_nofext=strip_filename(fid,'E');
new_fid=buf_nofext:+'.':+new_fext;
status=name(new_fid);
if (status) {
message('Fext was not changed');
return(0);
} else {
save_as(new_fid);
return(1);
}
}

Remove "no-save" and "current line" lineflags

Friday, February 27, 2009 0 comments

The purpose of this macro is to remove certain 
lineflags placed in the text manually be the user.
These particular lineflags were explored in "Fun
with Lineflags." That post can be found elsewhere
in this blog.

The use of the "no-save"(NLF) and "current line"
(CLF) lineflags were bastardizations, in that the
flags were not meant to used in ways I showed.
One thing you can do with these lineflags is to
use them to tag special lines, much like using a
bookmark. It doesn't have any great uses.

When flagged with either of these lineflags, small
icons will be displayed in the left margin of the
selected lines. You can also use the lineflags to
show a background color of you choice for each
flagged line. This can help to distinguish the
specially lineflagged lines from other selective
display lines. You can create selective displays
using color backgrounds of your choice for the
selected lines to display all lines with those
particular bits.

I have some simple macros named "all_nl" and
"all_clf" and others created to be used for having
fun with lineflags. At the bottom of this post is
a tiny macro for toggling a lineflag on a line.
In this case it deals with "CLFs" only.

// turn NLF & 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);
}
}

------------------------

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



arg_show_lines (...)

, , , , , , , , , Sunday, February 22, 2009 0 comments

// command arg_show_lines(...)

// In a selective display, show a range of lines by line number
// The main goal here is to open a range of hidden lines to view.
// If the range contains already-displayed lines, they'll be
// displayed along with the newly displayed hidden lines
//
// Usage: arg_show_lines 352 396
//
// Note: if a line with a "minus bitmap" is displayed before
// executing this macro, the minus bitmap will be removed; this
// makes some sense because the macro is meant to remove all
// minus and plusbitmap lines as it unhides all lines in the
// selected range
//
_command void arg_show_lines(...) name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
parse arg(1) with first_line last_line // get line numbers from the
p_line=first_line; // command-line arguments
up();
if (p_line<=1) { // check the lineflags in the p_line=0; // range, get seldisp level new_level=0; } else { new_level=_lineflags()&LEVEL_LF; if (!up()) { new_level=_lineflags()&LEVEL_LF; down(); } } for (;;) { // go through all lines in the if (p_line>last_line) break; // specified range and bust
level=(_lineflags() & LEVEL_LF); // them out of hidden state
_lineflags(new_level,HIDDEN_LF|LEVEL_LF|PLUSBITMAP_LF|MINUSBITMAP_LF);
status=down();
if (status) {
break;
}
if (p_line>last_line) {
break;
}
}
p_line=last_line;
}

Revisiting the Longest_line Macro

, Saturday, February 7, 2009 0 comments

If anyone can tell me how to prevent the macro text
from truncating on the right, please drop me a line.
I know I can insert line breaks manually, but there
must be a better way.


Thank you, fellow Slick editors John Hurst and Mark
Robbins, for providing useful suggestions and
revisions to the
longest_line macro.

Below are (1) the original version of the macro
(called longest_line0),
(2) John's improved version
(longest_line1), and (3) Mark's expanded and
enhanced
version (longest_line2).

This macro still needs the ability to
identify and
report on multiple lines matching the length of the
first of the longest lines found.
The macro could
further be easily modified to show the document's
longest lines in a selective display.

Anywho, I'll let you decide the differences in each
of these three macros. Maybe you'll find a good use
for them.

// Find longest line in buffer; display line number and length

_command longest_line0,longln() 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);
}

// with modifications by John Hurst
_command longest_line1() name_info(','READ_ONLY_ARG2)
{
typeless p;
save_pos(p);
top();
up();
int max = 0;
int max_linenum = -1;
while (!down()) {
_str line;
get_line(line);
int len = length(line);
if (len > max) {
max = len;
max_linenum = p_line;
}
}
message('Longest line is ' max_linenum ' with ' max ' bytes');
restore_pos(p);
if (max_linenum >= 0) {
p_line = max_linenum;
}
}

// modified and enhanced by Mark Robbins
_command longest_line2() name_info(','READ_ONLY_ARG2)
{
save_pos(p);
top();up();max=0;
while (!down()) {
get_line(lin);
len=length(lin);
if (len > max) {
max=len;
max_linenum=p_line;
}
}
//message('Longest line is ' max_linenum ' with ' max ' bytes');
s='Line 'max_linenum ' is longest with ' max ' bytes';
rv=(_message_box(s,'Goto Line', MB_DEFBUTTON2|MB_YESNO|MB_ICONQUESTION)==IDYES);
if (rv==1) {
goto_line(max_linenum);
}else{
restore_pos(p);
}
}

/*
And here is a simplified msg box:

Usage:
rv=msg_box_ask(‘Question?’,‘yN’); // default btn is No, returns 1 if yes, 0 if no
rv=msg_box_ask(‘Question?’,‘ynC’); // default btn is Cancel, returns 1 if yes, 2 if no, 3 if cancel
rv=msg_box_ask(‘Question?’,‘yN’,’h’); // default btn is No, Icon is hand, returns 1 if yes, 0 if no
rv=msg_box_ask(‘Question?’,‘yN’,’h’,’title’,’’); // default btn is No, Icon is hand, returns ‘yes’ if yes, ‘no’ if no
very tricky now….

rv=msg_box_ask(‘Question?’,‘yN’,’h’,’title’,’yes’); // default btn is No, Icon is hand, returns true if yes, else false
*/

int msg_box(_str string, _str title="Visual SlickEdit", int mb_flags=MB_OK|MB_ICONEXCLAMATION)
{
return _message_box(string,title,mb_flags);
}


typeless msg_box_ask(_str string, _str buttons='', _str icon='', _str title="Visual SlickEdit",typeless retcode=0)
{
//buttons
// o,ok,'';c,oc;a,ari,i;y,n,yn;ync;r,rc
// icons
// h,q,e,i,s,n
but=0;
b=lowcase(buttons);
if (b==''||b=='o') {but=MB_OK;}
if (b=='oc'||b=='c') {but=MB_OKCANCEL;}
if (b=='ari'||b=='a'||b=='i') {but=MB_ABORTRETRYIGNORE;}
if (b=='y'||b=='n'||b=='yn') {but=MB_YESNO;}
if (b=='ync') {but=MB_YESNOCANCEL;}
if (b=='r'||b=='rc') {but=MB_RETRYCANCEL;}
b=buttons;
d=0;
if (pos('C',b)==2) {d=MB_DEFBUTTON2;}
if (pos('C',b)==3) {d=MB_DEFBUTTON3;}
if (pos('R',b)==2) {d=MB_DEFBUTTON2;}
if (pos('R',b)==1) {d=MB_DEFBUTTON1;}
if (pos('I',b)==3||pos('I',b)==1) {d=MB_DEFBUTTON3;}
if (pos('N',b)==2) {d=MB_DEFBUTTON2;}
i=MB_ICONNONE;
// h,q,e,i,s,n
if (icon=='n') {i=MB_ICONNONE;}
if (icon=='h') {i=MB_ICONHAND;}
if (icon=='q') {i=MB_ICONQUESTION;}
if (icon=='e') {i=MB_ICONEXCLAMATION;}
if (icon=='i') {i=MB_ICONINFORMATION;}
if (icon=='s') {i=MB_ICONSTOP;}
if (icon=='n') {i=MB_ICONNONE;}
if (icon=='') {
if (but==MB_OK) {i=MB_ICONNONE;}
if (but==MB_OKCANCEL) {i=MB_ICONQUESTION;}
if (but==MB_ABORTRETRYIGNORE) {i=MB_ICONEXCLAMATION;}
if (but==MB_YESNO) {i=MB_ICONQUESTION;}
if (but==MB_YESNOCANCEL) {i=MB_ICONQUESTION;}
if (but==MB_RETRYCANCEL) {i=MB_ICONSTOP;}
}
v=msg_box(string,title,but|d|i);
if (retcode==0&&retcode._varformat()==VF_INT) {
if (but==MB_OK) {
if (v==IDOK) {return 1;}else{return 0;}
}
if (but==MB_OKCANCEL) {
if (v==IDOK) {return 1;}else{return 0;}
}
if (but==MB_ABORTRETRYIGNORE) {
if (v==IDABORT) {return 1;}
if (v==IDRETRY) {return 2;}
if (v==IDIGNORE) {return 3;}
}
if (but==MB_YESNO) {
if (v==IDYES) {return 1;}else{return 0;}
}
if (but==MB_YESNOCANCEL) {
if (v==IDYES) {return 1;}
if (v==IDNO) {return 2;}
if (v==IDCANCEL) {return 3;}
}
if (but==MB_RETRYCANCEL) {
if (v==IDRETRY) {return 1;}else{return 0;}
}
return(0);
}
if (retcode==1&&retcode._varformat()==VF_INT) {return v;}

if (retcode._varformat()==VF_LSTR) {
if (retcode=='') {
if (but==MB_OK) {
if (v==IDOK) {return 'ok';}
}
if (but==MB_OKCANCEL) {
if (v==IDOK) {return 'ok';}else{return 'cancel';}
}
if (but==MB_ABORTRETRYIGNORE) {
if (v==IDABORT) {return 'abort';}
if (v==IDRETRY) {return 'retry';}
if (v==IDIGNORE) {return 'ignore';}
}
if (but==MB_YESNO) {
if (v==IDYES) {return 'yes';}else{return 'no';}
}
if (but==MB_YESNOCANCEL) {
if (v==IDYES) {return 'yes';}
if (v==IDNO) {return 'no';}
if (v==IDCANCEL) {return 'cancel';}
}
if (but==MB_RETRYCANCEL) {
if (v==IDRETRY) {return 'retry'}else{return 'cancel';}
}
return'fail';
}else{
r=retcode;

if (but==MB_OK) {
if (v==IDOK) {return ('ok'==r);}
}
if (but==MB_OKCANCEL) {
if (v==IDOK) {return ('ok'==r);}else{return ('cancel'==r);}
}
if (but==MB_ABORTRETRYIGNORE) {
if (v==IDABORT) {return ('abort'==r);}
if (v==IDRETRY) {return ('retry'==r);}
if (v==IDIGNORE) {return ('ignore'==r);}
}
if (but==MB_YESNO) {
if (v==IDYES) {return ('yes'==r);}else{return ('no'==r);}
}
if (but==MB_YESNOCANCEL) {
if (v==IDYES) {return ('yes'==r);}
if (v==IDNO) {return ('no'==r);}
if (v==IDCANCEL) {return ('cancel'==r);}
}
if (but==MB_RETRYCANCEL) {
if (v==IDRETRY) {return ('retry'==r)}else{return ('cancel'==r);}
}
return false;
}
}
return(-1);
//return (msg_box(string,title,MB_YESNO|MB_DEFBUTTON2|mb_flags2)==IDYES);
}


Count Paragraphs

, , , , , , , , , Tuesday, December 30, 2008 0 comments

COUNT PARAGRAPHS in a document

// count number of paragraphs in buffer
// FYI: the macro counts the first paragraph properly
// regardless of whether it is proceeded by a blank line
// at the top of file

// count number of paragraphs in buffer or range

_command coupar,count_paragraphs() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
#define PARAGRAPH_SKIP_CHARS ' \t '
#define PARAGRAPH_SEP_RE ('(^['PARAGRAPH_SKIP_CHARS']*$)')
#define SKIP_PARAGRAPH_SEP_RE ('^~(['PARAGRAPH_SKIP_CHARS']*$)')

if (p_Noflines==0) {
message('Empty file');
stop;
} else {
para_count=1;
}
_save_pos2(p);
top();up();

for (;;) {
_begin_line;
/* skip paragraph separator lines */
status=search(SKIP_PARAGRAPH_SEP_RE,'r');
if ((status==BOTTOM_OF_FILE_RC) || (status==STRING_NOT_FOUND_RC)) {

get_line(line);
if (line=='') {
para_count = para_count - 1;
}

message(para_count ' paragraphs');
break;
} else {
/* Search for paragraph separator line. */
status=search(PARAGRAPH_SEP_RE,'r');
if ((status==BOTTOM_OF_FILE_RC) || (status==STRING_NOT_FOUND_RC)) {
message(para_count ' paragraphs');
break;
}
++para_count;
}
}
_restore_pos2(p);
}

Childlike in Its Simplicity

, , , Sunday, December 21, 2008 0 comments

Over many years, I've used different versions of the "jump_srch" macro as a productivity enhancer.

The example of jump_srch code below is rudimentary and only involves quick searching between two MDI windows. I plan to add more complete versions in the near future. This macro can be made much more versatile and powerful, incorporating such tasks as automatic insertion, deletion, checking and indexing of text between windows. For now, you're getting it in its bare bones flavor. Please don't laugh too loud when you see the actual macro. We all have to start somewhere!

The base use of the macro is to employ find and next_window commands between two visible Slickedit MDI windows. The two windows can contain the same buffer (aka "file") or two different buffers. I haven't used this macro in more than two windows at a time, although I
suppose you could cycle the cursor through however many windows you want. FYI: I always have "one file per window" turned OFF. I don't know if the macro would be adversely affected by having it turned ON.

How it works: with your cursor on a text string in Window A, you press the key binding for jump_srch and the cursor jumps to Window B and locates the next matching search string, if any, in Window B. You can, if you like, continue searching for the same string in Window B or you can return to Window A and go on your merry way.

I use the binding "Ctrl-G" to find the next instance of a search term within a single file. To move my cursor back to Window A from Window B, I use a simple key binding for next_window. In my case, next_window is bound to "Ctrl-Tab." In other words, you use jump_srch to seek the string in the other window, and then you use next_window to return to the first window. This is handy when you need to locate or cross-check certain words or phrases which appear in both files or both windows.

Here's an example of tasks for which this macro can be useful. Please forgive the use of jargon from the book publishing industry, which is where I've used this macro most often.

/*---------------------------------------------------------------*/

How to Automate Cross-checking of References and Citations
in Publication Drafts

There are two basic goals in cross-checking literary references;
these are:

1) to verify that every reference appearing in the main section of the
document (body text, tables, figures, etc.) has a corresponding
citation in the section designated to hold a comprehensive listing
of citations (this is usually titled References, and appears as a
component of the back matter)

2) to verify that every citation in the References section has a
corresponding reference in the main section of the document

These two sections are typically maintained in two separate text files.
This facilitates creation of a system for automating
the process of checking crossreferences . . .
/*---------------------------------------------------------------*/

jump_srch, barebones version:
// put the current word into a variable, jump to the next open window, and find the text in that window
// todo etc.
// make it work with any selected current word or phrase or line

// whether the cursor is in or at the start of a word; selected or not
// allow for omore than 2 windows, etc
// pass cmdline parameters to search command

_command void jump_search(...) name_info(',')
{
_str keywd=cur_word(stuff);
message(something);
next_window();
search(something);
}

dh_Fileinfo

, , , , , , , , , , , , , , , , Monday, December 1, 2008 0 comments

The dh_fileinfo macro displays an informational dialog box with
various
details about the file currently being edited. I cobbled the macro
together from code available on the Slickedit Web site and elsewhere.
I've since forgotten where everything came from. This macro can be
greatly improved. You might notice, for example, that the value of some
of the variables, such as 'cc' (character count) are not yet included in the
message box.

I invite your improvements and suggestions, including ideas on stats to
add to the existing dialog.


The dh_fileinfo dialog lists:
  • fully qualified filename and path
  • file's current date and time
  • file size in bytes
  • file attributes
  • total number of lines in file
  • current margin setting
  • file's mode (e.g., Slick-C, HTML, C, etc.)
  • number of windows open in the editor
  • number of buffers open
  • number of the current View id
  • decimal and hex values of character under the cursor
// detailed fileinfo message box
_command dh_fileinfo() name_info(FILE_ARG'*,'READ_ONLY_ARG2)
{
_str filename=arg(1)
if (filename=='') {
filename=p_buf_name;
}
filename=absolute(filename);
_str filedate=file_date(filename);
_str filetime=file_time(filename);
_str fileattr=file_list_field(filename,DIR_ATTR_COL,DIR_ATTR_WIDTH);
int filelen=p_Noflines;
int bufcou=_Nofbuffers();
int wincou=Nofwindows();
get_view_id view_id
parse p_margins with left_ma right_ma new_para_ma;

// info on current character
get_line(line);
char=substr(line,p_col,1);
chr_num=_asc(get_text());

cc = 0; /* chars */
wc = 0; /* words */
lc = 0; /* lines */
dc = 0; /* displayed lines */
hc = 0; /* hidden lines */

int item = 0;
save_pos(p);
top_of_buffer();
for (;;) {
get_line line;
lc++;
for (i = 1; i < length(line); i++) {
cc++;
if (isalnum(substr(line,i,1))) {
item = 1;
} else if (item && !isalnum(substr(line,i,1))) {
wc++;
item = 0;
}
}
status=down();
bytesize = fsize();
dc = (p_Noflines - p_NofSelDispBitmaps);
hc = p_Nofhidden;
sc = p_NofSelDispBitmaps;
pc = ( filelen intdiv 55 );
_message_box(filename \r filedate' 'filetime \r bytesize ' bytes; ' fileattr \r\
'Lines: 'filelen' '\r\
'Margins: 'left_ma' 'right_ma' 'new_para_ma \r\
'Mode: 'p_mode_name \r 'Windows: 'wincou', Buffers: 'bufcou \r\
'Viewid 'view_id \r\
'Ascii char "'char'" dec='chr_num', hex='dec2hex(chr_num),' ':+
'FileInfo: '
p_buf_name, MB_ICONINFORMATION);

restore_pos(p);
break;
}
}

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