dotemacs

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

clojure-mode-expansions.el (3737B)


      1 ;;; clojure-mode-expansions.el --- Clojure-specific expansions for expand-region
      2 
      3 ;; Copyright (C) 2011-2020  Free Software Foundation, Inc
      4 
      5 ;; Author: Magnar Sveen <magnars@gmail.com>
      6 ;; Keywords: marking region
      7 
      8 ;; This program is free software; you can redistribute it and/or modify
      9 ;; it under the terms of the GNU General Public License as published by
     10 ;; the Free Software Foundation, either version 3 of the License, or
     11 ;; (at your option) any later version.
     12 
     13 ;; This program is distributed in the hope that it will be useful,
     14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 ;; GNU General Public License for more details.
     17 
     18 ;; You should have received a copy of the GNU General Public License
     19 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
     20 
     21 ;;; Commentary:
     22 
     23 ;; Extra expansions for clojure-mode:
     24 ;;
     25 ;; * `er/mark-clj-word` - includes dashes, but not slashes.
     26 ;; * `er/mark-clj-regexp-literal`
     27 ;; * `er/mark-clj-function-literal`
     28 ;;
     29 ;; Feel free to contribute any other expansions for Clojure at
     30 ;;
     31 ;;     https://github.com/magnars/expand-region.el
     32 
     33 ;;; Code:
     34 
     35 (require 'expand-region-core)
     36 (require 'er-basic-expansions)
     37 
     38 (defun er/mark-clj-word ()
     39   "Mark the entire word around or in front of point, including dashes."
     40   (interactive)
     41   (let ((word-regexp "\\(\\sw\\|-\\)"))
     42     (when (or (looking-at word-regexp)
     43               (er/looking-back-on-line word-regexp))
     44       (while (looking-at word-regexp)
     45         (forward-char))
     46       (set-mark (point))
     47       (while (er/looking-back-on-line word-regexp)
     48         (backward-char)))))
     49 
     50 (defun er/mark-clj-set-literal ()
     51   "Mark clj-set-literal presumes that point is outside the brackets.
     52 If point is inside the brackets, those will be marked first anyway."
     53   (interactive)
     54   (when (or (looking-at "#{")
     55             (er/looking-back-exact "#"))
     56     (forward-char 1)
     57     (search-backward "#")
     58     (set-mark (point))
     59     (search-forward "{")
     60     (forward-char -1)
     61     (forward-list 1)
     62     (exchange-point-and-mark)))
     63 
     64 (defun er/mark-clj-regexp-literal ()
     65   "Mark clj-regexp-literal presumes that point is outside the string.
     66 If point is inside the string, the quotes will be marked first anyway."
     67   (interactive)
     68   (when (or (looking-at "#\"")
     69             (er/looking-back-exact "#"))
     70     (forward-char 1)
     71     (search-backward "#")
     72     (set-mark (point))
     73     (search-forward "\"")
     74     (forward-char 1)
     75     (er--move-point-forward-out-of-string)
     76     (exchange-point-and-mark)))
     77 
     78 (defun er/mark-clj-function-literal ()
     79   "Mark clj-function-literal presumes that point is outside the parens.
     80 If point is inside the parens, they will be marked first anyway."
     81   (interactive)
     82   (when (or (looking-at "#(")
     83             (er/looking-back-exact "#"))
     84     (forward-char)
     85     (search-backward "#")
     86     (set-mark (point))
     87     (search-forward "(")
     88     (backward-char)
     89     (forward-list)
     90     (exchange-point-and-mark)))
     91 
     92 (defun er/add-clojure-mode-expansions ()
     93   "Adds clojure-specific expansions for buffers in clojure-mode"
     94   (set (make-local-variable 'er/try-expand-list) (append
     95                                                   er/try-expand-list
     96                                                   '(er/mark-clj-word
     97                                                     er/mark-clj-regexp-literal
     98                                                     er/mark-clj-set-literal
     99                                                     er/mark-clj-function-literal))))
    100 
    101 (er/enable-mode-expansions 'clojure-mode 'er/add-clojure-mode-expansions)
    102 (er/enable-mode-expansions 'nrepl-mode 'er/add-clojure-mode-expansions)
    103 
    104 (provide 'clojure-mode-expansions)
    105 
    106 ;; clojure-mode-expansions.el ends here