dotfiles / .emacs.d / ag-functions.el

;; TODO: unused?
(defun kill-whole-line ()
  (move-beginning-of-line 1)
  (kill-line 1)

;; TODO: annoying, get rid of it.
(defun diff-buffer-with-associated-file ()
  "View the differences between BUFFER and its associated file.
This requires the external program \"diff\" to be in your `exec-path'. 
Returns nil if no differences found, 't otherwise."
  (let ((buf-filename buffer-file-name)
        (buffer (current-buffer)))
    (unless buf-filename
      (error "Buffer %s has no associated file" buffer))
    (let ((diff-buf (get-buffer-create
                     (concat "*Assoc file diff: "
      (with-current-buffer diff-buf
        (setq buffer-read-only nil)
      (let ((tempfile (make-temp-file "buffer-to-file-diff-")))
              (with-current-buffer buffer
                (write-region (point-min) (point-max) tempfile nil 'nomessage))
              (if (zerop
                   (apply #'call-process "diff" nil diff-buf nil
                           (when (and (boundp 'ediff-custom-diff-options)
                                      (stringp ediff-custom-diff-options))
                             (list ediff-custom-diff-options))
                           (list buf-filename tempfile))))
                    (message "No differences found")
                  (with-current-buffer diff-buf
                    (goto-char (point-min))
                    (if (fboundp 'diff-mode)
                  (display-buffer diff-buf)
          (when (file-exists-p tempfile)
            (delete-file tempfile)))))))

;; tidy up diffs when closing the file
(defun kill-associated-diff-buf ()
  (let ((buf (get-buffer (concat "*Assoc file diff: "
    (when (bufferp buf)
      (kill-buffer buf))))

(add-hook 'kill-buffer-hook 'kill-associated-diff-buf)

(defun de-context-kill (arg)
  "Kill buffer, taking gnuclient into account."
  (interactive "p")
  (when (and (buffer-modified-p)
             (not (string-match "\\*.*\\*" (buffer-name)))
             ;; erc buffers will be automatically saved
             (not (eq major-mode 'erc-mode))
             (= 1 arg))
    (let ((differences 't))
    (when (file-exists-p buffer-file-name)
      (setq differences (diff-buffer-with-associated-file)))
    (error (if differences 
               "Buffer has unsaved changes"
             "Buffer has unsaved changes, but no differences wrt. the file"))))
  (if (and (boundp 'gnuserv-minor-mode)
    (set-buffer-modified-p nil)
    (kill-buffer (current-buffer))))

(fset 'lparen
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("\\left(" 0 "%d")) arg)))
(fset 'rparen
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("\\right)" 0 "%d")) arg)))

(defun my-list-buffers (&optional files-only)
  "Opens list-buffers and put focus on it"
  (interactive "P")
  (let ((b (list-buffers-noselect files-only)))
    (display-buffer b)
    (pop-to-buffer b)

(defun really-revert-buffer ()
  "Revert the buffer without asking for confirmation."
  (revert-buffer t t t))

(defun close-project (project)
  "Filesets never seems to work."
  (interactive "P")
  (dolist (buffer (buffer-list))
    (with-current-buffer buffer
      (if (and (buffer-file-name)
               (string-match project (buffer-file-name)))
          (kill-buffer buffer)))))

(defun my-clean-line (foo)
  (interactive "P")
  (move-end-of-line nil)

(defun insert-time-stamp ()
  (insert-and-inherit (time-stamp-yyyy/mm/dd) " - "(time-stamp-hh:mm:ss))

(provide 'ag-functions)