-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathhy-base.el
107 lines (79 loc) · 3 KB
/
hy-base.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
;;; hy-base.el --- Common Utils -*- lexical-binding: t -*-
;; Copyright © 2013 Julien Danjou <[email protected]>
;; © 2017-2019 Eric Kaschalk <[email protected]>
;;
;; hy-mode is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; hy-mode is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with hy-mode. If not, see <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Common requires and utilities for `hy-mode'.
;; Mostly just methods for syntax states and sexp traversals.
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 's)
;;; Syntax Methods
;;;; `syntax-ppss' and `parse-partial-sexp' aliases
;;;;; Positions
(defun hy--syntax->inner-char (syntax)
"Get innermost char of SYNTAX."
(nth 1 syntax))
(defun hy--syntax->last-sexp-start (state)
"Return start of last sexp of syntax STATE."
(nth 2 state))
(defun hy--syntax->string-start (syntax)
"Return start of STATE that is in a string."
(nth 8 syntax))
(defun hy--syntax->inner-symbol (syntax)
"Get innermost sexp of SYNTAX."
(save-excursion
(when (hy--goto-inner-sexp syntax)
(thing-at-point 'symbol))))
;;;;; Predicates
(defun hy--in-string? (state)
"Is syntax STATE in a string?"
(nth 3 state))
(defun hy--in-string-or-comment? (state)
"Is syntax STATE in a string or comment?"
(or (nth 3 state) (nth 4 state)))
(defun hy--prior-sexp? (state)
"Is there a prior sexp from syntax STATE?"
(number-or-marker-p (hy--syntax->last-sexp-start state)))
;;;; Gotos
(defun hy--goto-inner-char (syntax)
"Goto innermost char of SYNTAX."
(-some-> syntax hy--syntax->inner-char goto-char))
(defun hy--goto-inner-sexp (syntax)
"Goto innermost sexp of SYNTAX."
(-some-> syntax hy--syntax->inner-char 1+ goto-char))
(defun hy--goto-last-sexp-start (syntax)
"Goto start of last sexp of SYNTAX."
(-some-> syntax hy--syntax->last-sexp-start goto-char))
;;; Form Captures
(defun hy--current-form-string ()
"Get form containing current point as string plus a trailing newline."
(save-excursion
(-when-let (start (hy--goto-inner-char (syntax-ppss)))
(while (ignore-errors (forward-sexp)))
(s-concat (buffer-substring-no-properties start (point))
"\n"))))
(defun hy--last-sexp-string ()
"Get form containing last s-exp point as string plus a trailing newline."
(save-excursion
(-when-let (start (hy--goto-last-sexp-start (syntax-ppss)))
(while (ignore-errors (forward-sexp)))
(s-concat (buffer-substring-no-properties start (point))
"\n"))))
;;; Provide:
(provide 'hy-base)
;;; hy-base.el ends here