-
Notifications
You must be signed in to change notification settings - Fork 19
/
gnu-apl-util.el
94 lines (75 loc) · 2.69 KB
/
gnu-apl-util.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
;;; gnu-apl-util.el --- Utility functionality for `gnu-apl-mode' -*- lexical-binding: t -*-
;; Copyright (C) 2013-2015 Elias Mårtenson
;;; Code:
(require 'cl-lib)
(cl-defun gnu-apl--trim (regexp string &optional (start t) (end t))
(if (or start end)
(let ((res string)
(reg (cond ((and start end)
(concat "\\(\\`" regexp "\\)\\|\\(" regexp "\\'\\)"))
((not end)
(concat "\\`" regexp))
(t
(concat regexp "\\'")))))
(while (string-match reg res)
(setq res (replace-match "" t t res)))
res)
string))
(cl-defun gnu-apl--trim-spaces (string &optional (start t) (end t))
(gnu-apl--trim "[ \t]" string start end))
(cl-defun gnu-apl--trim-trailing-newline (string)
(gnu-apl--trim "[\n\r]" string nil t))
(defun gnu-apl--open-new-buffer (name)
(let ((buffer (get-buffer name)))
(when buffer
(delete-windows-on buffer)
(kill-buffer buffer))
(get-buffer-create name)))
(defun gnu-apl--string-match-start (string key)
(and (>= (length string) (length key))
(string= (cl-subseq string 0 (length key)) key)))
(defun gnu-apl--kbd (definition)
(if (functionp #'kbd)
(kbd definition)
(eval `(kbd ,definition))))
(cl-defmacro gnu-apl--when-let ((var value) &rest body)
"Evaluate VALUE, if the result is non-nil bind it to VAR and eval BODY.
\(fn (VAR VALUE) &rest BODY)"
(declare (indent 1))
`(let ((,var ,value))
(when ,var ,@body)))
(defun gnu-apl--move-to-line (line)
"A version of ‘goto-line’ that does the right thing in narrowed buffers."
(let ((dest-char (save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(forward-line (1- line))
(point)))))
(goto-char dest-char)))
(defun gnu-apl--current-line-number (&optional pos)
(save-restriction
(widen)
(save-excursion
(when pos
(goto-char pos))
(beginning-of-line)
(1+ (count-lines 1 (point))))))
(defun gnu-apl--current-line-string ()
(let ((start (save-excursion (beginning-of-line) (point)))
(end (save-excursion (end-of-line) (point))))
(buffer-substring start end)))
(unless (fboundp 'cl-find)
(defun cl-find (&rest args)
(apply #'cl-find args)))
(unless (fboundp 'cl-defun)
(defmacro cl-defun (&rest args)
`(defun* ,@args)))
(unless (fboundp 'cl-defmacro)
(defmacro cl-defmacro (&rest args)
`(defmacro* ,@args)))
(unless (fboundp 'cl-remove-if-not)
(defun cl-remove-if-not (&rest args)
(apply #'cl-remove-if-not args)))
(provide 'gnu-apl-util)
;;; gnu-apl-util.el ends here