Thread: Problem with skipping columns for drawing in a text editor.

  1. #1
    Registered User
    Join Date
    Jun 2021
    Posts
    3

    Problem with skipping columns for drawing in a text editor.

    Hi all, my problem is with e.skipcols in KEY_RIGHT and KEY_END case in the switch statement. Cannot seem to get the math right on skipping columns. It either goes before the end of line or after.
    Please help, the full code is below. Thanks in advance to any help in resolving this issue.

    EDIT: It already skips columns but it goes to far (greater than the length of the current line).

    NOTE: To compile use the given command below.
    Code:
    $ gcc -std=c11 -W -O0 -g -o psedit main.c -lncurses
    main.c
    Last edited by psimonson1988; 06-19-2021 at 07:47 PM. Reason: Added more information

  2. #2
    Registered User
    Join Date
    Jun 2021
    Posts
    3
    Sorry for double positing but I got everything working except the KEY_END case in the switch statement. In KEY_END the e.cx variable (cursor X position) is way off the screen.
    I'll post the new relevant code here...

    Code:
                case KEY_END:
                    startx = editor_getoffset(&e, e.cy + e.skiprows);
                    endx = editor_getoffset(&e, (e.cy + e.skiprows) + 1);
                    if(e.cx < (e.cols - 1) &&
                        (e.cx + e.skipcols) < (endx - startx)) {
                        int len = (endx - startx);
                        int skipcols = (len - 1) / e.cols;
                        e.skipcols = (len >= e.cols ? skipcols: 0);
                        // Problem with the line below.
                        e.cx = len > 0 ? (len - 1) + skipcols : 0;
                        e.dirty = true;
                    }
                break;

  3. #3
    Registered User
    Join Date
    Jun 2021
    Posts
    3
    I fixed it, now it all works. Never mind, I should've looked at my code closer. The code that works perfect is below.

    Code:
                case KEY_END:
                    startx = editor_getoffset(&e, e.cy + e.skiprows);
                    endx = editor_getoffset(&e, (e.cy + e.skiprows) + 1);
                    if((e.cx + e.skipcols) < (e.cols - 1) &&
                        (e.cx + e.skipcols) < (endx - startx)) {
                        int len = (endx - startx) - 1;
                        int skipcols = len - e.cols + 1;
                        int skipcx = (len - skipcols) % e.cols;
    
                        // Calculate how many columns to skip.
                        e.skipcols = (len >= e.cols ? skipcols: 0);
                        // Calculate how many columns left from length.
                        e.cx = len >= e.cols ? skipcx : len > 0 ? len : 0;
                        e.dirty = true;
                    }
                break;
    Thanks anyway.
    PS: Please mark this topic as solved, as I don't know how.

    -Phil
    Last edited by psimonson1988; 06-21-2021 at 09:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-16-2019, 07:47 PM
  2. copying columns from a text file.
    By alienxx in forum C Programming
    Replies: 2
    Last Post: 12-20-2012, 06:42 AM
  3. Error compiling. Problem with my text editor?
    By Sinensis in forum C Programming
    Replies: 3
    Last Post: 09-07-2008, 03:27 PM
  4. Skipping Columns in Text File
    By Vulpecula in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2008, 12:10 PM
  5. Problem drawing Rectangles and Text
    By cram in forum Windows Programming
    Replies: 2
    Last Post: 10-13-2004, 09:51 PM

Tags for this Thread