the-org-mode-expansions.el (3595B)
1 ;;; the-org-mode-expansions.el --- Expansions for expand-region to be used in org-mode 2 3 ;; Copyright (C) 2012-2020 Free Software Foundation, Inc 4 5 ;; Author: Magnar Sveen 6 ;; Based on text-mode-expansions by: Ivan Andrus 7 ;; Keywords: marking region 8 9 ;; This program is free software; you can redistribute it and/or modify 10 ;; it under the terms of the GNU General Public License as published by 11 ;; the Free Software Foundation, either version 3 of the License, or 12 ;; (at your option) any later version. 13 14 ;; This program is distributed in the hope that it will be useful, 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 ;; GNU General Public License for more details. 18 19 ;; You should have received a copy of the GNU General Public License 20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. 21 22 ;;; Commentary: 23 24 ;; The file needs to be weirdly name (prefixed with the-) to avoid 25 ;; conflict with org-reload, which bases its functionality on the names 26 ;; of files, for some reason. 27 ;; 28 ;; Feel free to contribute any other expansions for org-mode at 29 ;; 30 ;; https://github.com/magnars/expand-region.el 31 32 ;;; Code: 33 34 (require 'expand-region-core) 35 (require 'org-macs) 36 37 (declare-function org-up-element "org") 38 (declare-function org-mark-subtree "org") 39 40 (defun er/mark-org-element () 41 (interactive) 42 (let* ((el (org-element-at-point)) 43 (begin (plist-get (cadr el) :begin)) 44 (end (plist-get (cadr el) :end))) 45 (goto-char begin) 46 (set-mark (point)) 47 (goto-char end) 48 (exchange-point-and-mark))) 49 50 (defun er/mark-org-element-parent () 51 (interactive) 52 (let* ((el (plist-get (cadr (org-element-at-point)) :parent)) 53 (begin (plist-get (cadr el) :begin)) 54 (end (plist-get (cadr el) :end))) 55 (when (and begin end) 56 (goto-char begin) 57 (set-mark (point)) 58 (goto-char end) 59 (exchange-point-and-mark)))) 60 61 (defun er/mark-sentence () 62 "Marks one sentence." 63 (interactive) 64 (forward-char 1) 65 (backward-sentence 1) 66 (set-mark (point)) 67 (forward-sentence 1) 68 (exchange-point-and-mark)) 69 70 (defun er/mark-paragraph () 71 "Marks one paragraph." 72 (interactive) 73 (mark-paragraph) 74 (exchange-point-and-mark) 75 (skip-chars-backward er--space-str) 76 (exchange-point-and-mark) 77 (skip-chars-forward er--space-str)) 78 79 (defun er/mark-org-code-block () 80 "Marks an org-code-block." 81 (interactive) 82 (let ((case-fold-search t) 83 (re "#\\+begin_\\(\\sw+\\)")) 84 (unless (looking-at re) 85 (search-backward-regexp re)) 86 (set-mark (point)) 87 (search-forward (concat "#+end_" (match-string 1))) 88 (exchange-point-and-mark))) 89 90 (defun er/mark-org-parent () 91 "Marks a heading 1 level up from current subheading" 92 (interactive) 93 (org-up-element) 94 (org-mark-subtree)) 95 96 (defun er/save-org-mode-excursion (action) 97 "Save outline visibility while expanding in org-mode" 98 (org-save-outline-visibility t 99 (funcall action))) 100 101 (defun er/add-org-mode-expansions () 102 "Adds org-specific expansions for buffers in org-mode" 103 (set (make-local-variable 'er/try-expand-list) 104 (append 105 (remove #'er/mark-defun er/try-expand-list) 106 '(org-mark-subtree 107 er/mark-org-element 108 er/mark-org-element-parent 109 er/mark-org-code-block 110 er/mark-sentence 111 er/mark-org-parent 112 er/mark-paragraph))) 113 (set (make-local-variable 'er/save-mode-excursion) 114 #'er/save-org-mode-excursion)) 115 116 (er/enable-mode-expansions 'org-mode 'er/add-org-mode-expansions) 117 118 (provide 'the-org-mode-expansions)