;;; open-with.el --- Open a GUI system program -*- lexical-binding: t -*-
;; This file is part of mydot.
;; mydot is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, version 3.
;; mydot is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with mydot. If not, see .
;; Copyright (c) 2019-2022, Maciej Barć
;; Licensed under the GNU GPL v3 License
;; Authors: Maciej Barć
;; Created: 27 Jul 2022
;; Version: 0.0.0
;; Keywords: convenience
;; Homepage: https://gitlab.com/xgqt/mydot
;; Package-Requires: ((emacs "24.3"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; Open a GUI system program using current buffer's directory / file path.
;;; Code:
(require 'dired)
(defun open-with--buffer-mode (buffer)
"Return major mode of BUFFER."
(with-current-buffer buffer
major-mode))
(defun open-with--dired-buffer-path (buffer)
"Return the path to current Dired BUFFER's directory."
(with-current-buffer buffer
(dired-current-directory)))
(defun open-with--buffer-path (buffer)
"Return the path to current BUFFER's directory or file."
(cond
((equal 'dired-mode (open-with--buffer-mode buffer))
(open-with--dired-buffer-path buffer))
((buffer-file-name buffer))
(t
(user-error "Buffer is not associated with any directory or file"))))
(defun open-with--execute (executable &rest args)
"Execute EXECUTABLE with ARGS."
(let ((command
(format "%s %s" executable (mapconcat 'identity args " "))))
(start-process-shell-command executable nil command)))
(defun open-with--execute-default (executable)
"Execute EXECUTABLE."
(open-with--execute executable (open-with--buffer-path (current-buffer))))
;;;###autoload
(defun open-with ()
"Open buffer with a chosen program."
(interactive)
(open-with--execute-default (read-string "Program: ")))
;;;###autoload
(defun open-with-dolphin ()
"Open buffer with Dolphin."
(interactive)
(open-with--execute-default "dolphin"))
;;;###autoload
(defun open-with-kate ()
"Open buffer with Kate."
(interactive)
(open-with--execute-default "kate"))
;;;###autoload
(defun open-with-konsole ()
"Open buffer with Konsole."
(interactive)
(open-with--execute "konsole"
"--workdir"
(open-with--buffer-path (current-buffer))))
;; TODO: Does not work
;; ;;;###autoload
;; (defun open-with-anything ()
;; "Open buffer with xdg-open."
;; (interactive)
;; (open-with--execute-default "xdg-open"))
(provide 'open-with)
;;; open-with.el ends here