Skip to content

Commit 6037167

Browse files
committed
Allow controlling the ligature strategy dynamically, per window
Fixes #1574
1 parent 0804447 commit 6037167

File tree

11 files changed

+134
-32
lines changed

11 files changed

+134
-32
lines changed

kitty/boss.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,23 @@ def move_tab_backward(self):
996996
if tm is not None:
997997
tm.move_tab(-1)
998998

999+
def disable_ligatures_in(self, where, strategy):
1000+
if isinstance(where, str):
1001+
windows = ()
1002+
if where == 'active':
1003+
if self.active_window is not None:
1004+
windows = (self.active_window,)
1005+
elif where == 'all':
1006+
windows = self.all_windows
1007+
elif where == 'tab':
1008+
if self.active_tab is not None:
1009+
windows = tuple(self.active_tab)
1010+
else:
1011+
windows = where
1012+
for window in windows:
1013+
window.screen.disable_ligatures = strategy
1014+
window.refresh()
1015+
9991016
def patch_colors(self, spec, cursor_text_color, configured=False):
10001017
if configured:
10011018
for k, v in spec.items():

kitty/cmds.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ def w(func):
7878
'''
7979

8080

81+
def windows_for_payload(boss, window, payload):
82+
if payload.get('all'):
83+
windows = tuple(boss.all_windows)
84+
else:
85+
windows = (window or boss.active_window,)
86+
if payload.get('match_window'):
87+
windows = tuple(boss.match_windows(payload['match_window']))
88+
if not windows:
89+
raise MatchError(payload['match_window'])
90+
if payload.get('match_tab'):
91+
tabs = tuple(boss.match_tabs(payload['match_tab']))
92+
if not tabs:
93+
raise MatchError(payload['match_tab'], 'tabs')
94+
for tab in tabs:
95+
windows += tuple(tab)
96+
97+
8198
# ls {{{
8299
@cmd(
83100
'List all tabs/windows',
@@ -711,20 +728,7 @@ def cmd_set_colors(global_opts, opts, args):
711728

712729
def set_colors(boss, window, payload):
713730
from .rgb import color_as_int, Color
714-
if payload['all']:
715-
windows = tuple(boss.all_windows)
716-
else:
717-
windows = (window or boss.active_window,)
718-
if payload['match_window']:
719-
windows = tuple(boss.match_windows(payload['match_window']))
720-
if not windows:
721-
raise MatchError(payload['match_window'])
722-
if payload['match_tab']:
723-
tabs = tuple(boss.match_tabs(payload['match_tab']))
724-
if not tabs:
725-
raise MatchError(payload['match_tab'], 'tabs')
726-
for tab in tabs:
727-
windows += tuple(tab)
731+
windows = windows_for_payload(boss, window, payload)
728732
if payload['reset']:
729733
payload['colors'] = {k: color_as_int(v) for k, v in boss.startup_colors.items()}
730734
payload['cursor_text_color'] = boss.startup_cursor_text_color
@@ -815,6 +819,36 @@ def set_background_opacity(boss, window, payload):
815819
# }}}
816820

817821

822+
# disable_ligatures {{{
823+
@cmd(
824+
'Control ligature rendering',
825+
'Control ligature rendering for the specified windows/tabs (defaults to active window). The STRATEGY'
826+
' can be one of: never, always, cursor',
827+
options_spec='''\
828+
--all -a
829+
type=bool-set
830+
By default, ligatures are only affected in the active window. This option will
831+
cause ligatures to be changed in all windows.
832+
833+
''' + '\n\n' + MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t'),
834+
argspec='STRATEGY'
835+
)
836+
def cmd_disable_ligatures(global_opts, opts, args):
837+
strategy = args[0]
838+
if strategy not in ('never', 'always', 'cursor'):
839+
raise ValueError('{} is not a valid disable_ligatures strategy'.format('strategy'))
840+
return {
841+
'strategy': strategy, 'match_window': opts.match, 'match_tab': opts.match_tab,
842+
'all': opts.all,
843+
}
844+
845+
846+
def disable_ligatures(boss, window, payload):
847+
windows = windows_for_payload(boss, window, payload)
848+
boss.disable_ligatures_in(windows, payload['strategy'])
849+
# }}}
850+
851+
818852
# kitten {{{
819853
@cmd(
820854
'Run a kitten',

kitty/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ def nth_window(func, rest):
214214
return func, [num]
215215

216216

217+
@func_with_args('disable_ligatures_in')
218+
def disable_ligatures_in(func, rest):
219+
parts = rest.split(maxsplit=1)
220+
if len(parts) == 1:
221+
where, strategy = 'active', parts[0]
222+
else:
223+
where, strategy = parts
224+
if where not in ('active', 'all', 'tab'):
225+
raise ValueError('{} is not a valid set of windows to disable ligatures in'.format(where))
226+
if strategy not in ('never', 'always', 'cursor'):
227+
raise ValueError('{} is not a valid disable ligatures strategy'.format(strategy))
228+
return func, [where, strategy]
229+
230+
217231
def parse_key_action(action):
218232
parts = action.strip().split(maxsplit=1)
219233
func = parts[0]

kitty/config_data.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,13 @@ def disable_ligatures(x):
267267
Choose how you want to handle multi-character ligatures. The default is to
268268
always render them. You can tell kitty to not render them when the cursor is
269269
over them by using :code:`cursor` to make editing easier, or have kitty never
270-
render them at all by using :code:`never`, if you don't like them.
270+
render them at all by using :code:`always`, if you don't like them. The ligature
271+
strategy can be set per-window either using the kitty remote control facility
272+
or by defining shortcuts for it in kitty.conf, for example::
273+
274+
map alt+1 disable_ligatures_in active always
275+
map alt+2 disable_ligatures_in all never
276+
map alt+3 disable_ligatures_in tab cursor
271277
'''))
272278

273279

kitty/data-types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef uint16_t sprite_index;
4040
typedef uint16_t attrs_type;
4141
typedef uint8_t line_attrs_type;
4242
typedef enum CursorShapes { NO_CURSOR_SHAPE, CURSOR_BLOCK, CURSOR_BEAM, CURSOR_UNDERLINE, NUM_OF_CURSOR_SHAPES } CursorShape;
43+
typedef enum { DISABLE_LIGATURES_NEVER, DISABLE_LIGATURES_CURSOR, DISABLE_LIGATURES_ALWAYS } DisableLigature;
4344

4445
#define ERROR_PREFIX "[PARSE ERROR]"
4546
typedef enum MouseTrackingModes { NO_TRACKING, BUTTON_MODE, MOTION_MODE, ANY_MODE } MouseTrackingMode;

kitty/fonts.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ shape(CPUCell *first_cpu_cell, GPUCell *first_gpu_cell, index_type num_cells, hb
744744
group_state.last_gpu_cell = first_gpu_cell + (num_cells ? num_cells - 1 : 0);
745745
load_hb_buffer(first_cpu_cell, first_gpu_cell, num_cells);
746746

747-
if (disable_ligature || OPT(disable_ligatures) == DISABLE_LIGATURES_ALWAYS) {
747+
if (disable_ligature) {
748748
hb_shape(font, harfbuzz_buffer, &no_calt_feature, 1);
749749
} else {
750750
hb_shape(font, harfbuzz_buffer, NULL, 0);
@@ -1015,10 +1015,10 @@ test_shape(PyObject UNUSED *self, PyObject *args) {
10151015
#undef G
10161016

10171017
static inline void
1018-
render_run(FontGroup *fg, CPUCell *first_cpu_cell, GPUCell *first_gpu_cell, index_type num_cells, ssize_t font_idx, bool pua_space_ligature, bool center_glyph, int cursor_offset) {
1018+
render_run(FontGroup *fg, CPUCell *first_cpu_cell, GPUCell *first_gpu_cell, index_type num_cells, ssize_t font_idx, bool pua_space_ligature, bool center_glyph, int cursor_offset, DisableLigature disable_ligature_strategy) {
10191019
switch(font_idx) {
10201020
default:
1021-
shape_run(first_cpu_cell, first_gpu_cell, num_cells, &fg->fonts[font_idx], false);
1021+
shape_run(first_cpu_cell, first_gpu_cell, num_cells, &fg->fonts[font_idx], disable_ligature_strategy == DISABLE_LIGATURES_ALWAYS);
10221022
if (pua_space_ligature) merge_groups_for_pua_space_ligature();
10231023
else if (cursor_offset > -1) {
10241024
index_type left, right;
@@ -1052,21 +1052,18 @@ render_run(FontGroup *fg, CPUCell *first_cpu_cell, GPUCell *first_gpu_cell, inde
10521052
}
10531053

10541054
void
1055-
render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor) {
1055+
render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, DisableLigature disable_ligature_strategy) {
10561056
#define RENDER if (run_font_idx != NO_FONT && i > first_cell_in_run) { \
10571057
int cursor_offset = -1; \
1058-
if (disable_ligature_in_line && first_cell_in_run <= cursor->x && cursor->x <= i) cursor_offset = cursor->x - first_cell_in_run; \
1059-
render_run(fg, line->cpu_cells + first_cell_in_run, line->gpu_cells + first_cell_in_run, i - first_cell_in_run, run_font_idx, false, center_glyph, cursor_offset); \
1058+
if (disable_ligature_at_cursor && first_cell_in_run <= cursor->x && cursor->x <= i) cursor_offset = cursor->x - first_cell_in_run; \
1059+
render_run(fg, line->cpu_cells + first_cell_in_run, line->gpu_cells + first_cell_in_run, i - first_cell_in_run, run_font_idx, false, center_glyph, cursor_offset, disable_ligature_strategy); \
10601060
}
10611061
FontGroup *fg = (FontGroup*)fg_;
10621062
ssize_t run_font_idx = NO_FONT;
10631063
bool center_glyph = false;
1064-
bool disable_ligature_in_line = false;
1064+
bool disable_ligature_at_cursor = cursor != NULL && disable_ligature_strategy == DISABLE_LIGATURES_CURSOR && lnum == cursor->y;
10651065
index_type first_cell_in_run, i;
10661066
attrs_type prev_width = 0;
1067-
if (cursor != NULL && OPT(disable_ligatures) == DISABLE_LIGATURES_CURSOR) {
1068-
if (lnum == cursor->y) disable_ligature_in_line = true;
1069-
}
10701067
for (i=0, first_cell_in_run=0; i < line->xnum; i++) {
10711068
if (prev_width == 2) { prev_width = 0; continue; }
10721069
CPUCell *cpu_cell = line->cpu_cells + i;
@@ -1104,7 +1101,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor)
11041101
center_glyph = true;
11051102
RENDER
11061103
center_glyph = false;
1107-
render_run(fg, line->cpu_cells + i, line->gpu_cells + i, num_spaces + 1, cell_font_idx, true, center_glyph, -1);
1104+
render_run(fg, line->cpu_cells + i, line->gpu_cells + i, num_spaces + 1, cell_font_idx, true, center_glyph, -1, disable_ligature_strategy);
11081105
run_font_idx = NO_FONT;
11091106
first_cell_in_run = i + num_spaces + 1;
11101107
prev_width = line->gpu_cells[i+num_spaces].attrs & WIDTH_MASK;
@@ -1296,7 +1293,7 @@ test_render_line(PyObject UNUSED *self, PyObject *args) {
12961293
PyObject *line;
12971294
if (!PyArg_ParseTuple(args, "O!", &Line_Type, &line)) return NULL;
12981295
if (!num_font_groups) { PyErr_SetString(PyExc_RuntimeError, "must create font group first"); return NULL; }
1299-
render_line((FONTS_DATA_HANDLE)font_groups, (Line*)line, 0, NULL);
1296+
render_line((FONTS_DATA_HANDLE)font_groups, (Line*)line, 0, NULL, DISABLE_LIGATURES_NEVER);
13001297
Py_RETURN_NONE;
13011298
}
13021299

kitty/fonts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ PyObject* face_from_descriptor(PyObject*, FONTS_DATA_HANDLE);
3434

3535
void sprite_tracker_current_layout(FONTS_DATA_HANDLE data, unsigned int *x, unsigned int *y, unsigned int *z);
3636
void render_alpha_mask(uint8_t *alpha_mask, pixel* dest, Region *src_rect, Region *dest_rect, size_t src_stride, size_t dest_stride);
37-
void render_line(FONTS_DATA_HANDLE, Line *line, index_type lnum, Cursor *cursor);
37+
void render_line(FONTS_DATA_HANDLE, Line *line, index_type lnum, Cursor *cursor, DisableLigature);
3838
void sprite_tracker_set_limits(size_t max_texture_size, size_t max_array_len);
3939
typedef void (*free_extra_data_func)(void*);
4040
StringCanvas render_simple_text_impl(PyObject *s, const char *text, unsigned int baseline);

kitty/screen.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
113113
self->alt_grman = grman_alloc();
114114
self->grman = self->main_grman;
115115
self->pending_mode.wait_time = 2.0;
116+
self->disable_ligatures = OPT(disable_ligatures);
116117
self->main_tabstops = PyMem_Calloc(2 * self->columns, sizeof(bool));
117118
if (self->cursor == NULL || self->main_linebuf == NULL || self->alt_linebuf == NULL || self->main_tabstops == NULL || self->historybuf == NULL || self->main_grman == NULL || self->alt_grman == NULL || self->color_profile == NULL) {
118119
Py_CLEAR(self); return NULL;
@@ -1479,7 +1480,7 @@ screen_update_cell_data(Screen *self, void *address, FONTS_DATA_HANDLE fonts_dat
14791480
lnum = self->scrolled_by - 1 - y;
14801481
historybuf_init_line(self->historybuf, lnum, self->historybuf->line);
14811482
if (self->historybuf->line->has_dirty_text) {
1482-
render_line(fonts_data, self->historybuf->line, lnum, self->cursor);
1483+
render_line(fonts_data, self->historybuf->line, lnum, self->cursor, self->disable_ligatures);
14831484
historybuf_mark_line_clean(self->historybuf, lnum);
14841485
}
14851486
update_line_data(self->historybuf->line, y, address);
@@ -1489,7 +1490,7 @@ screen_update_cell_data(Screen *self, void *address, FONTS_DATA_HANDLE fonts_dat
14891490
linebuf_init_line(self->linebuf, lnum);
14901491
if (self->linebuf->line->has_dirty_text ||
14911492
(cursor_has_moved && (self->cursor->y == lnum || self->last_rendered_cursor_y == lnum))) {
1492-
render_line(fonts_data, self->linebuf->line, lnum, self->cursor);
1493+
render_line(fonts_data, self->linebuf->line, lnum, self->cursor, self->disable_ligatures);
14931494
linebuf_mark_line_clean(self->linebuf, lnum);
14941495
}
14951496
update_line_data(self->linebuf->line, y, address);
@@ -1888,6 +1889,37 @@ MODE_GETSET(auto_repeat_enabled, DECARM)
18881889
MODE_GETSET(cursor_visible, DECTCEM)
18891890
MODE_GETSET(cursor_key_mode, DECCKM)
18901891

1892+
static PyObject* disable_ligatures_get(Screen *self, void UNUSED *closure) {
1893+
const char *ans = NULL;
1894+
switch(self->disable_ligatures) {
1895+
case DISABLE_LIGATURES_NEVER:
1896+
ans = "never";
1897+
break;
1898+
case DISABLE_LIGATURES_CURSOR:
1899+
ans = "cursor";
1900+
break;
1901+
case DISABLE_LIGATURES_ALWAYS:
1902+
ans = "always";
1903+
break;
1904+
}
1905+
return PyUnicode_FromString(ans);
1906+
}
1907+
1908+
static int disable_ligatures_set(Screen *self, PyObject *val, void UNUSED *closure) {
1909+
if (val == NULL) { PyErr_SetString(PyExc_TypeError, "Cannot delete attribute"); return -1; }
1910+
if (!PyUnicode_Check(val)) { PyErr_SetString(PyExc_TypeError, "unicode string expected"); return -1; }
1911+
if (PyUnicode_READY(val) != 0) return -1;
1912+
const char *q = PyUnicode_AsUTF8(val);
1913+
DisableLigature dl = DISABLE_LIGATURES_NEVER;
1914+
if (strcmp(q, "always") == 0) dl = DISABLE_LIGATURES_ALWAYS;
1915+
else if (strcmp(q, "cursor") == 0) dl = DISABLE_LIGATURES_CURSOR;
1916+
if (dl != self->disable_ligatures) {
1917+
self->disable_ligatures = dl;
1918+
screen_dirty_sprite_positions(self);
1919+
}
1920+
return 0;
1921+
}
1922+
18911923
static PyObject*
18921924
cursor_up(Screen *self, PyObject *args) {
18931925
unsigned int count = 1;
@@ -2238,6 +2270,7 @@ static PyGetSetDef getsetters[] = {
22382270
GETSET(focus_tracking_enabled)
22392271
GETSET(cursor_visible)
22402272
GETSET(cursor_key_mode)
2273+
GETSET(disable_ligatures)
22412274
{NULL} /* Sentinel */
22422275
};
22432276

kitty/screen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ typedef struct {
105105
int state;
106106
uint8_t stop_buf[32];
107107
} pending_mode;
108+
DisableLigature disable_ligatures;
108109

109110
} Screen;
110111

kitty/shaders.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ cell_prepare_to_render(ssize_t vao_idx, ssize_t gvao_idx, Screen *screen, GLfloa
293293

294294
bool cursor_pos_changed = screen->cursor->x != screen->last_rendered_cursor_x
295295
|| screen->cursor->y != screen->last_rendered_cursor_y;
296-
bool disable_ligatures = OPT(disable_ligatures) == DISABLE_LIGATURES_CURSOR;
296+
bool disable_ligatures = screen->disable_ligatures == DISABLE_LIGATURES_CURSOR;
297297

298298
if (screen->scroll_changed || screen->is_dirty || (disable_ligatures && cursor_pos_changed)) {
299299
sz = sizeof(GPUCell) * screen->lines * screen->columns;

0 commit comments

Comments
 (0)