summaryrefslogtreecommitdiff
path: root/lisp/km-org-agenda.el
blob: 45518b9f88e87e991f88c06f9144fc7ef38d13f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
;;; km-org-agenda.el --- Org mode agenda extensions

;; Copyright (C) 2012-2020 Kyle Meyer <kyle@kyleam.com>

;; Author: Kyle Meyer <kyle@kyleam.com>
;; URL: https://git.kyleam.com/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 <http://www.gnu.org/licenses/>.

;;; Code:

(require 'org-agenda)
(require 'km-org)

(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-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)
  (outline-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)))

(defun km/org-agenda-reschedule-by-days ()
  (interactive)
  (org-agenda-schedule
   nil
   (concat "+" (read-string "Days from now: ") "d")))

(provide 'km-org-agenda)
;;; km-org-agenda.el ends here