Академический Документы
Профессиональный Документы
Культура Документы
Motivation:
The motivation for doing this project is to develop a graphical text editor which can be more user friendly and reliable having good user interface and more features.
Design Decision:
C language is selected for designing his project. Several inbuilt functions and templates present in C language are used for making the coding easier. The idea of using ncurses library for GUI purpose is that it supports: overall screen, window and pad manipulation. Output to windows and pads, reading terminal input, control terminals and curses input and output, color manipulation, use of soft label keys, term info capabilities and access to low level terminal manipulation routines.
Platform:
This editor is for LINUX operating system. I chose linux platform because for different GUI options which are present in ncurses library present in the LINUX operating system. Also I have used list.h library for buffering purposes used in the design of the editor.
Language:
Language used is C Language.
1.2:Product Perspective:
The product is a full screen text editor for the LINUX operating system with the capabilities to edit existing files and also create new ones. To make editing simple is provided a pop own menu interface, as also are shortcut keys for command. Product Functions 1. File operations Create a new file Open an existing file Save and Save As a file Delete a file Exit from the editor 2. Editing operations Cut copy and paste Delete word Delete Line Insert line 1
A Simple Text Editor Find and Replace 3. Page Optoins Change background and foreground color Setting the page margins. 4. Help options Help topics About
1.3:HARDWARE REQUIREMENTS
The hardware requirements are minimum that is required to install the Linux OS MAIN PROCESSOR : Pentium IV (500Mhz) RAM SIZE : 128 MB CACHE MEMORY : 256KB DISKETTE DRIVE : 1.44MB,3.5inches OPTICAL DRIVE : 4X DVD-ROM DRIVE
1.4:SOFTWARE REQUIREMENTS
OPERATING SYSTEM: Red Hat Linux 9.0 or Fedora core COMPILER USED: GCC version 3.2.2 EDITOR: VI Editor version 6.1 Ncurses library present in the Linux Operaring System
A Simple Text Editor 3.2 ADVANTAGES OF LINUX Configurability Convenience Stability Community Freedom
3.3 LINUX FEATURES It is a sophisticated high performance Unix like operating system for multiple architectures Key Linux Features Per user file and application security. Can have applications from multiple machines on your workstation at the same time. Shared libraries for disk and memory efficiency. Unified memory/disk cache for high performance. Transparent software FPU emulation for integer only machines. POSIX.1 style API with both USL and BSD extensions. Porting almost all correctly written POSIX or Unix API based programs is trivial. TCP/IP networking including both protocol layers and the standard BSD tool set. Wide set of internet World Wide Web tools. NFS server and client - the standard Unix network file system. Internet SMTP mail server and clients - including MIME support. SLIP and PPP for internet networking over analog or digital modems. Wide range of high performance network drivers. Wide range of supported hardware. Multimedia soundcard and CD-ROM drivers.
CHAPTER-3 EDITORS
3.1:Definition:
An editor is general-purpose system software, which can be used to enter and/or modify text, programs, graphics or data. An editor may be a line editor, screen editor, graphics editor or word Processors. We will be discussing all these as we move on.
3.2:Types of Editors:
Editors are broadly classified as 1. Line Editors 2. Screen Editors 3. Graphic Editors
In this above process, the selection of the part of the document to be viewed and edited involves first traveling through the document to locate the area of interest. This search is accomplished with operations such as find pattern, move cursor & find & replace. Filtering controls the process of what is to be viewed & manipulated. Filtering extracts the relevant subset of the target document at the point of interest, such as the next screen of the text or the next statement. In the actual editing phase, the target document is created or altered with a set of operations such as insert, delete, replace, move, copy. The editing functions are often specialized to operate on elements meaningful to the type of editor. For example, a manuscript-oriented editor might operate on elements such as single characters, words, lines, sentences, and paragraphs; a program-oriented editor might operate on elements such as identifiers, keywords, and statements.
A Simple Text Editor In a simple scenario, the, the user might travel to the end of the document. A screenful of text would be filtered, this segment would be formatted, and the view would be displayed on an output device. The user could then, for example, delete the first three words of this view.
Editing component
Input
FILE: It is the Floppy Disk/Hard Disk drive where the required source information is stored. This data is to be retrieved into the Primary memory before any changes are done. It is stored in a named file USER COMMAND: This device provides information to modify/append the data. The command is entered through the keyboard/mouse.
BUFFER: It is the primary memory where the actual modification of information is possible. Data is brought here from the file through commands. DISPLAY UNIT: Data can be seen here. It is also called Visual Display Unit. It is possible to see the changes made in this buffer and unit.
EDITOR: This is the main part of the complete process. It has the actual program to retrieve data from the file and place it in the buffer.
The system specification can be described by following data flow diagrams (DFD). 7
FILE
BUFFER
EDITOR
USER
DISPLAY
Fig: 4.1: Basic Blocks of the editor
Node 1 Character 1
Node 2 Character 2
Node 3 Character 3
\0
A Simple Text Editor The header file contains window structures definitions, macros, function definitions and prototypes, keyboard definitions, screen attitudes.
11
A Simple Text Editor A_ BOLD A_DIM A_REVERSE A_UNDERLINE A_STANDOUT extra bright or bold half bright reverse video under lining high lighting
Note : It is important to note here that terminal should support specified else the specified description holds no value. Attributes can be turned on singly or in combination. For example a text has to be bold and under lined. There are several functions for manipulating attributes. They are: attrset( ) : turns off previously defined attribute and sets requested one. attron ( ) : adds attribute to the existing ones in a string. attroff( ) : Turns off the required attribute. The window : Windows can be created on the physical screen and use them instead default screen. A window is that part of the screen considered to be achive at a time and hence cannot be greater then the stdscr. Windows are useful in maintaining several different screen images, For example, One window could be used to control input and output and one to print error messages. To create a window called WIN, first declare this window in-the following manner: WINDOW * win; * win is a pointer to the structure WINDOW which is defined in the header in the window. The function that support Window are: newwin ( ) : The parameters specified with newwin ( ) are assigned to the new of the new window. Window *fwin = newwin (x1, y1, x2, y2); x1=number of rows; y1=number of columns: x2=begining of row; y2=beginning of columns: wdelch ( ) : This function deletes characters plotted on the cursor position indiscriminately. The name of the window on which characters should be deleted has to be specified with it. winsch ( ) : This function permits to insert characters without deleting the existing character on the cursor position. keypad (WINDOW *,BOOL ) : This is a function that enables the terminal to interpret the ASCII value of each character. WINDOW * is the variable of type window BOOL :- TRUE to enable key pad options & FALSE to disable the key pad options.
12
CHAPTER-6
IMPLEMENTATION DETAILS
6.1: Introduction:
This text editor supports many editing functions to the user. An user can edit a given text file. The different editing features are Open file, Save file, Delete file, cut copy and paste, delete line/word, insert line/word, Find and Replace. The project is divided into three phases. Developing the graphical user interface. Writing the code for different functions. Code for user key functions.
6.2: Implementation
Phase 1:
Developing the background window and menu bar. At first background window is generated using ncurses library functions library command. Then the background color is set. Then on the top and bottom of this background window two sub windows of different colors are created. For each menu options different sub windows are created and appropriate strings of different names are added into those sub windows. Key Binding in each sub windows Here for each options in the sub windows , keys are binded and the keys include shortcut keys for each options in that window including the up-down motion of the curser in that sub window and again the left right motion of the sub windows in the menu bar.
Phase 2:
In this phase I have used several inbuilt functions in the list.h for buffers used in the editors. Doubly Linked List is the intermediate representation of the characters. I have connected both the front end and the back end using these functions.
Phase 3:
In this phase, I have binded the different user keys. The minimum keys needed are arrow keys. Then all the other shortcut keys are also binded.
13
A Simple Text Editor Read filename and call save() function Else Call save() function End {if} If cmd=4 then Begin Print the message Enter the new filename Call save() function End {if} If cmd=5 then Delete the file in the given directory If cmd=6 then Call the quit() function Else if ch is <ctrl-E> then Begin Get the command cmd from Edit menu option If cmd=1 then Call insword () function. If cmd=2 then Call insline() function. If cmd=3 then Call delword () function. If cmd=4 then Call dellinecur() function. If cmd=5 then cut() function. If cmd=6 then Call copy() function. If cmd=7 then Call paste() function If cmd=8 then Call find() function If cmd=9 then Call replace() function If Escape then exit drop down menu End If Else if ch is <ctrl-P> then Begin Get command cmd from Page Setup menu option If cmd=1 then Read and set the Left Margin If cmd=2 then Read and set the Right Margin If cmd=3 then Read and set Top Margin If cmd=4 then Read and set Bottom Margin If cmd=5 then Call changcol() function 15
A Simple Text Editor End Else if ch is <ctrl-H> then Begin Get command cmd from Help menu option If cmd=1 then Call help() function If cmd=2 then Call about() function. End End Else If Default: print the alphanumeric key pressed. Display the file End {while} End {Algorithm for main () function}
16
17
7.2: LIMITATIONS
This text editor has following imitations: Multitab feature is not supported. Only text files can be edited. Few shortcut keys are provided. The source code is non portable to non linux platforms. Mouse support is only for single page files and for text selection only. Not much user interface options through mouse is provided. Source code is not helpful without ncurses library routines.
7.3:FUTURE ENHANCEMENT
User interface can be improved. Multi-tab facility can be provided. More shortcut keys can be assigned. More editing options must be provided.
18
SNAPSHOTS
19
20
21
BIBLIOGRAPHY:
System Software by Leland L Beck and D. Manjula. Data Structures in C by Tenun Baum Let Us C by Yashwanth Kanetkar Understanding the Linux Kernal by Daniel P.Bovet and Marco Cesati Programmers Guide to Ncurses by Dan Gookin www.wikipedia.org
22
APPENDIX A
SOURCE CODE FOR THE TEXT EDITOR
#include<stdio.h> #include<ncurses.h> #include<malloc.h> #include<stdlib.h> #include<string.h> //############# BASIC STRUCTRE ############ struct node { chtype ch; struct node *prev,*next; }; typedef struct node * NODE; NODE getnode(); //############# ALLOCATE MEMORY ######### NODE getnode() { NODE temp; temp=(NODE)malloc(sizeof(struct node)); temp->prev=temp->next=NULL; return temp; } void delline(); void delword(); void updline(); void frep(); void cut(); void copy(); void paste(); void find(); void replace(); void changcol(int); void menuselect(int); void filemenu(); void newfile(); void openfile(); void savefile(); void deletefile(); void loadfile(); void editmenu(); void inschar(chtype ch); void insword(); void insline(); void pagemenu(); 23
A Simple Text Editor void helpmenu(); void about(); void helpm(); void pagebegin(); void pageend(); void myrefresh(); void tabchar(); void bspace(); void home(); void end(); void keyleft(); void keyright(); void keyup(); void keydown(); void pagedown(); void pageup(); void updscr(NODE); void updtxt(); void curpos(); int display(); void deallocate(); void horizonscroll(); NODE vscrollup(); NODE vscrolldown(); void delchar(); char fil[' '][' ']={"New","Open","Save","Save As","Delete","Shell","Exit"}; char edi[' '][' ']={"Ins Word","Ins Line","Delete Word","Delete Line","Cut","Copy","Paste","Find","Replace"}; char pag[' '][' ']={"Left Margin","Right Margin","Begin","End","Change Color"}; char hlp[' '][' ']={"Help ","About"}; int x=0,y=0,curx=0,cury=0,top=0,bot=34,xl=0,xr=74,maxy=0,mbit=0,entercount,rightcount,kee p,linecount; WINDOW *title,*back,*text,*status; NODE first=NULL,cur=NULL,last=NULL; char fname[20]="UNTITLED"; NODE src=NULL,des=NULL,mtemp=NULL,temps=NULL; char str[120],reps[30];
24
A Simple Text Editor init_pair(4,COLOR_BLACK,COLOR_WHITE); init_pair(5,COLOR_BLACK,COLOR_MAGENTA); init_pair(6,COLOR_BLACK,COLOR_GREEN); init_pair(7,COLOR_BLACK,COLOR_YELLOW); init_pair(8,COLOR_BLACK,COLOR_WHITE); init_pair(9,COLOR_BLACK,COLOR_RED); init_pair(10,COLOR_WHITE,COLOR_BLACK); init_pair(11,COLOR_BLACK,COLOR_BLACK); title=newwin(1,COLS-1,0,0); wbkgd(title,COLOR_PAIR(3)); wrefresh(title); wattron(title,A_BOLD); mvwaddstr(title,0,60,fname); wattroff(title,A_BOLD); wrefresh(title); back=newwin(LINES-2,COLS-1,1,0); wborder(back,0,0,0,0,0,0,0,0); wbkgd(back,COLOR_PAIR(8)); wrefresh(back); wattron(back,A_BOLD); mvwaddch(back,0,8,'F'); wattroff(back,A_BOLD); mvwaddstr(back,0,9,"ile"); wrefresh(back); wattron(back,A_BOLD); mvwaddch(back,0,24,'E'); wattroff(back,A_BOLD); mvwaddstr(back,0,25,"dit"); wrefresh(back); wattron(back,A_BOLD); mvwaddch(back,0,43,'P'); wattroff(back,A_BOLD); mvwaddstr(back,0,44,"age setup"); wrefresh(back); wattron(back,A_BOLD); mvwaddch(back,0,64,'H'); wattroff(back,A_BOLD); mvwaddstr(back,0,65,"elp"); wrefresh(back); text=newwin(35,COLS-5,2,2); wbkgd(text,COLOR_PAIR(3)); wrefresh(text);
25
A Simple Text Editor status=newwin(1,COLS-1,LINES-1,0); wbkgd(status,COLOR_PAIR(3)); wrefresh(status); wattron(status,A_BOLD); mvwaddstr(status,0,1,"ROW:"); mvwaddstr(status,0,8,"COL:"); wattroff(status,A_BOLD); wrefresh(status); wmove(text,0,0); curpos(); wrefresh(text); } //################# MAIN FUNCTION ################## main() { int s; chtype ch; inscr(); NODE temp,temp1,temp2;int ty,tx;int i=0; WINDOW *sel; noecho(); mousemask(ALL_MOUSE_EVENTS,NULL); first=getnode(); first->ch='_'; last=first; cur=first; while(1) { keypad(text,TRUE); noecho(); ch=wgetch(text); switch(ch) { case 6 : menuselect(1); break; case 5 : menuselect(2); break; case 16 : menuselect(3); break; case 8 : menuselect(4); break; 26
case 27 : if(i){myrefresh();break;} endwin(); exit(0); case 24: cut();src=NULL;des=NULL; break; case 3: copy();src=NULL;des=NULL; break;
case 25:paste();break; case 263 : bspace(); break; 4 : pageend(); break; 21 : pagebegin(); break; 262 : home(); break; 360 : end(); break;
case
case
case
case
case 266:savefile(); break; case 14:newfile();break; case 276:helpmenu(); break; case KEY_LEFT : keyleft(); break; case KEY_RIGHT : keyright(); break; case ty=2,tx=2; void myrefresh() { touchwin(back); wrefresh(back); touchwin(title); wrefresh(title); touchwin(text); 27 KEY_UP : keyup(); break;
A Simple Text Editor wrefresh(text); } void updtxt() { werase(text); wrefresh(text); } //##################### FUNCTION TO SELECT MENUS ################ void menuselect(int i) { switch(i) { case 1 :filemenu(); break; case 2 :editmenu(); break; case 3 :pagemenu(); break; case 4 :helpmenu(); break; case 27:myrefresh(); break; default :break; } } //################ FUNCTION FOR FILEMENU ############## void filemenu() { WINDOW *file,*sh1; int i=1; chtype ch; char c; file=newwin(9,11,2,5); sh1=newwin(9,11,3,6); wbkgd(sh1,COLOR_PAIR(11)); wbkgd(file,COLOR_PAIR(4)); box(file,ACS_VLINE,ACS_HLINE); wattron(file,A_BOLD); mvwaddstr(file,1,2,"New "); mvwaddstr(file,2,2,"Open "); mvwaddstr(file,3,2,"Save "); 28
A Simple Text Editor mvwaddstr(file,4,2,"Save As"); mvwaddstr(file,5,2,"Delete "); mvwaddstr(file,6,2,"Shell "); mvwaddstr(file,7,2,"Exit "); wattron(file,A_STANDOUT); mvwprintw(file,i,2,fil[i-1]); wattroff(file,A_STANDOUT); mvwprintw(file,i+1,2,fil[i]); wmove(file,1,2); wrefresh(sh1); wrefresh(file); keypad(file,TRUE); ch=wgetch(file); while(ch!=27 && ch !='\n') { switch(ch) { case KEY_UP :if(i!=1) { i--; wattron(file,A_STANDOUT); mvwprintw(file,i,2,fil[i-1]); wattroff(file,A_STANDOUT); mvwprintw(file,i+1,2,fil[i]); wrefresh(file); } else { i=7;beep(); wattron(file,A_STANDOUT); mvwprintw(file,7,2,fil[6]); wattroff(file,A_STANDOUT); mvwprintw(file,1,2,fil[0]); wrefresh(file); } break; case KEY_DOWN :if(i!=7) { i++; wattron(file,A_STANDOUT); mvwprintw(file,i,2,fil[i-1]); wattroff(file,A_STANDOUT); mvwprintw(file,i-1,2,fil[i-2]); wrefresh(file); } else 29
A Simple Text Editor { i=1;beep(); wattron(file,A_STANDOUT); mvwprintw(file,1,2,fil[0]); wattroff(file,A_STANDOUT); mvwprintw(file,7,2,fil[6]); wrefresh(file); } break; case KEY_RIGHT : beep(); myrefresh(); menuselect(2); return; KEY_LEFT: beep(); myrefresh(); menuselect(4); return; : break;
case
default }
wmove(file,i,2); wrefresh(file); ch=wgetch(file); } if(ch!='\n') { myrefresh(); return; } switch(i) { case 1:newfile(); break; case 2:openfile(); break; case 3:savefile(); break; case 4:strcpy(fname,"UNTITLED"); savefile(); break; case 5:myrefresh(); 30
A Simple Text Editor deletefile(); myrefresh(); break; case 6: myrefresh(); WINDOW *swin; swin=newwin(3,35,9,20); wbkgd(swin,COLOR_PAIR(1)); box(swin,ACS_VLINE,ACS_HLINE); mvwaddstr(swin,1,2,"..PRESS ANY KEY TO SHELL OUT.."); wrefresh(swin); curs_set(0); chtype ch1; ch1=wgetch(swin); doupdate(); myrefresh(); curs_set(1); def_prog_mode(); endwin(); system("sh"); swin=newwin(3,36,9,20); wbkgd(swin,COLOR_PAIR(1)); box(swin,ACS_VLINE,ACS_HLINE); mvwaddstr(swin,1,2,"...PRESS ANY KEY TO CONTINUE..."); curs_set(0); ch1=wgetch(swin); wrefresh(swin); doupdate(); myrefresh(); curs_set(1); wrefresh(text); break; case 7:myrefresh(); while(1) { c=display(); if(c==27) return; if(c=='Y' || c=='y') { savefile(); endwin(); system("clear"); exit(0); } else if(c=='N'|| c=='n') { endwin(); 31
} void newfile() { myrefresh(); char c; while(1) { c=display(); if(c==27) return; if(c=='Y' || c== 'y') { savefile(); updtxt(); break; } else if(c=='N' || c=='n') { updtxt(); break; } else beep(); } deallocate(); werase(title); wrefresh(title); strcpy(fname,"UNTITLED"); mvwaddstr(title,0,60,fname); wrefresh(title); y=top;x=xl;curx=xl;cury=top; wmove(text,y,x); curpos(); wrefresh(text); first=NULL; cur=NULL; last=NULL; first=getnode(); first->ch='_'; 32
A Simple Text Editor cur=first; last=first; } void openfile() { myrefresh(); char c;int i=0; while(1) { c=display(); if(c==27) return; if(c=='Y' || c=='y') { savefile(); updtxt(); break; } else if(c=='N' || c=='n') { updtxt(); break; } else beep(); } deallocate(); WINDOW *openwin,*notexist; // Open an existing file
openwin=newwin(3,40,9,18); wbkgd(openwin,COLOR_PAIR(1)); box(openwin,ACS_VLINE,ACS_HLINE); echo(); wattron(openwin,A_BOLD); mvwaddstr(openwin,1,4,"FILENAME TO OPEN :"); wattroff(openwin,A_BOLD); wrefresh(openwin); mvwscanw(openwin,1,23,"%s",fname); mvwaddstr(openwin,1,23,fname); myrefresh(); noecho(); FILE *fp; fp=fopen(fname,"r"); if(!fp) { notexist=newwin(3,35,9,23); wbkgd(notexist,COLOR_PAIR(1)); 33
A Simple Text Editor box(notexist,ACS_VLINE,ACS_HLINE); echo(); wattron(notexist,A_BOLD); mvwaddstr(notexist,1,4,"!!! FILE DOES NOT EXIST !!!"); wattroff(notexist,A_BOLD); wrefresh(notexist); for(i=0;i<10000;i++); myrefresh(); strcpy(fname,"UNTITLED"); werase(title); mvwaddstr(title,0,60,fname); wrefresh(title); x=xl;curx=xl;y=top;cury=top; wmove(text,y,x); curpos(); wrefresh(text); first=NULL; cur=NULL; last=NULL; first=getnode(); first->ch='_'; cur=first; last=first; return; } myrefresh(); werase(title); wrefresh(title); mvwaddstr(title,0,60,fname); wrefresh(title); fclose(fp); loadfile(); } void loadfile() { char c; entercount=0; x=xl; curx=xl; y=top; cury=top; FILE *fp; fp=fopen(fname,"r"); if((c=fgetc(fp))==EOF) { // Load a file
34
A Simple Text Editor updtxt(); wmove(text,y,x); curpos(); wrefresh(text); return; } NODE front=NULL,front1=NULL,tempr=NULL,tempr1=NULL; front=getnode(); front->ch=c; front1=front; while((c=fgetc(fp))!=EOF) { if(c=='\n') linecount++; tempr=getnode(); tempr->ch=c; front1->next=tempr; tempr->prev=front1; front1=front1->next; } tempr1=getnode(); tempr1->ch='_'; tempr->next=tempr1; tempr1->prev=tempr; last=tempr1; cur=front; first=front; fclose(fp); updtxt(); updscr(first); x=xl;curx=xl;y=top;cury=top; wmove(text,y,x); curpos(); wrefresh(text); } //######### FUNCTION FOR EDITMENU ######### void editmenu() { WINDOW *edit,*sh2; int i=1; chtype ch; edit=newwin(11,14,2,20); sh2=newwin(11,14,3,21); wbkgd(sh2,COLOR_PAIR(11)); 35
A Simple Text Editor wbkgd(edit,COLOR_PAIR(4)); box(edit,ACS_VLINE,ACS_HLINE); keypad(edit,TRUE); wattron(edit,A_BOLD); mvwaddstr(edit,1,2,"Ins Word"); mvwaddstr(edit,2,2,"Ins Line"); mvwaddstr(edit,3,2,"Delete Word"); mvwaddstr(edit,4,2,"Delete Line"); mvwaddstr(edit,5,2,"Cut"); mvwaddstr(edit,6,2,"Copy"); mvwaddstr(edit,7,2,"Paste"); mvwaddstr(edit,8,2,"Find"); mvwaddstr(edit,9,2,"Replace"); wattron(edit,A_STANDOUT); mvwprintw(edit,i,2,edi[i-1]); wattroff(edit,A_STANDOUT); mvwprintw(edit,i+1,2,edi[i]); wmove(edit,1,2); wrefresh(sh2); wrefresh(edit); ch=wgetch(edit); while(ch!='\n') { switch(ch) { case 27 :myrefresh(); wmove(text,y,x); curpos(); wrefresh(text); return; case KEY_UP :if(i!=1) { i--; wattron(edit,A_STANDOUT); mvwprintw(edit,i,2,edi[i-1]); wattroff(edit,A_STANDOUT); mvwprintw(edit,i+1,2,edi[i]); wrefresh(edit); } else { i=9;beep(); wattron(edit,A_STANDOUT); mvwprintw(edit,10,2,edi[9]); wattroff(edit,A_STANDOUT); mvwprintw(edit,1,2,edi[0]); 36
A Simple Text Editor wrefresh(edit); } break; case KEY_DOWN :if(i!=9) { i++; wattron(edit,A_STANDOUT); mvwprintw(edit,i,2,edi[i-1]); wattroff(edit,A_STANDOUT); mvwprintw(edit,i-1,2,edi[i-2]); wrefresh(edit); } else { i=1;beep(); wattron(edit,A_STANDOUT); mvwprintw(edit,1,2,edi[0]); wattroff(edit,A_STANDOUT); mvwprintw(edit,10,2,edi[9]); wrefresh(edit); } break; case KEY_RIGHT:beep(); myrefresh(); menuselect(3); return; KEY_LEFT: beep(); myrefresh(); menuselect(1); return; : break;
case
default }
A Simple Text Editor { case 1:myrefresh(); insword(); break; case 2:myrefresh(); insline(); break; case 3:myrefresh(); delword(); break; case 4:myrefresh(); delline(); break; case 5:myrefresh(); cut();src=NULL;des=NULL; break; case 6 :myrefresh(); copy();src=NULL;des=NULL; break; case 7 :myrefresh(); paste(); break; case 8:myrefresh(); find(); break; case 9:myrefresh(); replace(); myrefresh(); break; default :break; } } //######### FUNCTION TO INSERT A CHARACTER ########## void inschar(chtype ch) { int i,c,x0,y0,x1,y1; //c:return value for wclrtoeol NODE temp=NULL,temp1=NULL,temp2=NULL; //1 if(first->next==NULL) { first->ch=ch; cur=getnode(); cur->ch='_'; 38
A Simple Text Editor first->next=cur; cur->prev=first; last=cur; if(ch!='\n') { mvwaddch(text,y,x,ch); x++; curx++; curpos(); wmove(text,y,x); wrefresh(text); return; } else { rightcount=0; y++; cury++; maxy++; wmove(text,y,x); curpos(); wrefresh(text); return; } } //e1 //2 if(last==cur) { last->ch=ch; cur=getnode(); cur->ch='_'; cur->prev=last; last->next=cur; last=cur; if(ch!='\n') { if(curx>=74) { curx++; curs_set(0); rightcount=curx-74; horizonscroll(); curpos(); curs_set(1); x=74; wmove(text,y,x); 39
A Simple Text Editor wrefresh(text); return; } //cur=last,first screen mvwaddch(text,y,x,ch); x++; curx++; curpos(); wmove(text,y,x); wrefresh(text); return; } else { rightcount=0; if(y==bot) { entercount++; int e=scrollok(text,TRUE); int m=scroll(text); x=xl; curx=xl; cury++; wmove(text,y,x); curpos(); wrefresh(text); return; } if(curx>=74) { y0=y; updtxt(); updscr(first); curx=xl; cury++; y=y0; y++; x=xl; wmove(text,y,x); wrefresh(text); curpos(); return; } //cur=last, if\n, first screen y++; cury++; maxy++; x=xl; curx=xl; wmove(text,y,x); 40
A Simple Text Editor curpos(); wrefresh(text); return; } } //end of seconnd condition //3 if(cur==first) { rightcount=0; temp2=getnode(); temp2->ch=ch; temp2->next=cur; cur->prev=temp2; first=temp2; cur=temp2; x0=x; y0=y; updtxt(); updscr(first); x=x0;y=y0; wmove(text,y,x); curpos(); wrefresh(text); return; } //end of third condition x0=x;y0=y; c=wclrtoeol(text); while(temp!=last && temp->ch!='\n') { mvwaddch(text,y0,x0,temp->ch); x0++; temp=temp->next; } curx++; x++; wmove(text,y,x); curpos(); wrefresh(text); return; } else //if '\n' { rightcount=0; entercount=(cury-y); NODE tfirst=vscrollup(); x0=x;y0=y; 41
A Simple Text Editor updtxt(); updscr(tfirst); x=xl; if(y0!=bot) y=y0+1; else y=y0; curx=xl; cury++; wmove(text,y,x); curpos(); wrefresh(text); } }
//############ FUNCTION FOR PAGE SETUP MENU ############ void pagemenu() { WINDOW *page,*sh3; int i=1; chtype ch; page=newwin(7,20,2,39); sh3=newwin(7,20,3,40); wbkgd(sh3,COLOR_PAIR(11)); wbkgd(page,COLOR_PAIR(4)); box(page,ACS_VLINE,ACS_HLINE); keypad(page,TRUE); wattron(page,A_BOLD); mvwaddstr(page,1,2,"Left Margin :"); mvwaddstr(page,2,2,"Right Margin :"); mvwaddstr(page,3,2,"Begin"); mvwaddstr(page,4,2,"End"); mvwaddstr(page,5,2,"Change Color"); wattron(page,A_STANDOUT); mvwprintw(page,i,2,pag[i-1]); wattroff(page,A_STANDOUT); mvwprintw(page,i+1,2,pag[i]); wmove(page,1,2); wrefresh(sh3); wrefresh(page); ch=wgetch(page); while(ch!='\n') { switch(ch) 42
27:myrefresh(); wmove(text,y,x); curpos(); wrefresh(text); return; case KEY_UP :if(i!=1) { i--; wattron(page,A_STANDOUT); mvwprintw(page,i,2,pag[i-1]); wattroff(page,A_STANDOUT); mvwprintw(page,i+1,2,pag[i]); wrefresh(page); } else { i=5;beep(); wattron(page,A_STANDOUT); mvwprintw(page,5,2,pag[4]); wattroff(page,A_STANDOUT); mvwprintw(page,1,2,pag[0]); wrefresh(page); } break; case KEY_DOWN :if(i!=5) { i++; wattron(page,A_STANDOUT); mvwprintw(page,i,2,pag[i-1]); wattroff(page,A_STANDOUT); mvwprintw(page,i-1,2,pag[i-2]); wrefresh(page); } else { i=1;beep(); wattron(page,A_STANDOUT); mvwprintw(page,1,2,pag[0]); wattroff(page,A_STANDOUT); mvwprintw(page,5,2,pag[4]); wrefresh(page); } break; case KEY_RIGHT:beep(); myrefresh(); menuselect(4); return; case KEY_LEFT:beep(); myrefresh(); 43
A Simple Text Editor menuselect(2); return; default : break; } wmove(page,i,2); wrefresh(page); ch=wgetch(page); } if(i==1) { myrefresh(); WINDOW *lmwin; lmwin=newwin(3,40,9,18); wbkgd(lmwin,COLOR_PAIR(1)); box(lmwin,ACS_VLINE,ACS_HLINE); wrefresh(lmwin); keypad(lmwin,TRUE); echo(); mvwaddstr(lmwin,1,3,"ENTER LEFT MARGIN [0-73]:"); mvwscanw(lmwin,1,29,"%d",&xl); mvwprintw(lmwin,1,29,"%d",xl); noecho(); x=xl; curx=xl; wrefresh(lmwin); } if(i==2) { myrefresh(); WINDOW *rmwin; rmwin=newwin(3,40,9,18); wbkgd(rmwin,COLOR_PAIR(1)); box(rmwin,ACS_VLINE,ACS_HLINE); wrefresh(rmwin); echo(); mvwaddstr(rmwin,1,3,"ENTER RIGHT MARGIN[0-74]:"); mvwscanw(rmwin,1,30,"%d",&xr); mvwprintw(rmwin,1,30,"%d",xr); noecho(); wrefresh(rmwin); } if(i==3) { myrefresh(); WINDOW *tmwin; tmwin=newwin(3,40,9,18); wbkgd(tmwin,COLOR_PAIR(1)); 44
A Simple Text Editor box(tmwin,ACS_VLINE,ACS_HLINE); wrefresh(tmwin); echo(); mvwaddstr(tmwin,1,3,"ENTER TOP MARGIN [0-19]:"); mvwscanw(tmwin,1,28,"%d",&top); mvwprintw(tmwin,1,28,"%d",top); noecho(); y=top; cury=top; wrefresh(tmwin); } if(i==4) { myrefresh(); WINDOW *bmwin; bmwin=newwin(3,40,9,18); wbkgd(bmwin,COLOR_PAIR(1)); box(bmwin,ACS_VLINE,ACS_HLINE); wrefresh(bmwin); echo(); mvwaddstr(bmwin,1,3,"ENTER BOTTOM MARGIN [0-20]:"); mvwscanw(bmwin,1,31,"%d",&bot); mvwprintw(bmwin,1,31,"%d",bot); noecho(); wrefresh(bmwin); } if(i==5) { myrefresh(); WINDOW *mwin=newwin(11,41,9,18); wbkgd(mwin,COLOR_PAIR(1)); box(mwin,ACS_VLINE,ACS_HLINE); wrefresh(mwin); mvwaddstr(mwin,1,3,"1-Text:Black,Background:White"); mvwaddstr(mwin,2,3,"2-Text:Black,Background:Red"); mvwaddstr(mwin,3,3,"3-Text:Black,Background:Yellow"); mvwaddstr(mwin,4,3,"4-Text:Black,Background:Green"); mvwaddstr(mwin,5,3,"5-Text:Black,Background:Magenta"); mvwaddstr(mwin,6,3,"6-Text:White,Background:Black"); mvwaddstr(mwin,7,3,"Other-Text:White,Background:Blue"); mvwaddstr(mwin,8,3,"Your Choice:"); echo(); int c; mvwscanw(mwin,8,15,"%d",&c); mvwprintw(mwin,8,15,"%d",c); noecho(); changcol(c); wrefresh(mwin); myrefresh(); } 45
A Simple Text Editor if(ch!='\n') { myrefresh(); return; } switch(i) { case 1: case 2: case 3: case 4:myrefresh(); updtxt(); updline(); break; default :break; } } /######### FUNCTION TO REACH BEGNINING OF A PAGE ######### void pagebegin() { cur=first; updtxt(); updscr(first); x=xl;y=top;curx=xl;cury=top; wmove(text,y,x); curpos(); wrefresh(text); } //######## FUNCTION TO REACH END OF A PAGE ####### void pageend() { int i; while(cur->next!=NULL) { if(cur->ch=='\n') { if(y!=bot) y++; cury++; x=xl; curx=xl; } else 46
A Simple Text Editor { x++; curx++; } cur=cur->next; } int cury0=cury; int curx0=curx; while(cur->prev->ch!='\n' && cur->prev!=NULL) cur=cur->prev; if(cur==first) { wmove(text,y,x); curpos(); return; } for(i=1;i<=20;i++) { keyup(); if(cur==first) break; } updtxt(); updscr(cur); cur=last; curx=curx0; cury=cury0; wmove(text,y,x); curpos(); wrefresh(text); } void savefile() { myrefresh(); NODE temp=first; FILE *fp; WINDOW *savewin; while(1) { if(strcmp(fname,"UNTITLED")==0) { myrefresh(); savewin=newwin(3,40,9,19); wbkgd(savewin,COLOR_PAIR(1)); box(savewin,ACS_VLINE,ACS_HLINE); echo(); wattron(savewin,A_BOLD); mvwaddstr(savewin,1,4,"ENTER THE FILENAME :"); 47 // Save a file
A Simple Text Editor wattroff(savewin,A_BOLD); wrefresh(savewin); mvwscanw(savewin,1,24,"%s",fname); mvwaddstr(savewin,1,24,fname); wrefresh(savewin); noecho(); } fp=fopen(fname,"r"); if(fp) { myrefresh(); if(strcmp(fname,"UNTITLED")==0) { WINDOW *owin; owin=newwin(3,50,9,19); wbkgd(owin,COLOR_PAIR(1)); box(owin,ACS_VLINE,ACS_HLINE); echo(); wattron(owin,A_BOLD); mvwaddstr(owin,1,3,"FILE EXISTS! DO YOU WANT TO OVERWRITE[y/n]:"); wattroff(owin,A_BOLD); wrefresh(owin); char b=wgetch(owin); noecho(); if(b==27) { beep(); return; } if(b=='y') break; else if(b=='n') { fclose(fp); continue; } else beep(); } else break; }} if(fp!=NULL) fclose(fp); myrefresh(); werase(title); wrefresh(title); mvwaddstr(title,0,60,fname); wrefresh(title); wmove(text,y,x); 48
A Simple Text Editor curpos(); wrefresh(text); fp=fopen(fname,"w"); if(first->next==NULL) return; while(temp!=last) { fputc(temp->ch,fp); temp=temp->next; } fclose(fp); return; } //####### FUNCTION TO REPRINT ALL OF THE DATA STRUCTURE ######### void updscr(NODE temp) { x=xl;y=top; if(temp->next==NULL) { beep(); wmove(text,y,x); curpos(); wrefresh(text); return; } while(1) { if(y!=bot+1) { if(temp!=last) { if(temp->ch!='\n') { mvwaddch(text,y,x,temp->ch); temp=temp->next; x++; } else { y++; x=xl; temp=temp->next; } } else break; } else 49
A Simple Text Editor break; } return; } //############# FUNCTION TO MOVE CURSOR RIGHT ################ void keyright() { NODE temp=NULL; int x0,y0; if(cur==last) { beep(); return; } if(x==74 && cur!=last && cur->ch!='\n') { curx++; rightcount=curx-74; cur=cur->next; horizonscroll(); wmove(text,y,x); curpos(); wrefresh(text); return; } if(curx>74 && cur->ch=='\n') { cur=cur->next; y0=y; updtxt(); updscr(first); x=xl; curx=xl; y=++y0; cury++; wmove(text,y,x); curpos(); wrefresh(text); return; } if(cur->ch=='\n' && y==bot) { cur=cur->next; x0=xl;y0=y; temp=cur; entercount++; wmove(text,top,xl); wdeleteln(text); while(temp!=last && temp->ch!='\n') 50
A Simple Text Editor { mvwaddch(text,y0,x0,temp->ch); x0++; temp=temp->next; } x=xl; curx=xl; cury++; wmove(text,y,x); curpos(); wrefresh(text); return; } if(cur->ch=='\n') { cur=cur->next; x=xl; curx=xl; y++; cury++; wmove(text,y,x); wrefresh(text); return; } if(x!=74) x++; curx++; wmove(text,y,x); curpos(); wrefresh(text); cur=cur->next; }
//################## FUNCTION TO SCROLL UP VERTICALLY ############ NODE vscrollup() { NODE tfirst=first; int i; if(entercount==0) return tfirst; for(i=1;i<=entercount;i++) { while(tfirst->ch!='\n') tfirst=tfirst->next; tfirst=tfirst->next; } 51
void cut() // Cut the selected text { NODE temp=cur,temp1=NULL,temp2=NULL,temp3=NULL,temp4=NULL; int x0,y0,lncount=0;int i=0; if(src==NULL||des==NULL) {beep();return;} while(temp!=NULL&&temp!=last&&temp!=des) { str[i++]=temp->ch;temp=temp->next; } str[i++]=des->ch; str[i]='\0'; //cur=src; temp=cur; if(cur==first) temp1=first; else temp1=cur->prev; if(des==last) temp3=last; else temp3=des->next; if(temp1==first && temp3==last) { temp1->next=NULL; last->prev=NULL; first=last; cur=last; if(temp1!=temp) { free(temp1); temp1=NULL; lncount++; free(temp); temp=NULL; lncount++; } else { free(temp1); temp1=NULL; lncount++; } updtxt(); 52
A Simple Text Editor updscr(first); x=xl; curx=xl; y=top; wmove(text,y,x); curpos(); wrefresh(text); return; } else if(temp1==first) { temp=temp1->next; while(temp!=temp3) { temp2=temp;temp=temp->next;free(temp2);} temp1->next=temp3; temp3->prev=temp1; cur=first; updtxt(); updscr(first); x=xl; curx=xl; y=top; wmove(text,y,x); curpos(); wrefresh(text); return; } else if(temp3==last) { cur=last; free(temp); temp=NULL; lncount++; last->prev=NULL; temp1->next=NULL; temp=temp1->prev; temp->next=last; last->prev=temp; free(temp1); temp1=NULL; lncount++; x0=x; y0=y; if(rightcount<=0) { entercount=cury-y; NODE tfirst=vscrollup(); updtxt(); updscr(tfirst); 53
A Simple Text Editor } else { updtxt(); horizonscroll(); } x=x0; y=y0; wmove(text,y,x); curpos(); wrefresh(text); return; } else { temp=src; temp2=NULL; while(temp!=temp3) {if(temp->ch=='\n');y--; temp2=temp; temp=temp->next; free(temp2); } if(temp1==first&&temp3!=last) first=temp3; else if(temp1==first&&temp3==last) first=last; else if(temp1!=first&&temp3==last){ temp1->next=last;last->prev=temp1;} else {temp1->next=temp3;temp3->prev=temp1;} updtxt(); updscr(first); myrefresh(); wrefresh(text); return; } } void paste() //Paste the selected text { int i=0,len,x0,y0; NODE temp=NULL,temp0=NULL,temp1=NULL,temp2=NULL; char c; temp=cur; len=strlen(str); temp0=getnode(); temp0->ch=str[0]; temp1=temp0; 54
A Simple Text Editor for(i=1;i<len;i++) { temp2=getnode(); temp2->ch=str[i]; temp1->next=temp2; temp2->prev=temp1; temp1=temp2; } if(temp==first) { temp1=getnode(); temp1->ch=' '; temp2->next=temp1; temp1->prev=temp2; temp2=temp1; temp2->next=temp; temp->prev=temp2; first=temp0; cur=first; x0=x; y0=y; updtxt(); updscr(first); x=x0; y=y0; wmove(text,y,x); curpos(); wrefresh(text); return; } temp1=getnode(); temp1->ch=' '; temp1->next=temp0; temp0->prev=temp1; temp0=temp1; temp1=getnode(); temp1->ch=' '; temp2->next=temp1; temp1->prev=temp2; temp2=temp1; temp=temp->prev; temp->next=temp0; temp0->prev=temp; cur->prev=temp2; temp2->next=cur;
55
A Simple Text Editor cur=temp0; x0=x; y0=y; int curx0=curx; rightcount=curx-74+len+2; if(rightcount<=0) { entercount=cury-y; NODE tfirst=vscrollup(); updtxt(); updscr(tfirst); x=x0; mvwprintw(text,12,24,"%d",curx); } else { updtxt(); horizonscroll(); x=x0-len-1; } y=y0; curx=curx0; wmove(text,y,x); curpos(); wrefresh(text); return; }
56