I have some snippets in my config, that depend on external tools. For example I use xmodmap at startup.

(if (eq window-system 'x)
    (if (file-exists-p "/usr/bin/xmodmap")
        (shell-command "xmodmap -e 'clear Lock' -e 'keycode 66 = F13'")))

Is it possible to install xmodmap automatically if it isn't installed ?


@Ehvince When I install xmodmap in the else clause, the xmodmap command won't be executed. I think I need some piece of code that waits for the subprocess to be finished before executing the xmodmap command ? Another problem is, I can't install a package when I start emacs without sudo. So I need a password prompt, I guess.

  • what do you mean ? Looks like you did all the job with the if, didn't you ? You could install xmodmap in the else clause.
    – Ehvince
    Nov 21 '16 at 17:41
  • You can start a process synchroneous via call-process-region. Emacs then waits until the process is finished before executing the next statement. The password can be read from stdin by sudo. The password input is read with read-passwd. Overall you have something like: (with-temp-buffer (insert (read-password) "\n") (call-process-region (point-min) (point-max) "sudo" t nil "-S" "aptitude" "install" "xmodmap"). (Note, that this is untested.)
    – Tobias
    Nov 22 '16 at 15:13

Thanks to Tobias and this answer I was able to find a solution.

(when (eq window-system 'x)
  (unless (file-exists-p "/usr/bin/xmodmap")
    (call-process-shell-command (concat "echo " (shell-quote-argument (read-passwd "Password: "))
                                        " | sudo -S apt-get install xmodmap")))
  (shell-command "xmodmap -e 'clear Lock' -e 'keycode 66 = F13'"))

call-process-shell-command executes aptitude synchronously in a separate process.

  • Please accept your own answer to mark this question answered. Thanks in advance,
    – Tobias
    Nov 23 '16 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.