0

Is there a function in ncurses to add a line and move the text after the cursor to that new line? I am using insertln () now and that moves the entire current row down, but I would like behavior closer to vi when hitting enter in the middle of a row.

Thanks!

Kevin Gardenhire
  • 626
  • 8
  • 22
  • See http://man7.org/linux/man-pages/man3/ncurses.3x.html look at the `wmove` function to move the cursor. – Pablo Mar 21 '18 at 01:39
  • I am familiar with wmove to move my logical cursor. This alone does not accomplish what I am trying to do. Am I missing something? I hope so lol – Kevin Gardenhire Mar 21 '18 at 01:42
  • And why does that not accomplish your goal? If I understand you correctly, you are using `insertln` insert a new line but it also moves the cursor. Then you can move the cursor back with `wmove` to the old position. – Pablo Mar 21 '18 at 01:45
  • Sorry, what I meant was that I wanted to be able to have the cursor in the middle of a string and then insert a new line and have all the text after the cursor be moved to the new line. if you open a file in vi and hit enter mid string you will see the behavior that I mean – Kevin Gardenhire Mar 21 '18 at 01:48
  • Ok, are you talking about a buffer where the user can enter text or any position on screen? – Pablo Mar 21 '18 at 01:49
  • correct, I am just making a simple text editor – Kevin Gardenhire Mar 21 '18 at 01:51
  • I don't think there is a ncurses function thatdoes that, it's been a long time since I've programmed my last ncurses app. I guess the best action would be to store all lines in separate buffers and track the position of the cursor. If the uses presses ENTER then you modify `buffer[lineno]` by moving everything after the cursor into `buffer[lineno+1]`, then you do a redraw of the lines/screen. – Pablo Mar 21 '18 at 01:54

1 Answers1

2

You can use instr to read the text currently in the window, then erase it with clrtoeol, and addstr to put the text in the new location. (Add w and/or mv prefixes as needed).

Note that a real text editor doing an "insert newline" will not just be operating on screen lines. It will operate on a logical line in the file buffer, potentially affecting multiple screen lines (if the line in the file is big enough to wrap). If you don't keep your own logical line structure separate from what you're displaying on screen, you aren't really doing a text-editor-like job.