summaryrefslogtreecommitdiff
path: root/elchemy-functions.el
blob: 57663b2a958d85fec4cd91b292c36614a2e7fd20 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(defun elchemy/replace-in-buffer ()
  "Replace strings in buffer"
  (interactive)
  (save-excursion
    (if (equal mark-active nil) (mark-word))
    (setq elchemy/rib/current-word (buffer-substring-no-properties (mark) (point)))
    (setq elchemy/rib/old-string (read-string "OLD string: " elchemy/rib/current-word))
    (setq elchemy/rib/new-string (read-string "NEW string: " elchemy/rib/old-string))
    (query-replace elchemy/rib/old-string elchemy/rib/new-string nil (point-min) (point-max))))

(defun elchemy/add-to-pypath (&optional path)
  "* Add to python path"
  (interactive)
  (let* ((fullpath (if path
		       path
		     (ido-read-directory-name "Path: "))))
    (setenv "PYTHONPATH"
	    (let ((current (getenv "PYTHONPATH"))
		  (new fullpath))
	      (if current
		  (concat new ":" current)
		new)))))

(defun elchemy/personal/ignore-angle-brackets ()
  "Ignore angle brackets in org mode"
  (modify-syntax-entry ?< ".")
  (modify-syntax-entry ?> "."))

(defun elchemy/read-alist-file (file-path)
  "Read a file where each line is an alist of the form (name . location), returning a list of these pairs."
  (interactive "fEnter file path: ")
  (let (result)
    (with-temp-buffer
      (insert-file-contents file-path)
      (goto-char (point-min))
      (while (not (eobp))
        (let ((line (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
          (when (not (string-blank-p line))
            (let ((pair (car (read-from-string line))))
              (push pair result))))
        (forward-line 1)))
    (reverse result)))

(defun elchemy/find-file (FILENAME)
  "Find File to new split"
  (let ((buffer (find-file-noselect FILENAME)))
    (when (one-window-p)
      (split-window-right))
    (other-window 1)
    (switch-to-buffer buffer)))

(defun elchemy/term (TERM)
  "Terminal to new split"
  (when (one-window-p)
    (split-window-right))
  (other-window 1)
  (term TERM))

(defun elchemy/replace-nil (STRING &optional REPLACEMENT)
  "Replace a nil value with an empty string"
  (unless REPLACEMENT
    (setq REPLACEMENT " "))
  (if STRING
      STRING
    REPLACEMENT))

(defun elchemy/get-agenda-items (TODO LEVEL)
  "Get Agenda Items"
  (let* ((agenda-items-raw (org-ql-select
			     (org-agenda-files)
			     `(todo ,TODO)
			     :sort '(priority todo date)))
	 (agenda-items-plist (mapcar #'(lambda (x) (cl--plist-to-alist (car (cdr x)))) agenda-items-raw))
	 (agenda-items-filtered (seq-filter (lambda (x) (eq (cdr (assoc :level x)) LEVEL)) agenda-items-plist)))
    (mapcar #'(lambda (x) (cdr (assoc :raw-value x))) agenda-items-filtered)))

(defun elchemy/process-agenda-heading (heading)
  "Process agenda headings

Returns '((:raw-value . <Heading Text>) (:level <Org Heading Level>) (:priority . <Org Priority>) (:todo-keyword . <Org Todo Keyword>) (:scheduled . <Date Scheduled as Text>))"
  (let ((heading-alist (cl--plist-to-alist (car (cdr heading)))))
    `(,(assoc :raw-value heading-alist) ,(assoc :level heading-alist) ,(assoc :priority heading-alist) ,(assoc :todo-keyword heading-alist) (:scheduled . ,(cdr (assoc :raw-value (cl--plist-to-alist (nth 2 (assoc :scheduled heading-alist)))))))))

(defun elchemy/format-priority (priority)
  "Format a priority to the letter or '-' if no priority"
  (if priority
      priority
    45))

(defun elchemy/format-processed-agenda (agenda-list)
  "Format a processed agenda to a string:
[PRIORITY] TODO-KEYWORD: HEADING ~ SCHEDULED"
  (apply #'concat (mapcar #'(lambda (x)
			      (concat (format "[%c] " (elchemy/format-priority (cdr (assoc :priority x)))) (cdr (assoc :todo-keyword x)) ": " (cdr (assoc :raw-value x)) " ~ " (cdr (assoc :scheduled x)) "\n"))
			  agenda-list)))

(defun elchemy/display-tabular-button-alist (ALIST &optional columns padding)
  "Display a Button Alist as a Table"
  (unless columns
    (setq columns 1))
  (unless padding
    (setq padding 0))
  (let* ((colwidth (+ padding (apply 'max (mapcar #'(lambda (x) (length (car x))) ALIST)))))
    (dotimes (i (length ALIST))
      (let* ((elem (nth i ALIST))
	     (name (car elem))
	     (callback (cdr elem)))
	(insert (buttonize name callback))
	(message "%s\n" columns)
	(if (eq (% (+ i 1) columns) 0)
	    (insert "\n")
	  (insert (format (concat "%" (format "%ds" (- colwidth (length name)))) " ")))
	))
    (unless (eq (% (length ALIST) columns) 0)
      (insert "\n"))))

(provide 'elchemy-functions)