123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- ;; -*- lexical-binding: t; -*-
- ;; This defines the macro after-load, which does stuff after a package is loaded.
- (if (fboundp 'with-eval-after-load)
- (defalias 'after-load 'with-eval-after-load)
- (defmacro after-load (feature &rest body)
- "After FEATURE is loaded, evaluate BODY."
- (declare (indent defun))
- `(eval-after-load ,feature
- '(progn ,@body))))
- ;;----------------------------------------------------------------------------
- ;; Handier way to add modes to auto-mode-alist
- ;;----------------------------------------------------------------------------
- (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))))
- ;;----------------------------------------------------------------------------
- ;; String utilities missing from core emacs
- ;;----------------------------------------------------------------------------
- (defun sanityinc/string-all-matches (regex str &optional group)
- "Find all matches for `REGEX' within `STR', returning the full match string or group `GROUP'."
- (let ((result nil)
- (pos 0)
- (group (or group 0)))
- (while (string-match regex str pos)
- (push (match-string group str) result)
- (setq pos (match-end group)))
- result))
- (defun sanityinc/string-rtrim (str)
- "Remove trailing whitespace from `STR'."
- (replace-regexp-in-string "[ \t\n]*$" "" str))
- ;;----------------------------------------------------------------------------
- ;; Find the directory containing a given library
- ;;----------------------------------------------------------------------------
- (autoload 'find-library-name "find-func")
- (defun sanityinc/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))))
- ;;----------------------------------------------------------------------------
- ;; Delete the current file
- ;;----------------------------------------------------------------------------
- (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)))
- ;;----------------------------------------------------------------------------
- ;; Rename the current file
- ;;----------------------------------------------------------------------------
- (defun rename-this-file-and-buffer (new-name)
- "Renames both current buffer and file it's visiting to NEW-NAME."
- (interactive "sNew name: ")
- (let ((name (buffer-name))
- (filename (buffer-file-name)))
- (unless filename
- (error "Buffer '%s' is not visiting a file!" name))
- (if (get-buffer new-name)
- (message "A buffer named '%s' already exists!" new-name)
- (progn
- (when (file-exists-p filename)
- (rename-file filename new-name 1))
- (rename-buffer new-name)
- (set-visited-file-name new-name)))))
- ;;----------------------------------------------------------------------------
- ;; Browse current HTML file
- ;;----------------------------------------------------------------------------
- (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)))))
- ;;;-------
- ;;;Don't require org-mode to load in org files
- ;;;-------
- (defvar endless/init.org-message-depth 3
- "What depth of init.org headers to message at startup.")
- (defun my/load-org-file (file)
- (with-temp-buffer
- (insert-file file)
- (goto-char (point-min))
- (search-forward-regexp "^\\*")
- (while (not (eobp))
- (forward-line 1)
- (cond
- ;; Report Headers
- ((looking-at
- (format "\\*\\{2,%s\\} +.*$"
- endless/init.org-message-depth))
- (message "%s" (match-string 0)))
- ;; Evaluate Code Blocks
- ((looking-at "^#\\+BEGIN_SRC +emacs-lisp *$")
- (let ((l (match-end 0)))
- (search-forward "\n#+END_SRC")
- (eval-region l (match-beginning 0))))
- ;; Finish on the next level-1 header
- ((looking-at "^\\* ")
- (goto-char (point-max)))))))
- (provide 'init-utils)
|