1286
|
1 |
;;; autobm.el --- suggest bookmark name from local context
|
|
2 |
|
1287
|
3 |
(require 'cl-lib)
|
|
4 |
|
1286
|
5 |
(defvar autobm-mode-handlers
|
|
6 |
'((emacs-lisp-mode . (autobm-try-semantic))
|
|
7 |
(semantic-decoration-mode . (autobm-try-semantic))
|
|
8 |
(t . (autobm-try-which-func autobm-try-thingatpt)))
|
|
9 |
"Alist of handlers by mode. Key is a mojor or minor mode name, or `t' as fallback.
|
|
10 |
Value is a no-argument function or a list of such functions,
|
|
11 |
which returns string or `nil'.")
|
|
12 |
|
|
13 |
(defun autobm-try-which-func ()
|
|
14 |
(which-function))
|
|
15 |
|
|
16 |
(defvar autobm-try-thingatpt-things
|
|
17 |
'(symbol url email))
|
|
18 |
|
|
19 |
(defun autobm-try-thingatpt ()
|
1287
|
20 |
(cl-some #'thing-at-point autobm-try-thingatpt-things))
|
1286
|
21 |
|
|
22 |
(defun autobm-get-active-handlers ()
|
|
23 |
(let ( h handlers (modes (list t)) )
|
|
24 |
(dolist (m minor-mode-list)
|
|
25 |
(when (and (boundp m) (symbol-value m))
|
|
26 |
(push m modes)))
|
|
27 |
(push major-mode modes)
|
|
28 |
(dolist (m modes)
|
|
29 |
(setq h (assoc m autobm-mode-handlers))
|
|
30 |
(when h
|
|
31 |
(setq h (cdr h))
|
|
32 |
(cond
|
|
33 |
((symbolp h)
|
|
34 |
(push h handlers))
|
|
35 |
((listp h)
|
|
36 |
(setq handlers (append h handlers)))
|
|
37 |
(t (error "autobm-mode-handlers isn't a list or a symbol")))))
|
|
38 |
handlers))
|
|
39 |
|
|
40 |
(defvar autobm-try-semantic-type-alist
|
|
41 |
'((variable . "var")
|
|
42 |
(function . "fn")
|
|
43 |
(type . "type")))
|
|
44 |
|
|
45 |
(defun autobm-try-semantic ()
|
|
46 |
(let ( (tag (semantic-current-tag)) prefix )
|
|
47 |
(when tag
|
|
48 |
(setq prefix (assoc (cadr tag) autobm-try-semantic-type-alist))
|
|
49 |
(when prefix
|
|
50 |
(format "%s: %s" (cdr prefix) (car tag))))))
|
|
51 |
|
|
52 |
(defun autobm-suggest ()
|
|
53 |
"Suggest bookmark name depending on buffer mode and local
|
|
54 |
context using `autobm-semantic-type-alist'. First checked major
|
|
55 |
mode then active minor modes."
|
|
56 |
(let ((result nil))
|
|
57 |
(catch 'exit
|
|
58 |
(dolist (h (autobm-get-active-handlers))
|
|
59 |
(when (setq result (funcall h))
|
|
60 |
(throw 'exit result))))))
|
|
61 |
|
|
62 |
;;;###autoload
|
|
63 |
(defun autobm (&optional no-overwrite)
|
|
64 |
(interactive "P")
|
|
65 |
(let ( (name (autobm-suggest)) )
|
|
66 |
(when name
|
|
67 |
(setq name (read-string "Set bookmark: " name bookmark-minibuffer-read-name-map name)))
|
|
68 |
(bookmark-set name no-overwrite)))
|
|
69 |
|
|
70 |
(global-set-key (kbd "C-x r m") 'autobm)
|