help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Better way to make sure external command exists in the system?


From: Jean Louis
Subject: Re: Better way to make sure external command exists in the system?
Date: Sun, 21 Mar 2021 17:07:18 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Daniel Martín <mardani29@yahoo.es> [2021-03-19 16:59]:
> > - before variables get defined, I would use `executable-find' but I
> >   would like for the program loading to fail if those external
> >   programs cannot be found.
> 
> Why do you think that checking for the existence of programs on load is
> better? I think it would slow down loading the module, and the benefits
> are not clear to me.  For example, the Elisp module may still be useful
> even if it cannot call external programs.
> 
> Why is it better than simply calling `executable-find' when invoking a
> command that depends on an external program?

I would not like later invoking functions that don't work. But I do
the checks in the functions. Sometimes majority of functions belong in
the same domain that uses external commands.

(defun rcd-which-list (command-list)
  "Verifies that list of shell commands COMMAND-LIST exist in
user's $PATH"
  (let ((all-exist t))
    (dolist (command command-list all-exist)
      (unless (executable-find command)
        (setq all-exist nil)
        (rcd-warning-message "Shell command `%s' does not exist" command)))))

(defun optimize-image-jpg (file)
  "Optimizes the JPG image with quality 70%"
  (if (rcd-which-list '("mogrify"))
      (let ((extension (file-name-extension file)))
        (when (equal (downcase extension) "jpg")
          (let* ((file (shell-double-quote file))
                 (command (format "mogrify -sampling-factor 4:2:0 -strip 
-interlace JPEG -colorspace RGB -quality 70 \"%s\"" file)))
            (message command)
            (shell-command command))))
    (rcd-warning-message "RCD ERROR: `mogrify' not found in $PATH")))

(defun optimize-jpg-images-dired ()
  "Optimizes JPG images inside of Dired"
  (interactive)
  (let ((files (dired-get-marked-files)))
    (dolist (file files)
      (optimize-image-jpg file))
    (revert-buffer)))

(defun image-resize (file &optional size)
  "Resizes the JPG image with default size"
  (if (rcd-which-list '("mogrify"))
      (let ((extension (file-name-extension file)))
        (when (or (equal (downcase extension) "jpg")
                  (equal (downcase extension) "png"))
          (let* ((file (shell-double-quote file))
                 (command (format "mogrify -resize %s \"%s\"" size file)))
            (message command)
            (call-process-shell-command command))))
    (rcd-warning-message "RCD ERROR: `mogrify' not found in `$PATH'")))
    
(defun image-resize-dired ()
  "Resizes images"
  (interactive)
  (let ((files (dired-get-marked-files))
        (size (read-number "Size: " *image-default-resize-size* 
'(*image-resize-sizes*))))
    (dolist (file files)
      (image-resize file size))
    (revert-buffer)))

(defun rcd-play-sound-bg (file)
  "Plays found file in background"
  (let ((play (executable-find "play")))
    (if play
          (let ((command (format "play \"%s\"" file)))
            (async-shell-command command))
      (message "RCD ERROR: `play' not found in $PATH"))))

(defun rcd-warning-message (format-string &rest message)
  "Plays a warning sound while using function `message' as
usual."
  (rcd-play-sound-bg rcd-warning-message-sound-file)
  (apply 'message format-string message))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]