init-utils.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;; -*- lexical-binding: t; -*-
  2. ;; This defines the macro after-load, which does stuff after a package is loaded.
  3. (if (fboundp 'with-eval-after-load)
  4. (defalias 'after-load 'with-eval-after-load)
  5. (defmacro after-load (feature &rest body)
  6. "After FEATURE is loaded, evaluate BODY."
  7. (declare (indent defun))
  8. `(eval-after-load ,feature
  9. '(progn ,@body))))
  10. ;;----------------------------------------------------------------------------
  11. ;; Handier way to add modes to auto-mode-alist
  12. ;;----------------------------------------------------------------------------
  13. (defun add-auto-mode (mode &rest patterns)
  14. "Add entries to `auto-mode-alist' to use `MODE' for all given file `PATTERNS'."
  15. (dolist (pattern patterns)
  16. (add-to-list 'auto-mode-alist (cons pattern mode))))
  17. ;;----------------------------------------------------------------------------
  18. ;; String utilities missing from core emacs
  19. ;;----------------------------------------------------------------------------
  20. (defun sanityinc/string-all-matches (regex str &optional group)
  21. "Find all matches for `REGEX' within `STR', returning the full match string or group `GROUP'."
  22. (let ((result nil)
  23. (pos 0)
  24. (group (or group 0)))
  25. (while (string-match regex str pos)
  26. (push (match-string group str) result)
  27. (setq pos (match-end group)))
  28. result))
  29. (defun sanityinc/string-rtrim (str)
  30. "Remove trailing whitespace from `STR'."
  31. (replace-regexp-in-string "[ \t\n]*$" "" str))
  32. ;;----------------------------------------------------------------------------
  33. ;; Find the directory containing a given library
  34. ;;----------------------------------------------------------------------------
  35. (autoload 'find-library-name "find-func")
  36. (defun sanityinc/directory-of-library (library-name)
  37. "Return the directory in which the `LIBRARY-NAME' load file is found."
  38. (file-name-as-directory (file-name-directory (find-library-name library-name))))
  39. ;;----------------------------------------------------------------------------
  40. ;; Delete the current file
  41. ;;----------------------------------------------------------------------------
  42. (defun delete-this-file ()
  43. "Delete the current file, and kill the buffer."
  44. (interactive)
  45. (or (buffer-file-name) (error "No file is currently being edited"))
  46. (when (yes-or-no-p (format "Really delete '%s'?"
  47. (file-name-nondirectory buffer-file-name)))
  48. (delete-file (buffer-file-name))
  49. (kill-this-buffer)))
  50. ;;----------------------------------------------------------------------------
  51. ;; Rename the current file
  52. ;;----------------------------------------------------------------------------
  53. (defun rename-this-file-and-buffer (new-name)
  54. "Renames both current buffer and file it's visiting to NEW-NAME."
  55. (interactive "sNew name: ")
  56. (let ((name (buffer-name))
  57. (filename (buffer-file-name)))
  58. (unless filename
  59. (error "Buffer '%s' is not visiting a file!" name))
  60. (if (get-buffer new-name)
  61. (message "A buffer named '%s' already exists!" new-name)
  62. (progn
  63. (when (file-exists-p filename)
  64. (rename-file filename new-name 1))
  65. (rename-buffer new-name)
  66. (set-visited-file-name new-name)))))
  67. ;;----------------------------------------------------------------------------
  68. ;; Browse current HTML file
  69. ;;----------------------------------------------------------------------------
  70. (defun browse-current-file ()
  71. "Open the current file as a URL using `browse-url'."
  72. (interactive)
  73. (let ((file-name (buffer-file-name)))
  74. (if (tramp-tramp-file-p file-name)
  75. (error "Cannot open tramp file")
  76. (browse-url (concat "file://" file-name)))))
  77. ;;;-------
  78. ;;;Don't require org-mode to load in org files
  79. ;;;-------
  80. (defvar endless/init.org-message-depth 3
  81. "What depth of init.org headers to message at startup.")
  82. (defun my/load-org-file (file)
  83. (with-temp-buffer
  84. (insert-file file)
  85. (goto-char (point-min))
  86. (search-forward-regexp "^\\*")
  87. (while (not (eobp))
  88. (forward-line 1)
  89. (cond
  90. ;; Report Headers
  91. ((looking-at
  92. (format "\\*\\{2,%s\\} +.*$"
  93. endless/init.org-message-depth))
  94. (message "%s" (match-string 0)))
  95. ;; Evaluate Code Blocks
  96. ((looking-at "^#\\+BEGIN_SRC +emacs-lisp *$")
  97. (let ((l (match-end 0)))
  98. (search-forward "\n#+END_SRC")
  99. (eval-region l (match-beginning 0))))
  100. ;; Finish on the next level-1 header
  101. ((looking-at "^\\* ")
  102. (goto-char (point-max)))))))
  103. (provide 'init-utils)