aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2021-04-21 09:24:30 +0200
committerOmar Polo <op@omarpolo.com>2021-04-21 09:24:30 +0200
commit8f580bff30f33ea63b1d9e5899cfe4cfea6fff4a (patch)
tree51778e87fdc76a434efea56592e7e82f9541a5de
parent5e0bae9e72b58f4c29a907c711f160b06a596e10 (diff)
downloadsam.el-8f580bff30f33ea63b1d9e5899cfe4cfea6fff4a.tar.gz
sam.el-8f580bff30f33ea63b1d9e5899cfe4cfea6fff4a.tar.bz2
added s command
-rw-r--r--sam.el32
1 files changed, 27 insertions, 5 deletions
diff --git a/sam.el b/sam.el
index 10ee286..388232c 100644
--- a/sam.el
+++ b/sam.el
@@ -72,7 +72,8 @@
("b" . sam-cmd-switch-buffer)
("B" . sam-cmd-switch-buffer-no-fuzzy)
("n" . sam-cmd-buflist)
- ("q" . sam-cmd-quit)))
+ ("q" . sam-cmd-quit)
+ ("s" . sam-cmd-subst)))
(defun sam-get-buffer ()
"Gets the buffer of this sam instance."
@@ -132,13 +133,25 @@
"Parse CMD and return a cons of `cmd' and the associated function, or nil on invalid commands."
(cl-loop
for (command . fn) in sam-cmd-alist
- when (string-equal command cmd) return `(,command . ,fn)))
+ when (string-prefix-p command cmd) return `(,command . ,fn)))
+
+;; TODO: this is still pretty hacky, should be made more robust.
+(defun sam-parse-delimited (cmd)
+ "Parse a delimited string.
+Expect CMD to be in the form `S.*S.*' where `S' is an arbitrary
+separator (usually /) and return a list of (PARSED REST), where
+parsed is the text inside the first delimiters pair (possibly the
+empty string), and rest is what follows."
+ (if (string-empty-p cmd)
+ nil
+ (let ((delimiter (make-string 1 (aref cmd 0))))
+ (cdr (split-string cmd (regexp-quote delimiter))))))
(defun sam-exec-command (cmd)
"Execute the string CMD as sam command."
(if-let (tmp (sam-parse-command cmd))
- (cl-destructuring-bind (cmd . fn) tmp
- (funcall fn (substring cmd (length cmd))))
+ (cl-destructuring-bind (c . fn) tmp
+ (funcall fn (substring cmd (length c))))
(sam-report-error (concat "unknown command: " cmd))))
(defun sam-exec-line ()
@@ -156,7 +169,6 @@
"Insert a newline, executing the command on this line if in command mode."
(interactive)
(sam-exec-line)
- (message "foo")
(if (and (not sam-is-inserting) sam-prompt)
(insert sam-prompt)))
@@ -231,6 +243,16 @@
(defun sam-cmd-quit (_arg)
(kill-buffer (sam-get-buffer)))
+(defun sam-cmd-subst (arg)
+ (cl-destructuring-bind (parsed rest) (sam-parse-delimited arg)
+ (with-current-buffer sam-current-buffer
+ (let ((start (region-beginning))
+ (end (region-end)))
+ (save-excursion
+ (goto-char start)
+ (while (search-forward parsed end t)
+ (replace-match rest nil t)))))))
+
(defun sam-mode ()
"Major mode for sam buffers."
(kill-all-local-variables)