From 8ba784883c049d5a7b9764612874b75ee7779220 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 28 Jun 2025 12:40:39 -0500 Subject: Add code in system-utils.el to open specific extensions via xdg-open --- modules/system-utils.el | 56 +++++++++++++++++++++++++++++++++++++++++----- todo.org | 59 ------------------------------------------------- 2 files changed, 50 insertions(+), 65 deletions(-) diff --git a/modules/system-utils.el b/modules/system-utils.el index 605a2a1c..33ad679a 100644 --- a/modules/system-utils.el +++ b/modules/system-utils.el @@ -1,17 +1,61 @@ ;;; system-utils --- System-Wide Utilities -*- lexical-binding: t; -*- +;; -------------------------------- Use Xdg-Open ------------------------------- ;; author Craig Jennings - ;;; Commentary: ;;; Code: ;; ------------------------------- Disable Mouse ------------------------------- -;; prevents accidental mouse moves resulting in modifying unwanted text +;; prevents accidental mouse moves resulting in accidentally modifying text (use-package disable-mouse :config (global-disable-mouse-mode)) +;; ---------------------------------- Xdg-Open --------------------------------- +;; open specific file extensions with the system's default mime-type handler + +(defun cj/xdg-open (&optional filename) + (interactive) + (let ((command (cond ((eq system-type 'gnu/linux) "xdg-open") + ((eq system-type 'darwin) "open") + ((eq system-type 'windows-nt) "start") + (t "")))) + (with-current-buffer (get-buffer-create "xdg-open.log") + (goto-char (point-max)) + (insert (format "Running command %s\n" command)) + (let ((exit-code (call-process command nil t t (expand-file-name + (or filename (dired-file-name-at-point)))))) + (insert (format "Exit code: %s\n" exit-code)))))) + +(defun cj/find-file-auto (orig-fun &rest args) + (let ((filename (car args))) + (if (cl-find-if + (lambda (regexp) (string-match regexp filename)) + '("\\.avi\\'" + "\\.mp4\\'" + "\\.divx\\'" + "\\.flv\\'" + "\\.mkv\\'" + "\\.mpeg\\'" + "\\.mov\\'" + "\\.wav\\'" + "\\.webm\\'" + "\\.mp3\\'" + "\\.opus\\'" + "\\.ogg\\'" + "\\.flac\\'" + "\\.docx?\\'" + "\\.pptx?\\'" + "\\.xlsx?\\'" + )) + (progn + (cj/xdg-open filename)) + (progn + (apply orig-fun args))))) + +(advice-add 'find-file :around 'cj/find-file-auto) + ;; ---------------------------------- Ibuffer ---------------------------------- (global-set-key [remap list-buffers] 'ibuffer) ;; use ibuffer, not list-buffers @@ -328,10 +372,10 @@ with a prefix argument." :defer .5 :bind ("M-t" . tmr-prefix-map) :config - (setq tmr-sound-file - "/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga") - (setq tmr-notification-urgency 'normal) - (setq tmr-descriptions-list 'tmr-description-history)) + (setq tmr-sound-file + "/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga") + (setq tmr-notification-urgency 'normal) + (setq tmr-descriptions-list 'tmr-description-history)) ;; ------------------------------- Who Called Me? ------------------------------ ;; convenience function to display which function called a message diff --git a/todo.org b/todo.org index 889eac43..c75f4145 100644 --- a/todo.org +++ b/todo.org @@ -1,61 +1,4 @@ * Emacs Open Work -** TODO [#A] Emacs should open specific file extensions outside emacs -*** Reference Ideas -**** https://www.reddit.com/r/emacs/comments/1cdhum7/how_to_make_emacs_open_certain_files_using/ -(defun my/dired-open() - (interactive) - (let ((filext '("mp4" "rar" "zip" "gz" "epub" "xz" "sig" "pdf" "jpg" "png" "7z"))) - (cond - ((file-directory-p (dired-get-file-for-visit)) - (dired-find-file)) - - ((member (f-ext (dired-get-file-for-visit)) filext) - (call-process "xdg-open" nil nil nil (dired-get-file-for-visit))) - - (t (dired-find-file))))) -**** https://www.reddit.com/r/emacs/comments/1cdhum7/how_to_make_emacs_open_certain_files_using/ -(defun xdg-open (&optional filename) - (interactive) - (let ((process-connection-type)) - (start-process - "" nil (cond ((eq system-type 'gnu/linux) "xdg-open") - ((eq system-type 'darwin) "open") - ((eq system-type 'windows-nt) "start") - (t "")) (expand-file-name - (or filename (dired-file-name-at-point)))))) - -(defun find-file-auto (orig-fun &rest args) - (let ((filename (car args))) - (if (cl-find-if - (lambda (regexp) (string-match regexp filename)) - '("\\.pdf\\'" "\\.mp4\\'" "\\.wav\\'" "\\.avi\\'" "\\.gif\\'" "\\.docx?\\'")) - (xdg-open filename) - (apply orig-fun args)))) - -(advice-add 'find-file :around 'find-file-auto) -**** https://pastebin.com/HShdkFrY -;; Dired "native open": Use the OS-native program for opening the file -;; type from Dired - -(defvar dired-native-open-program nil "The OS-native program for opening files.") - -(cond ((eq system-type 'darwin) - (setq dired-native-open-program "/usr/bin/open")) - ((eq system-type 'gnu/linux) - (setq dired-native-open-program "xdg-open")) - ((eq system-type 'windows-nt) - (setq dired-native-open-program "explorer.exe"))) - -(defun dired-open-with-native-open-program () - "Open the marked files with the program associated with those file types." - (interactive) - (let ((file-list (dired-get-marked-files))) - (dired-run-shell-command - (dired-shell-stuff-it dired-native-open-program file-list t)))) - -(define-key dired-mode-map (kbd "M-o") #'dired-open-with-native-open-program) -(define-key locate-mode-map (kbd "M-o") #'dired-open-with-native-open-program) - ** TODO [#B] Mu4e Working with Proton and Gmail ** TODO [#B] Dirvish should open not showing hidden files by default *** 2025-06-07 Sat @ 12:25:28 -0500 It isn't omitting the h switch on directory listings @@ -65,8 +8,6 @@ No. Didn't help Emacs Alarm seems to sound off periodically after the ** TODO [#B] Add Past Due to F8 Agenda Summary in Emacs ** TODO [#B] Separate B and C in F8 Agenda Summary in Emacs -** TODO [#B] Adjust Eshell Colors -the prompt is pink? ** TODO [#B] Fix Emacs Org Roam Recipe Error C-c n i then type a recipe name. org-element--get-category: Invalid function: org-element-with-disabled-cache -- cgit v1.2.3