[ samkhn Home | Comments or bugs? Contact me ]


Useful Emacs Snippets

Published: March 22, 2023

Speed up emacs startup by tuning the garbage collector

;; At the start
(setq gc-cons-threshold 402653184
      gc-cons-percentage 0.6
      file-name-handler-alist nil)

;; Before exiting
(setq gc-cons-threshold 16777216
      gc-cons-percentage 0.1
      file-name-handler-alist last-file-name-handler-alist)

Go to column

(defun sk/go-to-column (column)
  "By default M-g M-g goes to line. Here is goto column.
  M-g -> go to line, M-g c is go to character"
  (interactive "nColumn: ")
  (move-to-column column t))
(global-set-key (kbd "M-g M-c") #'sk/go-to-column)

Right click to search a word

(defun sk/click-to-search (*click)
  (interactive "e")
  (let ((p1 (posn-point (event-start *click))))
    (goto-char p1)
    (isearch-forward-symbol-at-point)))
(global-set-key (kbd "<mouse-3>") 'sk/click-to-search)

Scroll down on highlighted region to execute asynchronously (inspired by the Acme editor from Plan9)

(defun sk/run ()
  (interactive)
  (let ((region (buffer-substring-no-properties (region-beginning) (region-end))))
    (async-shell-command region)))
(global-set-key (kbd "C-c r") 'sk/run)
(global-set-key (vector (list 'control mouse-wheel-up-event)) 'sk/run)

Better compilation buffer

(defun sk/compilation-hook ()
  (setq compilation-scroll-output nil)
  (make-local-variable 'truncate-lines)
  (setq truncate-lines nil)
  (setq compilation-error-screen-columns nil))
(add-hook 'compilation-mode-hook 'sk/compilation-hook)

You don't need a clang-format package

;; Clang format is from https://github.com/filcab/elisp/blob/master/clang-format.el
(defvar clang-format-binary "clang-format")
(defun clang-format (styleformat begin end)
  (interactive)
  (let* ((orig-windows (get-buffer-window-list (current-buffer)))
         (orig-window-starts (mapcar #'window-start orig-windows))
         (orig-point (point)))
    (unwind-protect
        (call-process-region (point-min) (point-max) clang-format-binary
                             t (list t nil) nil
                             "-offset" (number-to-string (1- begin))
                             "-length" (number-to-string (- end begin))
                             "-cursor" (number-to-string (1- (point)))
                             "-assume-filename" (buffer-file-name)
                             "-style" styleformat)
      (goto-char (point-min))
      (let ((json-output (json-read-from-string
                          (buffer-substring-no-properties
                           (point-min) (line-beginning-position 2)))))
        (delete-region (point-min) (line-beginning-position 2))
        (goto-char (1+ (cdr (assoc 'Cursor json-output))))
        (dotimes (index (length orig-windows))
          (set-window-start (nth index orig-windows)
                            (nth index orig-window-starts)))))))

(defun sk/clang-format-buffer ()
  (interactive)
  (clang-format "Google" (point-min) (point-max)))
(defun llvm/clang-format-buffer ()
  (interactive)
  (clang-format "LLVM" (point-min) (point-max)))

You don't need a CMake package

(defun sk/set-compile-from-cmake ()
  (interactive)
  (setq splitdir (split-string default-directory "/"))
  (setq p (expand-file-name "" "/"))
  (setq m nil)
  (catch 'found
	(dolist (d splitdir)
	  (setq p (expand-file-name d p))
	  (setq m (expand-file-name "CMakeLists.txt" p))
	  (if (file-exists-p m)
		  (throw 'found m))))
  (unless m (error "CMakeLists not found in path, ignoring..."))
  (setq sk/build-dir (concat (file-name-directory m) "build"))
  (unless (file-exists-p sk/build-dir) (error "CMakeLists exists but build directory not found"))
  (set (make-local-variable 'compile-command) (concat "cmake --build " (shell-quote-argument sk/build-dir))))
(add-hook 'c-mode-common-hook 'sk/set-compile-from-cmake)

You don't need a solarized package

;; Theme. What setting controls what?
;; comment := content inside /**/ or after // or ;;
;; comment delimiter := // or /**/ in C or ;; in lisp
;; constant := in std::string, the std
;; function name := void Do(), the Do
;; keyword := static, return keywords
;; string := content inside ""
;; type := std::string s; the string (std and s are covered elsewhere).
;; variable name := void Traverse(BST *tree); the tree
;; preprocessor := #include

;; Theme: Solarized
(set-face-attribute 'font-lock-builtin-face nil :foreground "#ffffff")
(set-face-attribute 'font-lock-comment-face nil :foreground "#44b340")
(set-face-attribute 'font-lock-comment-delimiter-face nil :foreground "#8cde94")
(set-face-attribute 'font-lock-constant-face nil :foreground "#7ad0c6")
(set-face-attribute 'font-lock-doc-face nil :foreground "44b340")
(set-face-attribute 'font-lock-function-name-face nil :foreground "#ffffff")
(set-face-attribute 'font-lock-keyword-face nil :foreground "#ffffff")
(set-face-attribute 'font-lock-string-face nil :foreground "#2ec09c")
(set-face-attribute 'font-lock-type-face nil :foreground "#8cde94")
(set-face-attribute 'font-lock-variable-name-face nil :foreground "#c1d1e3")
(set-face-attribute 'font-lock-preprocessor-face nil :foreground "#8cde94")
(set-face-attribute 'font-lock-warning-face nil :foreground "#ffaa00")
(set-face-attribute 'region nil :background "#0000ff" :foreground "nil")
(set-face-attribute 'fringe nil :background "#062329" :foreground "white")
(set-face-attribute 'highlight nil :background "#0000ff" :foreground "nil")
(set-face-attribute 'mode-line nil :background "#d1b897" :foreground "#062329")
(add-to-list 'default-frame-alist '(cursor-color . "white"))
(add-to-list 'default-frame-alist '(foreground-color . "#d1b897"))
(add-to-list 'default-frame-alist '(background-color . "#292929")) ;; graybg #292929 bluebg #062329