Crafting Text Editor with C: Tips on Efficient Text Handling

  • Thread starter Thread starter Eus
  • Start date Start date
  • Tags Tags
    Text
AI Thread Summary
Efficient text handling in programming, particularly for text editors, is a crucial topic for developers. A common approach involves using a dynamic array instead of a fixed-size character array, which can lead to memory waste and limitations on input size. Dynamic arrays allow for resizing as needed, addressing issues with accommodating longer text inputs. It's important to avoid frequent file I/O operations while typing, as this can slow down performance. Instead, utilizing a memory buffer to temporarily store text can enhance efficiency. Emacs is noted for its effective text handling using buffers, which reside in main memory rather than being written directly to a file. This method allows for quick access and manipulation of text.For further learning, examining the source code of simpler text editors like pico or vim can provide insights into efficient text handling techniques. Starting with even more basic editors, such as ed, may help in understanding foundational concepts without the complexities of terminal interactions.
Eus
Messages
93
Reaction score
0
Hi Ho! ^^

I'm still learning programming in C (I'm still on my first year in Information Technology faculty)
Well, I'm interested in making something from scratch.
Hehehe... right now I'm curious about efficient text handling technique used in a text editor.

When creating a simple text editor, I create a temporary array of char in C.
It is: char *charInput[ 1000 ];
I hope the user doesn't type longer than 999 characters.
Then when the user press ENTER, I'll copy the content of the charInput into a file.
That's my idea to handle the text. I know this is a bad one because:
1. If the user input a text longer than 1000 (e.g. DNA chain code), my charInput can't accommodate it.
2. Time consuming of IO operation when the user press ENTER or ARROW key to navigate the screen because my program always read the file from HD.
3. charInput is such a waste of space because it reserves 1000 bytes in memory although the user only type 3 characters.

From my data structures course, I've an idea to create a linklist for every character which is typed. But this is a bad idea because it will waste more space for the pointer.

Yesterday I browsed through the Internet searching an efficient method for this problem. Unfortunately, I didn't find it.
But I've had some information.

According to you, does Emacs has the most efficient text handling method?
I've read several articles on Emacs and downloaded the source code from FSF.
I wonder how Emacs handles text using its buffer. The structure for buffer in buffer.h only contain some int variables. So, where's the text if it is not written directly to the file? (I read that Emacs buffer is on the main memory, not in HD)
So, could you please give me an information about how actually Emacs handles the text using the buffer?

Do you have any suggestions where I must go to find out more about text handling technique?

Mmm... sorry if I'm not straight to the point but I just want to explain my situation.

Thank you very much for helping me, guys!
 
Last edited:
Computer science news on Phys.org
You will need to use a dynamic array. You are currently creating an array with 1000 holes that can be filled with letters. A better solution is to create an array with one hole and simple add holes as needed. To do this you will need to look into reallocating array space to grow or shrink as needed.

http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/dynamic.html

Also you will want to use a buffer(memory) to store your work. Don't read/write every letter as it is typed to a file because that is very inefficient as you have found out.

Look at the code for the pico text editor. It is much smaller than emacs but still very powerful. You might also try reading the code for vim.

Good luck.
 
I would recommend going even simpler than pico or vim. Basically at first you want to get away from anything even related to a terminal (because this is where things get most complicated). I would start with ed.
http://directory.fsf.org/text/editors/ed.html
 
I came across a video regarding the use of AI/ML to work through complex datasets to determine complicated protein structures. It is a promising and beneficial use of AI/ML. AlphaFold - The Most Useful Thing AI Has Ever Done https://www.ebi.ac.uk/training/online/courses/alphafold/an-introductory-guide-to-its-strengths-and-limitations/what-is-alphafold/ https://en.wikipedia.org/wiki/AlphaFold https://deepmind.google/about/ Edit/update: The AlphaFold article in Nature John Jumper...
Thread 'Urgent: Physically repair - or bypass - power button on Asus laptop'
Asus Vivobook S14 flip. The power button is wrecked. Unable to turn it on AT ALL. We can get into how and why it got wrecked later, but suffice to say a kitchen knife was involved: These buttons do want to NOT come off, not like other lappies, where they can snap in and out. And they sure don't go back on. So, in the absence of a longer-term solution that might involve a replacement, is there any way I can activate the power button, like with a paperclip or wire or something? It looks...

Similar threads

Replies
2
Views
24K
Replies
14
Views
5K
Replies
8
Views
2K
Replies
3
Views
1K
Replies
5
Views
2K
Replies
1
Views
6K
Back
Top