Skip to content

Commit 43c7f09

Browse files
committed
Merge pull request #302 from Malabarba/master
Implement commands for navigating over logical sexps
2 parents fda8788 + 571cb10 commit 43c7f09

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master (unreleased)
44

5+
* [#302](https://github.com/clojure-emacs/clojure-mode/pull/302) Add new sexp navigation commands. `clojure-forward-logical-sexp` and `clojure-backward-logical-sexp` consider `^hints` and `#reader.macros` to be part of the sexp that follows them.
6+
57
## 4.1.0 (20/06/2015)
68

79
### Changes

clojure-mode.el

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,45 @@ Returns a list pair, e.g. (\"defn\" \"abc\") or (\"deftest\" \"some-test\")."
10441044
(list (match-string 1)
10451045
(match-string 2))))))
10461046

1047+
1048+
;;; Sexp navigation
1049+
(defun clojure-forward-logical-sexp (&optional n)
1050+
"Move forward N logical sexps.
1051+
This will skip over sexps that don't represent objects, so that ^hints and
1052+
#reader.macros are considered part of the following sexp."
1053+
(interactive "p")
1054+
(if (< n 0)
1055+
(clojure-backward-logical-sexp (- n))
1056+
(while (> n 0)
1057+
;; Non-logical sexps.
1058+
(while (progn (forward-sexp 1)
1059+
(forward-sexp -1)
1060+
(looking-at-p "\\^\\|#[[:alpha:]]"))
1061+
(forward-sexp 1))
1062+
;; The actual sexp
1063+
(forward-sexp 1)
1064+
(setq n (1- n)))))
1065+
1066+
(defun clojure-backward-logical-sexp (&optional n)
1067+
"Move backward N logical sexps.
1068+
This will skip over sexps that don't represent objects, so that ^hints and
1069+
#reader.macros are considered part of the following sexp."
1070+
(interactive "p")
1071+
(if (< n 0)
1072+
(clojure-forward-logical-sexp (- n))
1073+
(while (> n 0)
1074+
;; The actual sexp
1075+
(backward-sexp 1)
1076+
;; Non-logical sexps.
1077+
(while (and (not (bobp))
1078+
(ignore-errors
1079+
(save-excursion
1080+
(backward-sexp 1)
1081+
(looking-at-p "\\^\\|#[[:alpha:]]"))))
1082+
(backward-sexp 1))
1083+
(setq n (1- n)))))
1084+
1085+
10471086
;;;###autoload
10481087
(progn
10491088
(add-to-list 'auto-mode-alist

test/clojure-mode-sexp-test.el

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
;;; clojure-mode-sexp-test.el --- Clojure Mode: sexp tests -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2015 Artur Malabarba <artur@endlessparentheses.com>
4+
5+
;; This file is not part of GNU Emacs.
6+
7+
;; This program is free software; you can redistribute it and/or modify
8+
;; it under the terms of the GNU General Public License as published by
9+
;; the Free Software Foundation, either version 3 of the License, or
10+
;; (at your option) any later version.
11+
12+
;; This program is distributed in the hope that it will be useful,
13+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
;; GNU General Public License for more details.
16+
17+
;; You should have received a copy of the GNU General Public License
18+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
20+
;;; Code:
21+
22+
(require 'clojure-mode)
23+
(require 'ert)
24+
25+
(ert-deftest test-sexp ()
26+
(with-temp-buffer
27+
(insert "^String #macro ^dynamic reverse")
28+
(clojure-mode)
29+
(clojure-backward-logical-sexp 1)
30+
(should (looking-at-p "\\^String \\#macro \\^dynamic reverse"))
31+
(clojure-forward-logical-sexp 1)
32+
(should (looking-back "\\^String \\#macro \\^dynamic reverse"))
33+
(insert " ^String biverse inverse")
34+
(clojure-backward-logical-sexp 1)
35+
(should (looking-at-p "inverse"))
36+
(clojure-backward-logical-sexp 2)
37+
(should (looking-at-p "\\^String \\#macro \\^dynamic reverse"))
38+
(clojure-forward-logical-sexp 2)
39+
(should (looking-back "\\^String biverse"))
40+
(clojure-backward-logical-sexp 1)
41+
(should (looking-at-p "\\^String biverse"))))
42+
43+
(provide 'clojure-mode-sexp-test)
44+
45+
;;; clojure-mode-sexp-test.el ends here

0 commit comments

Comments
 (0)