I share an .emacs
file on two machines. About once per month I find that Emacs on one machine or the other fails to load the file. Just today the error was:
File error: https://melpa.org/packages/with-editor-20220412.1340.tar, Not found
I'm not looking for a solution to this particular error, rather, I would like to know **how to make the .emacs
file more robust? So that when e.g. Melpa is down, or whatever the cause of this error turns out to be, the rest of the file continues loading.
Here is the current state of my file, what better-practices should I be making a habit out of?
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
; Add the NonGNU ELPA package archive
; https://emacs.amodernist.com/
(add-to-list 'package-archives '("nongnu" . "https://elpa.nongnu.org/nongnu/"))
;; Git client
; https://emacs.amodernist.com/
(unless (package-installed-p 'magit)
(package-install 'magit))
; Bind the `magit-status' command to a convenient key.
(global-set-key (kbd "C-c g") #'magit-status)
; Show word-granularity differences within diff hunks
(setq magit-diff-refine-hunk t)
; Indication of local VCS changes
; https://emacs.amodernist.com/
(unless (package-installed-p 'diff-hl)
(package-install 'diff-hl))
; Update the highlighting without saving
(diff-hl-flydiff-mode t)
; JSON Support
; https://emacs.amodernist.com/
(unless (package-installed-p 'json-mode)
(package-install 'json-mode))
; Markdown support
; https://emacs.amodernist.com/
(unless (package-installed-p 'markdown-mode)
(package-install 'markdown-mode))
; Put backup files in a specific directory
; Backup files are of the form filename~
; https://stackoverflow.com/questions/151945/how-do-i-control-how-emacs-makes-backup-files
(setq backup-directory-alist `(("." . "~/.emacs-saves/")))
(setq backup-by-copying t)
; Put autosave files in a specific directory
; Autosave files are of the form #filename#
; https://www.gnu.org/software/emacs/manual/html_node/emacs/Auto-Save-Files.html
; https://github.com/emacscollective/no-littering
(setq auto-save-file-name-transforms `((".*", "~/.emacs-saves/" t)))
; Enable recent files
; Does seem to slow down startup times!
; https://stackoverflow.com/questions/3527150/open-recent-in-emacs
(require 'recentf)
(recentf-mode t)
; Open only a single window.
; https://stackoverflow.com/questions/29915133/how-can-i-open-the-files-only-in-a-specific-window-in-emacs
(setq inhibit-splash-screen t)
; Start maximized
(add-to-list 'default-frame-alist '(fullscreen . maximized))
; Vim Emulation
; https://emacs.amodernist.com/
(unless (package-installed-p 'evil)
(package-install 'evil))
; Proper Tab-key behaviour in org-mode
; Must be set before enabling evil
; https://stackoverflow.com/questions/22878668/emacs-org-mode-evil-mode-tab-key-not-working
(setq evil-want-C-i-jump nil)
; Enable Evil
(require 'evil)
(evil-mode 1)
; Enable org-evil
; https://github.com/GuiltyDolphin/org-evil
(require 'org-evil)
; esc quits pretty much anything (like pending prompts in the minibuffer)
; https://modulitos.com/blog/lightning-intro-to-emacs-using-evil-mode-and-org-mode/
(define-key evil-normal-state-map [escape] 'keyboard-quit)
(define-key evil-visual-state-map [escape] 'keyboard-quit)
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
; Ctrl-C W Toggle word wrap
; https://emacs.stackexchange.com/questions/30171/how-do-i-toggle-soft-wrapped-lines-in-org-mode
(global-set-key (kbd "C-c w") 'toggle-truncate-lines)
; Run these options only in org-mode
; https://modulitos.com/blog/lightning-intro-to-emacs-using-evil-mode-and-org-mode/
(add-hook 'org-mode-hook (lambda()
; Get Firefox link with Ctrl-C F
; https://stackoverflow.com/questions/8397319/how-to-bind-a-key-to-run-a-shell-command-in-dired-emacs
(defun ff-link-org ()
"Get Firefox link in Org mode format"
(interactive)
(shell-command "xdotool search 'Navigator' windowactivate --sync key --clearmodifiers Escape y o ; xdotool keydown alt key Tab ; xdotool keyup alt")
)
(global-set-key (kbd "C-c f") 'ff-link-org)
; Reenable <sTAB
; https://github.com/syl20bnr/spacemacs/issues/11798
(require 'org-tempo)
; Indent org-mode files nicely
; https://modulitos.com/blog/lightning-intro-to-emacs-using-evil-mode-and-org-mode/
(local-set-key (kbd "C-c C-c") 'org-table-align)
(local-set-key (kbd "C-c C-f") 'org-table-calc-current-TBLFM)
(org-indent-mode t)
; Proper RTL in org-mode
; https://emacs.stackexchange.com/questions/71371/rtl-text-with-left-alignment-and-reversed-line-wrapping-in-org-mode
;(setq bidi-paragraph-direction 'right-to-left)
(setq bidi-paragraph-direction nil)
; Proper wrapping!
(visual-line-mode 1)
) )
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ansi-color-faces-vector
[default default default italic underline success warning error])
'(ansi-color-names-vector
["#242424" "#e5786d" "#95e454" "#cae682" "#8ac6f2" "#333366" "#ccaa8f" "#f6f3e8"])
'(blink-cursor-mode nil)
'(custom-enabled-themes '(wheatgrass))
'(org-agenda-files '("~/beer.org"))
'(package-selected-packages '(csv-mode org-evil dash)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:family "Source Code Pro" :foundry "ADBO" :slant normal :weight normal :height 143 :width normal)))))
package-install
calls in your init. This implies that you could get some install (melpa) error at the time (a) you add a new package to your mix; plus (b) you're on a different machine. After that it should run stably (until next package choice)