Debian Codegraph @

ide/emacs/def-use-util.el

raw
1 ;;; def-use-util.el --- def-use-util.el -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2007 Vesa Karvonen
3 ;;
4 ;; MLton is released under a HPND-style license.
5 ;; See the file MLton-LICENSE for details.
6
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;; Utilities
9
10 ;; In Gnu Emacs, `buffer-file-truename' is abbreviated while in XEmacs it
11 ;; isn't. This isn't in compat.el, because we want to use our cached
12 ;; version of `file-truename', namely `def-use-file-truename'.
13 (defun def-use-buffer-file-truename (&rest buffer)
14 "Returns the true filename of the current buffer."
15 (let ((name (apply (function buffer-file-name) buffer)))
16 (when name
17 (def-use-file-truename name))))
18
19 (defvar def-use-file-truename-table
20 (make-hash-table :test 'equal :weakness 'key)
21 "Weak hash table private to `def-use-file-truename'.")
22
23 (defun def-use-file-truename (file)
24 "Cached version of `file-truename' combined with `abbreviate-file-name'."
25 (def-use-gethash-or-put file
26 (function
27 (lambda ()
28 (def-use-intern
29 (def-use-add-face 'font-lock-keyword-face
30 (abbreviate-file-name (file-truename file))))))
31 def-use-file-truename-table))
32
33 (defun def-use-find-buffer-visiting-file (file)
34 "Tries to find a buffer visiting the specified file."
35 (let ((truename (def-use-file-truename file)))
36 (cl-loop for buffer in (buffer-list) do
37 (if (with-current-buffer buffer
38 (string= (def-use-buffer-file-truename) truename))
39 (cl-return buffer)))))
40
41 (defun def-use-find-file (file &optional other-window)
42 "Roughly as `find-file' or `find-file-other-window' except that will not
43 open the file a second time if a buffer is editing a file by the same true
44 file name."
45 (let ((buffer (def-use-find-buffer-visiting-file file)))
46 (cond
47 (buffer
48 (let ((window (get-buffer-window buffer)))
49 (cond
50 (other-window
51 (switch-to-buffer-other-window buffer))
52 (window
53 (set-frame-selected-window nil window))
54 (t
55 (switch-to-buffer buffer)))))
56 (other-window
57 (find-file-other-window file))
58 (t
59 (find-file file)))))
60
61 (defun def-use-point-at-next-line ()
62 "Returns point at the beginning of the next line."
63 (save-excursion
64 (end-of-line)
65 (+ 1 (point))))
66
67 (defun def-use-point-at-current-line ()
68 "Returns point at the beginning of the current line."
69 (save-excursion
70 (beginning-of-line)
71 (point)))
72
73 (defun def-use-current-line ()
74 "Returns the current line number counting from 1."
75 (+ 1 (count-lines 1 (def-use-point-at-current-line))))
76
77 (defun def-use-gethash-or-put (key_ mk-value_ table_)
78 (or (gethash key_ table_)
79 (puthash key_ (funcall mk-value_) table_)))
80
81 (defvar def-use-intern-table
82 (make-hash-table :test 'equal :weakness 'key-and-value)
83 "Weak hash table private to `def-use-intern'.")
84
85 (defun def-use-intern (value)
86 "Hashes the given value to itself. The assumption is that the value
87 being interned is not going to be mutated."
88 (def-use-gethash-or-put value (function (lambda () value))
89 def-use-intern-table))
90
91 (defun def-use-hash-table-to-assoc-list (hash-table)
92 "Returns an assoc list containing all the keys and values of the hash
93 table."
94 (let ((result nil))
95 (maphash (function
96 (lambda (key value)
97 (push (cons key value) result)))
98 hash-table)
99 result))
100
101 (defun def-use-hash-table-to-key-list (hash-table)
102 "Returns a list of the keys of hash-table."
103 (mapcar (function car)
104 (def-use-hash-table-to-assoc-list hash-table)))
105
106 (defun def-use-hash-table-to-value-list (hash-table)
107 "Returns a list of the values of the hash-table."
108 (mapcar (function cdr)
109 (def-use-hash-table-to-assoc-list hash-table)))
110
111 (defun def-use-set-to-list (set)
112 "Returns a list of the keys of the set (identity hash-table)."
113 (def-use-hash-table-to-key-list set))
114
115 (defun def-use-make-hash-table ()
116 "Makes a hash table with `equal' semantics."
117 (make-hash-table :test 'equal :size 1))
118
119 (defun def-use-kill-current-buffer ()
120 "Kills the current buffer."
121 (interactive)
122 (kill-buffer (current-buffer)))
123
124 (defun def-use-add-face (face string)
125 "Adds the face as a property to the entire string and returns the
126 string."
127 (add-text-properties 0 (length string) `(face ,face) string)
128 string)
129
130 (defun def-use-time-to-double (time)
131 "Converts a time to a double."
132 (+ (* (car time) 65536.0)
133 (cadr time)
134 (if (cddr time) (* (caddr time) 1e-06) 0)))
135
136 (defun def-use-attr-newer? (attr1 attr2)
137 "Returns non-nil iff the modification time of `attr1' is later than the
138 modification time of `attr2'. Note that this also returns nil when either
139 one of the modification times is nil."
140 (and attr1 attr2
141 (> (def-use-time-to-double (nth 5 attr1))
142 (def-use-time-to-double (nth 5 attr2)))))
143
144 (defun def-use-attr-changed? (attr1 attr2)
145 "Returns non-nil iff the file attributes of `attr1' are different than
146 the file attributes of `attr2'. Note that this also returns nil when either
147 one of the file attributes is nil."
148 (cl-labels ((nequal (i) (not (equal (nth i attr1) (nth i attr2)))))
149 (and attr1 attr2
150 (or (def-use-attr-newer? attr1 attr2)
151 (nequal 7) ;; size
152 (nequal 6) ;; status change time
153 (nequal 8) ;; file modes
154 (nequal 10) ;; inode
155 ))))
156
157 (defun def-use-goto-line (line)
158 "Goes to specified line quietly without setting mark. By default, the
159 standard `goto-line' function in latest Gnu Emacs sets the mark displaying
160 the message \"Mark set\"."
161 (save-restriction
162 (widen)
163 (goto-char 1)
164 (forward-line (1- line))))
165
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167
168 (provide 'def-use-util)