dotemacs

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

expand-region.el (8425B)


      1 ;;; expand-region.el --- Increase selected region by semantic units.
      2 
      3 ;; Copyright (C) 2011-2020  Free Software Foundation, Inc
      4 
      5 ;; Author: Magnar Sveen <magnars@gmail.com>
      6 ;; Keywords: marking region
      7 ;; URL: https://github.com/magnars/expand-region.el
      8 ;; Version: 0.11.0
      9 
     10 ;; This program is free software; you can redistribute it and/or modify
     11 ;; it under the terms of the GNU General Public License as published by
     12 ;; the Free Software Foundation, either version 3 of the License, or
     13 ;; (at your option) any later version.
     14 
     15 ;; This program is distributed in the hope that it will be useful,
     16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 ;; GNU General Public License for more details.
     19 
     20 ;; You should have received a copy of the GNU General Public License
     21 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
     22 
     23 ;;; Commentary:
     24 
     25 ;; Expand region increases the selected region by semantic units. Just keep
     26 ;; pressing the key until it selects what you want.
     27 
     28 ;; An example:
     29 
     30 ;;     (setq alphabet-start "abc def")
     31 
     32 ;; With the cursor at the `c`, it starts by marking the entire word `abc`, then
     33 ;; expand to the contents of the quotes `abc def`, then to the entire quote
     34 ;; `"abc def"`, then to the contents of the sexp `setq alphabet-start "abc def"`
     35 ;; and finally to the entire sexp.
     36 
     37 ;; You can set it up like this:
     38 
     39 ;;     (require 'expand-region)
     40 ;;     (global-set-key (kbd "C-=") 'er/expand-region)
     41 
     42 ;; There's also `er/contract-region` if you expand too far.
     43 
     44 ;; ## Video
     45 
     46 ;; You can [watch an intro to expand-region at Emacs Rocks](http://emacsrocks.com/e09.html).
     47 
     48 ;; ## Language support
     49 
     50 ;; Expand region works fairly well with most languages, due to the general
     51 ;; nature of the basic expansions:
     52 
     53 ;;     er/mark-word
     54 ;;     er/mark-symbol
     55 ;;     er/mark-method-call
     56 ;;     er/mark-inside-quotes
     57 ;;     er/mark-outside-quotes
     58 ;;     er/mark-inside-pairs
     59 ;;     er/mark-outside-pairs
     60 
     61 ;; However, most languages also will benefit from some specially crafted
     62 ;; expansions. For instance, expand-region comes with these extra expansions for
     63 ;; html-mode:
     64 
     65 ;;     er/mark-html-attribute
     66 ;;     er/mark-inner-tag
     67 ;;     er/mark-outer-tag
     68 
     69 ;; You can add your own expansions to the languages of your choice simply by
     70 ;; creating a function that looks around point to see if it's inside or looking
     71 ;; at the construct you want to mark, and if so - mark it.
     72 
     73 ;; There's plenty of examples to look at in these files.
     74 
     75 ;; After you make your function, add it to a buffer-local version of
     76 ;; the `er/try-expand-list`.
     77 
     78 ;; **Example:**
     79 
     80 ;; Let's say you want expand-region to also mark paragraphs and pages in
     81 ;; text-mode. Incidentally Emacs already comes with `mark-paragraph` and
     82 ;; `mark-page`. To add it to the try-list, do this:
     83 
     84 ;;     (defun er/add-text-mode-expansions ()
     85 ;;       (make-variable-buffer-local 'er/try-expand-list)
     86 ;;       (setq er/try-expand-list (append
     87 ;;                                 er/try-expand-list
     88 ;;                                 '(mark-paragraph
     89 ;;                                   mark-page))))
     90 
     91 ;;     (er/enable-mode-expansions 'text-mode 'er/add-text-mode-expansions)
     92 
     93 ;; Add that to its own file, and require it at the bottom of this one,
     94 ;; where it says "Mode-specific expansions"
     95 
     96 ;; **Warning:** Badly written expansions might slow down expand-region
     97 ;; dramatically. Remember to exit quickly before you start traversing
     98 ;; the entire document looking for constructs to mark.
     99 
    100 ;; ## Contribute
    101 
    102 ;; If you make some nice expansions for your favorite mode, it would be
    103 ;; great if you opened a pull-request. The repo is at:
    104 
    105 ;;     https://github.com/magnars/expand-region.el
    106 
    107 ;; Changes to `expand-region-core` itself must be accompanied by feature tests.
    108 ;; They are written in [Ecukes](http://ecukes.info), a Cucumber for Emacs.
    109 
    110 ;; To fetch the test dependencies:
    111 
    112 ;;     $ cd /path/to/expand-region
    113 ;;     $ git submodule init
    114 ;;     $ git submodule update
    115 
    116 ;; Run the tests with:
    117 
    118 ;;     $ ./util/ecukes/ecukes features
    119 
    120 ;; If you want to add feature-tests for your mode-specific expansions as well,
    121 ;; that is utterly excellent.
    122 
    123 ;; ## Contributors
    124 
    125 ;; * [Josh Johnston](https://github.com/joshwnj) contributed `er/contract-region`
    126 ;; * [Le Wang](https://github.com/lewang) contributed consistent handling of the mark ring, expanding into pairs/quotes just left of the cursor, and general code clean-up.
    127 ;; * [Matt Briggs](https://github.com/mbriggs) contributed expansions for ruby-mode.
    128 ;; * [Ivan Andrus](https://github.com/gvol) contributed expansions for python-mode, text-mode, LaTeX-mode and nxml-mode.
    129 ;; * [Raimon Grau](https://github.com/kidd) added support for when transient-mark-mode is off.
    130 ;; * [Gleb Peregud](https://github.com/gleber) contributed expansions for erlang-mode.
    131 ;; * [fgeller](https://github.com/fgeller) and [edmccard](https://github.com/edmccard) contributed better support for python and its multiple modes.
    132 ;; * [François Févotte](https://github.com/ffevotte) contributed expansions for C and C++.
    133 ;; * [Roland Walker](https://github.com/rolandwalker) added option to copy the contents of the most recent action to a register, and some fixes.
    134 ;; * [Damien Cassou](https://github.com/DamienCassou) added option to continue expanding/contracting with fast keys after initial expand.
    135 
    136 ;; Thanks!
    137 
    138 ;;; Code:
    139 
    140 (require 'expand-region-core)
    141 (require 'expand-region-custom)
    142 (require 'er-basic-expansions)
    143 
    144 ;;;###autoload
    145 (defun er/expand-region (arg)
    146   "Increase selected region by semantic units.
    147 
    148 With prefix argument expands the region that many times.
    149 If prefix argument is negative calls `er/contract-region'.
    150 If prefix argument is 0 it resets point and mark to their state
    151 before calling `er/expand-region' for the first time."
    152   (interactive "p")
    153   (if (< arg 1)
    154       (er/contract-region (- arg))
    155     (er--prepare-expanding)
    156     (while (>= arg 1)
    157       (setq arg (- arg 1))
    158       (when (eq 'early-exit (er--expand-region-1))
    159         (setq arg 0)))
    160     (when (and expand-region-fast-keys-enabled
    161                (not (memq last-command '(er/expand-region er/contract-region))))
    162       (er/prepare-for-more-expansions))))
    163 
    164 (eval-after-load 'clojure-mode   '(require 'clojure-mode-expansions))
    165 (eval-after-load 'css-mode       '(require 'css-mode-expansions))
    166 (eval-after-load 'erlang-mode    '(require 'erlang-mode-expansions))
    167 (eval-after-load 'feature-mode   '(require 'feature-mode-expansions))
    168 (eval-after-load 'sgml-mode      '(require 'html-mode-expansions)) ;; html-mode is defined in sgml-mode.el
    169 (eval-after-load 'rhtml-mode     '(require 'html-mode-expansions))
    170 (eval-after-load 'nxhtml-mode    '(require 'html-mode-expansions))
    171 (eval-after-load 'web-mode       '(require 'web-mode-expansions))
    172 (eval-after-load 'js             '(require 'js-mode-expansions))
    173 (eval-after-load 'js2-mode       '(require 'js-mode-expansions))
    174 (eval-after-load 'js2-mode       '(require 'js2-mode-expansions))
    175 (eval-after-load 'js3-mode       '(require 'js-mode-expansions))
    176 (eval-after-load 'latex          '(require 'latex-mode-expansions))
    177 (eval-after-load 'nxml-mode      '(require 'nxml-mode-expansions))
    178 (eval-after-load 'octave-mod     '(require 'octave-expansions))
    179 (eval-after-load 'octave         '(require 'octave-expansions))
    180 (eval-after-load 'python         '(progn
    181                                     (when expand-region-guess-python-mode
    182                                       (expand-region-guess-python-mode))
    183                                     (if (eq 'python expand-region-preferred-python-mode)
    184                                         (require 'python-el-expansions)
    185                                       (require 'python-el-fgallina-expansions))))
    186 (eval-after-load 'python-mode    '(require 'python-mode-expansions))
    187 (eval-after-load 'ruby-mode      '(require 'ruby-mode-expansions))
    188 (eval-after-load 'org            '(require 'the-org-mode-expansions))
    189 (eval-after-load 'cc-mode        '(require 'cc-mode-expansions))
    190 (eval-after-load "text-mode"      '(require 'text-mode-expansions))
    191 (eval-after-load 'cperl-mode     '(require 'cperl-mode-expansions))
    192 (eval-after-load 'sml-mode       '(require 'sml-mode-expansions))
    193 (eval-after-load 'enh-ruby-mode  '(require 'enh-ruby-mode-expansions))
    194 (eval-after-load 'subword        '(require 'subword-mode-expansions))
    195 
    196 (provide 'expand-region)
    197 
    198 ;;; expand-region.el ends here