Files
.emacs.d/lisp/init-org.el
trogloxene 95a6adb2b8 feat: add org-journal integration and calendar keybindings
- Configure org-journal with custom templates and auto-setup hooks
- Add keybindings for calendar access and journal management
- Update org-agenda-files to include journal and blog paths
2026-03-22 03:16:08 +08:00

203 lines
6.8 KiB
EmacsLisp
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
;;; init-org.el
;;; -*- lexical-binding: t -*-
(use-package org)
;; 预加载防止windows的org-mode加载卡顿
(if *is-windows*
(progn (run-with-idle-timer
0.5 nil
(lambda ()
(with-temp-buffer
(org-mode)
(org-mode))
(message "Org preloaded")))))
;; 一些随org-mode加载的基础配置
(add-hook 'org-mode-hook
(lambda ()
(require 'org-tempo)
(org-indent-mode)
(local-set-key (kbd "C-c i") 'consult-org-heading)))
;; 设置todo关键字
(setq org-todo-keywords
(quote ((sequence "TODO(t)" "STARTED(s)" "|" "DONE(d!/!)")
(sequence "WAITING(w@/!)" "SOMEDAY(S)" "|" "CANCELLED(c@/!)" "MEETING(m)" "PHONE(p)"))))
;; 设置标题字体大小
;; (dolist (face org-level-faces)
;; (set-face-attribute face nil :height 1))
;; log默认放入drawer中
(setq org-log-done t)
(setq org-log-into-drawer t)
;; 将系统时间(星期)设为英文,防止中文乱码
(setq system-time-locale "C")
;; capture模板
(setq org-capture-templates
'(("t" "Todo" entry (file+headline "d:/emacs/org/todos.org" "Workspace")
"* TODO [#B] %?\n %i\n %U"
:empty-lines 1)
("j" "Journal" entry (file+datetree "d:/emacs/org/journal.org")
"* %?\nEntered on %U\n %i\n")))
(global-set-key (kbd "C-c o c") 'org-capture)
(setq org-agenda-custom-commands
'(("c" "important stuff"
((tags-todo "+PRIORITY=\"A\"")))
;; ...other commands here
))
;; org-contrib 和 org-checklist
(use-package org-contrib
:straight (org-contrib :type git :host github :repo "emacsmirror/org-contrib"
:depth 1))
(add-hook 'org-mode-hook
(lambda ()
(require 'org-checklist nil t)))
;; agenda相关
(global-set-key (kbd "C-c a") 'org-agenda)
(with-eval-after-load 'org
(setq org-agenda-files '("d:/emacs/org/learning-org-mode.org"
"d:/emacs/hugo/this-is-my-blog/all-blog.org"
"d:/emacs/my-org-daily/"))) ; agenda文件目录或文件
(setq org-agenda-span 'day)
;; hugo
(with-eval-after-load 'org-capture
(defun org-hugo-new-subtree-post-capture-template ()
"Returns `org-capture' template string for new Hugo post.
See `org-capture-templates' for more information."
(let* ((title (read-from-minibuffer "Post Title: ")) ;Prompt to enter the post title
(fname (org-hugo-slug title)))
(mapconcat #'identity
`(
,(concat "* TODO " title)
":PROPERTIES:"
,(concat ":EXPORT_FILE_NAME: " fname)
":END:"
"\n\n") ;Place the cursor here finally
"\n")))
(add-to-list 'org-capture-templates
'("h" ;`org-capture' binding + h
"Hugo post"
entry
;; It is assumed that below file is present in `org-directory'
;; and that it has a "Blog Ideas" heading. It can even be a
;; symlink pointing to the actual location of all-posts.org!
(file+headline "d:/emacs/hugo/this-is-my-blog/all-blog.org" "Blog Ideas")
(function org-hugo-new-subtree-post-capture-template))))
;; 快速部署
(defun my-blog-publish ()
"publish blog , git add/commit/push , generate commit message"
(interactive)
(let* ((blog-dir "d:/emacs/hugo/this-is-my-blog/")
(timestamp (format-time-string "%Y-%m-%d %H:%M:%S"))
(default-directory blog-dir))
;; save buffer
(save-buffer)
;; git add
(message "Adding files...")
(eshell-command "git add .")
;; git commit with timestamp
(message "Committing: %s" timestamp)
(eshell-command (format "git commit -m \"Update: %s\"" timestamp))
;; git push
(message "Pushing to remote...")
(let ((exit-code (eshell-command "git push")))
(if (eq exit-code t)
(message "Push may have failed or nothing to push, check *Messages* buffer for details")
(message "Push successfully %s" timestamp)))))
(global-set-key (kbd "C-c o p") 'my-blog-publish)
;; 预览博客
(defun my-blog-preview ()
(interactive)
(save-buffer)
(let ((default-directory "d:/emacs/hugo/this-is-my-blog/"))
;; 启动 Hugo server
(start-process "hugo-server" "*hugo-server*"
"hugo" "server" "-D" "--bind" "0.0.0.0" "--port" "1313")
;; 等待服务器启动
(sleep-for 2)
;; 自动打开浏览器
(browse-url "http://localhost:1313")))
(global-set-key (kbd "C-c o v") 'my-blog-preview)
;; 批量调整表格
(defun org-table-align-all ()
"Ajust all table in current buffer."
(interactive)
(save-excursion
(goto-char (point-min))
(org-table-map-tables 'org-table-align t)))
;; org-journal
(use-package org-journal
:config
(setq org-journal-dir "D:/emacs/my-org-daily/"
org-journal-file-format "%Y-%m-%d.org"
org-journal-file-header "#+title: Daily Journal\n#+category: journal\n\n"
org-journal-time-format "%R \n:PROPERTIES:\n:Created: <%Y-%m-%d %a %H:%M:%S>\n:END:\n"
org-journal-time-prefix "\n*** TODO "
org-journal-hide-entries-p t
org-journal-date-format (lambda (time)
(format "* %s %s\n:PROPERTIES:\n:Created: [%s]\n:END:"
(format-time-string "%Y-%m-%d" time)
(format-time-string "%a" time)
(format-time-string "%Y-%m-%d %a" time))))
(defun my/journal-setup-new-day ()
(save-excursion
(org-back-to-heading)
(let ((day-end (save-excursion (org-end-of-subtree) (point))))
(unless (search-forward "** daily" day-end t)
(goto-char day-end)
(insert "\n\n** daily\n\n\n")
(insert "** tasks\n")))))
(add-hook 'org-journal-after-header-create-hook #'my/journal-setup-new-day)
(defun my/journal-position-cursor-after-time ()
(when (derived-mode-p 'org-journal-mode)
(org-back-to-heading)
(end-of-line)))
(add-hook 'org-journal-after-entry-create-hook #'my/journal-position-cursor-after-time)
(defun org-journal-open-entry-for-editing (&optional event)
"Open journal entry for the date under cursor in calendar for editing."
(interactive
(list last-nonmenu-event))
(let* ((date (calendar-cursor-to-date t event))
(time (org-journal--calendar-date->time date))
(org-journal-file (org-journal--get-entry-path time)))
(if (file-exists-p org-journal-file)
(progn
(funcall org-journal-find-file org-journal-file)
(unless (org-journal--daily-p)
(org-journal--goto-entry date)))
(message "No journal entry for this date.")))))
;; 在org-journal加载cns-mode
(add-hook 'org-journal-mode-hook
(lambda ()
(global-cns-mode-enable-in-buffer)))
(provide 'init-org)