-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrugru-utils.el
60 lines (47 loc) · 2.35 KB
/
grugru-utils.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
;;; grugru-utils.el --- Helpful utilities for users to define complex grugru -*- lexical-binding: t; -*-
;; Copyright (C) 2021 ROCKTAKEY
;; Author: ROCKTAKEY <rocktakey@gmail.com>
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'cl-lib)
;;;###autoload
(defun grugru-utils-lisp-exchange-args (list-string permutation)
"Permute argument of sexp read from LIST-STRING according to PERMUTATION.
For example, (grugru-utils-lisp-exchange-args \"(nth 1 '(x y z))\" '(2 1))
returns \"(nth '(x y z) 1)\". Newlines and whitespaces are also kept.
This function is defined for user to define the function for grugru which rotate
not only fuction's name but also arguments' order."
(ignore-errors
(let* ((no-parenthesis (substring-no-properties list-string 1 -1))
(function-name-pos (cdr (read-from-string no-parenthesis)))
(args-string (substring-no-properties
no-parenthesis function-name-pos))
(args-string-list
(let ((pos 0)
new-pos result)
(while (setq new-pos (ignore-errors (cdr (read-from-string args-string pos))))
(push (substring-no-properties args-string pos new-pos) result)
(setq pos new-pos))
(nreverse result)))
(permutation-length (length permutation))
(result (make-vector permutation-length nil)))
(cl-mapcar
(lambda (arg num) (setf (aref result (1- num)) arg))
args-string-list permutation)
(apply #'concat
"(" (substring-no-properties no-parenthesis nil function-name-pos)
(append result (nthcdr permutation-length args-string-list)
'(")"))))))
(provide 'grugru-utils)
;;; grugru-utils.el ends here