From a61e4acfc7ff753592f9ce8e6686a0cff4547f78 Mon Sep 17 00:00:00 2001 From: nadrad Date: Mon, 26 Sep 2022 17:09:45 +0200 Subject: [PATCH] added internal, file, and command clipboard types * `--clipboard=os` uses xclip and similar applications (default). * `--clipboard=internal` uses an internal variable as the clipboard without exchanging content with external applications. * `--clipboard=file` along with `--clipboard-file=/path/filename` uses the file as the clipboard. * `--clipboard=command` uses `--clipboard-in-command="command %text%"` to send content and `--clipboard-out-command=command` to receive. --- h-m-m | 75 ++++++++++++++++++++++++++++++++++++++++++------------- readme.md | 14 +++++++++++ 2 files changed, 71 insertions(+), 18 deletions(-) diff --git a/h-m-m b/h-m-m index 245ec42..08732ec 100755 --- a/h-m-m +++ b/h-m-m @@ -36,7 +36,7 @@ for ( $i = 1 ; $i < $alen ; $i++ ) if (substr($argv[$i],0,2) == '--') { $a = explode('=', substr($argv[$i],2)); - $mm['arguments'][ str_replace('-','_',$a[0]) ] = ( $a[1] ?? true ); + $mm['arguments'][ str_replace('-','_',$a[0]) ] = trim( $a[1] ?? true, '"' ); } elseif (isset($mm['arguments']['filename'])) { @@ -106,6 +106,13 @@ config($mm, 'active_node_color', "\033[38;5;0m\033[48;5;172m\033[1m"); config($mm, 'message_color', "\033[38;5;0m\033[48;5;141m\033[1m"); config($mm, 'doubt_color', "\033[38;5;168m"); +config($mm, 'clipboard', "os"); +config($mm, 'clipboard_file', "/tmp/h-m-m"); +config($mm, 'clipboard_in_command', ""); +config($mm, 'clipboard_out_command', ""); + +$clipboard = ''; + $mm['changes'] = []; $mm['change_active_node'] = []; @@ -199,17 +206,24 @@ function check_required_extensions() function check_the_available_clipboard_tool(&$mm) { + + if ($mm['clipboard'] == 'file' && !file_exists($mm['clipboard_file'])) + file_put_contents($mm['clipboard_file'],''); + + if ($mm['clipboard'] != 'os') + return; + if (PHP_OS_FAMILY === "Windows") { - $mm['clipboard']['write'] = "clip"; - $mm['clipboard']['read'] = 'powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"'; + $mm['os_clipboard']['write'] = "clip"; + $mm['os_clipboard']['read'] = 'powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"'; return; } if (PHP_OS_FAMILY === "Darwin") { - $mm['clipboard']['write'] = "pbcopy"; - $mm['clipboard']['read'] = 'pbpaste'; + $mm['os_clipboard']['write'] = "pbcopy"; + $mm['os_clipboard']['read'] = 'pbpaste'; return; } @@ -228,18 +242,18 @@ function check_the_available_clipboard_tool(&$mm) switch ($tool) { case 'xclip': - $mm['clipboard']['write'] = 'xclip -selection clipboard'; - $mm['clipboard']['read'] = 'xclip -out -selection clipboard'; + $mm['os_clipboard']['write'] = 'xclip -selection clipboard'; + $mm['os_clipboard']['read'] = 'xclip -out -selection clipboard'; break; case 'xsel': - $mm['clipboard']['write'] = 'xsel --clipboard'; - $mm['clipboard']['read'] = 'xsel --clipboard'; + $mm['os_clipboard']['write'] = 'xsel --clipboard'; + $mm['os_clipboard']['read'] = 'xsel --clipboard'; break; case 'wl-copy': - $mm['clipboard']['write'] = 'wl-copy'; - $mm['clipboard']['read'] = 'wl-paste'; + $mm['os_clipboard']['write'] = 'wl-copy'; + $mm['os_clipboard']['read'] = 'wl-paste'; break; default: @@ -1217,7 +1231,7 @@ function magic_readline(&$mm, $title) } // delete - elseif ($in=="\033\133\63\176") + elseif ($in=="\033\133\063\176") { $title = mb_substr @@ -2107,22 +2121,47 @@ function paste_sub_tree(&$mm, $as_sibling ) function copy_to_clipboard(&$mm, $text) { - $clip = popen($mm['clipboard']['write'],'wb'); - if (!isset($clip)) return; - - fwrite($clip,$text); - pclose($clip); + switch ($mm['clipboard']) + { + case 'os': + $clip = popen($mm['os_clipboard']['write'],'wb'); + if (!isset($clip)) return; + fwrite($clip,$text); + pclose($clip); + break; + case 'internal': + $GLOBALS['clipboard'] = $text; + break; + case 'file': + file_put_contents($mm['clipboard_file'],$text); + break; + case 'command': + shell_exec(str_replace('%text%', '"'.$text.'"', $mm['clipboard_in_command'])); + break; + } } function get_from_clipboard(&$mm) { + + $text = 'clipboard unavailable!'; + + switch ($mm['clipboard']) + { + case 'os': $text = shell_exec($mm['os_clipboard']['read']); break; + case 'internal': $text = $GLOBALS['clipboard']; break; + case 'file': $text = file_get_contents($mm['clipboard_file']); break; + case 'command': $text = shell_exec($mm['clipboard_out_command']); break; + default: $text = "The clipboard type you've set is invalid!"; + } + return mb_ereg_replace ( "[\000-\010\013-\037\177]|".BOM ,'' - ,shell_exec($mm['clipboard']['read']) + ,$text ) ; } diff --git a/readme.md b/readme.md index 5577aa6..676efde 100644 --- a/readme.md +++ b/readme.md @@ -101,6 +101,10 @@ The following are the settings in h-m-m: max_undo_steps = 24 active_node_color = "\033[38;5;0m\033[48;5;172m\033[1m" message_color = "\033[38;5;0m\033[48;5;141m\033[1m" + clipboard = os + clipboard_file = /tmp/h-m-m + clipboard_in_command = "" + clipboard_out_command = "" The colors are ASCII escape codes. @@ -118,6 +122,16 @@ Both underscores and dashes are accepted for the setting keys. When multiple values exists, the highest priority goes to the command line arguments and the lowest to the config file. +# Clipboard + +The normal `os` clipboard works fine for most users, but some users may need other options: + +* `os` uses the global clipboard via xclip and similar tools. +* `internal` uses an internal variable as the clipboard (won't exchange text with external applications). +* `file` uses `/tmp/h-m-m` by default, or aother file set by the `clipboard_file=/path/filename` setting as the clipboard. +* `command` uses `clipboard_in_command="command %text%"` to send content to a shell command and `clipboard_out_command="command"` to read content. + + # Data format Mind maps are stored in plain text files (with `hmm` file extension by default) without metadata. The tree structure is represented by tab indentations; e.g.,