Skip to content

Instantly share code, notes, and snippets.

@ManasJayanth
Created October 22, 2024 09:52
Show Gist options
  • Save ManasJayanth/a9c19219cfc846ea657fceab9af202f1 to your computer and use it in GitHub Desktop.
Save ManasJayanth/a9c19219cfc846ea657fceab9af202f1 to your computer and use it in GitHub Desktop.
Emacs Starter

Emacs Configuration

On Windows, when creating shortcuts, set $HOME in the properties (as Target)

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -Command "$env:HOME = 'C:/Users/manas'; Start-Process 'C:\Program Files\Emacs\x86_64\bin\runemacs.exe'"

<2023-07-13 Thu 07:22> Update:

Alternatively set the HKCU:\SOFTWARE\GNU\Emacs\HOME registry key

Get-ItemProperty -Path HKCU:\SOFTWARE\GNU\Emacs\ -Name HOME -Value "C:/Users/manas"

Window setup

     (defun my-dpi ()
	(let* ((attrs (car (display-monitor-attributes-list)))
	       (size (assoc 'mm-size attrs))
	       (sizex (cadr size))
	       (res (cdr (assoc 'geometry attrs)))
	       (resx (- (caddr res) (car res)))
	       dpi)
	  (catch 'exit
	    ;; in terminal
	    (unless sizex
	      (throw 'exit 10))
	    ;; on big screen
	    (when (> sizex 1000)
	      (throw 'exit 10))
	    ;; DPI
	    (* (/ (float resx) sizex) 25.4))))
     
     (defun my-preferred-font-size ()
	(let ( (dpi (my-dpi)) )
	(cond
	  ((< dpi 110) 18) ;; MacOS: 127.88 Dell: 330
	  ((< dpi 130) 16)
	  ((< dpi 160) 24)
	  (t 32))))
     
     (defun my-zoom-frm-by-dpi (&optional frame)
	"Zoom FRAME so the DPI is closer to `my-zoom-frm-wanted-dpi'."
	(set-face-attribute 'default nil :font (format "-*-Hack-normal-normal-normal-*-%d-*-*-*-m-0-iso10646-1" (my-preferred-font-size)))
	(setq ns-auto-hide-menu-bar t)
	(menu-bar-mode 0)
	(print (display-graphic-p))
	;; Why (display-graphic-p frame) and not just (display-graphic-p)
	;; SO answer: the trick is that window-system and display-graphic-p are now frame specific. you need to be in that frame inside your hook function (seems like it should already be the case, but i don't think it is). i had to add this at the beginning of my after-make-frame-functions hook (to make window-system and display-graphic-p behave correctly):
	;; https://stackoverflow.com/questions/7616761/even-when-emacsclient-is-started-in-a-terminal-window-system-is-non-nil
       ;; An even better way is that you can pass display-graphic-p the frame directly: (display-graphic-p frame)
	(if (display-graphic-p frame)
	    (progn
	      (tool-bar-mode -1)
	      (scroll-bar-mode -1))))
     
     (add-hook 'after-make-frame-functions #'my-zoom-frm-by-dpi)
     (my-zoom-frm-by-dpi)
     
     (setq byte-compile-cond-use-jump-table nil)
     (add-to-list 'default-frame-alist '(fullscreen . maximized))
     (add-to-list 'default-frame-alist '(ns-appearance . dark))

Supressing splash screen

(setq inhibit-startup-message t)

Init file management

(global-set-key (kbd "C-x C-i") 
                (lambda() 
                    (interactive) 
                    (load-file "~/.emacs.d/init.el")))
(global-set-key (kbd "C-x i") 
                (lambda() 
                (interactive) 
                    (find-file "~/.emacs.d/settings.org")))

Package management

package-selected-packages was a nice way to get a new Emacs installation to fetch and install them all. Moving to use-package for a while

   (defvar bootstrap-version)
   (let ((bootstrap-file
	   (expand-file-name
	    "straight/repos/straight.el/bootstrap.el"
	    (or (bound-and-true-p straight-base-dir)
		user-emacs-directory)))
	  (bootstrap-version 7))
     (unless (file-exists-p bootstrap-file)
	(with-current-buffer
	    (url-retrieve-synchronously
	     "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
	     'silent 'inhibit-cookies)
	  (goto-char (point-max))
	  (eval-print-last-sexp)))
     (load bootstrap-file nil 'nomessage))

 (straight-use-package 'use-package)
 (setq straight-use-package-by-default t)

Evil mode

	 (use-package evil
	     :ensure t
	     :init
	     (setq evil-want-integration t) ;; This is optional since it's already set to t by default.
	     (setq evil-want-keybinding nil)
	     :config
	     (evil-mode 1))

	 (use-package evil-surround
	     :ensure t
	     :config
	     (global-evil-surround-mode 1))

	 (use-package evil-collection
	     :after evil
	     :ensure t
	     :config
	     (evil-collection-init))

	 (use-package key-chord
	     :after (evil)
	     :config (progn 
		     (setq key-chord-two-keys-delay 0.5)
		     (key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
		     (key-chord-mode 1)))

    (use-package evil-god-state
	 :config (progn
		   (evil-define-key 'normal global-map "," 'evil-execute-in-god-state))
        :straight
	 (evil-god-state
	  :host github
	  :repo "ManasJayanth/evil-god-state"
	  :type git))

    (use-package evil-adjust
      :straight
      (evil-adjust
	:host github
	:repo "troyp/evil-adjust"
	:type git))

Theme

(use-package ef-themes
    :config (ef-themes-select 'ef-elea-dark))

My stdlib

ssh-add from Emacs

   ;; Inspired and stolen from https://github.com/tarsius/keychain-environment/blob/master/keychain-environment.el
   ;; Example usage
   ;; (progn
   ;;   (ssh-refresh-environment)
   ;;   (ssh-add "~/.ssh/id_rsa"))

   (defun ssh-refresh-environment ()
     "Set ssh-agent environment variables.
   Set the environment variables `SSH_AUTH_SOCK', `SSH_AGENT_PID'
   in Emacs' `process-environment' according to
   information retrieved from files created by the keychain script."
     (interactive)
     (let* ((ssh (shell-command-to-string "ssh-agent")))
	 (list (and ssh
		    (string-match "SSH_AUTH_SOCK[=\s]\\([^\s;\n]*\\)" ssh)
		    (setenv       "SSH_AUTH_SOCK" (match-string 1 ssh)))
	       (and ssh
		    (string-match "SSH_AGENT_PID[=\s]\\([0-9]*\\)?" ssh)
		    (setenv       "SSH_AGENT_PID" (match-string 1 ssh)))
	       )))


   (defun ssh-add-process-filter (process string)
     (save-match-data
	 (if (string-match ":\\s *\\'" string)
	     (process-send-string process (concat (read-passwd string) "\n"))
	   (message "%s" string))))
   (defun ssh-add (key-file)
     "Run ssh-add to add a key to the running SSH agent.
   Let Emacs prompt for the passphrase."
     (interactive "fAdd key: \n")
     (let ((process-connection-type t)
		 process)
	 (unwind-protect
	     (progn
	       (setq process (start-process "ssh-add" nil
					    "ssh-add" (expand-file-name key-file)))
	       (set-process-filter process 'ssh-add-process-filter)
	       (while (accept-process-output process)))
	   (if (eq (process-status process) 'run)
	       (kill-process process)))))

$PATH and others

(setq home-path (getenv "HOME"))
(setq
 path-separator
 (if (equal system-type 'windows-nt) ";" ":"))

(defun join (sep lst)
  (mapconcat 'identity lst sep))

  (add-to-list 'exec-path "/Users/manas/.deno/bin")
  (add-to-list 'exec-path "/opt/homebrew/bin")
  (add-to-list 'exec-path "/Library/TeX/texbin")
  (add-to-list 'exec-path "/Users/manas/.deno/bin")
  (add-to-list 'exec-path "/usr/bin/core_perl")
  (add-to-list 'exec-path (format "%s/AppData/Local/Microsoft/WinGet/Links" home-path))
  (add-to-list 'exec-path "/usr/local/bin")
  (add-to-list 'exec-path (format "%s/AppData/Roaming/npm" home-path))
  (add-to-list 'exec-path "c:/Program Files/Git/usr/bin")
  (add-to-list 'exec-path "c:/Program Files/Go/bin")
  (add-to-list 'exec-path "C:/Program Files/Terraform")
  (add-to-list 'exec-path "C:/Users/prome/lib/oracle-cli/Scripts")
  (add-to-list 'exec-path "c:/Program Files/Git/bin")
  (add-to-list 'exec-path (concat home-path "/.yarn/bin"))
  (add-to-list 'exec-path "/Users/manas/Library/pnpm")
  (setenv "PATH" (join path-separator exec-path))
  (setenv "LD_LIBRARY_PATH" "/opt/homebrew/lib/gcc/11/")
  (setenv "DYLD_LIBRARY_PATH" "/opt/homebrew/lib")

Git

(use-package spinner)
(use-package compat)
(use-package lv
  :after (spinner)) ; included only because lv needs spinner but doesn't mention it. I dont' need it
(use-package magit
    :bind (("C-x m" . magit-status))
    :after (spinner compat)
    :straight (compat
		 :host github
		 :repo "phikal/compat.el"))
(use-package forge
  :after magit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment