SHARE
TWEET

Misc functions

a guest Jun 15th, 2017 872 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defun user--save-macro (name)
  2.   "Save a macro. Take a name as an argument and save the last defined macro under this name."
  3.   (interactive "SName of the macro :")
  4.   (kmacro-name-last-macro name)
  5.   (find-file "~/.emacs.d/macros.el")
  6.   (goto-char (point-max))
  7.   (newline)
  8.   (insert-kbd-macro name)
  9.   (newline)
  10.   (save-buffer)
  11.   (switch-to-buffer nil))
  12.  
  13.  
  14. (defun user--clean-buffer () "Cleans the buffer by re-indenting, changing tabs to spaces, and removing trailing whitespace."
  15.   (interactive)
  16.   (delete-trailing-whitespace) ;; Remove whitespace from the ends of lines
  17.   (save-excursion (replace-regexp "^\n\\{3,\\}" "\n\n" nil (point-min) (point-max))) ;; Replace more than 2 newlines with 2 newlines
  18.   (untabify (point-min) (point-max))) ;; Turn tabs into spaces
  19.  
  20. (defun user--delete-in-quotes () "Deletes the text inside of quotes."
  21.   (interactive)
  22.   (search-backward-regexp "[\"\']" (line-beginning-position)) ;; Search for a match on the same line, don't delete across lines
  23.   (forward-char)
  24.   (let  ((lstart (point)))
  25.     (search-forward-regexp "[\"\']" (line-end-position))
  26.     (backward-char)
  27.     (kill-region lstart (point))))
  28.  
  29. (defun user--delete-in-parentheses () "Deletes the text within parentheses."
  30.   (interactive)
  31.   (search-backward "(" (line-beginning-position)) ;; Search for a match on the same line, don't delete across lines
  32.   (forward-char)
  33.   (let  ((lstart (point)))
  34.     (search-forward ")" (line-end-position))
  35.     (backward-char)
  36.     (kill-region lstart (point))))
  37.  
  38. (defun user--delete-in-brackets () "Deletes the text within square brackets, angle brackets, and curly brackets."
  39.   (interactive)
  40.   (search-backward-regexp "[[{<]" (line-beginning-position)) ;; Search for a match on the same line, don't delete across lines
  41.   (forward-char)
  42.   (let ((lstart (point)))
  43.     (search-forward-regexp "[]}>]" (line-end-position))
  44.     (backward-char)
  45.     (kill-region lstart (point))))
  46.  
  47. (defun user--rename-this-file-and-buffer (new-name)
  48.   "Renames both current buffer and file it's visiting to NEW-NAME."
  49.   (interactive "sNew name: ")
  50.   (let ((name (buffer-name))
  51.         (filename (buffer-file-name)))
  52.     (unless filename
  53.       (error "Buffer '%s' is not visiting a file!" name))
  54.     (if (get-buffer new-name)
  55.         (message "A buffer named '%s' already exists!" new-name)
  56.       (progn
  57.         (rename-file name new-name 1)
  58.         (rename-buffer new-name)
  59.         (set-visited-file-name new-name)
  60.         (set-buffer-modified-p nil)))))
  61.  
  62. (defun user--insert-date ()
  63.   "Insert a time-stamp according to locale's date and time format."
  64.   (interactive)
  65.   (insert (format-time-string "%c" (current-time))))
  66.  
  67.  
  68. (defun user--generate-numbered-list (start end)
  69.   "Creates a numbered list from provided start to provided end."
  70.   (interactive "nStart num:\nnEnd num:")
  71.   (let ((x  start))
  72.     (while (<= x end)
  73.       (insert (number-to-string x) ".")
  74.       (newline)
  75.       (setq x (+ x 1)))))
  76.  
  77. (defun user--mark-ring-forward ()
  78.   "Moves forward through the mark ring. Does nothing if mark ring is empty."
  79.   (interactive)
  80.   (when mark-ring
  81.     (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
  82.     (set-marker (mark-marker) (car (last mark-ring)) (current-buffer))
  83.     (when (null (mark t)) (message "No marks set." nil))
  84.     (setq mark-ring (nbutlast mark-ring))
  85.     (goto-char (marker-position (car (last mark-ring))))))
  86.  
  87. (defun user--make-temp-file (name)
  88.   "Creates a temporary file in the system temp directory, for various purposes."
  89.   (interactive "sFile name:")
  90.   (generate-new-buffer name)
  91.   (switch-to-buffer name)
  92.   (write-file (concat temporary-file-directory name)))
  93.  
  94. (defun user--search-all-buffers (regexp) "Search all open buffers for a regex. Open an occur-like window."
  95.   (interactive "sRegexp: ")
  96.   (multi-occur-in-matching-buffers "." regexp t))
  97.  
  98. (defun user--safe-point-to-register (register)
  99.   "Asks for confirmation before overwriting an existing register with a point-to-register."
  100.   (interactive "cRegister:")
  101.   (if (not (get-register register))
  102.       (point-to-register register)
  103.     (if (y-or-n-p "Replace existing register?")
  104.         (point-to-register register))))
  105.  
  106. (defun user--safe-copy-to-register (register)
  107.   "Asks for confirmation before overwriting an existing register with a copy-to-register."
  108.   (interactive "cRegister:")
  109.     (if (not (get-register register))
  110.       (copy-to-register register (region-beginning) (region-end))
  111.     (if (y-or-n-p "Replace existing register?")
  112.         (copy-to-register register (region-beginning) (region-end)))))
  113.  
  114. (defun user--safe-window-config-to-register (register)
  115.   "Asks for confirmation before overwriting an existing register with a window-configuration-to-register."
  116.   (interactive "cRegister:")
  117.     (if (not (get-register register))
  118.       (window-configuration-to-register register)
  119.     (if (y-or-n-p "Replace existing register?")
  120.         (window-configuration-to-register register))))
  121.  
  122. (defun user--end-of-line-newline ()
  123.   "Moves to the end of the line and newlines."
  124.   (interactive)
  125.   (end-of-line)
  126.   (newline))
  127.  
  128. (defun user--skim-buffer (lines seconds)
  129.   "Scrolls the buffer `lines` every `seconds`, which allows for roughly skimming over a buffer."
  130.   (interactive "nScroll lines: \nnEvery seconds: ")
  131.   (while (not (= (point) (point-max)))
  132.     (scroll-up-line lines)
  133.     (end-of-line)
  134.     (sit-for seconds)))
RAW Paste Data
Top