eshell-tricks/eshell-tricks.el

52 lines
2.2 KiB
EmacsLisp

(setq eshell-tricks-file "/home/user/.emacs.d/eshell-tricks/eshell-tricks-macros.el")
(defun etricks-previous-output (&optional BUF-NAME)
"Returns the output of the previous eshell command in a buffer.
BUF-NAME can pass the name of the new buffer."
(interactive)
(let* ((BUF-NAME (or BUF-NAME (concat "cpy-" (eshell-previous-input-string 1))))
(cur-buf (current-buffer))
(prompt-pos (eshell-previous-prompt 2))
(end-pos (point-max))
(new-buf (get-buffer-create BUF-NAME)))
(with-current-buffer new-buf
(insert-buffer-substring cur-buf prompt-pos end-pos))
(switch-to-buffer-other-frame new-buf)))
(defun etricks-handle-eshell-return (RETURN-VALUE)
"Returns a simple string from the executed eshell command (you give it the returned object as RETURN-VALUE).
That's needed because e.g. elisp expressions might just return nil or a list or something."
(catch 'etricks-handle-eshell-return-exit
(if (stringp RETURN-VALUE)
(throw 'etricks-handle-eshell-return-exit (substring-no-properties RETURN-VALUE))
(throw 'etricks-handle-eshell-return-exit (format "%s" RETURN-VALUE)))))
(defmacro etricks-backwards-macro (FUNC-NAME ESHELL-COMMANDS &optional DIRECTORY)
"Returns the ESHELL-COMMANDS in a functinion with FUNC-NAME."
`(defun ,FUNC-NAME ()
(let ((default-directory (or ,DIRECTORY default-directory)))
(apply #'concat (mapcar (lambda (x) (if (null x)
(progn nil)
(substring-no-properties x)))
(mapcar #'eshell-command-result ',ESHELL-COMMANDS))))))
(defun eshell/etricks-record (FUNC-NAME N &optional USE-DIRECTORY)
"Saves the last N eshell commands as a function named FUNC-NAME in the file
you defined in the eshell-tricks-file variable. "
(interactive)
(let ((eshell-commands (reverse (mapcar #'substring-no-properties
(mapcar #'eshell-previous-input-string
(number-sequence 1 N))))))
(with-temp-buffer
(prin1 `(etricks-backwards-macro ,FUNC-NAME ,eshell-commands
,(if (null USE-DIRECTORY)
(progn nil)
(progn default-directory)))
(current-buffer))
(eval-last-sexp nil)
(insert "\n")
(append-to-file nil nil eshell-tricks-file))))