unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jean Louis <bugs@gnu.support>
To: "Daniel Martín" <mardani29@yahoo.es>
Cc: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: Re: Better way to make sure external command exists in the system?
Date: Sun, 21 Mar 2021 17:07:18 +0300	[thread overview]
Message-ID: <YFdTFpg4UJeS4aTY@protected.localdomain> (raw)
In-Reply-To: <m1im5nnt4h.fsf@yahoo.es>

* 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))



  parent reply	other threads:[~2021-03-21 14:07 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18 12:13 Better way to make sure external command exists in the system? Jean Louis
2021-03-19 13:57 ` Daniel Martín
2021-03-19 14:16   ` 2QdxY4RzWzUUiLuE
2021-03-21 14:10     ` Jean Louis
2021-03-21 17:17       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-03-21 14:07   ` Jean Louis [this message]
2021-03-21 17:16     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-03-23  8:02       ` Jean Louis
2021-03-23  9:56         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-03-23 10:13           ` Jean Louis
2021-03-25 15:01             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-03-25 15:17               ` Jean Louis
2021-03-25 20:47                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-03-25 20:56                   ` Jean Louis
2021-03-25 21:10                     ` Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YFdTFpg4UJeS4aTY@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mardani29@yahoo.es \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).