From 4af52c5700812df1457b8c3b7e3640432406226e Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Thu, 17 Nov 2016 21:07:07 -0500 Subject: Use separate file for Org agenda extensions --- init.el | 40 ++++++++++-------- lisp/km-org-agenda.el | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ lisp/km-org.el | 94 ----------------------------------------- 3 files changed, 137 insertions(+), 111 deletions(-) create mode 100644 lisp/km-org-agenda.el diff --git a/init.el b/init.el index 958263a..2a91f78 100644 --- a/init.el +++ b/init.el @@ -294,12 +294,7 @@ (use-package km-org :defer t - :after org-agenda :init - (bind-keys :map km/global-org-map - ("j" . km/org-goto-agenda-heading) - ("m" . km/org-open-default-notes-file-inbox) - ("n" . km/org-agenda-add-or-remove-file)) (bind-keys :map km/org-prefix-map ("c" . km/org-clone-and-shift-by-repeater) ("d" . km/org-link-dired-jump) @@ -336,21 +331,32 @@ (add-hook 'org-after-refile-insert-hook #'km/org-maybe-sort-parent) (org-add-link-type "pmid" #'km/org-pmid-open)) - (after 'org-agenda - (setq km/org-agenda-file-directory "~/notes/agenda/" - org-agenda-files (list km/org-agenda-file-directory)) - (add-hook 'org-agenda-finalize-hook #'km/org-agenda-cd-and-read-dir-locals) - (add-hook 'org-agenda-finalize-hook #'km/org-agenda-store-current-span) - - (add-to-list 'org-agenda-bulk-custom-functions - '(?D km/org-agenda-delete-subtree)) - - (define-key org-agenda-mode-map "D" #'km/org-agenda-delete-subtree) + (after 'ox-md + (advice-add 'org-md-paragraph :filter-return #'km/org-md-fill-string))) +(use-package km-org-agenda + :defer t + :after org-agenda + :init + (bind-keys :map km/global-org-map + ("j" . km/org-goto-agenda-heading) + ("m" . km/org-open-default-notes-file-inbox) + ("n" . km/org-agenda-add-or-remove-file)) + (after 'org (define-key org-mode-map [remap org-agenda-set-restriction-lock] #'km/org-agenda-set-restriction-lock)) - (after 'ox-md - (advice-add 'org-md-paragraph :filter-return #'km/org-md-fill-string))) + + :config + (setq km/org-agenda-file-directory "~/notes/agenda/" + org-agenda-files (list km/org-agenda-file-directory)) + + (add-hook 'org-agenda-finalize-hook #'km/org-agenda-cd-and-read-dir-locals) + (add-hook 'org-agenda-finalize-hook #'km/org-agenda-store-current-span) + + (add-to-list 'org-agenda-bulk-custom-functions + '(?D km/org-agenda-delete-subtree)) + + (define-key org-agenda-mode-map "D" #'km/org-agenda-delete-subtree)) (use-package poporg :defer t diff --git a/lisp/km-org-agenda.el b/lisp/km-org-agenda.el new file mode 100644 index 0000000..553db51 --- /dev/null +++ b/lisp/km-org-agenda.el @@ -0,0 +1,114 @@ +;;; km-org-agenda.el --- Org mode agenda extensions + +;; Copyright (C) 2012-2016 Kyle Meyer + +;; Author: Kyle Meyer +;; URL: https://github.com/kyleam/emacs.d + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Code: + +(require 'org-agenda) +(require 'km-org) + +(defvar km/org-agenda-file-directory nil) + +(defun km/org-agenda-cd-and-read-dir-locals () + (unless (get 'org-agenda-files 'org-restrict) + (setq default-directory (expand-file-name "~/notes/")) + (hack-local-variables))) + +(defun km/org-agenda-store-current-span () + "Store the current span value in `org-agenda-span'. +This allows the view to persist when the agenda buffer is +killed." + (when org-agenda-current-span + (setq org-agenda-span org-agenda-current-span))) + +;;;###autoload +(defun km/org-agenda-add-or-remove-file (file) + "Add or remove link to FILE in `km/org-agenda-file-directory'. +If a link for FILE does not exist, create it. Otherwise, remove +it. Like `org-agenda-file-to-front', this results in FILE being +displayed in the agenda." + (interactive (list (cl-case major-mode + (org-mode (buffer-file-name)) + (dired-mode (dired-get-filename)) + (org-agenda-mode (ignore-errors (save-window-excursion + (org-agenda-goto) + (buffer-file-name)))) + (t (read-file-name "Link file: "))))) + (let ((agenda-file (expand-file-name (file-name-nondirectory file) + km/org-agenda-file-directory))) + (if (file-equal-p (file-truename agenda-file) file) + (progn + (when (called-interactively-p) (message "Deleting %s" agenda-file)) + (delete-file agenda-file)) + (when (called-interactively-p) (message "Adding %s" agenda-file)) + (make-symbolic-link file agenda-file)))) + +;;;###autoload +(defun km/org-open-default-notes-file-inbox () + "Open \"Inbox\" heading of `org-default-notes-file'." + (interactive) + (find-file org-default-notes-file) + (goto-char (org-find-exact-headline-in-buffer "Inbox" nil t)) + (recenter-top-bottom 0) + (show-children)) + +;;;###autoload +(defun km/org-goto-agenda-heading () + "Jump to heading in agenda files." + (interactive) + (let ((org-refile-targets + '((org-agenda-files :maxlevel . 3) + (org-agenda-text-search-extra-files :maxlevel . 3))) + (org-refile-use-outline-path t)) + (org-refile '(4)))) + +(defun km/org-delete-subtree () + (org-back-to-heading t) + (delete-region + (point) + (org-element-property :end (org-element-at-point)))) + +(defun km/org-agenda-delete-subtree () + (interactive) + (org-agenda-archive-with #'km/org-delete-subtree)) + +;;;###autoload +(defun km/org-agenda-set-restriction-lock (&optional type) + "Call `org-agenda-set-restriction-lock' with flipped C-u meaning." + (interactive "P") + (org-agenda-set-restriction-lock + (cond ((equal type '(4)) nil) + (type) + (t '(4))))) + +(defun km/org-agenda-refile-dwim () + "Rebind `org-refile-targets' if next window is an Org buffer. +A target is determined by `km/org-refile-dwim-target-file'." + (interactive) + (let* ((dwim-target (km/org-refile-dwim-target-file)) + (org-refile-targets (if dwim-target + `((nil + :maxlevel . ,km/org-refile-dwim-maxlevel) + (dwim-target + :maxlevel . ,km/org-refile-dwim-maxlevel)) + org-refile-targets))) + (call-interactively #'org-agenda-refile))) + +(provide 'km-org-agenda) +;;; km-org-agenda.el ends here diff --git a/lisp/km-org.el b/lisp/km-org.el index 937a209..156218b 100644 --- a/lisp/km-org.el +++ b/lisp/km-org.el @@ -23,7 +23,6 @@ (require 'cl-lib) (require 'dash) (require 'org) -(require 'org-agenda) (require 'org-link-edit) (require 'ox-ascii) (require 's) @@ -368,86 +367,6 @@ the buffer widened." (org-show-entry) (pop-to-buffer (current-buffer)))))) - -;;; Agenda - - -(defvar km/org-agenda-file-directory nil) - -;;;###autoload -(defun km/org-agenda-cd-and-read-dir-locals () - (unless (get 'org-agenda-files 'org-restrict) - (setq default-directory (expand-file-name "~/notes/")) - (hack-local-variables))) - -;;;###autoload -(defun km/org-agenda-store-current-span () - "Store the current span value in `org-agenda-span'. -This allows the view to persist when the agenda buffer is -killed." - (when org-agenda-current-span - (setq org-agenda-span org-agenda-current-span))) - -;;;###autoload -(defun km/org-agenda-add-or-remove-file (file) - "Add or remove link to FILE in `km/org-agenda-file-directory'. -If a link for FILE does not exist, create it. Otherwise, remove -it. Like `org-agenda-file-to-front', this results in FILE being -displayed in the agenda." - (interactive (list (cl-case major-mode - (org-mode (buffer-file-name)) - (dired-mode (dired-get-filename)) - (org-agenda-mode (ignore-errors (save-window-excursion - (org-agenda-goto) - (buffer-file-name)))) - (t (read-file-name "Link file: "))))) - (let ((agenda-file (expand-file-name (file-name-nondirectory file) - km/org-agenda-file-directory))) - (if (file-equal-p (file-truename agenda-file) file) - (progn - (when (called-interactively-p) (message "Deleting %s" agenda-file)) - (delete-file agenda-file)) - (when (called-interactively-p) (message "Adding %s" agenda-file)) - (make-symbolic-link file agenda-file)))) - -;;;###autoload -(defun km/org-open-default-notes-file-inbox () - "Open \"Inbox\" heading of `org-default-notes-file'." - (interactive) - (find-file org-default-notes-file) - (goto-char (org-find-exact-headline-in-buffer "Inbox" nil t)) - (recenter-top-bottom 0) - (show-children)) - -;;;###autoload -(defun km/org-goto-agenda-heading () - "Jump to heading in agenda files." - (interactive) - (let ((org-refile-targets - '((org-agenda-files :maxlevel . 3) - (org-agenda-text-search-extra-files :maxlevel . 3))) - (org-refile-use-outline-path t)) - (org-refile '(4)))) - -(defun km/org-delete-subtree () - (org-back-to-heading t) - (delete-region - (point) - (org-element-property :end (org-element-at-point)))) - -(defun km/org-agenda-delete-subtree () - (interactive) - (org-agenda-archive-with #'km/org-delete-subtree)) - -;;;###autoload -(defun km/org-agenda-set-restriction-lock (&optional type) - "Call `org-agenda-set-restriction-lock' with flipped C-u meaning." - (interactive "P") - (org-agenda-set-restriction-lock - (cond ((equal type '(4)) nil) - (type) - (t '(4))))) - ;;; Refiling @@ -520,19 +439,6 @@ A target is determined by `km/org-refile-dwim-target-file'." org-refile-targets))) (call-interactively #'org-refile))) -(defun km/org-agenda-refile-dwim () - "Rebind `org-refile-targets' if next window is an Org buffer. -A target is determined by `km/org-refile-dwim-target-file'." - (interactive) - (let* ((dwim-target (km/org-refile-dwim-target-file)) - (org-refile-targets (if dwim-target - `((nil - :maxlevel . ,km/org-refile-dwim-maxlevel) - (dwim-target - :maxlevel . ,km/org-refile-dwim-maxlevel)) - org-refile-targets))) - (call-interactively #'org-agenda-refile))) - (defun km/org-refile-dwim-target-file () "Return next window that is an Org buffer." (let* ((from-buffer (current-buffer)) -- cgit v1.2.3