Fuco1 / .emacs.d

My emacs config
MIT License
69 stars 13 forks source link

Implement and package "org radio checkbox" #41

Closed Fuco1 closed 6 years ago

Fuco1 commented 6 years ago

References

implementation

(defun check-hook-fn ()
  (let ((parent (org-element-context)))
    (while (and parent
                (not (eq 'plain-list (org-element-type parent))))
      (setq parent (org-element-property :parent parent)))
    (when (-contains? (org-element-property :attr_org parent) ":radio")
      (save-excursion
        (--each (org-element-property :structure parent)
          (goto-char (car it))
          (when (re-search-forward "\\[X\\]" (line-end-position) t)
            (replace-match "[ ]"))))
      (save-excursion
        (beginning-of-line)
        (when (re-search-forward "\\[ \\]" (line-end-position) t)
          (replace-match "[X]"))))))

Also add the reference methods to get the picked value, will probably need a bit of work.

Usage

#+attr_org: :radio
#+name: test
- [ ] aasd
- [ ] Lorem ipsum dolor sit amet, consectetuer adipiscing
- [X] c
- [ ] dd
Fuco1 commented 6 years ago

Modified get value to discard the description :: (maybe make it optional)

(defun org-radio-list-get-value (list-name)
  "Return the value of the checked item in a radio list."
  (save-excursion
    (loop for el in (org-element-property
                     :structure
                      (org-get-plain-list list-name))
          if (string= (nth 4 el) "[X]")
          return (progn
                   (let ((item (buffer-substring (car el) (car (last el)))))
                     (string-match "\\[X\\]\\(.* ::\\)?\\(.*\\)$" item)
                     (match-string 2 item))))))

depends on

(defun get-radio-list-value (list-name)
  "Return the value of the checked item in a radio list."
  (save-excursion
    (loop for el in (org-element-property
                     :structure
                      (org-get-plain-list list-name))
          if (string= (nth 4 el) "[X]")
          return (progn
                   (let ((item (buffer-substring (car el) (car (last el)))))
                     (string-match "\\[X\\]\\(.* ::\\)?\\(.*\\)$" item)
                     (match-string 2 item))))))

Can be probably impemented better using org primitives.

Fuco1 commented 6 years ago

Practical use case

#+attr_org: :radio
#+name: mongo_port
- [ ] localhost :: 27018
- [X] staging b2c :: 27004
- [ ] staging mm :: 27044

#+NAME: query
#+BEGIN_SRC mongo :db db :host 127.0.0.1 :port (org-radio-list-get-value "mongo_port") :exports code
db.getCollection('request-logs').find({
    name: 'GetMp',
}).sort({_id: -1}).limit(1)
#+END_SRC
Fuco1 commented 6 years ago

https://github.com/Fuco1/org-radiobutton