Branch: refs/heads/master Author: Sylvan Mostert smostert.dev@gmail.com Committer: Sylvan Mostert smostert.dev@gmail.com Date: Sun, 10 Jan 2016 05:44:15 UTC Commit: 41a53a4e079a78e9eee5aacdbc8f440be7b13ddf https://github.com/geany/geany-plugins/commit/41a53a4e079a78e9eee5aacdbc8f44...
Log Message: ----------- Added Comments/documentation
Signed-off-by: Sylvan Mostert smostert.dev@gmail.com
Modified Paths: -------------- lineoperations/README lineoperations/README.md lineoperations/src/linefunctions.c lineoperations/src/linefunctions.h
Modified: lineoperations/README 68 lines changed, 0 insertions(+), 68 deletions(-) =================================================================== @@ -1,68 +0,0 @@ -LineOperations -============== - -.. contents:: - -About -===== - -Line Operations is a plugin for Geany that gives a convenient option to -remove duplicate lines, remove unique lines, remove empty lines, -remove lines with only whitespace, and sort lines. - - -Features --------- - -* Remove dupulicate lines -* Remove unique lines -* Remove empty lines -* Remove whitespace lines -* Sort lines ascending -* Sort lines descending - -Usage ------ - -After the plugins has been installed successfully, load the plugin via -Geany's plugin manager and a new menu item in the Tools menu will appear. - -Remove Duplicate Lines -^^^^^^^^^^^^^^^^^^^^^^ - -The first occurance of each duplicate line will remain in the file. - -e.g. If a file has the following lines: -Line 2 -Line 1 -Line 2 - -The 'Remove Duplicate Lines' will change the file into this: -Line 2 -Line 1 - -Remove Unique Lines -^^^^^^^^^^^^^^^^^^^ - - - -License -------- - -The Line Operations plugin is distributed under the terms of the -GNU General Public License as published by the Free Software Foundation; -either version 2 of the License, or (at your option) any later version. -A copy of this license can be found in the file COPYING included with the -source code of this program. - -Ideas, questions, patches and bug reports ------------------------------------------ - -Please direct all questions, bug reports and patches to the plugin author using the -email address listed below or to the Geany mailing list to get some help from other -Geany users. - - -or report them at https://github.com/geany/geany-plugins/issues. - -
Modified: lineoperations/README.md 186 lines changed, 186 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,186 @@ +# LineOperations # + + +## About ## + +*Line Operations* is a plugin for *Geany* that gives a convenient option to apply some useful line operations on an open file. + +### Features ### + +* [Remove Duplicate Lines, sorted](#remove-duplicate-lines) +* [Remove Duplicate Lines, ordered](#remove-duplicate-lines) +* [Remove Unique Lines](#remove-unique-lines) +* [Remove Empty Lines](#remove-empty-lines) +* [Remove Whitespace Lines](#remove-whitespace-lines) +* [Sort Lines Ascending](#sort-lines) +* [Sort Lines Descending](#sort-lines) + +## Usage ## + +After the plugins has been installed successfully, load the plugin via +Geany's plugin manager and a new menu item in the Tools menu will appear. Click on each menu item to apply operation on whole file. See descriptions below to see operations for each menu item. + +### Notes ### + +1. Line Operations will **not** make changes to a file until you save the file. + +2. New line characters are considered to be part of the line. If the last line does not contain new line characters it will be considered different from a line with new line character(s). + + line 1 + + is not the same as: + + line 1\r\n + + +### Operation Details ### + +#### Remove Duplicate Lines #### + +The first occurrence of each duplicate line will remain in the file.**Sorted** option will sort the file and remove duplicate lines **[fast]**. **Ordered** option will keep the same order of lines **[slow]**. + +**Example:** Suppose a file has the following lines. (#comments added for clarity) + + Line 2 + Line 1 + Line 2 #removed + Line 3 + Line 1 #removed + Line 2 #removed + +The **Remove Duplicate Lines, sorted** will change the file into this: + + Line 1 + Line 2 + Line 3 + +The **Remove Duplicate Lines, ordered** will change the file into this: + + Line 2 + Line 1 + Line 3 + + + +#### Remove Unique Lines #### + +Removes all lines that appear only once. + +**Example:** Suppose a file has the following lines. (#comments added for clarity) + + Line 2 + Line 1 + Line 2 + Line 3 #removed + Line 1 + Line 2 + +The 'Remove Unique Lines' will change the file into this: + + Line 2 + Line 1 + Line 2 + Line 1 + Line 2 + +#### Remove Empty Lines #### + +Removes all lines that end with a newline character(s) AND do not have any other characters. + +**Example:** Suppose a file has the following lines. (#comments, and \n newline characters added for clarity) + + Line 2\n + Line 1\n + \n #removed + \n #NOT removed (contains spaces) + Line 1\n + Line 2\n + +The 'Remove Unique Lines' will change the file into this: + + Line 2\n + Line 1\n + \n + Line 1\n + Line 2\n + + +#### Remove Whitespace Lines #### + +Removes all lines that have only white space characters. + +A **white space character** is one of: + +1. ```' '``` : space +2. ```'\t'```: horizontal tab +3. ```'\f'```: form feed +4. ```'\v'```: vertical tab +5. ```'\r'```: cartridge return +6. ```'\n'```: newline character + +**Example:** Suppose a file has the following lines. (#comments, and \n newline characters added for clarity) + + Line 2\n + Line 1\n + \n #removed + \n #removed (contains only whitespace chars) + \t \n #removed (contains only whitespace chars) + Line 1\n #NOT removed (contains non whitespace chars) + Line 2\n + +The 'Remove Unique Lines' will change the file into this: + + Line 2\n + Line 1\n + Line 1\n + Line 2\n + +#### Sort Lines #### + +Sorts lines ascending or descending based on ASCII values (lexicographic sort) + + +**Example:** Suppose a file has the following lines. + + line 1 + line 2 + line + line 3 + line + +The **Sort Lines Ascending** will change the file into this: + + line + line + line 1 + line 2 + line 3 + + +The **Sort Lines Descending** will change the file into this: + + line 3 + line 2 + line 1 + line + line + + +## License ## + +The Line Operations plugin is distributed under the terms of the +GNU General Public License as published by the Free Software Foundation; +either version 2 of the License, or (at your option) any later version. +A copy of this license can be found in the file COPYING included with the +source code of this program. + +## Ideas, questions, patches and bug reports ## + +Please direct all questions, bug reports and patches to the plugin author using the +email address listed below or to the *Geany* mailing list to get some help from other +*Geany* users. + + +or report them at https://github.com/geany/geany-plugins/issues. + +
Modified: lineoperations/src/linefunctions.c 79 lines changed, 43 insertions(+), 36 deletions(-) =================================================================== @@ -22,10 +22,11 @@ #include "linefunctions.h"
-// isspace() without newline comparisons +// isspace() gboolean issp(gchar c) { - return (c == ' ' || c == '\t' || c == '\f' || c == '\v'); + return (c == ' ' || c == '\t' || c == '\f' || + c == '\v' || c == '\r' || c == '\n'); }
@@ -75,9 +76,9 @@ void rmdupst(GeanyDocument *doc) { nfposn = 0; k = 0;
- if(newfile && lines) // verify memory allocation worked + if(newfile && lines) // verify memory allocation { - // put contents of document into **lines array + // copy *all* lines into **lines array for(i = 0; i < totalnumlines; i++) lines[numlines++] = sci_get_line(doc->editor->sci, i);
@@ -95,12 +96,12 @@ void rmdupst(GeanyDocument *doc) { newfile[nfposn++] = lines[i][j]; k = i; } + newfile[nfposn] = '\0'; + sci_set_text(doc->editor->sci, newfile); // set new document
}
- // set the *newfile as current document - sci_set_text(doc->editor->sci, newfile);
// free used memory for(i = 0; i < numlines; i++) @@ -130,9 +131,9 @@ void rmdupln(GeanyDocument *doc) { nfposn = 0; toremove = NULL;
- if(newfile && lines) + if(newfile && lines) // verify memory allocation { - // put contents of document into **lines array + // copy *all* lines into **lines array for(i = 0; i < totalnumlines; i++) lines[numlines++] = sci_get_line(doc->editor->sci, i);
@@ -148,11 +149,12 @@ void rmdupln(GeanyDocument *doc) { for(j = (i+1); j < numlines; j++) if(!toremove[j]) if(strcmp(lines[i], lines[j]) == 0) { - toremove[j] = TRUE; - //toremove[i] = TRUE;//remove all occurrances + toremove[j] = TRUE; // this line is duplicate, + // mark to remove + //toremove[i] = TRUE; //remove all occurrances }
- // copy line into 'newfile' if it is not FALSE(unique) + // copy line into 'newfile' if it is not FALSE(not duplicate) for(i = 0; i < numlines; i++) { if(!toremove[i]) @@ -160,10 +162,11 @@ void rmdupln(GeanyDocument *doc) { newfile[nfposn++] = lines[i][j]; if(lines[i]) g_free(lines[i]); } + newfile[nfposn] = '\0'; + sci_set_text(doc->editor->sci, newfile); // set new document } - newfile[nfposn] = '\0'; - sci_set_text(doc->editor->sci, newfile); +
// each line is freed in above for-loop if(lines) g_free(lines); @@ -192,12 +195,13 @@ void rmunqln(GeanyDocument *doc) { nfposn = 0; toremove = NULL;
- if(newfile && lines) + if(newfile && lines) // verify memory allocation { // copy *all* lines into **lines array for(i = 0; i < totalnumlines; i++) lines[numlines++] = sci_get_line(doc->editor->sci, i);
+ // allocate and set *toremove to all TRUE toremove = g_malloc(sizeof(gboolean) * numlines); for(i = 0; i < (numlines); i++) toremove[i] = TRUE; @@ -223,10 +227,11 @@ void rmunqln(GeanyDocument *doc) { newfile[nfposn++] = lines[i][j]; if(lines[i]) g_free(lines[i]); } + newfile[nfposn] = '\0'; + sci_set_text(doc->editor->sci, newfile); // set new document } - newfile[nfposn] = '\0'; - sci_set_text(doc->editor->sci, newfile); +
// each line is freed in above for-loop if(lines) g_free(lines); @@ -241,7 +246,7 @@ void rmemtyln(GeanyDocument *doc) { gint totalnumlines; // number of lines in the document gchar *line; // temporary line gint linelen; // length of *line - gchar *newfile; // pointer for new document + gchar *newfile; // *final* string to replace current document gint nfposn; // position to the last character in newfile gint i; // temporary iterator number gint j; // iterator @@ -257,7 +262,7 @@ void rmemtyln(GeanyDocument *doc) { newfile = g_malloc(sizeof(gchar) * (totalnumchars+1)); nfposn = 0;
- if(newfile) // make sure newfile memory allocation has not failed (NULL) + if(newfile) // verify memory allocation { for(i = 0; i < totalnumlines; i++) // loop through opened doc char by char { @@ -268,35 +273,36 @@ void rmemtyln(GeanyDocument *doc) { line = sci_get_line(doc->editor->sci, i);
if(line[0] != '\r') + // copy current line into *newfile for(j = 0; line[j] != '\0'; j++) newfile[nfposn++] = line[j]; } - else if(linelen != 1) { + else if(linelen != 1) + { line = sci_get_line(doc->editor->sci, i);
+ // copy current line into *newfile for(j = 0; line[j] != '\0'; j++) newfile[nfposn++] = line[j]; } } newfile[nfposn] = '\0'; + sci_set_text(doc->editor->sci, newfile); // set new document
- // set old file with new file - sci_set_text(doc->editor->sci, newfile); - g_free(newfile); } }
- +// Remove Whitespace Lines void rmwhspln(GeanyDocument *doc) { gint totalnumchars; // number of characters in the document gint totalnumlines; // number of lines in the document gchar *line; // temporary line gint linelen; // length of *line - gchar *newfile; // pointer for new document + gchar *newfile; // *final* string to replace current document gint nfposn; // position to the last character in newfile gint i; // temporary iterator number gint j; // iterator @@ -312,9 +318,9 @@ void rmwhspln(GeanyDocument *doc) { newfile = g_malloc(sizeof(gchar) * (totalnumchars+1)); nfposn = 0;
- if(newfile) // make sure newfile memory allocation has not failed (NULL) + if(newfile) // verify memory allocation { - for(i = 0; i < totalnumlines; i++) // loop through opened doc char by char + for(i = 0; i < totalnumlines; i++) // loop through opened doc { linelen = sci_get_line_length(doc->editor->sci, i);
@@ -323,29 +329,30 @@ void rmwhspln(GeanyDocument *doc) { line = sci_get_line(doc->editor->sci, i);
if(line[0] != '\r') + // copy current line into *newfile for(j = 0; line[j] != '\0'; j++) newfile[nfposn++] = line[j]; } - else if(linelen != 1) { + else if(linelen != 1) + { line = sci_get_line(doc->editor->sci, i); + if(!iswhitespaceline(line)) + // copy current line into *newfile for(j = 0; line[j] != '\0'; j++) newfile[nfposn++] = line[j]; }
} newfile[nfposn] = '\0'; - - - // set old file with new file - sci_set_text(doc->editor->sci, newfile); + sci_set_text(doc->editor->sci, newfile); // set new document
g_free(newfile); } }
- +// Sort Lines Ascending and Descending void sortlines(GeanyDocument *doc, gboolean asc) { gint totalnumchars; // number of characters in the document gint totalnumlines; // number of lines in the document @@ -365,15 +372,17 @@ void sortlines(GeanyDocument *doc, gboolean asc) {
if(newfile && lines) // verify memory allocation worked { + // copy *all* lines into **lines array for(i = 0; i < totalnumlines; i++) lines[numlines++] = sci_get_line(doc->editor->sci, i);
+ // sort **lines array if(asc) qsort(lines, numlines, sizeof(gchar *), compareasc); else qsort(lines, numlines, sizeof(gchar *), comparedesc);
- + // copy **lines array into *newfile for(i = 0; i < numlines; i++) { for(j = 0; lines[i][j] != '\0'; j++) @@ -382,12 +391,10 @@ void sortlines(GeanyDocument *doc, gboolean asc) { if(lines[i]) g_free(lines[i]); } newfile[nfposn] = '\0'; + sci_set_text(doc->editor->sci, newfile); // set new document
}
- sci_set_text(doc->editor->sci, newfile); - - // each line is freed in above for-loop if(lines) g_free(lines); if(newfile) g_free(newfile);
Modified: lineoperations/src/linefunctions.h 12 lines changed, 12 insertions(+), 0 deletions(-) =================================================================== @@ -28,16 +28,28 @@ //#include <ctype.h> /* isspace */ #include <stdlib.h> /* qsort */
+ +// Remove Duplicate Lines, sorted void rmdupst(GeanyDocument *doc);
+ +// Remove Duplicate Lines, ordered void rmdupln(GeanyDocument *doc);
+ +// Remove Unique Lines void rmunqln(GeanyDocument *doc);
+ +// Remove Empty Lines void rmemtyln(GeanyDocument *doc);
+ +// Remove Whitespace Lines void rmwhspln(GeanyDocument *doc);
+ +// Sort Lines Ascending and Descending void sortlines(GeanyDocument *doc, gboolean asc);
#endif
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).
plugins-commits@lists.geany.org