Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

mpereira’s Emacs configuration

This is my vanilla, Evil, literate Emacs configuration. It enables most of my computing needs, since most of the time I’m on a computer I’m on Emacs.

It can be found at

I wouldn’t recommend others to use this configuration as-is. I’m sure there are sections, snippets, or settings that might be interesting, though.

If you’d like to know more about my relationship with Emacs, check out this thing I wrote: How to open a file in Emacs: a short story about Lisp, technology, and human progress.

One day I’ll include some screenshots here.

Installing Emacs

I mostly use GUI Emacs on macOS. For work I use TUI Emacs on Ubuntu via SSH. On macOS I install the excellent homebrew-emacs-head package created by Davide Restivo:

brew install emacs-head@29 \
     --with-cocoa \
     --with-crash-debug \
     --with-imagemagick \
     --with-modern-icon-pen \
     --with-native-comp \
     --with-no-frame-refocus \
     --with-pdumper \
     --with-tree-sitter \

On Ubuntu I install Alex Murray’s GNU Emacs Snap package based on the “latest/edge” channel, which comes with native-comp and works great.

First make sure libgccjit is installed:

sudo apt install libgccjit-10-dev

Then install the Emacs snap:

sudo snap install emacs --edge --classic

Table of Contents


Some dependencies are installed with the script, which is tangled from this file.

Getting the file name: preamble:

# This file is auto-generated by Emacs via `(org-babel-tangle-file "<<configuration-org-file()>>")'.

set -euxo pipefail

Other dependencies have to be manually set up:

Silent exports for emacs lisp org babel code blocks

Having this as an org file property doesn’t seem to work for some reason.

:header-args: :results output silent :exports both

Set it with emacs lisp.

(setq org-babel-default-header-args:emacs-lisp '((:results . "output silent")))

quelpa and quelpa-use-package

I wasn’t able to fully transition to vc-use-package yet. Some packages are based on git branches or URLs.

(use-package quelpa)
(use-package quelpa-use-package)

Utility libraries


(use-package async)


(use-package aio)


(use-package cl-lib)


(use-package no-littering)


(use-package s)


(use-package dash)


(use-package thingatpt+
  :ensure nil
  :quelpa (thingatpt+
           :url ""
           :fetcher url))


(use-package help-fns+
  :ensure nil
  :vc (:fetcher github
       :repo "emacsmirror/help-fns-plus"))


(use-package ts
  :ensure nil
  :vc (:fetcher github
       :repo "alphapapa/ts.el"))


(use-package elx
  :ensure nil
  :vc (:fetcher github
       ;; :branch "dont-break-if-no-licensee"
       :repo "mpereira/elx"))



(use-package general
  (use-package-hook-name-suffix . nil))


(use-package paradox

  ;; Disable annoying "do you want to set up GitHub integration" prompt.
  (setq paradox-github-token t))


This needs to be loaded before code that depends on PATH modifications, e.g. executable-find.

Check mpereira/dotfiles .profile for PATH modifications.

(use-package exec-path-from-shell
  (dolist (shell-variable '("SSH_AUTH_SOCK"
    (add-to-list 'exec-path-from-shell-variables shell-variable))

  ;; Removing the "-l" flag so that it sources ~/HOME/profile after all the
  ;; other scripts (e.g. /etc/paths.d/*, etc.).
  (setq exec-path-from-shell-arguments '("-i"))



(setq mpereira/custom-file (expand-file-name "custom.el" user-emacs-directory))

(setq mpereira/leader ",")

;; NOTE(2023-01-25): switching from `doom-acario-light' because magit diffs look
;; bad.
(setq mpereira/light-theme 'modus-operandi)
(setq mpereira/dark-theme 'vscode-dark-plus)
(setq mpereira/initial-theme mpereira/dark-theme)

(setq mpereira/cloud-synced-directory
        "~/Library/Mobile Documents/com~apple~CloudDocs/")))
(setq mpereira/org-directory (expand-file-name "org" mpereira/cloud-synced-directory))

(setq mpereira/org-calendar-file (expand-file-name "gcal/"
(setq mpereira/org-calendar-buffer-name (file-name-nondirectory
;; Empirically, 2 seconds seems to be good enough.
(setq mpereira/org-gcal-request-timeout 2)

(setq mpereira/magit-status-width 120)

(setq mpereira/org-agenda-width 120)

(setq mpereira/fill-column 80)
(setq mpereira/fill-column-wide 120)

(setq mpereira/eshell-prompt-max-directory-length 50)
(setq mpereira/mode-line-max-directory-length 15)

(defun mpereira/is-gnu-program (executable)
    (call-process executable nil t nil "--version")
    (string-match-p "GNU" (buffer-string))))

Redefinitions, advices

Make org src buffer name shorter and nicer


*Org Src[ emacs-lisp ]*
*Org Src[ emacs-lisp ]<2>*

After (org src) (org src)<2>
(defun org-src--construct-edit-buffer-name (org-buffer-name lang)
  "Construct the buffer name for a source editing buffer."
  (concat org-buffer-name " (org src)"))

Improve Lisp code indentation


(:foo bar
      :baz qux)


(:foo bar
 :baz qux)

I got this from Fuco1/.emacs.d/site-lisp/my-redef.el.

(eval-after-load "lisp-mode"
  '(defun lisp-indent-function (indent-point state)
     "This function is the normal value of the variable `lisp-indent-function'.
The function `calculate-lisp-indent' calls this to determine if the arguments of
a Lisp function call should be indented specially. INDENT-POINT is the position
at which the line being indented begins. Point is located at the point to indent
under (for default indentation); STATE is the `parse-partial-sexp' state for
that position. If the current line is in a call to a Lisp function that has a
non-nil property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
it specifies how to indent. The property value can be: * `defun', meaning indent
`defun'-style \(this is also the case if there is no property and the function
has a name that begins with \"def\", and three or more arguments); * an integer
N, meaning indent the first N arguments specially
  (like ordinary function arguments), and then indent any further
  arguments like a body;
* a function to call that returns the indentation (or nil).
  `lisp-indent-function' calls this function with the same two arguments
  that it itself received.
This function returns either the indentation to use, or nil if the
Lisp function does not specify a special indentation."
     (let ((normal-indent (current-column))
           (orig-point (point)))
       (goto-char (1+ (elt state 1)))
       (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
        ;; car of form doesn't seem to be a symbol, or is a keyword
        ((and (elt state 2)
              (or (not (looking-at "\\sw\\|\\s_"))
                  (looking-at ":")))
         (if (not (> (save-excursion (forward-line 1) (point))
             (progn (goto-char calculate-lisp-indent-last-sexp)
                    (parse-partial-sexp (point)
                                        calculate-lisp-indent-last-sexp 0 t)))
         ;; Indent under the list or under the first sexp on the same
         ;; line as calculate-lisp-indent-last-sexp.  Note that first
         ;; thing on that line has to be complete sexp since we are
         ;; inside the innermost containing sexp.
        ((and (save-excursion
                (goto-char indent-point)
                (skip-syntax-forward " ")
                (not (looking-at ":")))
                (goto-char orig-point)
                (looking-at ":")))
           (goto-char (+ 2 (elt state 1)))
         (let ((function (buffer-substring (point)
                                           (progn (forward-sexp 1) (point))))
           (setq method (or (function-get (intern-soft function)
                            (get (intern-soft function) 'lisp-indent-hook)))
           (cond ((or (eq method 'defun)
                      (and (null method)
                           (> (length function) 3)
                           (string-match "\\`def" function)))
                  (lisp-indent-defform state indent-point))
                 ((integerp method)
                  (lisp-indent-specform method state
                                        indent-point normal-indent))
                  (funcall method indent-point state)))))))))

Make align-regexp not use tabs

Found on Stack Overflow.

(defadvice align-regexp (around align-regexp-with-spaces activate)
  (let ((indent-tabs-mode nil))

Helper functions


(require 'erc)

(fset 'remove-from-list #'ert--remove-from-list)


(defun mpereira/yank-current-selection-as-console-log ()
  (when (region-active-p)
    (let* ((selected-text (buffer-substring-no-properties (region-beginning) (region-end)))
           (output (concat "console.log(['" selected-text "', " selected-text "]);")))
      (kill-new output)
      (message output)


Got from

(defun mpereira/remove-from-list-variable ()
  (let* ((var (intern
               (completing-read "From variable: "
                                (let (symbols)
                                   (lambda (sym)
                                     (when (and (boundp sym)
                                                (seqp (symbol-value sym)))
                                       (push sym symbols))))
                                  symbols) nil t)))
         (values (mapcar (lambda (item)
                           (setq item (prin1-to-string item))
                           (concat (truncate-string-to-width
                                    (nth 0 (split-string item "\n"))
                                   (propertize item 'invisible t)))
                         (symbol-value var)))
         (index (progn
                  (when (seq-empty-p values) (error "Already empty"))
                  (seq-position values (completing-read "Delete: " values nil t)))))
    (unless index (error "Eeek. Something's up."))
    (set var (append (seq-take (symbol-value var) index)
                     (seq-drop (symbol-value var) (1+ index))))
    (message "Deleted: %s" (truncate-string-to-width
                            (seq-elt values index)
                            (- (window-body-width) 9)))))

Standard library type of things

(defmacro comment (&rest body)
  "Comment out one or more s-expressions."

(defun disable-y-or-n-p (orig-fun &rest args)
  (cl-letf (((symbol-function 'y-or-n-p) (lambda (prompt) t)))
    (apply orig-fun args)))

(defun eshell-p (buffer)
  "Return t if BUFFER is an Eshell buffer."
  (with-current-buffer buffer
    (eq major-mode 'eshell-mode)))

(defun plist-each (function plist)
  "Iterate FUNCTION (a two-argument function) over PLIST."
  (when plist
    (funcall function (car plist) (cadr plist))
    (plist-each function (cddr plist))))

(defun queue-push (queue-sym element &optional bounded-limit)
  "TODO: docstring."
  (when (or (not bounded-limit)
            (< (length (symbol-value queue-sym))
    (add-to-list queue-sym element t (lambda (a b) nil))))

(defun queue-pop (queue-sym)
  "TODO: docstring."
  (let* ((queue (symbol-value queue-sym))
         (popped-element (car queue)))
    (when popped-element
      (set queue-sym (cdr queue)))

(defun unadvice (sym)
  "Remove all advices from symbol SYM."
  (interactive "aFunction symbol: ")
  (advice-mapc (lambda (advice _props) (advice-remove sym advice)) sym))


(defmacro print-and-return (&rest body)
  "TODO: docstring."
  (let ((result-symbol (make-symbol "result")))
    `(let ((,result-symbol ,@body))
       (message "************************************************************")
       (pp ',@body)
       (message "||")
       (message "\\/")
       (print ,result-symbol)
       (message "************************************************************")

(defun mpereira/hl-line-mode-disable ()
  "TODO: docstring."
  (setq-local global-hl-line-mode nil))

(defun mpereira/hide-trailing-whitespace ()
  (setq-local show-trailing-whitespace nil))

(defun mpereira/delete-file-and-buffer ()
  "Kill the current buffer and deletes the file it is visiting."
  (let ((filename (buffer-file-name)))
    (when filename
      (if (vc-backend filename)
          (vc-delete-file filename)
          (delete-file filename)
          (message "Deleted file %s" filename)

(defun mpereira/rename-file-and-buffer ()
  "Rename the current buffer and file it is visiting."
  (let ((filename (buffer-file-name)))
    (if (not (and filename (file-exists-p filename)))
        (message "Buffer is not visiting a file!")
      (let ((new-name (read-file-name "New name: " filename)))
         ((vc-backend filename) (vc-rename-file filename new-name))
          (rename-file filename new-name t)
          (set-visited-file-name new-name t t)))))))

(defun mpereira/move-file-and-buffer (new-location)
  "Write this file to NEW-LOCATION, and delete the old one."
  (interactive (list (expand-file-name
                      (read-file-name "Move file to: "
                                      (expand-file-name (file-name-nondirectory (buffer-name))
  (if (file-regular-p new-location)
      (when (file-exists-p new-location)
        (delete-file new-location))
    (let ((old-location (expand-file-name (buffer-file-name))))
      (write-file new-location t)
      (when (and old-location
                 (file-exists-p new-location)
                 (not (string-equal old-location new-location)))
        (delete-file old-location)))))

(defun mpereira/pp-macroexpand-all ()
  "TODO: docstring."
  (let ((form (macroexpand-all (sexp-at-point))))
    (with-current-buffer-window " *mpereira/pp-macroexpand-all*" nil nil
      (pp form)

(require 'thingatpt)
(require 'thingatpt+)
(defun mpereira/eval-thing-at-or-around-point ()
  "Evaluate thing at or surrounding the point."
    (let* ((string-thing (tap-string-at-point))
           (symbol-thing (tap-symbol-at-point))
           (sexp-thing (sexp-at-point)))
        (let* ((_ (message "string"))
               (bounds (tap-bounds-of-string-at-point))
               (string-form (substring-no-properties string-thing))
               (string-value (substring-no-properties
          (message "%s%s" string-form string-form)
          (eros--eval-overlay string-value (cdr bounds))))
        (let* ((_ (message "symbol"))
               (bounds (tap-bounds-of-symbol-at-point))
               (symbol-name (substring-no-properties
               (symbol-value (eval symbol-thing)))
          (message "%s" symbol-name)
          (message "")
          (message "%s" symbol-value)
          (eros--eval-overlay symbol-value (cdr bounds))))
        (let* ((_ (message "sexp"))
               (bounds (tap-bounds-of-sexp-at-point))
               (value (eval sexp-thing)))
          (message "%s" sexp-thing)
          (message "")
          (message "%s" value)
          (eros--eval-overlay value (cdr bounds))))))))

(defun mpereira/split-window-below-and-switch ()
  "Split the window horizontally then switch to the new window."
  (other-window 1))

(defun mpereira/split-window-right-and-switch ()
  "Split the window vertically then switch to the new window."
  (other-window 1))

(defun mpereira/toggle-window-split ()
  (if (= (count-windows) 2)
      (let* ((this-win-buffer (window-buffer))
             (next-win-buffer (window-buffer (next-window)))
             (this-win-edges (window-edges (selected-window)))
             (next-win-edges (window-edges (next-window)))
             (this-win-2nd (not (and (<= (car this-win-edges)
                                         (car next-win-edges))
                                     (<= (cadr this-win-edges)
                                         (cadr next-win-edges)))))
              (if (= (car this-win-edges)
                     (car (window-edges (next-window))))
        (let ((first-win (selected-window)))
          (funcall splitter)
          (if this-win-2nd (other-window 1))
          (set-window-buffer (selected-window) this-win-buffer)
          (set-window-buffer (next-window) next-win-buffer)
          (select-window first-win)
          (if this-win-2nd (other-window 1))))
    (message "Can only toggle window split for 2 windows")))

(defun mpereira/indent-buffer ()
  "Indents the current buffer."
  (indent-region (point-min) (point-max)))

(with-eval-after-load "lispy"
  (defun mpereira/inside-bounds-dwim ()
    ;; (when-let (lispy--bounds-dwim)
    ;;   (when (<)))

  (defun mpereira/backward-sexp-begin (arg)
    "Moves to the beginning of the previous ARG nth sexp."
    (interactive "p")
    (if-let (bounds (lispyville--in-string-p))
        ;; Go to beginning of string.
        (goto-char (car bounds))
      ;; `backward-sexp' will enter list-like sexps when point is on the closing
      ;; character. So we move one character to the right.
      (when (looking-at lispy-right)
        (forward-char 1))
      (backward-sexp arg)))

  (defun mpereira/forward-sexp-begin (arg)
    "Moves to the beginning of the next ARG nth sexp. The fact that this doesn't
exist in any structured movement package is mind-boggling to me."
    (interactive "p")
    (when-let (bounds (lispyville--in-string-p))
      (goto-char (car bounds)))
    (dotimes (_ arg)
      (forward-sexp 1)
      (if (looking-at lispy-right)
          ;; Prevent moving forward from last element in current level.
          (backward-sexp 1)
          (forward-sexp 1)
          (backward-sexp 1)))))

  ;; Idea: move up to the parent sexp, count the number of sexps inside it with
  ;; `scan-lists' or `scan-sexps' or `paredit-scan-sexps-hack' to know whether
  ;; or not we're at the last sexp.
  (defun mpereira/forward-sexp-end (arg)
    "Moves to the end of the next ARG nth sexp. The fact that this doesn't exist
in any structured movement package is mind-boggling to me."
    (interactive "p")
    (let ((region-was-active (region-active-p)))
      ;; If a region is selected, pretend it's not so that `lispy--bounds-dwim'
      ;; doesn't return the bounds of the region. We want the bounds of the
      ;; actual thing under the point.
      (cl-letf (((symbol-function 'region-active-p) #'(lambda () nil)))
        (when-let (bounds (lispy--bounds-dwim))
          (let ((end (- (cdr bounds) 1)))
            (if (< (point) end)
                ;; Move to the end of the current sexp if not already there.
                  (goto-char end)
                  ;; When a region is active we need to move right an extra
                  ;; character.
                  (when (and region-was-active)
                    (forward-char 1)))
                ;; Move one character to the right in case point is on a list-like
                ;; closing character so that the subsequent `lispy--bounds-dwim'
                ;; start is right.
                (when (looking-at lispy-right)
                  (forward-char 1))
                ;; Go to the beginning of the current sexp so that
                ;; `mpereira/forward-sexp-begin' works.
                (when-let (bounds (lispy--bounds-dwim))
                  (goto-char (car bounds)))
                ;; Move to the beginning of the next sexp.
                (mpereira/forward-sexp-begin arg)
                ;; Go to the end of the sexp.
                (when-let (bounds (lispy--bounds-dwim))
                  (goto-char (- (cdr bounds) 1))
                  ;; When a region is active and we're not at the last sexp we
                  ;; need to move right an extra character.
                  (when (and region-was-active
                             ;; TODO
                             ;; (not last-sexp)
                    (forward-char 1)))))))))))

(with-eval-after-load "evil"
  (with-eval-after-load "lispyville"
    (defun mpereira/insert-to-beginning-of-list (arg)
      (interactive "p")
      (evil-insert arg))

    (defun mpereira/append-to-end-of-list (arg)
      (interactive "p")
      (evil-insert arg))))

(defun mpereira/org-sort-parent-entries (&rest args)
  ;; `org-sort-entries' doesn't respect `save-excursion'.
  (let ((origin (point)))
    (apply #'org-sort-entries args)
    (goto-char origin)))

(defun mpereira/org-cycle-cycle ()

(defun mpereira/call-interactively-with-prefix-arg (prefix-arg func)
  (let ((current-prefix-arg prefix-arg))
    (call-interactively func)))

(with-eval-after-load "find-file-in-project"
  (defun mpereira/find-directory ()
    (ffip-find-files "" nil t)))

(with-eval-after-load "projectile"
  (defun mpereira/maybe-projectile-dired ()
    (if (projectile-project-p)
      (dired ".")))

  (defun mpereira/maybe-projectile-ibuffer ()
    (if (projectile-project-p)
        (projectile-ibuffer nil)
      (ibuffer ".")))

  (with-eval-after-load "eshell"
    (defun mpereira/maybe-projectile-eshell ()
      (if (projectile-project-p)
          (projectile-run-eshell t)
        (eshell t))))

  (with-eval-after-load "find-file-in-project"
    (with-eval-after-load "consult-projectile"
      (defun mpereira/maybe-projectile-switch-buffer ()
        (if (projectile-project-p)

      (defun mpereira/maybe-projectile-find-file ()
        (if (projectile-project-p)

      (defun mpereira/maybe-projectile-find-directory ()
        (if (projectile-project-p)

(defun mpereira/enable-line-numbers ()
  (setq display-line-numbers t))

(defun mpereira/disable-line-numbers ()
  (setq display-line-numbers nil))

(defun mpereira/maybe-enable-aggressive-indent-mode ()
  "TODO: docstring."
  (when (not (or (cl-member-if #'derived-mode-p aggressive-indent-excluded-modes)
                 (-contains? aggressive-indent-excluded-buffers (buffer-name))

(defun mpereira/lock-screen ()
  "TODO: docstring."
  ;; TODO: make file path joining portable.
  (let ((command (concat "/System"
                         "/Menu\\ Extras"
                         " "
    (shell-command command)))

(defun mpereira/symbol-at-point ()
  "Return current symbol at point as a string."
  (let ((s (thing-at-point 'symbol)))
    (and (stringp s)
         (if (string-match "\\`[`']?\\(.*?\\)'?\\'" s)
             (match-string 1 s)

(defun mpereira/epoch-at-point-to-timestamp ()
  "TODO: docstring"
  (if-let (thing (mpereira/symbol-at-point))
      (let* ((seconds (string-to-number thing))
             (time (seconds-to-time seconds))
             (timestamp (format-time-string "%Y-%m-%d %a %H:%M:%S" time)))
        (kill-new timestamp)
        (message timestamp)

(defun mpereira/yank-buffer-file-name ()
  "TODO: docstring"
  (let ((buffer-file-name* (if (eshell-p (current-buffer))
    (kill-new buffer-file-name*)
    (message buffer-file-name*)

(defun mpereira/yank-buffer-name ()
  "TODO: docstring"
  (let ((buffer-name* (buffer-name)))
    (kill-new buffer-name*)
    (message buffer-name*)

(defun mpereira/narrow-or-widen-dwim (p)
  "Widen if buffer is narrowed, narrow-dwim otherwise.
Dwim means: region, org-src-block, org-subtree, or defun, whichever applies
first. Narrowing to org-src-block actually calls `org-edit-src-code'.

With prefix P, don't widen, just narrow even if buffer is already narrowed."
  (interactive "P")
  (declare (interactive-only))
  (cond ((and (buffer-narrowed-p) (not p)) (widen))
         (narrow-to-region (region-beginning)
        ((derived-mode-p 'org-mode)
         ;; `org-edit-src-code' is not a real narrowing command. Remove this
         ;; first conditional if you don't want it.
         (cond ((ignore-errors (org-edit-src-code) t)
               ((ignore-errors (org-narrow-to-block) t))
               (t (org-narrow-to-subtree))))
        ((derived-mode-p 'latex-mode)
        (t (narrow-to-defun))))

(defun mpereira/uuid ()
  "Return a UUID and make it the latest kill in the kill ring."
  (kill-new (format "%04x%04x-%04x-%04x-%04x-%06x%06x"
                    (random (expt 16 4))
                    (random (expt 16 4))
                    (random (expt 16 4))
                    (random (expt 16 4))
                    (random (expt 16 4))
                    (random (expt 16 6))
                    (random (expt 16 6)))))

;; TODO: make this better.
(defun mpereira/kill-last-kbd-macro ()
  "Save last executed macro definition in the kill ring."
  (let ((name (gensym "kill-last-kbd-macro-")))
    (name-last-kbd-macro name)
      (insert-kbd-macro name)
      (kill-new (buffer-substring-no-properties (point-min) (point-max))))))

(defun mpereira/load-light-theme ()
  "TODO: docstring."
  (consult-theme mpereira/light-theme))

(defun mpereira/load-dark-theme ()
  "TODO: docstring."
  (consult-theme mpereira/dark-theme))

(defun mpereira/process-using-port ()
  "Show list of processes listening on ports via TCP.
  Copies the selected process's PID to the clipboard."
  (let ((candidates (split-string (shell-command-to-string
                                   "lsof -nP -iTCP | grep LISTEN")
    (let ((chosen-process (completing-read "Port: " candidates nil t)))
      (when chosen-process
        (kill-new (cadr (split-string chosen-process " " t)))))))

(defun mpereira/ps ()
  "Show list of system processes.
Copies the selected process's PID to the clipboard."
  (let ((ps (split-string (shell-command-to-string
                           "ps axco user,pid,%cpu,%mem,start,time,command -r")
    (let ((chosen-process (completing-read "Process: " ps nil t)))
      (when chosen-process
        (kill-new (cadr (split-string chosen-process " " t)))))))

(defun mpereira/kill-buffer-and-maybe-window (&optional kill-buffer-p)
  (if (window-prev-buffers)
      (let ((previous-buffer (car (window-prev-buffers))) ; not using this.
            (current-buffer* (current-buffer)))
        (kill-buffer current-buffer*))

;; TODO: make it be able to get indirect buffer file names.
(defun mpereira/file-metadata ()
  (let* ((fname (buffer-file-name))
         (data (file-attributes fname))
         (access (current-time-string (nth 4 data)))
         (mod (current-time-string (nth 5 data)))
         (change (current-time-string (nth 6 data)))
         (size (nth 7 data))
         (mode (nth 8 data))
         (output (format

Accessed: %s
Modified: %s
Changed:  %s
Size:     %s bytes
Mode:     %s"
                  fname access mod change size mode)))
    (kill-new output)
    (message output)

(defun mpereira/buffer-project-directory (project-root-directory
                                          &optional max-length)
  "Returns a possibly left-truncated relative directory for a project buffer."
  (let* ((truncation-string (if (char-displayable-p ?…) "…/" ".../"))
         (relative-directory (s-chop-prefix project-root-directory buffer-directory))
         (abbreviated-directory (abbreviate-file-name relative-directory))
         (max-length (or max-length 1.0e+INF)))
    ;; If it fits, return the string.
    (if (and max-length
             (<= (string-width abbreviated-directory) max-length))
      ;; If it doesn't, shorten it.
      (let ((path (reverse (split-string abbreviated-directory "/")))
            (output ""))
        (when (and path (equal "" (car path)))
          (setq path (cdr path)))
        (let ((max (- max-length (string-width truncation-string))))
          ;; Concat as many levels as possible, leaving 4 chars for safety.
          (while (and path (<= (string-width (concat (car path) "/" output))
            (setq output (concat (car path) "/" output))
            (setq path (cdr path))))
        ;; If we had to shorten, prepend …/.
        (when path
          (setq output (concat truncation-string output)))

(defun mpereira/short-directory-path (directory &optional max-length)
  "Returns a potentially trimmed-down version of the directory DIRECTORY,
replacing parent directories with their initial characters to try to get the
character length of directory (sans directory slashes) down to MAX-LENGTH."
  (let* ((components (split-string (abbreviate-file-name directory) "/"))
         (max-length (or max-length 1.0e+INF))
         (len (+ (1- (length components))
                 (cl-reduce '+ components :key 'length)))
         (str ""))
    (while (and (> len max-length)
                (cdr components))
      (setq str (concat str
                        (cond ((= 0 (length (car components))) "/")
                              ((= 1 (length (car components)))
                               (concat (car components) "/"))
                               (if (string= "."
                                            (string (elt (car components) 0)))
                                   (concat (substring (car components) 0 2)
                                 (string (elt (car components) 0) ?/)))))
            len (- len (1- (length (car components))))
            components (cdr components)))
    (concat str (cl-reduce (lambda (a b) (concat a "/" b)) components))))

(defun mpereira/elpy-shell-clear-shell ()
  "Clear the current shell buffer."
  (with-current-buffer (process-buffer (elpy-shell-get-or-create-process))

(defun mpereira/prevent-buffer-kill ()
  "Prevents the current buffer from being killed."
  (emacs-lock-mode 'kill))

(defun mpereira/exec-path-from-shell-initialize ()
  "Clears PATH before running `exec-path-from-shell-initialize' so that there's
no duplicate or conflicting entries."
  (setenv "PATH" "")

(defun mpereira/org-todo-with-date (&optional arg)
  (interactive "P")
  (cl-letf* ((org-read-date-prefer-future nil)
             (my-current-time (org-read-date t t nil "when:" nil nil nil))
             ((symbol-function #'org-current-effective-time)
              #'(lambda () my-current-time)))
    (org-todo arg)))

(defun iso8601-date-string ()
  "TODO: docstring."
  (let* ((time-zone-part (format-time-string "%z"))
         (iso8601-date-string (concat
                               (format-time-string "%Y-%m-%dT%T")
                               (substring time-zone-part 0 3)
                               (substring time-zone-part 3 5))))
    (message iso8601-date-string)
    (kill-new iso8601-date-string)))

(defun mpereira/align-clojure-csv-vector (start end)
  "Aligns Clojure CSV vectors by the whitespace separating elements."
  (interactive "r")
  (align-regexp start end
                "\"\\(\\s-+\\)\"" 1 1 t))

(defun mpereira/align-clojure-vector (start end)
  "Aligns Clojure vectors by the whitespace separating elements."
  (interactive "r")
  (align-regexp start end
                "\\(\\s-+\\)" 1 1 t))

Read secrets

I use code outside of this repository to write a GPG-encrypted secrets.el.gpg file that will set these variables.

  • mpereira/secret-circe-nickserv-password
  • mpereira/secret-openai-secret-api-key
  • mpereira/secret-org-gcal-client-id
  • mpereira/secret-org-gcal-client-secret
  • mpereira/secret-wolfram-alpha-app-id
(load-library (expand-file-name "secrets.el.gpg" user-emacs-directory))

Toggle buffer maximize

(defvar mpereira/toggle-buffer-maximize-window-configuration nil
  "A window configuration to return to when unmaximizing the buffer.")

(defvar mpereira/toggle-buffer-maximize-point nil
  "A point to return to when unmaximizing the buffer.")

(defvar mpereira/toggle-buffer-maximize-centered-p nil
  "Whether or not the buffer was maximixed in centered mode.")

(defun mpereira/toggle-buffer-maximize (&optional centered-p)
  "Saves the current window configuration and makes the current buffer occupy
the whole window. Calling it a second time will restore the saved window
  (if (bound-and-true-p mpereira/toggle-buffer-maximize-window-configuration)
        (set-window-configuration mpereira/toggle-buffer-maximize-window-configuration)
        (setq mpereira/toggle-buffer-maximize-window-configuration nil)
        (goto-char mpereira/toggle-buffer-maximize-point)
        (setq mpereira/toggle-buffer-maximize-point nil)
        (when mpereira/toggle-buffer-maximize-centered-p
          (call-interactively 'olivetti-mode)
          (setq mpereira/toggle-buffer-maximize-centered-p nil)))
      (setq mpereira/toggle-buffer-maximize-window-configuration
      (setq mpereira/toggle-buffer-maximize-point (point))
      (setq mpereira/toggle-buffer-maximize-centered-p centered-p)
      (when centered-p
        (call-interactively 'olivetti-mode)))))

Native compilation

(use-package emacs
  (native-comp-async-report-warnings-errors nil))

Reload directory local variables when saving .dir-locals.el files

Taken from Stack Overflow.

(defun mpereira/reload-dir-locals-for-current-buffer ()
  "Reload directory local variables on the current buffer."
  (let ((enable-local-variables :all))

(defun mpereira/reload-dir-locals-for-all-buffer-in-this-directory ()
  "Reload directory local variables on every buffer with the same
`default-directory' as the current buffer."
  (let ((dir default-directory))
    (dolist (buffer (buffer-list))
      (with-current-buffer buffer
        (when (equal default-directory dir))

(defun mpereira/enable-autoreload-for-dir-locals ()
  (when (and (buffer-file-name)
             (equal dir-locals-file
                    (file-name-nondirectory (buffer-file-name))))
    (add-hook (make-variable-buffer-local 'after-save-hook)

(add-hook 'emacs-lisp-mode-hook #'mpereira/enable-autoreload-for-dir-locals)


(require 'tramp)

Disable version control on tramp buffers to avoid freezes.

(setq vc-ignore-dir-regexp
      (format "\\(%s\\)\\|\\(%s\\)"

Don’t clean up recentf tramp buffers.

(setq recentf-auto-cleanup 'never)

Make Emacs not crazy slow under TRAMP.

Yes, this is still needed.

(defadvice projectile-project-root (around ignore-remote first activate)
  (unless (file-remote-p default-directory 'no-identification) ad-do-it))

This is supposedly faster than the default, scp.

(setq tramp-default-method "ssh")

SSH controlmaster settings are set in ~/.ssh/config.

(setq tramp-use-ssh-controlmaster-options nil)

This will put in effect PATH changes in the remote ~/.profile.

(add-to-list 'tramp-remote-path 'tramp-own-remote-path)

Store TRAMP auto-save files locally.

(setq tramp-auto-save-directory
      (expand-file-name "tramp-auto-save" user-emacs-directory))

A more representative name for this file.

(setq tramp-persistency-file-name
      (expand-file-name "tramp-connection-history" user-emacs-directory))

Cache SSH passwords during the whole Emacs session.

(setq password-cache-expiry nil)

Reuse SSH connections. Taken from the TRAMP FAQ.

Not tangled for now because it seems to affect remote LSP buffers under rust-analyzer.

(customize-set-variable 'tramp-ssh-controlmaster-options
                         "-o ControlPath=/tmp/ssh-tramp-%%r@%%h:%%p "
                         "-o ControlMaster=auto -o ControlPersist=yes"))


(require 'server)

(unless (server-running-p)


;; Don't append customizations to init.el.
(setq custom-file mpereira/custom-file)
(load custom-file 'noerror)

;; Don't ask whether custom themes are safe.
(setq custom-safe-themes t)

;; Avoid loading old bytecode instead of newer source.
;; Re: jka-compr:
;; NOTE: uncomment the next 3 lines if seeing issues like:
;;     Recursive load: "/Applications/"
;; (setq load-prefer-newer nil)
;; (require 'jka-compr)
;; (setq load-prefer-newer t)

;; Automatically scroll compilation buffers to the bottom.
(setq compilation-scroll-output t)

;; Show CRLF characters.
(setq inhibit-eol-conversion t)

;; Enable narrowing commands.
(put 'narrow-to-region 'disabled nil)

;; Don't complain when calling `list-timers'.
(put 'list-timers 'disabled nil)

;; Show matching parens.
(setq show-paren-delay 0)
(show-paren-mode 1)

;; Disable eldoc.
(global-eldoc-mode -1)

;; Break lines automatically in "text" buffers.

(setq mpereira/auto-fill-disabled-text-modes '(yaml-mode))

(defun mpereira/maybe-enable-auto-fill-mode ()
  "TODO: docstring."
  (when (not (-contains? mpereira/auto-fill-disabled-text-modes major-mode))
    (auto-fill-mode 1)))
(add-hook 'text-mode-hook 'mpereira/maybe-enable-auto-fill-mode)

;; Highlight current line.
(global-hl-line-mode t)

;; Provide undo/redo commands for window changes.
(winner-mode 1)

;; Don't lock files.
(setq create-lockfiles nil)

;; Make Finder's "Open with Emacs" create a buffer in the existing Emacs frame.
(setq ns-pop-up-frames nil)

;; macOS modifiers.
(when (display-graphic-p)
  (setq mac-command-modifier 'meta)
  ;; Setting "Option" to nil allows me to type umlauts with "Option+u".
  (setq mac-option-modifier nil)
  (setq mac-control-modifier 'control)
  (setq ns-function-modifier 'hyper))

;; By default Emacs thinks a sentence is a full-stop followed by 2 spaces. Make
;; it a full-stop and 1 space.
(setq sentence-end-double-space nil)

;; Switch to help buffer when it's opened.
(setq help-window-select t)

;; Don't recenter buffer point when point goes outside window. This prevents
;; centering the buffer when scrolling down its last line.
(setq scroll-conservatively 100)

;; Keep cursor position when scrolling.
(setq scroll-preserve-screen-position 1)

(dolist (hook '(prog-mode-hook text-mode-hook))
  (add-hook hook #'mpereira/enable-line-numbers))

;; Better unique buffer names for files with the same base name.
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)

;; Remember point position between sessions.
(require 'saveplace)
(save-place-mode t)

;; Save a bunch of session state stuff.
(require 'savehist)
(setq savehist-additional-variables '(regexp-search-ring)
      savehist-autosave-interval 60
      savehist-file (expand-file-name "savehist" user-emacs-directory))
(savehist-mode t)

;; `setq', `setq-default' and `setq-local' don't seem to work with symbol
;; variables, hence the absence of a `dolist' here.
(setq-default whitespace-line-column mpereira/fill-column
              fill-column mpereira/fill-column
              comment-column mpereira/fill-column)

(setq emacs-lisp-docstring-fill-column 'fill-column)

;; UTF8 stuff.
(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)

;; Tab first tries to indent the current line, and if the line was already
;; indented, then try to complete the thing at point.
(setq tab-always-indent 'complete)

;; Make it impossible to insert tabs.
(setq-default indent-tabs-mode nil)

;; Make TABs be displayed with a width of 2.
(setq-default tab-width 2)

;; Force packages relying on this general indentation variable (e.g., lsp-mode)
;; to indent with 2 spaces.
(setq-default standard-indent 2)

;; Week start on monday.
(setq calendar-week-start-day 1)

(setq select-enable-clipboard t
      select-enable-primary t
      save-interprogram-paste-before-kill t
      apropos-do-all t
      mouse-yank-at-point t
      require-final-newline t
      save-place-file (concat user-emacs-directory "places"))

(setq display-time-world-list '(("Europe/Berlin" "Munich")
                                ("America/Sao_Paulo" "São Paulo")))

File backups

make-backup-files and auto-save-default are set to t by default.

(setq backup-directory-alist `(("." . ,(concat user-emacs-directory "file-backups"))))
(setq tramp-backup-directory-alist `(("." . ,(concat user-emacs-directory "remote-file-backups"))))
(setq auto-save-file-name-transforms `((".*" ,(concat user-emacs-directory "auto-saves") t)))


Increase the amount of data read from processes

(setq read-process-output-max (* 1024 1024)) ; 1mb.

Asynchronous Org Babel tangling and byte recompilation

I have a file-local expression set at the end of the file for this. Note that the fourth argument to add-hook is important so that the hook is only installed for this file.

# Local Variables:
# eval: (add-hook 'before-save-hook 'async-literate-org-queue-run nil t)
# End:
(defcustom async-literate-org-org-file-name
  (expand-file-name "" user-emacs-directory)
  "TODO: docstring.")

(defcustom async-literate-org-el-file-name
  (expand-file-name "configuration.el" user-emacs-directory)
  "TODO: docstring.")

(defvar async-literate-org-cached-load-path
  (list (file-name-directory (locate-library "org"))
        (file-name-directory (locate-library "ob-tangle"))))

(defcustom async-literate-org-interval-seconds 20
  "TODO: docstring."
  :group 'async-literate-org
  :type 'integer)

(defcustom async-literate-org-queue-size-limit 3
  "TODO: docstring."
  :group 'async-literate-org
  :type 'integer)

(defvar async-literate-org-requests nil)

 (queue-pop 'async-literate-org-requests))

(defvar async-literate-org-timer nil)

(defun async-literate-org-disable ()
  (and (timerp async-literate-org-timer)
       (cancel-timer async-literate-org-timer)))

(defun async-literate-org-enable ()
  (setq async-literate-org-timer
         (lambda ()
           (when-let ((request (queue-pop 'async-literate-org-requests)))
             (message "Starting `async-literate-org-tangle-and-byte-compile' run")

(defun async-literate-org-queue-run ()
  (queue-push 'async-literate-org-requests

(defun async-literate-org-tangle-and-byte-compile ()
  "TODO: docstring."
  (let ((configuration-org-file-name async-literate-org-org-file-name)
        (async-literate-org-el-file-name async-literate-org-el-file-name)
        (org-babel-initialize 'mpereira/org-babel-initialize))
     `(lambda ()
        (nconc load-path ,async-literate-org-cached-load-path)

        (defalias 'org-babel-initialize
          ,(symbol-function org-babel-initialize))

          (require 'org)
          (require 'ob-tangle)
          (find-file ,configuration-org-file-name)
          (byte-compile-file ,async-literate-org-el-file-name)))
     `(lambda (result)
        (let ((inhibit-message t))
          (message (format (concat "`org-babel-tangle' and `byte-compile-file' called "
                                   "asynchronously for %s%s")
                           (if (string= "" result)

                             (format ". output: %s" result)))))))))

Show garbage collections in minibuffer

Not showing them for now (the default).

(setq garbage-collection-messages nil)

Garbage collection magic hack

(use-package gcmh
  (gcmh-mode 1)
  ((gcmh-idle-delay 5)
   (gcmh-verbose nil)))

**Don’t** delete trailing whitespace on save

The code below is just for demonstration purposes. It is not tangled.

(add-hook 'before-save-hook #'delete-trailing-whitespace)

Make cursor movement an order of magnitude faster


(setq auto-window-vscroll nil)

(setq fast-but-imprecise-scrolling 't)
;; NOTE: setting this to `0' like it was recommended in the article above seems
;; to cause fontification to happen in real time, which can be pretty slow in
;; large buffers. Giving it a delay seems to be better.
(setq jit-lock-defer-time 0.25)

Start-up profiler: esup

(use-package esup
  :pin melpa
  :commands (esup))


(use-package explain-pause-mode
  :ensure nil
  :vc (:fetcher github
       :repo "lastquestion/explain-pause-mode")
  (setq explain-pause-alert-via-message nil)
  ;; Override to use `profiler-report-profile-other-window'.
  (defun explain--profile-report-click-profile (button)
    "Click-handler when profile BUTTON is clicked in event profile report view."
    (let ((profile (button-get button 'profile)))
      (profiler-report-profile profile)))

  (add-hook 'after-init-hook #'explain-pause-mode))

Color themes


My favorite Dark themes:

  1. modus-vivendi
  2. doom-one
  3. chocolate
  4. doom-molokai
  5. monokai
  6. material
  7. nimbus
  8. doom-Ioskvem
  9. doom-dracula
  10. srcery

My favorite light themes:

  1. modus-operandi
  2. doom-one-light
  3. doom-acario-light
  4. doom-nord-light
  5. github
  6. material-light
  7. twilight-bright
  8. espresso
(use-package material-theme :defer t)
(use-package monokai-theme :defer t)
(use-package github-theme :defer t)
(use-package srcery-theme :defer t)
(use-package nimbus-theme :defer t)
(use-package espresso-theme :defer t)
(use-package twilight-bright-theme :defer t)
(use-package modus-themes :defer t)
(use-package doom-themes
  :defer t
(use-package tron-legacy-theme
  :ensure nil
  :defer t
  :vc (:fetcher github
       :repo "ianpan870102/tron-legacy-emacs-theme"))
(use-package chocolate-theme
  :ensure nil
  :defer t
  :vc (:fetcher github
       :repo "SavchenkoValeriy/emacs-chocolate-theme"))
(use-package vscode-dark-plus-theme)

(add-hook 'after-init-hook
          (lambda () (consult-theme mpereira/initial-theme))

Create hook for theme change

(defvar after-load-theme-hook nil
  "Hook run after a color theme is loaded using `load-theme'.")

(defadvice load-theme (after run-after-load-theme-hook activate)
  "Run `after-load-theme-hook'."
  (run-hooks 'after-load-theme-hook))

Change themes when changing macOS light or dark appearance

(add-hook 'ns-system-appearance-change-functions
          (lambda (appearance)
            (pcase appearance
              ('light (mpereira/load-light-theme))
              ('dark (mpereira/load-dark-theme)))))

Configure Mode Line

(with-eval-after-load "projectile"
  (with-eval-after-load "eshell"
    (with-eval-after-load "magit"
      (with-eval-after-load "lsp-mode"
        (defconst mpereira/mode-line-projectile
            (let ((face 'bold))
              (if (mpereira/remote-p)
                (when-let (project-name (projectile-project-name))
                   (propertize " " 'face face)
                   (propertize (format "%s" project-name) 'face face)
                   (propertize " " 'face face)))))))

        (defconst mpereira/mode-line-buffer
            (let ((modified-or-ro-symbol (cond
                                          ((and buffer-file-name
                                          (buffer-read-only ":RO")
                                          (t "")))
                  ;; Not using %b because it sometimes prepends the directory
                  ;; name.
                  (buffer-name* (file-name-nondirectory (buffer-name)))
                  (directory-face 'italic)
                  (buffer-name-face 'bold)
                  (modified-or-ro-symbol-face 'font-lock-comment-face)
                  (directory (if (mpereira/remote-p)
                               (let ((project-root (fast-project-find-file-project-root)))
                                 (if (and buffer-file-name project-root)
               (propertize " " 'face buffer-name-face)
               (propertize (format "%s" directory) 'face directory-face)
               (propertize (format "%s" buffer-name*) 'face buffer-name-face)
               (propertize modified-or-ro-symbol 'face modified-or-ro-symbol-face)
               (propertize " " 'face buffer-name-face)))))

        (defconst mpereira/mode-line-major-mode
            (propertize " %m  " 'face 'font-lock-comment-face)))

        (defconst mpereira/mode-line-buffer-position
            (unless eshell-mode
              (propertize " %p %l,%c " 'face 'font-lock-comment-face))))

        (setq-default mode-line-format (list mpereira/mode-line-projectile

        (defun mpereira/set-mode-line-padding ()
          (dolist (face '(mode-line mode-line-inactive))
            (let ((background (face-attribute face :background)))
              (set-face-attribute face nil :box `(:line-width 5
                                                  :color ,background)))))


        ;; Set modeline padding after running `load-theme'.
        (advice-add 'load-theme
                    (lambda (&rest _)

Configure Header Line

(defun mpereira/set-header-line-format ()
  (setq header-line-format '((which-function-mode ("" which-func-format " ")))))

(defun mpereira/clear-header-line-format ()
  (setq header-line-format nil))

(setq which-func-unknown "")

;; TODO: do I want this?
;; (add-hook 'prog-mode-hook #'which-function-mode)
;; (add-hook 'prog-mode-hook #'mpereira/set-header-line-format)

Vi emulation


(use-package evil
  (evil-v$-excludes-newline t)
  ;; NOTE: replacing the stock `undo' with `undo-tree-undo' due to the following
  ;; error:
  ;;   "primitive-undo: Unrecognized entry in undo list undo-tree-canary"
  ;; More details in
  (evil-undo-system 'undo-tree)
  (:keymaps '(evil-motion-state-map)
   ";" #'evil-ex
   ":" #'evil-command-window-ex)
  ;; Setup for `evil-collection'.
  (setq evil-want-integration t)
  (setq evil-want-keybinding nil)

  ;; FIXME: this correctly causes '*' to match on whole symbols (e.g., on a
  ;; Clojure file pressing '*' on '' matches the whole thing, instead of
  ;; just 'foo' or 'bar', BUT, it won't match '' in something like
  ;; '(', which I don't like.
  (setq-default evil-symbol-word-search t)

  (setq-default evil-shift-width 2)
  (setq evil-jumps-cross-buffers nil)
  (setq evil-want-Y-yank-to-eol t)
  (setq evil-want-C-u-scroll t)
  (setq evil-search-module 'evil-search)

  ;; Prevent the cursor from moving beyond the end of line.
  (setq evil-move-cursor-back nil)
  (setq evil-move-beyond-eol nil)

  (add-hook 'after-init-hook 'evil-normalize-keymaps)

  (evil-mode t)

  ;; Don't create a kill entry on every visual movement.
  ;; More details:
  (fset 'evil-visual-update-x-selection 'ignore))


(use-package evil-org
  :after evil org
  ;; evil-org unconditionally remaps `evil-quit' to `org-edit-src-abort' which I
  ;; don't like because it results in `evil-quit' keybinding invocations to not
  ;; quit the window.
  (when (command-remapping 'evil-quit nil org-src-mode-map)
    (define-key org-src-mode-map [remap evil-quit] nil))

  (add-hook 'org-mode-hook 'evil-org-mode)
  (add-hook 'evil-org-mode-hook
            (lambda ()
              (evil-org-set-key-theme '(operators
  (evil-define-key 'motion 'evil-org-mode
    ;; NOTE: overriding default which includes newline: `evil-org-end-of-line',
    ;; even though `evil-v$-excludes-newline' is set to `t'.
    (kbd "$") 'evil-end-of-line))


(use-package evil-exchange
  :after evil


(use-package evil-nerd-commenter
  :after evil)


(use-package evil-surround
  :after evil
  (global-evil-surround-mode t))


(use-package evil-matchit
  :after evil
  (global-evil-matchit-mode 1)

  (evilmi-load-plugin-rules '(js-mode
                            '(simple javascript html)))


(use-package evil-lion
  :after evil


(use-package evil-string-inflection
  :after evil)


(use-package evil-goggles
  :after evil


(use-package evil-multiedit
  :after evil
  (setq evil-multiedit-follow-matches t)

  ;; ;; This is so that C-n and C-p don't get mapped to `evil-multiedit-next' and
  ;; ;; `evil-multiedit-prev' respectively.
  ;; (setq evil-multiedit-dwim-motion-keys t)

  ;; Make matching case-sensitive.
  (defun make-evil-multiedit-case-sensitive (fn &rest args)
    (let ((case-fold-search (not iedit-case-sensitive)))
      (apply fn args)))

  (advice-add #'evil-multiedit-match-and-next :around #'make-evil-multiedit-case-sensitive)

   :states '(normal)
   "C-RET" 'evil-multiedit-toggle-marker-here
   "RET" 'evil-multiedit-toggle-or-restrict-region
   "C-n" 'evil-multiedit-match-and-next
   "C-p" 'evil-multiedit-match-and-prev
   "C-S-n" 'evil-multiedit-match-all)

   :states '(visual)
   "C-RET" 'evil-multiedit-toggle-marker-here
   "C-k" 'evil-multiedit-prev
   "C-j" 'evil-multiedit-next
   "C-n" 'evil-multiedit-match-symbol-and-next
   "C-p" 'evil-multiedit-match-symbol-and-prev
   "C-S-n" 'evil-multiedit-match-all)

   :states '(normal insert)
   :keymaps '(evil-multiedit-mode-map)
   "RET" 'evil-multiedit-toggle-or-restrict-region
   "C-n" 'evil-multiedit-match-symbol-and-next
   "C-p" 'evil-multiedit-match-symbol-and-prev
   ;; FIXME: combobulate mode is overriding this.
   "C-k" 'evil-multiedit-prev
   "C-j" 'evil-multiedit-next))


(use-package evil-collection
  :after evil
  (condition-case err
    (error (message "Error initializing evil-collection-init: %S" err))))



(setq org-directory (expand-file-name "org" mpereira/cloud-synced-directory))

(setq org-modules '(org-habit
;; Requiring these modules because org mode only does that for `org-modules'
;; defined prior to loading it.
(require 'org-habit)
(require 'org-protocol)
(require 'org-tempo)

(add-hook 'org-mode-hook
          (lambda ()
            (setq-local electric-pair-inhibit-predicate
                        `(lambda (c)
                           (if (char-equal c ?<) t (,electric-pair-inhibit-predicate c))))))

;; Pretty ellipsis.
(setq org-ellipsis "")

(setq org-log-done 'time)

;; Indent content at the outline level.
(setq org-adapt-indentation t)

(setq org-image-actual-width 640)

;; When this is set to `nil':
;; - `org-insert-heading' will insert a heading *before* the current heading.
;; - `org-insert-heading-after-current' will insert a heading *after* the
;;   current heading.
(setq org-insert-heading-respect-content nil)

;; TODO: is this needed?
(setq org-catch-invisible-edits 'show)

;; Show empty line between collapsed trees if they are separated by just 1
;; line break.
(setq org-cycle-separator-lines 1)

(setq org-attach-auto-tag "attachment")

(add-hook 'org-mode-hook #'mpereira/disable-line-numbers)

(setq org-tags-column -80)

;; FIXME: don't use hard-coded color.
;; (face-spec-set 'org-tag '((t :box (:color "gray30" :line-width 1))))

;; Open org link in the same window.
(setq org-link-frame-setup
'((vm . vm-visit-folder-other-frame)
    (vm-imap . vm-visit-imap-folder-other-frame)
    (gnus . org-gnus-no-new-news)
    (file . find-file)
    (wl . wl-other-frame)))

;; Don't ask when trying to edit a src block with an existing buffer.
(setq org-src-ask-before-returning-to-edit-buffer nil)

;; Don't indent src block content.
(setq org-edit-src-content-indentation 0)

;; Don't close all other windows when exiting the src buffer.
(setq org-src-window-setup 'current-window)

;; Open indirect buffer in the same window as the src buffer.
(setq org-indirect-buffer-display 'current-window)

;; Fontify code in code blocks.
(setq org-src-fontify-natively t)

;; Make TAB act as if it were issued in a buffer of the language’s major mode.
(setq org-src-tab-acts-natively t)

(setq org-todo-keywords '((sequence "TODO(t!)"

(setq org-capture-templates
      '(("t" "To-do" entry
         (file "")
         "* TODO %i%?")
        ("c" "Calendar" entry
         (file mpereira/org-calendar-file)
         "* %i%?\n  :PROPERTIES:\n  :calendar-id: %(caar mpereira/secret-org-gcal-file-alist)\n  :END:\n:org-gcal:\n%^{When?}t\n:END:")
        ("a" "Appointment" entry
         (file "")
         "* %i%?\n  %^{When?}t")
        ("j" "Journal for today" entry
         (file+olp+datetree "" "Journal")
         "* %U %^{Title}\n  %?"
         :tree-type week
         :empty-lines-after 1)
        ("p" "Web page" entry
         (file+datetree "~/org/")
         "* %(org-web-tools--org-link-for-url) :website:

%U %?" :clock-in t :clock-resume t :empty-lines 1)
        ("J" "Journal for some other day" entry
         (file+olp+datetree "" "Journal")
         "* %(format-time-string \"[%Y-%m-%d \\%a %H:%M]\") %^{Title}\n  %?"
         :tree-type week
         :time-prompt t)))

;; Start org note and capture buffers in insert state.
(add-hook 'org-log-buffer-setup-hook #'evil-insert-state)
(add-hook 'org-capture-mode-hook #'evil-insert-state)

(setq mpereira/org-files
      (-map (lambda (file-name)
              (expand-file-name file-name mpereira/org-directory))

;; Only refile to a few files.
(setq mpereira/org-refile-files mpereira/org-files)

(setq org-refile-targets '((mpereira/org-refile-files :maxlevel . 1)))

(setq org-outline-path-complete-in-steps nil)
(setq org-refile-allow-creating-parent-nodes 'confirm)
(setq org-refile-use-cache t)
(setq org-refile-use-outline-path 'file)

;; `org-reverse-note-order' set to true along with the two following hooks gets
;; us two things after refiling:
;; 1. Line breaks between top-level headings are maintained.
;; 2. Entries are sorted and top-level heading visibility is set to CHILDREN.
(setq org-reverse-note-order t)

(add-hook 'org-after-refile-insert-hook
          (lambda ()
            (mpereira/org-sort-parent-entries nil ?o)))

(defun mpereira/org-refile-update-cache ()

(add-hook 'org-after-sorting-entries-or-items-hook #'mpereira/org-cycle-cycle)

;; Save org buffers after some operations.
(dolist (hook '(org-refile
  (advice-add hook :after (lambda (&rest _) (org-save-all-org-buffers))))

(defun mpereira/org-unfill-toggle ()
  "Toggle filling/unfilling of the current region, or current paragraph if no
region active."
  (let (deactivate-mark
         (if (eq last-command this-command)
             (progn (setq this-command nil)
    (call-interactively 'org-fill-paragraph)))

(defun mpereira/org-insert-heading ()
  "`org-insert-heading' will break the current heading unless the pointer is at
the beginning of the line. This fixes that."
  (move-beginning-of-line nil)

 :keymaps '(org-mode-map)
 :states '(visual)
 "C-n" 'evil-multiedit-match-and-next
 "C-p" 'evil-multiedit-match-and-prev)

 :keymaps '(org-mode-map)
 :states '(normal)
 "t" 'org-todo
 "T" 'mpereira/org-insert-heading
 "M-t" 'org-insert-heading-after-current
 "(" 'org-up-element
 ")" 'org-down-element
 "k" 'evil-previous-visual-line
 "j" 'evil-next-visual-line
 "C-S-h" 'org-metaleft
 "C-S-j" 'org-metadown
 "C-S-k" 'org-metaup
 "C-S-l" 'org-metaright
 ;; TODO: make this call `org-babel-next-src-block' if there are no
 ;; sibling headings.
 "C-j" 'org-forward-heading-same-level
 ;; TODO: make this call `org-babel-previous-src-block' if there are
 ;; no sibling headings.
 "C-k" 'org-backward-heading-same-level
 ;; TODO: remove temporary keybinding.
 "C-n" 'org-babel-next-src-block
 ;; TODO: remove temporary keybinding.
 "C-p" 'org-babel-previous-src-block
 ;; TODO: add binding for `org-down-element'. Lisp analogous:
 ;; `lispyville-next-opening'.

;; org source blocks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun mpereira/maybe-org-edit-src-save ()
  (if (buffer-modified-p)
    (message "(No changes need to be saved)")))

 :states '(normal visual)
 :keymaps '(org-src-mode-map)
 :prefix mpereira/leader
 ;; Originally bound to `save-buffer' via the global keymap.
 "w" 'mpereira/maybe-org-edit-src-save
 ;; Originally bound to `org-edit-src-abort'.
 ;; FIXME: doesn't seem to be working?
 "q" 'evil-quit)

;; org capture buffer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 :states '(normal visual)
 :keymaps '(org-capture-mode-map)
 :prefix mpereira/leader
 ;; Originally bound to `save-buffer' via the global keymap.
 "or" 'org-capture-refile)


 :keymaps '(org-mode-map)
 :states '(normal visual)
 :prefix mpereira/leader
 "c" (lambda ()
       (org-clone-subtree-with-time-shift 1)))

 :keymaps '(org-mode-map)
 :states '(normal visual)
 :prefix mpereira/leader
 :infix "f"
 "o" 'consult-org-heading)

 :keymaps '(org-mode-map)
 :states '(normal visual)
 :prefix mpereira/leader
 :infix "e"
 "e" 'org-babel-execute-src-block)

 :keymaps '(org-mode-map)
 :states '(normal visual)
 "gq" 'mpereira/org-unfill-toggle)

 :keymaps '(org-mode-map text-mode-map)
 :states '(normal visual insert)
 "M-q" 'mpereira/org-unfill-toggle)

 :states '(normal visual)
 :prefix mpereira/leader
 :infix "o"
 "a" #'mpereira/open-or-build-main-org-agenda
 "A" #'mpereira/open-or-build-review-org-agenda
 "c" 'org-capture
 "Ci" 'org-clock-in
 "Co" 'org-clock-out
 "Cg" 'org-clock-goto
 "D" 'org-check-deadlines
 "l" 'org-store-link)

 :keymaps '(org-mode-map)
 :states '(normal visual)
 :prefix mpereira/leader
 :infix "o"
 "!" 'org-time-stamp-inactive
 "." 'org-time-stamp
 "/" 'org-search-view
 "\\" '(lambda ()
 "|" 'org-columns
 "Cc" 'org-clock-cancel
 "Cd" 'org-clock-display
 "Ci" 'org-clock-in
 "Cl" 'org-clock-in-last
 "Co" 'org-clock-out
 "d" 'org-deadline
 "D" 'org-archive-hierarchically
 "b" (lambda ()
 "B" 'outline-show-branches
 "f" 'org-attach
 "i" 'org-insert-link
 "d" 'org-cut-subtree
 "n" 'org-add-note
 "p" 'org-insert-link ; "p" for "paste".
 "P" 'org-priority
 "r" 'org-refile
 "X" (lambda ()
        '(4) 'org-babel-remove-result-one-or-many))
 "Rd" (lambda ()
         '(4) 'org-deadline))
 "Rs" (lambda ()
         '(4) 'org-schedule))
 "s" 'org-schedule
 "S" 'org-sort-entries
 "t" 'org-set-tags-command
 "u" 'org-toggle-link-display
 "w" 'org-web-tools-insert-web-page-as-entry
 "x" 'org-export-dispatch
 "y" 'org-store-link
 "Y" 'org-copy-subtree)

 :keymaps '(org-columns-map)
 "s" (lambda ()
       (org-sort-entries nil ?r)

Org Babel


(use-package verb
  (setq tempo-template-org-verb '("#+begin_src verb :wrap src ob-verb-response"
                                  nil '> n p n
                                  "#+end_src" >))
  (add-to-list 'org-tempo-tags '("<h" . tempo-template-org-verb)))


(defun mpereira/org-babel-initialize ()
  "TODO: docstring."
  (org-babel-do-load-languages 'org-babel-load-languages
                               '((shell . t)
                                 (emacs-lisp . t)
                                 (python . t)
                                 (verb . t)))

  (setq org-confirm-babel-evaluate nil)

  ;; By default, don't evaluate src blocks when exporting.
  (setq org-export-use-babel nil)

  ;; REVIEW: doing this causes :e to load the whole file contents into the src
  ;; block buffer.
  ;; (defadvice org-edit-src-code (around set-buffer-file-name activate compile)
  ;;   (let ((file-name (buffer-file-name)))
  ;;     ad-do-it
  ;;     (setq buffer-file-name file-name)))

(add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images)


Prevent o/O (evil-open-below/above) from scrolling window

It calls indent-according-to-mode which does the undesired scrolling.


(defun mpereira/evil-open-no-auto-indent (oldfun arg)
  (if (and evil-auto-indent
           (eq major-mode 'org-mode))
      (let ((evil-auto-indent nil))
        (funcall oldfun arg))
    (funcall oldfun arg)))

(advice-add #'evil-open-above :around #'mpereira/evil-open-no-auto-indent)
(advice-add #'evil-open-below :around #'mpereira/evil-open-no-auto-indent)

Align all tags in the buffer on tag changes

(defun mpereira/org-align-all-tags ()
  "Aligns all org tags in the buffer."
  (when (eq major-mode 'org-mode)
    (org-align-tags t)))

(add-hook 'org-after-tags-change-hook #'mpereira/org-align-all-tags)

Paste images in the clipboard directly into org buffers

(defun mpereira/org-paste-clipboard-image ()
  "TODO: docstring."
  (if (executable-find "pngpaste")
      (let ((image-file (concat temporary-file-directory
                                (make-temp-name "org-image-paste-")
        (call-process-shell-command (concat "pngpaste " image-file))
        (insert (concat  "#+CAPTION: " (read-string "Caption: ") "\n"))
        (insert (format "[[file:%s]]" image-file))
    (message "Requires pngpaste in PATH")))

Sort org entries by multiple properties

I have org trees for projects which I like sorted by:


I get that with M-x mpereira/org-sort-entries.

(defun mpereira/todo-to-int (todo)
  "Returns incrementally bigger integers for todo values.

Example: | todo  | int |
         | TODO  |   0 |
         | DOING |   1 |
         | DONE  |   2 |"
  (first (-non-nil
          (mapcar (lambda (keywords)
                    (let ((todo-seq
                           (-map (lambda (x) (first (split-string  x "(")))
                                 (rest keywords))))
                      (cl-position-if (lambda (x) (string= x todo)) todo-seq)))

(defun mpereira/todo-to-int-fixed (todo)
  "TODO: TODO docstring."
  (cdr (assoc todo '((DOING . 0)
                     (NEXT . 1)
                     (WAITING . 2)
                     (TODO . 3)
                     (SOMEDAY . 4)
                     (DONE . 5)
                     (CANCELLED . 6)))))

(defun mpereira/escape-string (s)
  "Makes strings safe to be printed with `message'."
  (s-replace-all '(("%" . "%%")) s))

(defun mpereira/org-todo-completed? (todo)
  (or (string= "DONE" todo)
      (string= "CANCELLED" todo)))

(defun mpereira/org-sort-key ()
  "Returns a sort key for an org entry based on:

| Priority | Order | Property |
|        1 | asc   | TODO     |
|        2 | asc   | PRIORITY |
|        3 | asc   | ALLTAGS  |
|        4 | desc  | CLOSED   |
|        5 | desc  | CREATED  |
|        6 | asc   | ITEM     |

if they aren't DONE or CANCELLED. In that case the sort key disregards tags,
giving priority to CREATED:

| Priority | Order | Property |
|        1 | asc   | TODO     |
|        2 | asc   | PRIORITY |
|        4 | desc  | CLOSED   |
|        5 | desc  | CREATED  |
|        6 | asc   | ITEM     |
  (let* ((todo-max (apply #'max (mapcar #'length org-todo-keywords)))
         (todo (org-entry-get (point) "TODO"))
         (todo-int (if (and todo (mpereira/todo-to-int-fixed (intern todo)))
                       (mpereira/todo-to-int-fixed (intern todo))
         (priority (org-entry-get (point) "PRIORITY"))
         (priority-int (if priority (string-to-char priority) org-default-priority))
         (date-int-min 10000000000000) ; YYYY=1000 mm=00 dd=00 HH=00 MM=00 SS=00
         (date-int-max 30000000000000) ; YYYY=3000 mm=00 dd=00 HH=00 MM=00 SS=00
         (closed (org-entry-get (point) "CLOSED"))
         (closed-int (if closed
                          (ts-format "%Y%m%d%H%M%S" (ts-parse-org closed)))
         (created (org-entry-get (point) "CREATED"))
         (created-int (if created
                           (ts-format "%Y%m%d%H%M%S" (ts-parse-org created)))
         (alltags-default "zzzzzzzzzz")
         (alltags (or (org-entry-get (point) "ALLTAGS")
         (item (org-entry-get (point) "ITEM"))
         (sort-key (format "%03d %03d %s %.10f %.10f %s"
                           (if (mpereira/org-todo-completed? todo)
                           (/ (float date-int-max) closed-int)
                           (/ (float date-int-max) created-int)
                           (mpereira/escape-string item))))

(defun mpereira/org-sort-entries ()
  "Sorts child entries based on `mpereiera/ort-sort-key'."
    (org-sort-entries nil ?f #'mpereira/org-sort-key)))

Org clock

;; org-clock stuff.
(setq org-clock-idle-time 15)
(setq org-clock-mode-line-total 'current)
;; Maybe automatically switching to DOING is not the best idea. Leaving it
;; commented for now.
;; (setq org-clock-in-switch-to-state "DOING")

;; Resume clocking task when emacs is restarted.
;; Save the running clock and all clock history when exiting Emacs, load it on
;; startup.
(setq org-clock-persist t)
;; Resume clocking task on clock-in if the clock is open.
(setq org-clock-in-resume t)
;; Do not prompt to resume an active clock, just resume it.
(setq org-clock-persist-query-resume nil)
;; Clock out when moving task to a done state.
(setq org-clock-out-when-done t)
;; Include current clocking task in clock reports.
(setq org-clock-report-include-clocking-task t)
;; Use pretty things for the clocktable.
(setq org-pretty-entities nil)


(use-package oauth2-auto
  :ensure nil
  :vc (:fetcher github
       :repo "telotortium/emacs-oauth2-auto"))

(use-package org-gcal
  (setq mpereira/org-gcal-directory (expand-file-name "gcal" org-directory))
  (org-gcal-client-id mpereira/secret-org-gcal-client-id)
  (org-gcal-client-secret mpereira/secret-org-gcal-client-secret)
  (org-gcal-file-alist `((""
  (org-gcal-auto-archive nil)
  (org-gcal-notify-p nil))

Org agenda

(require 'org-agenda)

(setq org-agenda-files (cons mpereira/org-gcal-directory mpereira/org-files))

;; Full screen org-agenda.
;; NOTE: this also makes stuff like `org-search-view' full screen.
(setq org-agenda-window-setup 'only-window)

;; Don't destroy window splits.
(setq org-agenda-restore-windows-after-quit t)

;; Show only the current instance of a repeating timestamp.
(setq org-agenda-repeating-timestamp-show-all nil)

;; Don't look for free-form time string in headline.
(setq org-agenda-search-headline-for-time nil)

(setq org-agenda-tags-column (* -1 mpereira/org-agenda-width))

(setq org-agenda-format-date 'mpereira/org-agenda-format-date)

;; Redo agenda after capturing.
(add-hook 'org-capture-after-finalize-hook 'org-agenda-maybe-redo)

;; Don't show empty agenda sections.
(add-hook 'org-agenda-finalize-hook #'mpereira/org-agenda-delete-empty-blocks)

;; Disable `evil-lion-mode' so that "g" keeps the mapping to
;; `org-agenda-maybe-redo'.
(add-hook 'org-agenda-finalize-hook (lambda () (evil-lion-mode -1)))

(defun mpereira/org-gcal-entry-at-point-p ()
  (when-let ((link (org-entry-get (point) "LINK")))
    (string-match "Go to gcal web page" link)))

(evil-set-initial-state 'org-agenda-mode 'normal)

 :keymaps '(org-agenda-mode-map)
 :states '(normal emacs)
 "/" 'org-agenda-filter-by-regexp
 "<" #'org-agenda-filter-by-category
 "c" (lambda ()
       ;; When capturing to a calendar org-gcal sends a network request that
       ;; reorders the calendar headings on completion, causing them to have a
       ;; different order than the agenda entries. Here we install a buffer
       ;; local hook that will sync the agenda entries with the calendar
       ;; headings.
       (add-hook 'org-capture-after-finalize-hook
                 (lambda ()
                   (run-at-time mpereira/org-gcal-request-timeout
 "d" #'org-agenda-deadline
 "f" #'org-attach
 "F" #'org-gcal-sync
 "g" #'mpereira/build-org-agenda
 "h" nil
 "i" #'org-agenda-clock-in
 "j" #'org-agenda-next-item
 "k" #'org-agenda-previous-item
 "l" nil
 "o" #'org-agenda-clock-out
 "n" #'org-agenda-add-note
 "q" #'org-agenda-quit
 "r" #'org-agenda-refile
 "s" #'org-agenda-schedule
 "q" #'mpereira/close-org-agenda
 "t" #'org-agenda-todo
 "T" #'org-agenda-set-tags
 "u" #'org-agenda-undo
 "w" nil
 "x" (lambda ()
         (let ((agenda-buffer (current-buffer)))
           (if (mpereira/org-gcal-entry-at-point-p)
                 ;; org-gcal only removes the calendar headings after the
                 ;; network request finishes.
                 (run-at-time mpereira/org-gcal-request-timeout
 "C-j" #'org-agenda-next-item
 "C-k" #'org-agenda-previous-item
 "C-f" #'scroll-up-command
 "C-b" #'scroll-down-command)

(defmacro calendar-action (func)
  `(lambda ()
     "TODO: docstring."
     (org-eval-in-calendar #'(,func 1))))

;; TODO: programmatically sync this with `calendar-mode-map' instead of
;; hard-coding keybindings.
 :keymaps '(org-read-date-minibuffer-local-map)
 "q" 'minibuffer-keyboard-quit
 "h" (calendar-action calendar-backward-day)
 "l" (calendar-action calendar-forward-day)
 "k" (calendar-action calendar-backward-week)
 "j" (calendar-action calendar-forward-week)
 "{" (calendar-action calendar-backward-month)
 "}" (calendar-action calendar-forward-month)
 "[" (calendar-action calendar-backward-year)
 "]" (calendar-action calendar-forward-year)
 "(" (calendar-action calendar-beginning-of-month)
 ")" (calendar-action calendar-end-of-month)
 "0" (calendar-action calendar-beginning-of-week)
 "$" (calendar-action calendar-end-of-week))

My custom persistent (cached) org agendas

My agendas are a bit heavy to build so I don’t kill their buffers (I use set-window-configuration instead to go back to the window configuration state right before opening the agenda, which is bound to q). I have keybindings (<leader> O a for the main agenda and <leader> O A for the review agenda) that display an existing agenda buffer, or build and display a fresh agenda buffer.

I’m also planning to add automatic and periodic background refreshing (and perhaps exporting) of the agenda buffers with run-with-idle-timer soon.

Agenda library

These are functions that I use in the actual custom agenda definitions.

(defun mpereira/org-current-subtree-state-p (state)
  (string= state (org-get-todo-state)))

(defun mpereira/org-up-heading-top-level ()
  "Move to the top level heading."
  (while (not (= 1 (org-outline-level)))

(defun mpereira/org-skip-all-but-first ()
  "Skip all but the first non-done entry."
  (let (should-skip-entry)
    (unless (mpereira/org-current-subtree-state-p "TODO")
      (setq should-skip-entry t))
      (while (and (not should-skip-entry) (org-goto-sibling t))
        (when (mpereira/org-current-subtree-state-p "TODO"))
        (setq should-skip-entry t)))
    (when should-skip-entry
      (or (outline-next-heading)
          (goto-char (point-max))))))

(defun mpereira/org-skip-subtree-if-habit ()
  "Skip an agenda entry if it has a STYLE property equal to \"habit\"."
  (let ((subtree-end (save-excursion (org-end-of-subtree t))))
    (if (string= (org-entry-get nil "STYLE") "habit")

(defun mpereira/org-skip-subtree-unless-habit ()
  "Skip an agenda entry unless it has a STYLE property equal to \"habit\"."
  (let ((subtree-end (save-excursion (org-end-of-subtree t))))
    (if (string= (org-entry-get nil "STYLE") "habit")

(defun mpereira/org-skip-inbox ()
  "Skip agenda entries coming from the inbox."
  (let ((subtree-end (save-excursion (org-end-of-subtree t))))
    (if (string= (org-get-category) "inbox")

(defun mpereira/org-skip-someday-projects-subheadings ()
  "Skip agenda entries under a project with state \"SOMEDAY\"."
  (let ((subtree-end (save-excursion (org-end-of-subtree t))))
    (if (mpereira/org-current-subtree-state-p "SOMEDAY")

(defun mpereira/org-entry-at-point-get (property)
  (org-entry-get (point) property))

(defun mpereira/org-entry-parent-root-heading ()
  "Returns the root heading for the entry at point. Makes the root heading
available in the kill ring if called interactively.

For example, in an org file like

* Emacs
** TODO Periodically refresh org agenda

the \"parent root heading\" for the TODO entry would be \"Emacs\".
the \"parent root heading\" for the \"Emacs\" entry would be nil.
  (let* ((outline-path (condition-case err
                           (org-get-outline-path t)
                          (message "Error calling `org-get-outline-path' with heading (%s): %s"
                                   (error-message-string err))
         (parent-heading-name (when (< 1 (length outline-path))
                                (car outline-path))))
    (when (and parent-heading-name
               (called-interactively-p 'any))
      (kill-new parent-heading-name))
    ;; `concat' turns nil into an empty string.
    (concat parent-heading-name)))

(defun mpereira/timestamp-type ()
   ((mpereira/org-entry-at-point-get "DEADLINE") "Deadline")
   ((mpereira/org-entry-at-point-get "SCHEDULED") "Scheduled")
   ((mpereira/org-entry-at-point-get "TIMESTAMP") "Timestamp")
   ((mpereira/org-entry-at-point-get "TIMESTAMP_IA") "Timestamp (inactive)")))

(defun mpereira/org-agenda-tags-prefix-format ()
  "Used in the \"tags\" section of the main org agenda.

This function is only necessary because multiple EXPRESSIONs would be required
to achieve the same outcome just with a single `org-agenda-prefix-format', and
that's not allowed."
  (let* ((timestamp (or (mpereira/org-entry-at-point-get "DEADLINE")
                        (mpereira/org-entry-at-point-get "SCHEDULED")
                        (mpereira/org-entry-at-point-get "TIMESTAMP")))
         (current (calendar-date-string (calendar-current-date)))
         (days (time-to-number-of-days (time-subtract
                                        (org-read-date nil t timestamp)
                                        (org-read-date nil t current))))
         (date (format-time-string "%d %b" (org-read-date t t timestamp))))
    (concat (format "%-20s"
                    (s-truncate 18
            (format "%11s: " (mpereira/timestamp-type))
            " "
            (format "%6s" (format "In %dd" days))
            " "
            (format "%8s" (format "(%s)" date)))))

(defun mpereira/org-agenda-format-date (date)
  "Format a DATE string for display in the daily/weekly agenda.
This function makes sure that dates are aligned for easy reading."
  (let* ((dayname (calendar-day-name date))
         (day (cadr date))
         (day-of-week (calendar-day-of-week date))
         (month (car date))
         (monthname (calendar-month-name month))
         (year (nth 2 date)))
    (format "\n%-9s %2d %s"
            dayname day monthname year)))

(defun mpereira/yesterday ()
  (time-subtract (current-time) (days-to-time 1)))

(defun mpereira/time-to-calendar-date (time)
  (let* ((decoded-time (decode-time time))
         (day (nth 3 decoded-time))
         (month (nth 4 decoded-time))
         (year (nth 5 decoded-time)))
    (list month day year)))

(defun mpereira/format-calendar-date-Y-m-d (calendar-date)
  (format-time-string "%Y-%m-%d"
                      (mpereira/calendar-date-to-time calendar-date)))

(defun mpereira/format-calendar-date-d-m-Y (calendar-date)
  (format-time-string "%d %B %Y"
                      (mpereira/calendar-date-to-time calendar-date)))

(defun mpereira/calendar-date-to-time (calendar-date)
  (let* ((day (calendar-extract-day calendar-date))
         (month (calendar-extract-month calendar-date))
         (year (calendar-extract-year calendar-date)))
    (encode-time 0 0 0 day month year)))

(defun mpereira/calendar-read-date (string)
  (mpereira/time-to-calendar-date (org-read-date t t string)))

(defun mpereira/org-agenda-date-week-start (string)
  "Returns the first day of the week at DATE."
  (let* ((calendar-date (mpereira/calendar-read-date string)))
       (mpereira/calendar-date-to-time calendar-date)
       (days-to-time (if (zerop (calendar-day-of-week calendar-date))
                         6 ;; magic.
                       (- (calendar-day-of-week calendar-date)

(defun mpereira/org-agenda-date-week-end (string)
  "Returns the last day of the week at DATE."
  (let* ((calendar-date (mpereira/calendar-read-date string)))
    (if (= (calendar-week-end-day) (calendar-day-of-week calendar-date))
         (mpereira/calendar-date-to-time calendar-date)
         (days-to-time (- 7 (calendar-day-of-week calendar-date)))))))))

(defun mpereira/org-agenda-review-prefix-format ()
  (let* ((timestamp (or (mpereira/org-entry-at-point-get "CLOSED")
                        (mpereira/org-entry-at-point-get "DEADLINE")
                        (mpereira/org-entry-at-point-get "TIMESTAMP")
                        (mpereira/org-entry-at-point-get "TIMESTAMP_IA")
                        (mpereira/org-entry-at-point-get "SCHEDULED")))
         (calendar-date (mpereira/calendar-read-date timestamp)))
    (format "%-20s  %s"
            (s-truncate 18 (mpereira/org-entry-parent-root-heading) "")
            (mpereira/format-calendar-date-Y-m-d calendar-date))))

(defun mpereira/org-agenda-review-search (start end)
  (concat "CLOSED>=\"<" start ">\""
          "CLOSED<=\"<" end ">\""
          "TIMESTAMP_IA>=\"<" start ">\""
          "TIMESTAMP_IA<=\"<" end ">\""
          "TIMESTAMP>=\"<" start ">\""
          "TIMESTAMP<=\"<" end ">\""))

(defun mpereira/org-agenda-delete-empty-blocks ()
  "Remove empty agenda blocks.
A block is identified as empty if there are fewer than 2 non-empty
lines in the block (excluding the line with
`org-agenda-block-separator' characters)."
  (when org-agenda-compact-blocks
    (user-error "Cannot delete empty compact blocks"))
  (setq buffer-read-only nil)
    (goto-char (point-min))
    (let* ((blank-line-re "^\\s-*$")
           (content-line-count (if (looking-at-p blank-line-re) 0 1))
           (start-pos (point))
           (block-re (if (stringp org-agenda-block-separator)
                       (format "%c\\{10,\\}" org-agenda-block-separator))))
      (while (and (not (eobp)) (forward-line))
         ((looking-at-p block-re)
          (when (< content-line-count 2)
            (delete-region start-pos (1+ (point-at-bol))))
          (setq start-pos (point))
          (setq content-line-count (if (looking-at-p blank-line-re) 0 1)))
         ((not (looking-at-p blank-line-re))
          (setq content-line-count (1+ content-line-count)))))
      (when (< content-line-count 2)
        (delete-region start-pos (point-max)))
      (goto-char (point-min))
      ;; The above strategy can leave a separator line at the beginning of the
      ;; buffer.
      (when (looking-at-p block-re)
        (delete-region (point) (1+ (point-at-eol))))))
  (setq buffer-read-only t))

Main agenda

(defvar mpereira/main-org-agenda-buffer-name "*Main Org Agenda*"
  "The name of the main org agenda.")

(defvar mpereira/main-org-agenda-last-built nil
  "The last time the main org agenda was built.")

(defvar mpereira/main-org-agenda-previous-window-configuration nil
  "A window configuration to return to when closing the main org agenda.")

(defvar mpereira/main-org-agenda-previous-point nil
  "A point to return to when closing the main org agenda.")

(defun mpereira/build-main-org-agenda ()
  "Build and display the main org agenda."
  ;; Remember that EXPRESSION (e.g. "%(foo)") can be used only once per
  ;; `org-agenda-prefix-format'.
  (let* ((todo-prefix-format
          (concat "  "
                  ;; CATEGORY property or file name.
                  " "
                  ;; Truncated root heading.
                  "%-20(s-truncate 18 (mpereira/org-entry-parent-root-heading) \"\")"
                  " "
                  ;; Time of day specification.
                  " "
                  ;; Scheduling/Deadline information.
          (concat "  "
                  ;; CATEGORY property or file name.
                  " "
                  "  "))
         (agenda-ignore-todos '(list "DOING" "WAITING" "DONE" "CANCELLED"))
          `((todo "DOING"
                  ((org-agenda-overriding-header "\nDoing\n")
                   (org-agenda-prefix-format ,todo-prefix-format)))
            (todo "WAITING"
                  ((org-agenda-overriding-header "\nWaiting\n")
                   (org-agenda-prefix-format ,todo-prefix-format)))
            (agenda ""
                       "\nToday "
                       "(" (format-time-string "%A, %B %d" (current-time)) ")"))
                     (org-deadline-warning-days 0)
                     (org-agenda-span 'day)
                     (org-agenda-use-time-grid t)
                     (org-agenda-format-date "")
                     (org-agenda-prefix-format ,todo-prefix-format)
                     (org-habit-show-habits nil)
                     ;; Not using something like (org-agenda-skip-entry-if
                     ;; 'nottodo '("TODO")) here because I want non-TODO
                     ;; headings (e.g. calendar events) showing up here as well.
                      (quote (org-agenda-skip-entry-if 'todo ,agenda-ignore-todos)))))
            (agenda ""
                    ((org-agenda-overriding-header "\nNext 7 Days")
                     (org-agenda-start-day "+1d")
                     (org-agenda-span 'week)
                     (org-agenda-start-on-weekday nil)
                     (org-agenda-prefix-format ,todo-prefix-format)
                     ;; Not using something like (org-agenda-skip-entry-if
                     ;; 'nottodo '("TODO")) here because I want non-TODO
                     ;; headings (e.g. calendar events) showing up here as well.
                      (quote (org-agenda-skip-entry-if 'todo ,agenda-ignore-todos)))))
            (tags (concat "SCHEDULED>=\"<+8d>\"&SCHEDULED<=\"<+30d>\""
                  ((org-agenda-overriding-header "\nComing up\n")
                   (org-agenda-prefix-format ,tags-prefix-format)
                   (org-agenda-sorting-strategy '(timestamp-up))))))
         (inbox-file (expand-file-name "" org-directory))
         (inbox-buffer (find-file-noselect inbox-file))
         (inbox (with-current-buffer inbox-buffer
                  (org-element-contents (org-element-parse-buffer 'headline))))
         (_ (when inbox
               `(todo "TODO"
                      ((org-agenda-overriding-header "\nInbox\n")
                       (org-agenda-prefix-format ,todo-prefix-format)
                       (org-agenda-files (list ,inbox-file)))))))
         (org-agenda-buffer-name mpereira/main-org-agenda-buffer-name)
         (org-agenda-custom-commands (list
                                       "c" "Main agenda"
                                          ,(s-repeat mpereira/org-agenda-width "-")))))))
    (org-agenda nil "c")
    (with-current-buffer (get-buffer mpereira/main-org-agenda-buffer-name)
      (setq-local olivetti-body-width mpereira/org-agenda-width)
    (setq mpereira/main-org-agenda-last-built (ts-now))))

(defun mpereira/open-or-build-main-org-agenda ()
  "Display main org agenda if it was already built. Build and display it
  (let ((org-agenda-buffer (get-buffer mpereira/main-org-agenda-buffer-name)))
    (setq mpereira/main-org-agenda-previous-window-configuration
    (setq mpereira/main-org-agenda-previous-point (point))
    (if (and (bufferp org-agenda-buffer)
          (switch-to-buffer org-agenda-buffer)
          (let ((last-built (ts-human-format-duration
                             (ts-difference (ts-now)
            (message (format "Last built %s ago" last-built))))
        (message "Built now")))))

Review agenda

(defvar mpereira/review-org-agenda-buffer-name "*Review Org Agenda*"
  "The name of the review org agenda.")

(defvar mpereira/review-org-agenda-last-built nil
  "The last time the review org agenda was built.")

(defvar mpereira/review-org-agenda-previous-window-configuration nil
  "A window configuration to return to when closing the review org agenda.")

(defvar mpereira/review-org-agenda-previous-point nil
  "A point to return to when closing the review org agenda.")

(defun mpereira/build-review-org-agenda ()
  "Build and display the review org agenda."
  (let* ((single-day-prefix-format " %-10c %?-12t% s")
         (multi-day-prefix-format " %-10c %(mpereira/org-agenda-review-prefix-format) ")
          `((tags ,(mpereira/org-agenda-review-search "today" "+1d")
                     "\nDone today "
                     "(" (format-time-string "%A, %B %d" (current-time)) ")\n"))
                   (org-agenda-prefix-format ,single-day-prefix-format)))
            (tags ,(mpereira/org-agenda-review-search "-1d" "today")
                     "\nDone yesterday "
                     "(" (format-time-string "%A, %B %d" (mpereira/yesterday)) ")\n"))
                   (org-agenda-prefix-format ,single-day-prefix-format)))
            (tags ,(mpereira/org-agenda-review-search
                      (mpereira/calendar-read-date "today")))
                     (mpereira/calendar-read-date "today")))
                  ((org-agenda-overriding-header "\nDone this week\n")
                   (org-agenda-prefix-format ,multi-day-prefix-format)
                   (org-agenda-sorting-strategy '(timestamp-up))
                   (org-agenda-show-all-dates t)))
            (tags (mpereira/org-agenda-review-search
                     (mpereira/calendar-read-date "-1w")))
                     (mpereira/calendar-read-date "-1w"))))
                  ((org-agenda-overriding-header "\nDone last week\n")
                   (org-agenda-prefix-format ,multi-day-prefix-format)
                   (org-agenda-show-all-dates t)
                   (org-agenda-sorting-strategy '(timestamp-down))))))
         (org-agenda-buffer-name mpereira/review-org-agenda-buffer-name)
         (org-agenda-custom-commands (list
                                       "c" "Review agenda"
                                          ,(s-repeat mpereira/org-agenda-width "-")))))))
    (org-agenda nil "c")
    (with-current-buffer (get-buffer mpereira/review-org-agenda-buffer-name)
      (setq-local olivetti-body-width mpereira/org-agenda-width)
    (setq mpereira/review-org-agenda-last-built (ts-now))))

(defun mpereira/open-or-build-review-org-agenda ()
  "Display review org agenda if it was already built. Build and display it
  (let ((org-agenda-buffer (get-buffer mpereira/review-org-agenda-buffer-name)))
    (setq mpereira/review-org-agenda-previous-window-configuration
    (setq mpereira/review-org-agenda-previous-point (point))
    (if (bufferp org-agenda-buffer)
          (switch-to-buffer org-agenda-buffer)
          (let ((last-built (ts-human-format-duration
            (message (format "Last built %s ago" last-built))))
        (message "Built now")))))


(defun mpereira/build-org-agenda ()
  "Build the last opened org agenda."
   ((and mpereira/main-org-agenda-previous-window-configuration
         (not mpereira/review-org-agenda-previous-window-configuration))
    (funcall #'mpereira/build-main-org-agenda))
   ((and mpereira/review-org-agenda-previous-window-configuration
         (not mpereira/main-org-agenda-previous-window-configuration))
    (funcall #'mpereira/build-review-org-agenda))
   ((and mpereira/main-org-agenda-previous-window-configuration
    (if (ts<= mpereira/main-org-agenda-last-built
        (funcall #'mpereira/build-review-org-agenda)
      (funcall #'mpereira/build-main-org-agenda)))))

(defun mpereira/close-org-agenda ()
  "Close the currently opened org agenda and restore the previous window
configuration and point position."
  (let ((close-review-org-agenda
         (lambda ()
           (setq mpereira/review-org-agenda-previous-window-configuration nil)
           (goto-char mpereira/review-org-agenda-previous-point)
           (setq mpereira/review-org-agenda-previous-point nil)))
         (lambda ()
           (setq mpereira/main-org-agenda-previous-window-configuration nil)
           (goto-char mpereira/main-org-agenda-previous-point)
           (setq mpereira/main-org-agenda-previous-point nil))))
     ((string= mpereira/main-org-agenda-buffer-name (buffer-name))
      (funcall close-main-org-agenda))
     ((string= mpereira/review-org-agenda-buffer-name (buffer-name))
      (funcall close-review-org-agenda))
     (t (mpereira/kill-buffer-and-maybe-window)))))


(use-package shrface
  (with-eval-after-load 'eww
    (add-hook 'eww-after-render-hook 'shrface-mode)))


(use-package outorg
  :ensure nil
  :vc (:fetcher github
       :repo "alphapapa/outorg")
  (defun mpereira/outorg-edit-as-org ()
    "TODO: docstring."
    (let ((byte-compile-warnings '(not obsolete)))

  (defun mpereira/outorg-copy-edits-and-exit ()
    "TODO: docstring."
    (if (string= outorg-edit-buffer-name (buffer-name))
      (message "Not in the %s buffer" outorg-edit-buffer-name))))

(use-package outshine
  :ensure nil
  :vc (:fetcher github
       :repo "alphapapa/outshine")
  (add-hook 'emacs-lisp-mode-hook 'outshine-mode))


It’s very convenient to capture a screenshot to the clipboard with macOS (Shift-Cmd-5) and then paste it into an Org buffer with org-download-clipboard.

(defun filesystem-friendly-file-path (s)
  "Sanitizes string to be filesystem friendly."
  (replace-regexp-in-string "[^[:alpha:]_-]" "_" s))

(use-package org-download
  (org-download-screenshot-method "screencapture -i %s")
  (org-download-image-dir (concat mpereira/org-directory "/download"))
  ;; MONKEYPATCH(org-download).
  (defun org-download-get-heading (lvl)
    "Return the heading of the current entry's LVL level parent."
      (let ((cur-lvl (org-current-level)))
        (if cur-lvl
              (unless (= cur-lvl 1)
                (org-up-heading-all (- (1- (org-current-level)) lvl)))
              (let ((heading (nth 4 (org-heading-components))))
                (if heading
                      " " "_"


org-web-tools-insert-web-page-as-entry is so useful. I use it to capture websites into my to-read list.

(use-package org-web-tools)


From Emacs DWIM: do what I mean.

(declare-function org-in-regexp
                  (regexp &optional nlines visually))

(defun mpereira/org-insert-link-dwim ()
  "Like `org-insert-link' but with personal dwim preferences."
  (let* ((point-in-link (org-in-regexp org-link-any-re 1))
         (clipboard-url (when (string-match-p "^http" (current-kill 0))
                          (current-kill 0)))
         (region-content (when (region-active-p)
                           (buffer-substring-no-properties (region-beginning)
    (cond ((and region-content clipboard-url (not point-in-link))
           (delete-region (region-beginning) (region-end))
           (insert (org-make-link-string clipboard-url region-content)))
          ((and clipboard-url (not point-in-link))
           (insert (org-make-link-string
                    (read-string "title: "
                                 (with-current-buffer (url-retrieve-synchronously clipboard-url)
                                   (dom-text (car
                                              (dom-by-tag (libxml-parse-html-region
           (call-interactively 'org-insert-link)))))


Add a unique ID property to headings when they are created.

(add-hook 'org-insert-heading-hook 'org-id-get-create)

Have org-store-link copy an org-id reference link instead of a file reference link.

(setq org-id-link-to-org-use-id 'create-if-interactive)


(add-to-list 'org-modules 'org-expiry)

(require 'org-expiry)

(setq org-expiry-inactive-timestamps t)


(add-hook 'org-capture-before-finalize-hook 'org-expiry-insert-created)


(use-package org-bullets
  :after org
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))))


(use-package org-make-toc
  :after org)


(use-package org-tree-slide)


(use-package org-sidebar)


(use-package org-pomodoro
  (setq org-pomodoro-format "%s"))


FIXME: this seems to insert unwanted whitespace between the parent and the first child tree.

(use-package org-archive-hierarchically
  :ensure nil
  :vc (:fetcher gitlab
       :repo "andersjohansson/org-archive-hierarchically"))


(use-package org-autonum
  :ensure nil
  :vc (:fetcher github
       :repo "nma83/org-autonum"))

(defun re-seq (regexp string)
  "Get a list of all regexp matches in a string."
    (let ((pos 0)
      (while (string-match regexp string pos)
        (push (match-string 0 string) matches)
        (setq pos (match-end 0)))

;; FIXME: the `'tree' scope doesn't seem to be working. Calling this
;; function on a heading with subsequent siblings will consider the
;; first heading the root of all the other ones.
;; This is because of the promote/demote hack.
(defun mpereira/org-enumerate-headings ()
  "TODO: docstring."
    (let ((spacing nil)
          (current-level (org-current-level))
          (enumeration '()))
      (dotimes (i (- current-level 1))
       (lambda ()
         ;; We subtract 1 because we want the relevant outlines being
         ;; considered to have level 1.
         (setq level (- (org-outline-level) 1))
         (print (list (list 'level level) (list 'enumeration enumeration)))
         ;; Skip the tree root entry.
         (when (> level 0)
           ;; Move to start of heading text.
           (re-search-forward "\\* " (line-end-position) t)
           (if (< (length enumeration) level)
               ;; Expand enumeration to next level.
               (setq enumeration (append enumeration '(0)))
             (if (not (= (length enumeration) level))
                 ;; Prune enumeration to current level.
                 (setq enumeration (butlast enumeration
                                            (- (length enumeration)
           ;; Increment last enumeration number.
           (setq enumeration (append (butlast enumeration 1)
                                     (list (1+ (car (last enumeration 1))))))
           (setq enumeration-string (concat
                                      'number-to-string enumeration ".")
                                     ". "))
           ;; FIXME: this isn't working.
           (if (re-search-forward (concat "* "
                                          " ")
               ;; Replace existing enumeration if it's different.
               (unless (string= (match-string 0) enumeration-string)
                 (replace-match enumeration-string nil nil))
             ;; Insert new enumeration.
             (insert enumeration-string))))
      (dotimes (i (- current-level 1))


(use-package ob-async)


(use-package ox-jira)


(use-package ox-twbs)


(use-package ox-gfm)


(use-package ox-hugo)


(use-package ox-pandoc)

File management

Don’t change the inode of hard links on save

(setq backup-by-copying-when-linked t)


(setq dired-recursive-copies 'always)
(setq dired-recursive-deletes 'always)
(setq delete-by-moving-to-trash t)

(if (not (mpereira/is-gnu-program "ls"))
      (warn "Not GNU ls: %s" (s-trim (shell-command-to-string "which ls")))))

(setq dired-listing-switches "-AFhlv --group-directories-first")

(setq find-ls-option ;; applies to `find-name-dired'
      '("-print0 | xargs -0 ls -AFlv --group-directories-first" . "-AFlv --group-directories-first"))

(add-hook 'dired-mode-hook 'dired-hide-details-mode)

(dired-async-mode 1)

(require 'wdired)
(setq wdired-allow-to-change-permissions t)

(require 'dired-x)

 :keymaps '(dired-mode-map)
 :states '(normal visual)
 "(" 'dired-subtree-up
 ";" nil ; originally the first keystroke for encryption-related bindings.
 "C-9" 'dired-hide-details-mode
 "C-j" 'dired-next-dirline
 "C-k" 'dired-prev-dirline
 "M-c" 'dired-ranger-copy
 "M-v" 'dired-ranger-paste)


(use-package dired-quick-sort
  :general (:keymaps '(dired-mode-map)
            :states '(normal visual)
            ;; NOTE: "s" isn't used, and the default "S" is overriden by
            ;; evil-collection probably.
            "s" #'hydra-dired-quick-sort/body)


(use-package dired-ranger)


Disabled for now. Too overwhelming when combined with all-the-icons-dired.

(use-package dired-plus
  :ensure nil
  :vc (:fetcher github
       :repo "emacsmirror/dired-plus"))


Disabled for now. Doesn’t allow navigating README, doesn’t render markdown.

(use-package dired-show-readme
  :ensure nil
  :vc (:fetcher gitlab
       :repo "kisaragi-hiu/dired-show-readme")
  (add-hook 'dired-mode-hook 'dired-show-readme-mode))


(use-package dired-subtree
  :after dired)


(use-package reveal-in-osx-finder)


(use-package sudo-edit)

Shell, terminal


(use-package with-editor
  (add-hook 'eshell-mode-hook 'with-editor-export-editor)
  (add-hook 'term-exec-hook 'with-editor-export-editor)
  (add-hook 'shell-mode-hook 'with-editor-export-editor)

  (add-hook 'with-editor-mode-hook 'evil-insert-state))


(add-hook 'shell-mode-hook 'buffer-disable-undo)

 :keymaps '(shell-mode-map)
 :states '(insert)
 "C-l" 'comint-clear-buffer)


(require 'eshell)
(require 'em-dirs) ;; for `eshell/pwd'.
(require 'em-smart)
(require 'em-tramp)

;; Don't display the "Welcome to the Emacs shell" banner.
(setq eshell-banner-message "")

;; Make it possible to get a remote eshell buffer.
(add-to-list 'eshell-modules-list 'eshell-tramp)

(setenv "LANG" "en_US.UTF-8")
(setenv "LC_ALL" "en_US.UTF-8")
(setenv "LC_CTYPE" "en_US.UTF-8")

;; Don't page shell output.
(setenv "PAGER" "cat")

(setq eshell-scroll-to-bottom-on-input 'all)
(setq eshell-buffer-maximum-lines 20000)
(setq eshell-history-size 1000000)
(setq eshell-error-if-no-glob t)
(setq eshell-hist-ignoredups t)
(setq eshell-save-history-on-exit t)
;; `find` and `chmod` behave differently on eshell than unix shells. Prefer unix
;; behavior.
(setq eshell-prefer-lisp-functions nil)

(defun eshell/clear ()
  "Clears buffer while preserving input."
  (let* ((inhibit-read-only t)
         (input (eshell-get-old-input)))
    (insert input)))

(defun mpereira/eshell-clear ()

;; Inspired by Prot's.
(defun mpereira/eshell-complete-recent-directory (&optional arg)
  "Switch to a recent `eshell' directory using completion.
With \\[universal-argument] also open the directory in a `dired' buffer."
  (interactive "P")
  (let ((recent-dirs (delete-dups (ring-elements eshell-last-dir-ring))))
    (let ((chosen-dir (completing-read "Switch to recent dir: " recent-dirs nil t)))
      (when chosen-dir
        (insert chosen-dir)
        (when arg
          (dired chosen-dir))))))

;; Inspired by Prot's.
(defun mpereira/eshell-switch-to-last-output-buffer ()
  "Produce a buffer with output of last `eshell' command."
  (let ((eshell-output (kill-region (eshell-beginning-of-output)
    (with-current-buffer (get-buffer-create "*last-eshell-output*")
      ;; TODO: do it with `insert' and `delete-region'?
      (goto-char (point-min))
      (display-buffer (current-buffer)))))

;; Inspired by Prot's.
(defun mpereira/eshell-complete-redirect-to-buffer ()
  "Complete the syntax for appending to a buffer via `eshell'."
   (concat " >>> #<" (read-buffer-to-switch "Redirect to buffer:") ">")))

;; eshell-mode-map needs to be configured in an `eshell-mode-hook'.
(defun mpereira/initialize-eshell ()
  ;; Completion functions depend on pcomplete.
  ;; Don't use TAB for cycling through candidates.
  (setq pcomplete-cycle-completions nil)
  (setq pcomplete-ignore-case t)

  (eshell/alias "e" "find-file $1")


  ;; Eshell needs this variable set in addition to the PATH environment variable.
  (setq eshell-path-env (getenv "PATH"))

   :keymaps '(eshell-mode-map)
   "C-c C-c" 'eshell-interrupt-process
   "C-S-k" 'mpereira/eshell-switch-to-last-output-buffer
   "C->" 'mpereira/eshell-complete-redirect-to-buffer)

   :states '(normal visual)
   :keymaps '(eshell-mode-map)
   "0" 'eshell-bol
   "C-j" 'eshell-next-prompt
   "C-k" 'eshell-previous-prompt)

   :states '(insert)
   :keymaps '(eshell-mode-map)
   ;; TODO: `eshell-{previous,next}-matching-input-from-input' only work with
   ;; prefix inputs, like "git". They don't do fuzzy matching.
   ;; TODO: when on an empty prompt and going up and back down (or down and back
   ;; up), make it so that the prompt is empty again instead of cycling back to
   ;; the first input.
   "<tab>" 'completion-at-point
   "C-k" 'eshell-previous-matching-input-from-input
   "C-j" 'eshell-next-matching-input-from-input
   "C-/" 'consult-history
   "C-l" 'mpereira/eshell-clear)

  ;; REVIEW(maybe-unnecessary): workaround for a bug. When an eshell buffer is
  ;; created the `eshell-mode-map' mappings are not set up, even through
  ;; `eshell-mode-map' is correctly defined. Going to normal state sets them up
  ;; for some reason.

(add-hook 'eshell-mode-hook 'mpereira/initialize-eshell)

(defun mpereira/remote-p ()
  (tramp-tramp-file-p default-directory))

(defun mpereira/remote-user ()
  "Return remote user name."
  (or (tramp-file-name-user (tramp-dissect-file-name default-directory))

(defun mpereira/remote-host ()
  "Return remote host."
  ;; `tramp-file-name-real-host' is removed and replaced by
  ;; `tramp-file-name-host' in Emacs 26, see
  (if (fboundp 'tramp-file-name-real-host)
      (tramp-file-name-real-host (tramp-dissect-file-name default-directory))
    (tramp-file-name-host (tramp-dissect-file-name default-directory))))

(defun mpereira/eshell-prompt ()
  (let ((user-name (if (mpereira/remote-p)
        (host-name (if (mpereira/remote-p)
     (propertize user-name 'face '(:foreground "green"))
     " "
     (propertize "at" 'face 'eshell-ls-unreadable)
     " "
     (propertize host-name 'face '(:foreground "cyan"))
     " "
     (propertize "in" 'face 'eshell-ls-unreadable)
     " "
     (propertize (mpereira/short-directory-path
                 'face 'dired-directory)
     (propertize (if (= (user-uid) 0)
                 'face 'eshell-prompt)
     " ")))

(setq eshell-prompt-function 'mpereira/eshell-prompt)
(setq eshell-prompt-regexp "^[$#] ")

;; Make eshell append to history after each command.
;; (setq eshell-save-history-on-exit nil)
;; (defun eshell-append-history ()
;;   "Call `eshell-write-history' with the `append' parameter set to `t'."
;;   (when eshell-history-ring
;;     (let ((newest-cmd-ring (make-ring 1)))
;;       (ring-insert newest-cmd-ring (car (ring-elements eshell-history-ring)))
;;       (let ((eshell-history-ring newest-cmd-ring))
;;         (eshell-write-history eshell-history-file-name t)))))
;; (add-hook 'eshell-pre-command-hook #'eshell-append-history)

;; Shared history.
;; TODO: make this per project?
(defvar mpereira/eshell-history-global-ring nil
  "The history ring shared across Eshell sessions.")

(defun mpereira/eshell-hist-use-global-history ()
  "Make Eshell history shared across different sessions."
  (unless mpereira/eshell-history-global-ring
    (when eshell-history-file-name
      (eshell-read-history nil t))
    (setq mpereira/eshell-history-global-ring
          (or eshell-history-ring (make-ring eshell-history-size))))
  (setq eshell-history-ring mpereira/eshell-history-global-ring))

(add-hook 'eshell-mode-hook #'mpereira/eshell-hist-use-global-history)

Fix eshell autocomplete

This fix provided by Ethan Leba essentially reverts the commit which introduced the bug. Tracking bug in debbugs: #48995.

Without it the following happens when trying to autocomplete a file:

$ ls
$ ./f<TAB>

With it:

$ ls
$ ./f<TAB>
$ ./

And other weird completions.

This is apparently fixed on Emacs 29 so it’s loaded conditionally.

(use-package emacs
  :when (< emacs-major-version 29)
  (defun eshell--complete-commands-list ()
    "Generate list of applicable, visible commands."
    (let ((filename (pcomplete-arg)) glob-name)
      (if (file-name-directory filename)
          (if eshell-force-execution
              (pcomplete-dirs-or-entries nil #'file-readable-p)
        (if (and (> (length filename) 0)
                 (eq (aref filename 0) eshell-explicit-command-char))
            (setq filename (substring filename 1)
                  pcomplete-stub filename
                  glob-name t))
        (let* ((paths (eshell-get-path))
               (cwd (file-name-as-directory
                     (expand-file-name default-directory)))
               (path "") (comps-in-path ())
               (file "") (filepath "") (completions ()))
          ;; Go thru each path in the search path, finding completions.
          (while paths
            (setq path (file-name-as-directory
                        (expand-file-name (or (car paths) ".")))
                  (and (file-accessible-directory-p path)
                       (file-name-all-completions filename path)))
            ;; Go thru each completion found, to see whether it should
            ;; be used.
            (while comps-in-path
              (setq file (car comps-in-path)
                    filepath (concat path file))
              (if (and (not (member file completions)) ;
                       (or (string-equal path cwd)
                           (not (file-directory-p filepath)))
                       (if eshell-force-execution
                           (file-readable-p filepath)
                         (file-executable-p filepath)))
                  (setq completions (cons file completions)))
              (setq comps-in-path (cdr comps-in-path)))
            (setq paths (cdr paths)))
          ;; Add aliases which are currently visible, and Lisp functions.
           (if glob-name
             (setq completions
                   (append (if (fboundp 'eshell-alias-completions)
                               (eshell-alias-completions filename))
                             (lambda (name)
                               (substring name 7))
                             (all-completions (concat "eshell/" filename)
                                              obarray #'functionp))
                            nil '(eshell-find-alias-function))
             (append (and (or eshell-show-lisp-completions
                              (and eshell-show-lisp-alternatives
                                   (null completions)))
                          (all-completions filename obarray #'functionp))


(use-package vterm
  :if (executable-find "cmake")
  ;; Disabling hl-line-mode in vterm buffers because typing causes the highlight
  ;; to flicker.
  :hook (vterm-mode-hook . mpereira/hl-line-mode-disable)
  (setq vterm-always-compile-module t)
  (setq vterm-max-scrollback 100000)
  (setq vterm-clear-scrollback-when-clearing t))


(if (not (mpereira/is-gnu-program "bash"))
      (warn "Not GNU bash: %s" (s-trim (shell-command-to-string "which bash")))))

(setq explicit-shell-file-name "bash")

;; Infinite buffer.
(setq term-buffer-maximum-size 0)

;; This defaults to `t' which causes the point to not be movable from the
;; process mark.
(setq term-char-mode-point-at-process-mark nil)

;; REVIEW(maybe-unnecessary).
 :keymaps '(term-raw-map)
 :states '(normal)
 "p" 'term-paste
 "M-x" 'execute-extended-command)

;; REVIEW(maybe-unnecessary).
 :keymaps '(term-raw-map)
 :states '(insert)
 "M-v" 'term-paste)

;; REVIEW(maybe-unnecessary).
 ;; Are both necessary? C-c C-c wasn't working just with `term-raw-map' so I
 ;; added `term-mode-map' and re-evaluated, started working in a term buffer.
 :keymaps '(term-raw-map term-mode-map)
 :prefix "C-c"
 "" nil
 "C-c" #'term-interrupt-subjob)

(add-hook 'term-mode-hook #'mpereira/hide-trailing-whitespace)


(use-package eterm-256color
  (add-hook 'term-mode-hook #'eterm-256color-mode))


(use-package bash-completion


(use-package fish-completion
  (fish-completion-fallback-on-bash-p t)
  (if (executable-find "fish")
    (message "fish executable not found, not enabling fish-completion-mode")))


(use-package load-bash-alias
  (setq load-bash-alias-bashrc-file "~/.aliases"))



(setq confirm-kill-emacs 'y-or-n-p)
(fset 'yes-or-no-p 'y-or-n-p)

(menu-bar-mode -1)
(scroll-bar-mode -1)
(tool-bar-mode -1)
(blink-cursor-mode -1)
(setq frame-resize-pixelwise t)

;; Don't show UI-based dialogs from mouse events.
(setq use-dialog-box nil)

;; Shh...
(setq inhibit-startup-echo-area-message t)
(setq inhibit-startup-screen t)
(setq initial-scratch-message nil)
(setq ring-bell-function 'ignore)

;; Make cursor the width of the character it is under e.g. full width of a TAB.
(setq x-stretch-cursor t)

;; Minimal titlebar for macOS.
(add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
(add-to-list 'default-frame-alist '(ns-appearance . dark))
(setq ns-use-proxy-icon nil)
(setq frame-title-format nil)

;; Start in full-screen.
(add-hook 'after-init-hook #'toggle-frame-fullscreen)

Make profiler report columns wider

(use-package profiler
  (setf (caar profiler-report-cpu-line-format) 100
        (caar profiler-report-memory-line-format) 100))


(use-package tree-sitter
  (add-to-list 'tree-sitter-major-mode-language-alist '(tsx-ts-mode . tsx))
  (add-hook 'prog-mode-hook #'turn-on-tree-sitter-mode)
  (add-hook 'tree-sitter-after-on-hook #'tree-sitter-hl-mode))

(use-package tree-sitter-langs
  (tree-sitter-langs-install-grammars t)
  (tree-sitter-require 'javascript)
  (tree-sitter-require 'tsx)
  (tree-sitter-require 'typescript))

(use-package treesit-auto
  (setq treesit-auto-install t)


Doesn’t do nested folds for JavaScript yet.

(use-package ts-fold
  :ensure nil
  :vc (:fetcher github
       :repo "emacs-tree-sitter/ts-fold")
  (add-to-list 'ts-fold-range-alist
                 (export_clause . ts-fold-range-seq)
                 (statement_block . ts-fold-range-seq)
                 (comment . ts-fold-range-c-like-comment)))
  (add-to-list 'ts-fold-summary-parsers-alist
               '(tsx-ts-mode . ts-fold-summary-javadoc)))


(use-package default-text-scale)


(pixel-scroll-precision-mode 1)

Font sizes

A M-x disable-theme followed by a M-x load-theme is required after changing the default font size with default-text-scale-increase and default-text-scale-decrease.

I tried using frame-text-cols to get the frame width in characters, but it seems that its return value only changes after there has been some user interaction with the frame (like switching buffers), even after the font size has been changed programmatically. Because of this, I fell back to using

(/ (frame-text-width)

to get the actual frame width in characters.

(setq mpereira/font-family "Hack")
(setq mpereira/font-size-external-monitor 170)
(setq mpereira/font-size-external-monitor-posframe-width-multiplier 0.1)
(setq mpereira/font-size-laptop 150)
(setq mpereira/font-size-laptop-posframe-width-multiplier 0.5)
(setq mpereira/font-size-posframe-minimum-width 100)

(setq mpereira/font-size-initial mpereira/font-size-external-monitor)

(defun mpereira/font-size-normalize (font-size)
  (- font-size (mod font-size 2)))

(defun mpereira/font-size-handle-change (new-font-size)
  (let* ((default-font-family (face-attribute 'default :family))
         (frame-column-width (/ (frame-text-width)
         (posframe-width-multiplier 0.5)
         (posframe-font-size-multiplier 1.1)
         (posframe-font-size (mpereira/font-size-normalize
                               (* posframe-font-size-multiplier
                                  (/ new-font-size 10))))))
    (with-eval-after-load "vertico-posframe"
      (message (format "setting vertico-posframe-font to '%s'"
                       (format "%s %s"
      (setq vertico-posframe-font (format "%s %s"
      (message (format "setting vertico-posframe-width to '%d'"
                       (truncate (* posframe-width-multiplier
      (setq vertico-posframe-width (truncate (* posframe-width-multiplier

(defun mpereira/font-size-change (change-fn)
  (let* ((previous-default-font-size (face-attribute 'default :height))
         (_ (message "frame-column-width before: %d" (/ (frame-text-width)
         (_ (funcall change-fn previous-default-font-size))
         (_ (message "frame-column-width after: %d" (/ (frame-text-width)
         (increased-default-font-size (face-attribute 'default :height)))
    (mpereira/font-size-handle-change increased-default-font-size)))

(add-hook 'after-setting-font-hook
          (lambda ()
            (message "frame-column-width after after-setting-font-hook: %d"
                     (/ (frame-text-width)

(defun mpereira/font-size-increase ()
  (mpereira/font-size-change (lambda (actual-font-size)

(defun mpereira/font-size-decrease ()
  (mpereira/font-size-change (lambda (actual-font-size)

(defun mpereira/font-size-set (desired-font-size)
   (lambda (actual-font-size)
     (let ((delta (- desired-font-size actual-font-size)))
       (default-text-scale-increment (mpereira/font-size-normalize delta))))))

(defun mpereira/font-size-set-preset (font-size
  (mpereira/font-size-set font-size)
  (when reload-theme?
    (when-let ((current-theme (car custom-enabled-themes)))
      (disable-theme (symbol-name (car custom-enabled-themes)))
      (load-theme current-theme))))

(defun mpereira/font-size-set-external-monitor ()
   (called-interactively-p 'any)))

(defun mpereira/font-size-set-laptop ()
   (called-interactively-p 'any)))

(defun mpereira/font-initialize ()
  (when (x-list-fonts "Hack")
    (set-face-attribute 'default nil :family mpereira/font-family))
  (set-face-attribute 'default nil :height mpereira/font-size-initial)
  (mpereira/font-size-handle-change mpereira/font-size-initial))

(add-hook 'after-init-hook #'mpereira/font-initialize 'append)
(add-hook 'after-init-hook #'mpereira/font-size-set-external-monitor 'append)


(use-package posframe)


(use-package so-long


(use-package too-long-lines-mode
  :ensure nil
  :vc (:fetcher github
       :repo "rakete/too-long-lines-mode")


(use-package minibuffer-line
  (setq minibuffer-line-format
           (let ((time-string (format-time-string "%a %b %d %R")))
              (propertize (make-string (- (frame-text-cols)
                                          (string-width time-string))
                          'face 'default)
  (minibuffer-line-mode t))


Mode not enabled by default.

(use-package highlight-indent-guides
  (setq highlight-indent-guides-method 'character))


(use-package origami
  (add-hook 'prog-mode-hook #'origami-mode))


(use-package rainbow-delimiters
  (add-hook 'lisp-mode-hook 'rainbow-delimiters-mode))


Disabling for now because it was making buffers slow.

(use-package diff-hl
  (global-diff-hl-mode t)
  (diff-hl-flydiff-mode t)

  ;; FIXME(slow).
  ;; (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh)

  (set-face-foreground 'diff-hl-insert "diff-nonexistent")
  (set-face-background 'diff-hl-insert "green4")
  (set-face-foreground 'diff-hl-change "diff-nonexistent")
  (set-face-background 'diff-hl-change "yellow3")
  (set-face-foreground 'diff-hl-delete "diff-nonexistent")
  (set-face-background 'diff-hl-delete "red4"))


(use-package all-the-icons)


(use-package dired-sidebar
  :commands (dired-sidebar-toggle-sidebar))


Run M-x all-the-icons-install-fonts after installing.

(use-package all-the-icons-dired
  :after (all-the-icons dired)
  :commands (all-the-icons-dired-mode)
  (add-hook 'dired-mode-hook #'all-the-icons-dired-mode))


Mode not enabled by default.

(use-package emojify)



(use-package emacs
  (defun mpereira/combobulate-setup-install-grammars ()
    "Install Tree-sitter grammars if they are absent."
    (dolist (grammar
             '((css "")
               (javascript . ("" "master" "src"))
               (python "")
               (tsx . ("" "master" "tsx/src"))
               (yaml "")))
      (add-to-list 'treesit-language-source-alist grammar)
      ;; Only install `grammar' if we don't already have it
      ;; installed. However, if you want to *update* a grammar then
      ;; this obviously prevents that from happening.
      (unless (treesit-language-available-p (car grammar))
        (treesit-install-language-grammar (car grammar)))))

  ;; Optional, but recommended. Tree-sitter enabled major modes are distinct
  ;; from their ordinary counterparts.
  ;; You can remap major modes with `major-mode-remap-alist'. Note that this
  ;; does *not* extend to hooks! Make sure you migrate them also.
  (dolist (mapping '((python-mode . python-ts-mode)
                     (css-mode . css-ts-mode)
                     (typescript-mode . tsx-ts-mode)
                     (js-mode . js-ts-mode)
                     (css-mode . css-ts-mode)
                     (yaml-mode . yaml-ts-mode)))
    (add-to-list 'major-mode-remap-alist mapping))

  (use-package combobulate
    :hook ((python-ts-mode-hook . combobulate-mode)
           (js-ts-mode-hook . combobulate-mode)
           (css-ts-mode-hook . combobulate-mode)
           (yaml-ts-mode-hook . combobulate-mode)
           (typescript-ts-mode-hook . combobulate-mode)
           (tsx-ts-mode-hook . combobulate-mode))
    :load-path ("~/git/combobulate")
    (setq combobulate-flash-node nil)

     :keymaps '(mpereira/combobulate-mode-map)
     :states '(normal visual)
     "+" #'combobulate-mark-node-dwim
     "{" #'combobulate-navigate-beginning-of-defun
     "}" #'combobulate-navigate-end-of-defun
     "(" #'combobulate-navigate-up-list-maybe
     ")" #'combobulate-navigate-down
     "B" #'combobulate-navigate-logical-previous
     "C-j" #'combobulate-navigate-next
     "C-k" #'combobulate-navigate-previous
     "E" #'combobulate-navigate-logical-next
     "W" #'combobulate-navigate-forward
     "C-S-j" #'combobulate-drag-down
     "C-S-k" #'combobulate-drag-up
     "C-k" #'combobulate-navigate-previous)

     :keymaps '(mpereira/combobulate-mode-map)
     :states '(normal visual)
     :prefix mpereira/leader
     "r" #'combobulate-splice-up
     "m" #'combobulate-mark-node-dwim
     "R" #'combobulate-vanish-node
     "k" #'combobulate-kill-node-dwim
     "(" #'combobulate-envelop-tsx-ts-mode-wrap-parentheses
     "<" #'combobulate-envelop-tsx-ts-mode-tag
     "{" #'combobulate-envelop-tsx-ts-mode-expression
     "c" #'combobulate-clone-node-dwim))

(defvar mpereira/combobulate-mode-map (make-sparse-keymap)
  "Keymap for `mpereira/combobulate-mode'.")

(define-minor-mode mpereira/combobulate-mode
  "A minor mode so that my key settings override annoying major modes."
  ;; If init-value is not set to t, this mode does not get enabled in
  ;; `fundamental-mode' buffers even after doing \"(global-mpereira-combobulate-mode 1)\".
  ;; More info:
  :init-value nil
  :lighter " mpereira/combobulate-mode"
  :keymap mpereira/combobulate-mode-map)

(define-globalized-minor-mode global-mpereira-combobulate-mode

;; The keymaps in `emulation-mode-map-alists' take precedence over
;; `minor-mode-map-alist'
(add-to-list 'emulation-mode-map-alists `((mpereira/combobulate-mode . ,mpereira/combobulate-mode-map)))

(defun mpereira/turn-off-mpereira-combobulate-mode ()
  "Turn off `mpereira/combobulate-mode'."
  (mpereira/combobulate-mode -1))

(add-hook 'minibuffer-setup-hook #'mpereira/turn-off-mpereira-combobulate-mode)

(dolist (hook '(python-ts-mode-hook
  (add-hook hook #'mpereira/combobulate-mode))

(provide 'mpereira/combobulate-mode)


(use-package bm)


Command wishlist:

  • counsel-command-history
  • counsel-descbinds
  • counsel-org-capture
  • counsel-tramp
  • =counsel-web-*=
(use-package consult)
(use-package consult-lsp)
(use-package consult-projectile)
(use-package consult-git-log-grep
  (consult-git-log-grep-open-function #'magit-show-commit))


(use-package avy
  (setq avy-all-windows nil))


 :keymaps '(goto-address-highlight-keymap)
 "C-c C-o" #'goto-address-at-point)

(add-hook 'prog-mode-hook #'goto-address-prog-mode)


(use-package dumb-jump
  (setq dumb-jump-selector 'completing-read))


(use-package frog-jump-buffer
  :ensure nil
  :vc (:fetcher github
       :repo "waymondo/frog-jump-buffer"))


(use-package link-hint)

Text search and manipulation


(use-package rg
  :general (:keymaps '(rg-mode-map)
            :states '(normal visual)
            "<" 'rg-back-history
            ">" 'rg-forward-history
            "C-j" 'rg-next-file
            "C-k" 'rg-prev-file
            "G" 'evil-goto-line
            "gg" 'evil-goto-first-line
            "gr" 'rg-recompile)
  (setq rg-executable "rg")
  (setq rg-group-result t))


(use-package wgrep
  (setq wgrep-auto-save-buffer t))


(use-package double-saber
  :after (rg)
  :general (:keymaps '(double-saber-mode-map)
            :states '(normal visual)
            "C-r" 'double-saber-redo
            "u" 'double-saber-undo
            "D" 'double-saber-delete
            "F" 'double-saber-narrow
            "T" '(lambda ()
                   (setq rg-group-result (not rg-group-result))
            "S" 'double-saber-sort-lines)
  :hook ((rg-mode-hook . (lambda ()
                           (setq-local double-saber-start-line 6)
                           (setq-local double-saber-end-text "rg finished")))
         (grep-mode-hook . (lambda ()
                             (setq-local double-saber-start-line 5)
                             (setq-local double-saber-end-text "Grep finished")))))


(use-package symbol-overlay)


(use-package expand-region
   :states '(normal visual)
   "+" 'er/expand-region))


(use-package ialign)


I can’t get this to plan nice with `company-mode`, so I’m disabling it.

(use-package yasnippet
  (add-hook 'prog-mode-hook #'yas-minor-mode))


(use-package yasnippet-snippets
  :after yasnippet)