From 4944668dfdc2c33be7a08f7cc6caa18a74d71c1b Mon Sep 17 00:00:00 2001 From: Alex Ragone Date: Sat, 13 Apr 2019 17:18:11 +1200 Subject: [PATCH 1/2] feat: added org-habit module Added a simple way to make the org-habit consistency graphs align to the right margin as well as being responsive to the width of the window. --- init.example.el | 1 + modules/lang/org/+habit.el | 29 +++++++++++++++++++++++++++++ modules/lang/org/config.el | 1 + 3 files changed, 31 insertions(+) create mode 100644 modules/lang/org/+habit.el diff --git a/init.example.el b/init.example.el index c7cb7c8aa7b..6117947bae4 100644 --- a/init.example.el +++ b/init.example.el @@ -124,6 +124,7 @@ +babel ; running code in org +capture ; org-capture in and outside of Emacs +export ; Exporting org to whatever you want + +habit ; Keep track of your habits +present ; Emacs for presentations +protocol) ; Support for org-protocol:// links ;;perl ; write code no one else can comprehend diff --git a/modules/lang/org/+habit.el b/modules/lang/org/+habit.el new file mode 100644 index 00000000000..352b96a72f2 --- /dev/null +++ b/modules/lang/org/+habit.el @@ -0,0 +1,29 @@ +;;; lang/org/+habit.el -*- lexical-binding: t; -*- + +(require 'org-habit) + +(defvar +org-habit-graph-padding 2 + "The padding added to the end of the consistency graph") + +(defvar +org-habit-min-width 30 + "Hides the consistency graph if the `org-habit-graph-column' is less than this value") + +(defvar +org-habit-graph-window-ratio 0.3 + "The ratio of the consistency graphs relative to the window width") + +(defun +org-habit-resize-graph() + "Right align and resize the consistency graphs based on `+org-habit-graph-window-ratio'" + (let* ((total-days (float (+ org-habit-preceding-days org-habit-following-days))) + (preceding-days-ratio (/ org-habit-preceding-days total-days)) + (graph-width (floor (* (window-width) +org-habit-graph-window-ratio))) + (preceding-days (floor (* graph-width preceding-days-ratio))) + (following-days (- graph-width preceding-days)) + (graph-column (- (window-width) (+ preceding-days following-days))) + (graph-column-adjusted (if (> graph-column +org-habit-min-width) + (- graph-column +org-habit-graph-padding) + nil))) + (setq-local org-habit-preceding-days preceding-days) + (setq-local org-habit-following-days following-days) + (setq-local org-habit-graph-column graph-column-adjusted))) + +(add-hook! 'org-agenda-mode-hook #'+org-habit-resize-graph) diff --git a/modules/lang/org/config.el b/modules/lang/org/config.el index cfb8050d8db..18ee476a300 100644 --- a/modules/lang/org/config.el +++ b/modules/lang/org/config.el @@ -514,5 +514,6 @@ conditions where a window's buffer hasn't changed at the time this hook is run." (if (featurep! +babel) (load! "+babel")) (if (featurep! +capture) (load! "+capture")) (if (featurep! +export) (load! "+export")) + (if (featurep! +habit) (load! "+habit")) (if (featurep! +present) (load! "+present")) (if (featurep! +protocol) (load! "+protocol"))) From ae0c0f283727d6ccd754fa6a12026eacefa9a13d Mon Sep 17 00:00:00 2001 From: Alex Ragone Date: Mon, 22 Apr 2019 17:14:28 +1200 Subject: [PATCH 2/2] refactor: lazy load and use conventions for hooks --- modules/lang/org/+habit.el | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/lang/org/+habit.el b/modules/lang/org/+habit.el index 352b96a72f2..02963d06dd4 100644 --- a/modules/lang/org/+habit.el +++ b/modules/lang/org/+habit.el @@ -1,7 +1,5 @@ ;;; lang/org/+habit.el -*- lexical-binding: t; -*- -(require 'org-habit) - (defvar +org-habit-graph-padding 2 "The padding added to the end of the consistency graph") @@ -11,8 +9,9 @@ (defvar +org-habit-graph-window-ratio 0.3 "The ratio of the consistency graphs relative to the window width") -(defun +org-habit-resize-graph() +(defun +org-habit|resize-graph() "Right align and resize the consistency graphs based on `+org-habit-graph-window-ratio'" + (require 'org-habit) (let* ((total-days (float (+ org-habit-preceding-days org-habit-following-days))) (preceding-days-ratio (/ org-habit-preceding-days total-days)) (graph-width (floor (* (window-width) +org-habit-graph-window-ratio))) @@ -26,4 +25,4 @@ (setq-local org-habit-following-days following-days) (setq-local org-habit-graph-column graph-column-adjusted))) -(add-hook! 'org-agenda-mode-hook #'+org-habit-resize-graph) +(add-hook 'org-agenda-mode-hook #'+org-habit|resize-graph)