diff options
Diffstat (limited to 'modules/custom-buffer-file.el')
| -rw-r--r-- | modules/custom-buffer-file.el | 92 |
1 files changed, 61 insertions, 31 deletions
diff --git a/modules/custom-buffer-file.el b/modules/custom-buffer-file.el index 2d2fa919..105ed4ff 100644 --- a/modules/custom-buffer-file.el +++ b/modules/custom-buffer-file.el @@ -9,11 +9,17 @@ ;; - moving/renaming/deleting buffer files ;; - diffing buffer contents with saved file version ;; - copying file paths and file:// links to the kill ring -;; - copying entire buffer contents +;; - copying buffer contents (whole buffer, to top of buffer, to bottom of buffer) ;; - clearing buffer contents from point to top or bottom. ;; ;; The PostScript printing auto-detects the system print spooler (lpr or lp) -;; and prints with face/syntax highlighting. Bound to keymap prefix ~C-; b~. +;; and prints with face/syntax highlighting. +;; +;; Keybindings under ~C-; b~: +;; - Copy buffer content submenu at ~C-; b c~ +;; - ~C-; b c w~ copy whole buffer +;; - ~C-; b c t~ copy from beginning to point +;; - ~C-; b c b~ copy from point to end ;; ;;; Code: @@ -58,14 +64,21 @@ auto-detect once per session.") (defun cj/print-buffer-ps (&optional color) "Print the buffer (or active region) as PostScript to the default printer. -With prefix argument COLOR, print in color; otherwise print in monochrome. +With prefix argument COLOR, print in color and skip confirmation; otherwise +print in monochrome with confirmation prompt. Sends directly to the system spooler with no header." (interactive "P") (unless (require 'ps-print nil t) (user-error "Cannot print: ps-print library not found")) (let* ((spooler (cj/print--resolve-spooler)) (want-color (not (null color))) - (have-region (use-region-p))) + (have-region (use-region-p)) + (skip-confirm color)) ; C-u skips confirmation + ;; Confirm unless C-u was used + (when (and (not skip-confirm) + (not (y-or-n-p (format "Send %s to printer? " + (if have-region "region" "buffer"))))) + (user-error "Printing cancelled")) (let ((ps-lpr-command spooler) (ps-printer-name nil) ; default system printer (ps-lpr-switches nil) @@ -200,6 +213,24 @@ is created. A message is displayed when done." (kill-new contents) (message "Buffer contents copied to kill ring"))) +(defun cj/copy-to-bottom-of-buffer () + "Copy text from point to the end of the buffer to the kill ring. +Point and mark are left exactly where they were. No transient region +is created. A message is displayed when done." + (interactive) + (let ((contents (buffer-substring-no-properties (point) (point-max)))) + (kill-new contents) + (message "Copied from point to end of buffer"))) + +(defun cj/copy-to-top-of-buffer () + "Copy text from the beginning of the buffer to point to the kill ring. +Point and mark are left exactly where they were. No transient region +is created. A message is displayed when done." + (interactive) + (let ((contents (buffer-substring-no-properties (point-min) (point)))) + (kill-new contents) + (message "Copied from beginning of buffer to point"))) + (defun cj/clear-to-bottom-of-buffer () "Delete all text from point to the end of the current buffer. This does not save the deleted text in the kill ring." @@ -221,45 +252,41 @@ Do not save the deleted text in the kill ring." (message "Copied: %s" (buffer-name))) (defun cj/diff-buffer-with-file () - "Compare the current modified buffer with the saved version. -Uses unified diff format (-u) for better readability. -Signal an error if the buffer is not visiting a file. - -TODO: Future integration with difftastic for structural diffs (Method 3)." + "Compare the current modified buffer with the saved version using ediff. +Uses the same ediff configuration from diff-config.el (horizontal split, j/k navigation). +Signal an error if the buffer is not visiting a file." (interactive) - (let ((file-path (buffer-file-name))) - (cond - ((not file-path) - (user-error "Current buffer is not visiting a file")) - ((not (file-exists-p file-path)) - (user-error "File %s does not exist on disk" file-path)) - ((not (buffer-modified-p)) - (message "Buffer has no unsaved changes")) - (t - (let ((diff-switches "-u")) ; unified diff format - (diff-buffer-with-file (current-buffer))))))) + (if (buffer-file-name) + (ediff-current-file) + (user-error "Current buffer is not visiting a file"))) ;; --------------------------- Buffer And File Keymap -------------------------- +;; Copy buffer content sub-keymap +(defvar-keymap cj/copy-buffer-content-map + :doc "Keymap for copy buffer content operations." + "w" #'cj/copy-whole-buffer + "b" #'cj/copy-to-bottom-of-buffer + "t" #'cj/copy-to-top-of-buffer) + ;; Buffer & file operations prefix and keymap (defvar-keymap cj/buffer-and-file-map :doc "Keymap for buffer and file operations." "m" #'cj/move-buffer-and-file "r" #'cj/rename-buffer-and-file - "p" #'cj/print-buffer-ps + "p" #'cj/copy-path-to-buffer-file-as-kill "d" #'cj/delete-buffer-and-file "D" #'cj/diff-buffer-with-file - "c" #'cj/copy-whole-buffer + "c" cj/copy-buffer-content-map "n" #'cj/copy-buffer-name + "l" #'cj/copy-link-to-buffer-file + "P" #'cj/print-buffer-ps "t" #'cj/clear-to-top-of-buffer "b" #'cj/clear-to-bottom-of-buffer "x" #'erase-buffer "s" #'mark-whole-buffer "S" #'write-file ;; save as - "g" #'revert-buffer - - "l" #'cj/copy-link-to-buffer-file - "P" #'cj/copy-path-to-buffer-file-as-kill) + "g" #'revert-buffer) (keymap-set cj/custom-keymap "b" cj/buffer-and-file-map) (with-eval-after-load 'which-key @@ -267,19 +294,22 @@ TODO: Future integration with difftastic for structural diffs (Method 3)." "C-; b" "buffer and file menu" "C-; b m" "move file" "C-; b r" "rename file" - "C-; b p" "print to PS" + "C-; b p" "copy file path" "C-; b d" "delete file" "C-; b D" "diff buffer with file" - "C-; b c" "copy buffer" + "C-; b c" "buffer copy menu" + "C-; b c w" "copy whole buffer" + "C-; b c b" "copy to bottom" + "C-; b c t" "copy to top" "C-; b n" "copy buffer name" + "C-; b l" "copy file link" + "C-; b P" "print to PS" "C-; b t" "clear to top" "C-; b b" "clear to bottom" "C-; b x" "erase buffer" "C-; b s" "select whole buffer" "C-; b S" "save as" - "C-; b g" "revert buffer" - "C-; b l" "copy file link" - "C-; b P" "copy file path")) + "C-; b g" "revert buffer")) (provide 'custom-buffer-file) |
