开发者

How does Vim display :ls results (curses/cooked mode)

开发者 https://www.devze.com 2023-01-17 00:24 出处:网络
When I am inside Vim and type :ls, Vim lists the buffers. Most likely it is going into \"cooked mode\" using def_prog_mode() and endwin(). I\'d like to know how it print the values. The best I\'ve com

When I am inside Vim and type :ls, Vim lists the buffers. Most likely it is going into "cooked mode" using def_prog_mode() and endwin(). I'd like to know how it print the values. The best I've come out with is using system("echo ....") which would be quite laborious. I've tried printf - no effect, and printw.

I need to do the same kind of thing in my apps, and rather than create Windows or popups I would like to list internal information like Vim does.

Here's a sample of what I've tried, from http://gist.github.com/587622

#include <ncurses.h>

// it seems system echo is the only way to print some stuff in cooked mode
// i am trying to figure out how VIM displays the result of :ls

int main()
{       
        initscr();                      /* Start curses mode              */
        printw("Hello World !!! Hit a key\n");    /* Print Hello World              */
        refresh();                      /* Print it on to the real screen */
        getch();
        def_prog_mode();                /* Save the tty modes             */
        endwin();                       开发者_开发知识库/* End curses mode temporarily    */
        int i = 0;
        for (i = 0; i < 5; i++) {
            system("echo inside cooked mode");
        }
        //printf("helllow there\n");
        //system("/bin/ls");              /* Do whatever you like in cooked mode */
        //system("read");
        //system("/bin/sh");              /* Do whatever you like in cooked mode */
        //system("echo hit a key");              /* Do whatever you like in cooked mode */
        //printw("Hit a key buddy\n");    /* Print Hello World              */
        reset_prog_mode();              /* Return to the previous tty mode*/
        getch();
                                        /* stored by def_prog_mode()      */
        refresh();                      /* Do refresh() to restore the    */
                                        /* Screen contents                */
        printw("After cooked mode.\nKey to quit");     /* Back to curses use the full    */
        getch();
        refresh();                      /* capabilities of curses         */
        endwin();                       /* End curses mode                */

        return 0;
}

Compile it using:

gcc -o a.out -lncurses a.c && ./a.out


You probably just need to flush the stdio buffer using fflush. That is, after using printf to write some text, call fflush(stdout) to make sure the text gets sent to the terminal. ncurses is probably calling setbuf or setvbuf to enable block buffered IO to the terminal and this is not considered part of the tty modes saved and restored.


If you call setbuf(stdout, NULL) you don't have to call fflush(stdout) every time...

setbuf is declared in the header stdio.h

0

精彩评论

暂无评论...
验证码 换一张
取 消