You are not logged in.

#1 2016-01-03 16:25:24

Simooon
Member
Registered: 2015-12-30
Posts: 8

[SOLVED]Create a vim-like command line in C

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

#2 2016-01-03 16:53:32

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: [SOLVED]Create a vim-like command line in C

Simooon wrote:

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 a 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.


pkgshackscfgblag

Offline

#3 2016-01-03 16:58:51

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [SOLVED]Create a vim-like command line in C

Simooon wrote:

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. smile

Why are you making this software? To learn C? Because nobody has made this type of application yet? Another reason?

Offline

#4 2016-01-03 17:12:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,443
Website

Re: [SOLVED]Create a vim-like command line in C

drcouzelis wrote:

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

Online

#5 2016-01-03 17:57:19

Simooon
Member
Registered: 2015-12-30
Posts: 8

Re: [SOLVED]Create a vim-like command line in C

drcouzelis wrote:

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.

Trilby wrote:

I hope not, because it has been done.  It's called ed.

I know that smile


@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

#6 2016-01-03 18:07:42

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: [SOLVED]Create a vim-like command line in C

Simooon wrote:

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.


pkgshackscfgblag

Offline

#7 2016-01-03 18:11:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,443
Website

Re: [SOLVED]Create a vim-like command line in C

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

Online

#8 2016-01-03 21:46:20

respiranto
Member
Registered: 2015-05-15
Posts: 479
Website

Re: [SOLVED]Create a vim-like command line in C

Simooon wrote:

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

#9 2016-01-03 22:07:36

Simooon
Member
Registered: 2015-12-30
Posts: 8

Re: [SOLVED]Create a vim-like command line in C

respiranto wrote:
  • 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! smile

Offline

#10 2016-01-03 22:20:23

respiranto
Member
Registered: 2015-05-15
Posts: 479
Website

Re: [SOLVED]Create a vim-like command line in C

Simooon wrote:

Really thank you!
I tried to think about it but i didn't came up to this process of doing it! smile

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

#11 2016-01-04 01:27:35

jsoy9pQbYVNu5nfU
Member
Registered: 2013-04-19
Posts: 108

Re: [SOLVED]Create a vim-like command line in C

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

#12 2016-01-04 09:15:45

Simooon
Member
Registered: 2015-12-30
Posts: 8

Re: [SOLVED]Create a vim-like command line in C

2ion wrote:

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 smile

respiranto wrote:

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! smile


Marked as SOLVED!

Offline

Board footer

Powered by FluxBB