Table of Contents

1 Emacs Lisp hacks


1.1 Useful Stuff


1.2 Tricks

Tricks and Tips to make the GNU Emacs experience more smooth, better and fun.

  • (search-emacs-glossary - Search the whole Emacs Glossary)
  • (C-h i - Info mode, with info about lots of features)
    Opens the collection of info pages for features and packages
  • (C-h f - Lookup function or macro from name)
  • (C-h v - Lookup variable from name)

    This helps a lot when finding elisp functions, or exploring the elisp
    ecosystem. This provides a documentation page of the
    function/variable together with a link to the actual code of
    definition as well as an argument description.

  • Evaluate in another Emacs process
    The function subemacs-eval-expression (from subemacs.el)
    Evaluate an ELisp expression in separate Emacs process.

Remember: When referring to functions in a quoted form #' should
be used instead of regular quoting. This because it basically
communicates this is a defined function.

  1. Other nice libraries include
(require 'cl-lib)    ;; Common Lisp Library
(require 'cl-libify) ;; Remove the cl- prefix from cl-lib functions
(require 's)         ;; String manipulation
(require 'f)         ;; File manipulation
(require 'dash)      ;; Modern Lisp Api (lists mostly)
(require 'dash-functional) ;; Modern Lisp Api (Functional functions)

(require 'async)     ;; Concurrency library
(require 'aio)       ;; Concurrency library
(require 'deferred)  ;; Concurrency library
(require 'posframe)  ;; Display tooltip like frames hovering over the current frame
(require 'ivy)       ;; A standard of interface for the minibuffer

1.3 Regions

This function inserts the argument start (string), before the region.
And inserts end (string), at the end of the region.

This works well for making a selected text bold in org mode with
(region-encapsulate "*" "*").

(defun region-encapsulate (start end)
  "Insert `START' at the beginning of the active region, and `END' at the end."
  (interactive)

  (let ((pos-beg  (region-beginning))
        (pos-end  (region-end)))

    (progn 
           (goto-char pos-beg)
           (insert-before-markers start)
           (goto-char pos-end)
           (insert end)
           )))

This function line-encapsulate does the same thing but with lines,
which is good for things like surrounding code with the org source
blocks.

(defun lines-encapsulate (start end)
  "Insert `START' at the beginning of the active region, and `END' at the end."
  (interactive)

  (let ((pos-beg  (region-beginning))
        (pos-end  (region-end)))

    (progn 
           (goto-char pos-beg)
           (beginning-of-line)
           (insert-before-markers start)
           (goto-char pos-end)
           (beginning-of-line)
           (insert end)
           )))


1.4 Removing Distractions

This function checks for a set of defined minor-modes in the
current-buffer, and if they are enabled they are added to
333/disabled-modes and are disabled. The same goes for a set of
enabled modes, which are added to 333/enabled-modes. The fringe size
and line-spacing is also saved.

Here is the variables in which they are saved.

(defvar 333/enabled-modes '())
(defvar 333/disabled-modes '())
(defvar 333/save-fringe '(0 . 0))
(defvar 333/save-spacing 0)

Here is the function which removes distraction by disabling modes,
which can be distracting. And enables some visual tricks to improve
concentration.

(defun 333/comfy (&optional fringe-size line-spacing-size)
  "Removes distractions like line numbering etc.
All changes settings can be restored with '333/uncomfy'"
  (interactive)
  ;; Save current fringe
  (setq 333/save-fringe `(,left-fringe-width . ,right-fringe-width))
  ;; Set a new fringe
  (set-fringe-mode `(,fringe-size . ,fringe-size))

  (when eldoc-mode
    (add-to-list '333/disabled-modes 'eldoc-mode)
    (eldoc-mode -1))

  (when auto-complete-mode
    (add-to-list '333/disabled-modes 'auto-complete-mode)
    (auto-complete-mode -1))

  (when auto-complete-mode
    (add-to-list '333/disabled-modes 'auto-complete-mode)
    (auto-complete-mode -1))

  (when company-mode
    (add-to-list '333/disabled-modes 'company-mode)
    (company-mode -1))

  (when hl-paren-mode
    (add-to-list '333/disabled-modes 'hl-paren-mode)
    (hl-paren-mode -1))

  (when display-line-numbers-mode    
    (add-to-list '333/disabled-modes 'display-line-numbers-mode)
    (display-line-numbers-mode -1))

  (when (not visual-line-mode)
    (add-to-list '333/enabled-modes 'visual-line-mode)
    (visual-line-mode t))

  (setq 333/line-spacing line-spacing)
  (setq save-spacing line-spacing-size))

(provide '333/comfy)

This function restores the old saved values.

(defun 333/uncomfy ()
  "Removes distractions like line numbering etc.
All changes settings can be restored with '333/uncomfy'"
  (interactive)
  ;; Save current fringe

  ;; Set a new fringe
  (set-fringe-mode 333/save-fringe)

  (when (and (not eldoc-mode)
             (seq-contains 333/disabled-modes
                           'eldoc-mode))
    (eldoc-mode +1))

  (when (and (seq-contains 333/disabled-modes
                           'auto-complete-mode)
             (not auto-complete-mode))
    (auto-complete-mode +1))

  (when (and (seq-contains 333/disabled-modes
                           'company-mode)
             (not company-mode))

    (company-mode +1))

  (when (and (seq-contains 333/disabled-modes
                           'display-line-numbers-mode)
             (not hl-paren-mode))

    (hl-paren-mode +1))

  (when (and (seq-contains 333/disabled-modes
                           'display-line-numbers-mode)
             (not display-line-numbers-mode))
    (display-line-numbers-mode +1))

  (when (and visual-line-mode
             (seq-contains 333/enabled-modes
                           'visual-line-mode))
    (visual-line-mode -1))

  (setq line-spacing 333/line-spacing )
  (setq 333/enabled-modes '())
  (setq 333/disabled-modes '()))