Skip to content

WuTao18/doom-emacs-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Doom Emacs Configuration

This is my doom emacs configuration.

I mainly use Emacs on Windows PC (WSL2 and WSLg) and Linux servers.

Setup

My user info.

(setq
 user-full-name "Tao Wu"
 user-mail-address "taowuuwoat@outlook.com")

Set fonts.

(setq
 doom-font (font-spec :family "iosevka" :size 22)
 doom-variable-pitch-font (font-spec :family "iosevka" :size 22))

Set theme.

(setq doom-theme 'doom-opera-light)

Set window size at startup.

(pushnew! initial-frame-alist '(width . 200) '(height . 55))

WSL

Share clipboard.

References:

;; ========= share clipboard =========
(defun copy-selected-text (start end)
  (interactive "r")
  (if (use-region-p)
      (let ((text (buffer-substring-no-properties start end)))
        (shell-command (concat "echo '" text "' | clip.exe")))))

; wsl-copy
(defun wsl-copy (start end)
  (interactive "r")
  (shell-command-on-region start end "clip.exe")
  (deactivate-mark))

; wsl-paste
(defun wsl-paste ()
  (interactive)
  (let
      ((clipboard
        ; (shell-command-to-string "powershell.exe -command 'Get-Clipboard' 2> /dev/null")))
        (shell-command-to-string
         "powershell.exe -command 'Get-Clipboard' | iconv -f gbk -t utf8"))) ; Convert GBK to UTF-8, 解决粘贴中文时乱码的问题
    (setq clipboard (replace-regexp-in-string "\r" "" clipboard)) ; Remove Windows ^M characters
    (setq clipboard (substring clipboard 0 -1)) ; Remove newline added by Powershell
    (insert clipboard)))

Paste image from win clipboard. Press <F8> to paste image.

References

;; ========= paste image from win clipboard =========
(defun my-yank-image-from-win-clipboard-through-powershell ()
  "to simplify the logic, use c:/Users/Public as temporary directoy, then move it into current directoy

Anyway, if need to modify the file name, please DONT delete or modify file extension \".png\",
otherwise this function don't work and don't know the reason
"
  (interactive)
  (let* ((powershell
          "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe")
         (file-name
          (format "%s"
                  (read-from-minibuffer
                   "Img Name:"
                   (format-time-string "screenshot_%Y%m%d_%H%M%S.png"))))
         ;; (file-path-powershell (concat "c:/Users/\$env:USERNAME/" file-name))
         (file-path-wsl
          (concat
           "~/knowledge-base/org/images/"
           (format-time-string "%Y/")
           file-name)))
    (if (file-exists-p
         (concat "~/knowledge-base/org/images/" (format-time-string "%Y")))
        (ignore)
      (make-directory
       (concat "~/knowledge-base/org/images/" (format-time-string "%Y"))))
    ;; (shell-command (concat powershell " -command \"(Get-Clipboard -Format Image).Save(\\\"C:/Users/\\$env:USERNAME/" file-name "\\\")\""))
    (shell-command
     (concat
      powershell
      " -command \"(Get-Clipboard -Format Image).Save(\\\"C:/Users/Public/"
      file-name
      "\\\")\""))
    (rename-file (concat "/mnt/c/Users/Public/" file-name) file-path-wsl)
    (format "%s" file-path-wsl)))

(defun my-yank-image-link-into-org-from-wsl ()
  "call `my-yank-image-from-win-clipboard-through-powershell' and insert image file link with org-mode format"
  (interactive)
  (let* ((file-path (my-yank-image-from-win-clipboard-through-powershell))
         (file-link
          (format "[[file:%s][%s]]"
                  file-path
                  (file-name-sans-extension
                   (file-name-nondirectory file-path)))))
    (insert (concat "#+ATTR_HTML: :width 50%\n") file-link)))
(global-set-key (kbd "<f8>") 'my-yank-image-link-into-org-from-wsl)

Use win’s default web browser to open url.

;; ========= use win's default browser to open url =========
(when (and (eq system-type 'gnu/linux)
           (string-match
            "Linux.*Microsoft.*Linux" (shell-command-to-string "uname -a")))
  (setq
   browse-url-generic-program "/mnt/c/Windows/System32/cmd.exe"
   browse-url-generic-args '("/c" "start")
   browse-url-browser-function #'browse-url-generic))

Define a function to enable / disable mouse scroll in terminal emacs.

;; ========= Mouse scrolling in terminal emacs =========
(defun enable-mouse-scroll ()
  (unless (display-graphic-p)
    ;; activate mouse-based scrolling
    (xterm-mouse-mode 1)
    (global-set-key (kbd "<mouse-4>") 'scroll-down-line)
    (global-set-key (kbd "<mouse-5>") 'scroll-up-line)))

(defun mouse-scroll-mode ()
  (interactive)
  (if xterm-mouse-mode
      (xterm-mouse-mode 0)
    (enable-mouse-scroll)))

Set Chinese input method. I use pyim as my default input method. pyim and pyim-tsinghua-dict should be cloned into ~/.doom.d/packages.

;; ========= pyim =========
(add-load-path! "~/.doom.d/packages/pyim")

(require 'pyim)
(require 'pyim-basedict)
(require 'pyim-cregexp-utils)
;; 如果使用 popup page tooltip, 就需要加载 popup 包。
;; (require 'popup nil t)
;; (setq pyim-page-tooltip 'popup)
(setq pyim-page-tooltip '(posframe popup minibuffer))

;; 如果使用 pyim-dregcache dcache 后端,就需要加载 pyim-dregcache 包。
;; (require 'pyim-dregcache)
;; (setq pyim-dcache-backend 'pyim-dregcache)

;; 加载 basedict 拼音词库。
(pyim-basedict-enable)

;; 将 Emacs 默认输入法设置为 pyim.
(setq default-input-method "pyim")

;; 显示 5 个候选词。
(setq pyim-page-length 5)

;; 金手指设置,可以将光标处的编码(比如:拼音字符串)转换为中文。
;;(global-set-key (kbd "M-j") 'pyim-convert-string-at-point)
(global-set-key (kbd "M-n") 'pyim-convert-string-at-point)

;; 按 "C-<return>" 将光标前的 regexp 转换为可以搜索中文的 regexp.
(define-key
 minibuffer-local-map (kbd "C-<return>") 'pyim-cregexp-convert-at-point)

;; 设置 pyim 默认使用的输入法策略,我使用全拼。
(pyim-default-scheme 'quanpin)
;; (pyim-default-scheme 'wubi)
;; (pyim-default-scheme 'cangjie)

;; 设置 pyim 是否使用云拼音
(setq pyim-cloudim 'baidu)

;; 设置 pyim 探针
;; 设置 pyim 探针设置,这是 pyim 高级功能设置,可以实现 *无痛* 中英文切换 :-)
;; 我自己使用的中英文动态切换规则是:
;; 1. 光标只有在注释里面时,才可以输入中文。
;; 2. 光标前是汉字字符时,才能输入中文。
;; 3. 使用 M-j 快捷键,强制将光标前的拼音字符串转换为中文。
(setq-default pyim-english-input-switch-functions
              '(pyim-probe-dynamic-english
              ; '(pyim-probe-auto-english
                pyim-probe-isearch-mode
                pyim-probe-program-mode
                pyim-probe-org-structure-template))

(setq-default pyim-punctuation-half-width-functions
              '(pyim-probe-punctuation-line-beginning
                pyim-probe-punctuation-after-punctuation))

;; 开启代码搜索中文功能(比如拼音,五笔码等)
(pyim-isearch-mode 1)

;; 让 vertico, selectrum 等补全框架,通过 orderless 支持拼音搜索候选项功能。
(defun my-orderless-regexp (orig-func component)
  (let ((result (funcall orig-func component)))
    (pyim-cregexp-build result)))

;; https://www.skfwe.cn/p/org-roam-%E4%BD%BF%E7%94%A8/
;; 以下解决 在vertico 搜索时按 C-n C-p 卡顿的问题
(defun my/pyim-advice-add ()
  (advice-add 'orderless-regexp :around #'my-orderless-regexp))

(defun my/pyim-advice-remove (&optional n)
  (advice-remove 'orderless-regexp #'my-orderless-regexp))

(advice-add  #'vertico-next :before #'my/pyim-advice-remove)
(advice-add  #'vertico-previous :before #'my/pyim-advice-remove)
(advice-add  'abort-recursive-edit :before #'my/pyim-advice-add)
(advice-add  'abort-minibuffers :before #'my/pyim-advice-add)
(advice-add  'exit-minibuffer :before #'my/pyim-advice-add)
(my/pyim-advice-add)   ;; 默认开启

(add-load-path! "~/.doom.d/packages/pyim-tsinghua-dict")
(require 'pyim-tsinghua-dict)
(pyim-tsinghua-dict-enable)

Org Mode

Set default directory for org files.

(setq org-directory "~/knowledge-base/org/")

Log time when task is done.

;; ========= log time when task is done =========
(setq org-log-done 'time)

Set org capture templates.

;; ========= org capture =========
(after!
 org (setq org-capture-templates nil)
 (add-to-list
  'org-capture-templates
  '("i"
    "Inbox"
    entry
    (file+olp+datetree "~/knowledge-base/org/inbox.org")
    "* %U %?\n"
    :tree-type month))
 (add-to-list
  'org-capture-templates
  '("t"
    "Tasks"
    entry
    (file+olp+datetree "~/knowledge-base/org/tasks.org")
    "* TODO %? %U"
    :tree-type month))
 (add-to-list
  'org-capture-templates
  '("j"
    "Journal"
    entry
    (file+olp+datetree "~/knowledge-base/org/journal.org")
    "* %U %?\n")))

English date/timestamp format.

;; ========= English date/timestamp format =========
(setq system-time-locale "C")

Insert inactive timestamp with current time.

;; ========= insert timestamp with time =========
(defun insert-now-timestamp-inactive ()
  "Insert org mode inactive timestamp at point with current date and time."
  (interactive)
  ; (org-time-stamp-inactive (current-time)))
  (org-insert-time-stamp nil t t nil nil nil))

(map! :after org
      :map org-mode-map
      "C-c i" #'insert-now-timestamp-inactive)

Config org-modern.

;; ========= org-modern =========
(if (display-graphic-p)
    (use-package!
     org-modern
     :hook (org-mode . org-modern-mode)
     :config
     (setq
      ;; Edit settings
      org-auto-align-tags nil
      org-tags-column 0
      org-catch-invisible-edits 'show-and-error
      org-special-ctrl-a/e t
      org-insert-heading-respect-content t

      ;; Org styling, hide markup etc.
      ; org-hide-emphasis-markers t
      org-pretty-entities t
      org-ellipsis ""

      ;; Agenda styling
      org-agenda-tags-column 0
      org-agenda-block-separator ?─
      org-agenda-time-grid
      '((daily today require-timed)
        (800 1000 1200 1400 1600 1800 2000)
        " ┄┄┄┄┄ "
        "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄")
      org-agenda-current-time-string "⭠ now ─────────────────────────────────────────────────")

     ; (global-org-modern-mode)
     ))

Config org-roam & org-roam-ui.

;; ========= org roam + org roam ui =========
(use-package
 org-roam
 :ensure t
 :custom (org-roam-directory (file-truename "~/knowledge-base/org/roam"))
 :bind
 (("C-c n l" . org-roam-buffer-toggle)
  ("C-c n f" . org-roam-node-find)
  ("C-c n g" . org-roam-graph)
  ("C-c n i" . org-roam-node-insert)
  ("C-c n c" . org-roam-capture)
  ;; Dailies
  ("C-c n j" . org-roam-dailies-capture-today))
 :config
 ;; If you're using a vertical completion framework, you might want a more informative completion interface
 (setq org-roam-node-display-template
       (concat "${title:*} " (propertize "${tags:10}" 'face 'org-tag)))
 (org-roam-db-autosync-mode)
 ;; If using org-roam-protocol
 (require 'org-roam-protocol))

(use-package! websocket :after org-roam)

(use-package!
 org-roam-ui
 :after org-roam ;; or :after org
 ;;         normally we'd recommend hooking orui after org-roam, but since org-roam does not have
 ;;         a hookable mode anymore, you're advised to pick something yourself
 ;;         if you don't care about startup time, use
 ;;  :hook (after-init . org-roam-ui-mode)
 :config
 (setq
  org-roam-ui-sync-theme t
  org-roam-ui-follow t
  org-roam-ui-update-on-save t
  org-roam-ui-open-on-start t))

(setq org-roam-dailies-capture-templates
      '(("d" "default" entry
         "* %U %?"
         :target (file+head "%<%Y-%m-%d>.org"
                            "#+title: %<%Y-%m-%d>\n"))))