From 3e39cc5660e58ea868608ed396abe2db25611b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?=
Date: Thu, 20 Nov 2025 20:49:14 +0100 Subject: [PATCH] Add and integrate sdn-open Originally I thought that not supporting %cd would be an issue, making this kind of utility unclean. It turns out the desire to launch xdg-open quickly is stronger. --- CMakeLists.txt | 4 ++-- LICENSE | 2 +- NEWS | 5 ++++- sdn-open | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ sdn-open.1 | 23 ++++++++++++++++++++++ sdn.cpp | 20 +++++++++++++------ 6 files changed, 96 insertions(+), 10 deletions(-) create mode 100755 sdn-open create mode 100644 sdn-open.1 diff --git a/CMakeLists.txt b/CMakeLists.txt index d0a6042..7e62183 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,9 +33,9 @@ include (GNUInstallDirs) # sdn-mc-ext should be in libexec, but we prefer it in PATH. install (TARGETS sdn sdn-mc-ext DESTINATION ${CMAKE_INSTALL_BINDIR}) -install (PROGRAMS sdn-install sdn-view +install (PROGRAMS sdn-install sdn-open sdn-view DESTINATION ${CMAKE_INSTALL_BINDIR}) -install (FILES sdn.1 sdn-install.1 sdn-view.1 +install (FILES sdn.1 sdn-install.1 sdn-open.1 sdn-view.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) install (FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR}) diff --git a/LICENSE b/LICENSE index 7511f3e..689d036 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2017 - 2024, Přemysl Eric Janouch
+Copyright (c) 2017 - 2025, Přemysl Eric Janouch
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. diff --git a/NEWS b/NEWS index 54e2935..1ac3c33 100644 --- a/NEWS +++ b/NEWS @@ -5,7 +5,7 @@ Unreleased - + and - adjust the selection using shell globs; - t and T insert the selection into the external command line in relative or absolute form, respectively; - - Enter is like t but enters directories, and M-Enter is synonymous to t; + - Enter is like t but enters directories; - C-g or Escape clear the selection, similarly to the editor. * Added an sdn-view script that can process Midnight Commander mc.ext.ini files @@ -13,6 +13,9 @@ Unreleased while the original direct pager invocation has been moved to F13 (which also reflects Midnight Commander) + * Added an sdn-open script which does the same kind of processing as above + on top of xdg-open. This is what is now executed by M-Enter. + 1.0.0 (2024-12-21) diff --git a/sdn-open b/sdn-open new file mode 100755 index 0000000..7252488 --- /dev/null +++ b/sdn-open @@ -0,0 +1,52 @@ +#!/bin/sh -e +# sdn-open: an opener for sdn that makes use of Midnight Commander configuration +# to make more kinds of files directly openable + +if [ "$#" -ne 1 ] +then + echo "Usage: $0 FILE" >&2 + exit 2 +fi + +# This handles both MC_DATADIR and odd installation locations. +datadir= +if command -v mc >/dev/null +then datadir=$(mc --datadir | sed 's/ (.*)$//') +fi + +config= +for dir in "$HOME"/.config/mc "$datadir" /etc/mc +do + if [ -n "$dir" -a -f "$dir/mc.ext.ini" ] + then + config=$dir/mc.ext.ini + break + fi +done + +# This is often used in %env{} expansion, so let's be on the same page. +export PAGER=${PAGER:-less} + +export MC_EXT_FILENAME=$(realpath "$1") +export MC_EXT_BASENAME=$(basename "$1") +export MC_EXT_CURRENTDIR=$(dirname "$MC_EXT_FILENAME") +output=$(sdn-mc-ext <"$config" "$(file -Lbz "$1")" \ + "$MC_EXT_FILENAME" "$MC_EXT_BASENAME" "$MC_EXT_CURRENTDIR" Open || :) +kind=$(echo "$output" | sed -n 1p) +command=$(echo "$output" | sed -n 2p) + +case "$kind" in +cd) + # These mostly enter virtual filesystems, which we do not understand. + xdg-open "$MC_EXT_FILENAME" + ;; +'') + if [ -n "$command" ] + then eval "$command" + else xdg-open "$MC_EXT_FILENAME" + fi + ;; +*) + echo "Unsupported: $kind" >&2 + exit 1 +esac diff --git a/sdn-open.1 b/sdn-open.1 new file mode 100644 index 0000000..b234329 --- /dev/null +++ b/sdn-open.1 @@ -0,0 +1,23 @@ +.Dd November 20, 2025 +.Dt SDN-OPEN 1 +.Os +.Sh NAME +.Nm sdn-open +.Nd run Midnight Commander open configuration externally +.Sh SYNOPSIS +.Nm sdn-open +.Ar path +.Sh DESCRIPTION +.Nm +invokes +.Xr xdg-open 1 +on the passed filename. +.Pp +If it succeeds in finding a +.Xr mc 1 +.Pa mc.ext.ini +file, it will first process it, and run any matching command instead. +.Sh REPORTING BUGS +Use +.Lk https://git.janouch.name/p/sdn +to report bugs, request features, or submit pull requests. diff --git a/sdn.cpp b/sdn.cpp index 624047a..c5ea7af 100644 --- a/sdn.cpp +++ b/sdn.cpp @@ -1,7 +1,7 @@ // // sdn: simple directory navigator // -// Copyright (c) 2017 - 2024, Přemysl Eric Janouch
+// Copyright (c) 2017 - 2025, Přemysl Eric Janouch
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
@@ -430,8 +430,8 @@ enum { ALT = 1 << 24, SYM = 1 << 25 }; // Outside the range of Unicode
#define CTRL(char) ((char) == '?' ? 0x7f : (char) & 0x1f)
#define ACTIONS(XX) XX(NONE) XX(HELP) XX(QUIT) XX(QUIT_NO_CHDIR) \
- XX(ENTER) XX(CHOOSE) XX(CHOOSE_FULL) XX(VIEW_RAW) XX(VIEW) XX(EDIT) \
- XX(SORT_LEFT) XX(SORT_RIGHT) \
+ XX(ENTER) XX(OPEN) XX(CHOOSE) XX(CHOOSE_FULL) \
+ XX(VIEW_RAW) XX(VIEW) XX(EDIT) XX(SORT_LEFT) XX(SORT_RIGHT) \
XX(SELECT) XX(DESELECT) XX(SELECT_TOGGLE) XX(SELECT_ABORT) \
XX(UP) XX(DOWN) XX(TOP) XX(BOTTOM) XX(HIGH) XX(MIDDLE) XX(LOW) \
XX(PAGE_PREVIOUS) XX(PAGE_NEXT) XX(SCROLL_UP) XX(SCROLL_DOWN) XX(CENTER) \
@@ -453,7 +453,7 @@ static const char *g_action_names[] = {ACTIONS(XX)};
static map