# fafo.el -rw-r--r-- 2.2 KiB View raw
                                                                                
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
  ;;             .ooSSSSSSooo.
  ;;         .uSXXXXXXXXXXXXXXXSu.
  ;;       oXXXXXXXXXXXXXXXXXXXXXXXo
  ;;     sXXXXXXXXXXXXXXXXXXXXXXXXXXXs
  ;;    sXXXXXXXXXXXXXXXXXXXXXXXXXXXXXs
  ;;   oXXXXXXXXXXXXXX`  `XXXXXXXXXXXXXo
  ;;  oXXXXXXXXXXXXX        XXXXXXXXXXXXo
  ;;  XXXXXXXXXXXXX          XXXXXXXXXXXX
  ;; oXXXXXXXXXXXXX          XXXXXXXXXXXXo
  ;; XXXXXXXXXXXXXX          XXXXXXXXXXXXX
  ;; XXXXXXXXXXXXXXX        XXXXXXXXXXXXXX
  ;; oXXXXXXXXXXXXXXX..  ..XXXXXXXXXXXXXXo
  ;;  XXXXXXXXXXXXXXSSXXXXSSXXXXXXXXXXXXX
  ;;  oXXXXXXXXXXXXXSSXXXXSSXXXXXXXXXXXXo
  ;;   oXXXXXXXXXXXXSSXXXXSSXXXXXXXXXXXo
  ;;    sXXXXXXXXXXXSSXXXXSSXXXXXXXXXXs
  ;;     sXXXXXXXXXXSSXXXXSSXXXXXXXXXs
  ;;       oXXXXXXXXSSXXXXSSXXXXXXXo
  ;;         "uSXXXXSSXXXXSSXXXXSu"
  ;;             ""SSSSSSSSS""

(require 'org)

(defun orb (node)
  (list node
        (org-element-begin node)
        (org-element-end node)))

(cl-defgeneric clean-content (orb type))

(cl-defmethod clean-content :after (orb type)
  (cl-rest orb))

(cl-defmethod clean-content (orb type)
  (declare (ignore type))
  (cl-destructuring-bind (_ beg end) orb
    (string-clean-whitespace
     (buffer-substring-no-properties beg end))))

(cl-defmethod clean-content (orb (type (eql 'plain-list)))
  (declare (ignore type))
  (cl-destructuring-bind (_ beg end) orb
    (string-clean-whitespace
     (buffer-substring-no-properties (+ beg 2) end))))

(cl-defmethod clean-content (orb (type (eql 'src-block)))
  (declare (ignore type))
  (cl-destructuring-bind (_ beg end) orb
    (buffer-substring-no-properties
     (save-excursion
       (goto-char beg)
       (forward-line)
       (point))
     (save-excursion
       (goto-char end)
       (re-search-backward "#\\+end_src" 79 t)
       (backward-char)
       (point)))))

(defun find-around-for-org ()
  (with-current-buffer (current-buffer)
    (let ((node (org-element-at-point)))
      (clean-content (orb node) (car node)))))

(defun fafo ()
  (interactive)
  (if (region-active-p)
      (call-interactively #'kill-ring-save)
    (let ((text (find-around-for-org)))
      (with-temp-buffer
        (insert text)
        (kill-region (point-min) (point-max)))
      (message "✔ %d" (length text)))))

(define-key org-mode-map (kbd "M-w") #'fafo)