Using .org file
#+STARTUP: overview
* My Init Org [[https://github.com/zbs-test/][myrepo]]
*** UI #+setupfile:
#+BEGIN_SRC emacs-lisp
(load-theme 'tango-dark)
#+END_SRC
*** Package conf #+setupfile:
#+BEGIN_SRC emacs-lisp
(add-to-list 'load-path (expand-file-name (concat user-emacs-directory "lisp/")))
(require 'init-fn)
(require 'init-system)
(require 'init-elpa)
(require 'init-package)
(require 'init-builtin)
(require 'init-kbd)
(require 'init-ide)
(require 'init-ui)
#+END_SRC
*** Custom file #+setupfile:
#+BEGIN_SRC emacs-lisp
(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
(when (file-exists-p custom-file)
(load custom-file))
#+END_SRC
load org file in init.el
( org-babel-load-file ( expand-file-name "~/.emacs.d/MyInit.org" ) )
more modular
mkdir org-conf mv MyInit.org to dir org-conf and load that file
Sample code
[repo] https://gitee.com/zbs_139/emacs.d.git
I really want to set up a c++ IDE conf
Now I found the best way to configure emacs.d
try
ls -R org-conf/ init.el
init.el
org-conf/:
C++-IDE-Init.el
C++-IDE-Init.org
MyInit.el
MyInit.org
MyInit.org
#+STARTUP:
* UI conf
#+BEGIN_SRC emacs-lisp
(load-theme 'tango-dark)
#+END_SRC
* utils.el
#+BEGIN_SRC emacs-lisp
(setq
make-backup-files nil
backup-inhibited t
)
(defun open-my-init-file ()
(interactive)
(find-file "~/.emacs.d/init.el")
)
(global-set-key (kbd "<f2>") 'open-my-init-file)
;; auto save code conf start ----------------------------------------------------------------------------------------------------------------
(defgroup auto-save nil
"Auto save file when emacs idle."
:group 'auto-save)
(defcustom auto-save-idle 5;;!!!!!!!!!!!!
"The idle seconds to auto save file. 5 seconds better"
:type 'integer
:group 'auto-save)
(defcustom auto-save-slient nil
"Nothing to dirty minibuffer if this option is non-nil."
:type 'boolean
:group 'auto-save)
(setq auto-save-default nil)
;; 前方高能核心代码, 请集中注意力
(defun auto-save-buffers ()
(interactive)
(let ((autosave-buffer-list))
(save-excursion
(dolist (buf (buffer-list))
(set-buffer buf)
(if (and (buffer-file-name) (buffer-modified-p))
(progn
(push (buffer-name) autosave-buffer-list)
(if auto-save-slient
(with-temp-message ""
(basic-save-buffer))
(basic-save-buffer))
)))
(unless auto-save-slient
(cond
((= (length autosave-buffer-list) 1)
(message "# Saved %s" (car autosave-buffer-list)))
((> (length autosave-buffer-list) 1)
(message "# Saved %d files: %s"
(length autosave-buffer-list)
(mapconcat 'identity autosave-buffer-list ", "))))))))
(defun auto-save-enable ()
(interactive)
(run-with-idle-timer auto-save-idle t #'auto-save-buffers))
(auto-save-enable);; 开启自动保存功能
(setq auto-save-slient t);; 自动保存的时候静悄悄的, 不要打扰我
(provide 'utils)
(require 'utils)
#+END_SRC
* init-elpa
#+BEGIN_SRC emacs-lisp
(setq package-archives '(("gnu" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
("nongnu" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/nongnu/")
("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/"))
package-check-signature nil
load-prefer-newer t)
(require 'package)
;;; initialize the packages, avoiding a re-initialization
(unless (bound-and-true-p package--initialized) ;; To avoid warnings on 27
(package-initialize))
(unless package-archive-contents
(package-refresh-contents))
;; settings for use-package package
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
;; configure use-package prior to loading it
(eval-and-compile
(setq use-package-always-ensure t
use-package-always-defer t
use-package-expand-minimally t)
(require 'use-package))
(use-package gnu-elpa-keyring-update)
(use-package diminish)
(use-package delight)
(provide 'init-elpa)
(require 'init-elpa)
#+END_SRC
* init-package
#+BEGIN_SRC emacs-lisp
(use-package all-the-icons)
;; Settings for company
(use-package company
:diminish
:defines (company-dabbrev-ignore-case company-dabbrev-downcase)
:init (add-hook 'after-init-hook 'global-company-mode))
;; Settings for exec-path-from-shell
(use-package exec-path-from-shell
:defer nil
:if (memq window-system '(mac ns x))
:init (exec-path-from-shell-initialize))
;; Settings for projectile (use builtin project in Emacs 28)
(use-package projectile
:when (< emacs-major-version 28)
:diminish " Proj."
:init (add-hook 'after-init-hook 'projectile-mode)
:config (define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map))
;; Show the delimiters as rainbow color
(use-package rainbow-delimiters
:init (add-hook 'prog-mode-hook 'rainbow-delimiters-mode))
(use-package highlight-parentheses
:init (add-hook 'prog-mode-hook 'highlight-parentheses-mode))
;; Settings for which-key - suggest next key
(use-package which-key
:defer nil
:diminish
:init (which-key-mode))
;; Settings for yasnippet
(use-package yasnippet
:diminish
:init (add-hook 'after-init-hook 'yas-global-mode))
(use-package yasnippet-snippets)
(provide 'init-package)
(require 'init-package)
#+END_SRC
* Custom file
#+BEGIN_SRC emacs-lisp
(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
(when (file-exists-p custom-file)
(load custom-file))
#+END_SRC
* Flycheck
#+BEGIN_SRC emacs-lisp
(use-package flycheck
:ensure t
:init
(global-flycheck-mode t)
)
#+END_SRC
* Yasnippet-snippets
#+BEGIN_SRC bash
sudo apt install elpa-yasnippet-snippets
#+END_SRC
- check in elpa/yasnippet-yasnippet... directory
* init-buildin
#+BEGIN_SRC emacs-lisp
(setq-default abbrev-mode t)
;; Delete Behavior
(add-hook 'before-save-hook #'delete-trailing-whitespace)
(add-hook 'after-init-hook 'delete-selection-mode)
;; Electric-Pair
(add-hook 'after-init-hook 'electric-indent-mode)
(add-hook 'prog-mode-hook 'electric-pair-mode)
(add-hook 'prog-mode-hook 'electric-layout-mode)
;; Flymake
;(add-hook 'prog-mode-hook 'flymake-mode)
;; HideShow Minor Mode
(add-hook 'prog-mode-hook 'hs-minor-mode)
;; Ido ( instead of ivy & counsel & swiper)
(setq-default ido-auto-merge-work-directories-length -1
ido-enable-flex-matching t
isearch-lazy-count t
lazy-count-prefix-format "%s/%s: ")
(setq completion-ignored-extensions '(".o" ".elc" "~" ".bin" ".bak" ".obj" ".map" ".a" ".ln" ".class"))
(fido-mode t)
;; Line Number
(add-hook 'prog-mode-hook 'display-line-numbers-mode)
;; Org Mode
(setq org-hide-leading-stars t
org-startup-indented t)
;; Parentheses
(setq-default show-paren-style 'mixed
show-paren-when-point-inside-paren t
show-paren-when-point-in-periphery t)
(show-paren-mode t)
;; Recent Files
(add-hook 'after-init-hook (lambda ()
(recentf-mode 1)
(add-to-list 'recentf-exclude '("~\/.emacs.d\/elpa\/"))))
(setq-default recentf-max-menu-items 20
recentf-max-saved-items 20)
;; Save Place
(save-place-mode 1)
;; Diminish Builtins
(dolist (elem '(abbrev-mode eldoc-mode))
(diminish elem))
(add-hook 'hs-minor-mode-hook (lambda () (diminish 'hs-minor-mode)))
(provide 'init-builtin)
(require 'init-builtin)
#+END_SRC
* init-kbd
#+BEGIN_SRC emacs-lisp
(use-package crux)
(use-package hungry-delete)
(use-package drag-stuff)
(use-package format-all
:diminish " Fmt."
:init (add-hook 'prog-mode-hook 'format-all-mode))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Global Key Bindings
;;
;; 当前版本全局按键绑定秉承以下原则:
;; 1. 自定义全局按键尽可能以C-c开头(或绑F5-F9),此为Emacs设计规范预期
;; 2. 记忆方式上,尽可能VSCode相近,因同在用VSCode
;; 3. 不违背Emacs Quirks [http://ergoemacs.org/emacs/keyboard_shortcuts.html]
;; 4. 为方便统一管理,全局按键不分散于use-package中,模式按键仍在use-package中
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Emacs Basic Keys ------------------------------
(defalias 'yes-or-no-p 'y-or-n-p)
(global-set-key (kbd "C-c ,") #'crux-find-user-init-file) ; Open Settings
(global-set-key (kbd "C-c r") 'recentf-open-files) ; Open Recent Files
;; Window Move
(global-set-key (kbd "C-c <left>") 'windmove-left)
(global-set-key (kbd "C-c <right>") 'windmove-right)
(global-set-key (kbd "C-c <up>") 'windmove-up)
(global-set-key (kbd "C-c <down>") 'windmove-down)
;;; Code Editing ------------------------------
;; Comments(As C-x C-; is for comment-line, keep the postfix)
(global-set-key (kbd "C-c C-;") #'comment-or-uncomment-region)
;; Line Edit
(global-set-key (kbd "M-<down>") #'drag-stuff-down)
(global-set-key (kbd "M-<up>") #'drag-stuff-up)
(global-set-key (kbd "C-c C-d") #'crux-duplicate-current-line-or-region)
(global-set-key (kbd "C-a") #'crux-move-beginning-of-line)
;; Delete
(global-set-key (kbd "C-c <backspace>") #'hungry-delete-backward)
(global-set-key (kbd "C-c <delete>") #'hungry-delete-forward)
;; Code Beautify
(global-set-key (kbd "C-o") #'yas-expand)
(global-set-key (kbd "C-c f") #'format-all-buffer)
;; Syntax
(global-set-key (kbd "M-n") #'flymake-goto-next-error)
(global-set-key (kbd "M-p") #'flymake-goto-prev-error)
(provide 'init-kbd)
(require 'init-kbd)
#+END_SRC
* init-fn
#+BEGIN_SRC emacs-lisp
(defmacro cabins/timer (&rest body)
"Measure the time of code BODY running."
`(let ((time (current-time)))
,@body
(float-time (time-since time))))
(defun cabins/tmp-reset-elpa ()
"Reset Elpa temporary. Useful when emacs-china sync fails."
(interactive)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("melpa" . "http://melpa.org/packages/"))))
(provide 'init-fn)
(require 'init-fn)
#+END_SRC
* init-system
#+BEGIN_SRC emacs-lisp
(set-language-environment "UTF-8")
(setq make-backup-files nil ; disable backup file
auto-save-default nil
inhibit-startup-screen t ; disable the startup screen splash
inhibit-default-init t
visible-bell nil
inhibit-compacting-font-caches t
read-process-output-max (* 64 1024))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ====================OS Specific==================== ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; macOS
;; move file to trash when delete
(when (eq system-type 'darwin)
(setq delete-by-moving-to-trash t))
;; <macOS> Command -> Meta, Option -> Super
(when (eq system-type 'darwin)
(setq mac-command-modifier 'meta
mac-option-modifier 'super))
(provide 'init-system)
(require 'init-system)
#+END_SRC
* init-ide
#+BEGIN_SRC emacs-lisp
(use-package lsp-mode
:commands (lsp lsp-deferred lsp-format-buffer lsp-organize-imports)
:init
(add-hook 'lsp-mode-hook (lambda ()
(lsp-enable-which-key-integration)
(add-hook 'before-save-hook #'lsp-organize-imports t t)
(add-hook 'before-save-hook #'lsp-format-buffer t t)))
(add-hook 'prog-mode-hook (lambda()
(unless (derived-mode-p 'emacs-lisp-mode 'lisp-mode)(lsp-deferred))))
:config
(setq lsp-auto-guess-root t
lsp-headerline-breadcrumb-enable nil)
(setq lsp-keymap-prefix "C-c l")
(define-key lsp-mode-map (kbd "C-c l") lsp-command-map))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (use-package lsp-ui ;;
;; :after lsp-mode ;;
;; :commands lsp-ui-mode ;;
;; :init ;;
;; (setq lsp-ui-doc-include-signature t ;;
;; lsp-ui-doc-position 'at-point ;;
;; lsp-ui-sideline-ignore-duplicate t) ;;
;; (add-hook 'lsp-mode-hook 'lsp-ui-mode) ;;
;; (add-hook 'lsp-ui-mode-hook 'lsp-modeline-code-actions-mode) ;;
;; :config ;;
;; (define-key lsp-ui-mode-map [remap xref-find-definitions] #'lsp-ui-peek-find-definitions) ;;
;; (define-key lsp-ui-mode-map [remap xref-find-references] #'lsp-ui-peek-find-references)) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; If you like debugging in Emacs, enable the next lines.
;; I disabled it, as it imports too many dependencies.Such as:
;; posframe,lsp-treemacs(dash, treemacs[hydra(cl-lib), ace-window(avy)])
(use-package dap-mode
:init
(add-hook 'lsp-mode-hook 'dap-mode)
(add-hook 'dap-mode-hook 'dap-ui-mode)
(add-hook 'dap-mode-hook 'dap-tooltip-mode)
(add-hook 'python-mode-hook (lambda() (require 'dap-python)))
(add-hook 'java-mode-hook (lambda() (require 'dap-java))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; settings for Program Languages ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lisp
;; You can choose paredit or lispy,but I use none of them,as they defined too many keybindings.
;; (use-package paredit :init (add-hook 'emacs-lisp-mode-hook #'enable-paredit-mode))
;; (use-package lispy :init (add-hook 'emacs-lisp-mode-hook (lambda () (lispy-mode 1))))
;; Python
(defmacro check-run-execute (exec-file &rest body)
"Find the EXEC-FILE and run the BODY."
`(if (not (executable-find ,exec-file))
(message "[ERROR]: <%s> not found!" ,exec-file)
,@body))
;;;###autoload
(defun python-isort ()
"Sort the imports with isort."
(interactive)
(check-run-execute "isort"
(shell-command-on-region
(point-min) (point-max)
"isort --atomic --profile=black -"
(current-buffer) t)))
;;;###autoload
(defun python-remove-all-unused-imports ()
"Remove all the unused imports, do NOT use pyimport, as it has bugs.
eg.from datetime import datetime."
(interactive)
(check-run-execute "autoflake"
(shell-command
(format "autoflake -i --remove-all-unused-imports %s" (buffer-file-name)))
(revert-buffer t t t)))
(add-hook 'python-mode-hook
(lambda ()
(add-hook 'before-save-hook #'python-isort nil t)
(define-key python-mode-map (kbd "C-c p s") 'python-isort)
(define-key python-mode-map (kbd "C-c p r") 'python-remove-all-unused-imports)))
;; Rust
(use-package rust-mode
:config
(setq rust-format-on-save t)
(define-key rust-mode-map (kbd "C-c C-c") 'rust-run))
;; Vue.js
;(use-package vue-mode
;; disable the ugly background color
;; [refs] https://github.com/AdamNiederer/vue-mode#how-do-i-disable-that-ugly-background-color
; :config (set-face-background 'mmm-default-submode-face nil))
;; Web Developemnt (html, css, js)
;(use-package web-mode
; :init (add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
; :config (setq web-mode-enable-current-element-highlight t))
;; use C-j to expand emmet
(use-package emmet-mode
:init
(add-hook 'web-mode-hook #'emmet-mode)
(add-hook 'css-mode-hook #'emmet-mode))
;(use-package json-mode)
;(use-package markdown-mode)
;(use-package protobuf-mode)
;(use-package restclient
; :init (add-to-list 'auto-mode-alist '("\\.http\\'" . restclient-mode)))
;(use-package yaml-mode)
(provide 'init-ide)
(require 'init-ide)
#+END_SRC
C+±IDE-Init.org
#+STARTUP:
* lsp-c++-ide
#+BEGIN_SRC emacs-lisp
(setq package-selected-packages '(lsp-mode yasnippet lsp-treemacs helm-lsp
projectile hydra flycheck company avy which-key helm-xref dap-mode))
(when (cl-find-if-not #'package-installed-p package-selected-packages)
(package-refresh-contents)
(mapc #'package-install package-selected-packages))
;; sample `helm' configuration use https://github.com/emacs-helm/helm/ for details
(helm-mode)
(require 'helm-xref)
(define-key global-map [remap find-file] #'helm-find-files)
(define-key global-map [remap execute-extended-command] #'helm-M-x)
(define-key global-map [remap switch-to-buffer] #'helm-mini)
(which-key-mode)
(add-hook 'c-mode-hook 'lsp)
(add-hook 'c++-mode-hook 'lsp)
(setq gc-cons-threshold (* 100 1024 1024)
read-process-output-max (* 1024 1024)
treemacs-space-between-root-nodes nil
company-idle-delay 0.0
company-minimum-prefix-length 1
lsp-idle-delay 0.1) ;; clangd is fast
(with-eval-after-load 'lsp-mode
(add-hook 'lsp-mode-hook #'lsp-enable-which-key-integration)
(require 'dap-cpptools)
(yas-global-mode))
#+END_SRC
init.el
( org-babel-load-file ( expand-file-name "~/.emacs.d/org-conf/MyInit.org" ) )
( org-babel-load-file ( expand-file-name "~/.emacs.d/org-conf/C++-IDE-Init.org" ) )
copy paste