aboutsummaryrefslogtreecommitdiffstats
path: root/local-lib/zzq-funcs.el
blob: 400171d6e14df41d252f23e0d6c1925a83f909d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
;;; zzq-funcs.el --- miscellaneous functions
;; Author: jason <jason@zzq.org>
;; Created: 15 Jun 2017
;; X-URL: https://beehive.io/jason/emacs.d/
;;; Commentary:
;;
;; Contains miscellaneous functions I've written or collected.
;;
;;; Code:
(defun zzq/time-to-sec (time)
  "Convert the output of (current-time) to epoch in seconds"
  (truncate (+ (+ (* (nth 0 time) (expt 2 16)) (nth 1 time)) (* (nth 2 time) (expt 10 -6)))))
(defun zzq/time-to-msec (time)
  "Convert the output of (current-time) to epoch in milliseconds"
  (truncate (* 1000 (+ (+ (* (nth 0 time) (expt 2 16)) (nth 1 time)) (* (nth 2 time) (expt 10 -6))))))

(defun kill-whitespace ()
  "Remove all whitepspace seperating two non-whitespace characters"
  (interactive "*")
  (save-excursion
    (save-restriction
      (progn
	;; Move backward to the last non-whitespace character
	(re-search-backward "[^ \t\r\n]" nil)
	;; Then search for all whitespace to the next non-whitespace character
	(re-search-forward "[ \t\r\n]+")
	;; Replace everything from the last match (all whitespace) with nothing
	(replace-match "")))))

(defun maybe-backward-kill-word (arg)
  "Removes all whitespace backward up to the next non-whitespace character. If
the first character left of the cursor is not whitespace then remove the word. I
like to bind  this to C-backspace."
  (interactive "p")
  (if (looking-back "[ \t\r\n]")
      (let ((start (point)))
	(re-search-backward "[^ \t\r\n]" nil)
	(forward-char 1)
	(delete-region (point) start))
    (backward-kill-word arg)))

(defun maybe-kill-word (arg)
  "Removes all whitespace up to the next non-whitespace character. If the first
character right of the cursor is not whitespace then remove the word. I like to
bind this to C-delete and M-d"
  (interactive "p")
  (if (looking-at "[ \t\r\n]")
      (let ((start (point)))
	(re-search-forward "[ \t\r\n]+" nil)
	(delete-region (point) start))
    (kill-word arg)))


(provide 'zzq-funcs)