Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (provide 'my/utils)
- (require 'f)
- ;; See how this works and how it is used
- (defmacro my/with-advice (adlist &rest body)
- "Execute BODY with temporary advice in ADLIST.
- TODO You could also do this with something like the noflet package, if you prefer.
- Each element of ADLIST should be a list of the form
- (SYMBOL WHERE FUNCTION [PROPS])
- suitable for passing to `advice-add'. The BODY is wrapped in an
- `unwind-protect' form, so the advice will be removed even in the
- event of an error or nonlocal exit."
- (declare (debug ((&rest (&rest form)) body))
- (indent 1))
- `(progn
- ,@(mapcar (lambda (adform)
- (cons 'advice-add adform))
- adlist)
- (unwind-protect (progn ,@body)
- ,@(mapcar (lambda (adform)
- `(advice-remove ,(car adform) ,(nth 2 adform)))
- adlist))))
- (defun my/call-logging-hooks (command &optional verbose)
- "Call COMMAND, reporting every hook run in the process.
- Interactively, prompt for a command to execute.
- Return a list of the hooks run, in the order they were run.
- Interactively, or with optional argument VERBOSE, also print a
- message listing the hooks.
- [[https://emacs.stackexchange.com/questions/19578/list-hooks-that-will-run-after-command][List hooks that will run after command - Emacs Stack Exchange]]
- There are some caveats.
- "
- (interactive "CCommand to log hooks: \np")
- (let* ((log nil)
- (logger (lambda (&rest hooks)
- (setq log (append log hooks nil)))))
- (my/with-advice
- ((#'run-hooks :before logger))
- (call-interactively command))
- (when verbose
- (message
- (if log "Hooks run during execution of %s:"
- "No hooks run during execution of %s.")
- command)
- (dolist (hook log)
- (message "> %s" hook)))
- log))
- (defun get-string-from-file (filePath)
- "Return filePath's file content."
- (with-temp-buffer
- (insert-file-contents filePath)
- (buffer-string)))
- (defun exit ()
- "Exit/return early from an elisp file
- How is this not a builtin?"
- (with-current-buffer " *load*"
- (goto-char (point-max))))
- (defun log-it (message)
- "I want my own function because emacs does not remember messages after init"
- (bash "cat > $NOTES/programs/messages.log"))
- ;; Use this to print messages when debugging .emacs
- ;; I can use tmux, but, hide the window
- ;; I need a command that can use stdin.
- ;; emacs uses ptys. Therefore, It's hard to separate output. So I should write my own mechanism to collect stdout.
- ;; If I always use tmux, then it's ok. Always detach tmux.
- ;; TODO use (bash) to *locate* org files rather than searching for them.
- (defun sh-notty (&optional cmd stdin)
- "Runs command in shell and return the result"
- (interactive)
- (if (not cmd)
- (setq cmd "false"))
- (setq tf (make-temp-file "elispbash"))
- (setq final_cmd (concat "( cd \"" (get-dir) "\"; " cmd ") > " tf))
- (if (not stdin)
- (progn
- (shell-command final_cmd))
- (with-temp-buffer
- (insert stdin)
- (shell-command-on-region (point-min) (point-max) final_cmd)))
- (setq output (get-string-from-file tf))
- output)
- (defun bash (&optional cmd stdin b_output tm_session)
- (interactive)
- (sh cmd stdin b_output tm_session "bash"))
- (defun zsh (&optional cmd stdin b_output tm_session)
- (interactive)
- (sh cmd stdin b_output tm_session "zsh"))
- (defun sh (&optional cmd stdin b_output tm_session shell)
- "Runs command in a new tmux window and optionally returns the output of the command as a string.
- b_output is (t/nil) tm_session is the session of the new tmux window"
- (interactive)
- ;; (message "starting")
- (if (not shell)
- (setq shell "bash"))
- (if (not tm_session)
- (setq tm_session "localhost_current"))
- (setq session-dir-cmd (concat "nw -sh " shell " -t " tm_session " -d -c \"" (get-dir) "\" \"" cmd "\""))
- (if b_output
- ;; This means bash is recursive. Can't do this.
- ;; (setq tf (bash "nix tf tempfile dat"))
- (setq tf (make-temp-file "elispbash")))
- (if (not cmd)
- (setq cmd "zsh"))
- (if b_output
- ;; unbuffer breaks stdout
- (if stdin
- (setq final_cmd (concat "tm -f -s -sout " session-dir-cmd " > " tf))
- (setq final_cmd (concat "unbuffer tm -f -fout " session-dir-cmd " > " tf)))
- (if stdin
- (setq final_cmd (concat "tm -f -S -tout " session-dir-cmd))
- (setq final_cmd (concat "unbuffer tm -f -d -tout -d -te " session-dir-cmd))))
- ;; (message (concat "bash: " final_cmd))
- (if (not stdin)
- (shell-command final_cmd)
- (with-temp-buffer
- (insert stdin)
- ;; This only returns the exit code. I want stdout
- (shell-command-on-region (point-min) (point-max) final_cmd))
- ;; This does not fix it
- ;; (with-output-to-string
- ;; (shell-command-on-region (point-min) (point-max) final_cmd)
- ;; )
- )
- ;; (message (concat "bash-done: " final_cmd))
- (if b_output
- ;; (sleep-for 5)
- ;; Cat tf
- (progn
- (setq output (get-string-from-file tf))
- ;; (message (concat "output: " output))
- output)
- ))
- (defun my/add-all-commit ()
- (interactive)
- (bash "git add -A .; git commit \"$(k f8)\"")
- (message "Added all changes to git and committed."))
- (defun select-tmux-current ()
- (interactive)
- ;; This doesn't work because it selects itself
- ;; (bash "tm -f -d select-current")
- (shell-command "tm -f -d select-current"))
- (defun tv (stdin)
- "converts the parameter to its string representation and pipes it into tmux"
- (interactive)
- (bash "v" (format "%s" stdin))
- (select-tmux-current))
- ;; (message (sed "s/a/b/g" "aaaa"))
- (defun sed (command stdin)
- "wrapper around sed"
- (interactive)
- (setq stdin (format "%s" stdin))
- (setq command (concat "sed '" (format "%s" command) "'"))
- (sh-notty command stdin))
- (defun tmux-bash (&optional cmd)
- "Runs command in a new tmux window"
- (interactive)
- (if (not cmd)
- (setq cmd "zsh"))
- (shell-command (concat "unbuffer tm -f -d -te nw -c \"" (get-dir) "\" \"" cmd "\"")))
- ;; Use this when I get the error. Eshell buffer goes into "text is read only", can't type #3565
- (defun eshell/clear ()
- "Clear the eshell buffer."
- (interactive)
- (let ((inhibit-read-only t))
- (erase-buffer)))
- (defun my/slurp (f)
- (with-temp-buffer
- (insert-file-contents f)
- (buffer-substring-no-properties
- (point-min)
- (point-max))))
- (defun my/load-list (f)
- "split-string splits the file into a list of strings. mapcar intern turns the list of strings into a list of symbols"
- (mapcar 'intern
- (split-string
- (my/slurp f) "\n" t)))
- (defun my-load-all-in-directory (dir)
- "`load' all elisp libraries in directory DIR which are not already loaded."
- (interactive "D")
- (let ((libraries-loaded (mapcar #'file-name-sans-extension
- (delq nil (mapcar #'car load-history)))))
- (dolist (file (directory-files dir t ".+\\.elc?$"))
- (let ((library (file-name-sans-extension file)))
- (unless (member library libraries-loaded)
- (load library nil t)
- (push library libraries-loaded))))))
- (defun my-find-file (filename)
- (let ((size-in-bytes (nth 7 (file-attributes filename))))
- (if (< size-in-bytes 1e3)
- (find-file filename)
- (vlf filename))))
- ;; Command filter
- (defun cfilter (cmd)
- "This pipes the region or entire buffer through a shell command
- This is kinda outdated now."
- (let ((cmd (concat cmd " | s chomp")))
- (if (region-active-p)
- (let ((rstart (region-beginning))
- (rend (region-end)))
- (shell-command-on-region rstart rend cmd nil 1))
- (progn
- (mark-whole-buffer)
- (cfilter cmd)))))
- (defun toggle-evil ()
- (interactive)
- "Tries Holy Mode + falls back to evil"
- (if (fboundp 'spacemacs/toggle-holy-mode)
- (spacemacs/toggle-holy-mode)
- (call-interactively #'evil-mode)))
- (defun region-or-string (&optional object)
- "Return string rep of object if given, otherwise, return the string of the selected region"
- (interactive)
- (cond ((region-active-p) (buffer-substring (region-beginning) (region-end)))
- (object (format "%s" object))
- (t nil)
- ))
- (defun q (input)
- "Only quotes if it needs to"
- (interactive)
- (sh-notty "q" input))
- (defun qne (input)
- "Quotes but removes the ends"
- (interactive)
- (sh-notty "qne" input))
- (defun uq (input)
- "Unquotes"
- (interactive)
- (sh-notty "uq" input))
- (defun uqftln (input)
- "Unquotes each line"
- (interactive)
- (sh-notty "uq -ftln" input))
- (defun qftln (input)
- "Quotes each line"
- (interactive)
- (sh-notty "q -ftln" input))
- (defun fzf (&optional string)
- "Pipes argument or region through fzf"
- (interactive)
- (bash "fzf" (region-or-string string))
- (select-tmux-current)
- )
- (defun sph (&optional cmd)
- "Runs command in a horizontal split"
- (interactive)
- (if (not cmd)
- (setq cmd "zsh"))
- (shell-command (concat "unbuffer tm -f -d -te sph -c \"" (get-dir) "\" \"" cmd "\"")))
- (defun spv (&optional cmd)
- "Runs command in a vertical split"
- (interactive)
- (if (not cmd)
- (setq cmd "zsh"))
- (shell-command (concat "unbuffer tm -f -d -te spv -c \"" (get-dir) "\" \"" cmd "\"")))
- (defun compile-run ()
- (interactive)
- ( shell-command (concat "unbuffer tm -f -te -d spv -args cr \"" (buffer-file-name) "\"") )
- )
- (defmacro make-wrapper (wrappee wrapper)
- "Create a WRAPPER (a symbol) for WRAPPEE (also a symbol)."
- (let ((arglist (make-symbol "arglist")))
- `(defun ,wrapper (&rest ,arglist)
- ,(concat (documentation wrappee) "\n But I do something more.")
- ,(interactive-form wrappee)
- (prog1 (apply (quote ,wrappee) ,arglist)
- (message "Wrapper %S does something more." (quote ,wrapper))))))
- (defun wrappee-interactive (num str)
- "That is the doc string of wrappee-interactive."
- (interactive "nNumber:\nsString:")
- (message "The number is %d.\nThe string is \"%s\"." num str))
- (defun wrappee-non-interactive (format &rest arglist)
- "That is the doc string of wrappee-non-interactive."
- (apply 'message format arglist))
- (make-wrapper wrappee-interactive wrapper-interactive)
- (make-wrapper wrappee-non-interactive wrapper-non-interactive)
- ;; test of the non-interactive part:
- (wrapper-non-interactive "Number: %d, String: %s" 1 "test")
- ;; This doesnt even work because it uses simpleclip
- (defun copy-current-line-position-to-clipboard ()
- "Copy current line in file to clipboard as '</path/to/file>:<line-number>'"
- (interactive)
- (let ((path-with-line-number
- (concat (buffer-file-name) ":" (number-to-string (line-number-at-pos)))))
- ;;(x-select-text path-with-line-number)
- (simpleclip-set-contents path-with-line-number)
- (message (concat path-with-line-number " copied to clipboard"))))
- (defvar my-terminal-run-history nil)
- ;; But how to get this to show ansi-colors?
- (defun my-terminal-run (command &optional name)
- "Runs COMMAND in a `term' buffer."
- (interactive
- (list (read-from-minibuffer "$ " nil nil nil 'my-terminal-run-history)))
- (let* ((name (or name command))
- (switches (split-string-and-unquote command))
- (command (pop switches))
- (termbuf (apply 'make-term name command nil switches)))
- (set-buffer termbuf)
- (term-mode)
- (term-char-mode)
- (switch-to-buffer termbuf)))
- (global-set-key (kbd "C-c s c") 'my-terminal-run)
- (defun my/beep ()
- (let ((inhibit-message t)) (shell-command "tmux run -b 'a beep'")))
- ;; This is how to start and stop a timer
- ;; (run-with-timer 0 1 #'my-beep)
- ;; (cancel-function-timers #'my/beep)
- (defun do-lines (fun &optional start end)
- "Invoke function FUN on the text of each line from START to END."
- (interactive
- (let ((fn (intern (completing-read "Function: " obarray 'functionp t))))
- (if (use-region-p)
- (list fn (region-beginning) (region-end))
- (list fn (point-min) (point-max)))))
- (save-excursion
- (goto-char start)
- (while (< (point) end)
- (funcall fun (buffer-substring (line-beginning-position) (line-end-position)))
- (forward-line 1))))
- (defun crc32 (string)
- (interactive)
- ;; The extra bash is only necessary for the tmux version
- ;; (bash "bash -c 'crc32 <(cat)' | s chomp" string t)
- (sh-notty "crc32 <(cat) | s chomp" string))
- ;; Pure elisp sha512
- (defun sha512 (object &optional start end binary)
- "Return the SHA512 of an OBJECT.
- OBJECT is either a string or a buffer. Optional arguments START and
- END are character positions specifying which portion of OBJECT for
- computing the hash. If BINARY is non-nil, return a string in binary
- form."
- ;; md5
- ;; sha1
- ;; sha224
- ;; sha256
- ;; sha384
- ;; sha512
- ;; Emacs has some builtin hash functions, but I should make everything myself to learn it.
- ;; (secure-hash 'sha1 object start end binary)
- (secure-hash 'sha512 object start end binary))
- (defun my/sha1 (string)
- "Can't replace existing sha1 builtin function."
- (interactive)
- ;; The extra bash is only necessary for the tmux version
- ;; (bash "bash -c 'crc32 <(cat)' | s chomp" string t)
- ;; (sh-notty "zsh -c 'sha1sum =(cat) | cut -d \\ -f 1' | s chomp" string)
- (sh-notty "openssl dgst -sha1 -binary" string))
- (defun test-virtmic (mp3_path)
- (interactive))
- (defun mp42mkv (path_input)
- "Converts an mp4 into an mkv. Use this when streaming to another computer and I need to watch it as it's streaming."
- (interactive)
- ;; I thought about using sed here, but this is probably better because for performance
- (defvar path_output (concat path_input ".mkv"))
- (bash (concat "ffmpeg -i " path_input " -vcodec copy " path_output)))
- ;; [[https://stackoverflow.com/questions/24565068/emacs-text-is-read-only?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa][lisp - (Emacs) Text is read only? - Stack Overflow]]
- ;; Useful as temporary hack to fix slime repl.
- (defun set-region-writeable (begin end)
- "Removes the read-only text property from the marked region."
- ;; See http://stackoverflow.com/questions/7410125
- (interactive "r")
- (let ((modified (buffer-modified-p))
- (inhibit-read-only t))
- (remove-text-properties begin end '(read-only t))
- (set-buffer-modified-p modified)))
- (defun cider-connect-tmux ()
- "Scrapes the port from the tmux window and connects to cider"
- (interactive)
- (let ((port
- (sh-notty "tm cat-pane localhost_current:lein-repl.0 | sed -n '/on port \\([0-9]\\+\\)/ s/.*on port \\([0-9]\\+\\).*/\\1/p' | s chomp")))
- (cider-connect "localhost" port "$HOME/notes2018/ws/clojure/scratch/")))
- ;; ;; (ignore-errors (kill-buffer "*cider-repl tstprjclj*"))
- ;; Not using this anymore. Instead, I'm waiting for this to be ready
- ;; localhost_ws_clojure_scratch:cider-jack-in-default.0
- (defun clojure-cheatsheet-boot ()
- "Not using this anymore"
- (interactive)
- (ignore-errors (kill-buffer "*cider-repl localhost*"))
- (cider-connect-tmux)
- (ignore-errors (clojure-cheatsheet)))
- (defun py/install-file ()
- "Install a single python file as a module"
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement