dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

the-org-mode-expansions.el (3679B)


      1 ;;; the-org-mode-expansions.el --- Expansions for expand-region to be used in org-mode  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2012-2023  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 'er-basic-expansions)
     36 (require 'org-macs)
     37 (require 'org-element)
     38 
     39 (declare-function org-up-element "org")
     40 (declare-function org-mark-subtree "org")
     41 
     42 (defun er/mark-org-element ()
     43   (interactive)
     44   (let* ((el (org-element-at-point))
     45          (begin (plist-get (cadr el) :begin))
     46          (end (plist-get (cadr el) :end)))
     47     (goto-char begin)
     48     (set-mark (point))
     49     (goto-char end)
     50     (exchange-point-and-mark)))
     51 
     52 (defun er/mark-org-element-parent ()
     53   (interactive)
     54   (let* ((el (plist-get (cadr (org-element-at-point)) :parent))
     55          (begin (plist-get (cadr el) :begin))
     56          (end (plist-get (cadr el) :end)))
     57     (when (and begin end)
     58       (goto-char begin)
     59       (set-mark (point))
     60       (goto-char end)
     61       (exchange-point-and-mark))))
     62 
     63 (defun er/mark-sentence ()
     64   "Marks one sentence."
     65   (interactive)
     66   (forward-char 1)
     67   (backward-sentence 1)
     68   (set-mark (point))
     69   (forward-sentence 1)
     70   (exchange-point-and-mark))
     71 
     72 (defun er/mark-paragraph ()
     73   "Marks one paragraph."
     74   (interactive)
     75   (mark-paragraph)
     76   (exchange-point-and-mark)
     77   (skip-chars-backward er--space-str)
     78   (exchange-point-and-mark)
     79   (skip-chars-forward er--space-str))
     80 
     81 (defun er/mark-org-code-block ()
     82   "Marks an org-code-block."
     83   (interactive)
     84   (let ((case-fold-search t)
     85         (re "#\\+begin_\\(\\sw+\\)"))
     86     (unless (looking-at re)
     87       (search-backward-regexp re))
     88     (set-mark (point))
     89     (search-forward (concat "#+end_" (match-string 1)))
     90     (exchange-point-and-mark)))
     91 
     92 (defun er/mark-org-parent ()
     93   "Marks a heading 1 level up from current subheading"
     94   (interactive)
     95   (org-up-element)
     96   (org-mark-subtree))
     97 
     98 (defun er/save-org-mode-excursion (action)
     99   "Save outline visibility while expanding in org-mode"
    100   (org-save-outline-visibility t
    101     (funcall action)))
    102 
    103 (defun er/add-org-mode-expansions ()
    104   "Adds org-specific expansions for buffers in org-mode"
    105   (set (make-local-variable 'er/try-expand-list)
    106        (append
    107         (remove #'er/mark-defun er/try-expand-list)
    108         '(org-mark-subtree
    109           er/mark-org-element
    110           er/mark-org-element-parent
    111           er/mark-org-code-block
    112           er/mark-sentence
    113           er/mark-org-parent
    114           er/mark-paragraph)))
    115   (set (make-local-variable 'er/save-mode-excursion)
    116        #'er/save-org-mode-excursion))
    117 
    118 (er/enable-mode-expansions 'org-mode #'er/add-org-mode-expansions)
    119 
    120 (provide 'the-org-mode-expansions)