Set WOP_TOP_SELECT options for panel widgets: WPanel, WView and WTree.
[midnight-commander.git] / src / viewer / inlines.h
blobe0d66a39dcc426581fa7cc5ea869479ac1c4319f
1 #ifndef MC__VIEWER_INLINES_H
2 #define MC__VIEWER_INLINES_H
4 #ifdef HAVE_ASSERT_H
5 #include <assert.h>
6 #endif
8 /*** typedefs(not structures) and defined constants **********************************************/
10 /*** enums ***************************************************************************************/
12 /*** structures declarations (and typedefs of structures)*****************************************/
14 /*** global variables defined in .c file *********************************************************/
16 /*** declarations of public functions ************************************************************/
18 /*** inline functions ****************************************************************************/
20 /* difference or zero */
21 static inline off_t
22 mcview_offset_doz (off_t a, off_t b)
24 return (a >= b) ? a - b : 0;
27 /* --------------------------------------------------------------------------------------------- */
29 static inline off_t
30 mcview_offset_rounddown (off_t a, off_t b)
32 #ifdef HAVE_ASSERT_H
33 assert (b != 0);
34 #endif
35 return a - a % b;
38 /* --------------------------------------------------------------------------------------------- */
40 /* difference or zero */
41 static inline screen_dimen
42 mcview_dimen_doz (screen_dimen a, screen_dimen b)
44 return (a >= b) ? a - b : 0;
47 /* --------------------------------------------------------------------------------------------- */
49 /* {{{ Simple Primitive Functions for WView }}} */
50 static inline gboolean
51 mcview_is_in_panel (WView * view)
53 return (view->dpy_frame_size != 0);
56 /* --------------------------------------------------------------------------------------------- */
58 static inline gboolean
59 mcview_may_still_grow (WView * view)
61 return (view->growbuf_in_use && !view->growbuf_finished);
64 /* --------------------------------------------------------------------------------------------- */
66 /* returns TRUE if the idx lies in the half-open interval
67 * [offset; offset + size), FALSE otherwise.
69 static inline gboolean
70 mcview_already_loaded (off_t offset, off_t idx, size_t size)
72 return (offset <= idx && idx - offset < (off_t) size);
75 /* --------------------------------------------------------------------------------------------- */
77 static inline gboolean
78 mcview_get_byte_file (WView * view, off_t byte_index, int *retval)
80 #ifdef HAVE_ASSERT_H
81 assert (view->datasource == DS_FILE);
82 #endif
84 mcview_file_load_data (view, byte_index);
85 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
87 if (retval)
88 *retval = view->ds_file_data[byte_index - view->ds_file_offset];
89 return TRUE;
91 if (retval)
92 *retval = -1;
93 return FALSE;
96 /* --------------------------------------------------------------------------------------------- */
98 static inline gboolean
99 mcview_get_byte (WView * view, off_t offset, int *retval)
101 switch (view->datasource)
103 case DS_STDIO_PIPE:
104 case DS_VFS_PIPE:
105 return mcview_get_byte_growing_buffer (view, offset, retval);
106 case DS_FILE:
107 return mcview_get_byte_file (view, offset, retval);
108 case DS_STRING:
109 return mcview_get_byte_string (view, offset, retval);
110 case DS_NONE:
111 return mcview_get_byte_none (view, offset, retval);
112 default:
113 #ifdef HAVE_ASSERT_H
114 assert (!"Unknown datasource type");
115 #endif
116 return FALSE;
120 /* --------------------------------------------------------------------------------------------- */
122 static inline gboolean
123 mcview_get_byte_indexed (WView * view, off_t base, off_t ofs, int *retval)
125 if (base <= OFFSETTYPE_MAX - ofs)
127 return mcview_get_byte (view, base + ofs, retval);
129 if (retval)
130 *retval = -1;
131 return FALSE;
134 /* --------------------------------------------------------------------------------------------- */
136 static inline int
137 mcview_count_backspaces (WView * view, off_t offset)
139 int backspaces = 0;
140 int c;
141 while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces, &c)
142 && c == '\b')
143 backspaces++;
144 return backspaces;
147 /* --------------------------------------------------------------------------------------------- */
149 static inline gboolean
150 mcview_is_nroff_sequence (WView * view, off_t offset)
152 int c0, c1, c2;
154 /* The following commands are ordered to speed up the calculation. */
156 if (!mcview_get_byte_indexed (view, offset, 1, &c1) || c1 != '\b')
157 return FALSE;
159 if (!mcview_get_byte_indexed (view, offset, 0, &c0) || !g_ascii_isprint (c0))
160 return FALSE;
162 if (!mcview_get_byte_indexed (view, offset, 2, &c2) || !g_ascii_isprint (c2))
163 return FALSE;
165 return (c0 == c2 || c0 == '_' || (c0 == '+' && c2 == 'o'));
168 /* --------------------------------------------------------------------------------------------- */
170 #endif /* MC__VIEWER_INLINES_H */