Search for Emacs.app in /usr/local/opt

And refactors doom-patch-macos function, its dispatcher, and its usage
in doom-quickstart.
This commit is contained in:
Henrik Lissner 2018-08-28 19:13:34 +02:00
parent 2b560a6a0e
commit ef6e09dac9
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -190,7 +190,10 @@ recompile. Run this whenever you:
3. Add or remove autoloaded functions in module autoloaded files. 3. Add or remove autoloaded functions in module autoloaded files.
4. Update Doom outside of Doom (e.g. with git)") 4. Update Doom outside of Doom (e.g. with git)")
(dispatcher! (patch-macos) (doom-patch-macos args) (dispatcher! (patch-macos)
(doom-patch-macos (or (member "--undo" args)
(member "-u" args))
(doom--find-emacsapp-path))
"Patches Emacs.app to respect your shell environment. "Patches Emacs.app to respect your shell environment.
A common issue with GUI Emacs on MacOS is that it launches in an environment A common issue with GUI Emacs on MacOS is that it launches in an environment
@ -303,6 +306,16 @@ problems with doom."
(car e) (car e)
(buffer-string)))))))) (buffer-string))))))))
(defun doom--find-emacsapp-path ()
(or (getenv "EMACS_APP_PATH")
(cl-find-if #'file-directory-p
(list "/usr/local/opt/emacs"
"/usr/local/opt/emacs-plus"
"/Applications"
"~/Applications")
:key (lambda (x) (concat x "/Emacs.app")))
(user-error "Couldn't find Emacs.app")))
(defun doom-quickstart () (defun doom-quickstart ()
"Quickly deploy a private module and Doom. "Quickly deploy a private module and Doom.
@ -331,25 +344,11 @@ packages and regenerates the autoloads file."
(with-temp-file config-file (insert "")) (with-temp-file config-file (insert ""))
(print! (green "Done!"))))) (print! (green "Done!")))))
;; Ask if Emacs.app should be patched ;; Ask if Emacs.app should be patched
(condition-case e (when IS-MAC
(when IS-MAC (message "MacOS detected")
(message "MacOS detected") (condition-case e
(let ((appdir (cl-find-if #'file-directory-p (doom-patch-macos nil (doom--find-emacsapp-path))
(list "/Applications/Emacs.app" (user-error (message "%s" (error-message-string e)))))
"~/Applications/Emacs.app"))))
(unless appdir
(user-error "Couldn't find Emacs.app in /Applications or ~/Applications"))
(when (file-exists-p! "Contents/MacOS/RunEmacs" appdir)
(user-error "Emacs.app is already patched"))
(unless (or doom-auto-accept
(y-or-n-p
(concat "Doom would like to patch your Emacs.app bundle so that it respects\n"
"your shell configuration. For more information on why and how, run\n\n"
" bin/doom help patch-macos\n\n"
"Patch Emacs.app?")))
(user-error "Will not patch Emacs.app"))
(doom-patch-macos nil)))
(user-error (message "%s" (error-message-string e))))
;; Install Doom packages ;; Install Doom packages
(print! "Installing plugins") (print! "Installing plugins")
(doom-packages-install doom-auto-accept) (doom-packages-install doom-auto-accept)
@ -360,46 +359,44 @@ packages and regenerates the autoloads file."
(doom-template-insert "QUICKSTART_INTRO") (doom-template-insert "QUICKSTART_INTRO")
(print! (buffer-string)))) (print! (buffer-string))))
(defun doom-patch-macos (args) (defun doom-patch-macos (undo-p appdir)
"Patches Emacs.app to respect your shell environment." "Patches Emacs.app to respect your shell environment."
(unless IS-MAC (unless IS-MAC
(user-error "You don't seem to be running MacOS")) (user-error "You don't seem to be running MacOS"))
(let ((appdir (unless (file-directory-p appdir)
(cl-find-if #'file-exists-p (user-error "Couldn't find '%s'" appdir))
(list "/Applications/Emacs.app" (let ((oldbin (expand-file-name "Contents/MacOS/Emacs" appdir))
"~/Applications/Emacs.app")))) (newbin (expand-file-name "Contents/MacOS/RunEmacs" appdir)))
(unless appdir (cond (undo-p
(user-error "Couldn't find Emacs.app in /Applications or ~/Applications")) (unless (file-exists-p newbin)
(let ((oldbin (expand-file-name "Contents/MacOS/Emacs" appdir)) (user-error "Emacs.app is not patched"))
(newbin (expand-file-name "Contents/MacOS/RunEmacs" appdir))) (copy-file newbin oldbin 'ok-if-already-exists nil nil 'preserve-permissions)
(cond ((or (member "--undo" args) (unless (file-exists-p oldbin)
(member "-u" args)) (error "Failed to copy %s to %s" newbin oldbin))
(unless (file-exists-p newbin) (delete-file newbin)
(user-error "Emacs.app is not patched")) (message "%s successfully unpatched" appdir))
(copy-file newbin oldbin 'ok-if-already-exists nil nil 'preserve-permissions)
(unless (file-exists-p oldbin)
(error "Failed to copy %s to %s" newbin oldbin))
(delete-file newbin)
(message "%s successfully unpatched" appdir))
((file-exists-p newbin) ((file-exists-p newbin)
(user-error "%s is already patched" appdir)) (user-error "%s is already patched" appdir))
((or doom-auto-accept ((or doom-auto-accept
(y-or-n-p (concat "/Applications/Emacs.app needs to be patched. See `bin/doom help patch-macos' for why and how.\n\n" (y-or-n-p
"Patch Emacs.app?"))) (concat "Doom would like to patch your Emacs.app bundle so that it respects\n"
(message "Patching '%s'" appdir) "your shell configuration. For more information on why and how, run\n\n"
(copy-file oldbin newbin nil nil nil 'preserve-permissions) " bin/doom help patch-macos\n\n"
(unless (file-exists-p newbin) "Patch Emacs.app?")))
(error "Failed to copy %s to %s" oldbin newbin)) (message "Patching '%s'" appdir)
(with-temp-buffer (copy-file oldbin newbin nil nil nil 'preserve-permissions)
(insert "#!/usr/bin/env bash\n" (unless (file-exists-p newbin)
"args=\"$@\"\n" (error "Failed to copy %s to %s" oldbin newbin))
"pwd=\"$(cd \"$(dirname \"${BASH_SOURCE[0]}\")\"; pwd -P)\"\n" (with-temp-buffer
"exec \"$SHELL\" -c \"$pwd/RunEmacs $args\"") (insert "#!/usr/bin/env bash\n"
(write-file oldbin) "args=\"$@\"\n"
(chmod oldbin (file-modes newbin))) "pwd=\"$(cd \"$(dirname \"${BASH_SOURCE[0]}\")\"; pwd -P)\"\n"
(message "%s successfully patched" appdir)))))) "exec \"$SHELL\" -c \"$pwd/RunEmacs $args\"")
(write-file oldbin)
(chmod oldbin (file-modes newbin)))
(message "%s successfully patched" appdir)))))
;; ;;