2017-12-03 09:19:05 +01:00
|
|
|
|
;;; db-utils.el --- Utility Functions for Daniel's Emacs Configuration -*- lexical-binding: t -*-
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;
|
|
|
|
|
;; Some functions used in my ~/.emacs.d/init.el. Most of them are copied from
|
|
|
|
|
;; various sources around the internet.
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2017-11-16 19:32:56 +01:00
|
|
|
|
(require 'dash)
|
|
|
|
|
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
;;; application shortcuts
|
|
|
|
|
|
|
|
|
|
(defun db/run-or-hide-ansi-term ()
|
|
|
|
|
"Find `*ansi-term*' or run `ansi-term' with `explicit-shell-file-name'.
|
|
|
|
|
If already in `*ansi-term*' buffer, bury it."
|
|
|
|
|
(interactive)
|
|
|
|
|
(if (string= "term-mode" major-mode)
|
|
|
|
|
(bury-buffer)
|
|
|
|
|
(if (get-buffer "*ansi-term*")
|
|
|
|
|
(switch-to-buffer "*ansi-term*")
|
|
|
|
|
(ansi-term explicit-shell-file-name))))
|
|
|
|
|
|
|
|
|
|
(defun db/gnus ()
|
|
|
|
|
"Switch to the `*Group*' buffer, starting `gnus' if not existent."
|
|
|
|
|
(interactive)
|
|
|
|
|
(require 'gnus)
|
|
|
|
|
(if (get-buffer "*Group*")
|
|
|
|
|
(switch-to-buffer "*Group*")
|
|
|
|
|
(gnus)))
|
|
|
|
|
|
|
|
|
|
(defun db/org-agenda ()
|
|
|
|
|
"Show the main `org-agenda'."
|
|
|
|
|
(interactive)
|
|
|
|
|
(org-agenda nil "A"))
|
|
|
|
|
|
|
|
|
|
(defun db/scratch ()
|
|
|
|
|
"Switch to `*scratch*'."
|
|
|
|
|
(interactive)
|
|
|
|
|
(switch-to-buffer "*scratch*"))
|
|
|
|
|
|
|
|
|
|
(defun db/find-user-init-file ()
|
|
|
|
|
"Edit `user-init-file'."
|
|
|
|
|
(interactive)
|
|
|
|
|
(find-file user-init-file))
|
|
|
|
|
|
|
|
|
|
(defun db/run-or-hide-eshell (arg)
|
|
|
|
|
"Opens an eshell buffer if not already in one, and otherwise
|
|
|
|
|
returns to where we have been before."
|
|
|
|
|
;; idea to split the current window is from
|
|
|
|
|
;; http://howardism.org/Technical/Emacs/eshell-fun.html
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(if (string= "eshell-mode" major-mode)
|
|
|
|
|
;; bury buffer; reopen with current working directory if arg is given
|
|
|
|
|
(progn
|
|
|
|
|
(bury-buffer)
|
|
|
|
|
(delete-window)
|
|
|
|
|
(and arg (db/run-or-hide-eshell arg)))
|
|
|
|
|
(if-let ((eshell-window (db/find-window-by-buffer-mode 'eshell-mode)))
|
|
|
|
|
(select-window eshell-window)
|
|
|
|
|
;; open eshell
|
2018-01-16 18:20:24 +01:00
|
|
|
|
(let ((current-dir (expand-file-name default-directory))
|
2017-07-16 18:07:00 +02:00
|
|
|
|
(height (/ (window-total-height) 3)))
|
|
|
|
|
(split-window-vertically (- height))
|
|
|
|
|
(other-window 1)
|
|
|
|
|
(eshell 1)
|
|
|
|
|
(when arg
|
|
|
|
|
(end-of-line)
|
|
|
|
|
(eshell-kill-input)
|
|
|
|
|
(insert (format "cd %s" current-dir))
|
|
|
|
|
(eshell-send-input))))))
|
|
|
|
|
|
2018-01-16 18:20:24 +01:00
|
|
|
|
(defun db/run-or-hide-shell ()
|
2017-07-16 18:07:00 +02:00
|
|
|
|
"Opens an shell buffer if not already in one, and otherwise
|
|
|
|
|
returns to where we have been before."
|
2018-01-16 18:20:24 +01:00
|
|
|
|
(interactive "")
|
2017-07-16 18:07:00 +02:00
|
|
|
|
(if (string= "shell-mode" major-mode)
|
|
|
|
|
(progn
|
|
|
|
|
(bury-buffer)
|
|
|
|
|
(other-window -1))
|
|
|
|
|
(shell)))
|
|
|
|
|
|
|
|
|
|
|
2017-11-18 11:11:37 +01:00
|
|
|
|
;;; general utilities
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
(defun db/get-url-from-link ()
|
|
|
|
|
"Copy url of link under point into clipboard."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((url (plist-get (text-properties-at (point)) 'help-echo)))
|
|
|
|
|
(if url
|
|
|
|
|
(kill-new url)
|
|
|
|
|
(error "No link found."))))
|
|
|
|
|
|
|
|
|
|
(defun db/test-emacs ()
|
|
|
|
|
;; from oremacs
|
|
|
|
|
"Test whether emacs' configuration is not throwing any errors."
|
|
|
|
|
(interactive)
|
|
|
|
|
(require 'async)
|
|
|
|
|
(async-start
|
|
|
|
|
(lambda () (shell-command-to-string
|
|
|
|
|
"emacs --batch --eval \"
|
|
|
|
|
(condition-case e
|
|
|
|
|
(progn
|
|
|
|
|
(load \\\"~/.emacs.d/init.el\\\")
|
|
|
|
|
(message \\\"-OK-\\\"))
|
|
|
|
|
(error
|
|
|
|
|
(message \\\"ERROR!\\\")
|
|
|
|
|
(signal (car e) (cdr e))))\""))
|
|
|
|
|
`(lambda (output)
|
|
|
|
|
(if (string-match "-OK-" output)
|
|
|
|
|
(when ,(called-interactively-p 'any)
|
|
|
|
|
(message "All is well"))
|
|
|
|
|
(switch-to-buffer-other-window "*startup error*")
|
|
|
|
|
(delete-region (point-min) (point-max))
|
|
|
|
|
(insert output)
|
|
|
|
|
(search-backward "ERROR!")))))
|
|
|
|
|
|
|
|
|
|
(defun db/isearch-forward-symbol-with-prefix (p)
|
|
|
|
|
;; http://endlessparentheses.com/quickly-search-for-occurrences-of-the-symbol-at-point.html
|
|
|
|
|
"Like `isearch-forward', unless prefix argument is provided.
|
|
|
|
|
With a prefix argument P, isearch for the symbol at point."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(let ((current-prefix-arg nil))
|
|
|
|
|
(call-interactively
|
|
|
|
|
(if p
|
|
|
|
|
#'isearch-forward-symbol-at-point
|
|
|
|
|
#'isearch-forward))))
|
|
|
|
|
|
|
|
|
|
(defun endless/fill-or-unfill ()
|
|
|
|
|
"Like `fill-paragraph', but unfill if used twice."
|
|
|
|
|
;; http://endlessparentheses.com/fill-and-unfill-paragraphs-with-a-single-key.html
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((fill-column
|
|
|
|
|
(if (eq last-command 'endless/fill-or-unfill)
|
|
|
|
|
(progn (setq this-command nil)
|
|
|
|
|
(point-max))
|
|
|
|
|
fill-column)))
|
|
|
|
|
(call-interactively #'fill-paragraph)))
|
|
|
|
|
|
|
|
|
|
(defun db/delete-trailing-whitespace-maybe ()
|
|
|
|
|
"Call `delete-trailing-whitespace', but not in `message-mode'."
|
|
|
|
|
(unless (derived-mode-p 'message-mode)
|
|
|
|
|
(delete-trailing-whitespace)))
|
|
|
|
|
|
|
|
|
|
(defun db/find-window-by-buffer-mode (mode)
|
|
|
|
|
"Return first window in current frame displaying a buffer with
|
|
|
|
|
major mode MODE."
|
|
|
|
|
(cl-find-if (lambda (window)
|
|
|
|
|
(with-current-buffer (window-buffer window)
|
|
|
|
|
(eq major-mode mode)))
|
|
|
|
|
(window-list-1)))
|
|
|
|
|
|
|
|
|
|
(defun db/show-current-org-task ()
|
|
|
|
|
"Show title of currently clock in task in modeline."
|
|
|
|
|
(interactive)
|
|
|
|
|
(message org-clock-current-task))
|
|
|
|
|
|
2017-11-16 19:32:56 +01:00
|
|
|
|
(defun db/hex-to-ascii (hex-string)
|
|
|
|
|
"Convert HEX-STRING to its ASCII equivalent."
|
|
|
|
|
;; https://stackoverflow.com/questions/12003231/how-do-i-convert-a-string-of-hex-into-ascii-using-elisp
|
2017-11-18 11:25:55 +01:00
|
|
|
|
(interactive "sString (hex): ")
|
2017-11-16 19:32:56 +01:00
|
|
|
|
(->> (string-to-list hex-string)
|
|
|
|
|
(-partition 2)
|
|
|
|
|
(--map (string-to-number (concat it) 16))
|
2017-11-18 11:25:55 +01:00
|
|
|
|
concat
|
|
|
|
|
message))
|
2017-11-16 19:32:56 +01:00
|
|
|
|
|
2017-11-20 19:52:39 +01:00
|
|
|
|
(defun db/ascii-to-hex (ascii-string)
|
|
|
|
|
"Convert ASCII-STRING to its hexadecimal representation."
|
|
|
|
|
(interactive "sString (ascii): ")
|
2017-11-20 20:20:47 +01:00
|
|
|
|
(->> (--map (format "%2X" it) ascii-string)
|
2017-11-20 19:52:39 +01:00
|
|
|
|
(apply #'concat)
|
|
|
|
|
message))
|
|
|
|
|
|
2017-11-18 11:11:48 +01:00
|
|
|
|
(defun db/ntp-to-time (high low &optional format-string)
|
|
|
|
|
"Format NTP time given by HIGH and LOW (both integer) to time as given by FORMAT-STRING.
|
|
|
|
|
If not given, FORMAT-STRING defaults to some ISO 8601-like format."
|
2017-11-18 11:25:55 +01:00
|
|
|
|
(interactive
|
|
|
|
|
(list (string-to-number (read-string "High (hex): ") 16)
|
|
|
|
|
(string-to-number (read-string "Log (hex): ") 16)))
|
2017-11-18 11:11:48 +01:00
|
|
|
|
(let* ((high-seconds (- high 2208992400)) ; subtract seconds between 1900-01-01 and the epoch
|
|
|
|
|
(h (lsh high-seconds -16))
|
2017-11-18 15:14:09 +01:00
|
|
|
|
(l (% high-seconds 65536))
|
2017-11-18 11:11:48 +01:00
|
|
|
|
(u (floor (* (/ low 4294967296.0) 1e6)))
|
|
|
|
|
(p (- low (floor (/ (* u 4294967296) 1e6)))))
|
2017-11-18 11:25:55 +01:00
|
|
|
|
(message
|
|
|
|
|
(format-time-string (or format-string "%Y-%m-%dT%H:%M:%S.%9NZ")
|
|
|
|
|
(list h l u p)))))
|
2017-11-18 11:11:48 +01:00
|
|
|
|
|
2018-01-13 14:19:12 +01:00
|
|
|
|
(defun conditionally-enable-lispy ()
|
|
|
|
|
"Enable lispy-mode when in `eval-expression’ or in
|
|
|
|
|
`pp-eval-expression’. lispy must have been loaded for this
|
|
|
|
|
first, i.e., this function will not automatically load
|
|
|
|
|
lispy."
|
|
|
|
|
(when (and (featurep 'lispy)
|
|
|
|
|
(or (eq this-command 'eval-expression)
|
|
|
|
|
(eq this-command 'pp-eval-expression)))
|
|
|
|
|
(lispy-mode 1)))
|
|
|
|
|
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
;;; helm configuration
|
|
|
|
|
|
|
|
|
|
(defcustom db/helm-frequently-used-features
|
2017-10-27 21:46:50 +02:00
|
|
|
|
'(("Mail" . db/gnus)
|
|
|
|
|
("Agenda" . db/org-agenda)
|
|
|
|
|
("Init File" . db/find-user-init-file)
|
|
|
|
|
("EMMS" . emms)
|
|
|
|
|
("Gnus" . (lambda ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(find-file gnus-init-file)))
|
|
|
|
|
("Shell" . shell)
|
|
|
|
|
("EShell" . eshell)
|
|
|
|
|
("scratch" . db/scratch))
|
|
|
|
|
"Helm shortcuts for frequently used features."
|
|
|
|
|
:group 'personal-settings
|
|
|
|
|
:type '(alist :key-type string :value-type sexp))
|
|
|
|
|
|
2017-10-27 21:57:27 +02:00
|
|
|
|
(defvar db/helm-source-frequently-used-features
|
2017-07-16 18:07:00 +02:00
|
|
|
|
'((name . "Frequently Used")
|
2017-10-27 21:46:50 +02:00
|
|
|
|
(candidates . db/helm-frequently-used-features)
|
2017-07-16 18:07:00 +02:00
|
|
|
|
(action . (("Open" . funcall)))
|
|
|
|
|
(filtered-candidate-transformer . helm-adaptive-sort))
|
2017-10-27 21:46:50 +02:00
|
|
|
|
"Helm source for `db/helm-frequently-used-features’.")
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
(defcustom db/helm-frequently-visited-locations
|
2017-10-27 21:46:50 +02:00
|
|
|
|
'(("db-utils" . "~/.emacs.d/site-lisp/db-utils.el")
|
|
|
|
|
("db-org" . "~/.emacs.d/site-lisp/db-org.el")
|
|
|
|
|
("db-private" . "~/.emacs.d/site-lisp/db-private.el")
|
|
|
|
|
("notes" . "~/Documents/home/notes.org")
|
|
|
|
|
("pensieve" . "~/Documents/home/pensieve.org.gpg")
|
|
|
|
|
("things (home)" . "~/Documents/home/admin/things.gpg")
|
|
|
|
|
("things (work)" . "~/Documents/uni/admin/misc/things.gpg"))
|
2017-07-16 18:07:00 +02:00
|
|
|
|
"Helm shortcuts to frequentely visited locations"
|
|
|
|
|
:group 'personal-settings
|
2017-10-27 21:46:50 +02:00
|
|
|
|
:type '(alist :key-type string :value-type sexp))
|
|
|
|
|
|
2017-10-27 21:57:27 +02:00
|
|
|
|
(defvar db/helm-source-frequently-visited-locations
|
2017-10-27 21:46:50 +02:00
|
|
|
|
'((name . "Locations")
|
|
|
|
|
(candidates . db/helm-frequently-visited-locations)
|
2017-10-30 20:03:42 +01:00
|
|
|
|
(action . (("Open" . (lambda (entry)
|
|
|
|
|
(if (consp entry)
|
|
|
|
|
(funcall (car entry) (cdr entry))
|
|
|
|
|
(find-file entry))))))
|
2017-10-27 21:46:50 +02:00
|
|
|
|
(filtered-candidate-transformer . helm-adaptive-sort)))
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
2017-10-27 21:48:46 +02:00
|
|
|
|
(defcustom db/important-documents-path "~/Documents/library/"
|
2017-10-27 19:39:45 +02:00
|
|
|
|
"Path of important documents."
|
|
|
|
|
:group 'personal-settings
|
|
|
|
|
:type 'string)
|
|
|
|
|
|
|
|
|
|
(defun db/important-documents ()
|
2017-10-27 20:47:34 +02:00
|
|
|
|
"Recursively return paths of all files found in `db/important-documents-path’.
|
|
|
|
|
The result will be a list of cons cells, where the car is the
|
|
|
|
|
path relative to `db/important-documents’ and the cdr is the full
|
|
|
|
|
path."
|
|
|
|
|
;; code adapted from `directory-files-recursively’
|
2017-10-27 21:57:02 +02:00
|
|
|
|
(let ((db/important-documents-path (expand-file-name db/important-documents-path)))
|
|
|
|
|
(cl-labels ((all-files-in-dir (dir)
|
|
|
|
|
(let ((result nil)
|
|
|
|
|
(files nil))
|
|
|
|
|
(dolist (file (sort (file-name-all-completions "" dir)
|
|
|
|
|
'string<))
|
|
|
|
|
(unless (eq ?. (aref file 0)) ; omit hidden files
|
|
|
|
|
(if (directory-name-p file)
|
|
|
|
|
(let* ((leaf (substring file 0 (1- (length file))))
|
|
|
|
|
(full-file (expand-file-name leaf dir)))
|
|
|
|
|
;; Don't follow symlinks to other directories.
|
|
|
|
|
(unless (file-symlink-p full-file)
|
|
|
|
|
(setq result
|
|
|
|
|
(nconc result (all-files-in-dir full-file)))))
|
|
|
|
|
(push (cons
|
|
|
|
|
(string-remove-prefix db/important-documents-path
|
|
|
|
|
(expand-file-name file dir))
|
|
|
|
|
(expand-file-name file dir))
|
|
|
|
|
files))))
|
|
|
|
|
(nconc result (nreverse files)))))
|
|
|
|
|
(when (file-directory-p db/important-documents-path)
|
|
|
|
|
(all-files-in-dir db/important-documents-path)))))
|
2017-10-27 19:39:45 +02:00
|
|
|
|
|
|
|
|
|
(defun db/system-open (path)
|
|
|
|
|
"Open PATH with default program as defined by the underlying system."
|
2018-01-16 08:44:44 +01:00
|
|
|
|
(cond
|
|
|
|
|
((eq system-type 'windows-nt)
|
|
|
|
|
(w32-shell-execute "open" path))
|
|
|
|
|
((eq system-type 'cygwin)
|
|
|
|
|
(start-process "" nil "cygstart" path))
|
|
|
|
|
(t
|
|
|
|
|
(start-process "" nil "xdg-open" path))))
|
2017-10-27 19:39:45 +02:00
|
|
|
|
|
2017-10-27 21:57:27 +02:00
|
|
|
|
(defvar db/helm-source-important-documents
|
|
|
|
|
'((name . "Important files")
|
2017-10-27 19:39:45 +02:00
|
|
|
|
(candidates . db/important-documents)
|
2017-10-27 20:53:37 +02:00
|
|
|
|
(action . (("Open externally" . db/system-open)
|
|
|
|
|
("Find file" . find-file))))
|
2017-10-27 21:57:27 +02:00
|
|
|
|
"Helm source for important documents.")
|
2017-10-27 19:39:45 +02:00
|
|
|
|
|
2017-11-25 20:45:59 +01:00
|
|
|
|
(defun db/helm-shortcuts (arg)
|
2017-10-27 19:39:45 +02:00
|
|
|
|
"Open helm completion on common locations."
|
2017-11-25 20:45:59 +01:00
|
|
|
|
(interactive "p")
|
2017-10-27 19:39:45 +02:00
|
|
|
|
(require 'helm-files)
|
2017-10-27 21:54:20 +02:00
|
|
|
|
(require 'helm-bookmark)
|
2017-10-27 21:57:27 +02:00
|
|
|
|
(helm :sources `(db/helm-source-frequently-used-features
|
|
|
|
|
db/helm-source-frequently-visited-locations
|
2017-11-25 20:45:59 +01:00
|
|
|
|
,(when (and (= arg 4)
|
|
|
|
|
(file-directory-p db/important-documents-path))
|
2017-10-27 21:57:27 +02:00
|
|
|
|
'db/helm-source-important-documents)
|
2018-07-14 12:07:01 +02:00
|
|
|
|
,(when (package-installed-p 'helm-eww)
|
2018-07-20 16:24:34 +02:00
|
|
|
|
(require 'helm-eww)
|
2018-07-14 12:07:01 +02:00
|
|
|
|
(helm-eww-bookmarks-build-source))
|
2017-10-27 19:43:48 +02:00
|
|
|
|
helm-source-bookmarks
|
|
|
|
|
helm-source-bookmark-set)))
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
|
2017-11-16 19:32:56 +01:00
|
|
|
|
;;; Org Utilities
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
(defun db/org-cleanup-continuous-clocks ()
|
|
|
|
|
"Join continuous clock lines in the current buffer."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let* ((inactive-timestamp (org-re-timestamp 'inactive))
|
|
|
|
|
(clock-line (concat "\\(^ *\\)CLOCK: " inactive-timestamp "--" inactive-timestamp " => .*"
|
|
|
|
|
"\n"
|
|
|
|
|
" *CLOCK: " inactive-timestamp "--\\[\\2\\] => .*$")))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(while (search-forward-regexp clock-line nil t)
|
|
|
|
|
(replace-match "\\1CLOCK: [\\4]--[\\3]")
|
|
|
|
|
(org-clock-update-time-maybe)))))
|
|
|
|
|
|
|
|
|
|
|
2018-01-13 18:29:56 +01:00
|
|
|
|
;;; End
|
2017-07-16 18:07:00 +02:00
|
|
|
|
|
|
|
|
|
(provide 'db-utils)
|
|
|
|
|
|
|
|
|
|
;;; db-utils.el ends here
|