author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sat, 21 Nov 2020 21:34:47 +0200 | |
changeset 1643 | ce2756ae43b2 |
parent 1598 | 09079d2df4af |
permissions | -rw-r--r-- |
1591
dd81e33d0dd0
Marked file for lexical-binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1590
diff
changeset
|
1 |
;;; autobm.el --- suggest bookmark name from local context -*- lexical-binding: t -*- |
1286 | 2 |
|
1287 | 3 |
(require 'cl-lib) |
1318
a37d43074061
Fix: Symbol's value as variable is void: bookmark-minibuffer-read-name-map.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1288
diff
changeset
|
4 |
(require 'bookmark) |
1524
6d4fee1f7cb0
Fix: Warning: the following functions are not known to be defined:
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1319
diff
changeset
|
5 |
(require 'which-func) |
6d4fee1f7cb0
Fix: Warning: the following functions are not known to be defined:
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1319
diff
changeset
|
6 |
(require 'semantic/find) |
1287 | 7 |
|
1286 | 8 |
(defvar autobm-mode-handlers |
9 |
'((emacs-lisp-mode . (autobm-try-semantic)) |
|
10 |
(semantic-decoration-mode . (autobm-try-semantic)) |
|
1598
09079d2df4af
It is better to select hole word (including dashes/underscores, depending on mode).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1591
diff
changeset
|
11 |
(rst-mode . (autobm-try-word)) |
1286 | 12 |
(t . (autobm-try-which-func autobm-try-thingatpt))) |
13 |
"Alist of handlers by mode. Key is a mojor or minor mode name, or `t' as fallback. |
|
14 |
Value is a no-argument function or a list of such functions, |
|
15 |
which returns string or `nil'.") |
|
16 |
||
17 |
(defun autobm-try-which-func () |
|
18 |
(which-function)) |
|
19 |
||
20 |
(defvar autobm-try-thingatpt-things |
|
21 |
'(symbol url email)) |
|
22 |
||
23 |
(defun autobm-try-thingatpt () |
|
1287 | 24 |
(cl-some #'thing-at-point autobm-try-thingatpt-things)) |
1286 | 25 |
|
1598
09079d2df4af
It is better to select hole word (including dashes/underscores, depending on mode).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1591
diff
changeset
|
26 |
(defun autobm-try-word () |
09079d2df4af
It is better to select hole word (including dashes/underscores, depending on mode).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1591
diff
changeset
|
27 |
(thing-at-point 'word)) |
09079d2df4af
It is better to select hole word (including dashes/underscores, depending on mode).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1591
diff
changeset
|
28 |
|
1590
b117f393fd9a
Added bookmark creation for RST mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1524
diff
changeset
|
29 |
(defun autobm-try-line () |
b117f393fd9a
Added bookmark creation for RST mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1524
diff
changeset
|
30 |
(let ( (line (thing-at-point 'line)) ) |
b117f393fd9a
Added bookmark creation for RST mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1524
diff
changeset
|
31 |
(substring line 0 (min (1- (length line)) 30)))) |
b117f393fd9a
Added bookmark creation for RST mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1524
diff
changeset
|
32 |
|
1286 | 33 |
(defun autobm-get-active-handlers () |
1319
d893dc8fe820
Fix: major-mode should have highest priority, default handlers - least.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1318
diff
changeset
|
34 |
(let ( h handlers (modes (list major-mode)) ) |
1286 | 35 |
(dolist (m minor-mode-list) |
36 |
(when (and (boundp m) (symbol-value m)) |
|
37 |
(push m modes))) |
|
1319
d893dc8fe820
Fix: major-mode should have highest priority, default handlers - least.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1318
diff
changeset
|
38 |
(push t modes) |
1286 | 39 |
(dolist (m modes) |
40 |
(setq h (assoc m autobm-mode-handlers)) |
|
41 |
(when h |
|
42 |
(setq h (cdr h)) |
|
43 |
(cond |
|
44 |
((symbolp h) |
|
45 |
(push h handlers)) |
|
46 |
((listp h) |
|
47 |
(setq handlers (append h handlers))) |
|
48 |
(t (error "autobm-mode-handlers isn't a list or a symbol"))))) |
|
49 |
handlers)) |
|
50 |
||
51 |
(defvar autobm-try-semantic-type-alist |
|
52 |
'((variable . "var") |
|
53 |
(function . "fn") |
|
54 |
(type . "type"))) |
|
55 |
||
56 |
(defun autobm-try-semantic () |
|
57 |
(let ( (tag (semantic-current-tag)) prefix ) |
|
58 |
(when tag |
|
59 |
(setq prefix (assoc (cadr tag) autobm-try-semantic-type-alist)) |
|
60 |
(when prefix |
|
61 |
(format "%s: %s" (cdr prefix) (car tag)))))) |
|
62 |
||
63 |
(defun autobm-suggest () |
|
64 |
"Suggest bookmark name depending on buffer mode and local |
|
1590
b117f393fd9a
Added bookmark creation for RST mode.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1524
diff
changeset
|
65 |
context using `autobm-try-semantic-type-alist'. First checked major |
1286 | 66 |
mode then active minor modes." |
67 |
(let ((result nil)) |
|
68 |
(catch 'exit |
|
69 |
(dolist (h (autobm-get-active-handlers)) |
|
70 |
(when (setq result (funcall h)) |
|
71 |
(throw 'exit result)))))) |
|
72 |
||
73 |
;;;###autoload |
|
74 |
(defun autobm (&optional no-overwrite) |
|
75 |
(interactive "P") |
|
76 |
(let ( (name (autobm-suggest)) ) |
|
77 |
(when name |
|
1318
a37d43074061
Fix: Symbol's value as variable is void: bookmark-minibuffer-read-name-map.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1288
diff
changeset
|
78 |
(setq name (read-string "Set bookmark: " name bookmark-minibuffer-read-name-map name))) |
1286 | 79 |
(bookmark-set name no-overwrite))) |
80 |
||
1288
6333a733046f
Use new autobm mode!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1287
diff
changeset
|
81 |
;; (global-set-key (kbd "C-x r m") 'autobm) |
6333a733046f
Use new autobm mode!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1287
diff
changeset
|
82 |
|
6333a733046f
Use new autobm mode!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1287
diff
changeset
|
83 |
(provide 'autobm) |
6333a733046f
Use new autobm mode!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1287
diff
changeset
|
84 |