-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocc.el
More file actions
470 lines (415 loc) · 18.9 KB
/
occ.el
File metadata and controls
470 lines (415 loc) · 18.9 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
;;; occ.el --- occ -*- lexical-binding: t; -*-
;; Copyright (C) 2016 sharad
;; Author: sharad <>
;; Keywords: convenience
;; 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/>.
;;; Commentary:
;; https://economictimes.indiatimes.com/small-biz/hr-leadership/leadership/getting-more-done-why-task-management-is-the-key-to-managing-time/articleshow/70814450.cms
;;; Code:
(provide 'occ)
(require 'switch-buffer-functions)
(require 'package)
(require 'occ-obj-ctor)
(require 'occ-main)
(require 'occ-util-common)
(require 'occ-filter-config)
(require 'occ-resolve-clock)
(require 'occ-test)
(require 'occ-config)
(require 'occ-mode)
(require 'occ-unnamed)
(defvar *occ-collector* nil)
(defvar *occ-collector-default-key* 'default)
(defun occ-collector-default-key (&optional key)
(if key
(setq *occ-collector-default-key* key)
*occ-collector-default-key*))
(defun occ-collector-read-key (&optional prompt keys)
(occ-util-select-from-sym-list (or prompt "key for spec: ")
(or keys (occ-collector-keys))))
(defun occ-collector-get (key)
(alist-get key *occ-collector*))
(cl-defun occ-collector-get-create (key desc spec files &key depth limit rank level)
(let ((depth (or depth 0))
(limit (or limit 0))
(rank (or rank 0))
(level (or level :optional)))
(unless (alist-get key *occ-collector*)
(setf (alist-get key *occ-collector*) (occ-obj-build-collection desc
key
spec
files
depth
limit
rank
level))))
(alist-get key *occ-collector*))
(defun occ-collector-remove (key)
(setq *occ-collector* (assoc-delete-all key *occ-collector*)))
(defun occ-collector-set (key value)
(setcdr (alist-get key *occ-collector*) value))
(defun occ-collector-spec (key)
(let ((collection (occ-collector-get key)))
(when collection
(occ-obj-collection-spec collection))))
(defun occ-collector-roots (key)
(let ((collection (occ-collector-get key)))
(when collection
(occ-obj-collection-roots collection))))
(defun occ-collector-files (key)
(let ((collection (occ-collector-get key)))
(when collection
(occ-obj-collection-files collection))))
(defun occ-collector-keys ()
(let* ((all-list (mapcar #'cl-first
*occ-collector*))
(default-list (list *occ-collector-default-key*))
(all-without-unnamed-list (remove *occ-collector-unnamed-key*
all-list))
(unnamed-list (when (memq *occ-collector-unnamed-key*
all-list)
(list *occ-collector-unnamed-key*))))
(cl-remove-duplicates (append default-list
all-without-unnamed-list
unnamed-list)
:from-end t)))
;; moved to occ-obj-accessor.el
;; (cl-defmethod occ-obj-collection ((obj symbol))
;; (let ((key obj))
;; (occ-collector-get key)))
;; (cl-defmethod occ-obj-collection ((obj occ-obj-collection))
;; obj)
(defun occ-collections (&rest keys)
(remove nil
(mapcar #'occ-obj-collection keys)))
(defun occ-collections-default ()
(occ-collections (occ-collector-default-key)
*occ-collector-unnamed-key*))
(defun occ-collections-all ()
(apply #'occ-collections
(occ-collector-keys)))
;; (defun occ-switch-buffer-run-curr-ctx-timer-function (prev next)
;; (occ-run-curr-ctx-timer))
;;;###autoload
(defun occ-add-after-save-hook-fun-in-org-mode ()
(add-hook 'after-save-hook
'occ-after-save-hook-fun t t))
(defun occ-reset-collection-spec (&optional key)
"Reset spec for collection associated with KEY."
(interactive (list (occ-collector-read-key "key for spec: ")))
(occ-debug "resetting deafult-tsk-collection")
(let ((key (or key
(occ-collector-default-key))))
(occ-reset-collection-object key)))
(defun occ-reset-collection-roots (key)
"Reset roots for collection associated with KEY."
(interactive (list (occ-collector-read-key "key for spec: ")))
(setf (occ-collection-roots (occ-collector-get key)) nil))
(defun occ-reset-collection-tsks (key)
"Reset tasks for collection associated with KEY."
(interactive (list (occ-collector-read-key "key for spec: ")))
(occ-do-reset-tsks (occ-collector-get key)))
(defun occ-collect-tsks (key)
"Populate tasks for collection associated with KEY."
(interactive (list (occ-collector-read-key "key for spec: ")))
(occ-obj-collect-tsks (occ-collector-get key)))
;;;###autoload
(defun occ-reset-collection-object (key)
(interactive (list (occ-collector-read-key "key for spec: ")))
(occ-reset-collection-tsks key))
;;;###autoload
(cl-defun occ-set-collection-spec (key desc spec files &key depth limit rank level)
(let ((depth (or depth 0))
(limit (or limit 0))
(rank (or rank 0))
(level (or level :optional)))
(occ-collector-get-create key
desc
spec
files
:depth depth
:limit limit
:rank rank
:level level)))
;;;###autoload
(cl-defun occ-set-deafult-collection-spec (spec files &key depth limit rank level)
(let ((depth (or depth 0))
(limit (or limit 0))
(rank (or rank 0))
(level (or level :optional)))
(occ-set-collection-spec (occ-collector-default-key)
"Default"
spec
files
:depth depth
:limit limit
:rank rank
:level level)))
;;;###autoload
(cl-defun occ-set-primary-deafult-collection-spec (spec files &key depth limit rank level)
(let ((depth (or depth 0))
(limit (or limit 0))
(rank (or rank 20))
(level (or level :primary)))
(occ-set-collection-spec (occ-collector-default-key)
"Default"
spec
files
:depth depth
:limit limit
:rank rank
:level level)))
(defun occ-reset-deafult-collection-object ()
(occ-debug "resetting deafult-tsk-collection")
(occ-reset-collection-object (occ-collector-default-key)))
(defun occ-do-priority-initialize ()
(interactive)
;; (occ-obj-properties-for-rank)
;; (current-clock key status timebeing root currfile)
(occ-do-add-ineq 'root "nil > (key + 20)")
(occ-do-add-ineq 'key "nil > 10")
(occ-do-add-ineq 'status "nil > 2 * root")
(occ-do-add-ineq 'currfile "nil > status")
(occ-do-add-ineq 'timebeing "nil > currfile")
(occ-do-add-ineq 'current-clock "nil > timebeing")
(occ-do-add-ineq 'git-branch "this > root")
(occ-do-set-prop-priorities))
(defun occ-initialize-hooks (key)
(let ((spec (occ-collector-spec key)))
(when (and spec
(occ-valid-spec-p spec))
;; (add-hook 'buffer-list-update-hook 'occ-run-curr-ctx-timer t)
;; (add-hook 'elscreen-screen-update-hook 'occ-run-curr-ctx-timer t)
;; (add-hook 'elscreen-goto-hook 'occ-run-curr-ctx-timer t)
;; (debug)
(unless (memq 'switch-buffer-functions-run post-command-hook)
(add-hook 'post-command-hook
'switch-buffer-functions-run))
(add-hook 'switch-buffer-functions #'occ-switch-buffer-run-curr-ctx-timer-function)
(add-hook 'org-mode-hook #'occ-add-after-save-hook-fun-in-org-mode)
(add-hook 'org-mode-hook #'occ-add-org-file-timer))))
(defun occ-uninitialize-hooks ()
(remove-hook 'switch-buffer-functions #'occ-switch-buffer-run-curr-ctx-timer-function)
(remove-hook 'org-mode-hook #'occ-add-after-save-hook-fun-in-org-mode)
(remove-hook 'org-mode-hook #'occ-add-org-file-timer))
;;;###autoload
(defun occ-initialize (key)
"occ-initialize"
(setq *occ-tsk-previous-ctx* (occ-obj-make-ctx-at-point))
(progn
(occ-filter-config-initialize)
(occ-helm-actions-config-initialize)
(occ-unnamed-initialize)
(occ-enable-mode-map)
(occ-register-resolve-clock)
(occ-cancel-timer)
(occ-reset-collection-object key)
(occ-ctx-clrhash)
(occ-initialize-hooks key))
(when nil
(dolist (prop (occ-obj-properties-for-ranking nil))
(let ((propstr (upcase (occ-obj-org-property-name prop))))
;; CHECK: is it required, as inheritance inow maintained by occ-rank.el by (occ-obj-rank-inheritable) functions
(unless (member propstr org-use-property-inheritance)
(cl-pushnew propstr org-use-property-inheritance)))))
(let ((spec (occ-collector-spec key)))
(occ-debug "init Test %s" key)
(unless spec
(if (occ-valid-spec-p spec)
(progn
(occ-collector-get-create key "Test" spec (list (read-file-name "org file for occ: "
"~/Documents"
"~/Documents/tasks.org"
t
nil
#'(lambda (f)
(string-match "*.org/" f)))))
(setq occ-mode t)
(occ-initialize-hooks key))
(if (called-interactively-p 'interactive) ;; (called-interactively-p 'interactive)
(progn
(occ-debug "init Test2")
(occ-obj-build-spec key)
(setq occ-mode t))
(occ-error "Not able to start occ")
nil))))
(occ-do-priority-initialize)
;; newly added
;; (org-clock-load) ;; is getting struck
(run-with-idle-timer 3
nil
#'org-clock-load))
;;;###autoload
(defun occ-uninitialize ()
"occ-uninitialize"
(progn
(occ-disable-mode-map)
(occ-unregister-resolve-clock)
(occ-cancel-timer)
(occ-reset-collection-object (occ-collector-default-key))
(occ-ctx-clrhash)
(occ-uninitialize-hooks))
(when nil
(dolist (prop (occ-obj-properties-for-ranking nil))
(let ((propstr (upcase (occ-obj-org-property-name prop))))
;; CHECK: is it required, as inheritance inow maintained by occ-rank.el by (occ-obj-rank-inheritable) functions
(unless (member propstr org-use-property-inheritance)
(delete propstr org-use-property-inheritance)))))
(setq occ-mode nil))
(defun occ-status ()
(interactive)
(occ-message "Occ mode is %s and switch-buffer-functions is %s"
(if occ-mode "on" "off")
(if (memq 'switch-buffer-functions-run
(default-value 'post-command-hook))
"working"
"not working")))
(defun occ-find-library-dir (library)
(unless occ-dev-dir
(occ-set-dev-dir))
(unless occ-dev-dir
(occ-error "occ-dev-dir is NIL"))
(progn
(cl-delete (expand-file-name occ-dev-dir library) load-path)
(let ((libpath (expand-file-name (concat library ".el")
occ-dev-dir)))
(if (file-exists-p libpath)
(prog1
occ-dev-dir
(cl-pushnew occ-dev-dir load-path))
(file-name-directory (or (locate-library library)
""))))))
(defun occ-get-version (here full message)
"Show the Occ version.
Interactively, or when MESSAGE is non-nil, show it in echo area.
With prefix argument, or when HERE is non-nil, insert it at point.
In non-interactive uses, a reduced version string is output unless
FULL is given."
(ignore message)
(let ((occ-dir (ignore-errors (occ-find-library-dir "occ")))
(save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
(load-suffixes (list ".el"))
(occ-install-dir (ignore-errors (occ-find-library-dir "occ-loaddefs"))))
(unless (and (fboundp 'occ-release)
(fboundp 'occ-git-version))
(org-load-noerror-mustsuffix (concat occ-dir "occ-version")))
(let* ((load-suffixes save-load-suffixes)
(release (occ-release))
(git-version (occ-git-version))
(version (format "Occ mode version %s (%s @ %s)"
release
git-version
(if occ-install-dir
(if (string= occ-dir occ-install-dir)
occ-install-dir
(concat "mixed installation! "
occ-install-dir
" and "
occ-dir))
"occ-loaddefs.el can not be found!")))
(version1 (if full version release)))
(when here (insert version1))
;; (when message (occ-debug "%s" version1))
version1)))
;;;###autoload
(defun occ-set-dev-dir (&optional dirpath)
(interactive
(list (read-directory-name "occ src dir: " nil nil t)))
(let ((dirpath (or dirpath
(read-directory-name "occ src dir: " nil nil t))))
(setq occ-dev-dir dirpath)))
;;;###autoload
(defun occ-add-deps-libs (pkg)
(let ((deps (cons (symbol-name pkg)
(mapcar #'(lambda (x)
(symbol-name (cl-first x)))
(package-desc-reqs (nth 1
(assoc 'occ
package-alist)))))))
(if occ-dev-dir
(dolist (lib deps)
(cl-delete (concat occ-dev-dir lib) load-path)
(cl-pushnew (concat occ-dev-dir lib) load-path))
(occ-error "occ-dev-dir not defined"))))
;;;###autoload
(defun occ-load-pkg (pkg-str)
;; TODO: load all files in lib dir
(let ((pkg-dir (occ-find-library-dir pkg-str)))
(dolist (ef (directory-files pkg-dir nil ".el$"))
(let ((efile (expand-file-name ef pkg-dir)))
(unless (string-match "pkg.el$" efile)
(occ-debug "trying to load %s %s %s"
pkg-str
ef
efile)
(or (and (occ-load-noerror-mustsuffix efile)
't)
pkg-str))))))
;;;###autoload
(defun occ-reload-lib (uncompiled)
"Reload all Occ Lisp files.
With prefix arg UNCOMPILED, load the uncompiled versions."
(require 'loadhist)
(let* ((pkg 'occ)
(occ-dir (occ-find-library-dir (symbol-name pkg)))
;; (contrib-dir (or (occ-find-library-dir "org-contribdir") occ-dir))
;; (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
(occ-deps (cons (symbol-name pkg)
(mapcar #'(lambda (x) (symbol-name (cl-first x)))
(package-desc-reqs (nth 1 (assoc 'occ
package-alist))))))
(occ-pkg-regexp (regexp-opt occ-deps))
(feature-re (concat "^" occ-pkg-regexp "$"))
(remove-re (format "\\`%s\\'"
(regexp-opt '("dash" "org" "org-loaddefs" "occ-version" "helm"))))
(feats (delete-dups (mapcar #'file-name-sans-extension
(mapcar 'file-name-nondirectory
(delq nil (mapcar 'feature-file features))))))
(lfeat (append (sort (setq feats (delq nil (mapcar #'(lambda (f)
(if (and (string-match feature-re f)
(not (string-match remove-re f)))
(progn
(occ-debug "%s matched." f)
f)
nil))
feats)))
'string-lessp)
(list "occ-version" "occ")))
(load-suffixes (when (boundp 'load-suffixes) load-suffixes))
(load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
(load-uncore nil)
(load-misses nil))
(ignore occ-dir)
(unless occ-dev-dir
(occ-set-dev-dir))
(unless occ-dev-dir
(occ-error "occ-dev-dir is NIL"))
(occ-add-deps-libs pkg)
(occ-debug "working on %s" lfeat)
(let ((load-missed-1 (mapcar #'occ-load-pkg
lfeat)))
(setq load-misses (delq 't load-missed-1)))
(occ-debug "starting")
(when load-uncore
(occ-debug "The following feature%s found in load-path, please check if that's correct:\n%s"
(if (> (length load-uncore) 1) "s were" " was")
load-uncore))
(if load-misses
(occ-debug "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
(if (> (length load-misses) 1) "s" "")
load-misses (occ-version nil 'full))
(occ-debug "Successfully reloaded Org\n%s" (occ-version nil 'full)))))
(when nil
(let* ((occ-pkg-regexp (regexp-opt (cons "occ" (mapcar #'(lambda (x) (symbol-name (cl-first x))) (package-desc-reqs (nth 1 (assoc 'occ package-alist)))))))
(feature-re (concat "^" occ-pkg-regexp "$")))
(string-match feature-re "xocc")))
;;; occ.el ends here