-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeincrement.el
More file actions
67 lines (60 loc) · 2.87 KB
/
deincrement.el
File metadata and controls
67 lines (60 loc) · 2.87 KB
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
;; Common subroutines used for increment and decrement.
(defun bound-of-integer (str bounds)
(let* ((left-bound (string-match "[-+]?[0-9]+" str))
(right-bound (match-end 0)))
(list (+ (car bounds) left-bound) (+ (car bounds) right-bound) left-bound right-bound)))
(defun test-bound-of-integer ()
(let* ((testResults (let ((testData '(("200" 0 3) ("200dsdfs" 0 3) ("abc200dsdfs" 3 6) ("abc200" 3 6) ("abc-200dsdfs" 3 7) ("abc+200dsdfs" 3 7))))
(mapcar (lambda (test)
(let* ((testStr (car test))
(expected-left-bound (cadr test))
(expected-right-bound (caddr test))
(left-bound (car (bound-of-integer testStr '(0 0))))
(right-bound (cadr (bound-of-integer testStr '(0 0))))
)
(progn
(message testStr)
(message (number-to-string expected-left-bound))
(message (number-to-string expected-right-bound))
(message (number-to-string left-bound))
(message (number-to-string right-bound))
(message "%s" (equal (equal expected-left-bound left-bound) (equal expected-right-bound right-bound)))
(equal (equal expected-left-bound left-bound) (equal expected-right-bound right-bound))
)
)
)
testData)
)
)
(result (reduce 'equal testResults))
)
(if result
(message "Test passed")
(message "Tests failed: %s" result)
)
)
)
;; (test-bound-of-integer)
(defun deincrement (op)
(let* ((str (thing-at-point 'symbol))
(string-bounds (bound-of-integer str (bounds-of-thing-at-point 'symbol)))
(left-bound (caddr string-bounds))
(right-bound (cadddr string-bounds))
(n (string-to-number (substring str left-bound right-bound)))
(org_point (point)))
(unless (null string-bounds)
(progn
(kill-region (car string-bounds)
(cadr string-bounds))
(insert (number-to-string (funcall op n)))
(goto-char org_point)))))
;; increment integer at point with 1.
(defun increment ()
(interactive)
(deincrement (lambda (x) (1+ x))))
(define-key global-map [M-up] 'increment)
;; decrement integer at point with 1.
(defun decrement ()
(interactive)
(deincrement (lambda (x) (1- x))))
(define-key global-map [M-down] 'decrement)