Skip to content

Latest commit

 

History

History
113 lines (89 loc) · 3.6 KB

init-defuns.org

File metadata and controls

113 lines (89 loc) · 3.6 KB

emacs.d - Defuns

Abstract

Here we define utility functions we’ll use in both subsequent configuration, and interactively.

add-auto-mode

This function adds a more convenient way of defining a list of file patterns that should be attributed to a given mode.

(defun add-auto-mode (mode &rest patterns)
  "Add entries to `auto-mode-alist' to use `MODE' for all given file
`PATTERNS'."
  (dolist (pattern patterns)
    (add-to-list 'auto-mode-alist (cons pattern mode))))

after-load

This function evaluates of body of Emacs Lisp after a named feature has been loaded. By deferring execution of some code we benefit in two ways:

  1. Emacs can start faster by only running the code necessary to get started.
  2. We can avoid executing code that has a dependency yet to be loaded.
(defmacro after-load (feature &rest body)
  "After FEATURE is loaded, evaluate BODY."
  (declare (indent defun))
  `(eval-after-load ,feature
     '(progn ,@body)))

browse-current-file

This function opens the current file using browse-url. I typically use gf via Evil making this somewhat redundent.

(defun browse-current-file ()
  "Open the current file as a URL using `browse-url'."
  (interactive)
  (let ((file-name (buffer-file-name)))
    (if (tramp-tramp-file-p file-name)
        (error "Cannot open tramp file")
      (browse-url (concat "file://" file-name)))))

delete-this-file

This function deletes a file, removing the buffer in the process.

(defun delete-this-file ()
  "Delete the current file, and kill the buffer."
  (interactive)
  (or (buffer-file-name) (error "No file is currently being edited"))
  (when (yes-or-no-p (format "Really delete '%s'?"
                             (file-name-nondirectory buffer-file-name)))
    (delete-file (buffer-file-name))
    (kill-this-buffer)))

jcf-directory-of-library

This function will provide you with the directory containing the named library.

(autoload 'find-library-name "find-func")
(defun jcf-directory-of-library (library-name)
  "Return the directory in which the `LIBRARY-NAME' load file is found."
  (file-name-as-directory
   (file-name-directory (find-library-name library-name))))

jcf-log-startup-time

(defun jcf-log-startup-time ()
  (message "init completed in %.2fms"
           (jcf-time-subtract-millis after-init-time before-init-time)))

rename-buffer-and-file

This function renames the current buffer, and the underlying file if it has been written to disk.

This is a modified version of the ~prelude-rename-buffer-and-file~ function in Emacs Prelude.

(defun rename-buffer-and-file ()
  "Rename current buffer and if the buffer is visiting a file, rename it too."
  (interactive)
  (let ((filename (buffer-file-name)))
    (if (not (and filename (file-exists-p filename)))
        (rename-buffer (read-from-minibuffer "New name: " (buffer-name)))
      (let ((new-name (read-file-name "New name: ")))
        (cond
         ((vc-backend filename) (vc-rename-file filename new-name))
         (t
          (rename-file filename new-name t)
          (set-visited-file-name new-name t t)))))))