-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmy-debug.el
More file actions
133 lines (116 loc) · 4.63 KB
/
my-debug.el
File metadata and controls
133 lines (116 loc) · 4.63 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
;;; my-debug --- debug helper functions
;;
;;; Commentary:
;;
;; This is not included normally and has to be loaded manually
;;
;;; Code:
(defun my-check-for-subdir (filename newdir)
"Check to see if newdir is bellow where filename is in the heirachy"
(let ((dir (file-name-directory filename)))
(> (length newdir) (length dir))))
;; (my-check-for-subdir "/this/is/a.file" "/this/is/in/a/subdir")
;; (my-check-for-subdir "/this/is/a.file" "/this/is/")
(defun my-default-dir-watcher (symbol newval operation where)
(when (and where (eq operation 'set))
(let ((name (buffer-name where))
(path (buffer-file-name where)))
(message "default-directory set to: %s in %s" newval name)
(when (and path
(my-check-for-subdir (buffer-file-name) newval))
(message "%s not in %s\n%s" newval path
(catch 'catcher
(throw 'catcher
(with-temp-buffer
(backtrace)
(buffer-string)))))))))
;; (add-variable-watcher 'default-directory 'my-default-dir-watcher)
;; (remove-variable-watcher 'default-directory 'my-default-dir-watcher)
(defun my-add-dir-watcher-for-this-buffer ()
"Add a watcher to current-buffer for when default-directory changes
from it's current value."
(interactive)
(let ((orig-dir default-directory)
(buf (current-buffer)))
(add-variable-watcher
'default-directory
(lambda (symbol newval operation where)
(when (and (not (eq operation 'let))
(eq buf (current-buffer))
(not (string-equal orig-dir newval)))
(message "%s default-directory to %s (from %s) in %s"
operation newval orig-dir (current-buffer))
(backtrace))))))
(defun my-reset-dir-watchers ()
"Remove all default-directory watchers."
(interactive)
(--each
(get-variable-watchers 'default-directory)
(remove-variable-watcher 'default-directory it)))
;; Total buffer size
(defun my-debug-total-buffer-size ()
(let ((stext 0))
(dolist (b (buffer-list))
(setq stext (+ stext (buffer-size))))
stext))
;;
(defun my-dump-window-tree (window indent)
"Recursively walk the window tree and print its structure."
(let* ((is-internal (window-child window))
(edges (window-pixel-edges window))
(width (- (nth 2 edges) (nth 0 edges)))
(height (- (nth 3 edges) (nth 1 edges)))
(type (cond ((window-combined-p window) "Vertical Split")
((window-combined-p window t) "Horizontal Split")
(t "Root/Leaf"))))
;; Print current window info
(insert (format "%s%s [%dx%d] %s\n"
indent
(if is-internal "Node:" "Leaf:")
width height
(if is-internal
(format "(%s)" type)
(format "Buffer: %s" (buffer-name (window-buffer window))))))
;; Recurse into children if they exist
(let ((child (window-child window)))
(while child
(my-dump-window-tree child (concat indent " │ "))
(setq child (window-right child))))))
;; Execute the dump
(defun my-dump-current-window-tree ()
(interactive)
(with-current-buffer (get-buffer-create "*Window-Tree-Dump*")
(erase-buffer)
(insert "Emacs Window Tree Layout:\n")
(insert "=========================\n")
(my-dump-window-tree (frame-root-window) "")
(display-buffer (current-buffer))))
;; gnus headers
(defun my-gnus-list-all-headers ()
"Extract all headers from the current article as an alist."
(interactive)
(let (header-alist)
(gnus-with-article-buffer
(save-excursion
(goto-char (point-min))
;; Gnus articles have headers separated from body by an empty line
(let ((header-end (save-excursion
(re-search-forward "^$" nil t)
(point))))
(while (re-search-forward "^\\([^ \t:]+\\):\\s-*" header-end t)
(let ((key (match-string 1))
(beg (point))
(val ""))
;; Handle multi-line (folded) headers
(forward-line 1)
(while (and (< (point) header-end)
(looking-at "^[ \t]"))
(forward-line 1))
(setq val (buffer-substring-no-properties beg (1- (point))))
(push (cons key (string-trim val)) header-alist))))))
;; Print to *Messages* for debugging
(dolist (pair (reverse header-alist))
(message "%s: %s" (car pair) (cdr pair)))
header-alist))
(provide 'my-debug)
;;; my-debug.el ends here