text editor in C

Posted on 2001-07-10
Last Modified: 2013-12-13

I am working a project that requires me to develop a new test editor using C programming language.

This text editor should be able to allow the user to do the ff:
    * insert a new char/string
    * delete a char/line
    * save a file in text format.

I'm not that really proficient in C, but i need to have a starting to point to work with in this project.

Any suggestions/comments will be highly appreciated. But please make them simple.

Thanks in advance.
Question by:ylen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Expert Comment

ID: 6271753
You are probably going to need an array of arrays to store your data in and a global variable to identify where your cursor is in these arrays.  That's your basic data structure and all else followis from that.

Expert Comment

ID: 6271816
Do you have to "wrap" the lines, or can the user just type and type on a single line as long as possible?  That will make a HUGE difference in how you need to store the data.  A fixed 2D array won't work if the lines aren't wrapped.  

If they are wrapped, you will have to check the length of the lines.  When the line reaches the max length (80 chars?) you will have to drop to the next line.  If the user is in INSERT mode (are you keeping up with that?) you will have to shift the entire array of arrays, starting at the bottom line and moving up, to the right by one character.
LVL 22

Expert Comment

ID: 6271910
One way of dealing with variable length lines is to store them sequentially with each line prefaced with its' length.  

Insertion performance can be enhanced by using a doubly linked chain of lines.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6272116
In my early projects I made some editor in TurboC 2.01.
Main structures are:

#define  MAXLINES  2000
#define  MAX_LONG   129
#define  WIN_HIGHT   23
typedef  char *LINESS[MAXLINES];
typedef struct {
     LINESS lines;
      char filename[75];
      int  win_hight;     /* Editor window height */
      int  max_long;      /* Max line length */
      int  insert_status; /* INSERT / OVERWRITE */
      int  caps_status;   /* CAPS LOCK-a ( ON / OFF ) */
      int  last_line;     /* Last line */
      int  temp_row;      /* Current file line */
      int  temp_col;      /* Current file column */
      int  screen_row;     /* Current screen line */
      int  screen_col;     /* Current screen column */
      int  first_row;    /* First visible file line */
      int  last_row;     /* Last visible file line */
      int  first_col;      /* First visible file column */

All data are saved in that structure.
Text is saved in structure LINESS which is array of char pointers.
There is dynamic allocating for each row of text (on maximum line length).

Any questions.

Author Comment

ID: 6274361
Thanks for the replies folks.

I know those ideas are going to be really useful. But I what i really wanted is an editor which is similar to vi.
A simple version of vi, that is.

Say for example, the program name is NewEdit.c. If the user type in 'NewEdit newText.txt', the program should be able to remember that the file has to be named as newTest.txt, and a screen will be open to allow that user to start typing/editing. How do i do this?

It's really the UI that's givinng me a hard time.

Shall wait for your comments/replies.

Thanks again.

Expert Comment

ID: 6274414
You can variable to hold the file name...

void main( int argc, char **argv )
  //assuming the command line you used above was used
  char *fileName = argv[1];


Of course, you may want to thoroughly check out all of the command line arguments to make sure that they didn't enter things in the wrong order.
LVL 24

Expert Comment

ID: 6277659
> It's really the UI that's giving me a hard time.

You may want to also say what is already done (or will be soon) so we need not be redundantly repetetive and can focus better. [not that I good at that part, ;) ]

Can we say you want us focus on the command line interpreter piece? Or the visual editing? We can skip macros, of course. And fancy mass replacements. True?

You may well benefit from having the very last line (or top edge) of your screen be completely reserved for entry of manual commands. This avails you of good visual feedback of the input hitting your code (and text). Consider using the exact opposite of screen (bottom or top) to be simple status display, such as for the currently defined current filename. Current row&column. Etc.

You may want to invent commands. Once they are mapped out, come back and ask on_it.  For example, one command or special key on keyboard can be reserved to move the program focus from command line entry to visual editing. So you really have two Main program functions, that can nearly run complete on their own fairly running standalone.

Once in the visual editing main program, one main new piece is functions utilizing the cursor keys. Now, is this the place where you need the more help? The MadYugoslav structure seems fit to start with for a stripped down VI emulation.
LVL 24

Expert Comment

ID: 6277731
> allow that user to start typing/editing. How do i do this?

1) Fill screen displaying lines retrieved
2) Place cursor on last line for my example of command line entry
3) Enter something like "IHello World" <enter>
4) The screen should now place "Hello World" on very first line, while all other lines scroll down.

In my language, I defined symbol
"I" for insert command
and <enter> to complete it (also part of text entry).

You too must define a minimal command set. I take it you may be confused on the visual handling with mixing of commands, input characters, cursors, etc.

Thus I suggest you first get the commands to work, using less visual technique, but more manageable.

While doing so, your code should increase in value for functions like scrolling and buffering, so you can focus more on UI having confidence that the other scrolling and buffering is already in order (or, have you already done that part?)

Expert Comment

ID: 6283320
Also, which library are you using for screen management ? may I recommend curses (it is available for unix and windows, probably for almost everything else :).

Basically you need some way of displaying characters at specific places on the screen. With just cout it is going to be a huge pain :)

When I did my first text editor I used direct memory access on the video memory (under DOS, about 10 years ago :)

LVL 54

Expert Comment

ID: 7487623

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - PAQ'd and pts removed
Please leave any comments here within the
next seven days.



Accepted Solution

Netminder earned 0 total points
ID: 7518837
Per recommendation, points NOT refunded and question closed.

EE Admin

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question