]> code.delx.au - dotemacs/blobdiff - lisp/my-defuns.el
more text utility functions with key bindings
[dotemacs] / lisp / my-defuns.el
index 176da08dbf2520f9327b1b95ac7cffacc8d99084..2232e79e2cab6c1e482642c56911a0c58354c97a 100644 (file)
@@ -1,83 +1,55 @@
 ;;; -*- lexical-binding: t -*-
 
-(require 'cl-lib)
+(defun my/comment-dwim (arg)
+  "Toggles the comment on for the active region if present or the current line otherwise."
+  (interactive "*p")
+  (cond
+   ((and mark-active transient-mark-mode)
+    (let ((start (save-excursion (goto-char (region-beginning)) (line-beginning-position)))
+          (end (save-excursion (goto-char (region-end)) (line-end-position))))
+      (comment-or-uncomment-region start end)))
+   (t
+    (comment-or-uncomment-region (line-beginning-position) (line-end-position arg))
+    (forward-line arg))))
 
 (defun my/copy-line (arg)
-  "Copy lines in the kill ring"
-  (interactive "p")
-  (kill-ring-save (line-beginning-position)
+  "Copy the current line into the kill ring. With ARG copies that many lines."
+  (interactive "*p")
+  (kill-ring-save (line-beginning-position 1)
                   (line-beginning-position (+ 1 arg)))
-  (message "%d line%s copied" arg (if (= 1 arg) "" "s")))
-
-(defun my/generate-frame-title ()
-  (if (buffer-file-name)
-      (concat
-       (file-name-nondirectory (buffer-file-name))
-       (if (buffer-modified-p)
-           " +")
-       " ("
-       (abbreviate-file-name (substring (file-name-directory (buffer-file-name)) 0 -1))
-       ") - Emacs"
-       )
-    (concat
-     (buffer-name)
-     (if (buffer-modified-p)
-         " +")
-     " - Emacs")))
+  (message "Copied %d lines" arg))
 
-(defun my/frame-initial-frame-p (frame)
-  "Returns true if the given frame is the magic 'initial frame' that always exists in GUI emacs sessions"
-  (equal "initial_terminal" (terminal-name frame)))
-
-(defun my/frame-list-ignoring-initial-frame ()
-  (filtered-frame-list (lambda (frame) (not (my/frame-initial-frame-p frame)))))
+(defun my/duplicate-line (arg)
+  "Duplicate current line, leaving point in lower line. With ARG duplicates the line that many lines."
+  (interactive "*p")
+  (kill-ring-save (line-beginning-position 1)
+                  (line-beginning-position 2))
+  (forward-line)
+  (dotimes (ignored arg)
+    (yank))
+  (forward-line (- arg))
+  (back-to-indentation))
 
 (defun my/git-reset-buffer ()
+  "Runs git-reset to unstage all changes on the current file. Then updates the git-gutter."
   (interactive)
   (call-process "git" nil nil nil "reset" (buffer-file-name))
-  (git-gutter))
-
-(defun my/kill-buffer-safely (buffer)
-  "Kill the buffer if it is not special or modified"
-  (if (and
-       (not (string-match "^ " (buffer-name buffer)))
-       (not (equal "*Messages*" (buffer-name buffer)))
-       (or
-        (not (buffer-modified-p buffer))
-        (null (buffer-file-name buffer))))
-      (kill-buffer buffer)))
-
-(defun my/kill-buffers-if-deleting-last-frame (frame)
-  "Kill all buffers when closing the last frame"
-  (when (equal (list frame) (my/frame-list-ignoring-initial-frame))
-    (dolist (buffer (buffer-list))
-      (my/kill-buffer-safely buffer))))
-
-(defun my/kill-buffers-not-in-frame ()
-  "Kill buffers which are not loaded into some frame"
-  (interactive)
-  (let ((kill-count 0))
-    (dolist (buffer (buffer-list))
-      (let* ((window (get-buffer-window buffer t))
-             (frame (window-frame window)))
-        (if (or (null frame) (not (window-live-p window)) (my/frame-initial-frame-p frame))
-            (if (my/kill-buffer-safely buffer)
-                (cl-incf kill-count)))))
-    (message "Killed %d buffers" kill-count)))
+  (git-gutter)
+  (message "Finished git reset"))
 
-(defun my/open-line-above ()
-  "Open a new line above point with indentation"
-  (interactive)
+(defun my/open-line-above (arg)
+  "Open a new line above point with indentation. With ARG insert that many lines."
+  (interactive "*p")
   (beginning-of-line)
-  (newline)
-  (forward-line -1)
+  (newline arg)
+  (forward-line (- arg))
   (indent-for-tab-command))
 
-(defun my/open-line-below ()
-  "Open a new line below point with indentation"
-  (interactive)
+(defun my/open-line-below (arg)
+  "Open a new line below point with indentation. With ARG insert that many lines."
+  (interactive "*p")
   (end-of-line)
-  (newline)
+  (newline arg)
   (indent-for-tab-command))
 
 (defun my/scratch-buffer ()
              (setq n (1+ n))
              (get-buffer bufname)))
     (switch-to-buffer (get-buffer-create bufname))
-    (emacs-lisp-mode)
-    ))
+    (emacs-lisp-mode)))
 
-(defun my/toggle-comment-on-line ()
-  "Toggles the comment on for the active region if present or the current line otherwise."
-  (interactive)
-  (if (and mark-active transient-mark-mode)
-      (comment-or-uncomment-region (region-beginning) (region-end))
-    (comment-or-uncomment-region (line-beginning-position) (line-end-position)))
-  (forward-line))
-
-(defun my/terminal-update-title ()
-  "If using a terminal frame then sends the escape codes to update the title."
-  (if (terminal-parameter (frame-terminal) 'terminal-initted)
-      (send-string-to-terminal (concat "\033]0;" (my/generate-frame-title) "\007"))))
+(defun my/substitute-line (arg)
+  "Kill the current line and leave point at correct indentation level. With ARG kill that many lines first."
+  (interactive "*p")
+  (beginning-of-line)
+  (kill-line arg)
+  (indent-for-tab-command))