From a32f386b751c70f40e79b03b231e8805584ad78b Mon Sep 17 00:00:00 2001 From: nadrad Date: Thu, 15 Sep 2022 18:37:21 +0200 Subject: [PATCH] non-ascii support for the title editor --- h-m-m | 123 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 95 insertions(+), 28 deletions(-) diff --git a/h-m-m b/h-m-m index e9fab76..011cb5d 100755 --- a/h-m-m +++ b/h-m-m @@ -1088,16 +1088,30 @@ function insert_node(&$mm, $type) function show_line(&$mm, $title, $cursor, $shift) { - $output = - str_pad - ( - substr($title,$shift,$mm['terminal_width']-1), - $mm['terminal_width'] - ) - ; + $output = mb_substr($title,$shift,$mm['terminal_width']-1); + $output .= str_repeat( ' ' ,$mm['terminal_width'] - mb_strlen($output) ); - $output = substr_replace( $output, invert_off, $cursor-$shift , 0); - $output = substr_replace( $output, invert_on, $cursor-$shift-1, 0); + // showing the cursor + $output = + mb_substr + ( + $output + ,0 + ,$cursor-$shift-1 + ) + .invert_on + .mb_substr + ( + $output + ,$cursor-$shift-1 + ,1 + ) + .invert_off + .mb_substr + ( + $output + ,$cursor-$shift + ); put(0,$mm['terminal_height'],$mm['active_node_color'].$output); } @@ -1109,7 +1123,7 @@ function edit_node(&$mm, $rewrite = false) if ($mm['active_node']==0 && $title=='root') $title=''; $in = ''; - $cursor = strlen($title)+1; + $cursor = mb_strlen($title)+1; $shift = max( 0, $cursor - $mm['terminal_width'] ); show_line($mm, $title, $cursor, $shift); @@ -1130,16 +1144,20 @@ function edit_node(&$mm, $rewrite = false) } // up arrow and home - elseif ($in=="\033\133\101" || $in=="\033\133\110") $cursor = 1; + elseif ($in=="\033\133\101" || $in=="\033\133\110") + $cursor = 1; // right arrow - elseif ($in=="\033\133\103") $cursor = min( strlen($title)+1, $cursor+1); + elseif ($in=="\033\133\103") + $cursor = min( mb_strlen($title)+1, $cursor+1); // down arrow and end - elseif ($in=="\033\133\102" || $in=="\033\133\106") $cursor = strlen($title)+1; + elseif ($in=="\033\133\102" || $in=="\033\133\106") + $cursor = mb_strlen($title)+1; // left arrow - elseif ($in=="\033\133\104") $cursor = max(1, $cursor-1); + elseif ($in=="\033\133\104") + $cursor = max(1, $cursor-1); // ctrl+left and shift+left elseif ($in=="\033\133\061\073\065\104" || $in=="\033\133\061\073\062\104") @@ -1150,8 +1168,8 @@ function edit_node(&$mm, $rewrite = false) ( 1, ( - strrpos($title,' ',$cursor-strlen($title)-3) !== false - ? strrpos($title,' ',$cursor-strlen($title)-3) + 2 + mb_strrpos($title,' ',$cursor-mb_strlen($title)-3) !== false + ? mb_strrpos($title,' ',$cursor-mb_strlen($title)-3) + 2 : 1 ) ); @@ -1159,15 +1177,15 @@ function edit_node(&$mm, $rewrite = false) // ctrl+right and shift+right elseif ($in=="\033\133\061\073\065\103" || $in=="\033\133\061\073\062\103") $cursor = - $cursor > strlen($title) -2 - ? strlen($title) + 1 + $cursor > mb_strlen($title) -2 + ? mb_strlen($title) + 1 : min ( - strlen($title)+1, + mb_strlen($title)+1, ( - strpos($title,' ',$cursor+1) !== false - ? strpos($title,' ',$cursor+1) + 2 - : strlen($title) + 1 + mb_strpos($title,' ',$cursor+1) !== false + ? mb_strpos($title,' ',$cursor+1) + 2 + : mb_strlen($title) + 1 ) ); @@ -1185,7 +1203,18 @@ function edit_node(&$mm, $rewrite = false) { if ($cursor>1) { - $title = substr_replace($title, '', $cursor-2, 1); + $title = + mb_substr + ( + $title + ,0 + ,$cursor-2 + ) + .mb_substr + ( + $title + ,$cursor-1 + ); $cursor--; } } @@ -1200,7 +1229,19 @@ function edit_node(&$mm, $rewrite = false) // delete elseif ($in=="\033\133\63\176") { - $title = substr_replace($title, '', $cursor-1, 1); + // $title = substr_replace($title, '', $cursor-1, 1); + $title = + mb_substr + ( + $title + ,0 + ,$cursor-1 + ) + .mb_substr + ( + $title + ,$cursor + ); } // enter @@ -1215,7 +1256,7 @@ function edit_node(&$mm, $rewrite = false) return; } - // pasting + // ctrl+v elseif ($in=="\026") { $content = @@ -1233,16 +1274,42 @@ function edit_node(&$mm, $rewrite = false) ) ) ); - $title = substr_replace($title, $content, $cursor-1, 0); + + $title = + mb_substr + ( + $title + ,0 + ,$cursor-1 + ) + .$content + .mb_substr + ( + $title + ,$cursor-1 + ); + $cursor += mb_strlen($content); } // normal characters - elseif (strlen($in)==1) + else { if ($in=="\011") $in=' '; - $title = substr_replace($title, $in, $cursor-1, 0); + $title = + mb_substr + ( + $title + ,0 + ,$cursor-1 + ) + .$in + .mb_substr + ( + $title + ,$cursor-1 + ); $cursor++; }