Showing posts with label macros. Show all posts
Showing posts with label macros. Show all posts

I repeat myself when under stress, I repeat myself when under stress . . .

, , , , , , Thursday, March 12, 2009 0 comments

// repeat_n [how many times] [command name]
// Repeats a command the number of times specified.

// Args are "n command", where n is the number of times
// to execute the command, and command is the name of
// the command to run.
//
// Usage example: repeat_n 15 cursor_down
// This will execute the cursor-down command 15 times, with the
// expected results.

// Source: Professional Slickedit by John Hurst
// Copyright 2007 Wiley Publishing Inc, Indianapolis, Indiana, USA.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0

_command void repeat_n(_str args = null) \\
name_info(','VSARG2_EDITORCTL)
{
if (args == null) {
message("Specify number of times and " \\
"command to run."
);
return;
}
_str n;
_str command;
parse args with n command;
if (n <= 0 || command == null) {
message("Specify number of times and " \\
"command to run."
);
return;
}
int i;
for (i = 0; i < n; i++) {
execute(command);
}
}

// repeat_n_last_macro [how many times]
// Repeats the last recorded macro the number of times specified
// Usage example: repeat_n_last_macro 25
// Executes the last recorded macro 25 times

//
Source: Professional Slickedit by John Hurst
// Copyright 2007 Wiley Publishing Inc, Indianapolis, Indiana, USA.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0

_command void repeat_n_last_macro(_str args = null) \\
name_info
(','VSARG2_EDITORCTL)
{
repeat_n(args :+ " last_macro");
}


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

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

About myslickeditmacros

, , , , , Thursday, September 4, 2008 1 comments

If this is your first time here, please read the following
important background information.


Here you'll find Slick-C macros of all kinds
, from overly simple to fairly complex. I'm a writer/editor/journalist by trade, not a professional developer. Slick-C programmers should find something 0f interest here, including plenty of code that can be tweaked or fixed.

The purpose of this blog is to address the shortage of Slick-C code on the Web, while stimulating discussion and encouraging code sharing.

I've used Slickedit in my daily work since approximately 1998. Currently I have 388 _command macros in my vusrmacs.e file. I'm sure there are other Slickedit users whose macro folders are wishing to burst free and benefit the larger community.

My professional roles are technical writer; editor and researcher of print and electronic publications; desktop publisher
; journalist. My programming experience has generally been limited to high-level scripting languages. I've spent a lot of time using numerous text editors and macro languages. Of the two kinds of Slickedit users named in the subtitle of this blog, I'm a Wordsmith rather than a Code Maven.

This blog is not meant to replace Slickedit Documentation, Slickedit Community Forums, "Hello World:" The Slickedit Developer Blog or other Slickedit-related sites. There's plenty of good stuff in those places. I'll post a listing of a bunch of good resources soon.

Many of "my" macros--perhaps a third--borrow from, or are based on, code examples or fragments that I've run across in the past 10 years. This includes lots of Slick-C code and ideas plucked from the guts of Slickedit's code modules themselves. And of course, I've borrowed liberally from third party sources.

If I've failed to credit the authors of any of the code on this blog, please contact me. The same goes for any code that duplicates functionality already built into Slickedit. Please call deprecated procedures to my attention. I will gladly remove, change or credit code in response to reasonable requests.

Again, most macros on this site are relatively simple. Some are simply shortcuts for frequently used commands, regular expressions or tedious command-line typing. I hope you find some of them useful.

Wordsmiths and Code Mavens, grab a Jolt and let's get started. Let's all take full advantage of what is probably the best, most complete and most full-featured text editor ever created.


Here's a simple macro I use frequently. It quickly counts the number of paragraphs in the current buffer and displays the result on the command line. This is helpful because I often work with documents containing hundreds of thousands of lines and thousands of blocks of text. If you wish to change the parameters for recognizing a paragraph, you can modify the #defines at the top of the macro.

// count_paragraphs: count number of paragraphs in buffer
_command coupar,count_paragraphs() name_info(','VSARG2_REQUIRES_EDITORCTLVSARG2_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);
}


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