Вы находитесь на странице: 1из 56

A Simple Text Editor

CHAPTER-1 PROBLEM UNDERSTANDING 1.1 Problem Statement


To develop a graphical text editor using in-built ncurses library ncurses.h

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

CHAPTER -2 INTRODUCTION TO LINUX


3.1 OPERATING SYSTEM An operating system (OS) is a resource manager. It takes the form of a set of software routines that allow users and application programs to access system resources (e.g. the CPU, memory, disks, modems, printers network cards etc.) in a safe, efficient and abstract way. An OS encourages efficient use of the CPU by suspending programs that are waiting for I/O operations to complete to make way for programs that can use the CPU more productively. An OS also provides convenient abstractions (such as files rather than disk locations) which isolate application programmers and users from the details of the underlying hardware. Linux operating system Linux is a UNIX-based operating system originally developed for an Intel-compatible PC's. It is now available for most types of hardware platforms, ranging from PDAs to mainframes. Linux is a "modern operating system", meaning it has such features as virtual memory, memory protection, and preemptive multitasking. Linux is built and supported by a large international community of developers and users dedicated to free, open-source software. This community sees Linux as an alternative to such proprietary systems as Windows and Solaris and as a platform for alternatives to such proprietary applications as MS Office, Internet Explorer, and Outlook. Linux is an open-source operating system .The Linux operating system is touted for its speed, minimal hardware requirements, security and remote administration. Linux is a fully featured operating system which doesn't have to cost a dime. You download many different distributions directly from the Internet and the support community is huge. Because of Linux's speed and stability it has become the operating system of choice for most types of servers and permeates the entire web market. There are graphical environments (GUIs), office applications, developers' tools, system utilities, business applications, document publishing tools, network client and server applications. Linux specifically refers to the Linux kernel. However, the kernel is useless without a set of tools and applications to run on the kernel. Linux is most commonly distributed with this toolset and a collection of applications in what is called a "distribution". The most common are Red hat, Mandrake, Suse, and Debian. Distributions differ in three basic ways: the process for installing the distribution, the applications available, and process for installing and managing these applications.

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.

A Simple Text Editor

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

3.3:Overview of editing process:


An interactive editor is a software program that allows a user to create and revise a target document. The term document includes objects such as computer programs, text equations, tables, diagrams, line art and photographs anything we find on a hard copy. The text editor should be considered the primary interface to the computers for all types of knowledge workers as they compose, organize, study & manipulate computer based information. The primary elements being edited in a text editor are character strings of the target text. The interactive editing process is accomplished with the following tasks: The part of the target document that needs to be manipulated is viewed. The document is first displayed in the required format. Using the necessary designed tools, the document is modified. The view is updated appropriately and saved

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.

3.4: EDITOR STRUCTURE:


Most text editors have a structure similar to that shown in the figure, regardless of the particular features they offer and the computers on which they are implemented. The command language processor accepts the input from the users input devices, and analyses the tokens and syntactic structure of the commands. In this sense, the command language processor functions much like the lexical and syntactic phases of a compiler. Just as in compiler, the command language processor may invoke semantic routines directly. In a text editor, these semantic routines perform functions such as editing and viewing.

Editing component

Editing buffer Editing Filter Main Memory

Input

Command Language Processor

Travelling component Viewing component


Viewing buffer Viewing filter

Display component Output device File System Fig 3.1:Editor Structure

A Simple Text Editor

CHAPTER-4 SYSTEM DESIGN AND MODELING

4.1: SYSTEM DESIGN


System design refers to formulating a precise description of the desired system in Software Development terms. The information contained in the system specification is similar to that contained in the required documents. The focus is on what functions the Editor must perform, rather than how the Editor performs them. The system specification can be considered as a computer oriented description of the Requirements Documents. This is represented by the a DATA FLOW DIAGRAM (DFD). The documentation for the DFD is as follows:

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

A Simple Text Editor

FILE

BUFFER

EDITOR

USER

DISPLAY
Fig: 4.1: Basic Blocks of the editor

Fig: 4.2 : Block Diagram of the Text Editor

A Simple Text Editor

4.2: Data Structure Used:


A Doubly Linked List (DLL) with a pointer to an array to store the characters has been chosen as the data structure to represent a single line in the file. This data structure has been selected keeping in mind that whenever a line is inserted or deleted, all the nodes above and below the line need not be changed to point to their next/previous lines. This advantage scores over the disadvantage that random access of lines is not possible. Every line is a stored as an array whose address is stored in the pointer field of the node. Here, the fact that random access is not possible is compensated by the advantage of highly efficient insertion and deletion options (they just involve changing of pointers). This is because insertion and deletion, being the most fundamental operation, need more focus than the lack of random access to a particular character. As a whole, the string nodes in the DLL contain two links: one pointing to the previous node and one pointing to the next node and one pointer to the array which stores the characters entered. Diagram representing intermediate representation of the text in text editors:

Node 1 Character 1

Node 2 Character 2

Node 3 Character 3

\0

Fig 4.3: Intermediate Representation of the characters in the editor

A Simple Text Editor

CHAPTER-5 INTRODUCTION TO CURSES


5.1: Introduction:
In the olden days of Teletype terminals, terminals were away from computers and were connected to them through serial cables. Sending a series of bytes to each of them could configure the terminals. All the capabilities (such as moving the cursor to a new location, erasing part of the screen, scrolling the screen, changing modes, changing appearance, colors, brightness, blinking, underlining, reverse video etc.) of terminals could be accessed through these series of bytes that are usually called escape sequences because they start with an escape (0x1B) character. Even today, with proper emulation, we can send escape sequences to the emulator and achieve the same effect on the terminal window. Manipulation of certain display attribute (like appearance, colors, brightness etc) involved a series of complex escape sequences. It was very difficult to comprehend these escape characters. So the designers of UNIX invented a mechanism named term cap. It is a file that lists all the capabilities of a particular terminal, along with the escape sequences needed to achieve a particular effect. In the later years, this was replaced by terminfo which delved a little more into detail with capabilities of a terminal. The terminfo database became difficult to manage and gave birth to 'CURSES' The Curses library forms a wrapper over working with raw terminal codes, and provides highly flexible and efficient API (Application Programming Interface). It provides functions to move the cursor, create windows, produce colors, play with mouse etc. The Application programs need not worry about the underlying terminal capabilities. The curses package is a subroutine library for terminal-independent screen-painting and input-event handling which presents a high level screen model to the programmer, hiding differences between terminal types and doing automatic optimization of output to change one screen full of text into another. Curses uses terminfo, which is a database format that can describe the capabilities of thousands of different terminals. Curses is simpler and less expensive than using an X toolkit. The curses routines are usually located in /usr/lib/curses. The usual procedure for compiling a program in curses is: cc <filename .c> - lncurses [-o file. Out] Where File.c: It is the source program File.out: It is the executable object module. -lncurses option includes the curses library. If -o option is denied, then the executable object module is a. out. All programs must include the file curses.h. This is done with the following statement. # include <ncurses.h> 10

A Simple Text Editor The header file contains window structures definitions, macros, function definitions and prototypes, keyboard definitions, screen attitudes.

5.2: Built-in functions available in Curses.h


(1) initscr ( ); When the routine initscr () is called, it assigns the vertical and horizontal dimensions of the monitor to the two variable LINES and COLS. The value of LINES would be the number of rows and the value of COLS the number of columns. (2) endwin ( ); This function helps the exit from the ETI mode into the default mode. The variables LINES and COLS will then hold no values. Now let us get into some of the important functions in curses. (3) wrefresh ( ); This function plot characters and strings on the physical screen. Curses provide a default screen called the Standard screen or stdscr. Stdscr exists within the memory. It is a virtual screen and is of the same size as the terminal screen addch () only writes a character on to the stdscr. To display on the physical screen refresh ( ) is used. (4) wmove ( ); This function position cursor at specified row and specified column. The number of rows (024) and number of columns (0-7480) are passed as parameter. (5) mvwprintw ( ) ; This function is a combination of move ( ) and a write function. This routine performs two things: --positions the cursors at a particular row and column. --writes a formatted message. This function takes minimum 3 parameters:the row, the column, the string (6) noecho ( ); This function does not echo or move the cursors position at the time of accepting an input. This function blanks out all inputs giving privacy while typing. This can be used for typing password. Cursor control: Using the curses routines, a free format editor program which allow to type in text as well maneuver the cursor up,down,left and right at anytime. The following prog.10 uses most of the routines discussed to achieve cursor control. Attributes: An attribute is any features whose value can be set or read by an appropriate function . Some of the attribute commonly used is Blink, bold ,reverse video ,under lining, highlighting etc. Setting attribute is as easy as demonstrated above and needs so detailed explanation. The following is a list of the rest attributes and what they mean:

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

A Simple Text Editor

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

6.3: Pseudo Code of the project:


The algorithm for the main function is as follows:

Algorithm for main


Begin Initialize the terminal in curses mode Initialize all the variables Initialize all the color pairs Initialize the menu bar edit area, and status bar Initialize the edit menu, file menu, page setup, menu, background and help menu. Disable line buffering Switch off echoing Enable the editing area to read function keys and arrow keys. While TRUE do Begin Move the cursor to the position y and x Print the cury and curx values as the row no., & column no., in status bar Read a character ch from the editing pad If ch is BACK_SPACE then Begin Call the backspace() function Refresh the editing pad Display; the edited file End {if} Else if ch is Delete then Begin Call the delete()function Refresh the screen End Else if ch is <ctrl - F> then Begin Get the command cmd from the file menu option If cmd=1 then Call openNewfile() function If cmd=2 then Begin If saved=0 then Call display()function. else Read the file name Call openfile() function End {if} End {if} If cmd=3 then Begin If filename is not given 14

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

A Simple Text Editor

6.4: Data Structure Used:


The data structure used in my editor program is a doubly linked list with chtype field as the storage medium and the two pointers to point to the previous and the next node respectively. The node structures are: struct node { chtype ch; struct node *prev,*next; }; As the main program starts, initscr() is called which fills in the variables LINES and COLS defined in ncurses.h. The constants TRUE and FALSE are set to 1 and 0 respectively. Characters are inserted using the getnode() function to create a new node which stores the character typed. The new node created will have links to its previous node and as the nodes are created, they link themselves to their predecessor. In effect, a doubly linked list is created. The character index on the screen is maintained in the two variables curx and cury. cury represents the line number and curx, the column number. The variable lines keeps track of the current line on which the cursor is placed. The cursor movements are defined in the functions keyup(), keydown(), keyleft() and keyright(). We traverse along the list using the links either in the left or the right direction at the same time decrementing or incrementing curx. We can also move the cursor up or down, at the same time decrementing or incrementing cury. The menus in the editor are File Edit Pagesetup Help. File contains options to create a new file, open an existing file, to save a file(also save as), and to quit the editor screen. The Edit menu contains insert as well as delete options along with cut, copy, paste and find and replace. The Page Options contains options to indent the given file. The Help menu contains information about the keys used in the editor, the shortcut keys and about the programmer. The implementation details of all the above menus and their associated functions are in the source code .

17

A Simple Text Editor

CHAPTER 7 CONCLUSION AND FUTURE ENHANCEMENTS


7.1: CONCLUSION
Implementing a graphical text editor was very interesting experience and I gained a lot of knowledge about the editors as well as data structures through the project. The graphical Text editor is mainly to create and revise a document. The full screen text editor is designed to satisfy most of the user requirements such as creating a new file, opening an existing file, saving a file, deleting a file. Editing a file is enabled by providing options for inserting or deleting character, word and line, Find and Replace options are also provided along with cut, copy and paste. I have tried to provide as many options as possible with user interface. The Page Margin also can be adjusted. Help options are provided for users, which also mentions the shortcut keys assigned for various frequently used functions. For all these functions to operate the ncurses library present in the Linux platform is a must. The project also has many defects in it. I have tried to implement user interface through mouse. Multitab option is not provided. These defects can be corrected and a more comfortable document creating and revising environment for the user can be provided.

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

A Simple Text Editor

SNAPSHOTS

19

A Simple Text Editor

20

A Simple Text Editor

21

A Simple Text Editor

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

A Simple Text Editor

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];

void inscr() { initscr(); start_color(); init_pair(1,COLOR_CYAN,COLOR_BLUE); init_pair(2,COLOR_BLUE,COLOR_WHITE); init_pair(3,COLOR_WHITE,COLOR_BLUE);

// Initialize the screen

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

A Simple Text Editor

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

A Simple Text Editor system("clear"); exit(0); } else beep(); } default : } break;

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

wmove(edit,i,2); wrefresh(edit); ch=wgetch(edit); } if(ch!='\n') { myrefresh(); return; } switch(i) 37

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

A Simple Text Editor { case

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

A Simple Text Editor return tfirst; }

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

//END OF FULL SCREEN EDITOR CODE

56

Вам также может понравиться