Source

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

Full commit
;; TODO: unused?
(defun kill-whole-line ()
  (interactive)
  (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."
  (interactive)
  (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: "
                             (buffer-name)
                             "*"))))
      (with-current-buffer diff-buf
        (setq buffer-read-only nil)
        (erase-buffer))
      (let ((tempfile (make-temp-file "buffer-to-file-diff-")))
        (unwind-protect
            (progn
              (with-current-buffer buffer
                (write-region (point-min) (point-max) tempfile nil 'nomessage))
              (if (zerop
                   (apply #'call-process "diff" nil diff-buf nil
                          (append
                           (when (and (boundp 'ediff-custom-diff-options)
                                      (stringp ediff-custom-diff-options))
                             (list ediff-custom-diff-options))
                           (list buf-filename tempfile))))
                  (progn
                    (message "No differences found")
                    nil)
                (progn
                  (with-current-buffer diff-buf
                    (goto-char (point-min))
                    (if (fboundp 'diff-mode)
                        (diff-mode)
                      (fundamental-mode)))
                  (display-buffer diff-buf)
                  t)))
          (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: "
                             (buffer-name)
                             "*"))))
    (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)
             buffer-file-name
             (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)
             gnuserv-minor-mode)
      (gnuserv-edit)
    (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)
    (delete-other-windows)))


(defun really-revert-buffer ()
  "Revert the buffer without asking for confirmation."
  (interactive)
  (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")
  (indent-for-tab-command)
  (move-end-of-line nil)
  (delete-horizontal-space)
  (next-logical-line))

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

(provide 'ag-functions)