Emacs personal configuration
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

88 lines
3.4KB

  1. (defmacro after-load (feature &rest body)
  2. "After FEATURE is loaded, evaluate BODY."
  3. (declare (indent defun))
  4. `(eval-after-load ,feature
  5. '(progn ,@body)))
  6. ;;----------------------------------------------------------------------------
  7. ;; Handier way to add modes to auto-mode-alist
  8. ;;----------------------------------------------------------------------------
  9. (defun add-auto-mode (mode &rest patterns)
  10. "Add entries to `auto-mode-alist' to use `MODE' for all given file `PATTERNS'."
  11. (dolist (pattern patterns)
  12. (add-to-list 'auto-mode-alist (cons pattern mode))))
  13. ;;----------------------------------------------------------------------------
  14. ;; String utilities missing from core emacs
  15. ;;----------------------------------------------------------------------------
  16. (defun sanityinc/string-all-matches (regex str &optional group)
  17. "Find all matches for `REGEX' within `STR', returning the full match string or group `GROUP'."
  18. (let ((result nil)
  19. (pos 0)
  20. (group (or group 0)))
  21. (while (string-match regex str pos)
  22. (push (match-string group str) result)
  23. (setq pos (match-end group)))
  24. result))
  25. (defun sanityinc/string-rtrim (str)
  26. "Remove trailing whitespace from `STR'."
  27. (replace-regexp-in-string "[ \t\n]*$" "" str))
  28. ;;----------------------------------------------------------------------------
  29. ;; Find the directory containing a given library
  30. ;;----------------------------------------------------------------------------
  31. (autoload 'find-library-name "find-func")
  32. (defun sanityinc/directory-of-library (library-name)
  33. "Return the directory in which the `LIBRARY-NAME' load file is found."
  34. (file-name-as-directory (file-name-directory (find-library-name library-name))))
  35. ;;----------------------------------------------------------------------------
  36. ;; Delete the current file
  37. ;;----------------------------------------------------------------------------
  38. (defun delete-this-file ()
  39. "Delete the current file, and kill the buffer."
  40. (interactive)
  41. (or (buffer-file-name) (error "No file is currently being edited"))
  42. (when (yes-or-no-p (format "Really delete '%s'?"
  43. (file-name-nondirectory buffer-file-name)))
  44. (delete-file (buffer-file-name))
  45. (kill-this-buffer)))
  46. ;;----------------------------------------------------------------------------
  47. ;; Rename the current file
  48. ;;----------------------------------------------------------------------------
  49. (defun rename-this-file-and-buffer (new-name)
  50. "Renames both current buffer and file it's visiting to NEW-NAME."
  51. (interactive "sNew name: ")
  52. (let ((name (buffer-name))
  53. (filename (buffer-file-name)))
  54. (unless filename
  55. (error "Buffer '%s' is not visiting a file!" name))
  56. (if (get-buffer new-name)
  57. (message "A buffer named '%s' already exists!" new-name)
  58. (progn
  59. (when (file-exists-p filename)
  60. (rename-file filename new-name 1))
  61. (rename-buffer new-name)
  62. (set-visited-file-name new-name)))))
  63. ;;----------------------------------------------------------------------------
  64. ;; Browse current HTML file
  65. ;;----------------------------------------------------------------------------
  66. (defun browse-current-file ()
  67. "Open the current file as a URL using `browse-url'."
  68. (interactive)
  69. (let ((file-name (buffer-file-name)))
  70. (if (tramp-tramp-file-p file-name)
  71. (error "Cannot open tramp file")
  72. (browse-url (concat "file://" file-name)))))
  73. (provide 'init-utils)