You are not logged in.
Hi guys.
I was about to start a brand new project and I want to implement a command line like the vim one.
This program should display the content of a file and then, via commands, I can interact with the file, by serching words, ecc...
Just like vim!
Can you give me ideas about how could i do that?
Thank you!
Last edited by Simooon (2016-01-04 09:16:01)
Offline
This program should display the content of a file and then, via commands, I can interact with the file, by serching words, ecc...
Just
likea copy of vim!
If it's just a C exercise, then we ignore that, but it's still not trivial:
You will need a component that handles the file content via memory buffers, one that checks the user-input for correctness; then a component that parses, interprets and executes the user input (for simple things you might get around building abstract syntax trees by just using C's regex functionality); then a component that displays stuff on the screen: for that, you should either take a look at ncurses, or - if you want to build a graphical user interface - Gtk or Qt (or X11 directly? there's wayland around the corner, though...).
That's about as exact as I can get - I don't know anything about the approaches you have taken.
Online
I was about to start a brand new project and I want to implement a command line like the vim one.
This program should display the content of a file and then, via commands, I can interact with the file, by serching words, ecc...Just like vim!
Hi.
Why are you making this software? To learn C? Because nobody has made this type of application yet? Another reason?
Offline
Because nobody has made this type of application yet?
I hope not, because it has been done. It's called ed.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Why are you making this software? To learn C? Because nobody has made this type of application yet? Another reason?
I'm making this software just for fun, just for programming something. I could have said something different, I just wanted to know how could I make a vim-like command line and i think this example is good for my purpose.
I hope not, because it has been done. It's called ed.
I know that
@ayekat: I didn't take an approach yet. I was asking about how could I do that just for understanding the process. Anyway, thank you for notifying me about ncurses, i was not aware about its existence, i think this could give me some hints about how could i actually do that!
Maybe is the vim-style command line just a scanf only displayed while in command mode? I mean, like a hidden textbox that shows up when a certain key is pressed?
Offline
Maybe is the vim-style command line just a scanf only displayed while in command mode? I mean, like a hidden textbox that shows up when a certain key is pressed?
I think it's rather like fgetc - but AFAIK ncurses has its own functions for reading user input.
Online
You would likely not want to use scanf or fgetc for real cli.
Ncurses does have it's own version of these, but really ncurses is primarily for output. I'd suggest readline.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Maybe is the vim-style command line just a scanf only displayed while in command mode? I mean, like a hidden textbox that shows up when a certain key is pressed?
As Trilby pointed out, readline is probably a good option.
Though, if its for the sake of learning anyway, why don't do it yourself?
I would do and have done it the following way:
Create either a doubly linked list or two singly linked ones (one for the string left of the cursor and for for the string right of the cursor).
Use getchar() / wgetch() and put any read char into the list
When the arrow keys are used (or h[jk]l), move the visual curser and the 'list cursor', similarly for the delete and backspace key
If the cursor is not at the end when a new char is entered or deleted, reprint everything right of the cursor
To recognize both ends of the list, a special terminating element like '\0' on one end is required.
EDIT: Obviously, using hjkl in a command line for moving the cursor does not make any sense.
Last edited by respiranto (2016-01-03 22:11:51)
Offline
Create either a doubly linked list or two singly linked ones (one for the string left of the cursor and for for the string right of the cursor).
Use getchar() / wgetch() and put any read char into the list
When the arrow keys are used (or h[jk]l), move the visual curser and the 'list cursor', similarly for the delete and backspace key
If the cursor is not at the end when a new char is entered or deleted, reprint everything right of the cursor
To recognize both ends of the list, a special terminating element like '\0' on one end is required.
Really thank you!
I tried to think about it but i didn't came up to this process of doing it!
Offline
Really thank you!
I tried to think about it but i didn't came up to this process of doing it!
Glad to hear!
If you regard your problem as [SOLVED], please don't forget to mark it as such by prepending it to the title (of the initial post).
If you should have or later get any specific questions about what I wrote, feel free to contact me via email.
Offline
It might be inspirational to take a look at vis, a relatively new vim-like editor from a programmer of the Suckless community, because it's written with simplicity and straightforwardness in mind. Its editing model is also well documented.
Offline
It might be inspirational to take a look at vis, a relatively new vim-like editor from a programmer of the Suckless community, because it's written with simplicity and straightforwardness in mind. Its editing model is also well documented.
Thank you, i'll take a look
If you should have or later get any specific questions about what I wrote, feel free to contact me via email.
Thank you for your disponibility!
Marked as SOLVED!
Offline