-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcsound-util.el
113 lines (91 loc) · 3.79 KB
/
csound-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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
;;; csound-util.el --- A major mode for interacting and coding Csound
;; Copyright (C) 2017 - 2023 Hlöðver Sigurðsson
;; Author: Hlöðver Sigurðsson <[email protected]>
;; Version: 0.2.9
;; Package-Requires: ((emacs "25") (shut-up "0.3.2") (multi "2.0.1") (dash "2.16.0") (highlight "0"))
;; URL: https://github.com/hlolli/csound-mode
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Helper functions needed by various csound-mode files.
;;; Code:
(require 'csound-opcodes)
(require 'dash)
(defun csound-util-chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
str)
(setq str (replace-match "" t t str)))
str)
(defun csound-util-untab (str)
(while (string-match "\t" str)
(setq str (replace-match " " t t str)))
str)
(defun csound-util-line-boundry ()
"returns the end point of the line and stops befor the occurence of
a commentary"
(let ((comment (save-excursion
(search-forward ";" (line-end-position 1) t 1)))
(multi-comment (save-excursion
(search-forward
"/*" (line-end-position 1) t 1))))
(cond
(comment (1- comment))
(multi-comment (1- (1- multi-comment)))
(t (line-end-position 1)))))
(defun csound-util-remove-comment-in-string (string)
"replaces comments in a string with an empty string and gives back the string without the
comments"
(->> string
(replace-regexp-in-string ";.*" "")
(replace-regexp-in-string "/\\*\\(.\\|\n\\)*\\*/" "")))
(defun csound-util-recursive-count* (regex string start)
"counts the appearence of the regex in the given string"
(if (string-match regex string start)
(+ 1 (csound-util-recursive-count* regex string (match-end 0)))
0))
(defun csound-util-recursive-count (regex string start)
"counts only the appearence of the regex in the given string with
comments removed"
(csound-util-recursive-count* regex (csound-util-remove-comment-in-string string) start))
(defun csound-util--generate-random-uuid ()
"Insert a random UUID.
Example of a UUID: 1df63142-a513-c850-31a3-535fc3520c3d
WARNING: this is a simple implementation.
The chance of generating the same UUID is much higher than a robust algorithm.."
(format "%04x%04x-%04x-%04x-%04x-%06x%06x"
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 6))
(random (expt 16 6))))
(defun csound-util-strip-text-properties (txt)
(set-text-properties 0 (length txt) nil txt)
txt)
(defun csound-util-opcode-completion-at-point ()
(let ((bounds (bounds-of-thing-at-point 'word)))
(when bounds
(list (car bounds)
(cdr bounds)
csdoc-opcode-database
:exclusive 'no
:company-docsig (lambda (cand)
(csound-util-chomp (replace-regexp-in-string
"\n\\|\s+" "\s"
(nth 3 (gethash cand csdoc-opcode-database)))))
:company-doc-buffer (lambda (cand)
(prin1-to-string (nth 11 (gethash cand csdoc-opcode-database))))
;;:company-location (lambda (cand) (nth 11 (gethash cand csdoc-opcode-database)))
))))
(provide 'csound-util)
;;; csound-util.el ends here