about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/evil-commentary-20170413.1451/evil-commentary-integration.el
blob: 0d2760e73077d807d6eba4aa9d9182c3bc197689 (plain) (blame)
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
(declare-function org-in-src-block-p "org")

(defmacro evil-commentary/org-babel-do-in-edit-buffer (beg end &rest body)
  "Do `org-babel-do-in-edit-buffer' and restore view.

Return the same value as `org-babel-do-in-edit-buffer'. Save top
line of current window and restore it if sucess."
  (declare (indent defun))
  `(when (and (fboundp 'org-babel-do-in-edit-buffer)
              (org-in-src-block-p t))
     (let ((top-line (line-number-at-pos (window-start)))
           (current-point (point))
           found)
       (push-mark ,beg)
       (goto-char ,end)
       (setq mark-active t)
       (setq found (eval '(org-babel-do-in-edit-buffer
                             ,@body)))
       (pop-mark)
       (if (not found)
           (goto-char current-point)
         (save-excursion
           (scroll-up 1)              ; stupid fix
           (goto-char (point-min))
           (forward-line (1- top-line))
           (recenter 0)))
       found)))

;;;###autoload
(defun evil-commentary/org-comment-or-uncomment-region (beg end)
  "Comment function for `org-mode'."
  (interactive "r")
  (unless (evil-commentary/org-babel-do-in-edit-buffer beg end
            (call-interactively 'evil-commentary))
    (comment-or-uncomment-region beg end)))

(provide 'evil-commentary-integration)