summary refs log tree commit diff stats
path: root/lib/nimrtl.nim
blob: 4e4cf7e0e4d744c8a433cac6369cf7925a10a764 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#
#
#            Nim's Runtime Library
#        (c) Copyright 2010 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Main file to generate a DLL from the standard library.
## The default Nimrtl does not only contain the ``system`` module, but these
## too:
##
## * parseutils
## * strutils
## * parseopt
## * parsecfg
## * strtabs
## * times
## * os
## * osproc
## * unicode
## * pegs
## * ropes
##

when system.appType != "lib":
  {.error: "This file has to be compiled as a library!".}

when not defined(createNimRtl):
  {.error: "This file has to be compiled with '-d:createNimRtl'".}

import
  parseutils, strutils, parseopt, parsecfg, strtabs, unicode, pegs, ropes,
  os, osproc, times
46fdd5dc3a81bf1'>^
719ee428 ^

c72aed38 ^


e961b086 ^
01603dce ^
4511c956 ^
719ee428 ^
3de0cccc ^


b13d739c ^


e696b8b1 ^

372f936f ^

b028afab ^
372f936f ^

b028afab ^
719ee428 ^


4511c956 ^
372f936f ^
719ee428 ^
cc9ac4d1 ^
719ee428 ^


fe9ac5f7 ^
9ee1dd6e ^

cc9ac4d1 ^
cc9ac4d1 ^
719ee428 ^
719ee428 ^
2d414c2b ^
719ee428 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
            



                                                    


                                               
        


                                                                           
                             
                                                             
                              

                          






                                                                           


                                                                             
       








                                                                             
                                                           
                                                             
                   


                                                                               
        
                                                       
                               
         



                                                     
                                             

                                             


                                                                        
                               
                                  
                  
              


                                                                        


                                                                               

                                                                                

                                                                                
                                                    

                                                                              
                 


                                                     
        
                                                     
       
                                                                    


                                                                          
    

                                                                    
         
                
    
                                         
             
                                                           
compilation:
- reduce binary size
	* remove the null columns in EUC-KR decoding
	* fbf for unifont
	* maybe use system wcwidth?
charsets:
- set up some fuzzer
- use appropriate charsets in forms, urls, etc.
display:
- important: buffer list
	* either a buffer list buffer, or a buffer list popup menu. ideally
	  both.
- dark mode (basically max Y)
- override bgcolor ourselves when terminal fails to report it
- allow overriding ansi colors
config:
- important: config editor
- completely replace siteconf; the new solution should:
	* not be based on table arrays
	* allow overriding pretty much every global value per URL
	* allow better URL matching (regexes aren't great for this task)
	* be called url-config
	* allow matching $TERM string, buffer groups (but maybe this should
	  be a separate setting?)
- add per-scheme configuration (e.g. proto.gemini.known-hosts = '/some/path')
- add RPC for CGI scripts e.g. toggle settings/issue downloads/etc
	* also some way to set permissions for RPC calls
buffer:
- important: validate returned values
	* do not block container when receiving buffer data; if invalid, kill
	  buffer
	* this also includes not crashing when the buffer dies while
	  container is reading...
- important: add sandboxing
	* also, other security stuff:
		- configurable/better url filtering in loader
		- make peekCursor show the entire url
- when the log buffer crashes, print its contents to stderr
	* easiest way seems to be to just dump its cache file
- add buffer groups
- xhtml
- better horizontal line handling: allow viewing content positioned before page
  start, handle long lines, etc
network:
- uBO integration? (or at least implement filter lists)
- websockets (curl supports ws)
external:
- history, bookmarks (w3m format)
- save buffer (source, output)
- edit buffer (local file, or whatever we downloaded)
javascript:
- important: callbacks should not leak memory
- add support for JS mixins
- distinguish double from unrestricted double
- better dom support, more events
	* more concretely: get jQuery to work. this needs innerHTML, CSS
	  properties in DOM, ...
- implement ReadableStream, XHR
- separate console for each buffer
- buffer selection
layout engine:
- box borders
	* will probably need special treatment, as borders must round to
	  1ch in x direction and 1em in y direction.
- make table width calculation consistent with FF etc.
	* unfortunately, most websites are designed for auto table layouts
	  where w3m's space distribution algorithm does not work really well :/
- do not break inline boxes with out-of-flow block boxes (float, absolute, etc.)
	* this seems hard to fix properly :(
	* reminder: this does *not* apply to flexbox; in fact it has the inverse
	  problem AFAICT.
- table layout: include caption in width calculation
- flexbox: flex-basis, align-self, align-items, justify-content, proper margin
  handling
- details element
- overflow
- incremental layout & layout caching
	* first for tree generation, then for layout.
- iframe
- writing-mode, grid, ruby, ... (i.e. cool new stuff)
images:
- sixel encoding (eventually also kitty) -> actually display them :P
- more formats (apng, gif: write own decoders, jpeg: use libjpeg, webp: ?)
- incremental decoding (maybe implement streams first?)
- separate image decoder process? or just run on a different thread?
man:
* add a DOM -> man page converter so that we do not depend on pandoc
  for man page conversion
gmifetch:
- rewrite in Nim
etc:
- tests (including aforementioned fuzzer)
- orc support
- maybe windows support? (blocker: needs a windows machine)
le='author James Booth <boothj5@gmail.com> 2012-02-08 23:55:11 +0000 committer James Booth <boothj5@gmail.com> 2012-02-08 23:55:11 +0000 Added seperate window modules' href='/danisanti/profani-tty/commit/input_win.c?id=c8bf654e2cd234bfd35ca97d6994b078b1b9b5ba'>c8bf654e ^
9aa282f6 ^

03b90cf5 ^



f94b6ef0 ^









fe10f2b2 ^










83bd2073 ^



a9ed6491 ^
83bd2073 ^


e6f27de5 ^
83bd2073 ^

a9ed6491 ^
fe10f2b2 ^



83bd2073 ^
fe10f2b2 ^
f94b6ef0 ^


fe10f2b2 ^
b3448eb2 ^

83bd2073 ^
f94b6ef0 ^
b3448eb2 ^
f94b6ef0 ^
83bd2073 ^


b3448eb2 ^
83bd2073 ^









9aa282f6 ^

88de4474 ^
0e8092af ^


404dde28 ^
0f0cd4cd ^
88de4474 ^
99d33228 ^
15a87f0a ^
88de4474 ^



9aa282f6 ^

88de4474 ^
404dde28 ^
88de4474 ^

9aa282f6 ^
2ed78fe5 ^
dae4f1bb ^
0e8092af ^


404dde28 ^
dae4f1bb ^

2ed78fe5 ^





9ecdb394 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  
             
  
                                                            
  














                                                                       











                                                                                

   
                              

                   
                   
                   
                  
 


                              

                              

                    
      
 
                            
                   
                            

                               
                
                
                      
                        
                  
                         
                        
                       
                      
 
                       
 
                                   


                               



                                  
                         
 

                                          










                                         


                               
                                                





















                                                                                       
    





                                 
                         
 




                        
                                
                                              
                                                      
                                   
 

                                            
                                     
                                                   
                          
                         
 
                              

 
    
                    
 

                                
 
                                                          

                                      


                            

     
                                                   
                              

 




















                                                      
                               
 




















                                                     

 

               



                          









                                          










                                                           



                  
                                      


                                                            
                    

     
                                              



                                           
                                
 


                                                         
 

                              
                                            
            
                               
                           


                            
                                              









                                 

                              
 


                         
                              
               
             
                                                         
                         



                       

                  
 
                              

 
    
                   
 


                         
                              

 





                                                                      
 
/*
 * inputwin.c
 *
 * Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#define _XOPEN_SOURCE_EXTENDED
#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <wchar.h>

#include <readline/readline.h>
#include <readline/history.h>

#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#endif

#include "command/command.h"
#include "common.h"
#include "config/accounts.h"
#include "config/preferences.h"
#include "config/theme.h"
#include "log.h"
#include "muc.h"
#include "profanity.h"
#include "roster_list.h"
#include "ui/ui.h"
#include "ui/statusbar.h"
#include "ui/inputwin.h"
#include "ui/windows.h"
#include "xmpp/xmpp.h"

static WINDOW *inp_win;

static struct timeval p_rl_timeout;
static gint inp_timeout = 0;
static gint no_input_count = 0;

static fd_set fds;
static int r;
static gboolean cmd_result = TRUE;

static int pad_start = 0;

static void _inp_win_update_virtual(void);

static void
cb_linehandler(char *line)
{
    if (*line) {
        add_history(line);
    }
    rl_redisplay();
    cmd_result = cmd_process_input(line);
    free(line);
}

int
tab_handler(int count, int key)
{
    if (rl_point != rl_end || !rl_line_buffer) {
        return 0;
    }

    if ((strncmp(rl_line_buffer, "/", 1) != 0) && (ui_current_win_type() == WIN_MUC)) {
        char *result = muc_autocomplete(rl_line_buffer);
        if (result) {
            rl_replace_line(result, 0);
            rl_point = rl_end;
            inp_write(result, rl_point);
        }
    } else if (strncmp(rl_line_buffer, "/", 1) == 0) {
        char *result = cmd_autocomplete(rl_line_buffer);
        if (result) {
            rl_replace_line(result, 0);
            rl_point = rl_end;
            inp_write(result, rl_point);
        }
    }

    return 0;
}

void
resize_signal_handler(int signal)
{
    ui_resize();
}

void
create_input_window(void)
{
#ifdef NCURSES_REENTRANT
    set_escdelay(25);
#else
    ESCDELAY = 25;
#endif
	p_rl_timeout.tv_sec = 0;
    p_rl_timeout.tv_usec = inp_timeout * 1000;
    rl_callback_handler_install(NULL, cb_linehandler);
    rl_bind_key('\t', tab_handler);

    signal(SIGWINCH, resize_signal_handler);

    inp_win = newpad(1, INP_WIN_MAX);
    wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
    keypad(inp_win, TRUE);
    wmove(inp_win, 0, 0);

    _inp_win_update_virtual();
}

void
inp_win_resize(void)
{
    int col = getcurx(inp_win);
    int wcols = getmaxx(stdscr);

    // if lost cursor off screen, move contents to show it
    if (col >= pad_start + wcols) {
        pad_start = col - (wcols / 2);
        if (pad_start < 0) {
            pad_start = 0;
        }
    }

    wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
    _inp_win_update_virtual();
}

static int
offset_to_col(char *str, int offset)
{
    int i = 0;
    int col = 0;
    mbstate_t internal;

    while (i != offset && str[i] != '\n') {
        gunichar uni = g_utf8_get_char(&str[i]);
        size_t ch_len = mbrlen(&str[i], 4, &internal);
        i += ch_len;
        col++;
        if (g_unichar_iswide(uni)) {
            col++;
        }
    }

    return col;
}

void
inp_nonblocking(gboolean reset)
{
    if (! prefs_get_boolean(PREF_INPBLOCK_DYNAMIC)) {
        inp_timeout = prefs_get_inpblock();
        return;
    }

    if (reset) {
        inp_timeout = 0;
        no_input_count = 0;
    }

    if (inp_timeout < prefs_get_inpblock()) {
        no_input_count++;

        if (no_input_count % 10 == 0) {
            inp_timeout += no_input_count;

            if (inp_timeout > prefs_get_inpblock()) {
                inp_timeout = prefs_get_inpblock();
            }
        }
    }
}

void
inp_block(void)
{
    wtimeout(inp_win, -1);
}

void
inp_write(char *line, int offset)
{
    int col = offset_to_col(line, offset);
    werase(inp_win);
    waddstr(inp_win, line);
    wmove(inp_win, 0, col);
    _inp_win_update_virtual();
}

static int
_printable(const wint_t ch)
{
    char bytes[MB_CUR_MAX+1];
    size_t utf_len = wcrtomb(bytes, ch, NULL);
    bytes[utf_len] = '\0';
    gunichar unichar = g_utf8_get_char(bytes);

    return g_unichar_isprint(unichar) && (ch != KEY_MOUSE);
}

gboolean
inp_readline(void)
{
    FD_ZERO(&fds);
    FD_SET(fileno(rl_instream), &fds);
    r = select(FD_SETSIZE, &fds, NULL, NULL, &p_rl_timeout);
    if (r < 0) {
        log_error("Readline failed.");
        return TRUE;
    }

    if (FD_ISSET(fileno(rl_instream), &fds)) {
        if (_printable(rl_executing_key)) {
            cmd_reset_autocomplete();
        }

        rl_callback_read_char();

        if (rl_line_buffer && rl_line_buffer[0] != '/') {
            prof_handle_activity();
        }

        ui_reset_idle_time();
        inp_nonblocking(TRUE);
        inp_write(rl_line_buffer, rl_point);
    } else {
        inp_nonblocking(FALSE);
        prof_handle_idle();
    }

    p_rl_timeout.tv_sec = 0;
    p_rl_timeout.tv_usec = inp_timeout * 1000;

    return cmd_result;
}

void
inp_close(void)
{
    rl_callback_handler_remove();
}

void
inp_get_password(char *passwd)
{
    werase(inp_win);
    wmove(inp_win, 0, 0);
    pad_start = 0;
    _inp_win_update_virtual();
    doupdate();
    noecho();
    mvwgetnstr(inp_win, 0, 1, passwd, MAX_PASSWORD_SIZE);
    wmove(inp_win, 0, 0);
    echo();
    status_bar_clear();
}

void
inp_put_back(void)
{
    _inp_win_update_virtual();
}

void
inp_win_clear(void)
{
    werase(inp_win);
    wmove(inp_win, 0, 0);
    pad_start = 0;
    _inp_win_update_virtual();
}

static void
_inp_win_update_virtual(void)
{
    int wrows, wcols;
    getmaxyx(stdscr, wrows, wcols);
    pnoutrefresh(inp_win, 0, pad_start, wrows-1, 0, wrows-1, wcols-1);
}