text editor in C


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.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

NetminderConnect With a Mentor Commented:
Per recommendation, points NOT refunded and question closed.

EE Admin
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.
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.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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.
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.
ylenAuthor Commented:
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.
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.
> 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.
> 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?)
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 :)


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.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.