Commit | Line | Data |
---|---|---|
36ad1553 CD |
1 | ;;; org-mouse.el --- Better mouse support for org-mode |
2 | ||
5df4f04c | 3 | ;; Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation |
36ad1553 CD |
4 | ;; |
5 | ;; Author: Piotr Zielinski <piotr dot zielinski at gmail dot com> | |
6 | ;; Maintainer: Carsten Dominik <carsten at orgmode dot org> | |
acedf35c | 7 | ;; Version: 7.4 |
36ad1553 CD |
8 | ;; |
9 | ;; This file is part of GNU Emacs. | |
10 | ;; | |
b1fc2b50 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
36ad1553 | 12 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
36ad1553 CD |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
36ad1553 CD |
23 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
24 | ;; | |
25 | ;;; Commentary: | |
26 | ;; | |
27 | ;; Org-mouse provides mouse support for org-mode. | |
28 | ;; | |
29 | ;; http://orgmode.org | |
30 | ;; | |
31 | ;; Org-mouse implements the following features: | |
32 | ;; * following links with the left mouse button (in Emacs 22) | |
33 | ;; * subtree expansion/collapse (org-cycle) with the left mouse button | |
34 | ;; * several context menus on the right mouse button: | |
35 | ;; + general text | |
36 | ;; + headlines | |
37 | ;; + timestamps | |
38 | ;; + priorities | |
39 | ;; + links | |
40 | ;; + tags | |
41 | ;; * promoting/demoting/moving subtrees with mouse-3 | |
42 | ;; + if the drag starts and ends in the same line then promote/demote | |
20908596 | 43 | ;; + otherwise move the subtree |
36ad1553 CD |
44 | ;; |
45 | ;; Use | |
46 | ;; --- | |
47 | ;; | |
48 | ;; To use this package, put the following line in your .emacs: | |
49 | ;; | |
50 | ;; (require 'org-mouse) | |
51 | ;; | |
52 | ||
33306645 | 53 | ;; FIXME: |
36ad1553 CD |
54 | ;; + deal with folding / unfolding issues |
55 | ||
56 | ;; TODO (This list is only theoretical, if you'd like to have some | |
57 | ;; feature implemented or a bug fix please send me an email, even if | |
58 | ;; something similar appears in the list below. This will help me get | |
59 | ;; the priorities right.): | |
60 | ;; | |
61 | ;; + org-store-link, insert link | |
62 | ;; + org tables | |
63 | ;; + occur with the current word/tag (same menu item) | |
64 | ;; + ctrl-c ctrl-c, for example, renumber the current list | |
65 | ;; + internal links | |
66 | ||
67 | ;; Please email the maintainer with new feature suggestions / bugs | |
68 | ||
69 | ;; History: | |
70 | ;; | |
33306645 | 71 | ;; Since version 5.10: Changes are listed in the general org-mode docs. |
36ad1553 CD |
72 | ;; |
73 | ;; Version 5.09 | |
74 | ;; + Version number synchronization with Org-mode. | |
75 | ;; | |
76 | ;; Version 0.25 | |
77 | ;; + made compatible with org-mode 4.70 (thanks to Carsten for the patch) | |
78 | ;; | |
79 | ;; Version 0.24 | |
80 | ;; + minor changes to the table menu | |
81 | ;; | |
82 | ;; Version 0.23 | |
83 | ;; + preliminary support for tables and calculation marks | |
84 | ;; + context menu support for org-agenda-undo & org-sort-entries | |
85 | ;; | |
86 | ;; Version 0.22 | |
87 | ;; + handles undo support for the agenda buffer (requires org-mode >=4.58) | |
88 | ;; | |
89 | ;; Version 0.21 | |
90 | ;; + selected text activates its context menu | |
91 | ;; + shift-middleclick or right-drag inserts the text from the clipboard in the form of a link | |
92 | ;; | |
93 | ;; Version 0.20 | |
20908596 | 94 | ;; + the new "TODO Status" submenu replaces the "Cycle TODO" menu item |
36ad1553 CD |
95 | ;; + the TODO menu can now list occurrences of a specific TODO keyword |
96 | ;; + #+STARTUP line is now recognized | |
97 | ;; | |
98 | ;; Version 0.19 | |
99 | ;; + added support for dragging URLs to the org-buffer | |
100 | ;; | |
101 | ;; Version 0.18 | |
102 | ;; + added support for agenda blocks | |
103 | ;; | |
104 | ;; Version 0.17 | |
105 | ;; + toggle checkboxes with a single click | |
106 | ;; | |
107 | ;; Version 0.16 | |
108 | ;; + added support for checkboxes | |
109 | ;; | |
110 | ;; Version 0.15 | |
111 | ;; + org-mode now works with the Agenda buffer as well | |
112 | ;; | |
113 | ;; Version 0.14 | |
114 | ;; + added a menu option that converts plain list items to outline items | |
115 | ;; | |
20908596 | 116 | ;; Version 0.13 |
36ad1553 CD |
117 | ;; + "Insert Heading" now inserts a sibling heading if the point is |
118 | ;; on "***" and a child heading otherwise | |
20908596 | 119 | ;; |
36ad1553 CD |
120 | ;; Version 0.12 |
121 | ;; + compatible with Emacs 21 | |
122 | ;; + custom agenda commands added to the main menu | |
123 | ;; + moving trees should now work between windows in the same frame | |
124 | ;; | |
125 | ;; Version 0.11 | |
126 | ;; + fixed org-mouse-at-link (thanks to Carsten) | |
127 | ;; + removed [follow-link] bindings | |
128 | ;; | |
129 | ;; Version 0.10 | |
130 | ;; + added a menu option to remove highlights | |
131 | ;; + compatible with org-mode 4.21 now | |
132 | ;; | |
20908596 | 133 | ;; Version 0.08: |
36ad1553 CD |
134 | ;; + trees can be moved/promoted/demoted by dragging with the right |
135 | ;; mouse button (mouse-3) | |
136 | ;; + small changes in the above function | |
137 | ;; | |
138 | ;; Versions 0.01 -- 0.07: (I don't remember) | |
139 | ||
86fbb8ca CD |
140 | ;;; Code: |
141 | ||
36ad1553 CD |
142 | (eval-when-compile (require 'cl)) |
143 | (require 'org) | |
144 | ||
20908596 CD |
145 | (defvar org-agenda-allow-remote-undo) |
146 | (defvar org-agenda-undo-list) | |
147 | (defvar org-agenda-custom-commands) | |
148 | (declare-function org-agenda-change-all-lines "org-agenda" | |
d60b1ba1 | 149 | (newhead hdmarker &optional fixface just-this)) |
20908596 | 150 | (declare-function org-verify-change-for-undo "org-agenda" (l1 l2)) |
afe98dfa | 151 | (declare-function org-apply-on-list "org-list" (function init-value &rest args)) |
20908596 | 152 | |
36ad1553 CD |
153 | (defvar org-mouse-plain-list-regexp "\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) " |
154 | "Regular expression that matches a plain list.") | |
155 | (defvar org-mouse-direct t | |
156 | "Internal variable indicating whether the current action is direct. | |
157 | ||
158 | If t, then the current action has been invoked directly through the buffer | |
159 | it is intended to operate on. If nil, then the action has been invoked | |
160 | indirectly, for example, through the agenda buffer.") | |
161 | ||
162 | (defgroup org-mouse nil | |
163 | "Mouse support for org-mode." | |
164 | :tag "Org Mouse" | |
165 | :group 'org) | |
166 | ||
167 | (defcustom org-mouse-punctuation ":" | |
168 | "Punctuation used when inserting text by drag and drop." | |
169 | :group 'org-mouse | |
170 | :type 'string) | |
171 | ||
71d35b24 CD |
172 | (defcustom org-mouse-features |
173 | '(context-menu yank-link activate-stars activate-bullets activate-checkboxes) | |
174 | "The features of org-mouse that should be activated. | |
175 | Changing this variable requires a restart of Emacs to get activated." | |
176 | :group 'org-mouse | |
177 | :type '(set :greedy t | |
178 | (const :tag "Mouse-3 shows context menu" context-menu) | |
179 | (const :tag "C-mouse-1 and mouse-3 move trees" move-tree) | |
180 | (const :tag "S-mouse-2 and drag-mouse-3 yank link" yank-link) | |
181 | (const :tag "Activate headline stars" activate-stars) | |
182 | (const :tag "Activate item bullets" activate-bullets) | |
ff4be292 | 183 | (const :tag "Activate checkboxes" activate-checkboxes))) |
36ad1553 CD |
184 | |
185 | (defun org-mouse-re-search-line (regexp) | |
186 | "Search the current line for a given regular expression." | |
187 | (beginning-of-line) | |
188 | (re-search-forward regexp (point-at-eol) t)) | |
189 | ||
190 | (defun org-mouse-end-headline () | |
191 | "Go to the end of current headline (ignoring tags)." | |
192 | (interactive) | |
193 | (end-of-line) | |
194 | (skip-chars-backward "\t ") | |
ed21c5c8 | 195 | (when (org-looking-back ":[A-Za-z]+:") |
36ad1553 CD |
196 | (skip-chars-backward ":A-Za-z") |
197 | (skip-chars-backward "\t "))) | |
198 | ||
199 | (defvar org-mouse-context-menu-function nil | |
200 | "Function to create the context menu. | |
201 | The value of this variable is the function invoked by | |
202 | `org-mouse-context-menu' as the context menu.") | |
203 | (make-variable-buffer-local 'org-mouse-context-menu-function) | |
204 | ||
205 | (defun org-mouse-show-context-menu (event prefix) | |
206 | "Invoke the context menu. | |
207 | ||
208 | If the value of `org-mouse-context-menu-function' is a function, then | |
20908596 | 209 | this function is called. Otherwise, the current major mode menu is used." |
36ad1553 CD |
210 | (interactive "@e \nP") |
211 | (if (and (= (event-click-count event) 1) | |
20908596 | 212 | (or (not mark-active) |
36ad1553 CD |
213 | (sit-for (/ double-click-time 1000.0)))) |
214 | (progn | |
33306645 | 215 | (select-window (posn-window (event-start event))) |
36ad1553 CD |
216 | (when (not (org-mouse-mark-active)) |
217 | (goto-char (posn-point (event-start event))) | |
218 | (when (not (eolp)) (save-excursion (run-hooks 'post-command-hook))) | |
219 | (let ((redisplay-dont-pause t)) | |
220 | (sit-for 0))) | |
221 | (if (functionp org-mouse-context-menu-function) | |
222 | (funcall org-mouse-context-menu-function event) | |
0e47efc5 GM |
223 | (if (fboundp 'mouse-menu-major-mode-map) |
224 | (popup-menu (mouse-menu-major-mode-map) event prefix) | |
0bd48b37 CD |
225 | (org-no-warnings ; don't warn about fallback, obsolete since 23.1 |
226 | (mouse-major-mode-menu event prefix))))) | |
36ad1553 CD |
227 | (setq this-command 'mouse-save-then-kill) |
228 | (mouse-save-then-kill event))) | |
229 | ||
36ad1553 | 230 | (defun org-mouse-line-position () |
86fbb8ca | 231 | "Return `:beginning' or `:middle' or `:end', depending on the point position. |
36ad1553 CD |
232 | |
233 | If the point is at the end of the line, return `:end'. | |
234 | If the point is separated from the beginning of the line only by white | |
235 | space and *'s (`org-mouse-bolp'), return `:beginning'. Otherwise, | |
236 | return `:middle'." | |
237 | (cond | |
238 | ((eolp) :end) | |
239 | ((org-mouse-bolp) :beginning) | |
240 | (t :middle))) | |
241 | ||
242 | (defun org-mouse-empty-line () | |
243 | "Return non-nil iff the line contains only white space." | |
244 | (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))) | |
245 | ||
246 | (defun org-mouse-next-heading () | |
247 | "Go to the next heading. | |
248 | If there is none, ensure that the point is at the beginning of an empty line." | |
249 | (unless (outline-next-heading) | |
250 | (beginning-of-line) | |
251 | (unless (org-mouse-empty-line) | |
252 | (end-of-line) | |
253 | (newline)))) | |
254 | ||
255 | (defun org-mouse-insert-heading () | |
256 | "Insert a new heading, as `org-insert-heading'. | |
257 | ||
258 | If the point is at the :beginning (`org-mouse-line-position') of the line, | |
259 | insert the new heading before the current line. Otherwise, insert it | |
260 | after the current heading." | |
261 | (interactive) | |
262 | (case (org-mouse-line-position) | |
263 | (:beginning (beginning-of-line) | |
264 | (org-insert-heading)) | |
265 | (t (org-mouse-next-heading) | |
266 | (org-insert-heading)))) | |
267 | ||
20908596 CD |
268 | (defun org-mouse-timestamp-today (&optional shift units) |
269 | "Change the timestamp into SHIFT UNITS in the future. | |
36ad1553 CD |
270 | |
271 | For the acceptable UNITS, see `org-timestamp-change'." | |
272 | (interactive) | |
273 | (flet ((org-read-date (&rest rest) (current-time))) | |
274 | (org-time-stamp nil)) | |
275 | (when shift | |
276 | (org-timestamp-change shift units))) | |
277 | ||
278 | (defun org-mouse-keyword-menu (keywords function &optional selected itemformat) | |
279 | "A helper function. | |
280 | ||
281 | Returns a menu fragment consisting of KEYWORDS. When a keyword | |
282 | is selected by the user, FUNCTION is called with the selected | |
283 | keyword as the only argument. | |
284 | ||
285 | If SELECTED is nil, then all items are normal menu items. If | |
286 | SELECTED is a function, then each item is a checkbox, which is | |
287 | enabled for a given keyword iff (funcall SELECTED keyword) return | |
288 | non-nil. If SELECTED is neither nil nor a function, then the | |
289 | items are radio buttons. A radio button is enabled for the | |
20908596 | 290 | keyword `equal' to SELECTED. |
36ad1553 CD |
291 | |
292 | ITEMFORMAT governs formatting of the elements of KEYWORDS. If it | |
293 | is a function, it is invoked with the keyword as the only | |
294 | argument. If it is a string, it is interpreted as the format | |
295 | string to (format ITEMFORMAT keyword). If it is neither a string | |
86fbb8ca | 296 | nor a function, elements of KEYWORDS are used directly." |
20908596 CD |
297 | (mapcar |
298 | `(lambda (keyword) | |
36ad1553 CD |
299 | (vector (cond |
300 | ((functionp ,itemformat) (funcall ,itemformat keyword)) | |
301 | ((stringp ,itemformat) (format ,itemformat keyword)) | |
302 | (t keyword)) | |
303 | (list 'funcall ,function keyword) | |
20908596 | 304 | :style (cond |
36ad1553 CD |
305 | ((null ,selected) t) |
306 | ((functionp ,selected) 'toggle) | |
307 | (t 'radio)) | |
20908596 | 308 | :selected (if (functionp ,selected) |
36ad1553 CD |
309 | (and (funcall ,selected keyword) t) |
310 | (equal ,selected keyword)))) | |
311 | keywords)) | |
20908596 | 312 | |
36ad1553 CD |
313 | (defun org-mouse-remove-match-and-spaces () |
314 | "Remove the match, make just one space around the point." | |
315 | (interactive) | |
316 | (replace-match "") | |
317 | (just-one-space)) | |
318 | ||
319 | (defvar rest) | |
20908596 | 320 | (defun org-mouse-replace-match-and-surround (newtext &optional fixedcase |
36ad1553 CD |
321 | literal string subexp) |
322 | "The same as `replace-match', but surrounds the replacement with spaces." | |
323 | (apply 'replace-match rest) | |
324 | (save-excursion | |
20908596 | 325 | (goto-char (match-beginning (or subexp 0))) |
36ad1553 | 326 | (just-one-space) |
20908596 | 327 | (goto-char (match-end (or subexp 0))) |
36ad1553 | 328 | (just-one-space))) |
20908596 | 329 | |
36ad1553 CD |
330 | |
331 | (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat | |
332 | nosurround) | |
333 | "A helper function. | |
334 | ||
335 | Returns a menu fragment consisting of KEYWORDS. When a keyword | |
336 | is selected, group GROUP of the current match is replaced by the | |
337 | keyword. The method ensures that both ends of the replacement | |
338 | are separated from the rest of the text in the buffer by | |
33306645 | 339 | individual spaces (unless NOSURROUND is non-nil). |
36ad1553 CD |
340 | |
341 | The final entry of the menu is always \"None\", which removes the | |
342 | match. | |
343 | ||
344 | ITEMFORMAT governs formatting of the elements of KEYWORDS. If it | |
345 | is a function, it is invoked with the keyword as the only | |
346 | argument. If it is a string, it is interpreted as the format | |
347 | string to (format ITEMFORMAT keyword). If it is neither a string | |
86fbb8ca | 348 | nor a function, elements of KEYWORDS are used directly." |
36ad1553 | 349 | (setq group (or group 0)) |
20908596 | 350 | (let ((replace (org-mouse-match-closure |
36ad1553 CD |
351 | (if nosurround 'replace-match |
352 | 'org-mouse-replace-match-and-surround)))) | |
353 | (append | |
20908596 | 354 | (org-mouse-keyword-menu |
36ad1553 CD |
355 | keywords |
356 | `(lambda (keyword) (funcall ,replace keyword t t nil ,group)) | |
357 | (match-string group) | |
358 | itemformat) | |
20908596 | 359 | `(["None" org-mouse-remove-match-and-spaces |
36ad1553 CD |
360 | :style radio |
361 | :selected ,(not (member (match-string group) keywords))])))) | |
20908596 | 362 | |
36ad1553 CD |
363 | (defun org-mouse-show-headlines () |
364 | "Change the visibility of the current org buffer to only show headlines." | |
20908596 CD |
365 | (interactive) |
366 | (let ((this-command 'org-cycle) | |
36ad1553 CD |
367 | (last-command 'org-cycle) |
368 | (org-cycle-global-status nil)) | |
369 | (org-cycle '(4)) | |
370 | (org-cycle '(4)))) | |
371 | ||
372 | (defun org-mouse-show-overview () | |
373 | "Change visibility of current org buffer to first-level headlines only." | |
20908596 | 374 | (interactive) |
36ad1553 CD |
375 | (let ((org-cycle-global-status nil)) |
376 | (org-cycle '(4)))) | |
377 | ||
378 | (defun org-mouse-set-priority (priority) | |
379 | "Set the priority of the current headline to PRIORITY." | |
380 | (flet ((read-char-exclusive () priority)) | |
381 | (org-priority))) | |
382 | ||
383 | (defvar org-mouse-priority-regexp "\\[#\\([A-Z]\\)\\]" | |
384 | "Regular expression matching the priority indicator. | |
385 | Differs from `org-priority-regexp' in that it doesn't contain the | |
386 | leading '.*?'.") | |
387 | ||
388 | (defun org-mouse-get-priority (&optional default) | |
389 | "Return the priority of the current headline. | |
390 | DEFAULT is returned if no priority is given in the headline." | |
20908596 | 391 | (save-excursion |
36ad1553 CD |
392 | (if (org-mouse-re-search-line org-mouse-priority-regexp) |
393 | (match-string 1) | |
394 | (when default (char-to-string org-default-priority))))) | |
395 | ||
396 | ;; (defun org-mouse-at-link () | |
397 | ;; (and (eq (get-text-property (point) 'face) 'org-link) | |
398 | ;; (save-excursion | |
399 | ;; (goto-char (previous-single-property-change (point) 'face)) | |
400 | ;; (or (looking-at org-bracket-link-regexp) | |
401 | ;; (looking-at org-angle-link-re) | |
402 | ;; (looking-at org-plain-link-re))))) | |
403 | ||
404 | ||
405 | (defun org-mouse-delete-timestamp () | |
406 | "Deletes the current timestamp as well as the preceding keyword. | |
407 | SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:" | |
408 | (when (or (org-at-date-range-p) (org-at-timestamp-p)) | |
409 | (replace-match "") ; delete the timestamp | |
410 | (skip-chars-backward " :A-Z") | |
411 | (when (looking-at " *[A-Z][A-Z]+:") | |
412 | (replace-match "")))) | |
413 | ||
20908596 | 414 | (defun org-mouse-looking-at (regexp skipchars &optional movechars) |
36ad1553 CD |
415 | (save-excursion |
416 | (let ((point (point))) | |
417 | (if (looking-at regexp) t | |
418 | (skip-chars-backward skipchars) | |
419 | (forward-char (or movechars 0)) | |
420 | (when (looking-at regexp) | |
421 | (> (match-end 0) point)))))) | |
20908596 | 422 | |
36ad1553 | 423 | (defun org-mouse-priority-list () |
20908596 | 424 | (loop for priority from ?A to org-lowest-priority |
36ad1553 CD |
425 | collect (char-to-string priority))) |
426 | ||
c8d0cf5c CD |
427 | (defun org-mouse-todo-menu (state) |
428 | "Create the menu with TODO keywords." | |
429 | (append | |
430 | (let ((kwds org-todo-keywords-1)) | |
431 | (org-mouse-keyword-menu | |
432 | kwds | |
433 | `(lambda (kwd) (org-todo kwd)) | |
434 | (lambda (kwd) (equal state kwd)))))) | |
435 | ||
36ad1553 | 436 | (defun org-mouse-tag-menu () ;todo |
86fbb8ca | 437 | "Create the tags menu." |
36ad1553 | 438 | (append |
71d35b24 | 439 | (let ((tags (org-get-tags))) |
20908596 | 440 | (org-mouse-keyword-menu |
36ad1553 | 441 | (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp) |
20908596 CD |
442 | `(lambda (tag) |
443 | (org-mouse-set-tags | |
444 | (sort (if (member tag (quote ,tags)) | |
36ad1553 CD |
445 | (delete tag (quote ,tags)) |
446 | (cons tag (quote ,tags))) | |
447 | 'string-lessp))) | |
448 | `(lambda (tag) (member tag (quote ,tags))) | |
449 | )) | |
450 | '("--" | |
451 | ["Align Tags Here" (org-set-tags nil t) t] | |
452 | ["Align Tags in Buffer" (org-set-tags t t) t] | |
453 | ["Set Tags ..." (org-set-tags) t]))) | |
20908596 | 454 | |
36ad1553 | 455 | |
36ad1553 CD |
456 | (defun org-mouse-set-tags (tags) |
457 | (save-excursion | |
458 | ;; remove existing tags first | |
459 | (beginning-of-line) | |
460 | (when (org-mouse-re-search-line ":\\(\\([A-Za-z_]+:\\)+\\)") | |
461 | (replace-match "")) | |
462 | ||
463 | ;; set new tags if any | |
464 | (when tags | |
465 | (end-of-line) | |
466 | (insert " :" (mapconcat 'identity tags ":") ":") | |
467 | (org-set-tags nil t)))) | |
20908596 | 468 | |
36ad1553 CD |
469 | (defun org-mouse-insert-checkbox () |
470 | (interactive) | |
471 | (and (org-at-item-p) | |
472 | (goto-char (match-end 0)) | |
473 | (unless (org-at-item-checkbox-p) | |
474 | (delete-horizontal-space) | |
475 | (insert " [ ] ")))) | |
476 | ||
477 | (defun org-mouse-agenda-type (type) | |
478 | (case type | |
479 | ('tags "Tags: ") | |
480 | ('todo "TODO: ") | |
481 | ('tags-tree "Tags tree: ") | |
482 | ('todo-tree "TODO tree: ") | |
483 | ('occur-tree "Occur tree: ") | |
484 | (t "Agenda command ???"))) | |
485 | ||
486 | ||
487 | (defun org-mouse-list-options-menu (alloptions &optional function) | |
20908596 | 488 | (let ((options (save-match-data |
36ad1553 CD |
489 | (split-string (match-string-no-properties 1))))) |
490 | (print options) | |
491 | (loop for name in alloptions | |
20908596 CD |
492 | collect |
493 | (vector name | |
36ad1553 | 494 | `(progn |
20908596 CD |
495 | (replace-match |
496 | (mapconcat 'identity | |
36ad1553 CD |
497 | (sort (if (member ',name ',options) |
498 | (delete ',name ',options) | |
499 | (cons ',name ',options)) | |
500 | 'string-lessp) | |
501 | " ") | |
502 | nil nil nil 1) | |
503 | (when (functionp ',function) (funcall ',function))) | |
504 | :style 'toggle | |
505 | :selected (and (member name options) t))))) | |
506 | ||
507 | (defun org-mouse-clip-text (text maxlength) | |
508 | (if (> (length text) maxlength) | |
509 | (concat (substring text 0 (- maxlength 3)) "...") | |
510 | text)) | |
511 | ||
512 | (defun org-mouse-popup-global-menu () | |
20908596 | 513 | (popup-menu |
36ad1553 CD |
514 | `("Main Menu" |
515 | ["Show Overview" org-mouse-show-overview t] | |
516 | ["Show Headlines" org-mouse-show-headlines t] | |
517 | ["Show All" show-all t] | |
518 | ["Remove Highlights" org-remove-occur-highlights | |
519 | :visible org-occur-highlights] | |
520 | "--" | |
20908596 | 521 | ["Check Deadlines" |
36ad1553 CD |
522 | (if (functionp 'org-check-deadlines-and-todos) |
523 | (org-check-deadlines-and-todos org-deadline-warning-days) | |
524 | (org-check-deadlines org-deadline-warning-days)) t] | |
525 | ["Check TODOs" org-show-todo-tree t] | |
20908596 CD |
526 | ("Check Tags" |
527 | ,@(org-mouse-keyword-menu | |
36ad1553 CD |
528 | (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp) |
529 | '(lambda (tag) (org-tags-sparse-tree nil tag))) | |
530 | "--" | |
531 | ["Custom Tag ..." org-tags-sparse-tree t]) | |
532 | ["Check Phrase ..." org-occur] | |
533 | "--" | |
534 | ["Display Agenda" org-agenda-list t] | |
535 | ["Display Timeline" org-timeline t] | |
536 | ["Display TODO List" org-todo-list t] | |
20908596 CD |
537 | ("Display Tags" |
538 | ,@(org-mouse-keyword-menu | |
36ad1553 CD |
539 | (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp) |
540 | '(lambda (tag) (org-tags-view nil tag))) | |
541 | "--" | |
542 | ["Custom Tag ..." org-tags-view t]) | |
543 | ["Display Calendar" org-goto-calendar t] | |
544 | "--" | |
20908596 | 545 | ,@(org-mouse-keyword-menu |
36ad1553 | 546 | (mapcar 'car org-agenda-custom-commands) |
20908596 CD |
547 | '(lambda (key) |
548 | (eval `(flet ((read-char-exclusive () (string-to-char ,key))) | |
36ad1553 | 549 | (org-agenda nil)))) |
20908596 | 550 | nil |
36ad1553 CD |
551 | '(lambda (key) |
552 | (let ((entry (assoc key org-agenda-custom-commands))) | |
20908596 | 553 | (org-mouse-clip-text |
36ad1553 CD |
554 | (cond |
555 | ((stringp (nth 1 entry)) (nth 1 entry)) | |
20908596 | 556 | ((stringp (nth 2 entry)) |
36ad1553 CD |
557 | (concat (org-mouse-agenda-type (nth 1 entry)) |
558 | (nth 2 entry))) | |
559 | (t "Agenda Command '%s'")) | |
560 | 30)))) | |
561 | "--" | |
20908596 | 562 | ["Delete Blank Lines" delete-blank-lines |
36ad1553 CD |
563 | :visible (org-mouse-empty-line)] |
564 | ["Insert Checkbox" org-mouse-insert-checkbox | |
565 | :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))] | |
20908596 | 566 | ["Insert Checkboxes" |
36ad1553 CD |
567 | (org-mouse-for-each-item 'org-mouse-insert-checkbox) |
568 | :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))] | |
569 | ["Plain List to Outline" org-mouse-transform-to-outline | |
570 | :visible (org-at-item-p)]))) | |
571 | ||
20908596 | 572 | |
36ad1553 CD |
573 | (defun org-mouse-get-context (contextlist context) |
574 | (let ((contextdata (assq context contextlist))) | |
575 | (when contextdata | |
20908596 | 576 | (save-excursion |
36ad1553 CD |
577 | (goto-char (second contextdata)) |
578 | (re-search-forward ".*" (third contextdata)))))) | |
579 | ||
afe98dfa CD |
580 | (defun org-mouse-for-each-item (funct) |
581 | ;; Functions called by `org-apply-on-list' need an argument | |
582 | (let ((wrap-fun (lambda (c) (funcall funct)))) | |
583 | (when (org-in-item-p) | |
584 | (org-apply-on-list wrap-fun nil)))) | |
36ad1553 CD |
585 | |
586 | (defun org-mouse-bolp () | |
86fbb8ca CD |
587 | "Return true if there only spaces, tabs, and '*' before point. |
588 | This means, between the beginning of line and the point." | |
36ad1553 CD |
589 | (save-excursion |
590 | (skip-chars-backward " \t*") (bolp))) | |
20908596 | 591 | |
36ad1553 CD |
592 | (defun org-mouse-insert-item (text) |
593 | (case (org-mouse-line-position) | |
20908596 CD |
594 | (:beginning ; insert before |
595 | (beginning-of-line) | |
36ad1553 CD |
596 | (looking-at "[ \t]*") |
597 | (open-line 1) | |
ce4fdcb9 | 598 | (org-indent-to-column (- (match-end 0) (match-beginning 0))) |
36ad1553 | 599 | (insert "+ ")) |
20908596 | 600 | |
36ad1553 | 601 | (:middle ; insert after |
20908596 CD |
602 | (end-of-line) |
603 | (newline t) | |
36ad1553 CD |
604 | (indent-relative) |
605 | (insert "+ ")) | |
606 | ||
607 | (:end ; insert text here | |
20908596 | 608 | (skip-chars-backward " \t") |
36ad1553 | 609 | (kill-region (point) (point-at-eol)) |
ed21c5c8 | 610 | (unless (org-looking-back org-mouse-punctuation) |
36ad1553 | 611 | (insert (concat org-mouse-punctuation " "))))) |
20908596 | 612 | |
36ad1553 CD |
613 | (insert text) |
614 | (beginning-of-line)) | |
615 | ||
616 | (defadvice dnd-insert-text (around org-mouse-dnd-insert-text activate) | |
617 | (if (eq major-mode 'org-mode) | |
618 | (org-mouse-insert-item text) | |
619 | ad-do-it)) | |
620 | ||
621 | (defadvice dnd-open-file (around org-mouse-dnd-open-file activate) | |
622 | (if (eq major-mode 'org-mode) | |
623 | (org-mouse-insert-item uri) | |
624 | ad-do-it)) | |
625 | ||
626 | (defun org-mouse-match-closure (function) | |
627 | (let ((match (match-data t))) | |
20908596 | 628 | `(lambda (&rest rest) |
36ad1553 CD |
629 | (save-match-data |
630 | (set-match-data ',match) | |
631 | (apply ',function rest))))) | |
632 | ||
36ad1553 CD |
633 | (defun org-mouse-match-todo-keyword () |
634 | (save-excursion | |
635 | (org-back-to-heading) | |
636 | (if (looking-at outline-regexp) (goto-char (match-end 0))) | |
637 | (or (looking-at (concat " +" org-todo-regexp " *")) | |
638 | (looking-at " \\( *\\)")))) | |
639 | ||
640 | (defun org-mouse-yank-link (click) | |
641 | (interactive "e") | |
642 | ;; Give temporary modes such as isearch a chance to turn off. | |
643 | (run-hooks 'mouse-leave-buffer-hook) | |
644 | (mouse-set-point click) | |
645 | (setq mouse-selection-click-count 0) | |
646 | (delete-horizontal-space) | |
647 | (insert-for-yank (concat " [[" (current-kill 0) "]] "))) | |
648 | ||
649 | (defun org-mouse-context-menu (&optional event) | |
650 | (let ((stamp-prefixes (list org-deadline-string org-scheduled-string)) | |
651 | (contextlist (org-context))) | |
652 | (flet ((get-context (context) (org-mouse-get-context contextlist context))) | |
653 | (cond | |
654 | ((org-mouse-mark-active) | |
655 | (let ((region-string (buffer-substring (region-beginning) (region-end)))) | |
656 | (popup-menu | |
657 | `(nil | |
658 | ["Sparse Tree" (org-occur ',region-string)] | |
659 | ["Find in Buffer" (occur ',region-string)] | |
20908596 | 660 | ["Grep in Current Dir" |
36ad1553 | 661 | (grep (format "grep -rnH -e '%s' *" ',region-string))] |
20908596 | 662 | ["Grep in Parent Dir" |
36ad1553 CD |
663 | (grep (format "grep -rnH -e '%s' ../*" ',region-string))] |
664 | "--" | |
20908596 | 665 | ["Convert to Link" |
36ad1553 CD |
666 | (progn (save-excursion (goto-char (region-beginning)) (insert "[[")) |
667 | (save-excursion (goto-char (region-end)) (insert "]]")))] | |
668 | ["Insert Link Here" (org-mouse-yank-link ',event)])))) | |
669 | ||
670 | ((save-excursion (beginning-of-line) (looking-at "#\\+STARTUP: \\(.*\\)")) | |
20908596 CD |
671 | (popup-menu |
672 | `(nil | |
36ad1553 CD |
673 | ,@(org-mouse-list-options-menu (mapcar 'car org-startup-options) |
674 | 'org-mode-restart)))) | |
20908596 | 675 | ((or (eolp) |
36ad1553 | 676 | (and (looking-at "\\( \\|\t\\)\\(+:[0-9a-zA-Z_:]+\\)?\\( \\|\t\\)+$") |
ed21c5c8 | 677 | (org-looking-back " \\|\t"))) |
36ad1553 CD |
678 | (org-mouse-popup-global-menu)) |
679 | ((get-context :checkbox) | |
20908596 CD |
680 | (popup-menu |
681 | '(nil | |
36ad1553 CD |
682 | ["Toggle" org-toggle-checkbox t] |
683 | ["Remove" org-mouse-remove-match-and-spaces t] | |
684 | "" | |
685 | ["All Clear" (org-mouse-for-each-item | |
686 | (lambda () | |
687 | (when (save-excursion (org-at-item-checkbox-p)) | |
688 | (replace-match "[ ]"))))] | |
689 | ["All Set" (org-mouse-for-each-item | |
690 | (lambda () | |
691 | (when (save-excursion (org-at-item-checkbox-p)) | |
692 | (replace-match "[X]"))))] | |
693 | ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t] | |
694 | ["All Remove" (org-mouse-for-each-item | |
695 | (lambda () | |
696 | (when (save-excursion (org-at-item-checkbox-p)) | |
697 | (org-mouse-remove-match-and-spaces))))] | |
698 | ))) | |
699 | ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_") | |
c8d0cf5c | 700 | (member (match-string 0) org-todo-keywords-1)) |
20908596 | 701 | (popup-menu |
36ad1553 | 702 | `(nil |
c8d0cf5c | 703 | ,@(org-mouse-todo-menu (match-string 0)) |
20908596 | 704 | "--" |
36ad1553 CD |
705 | ["Check TODOs" org-show-todo-tree t] |
706 | ["List all TODO keywords" org-todo-list t] | |
20908596 | 707 | [,(format "List only %s" (match-string 0)) |
36ad1553 CD |
708 | (org-todo-list (match-string 0)) t] |
709 | ))) | |
710 | ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z") | |
711 | (member (match-string 0) stamp-prefixes)) | |
20908596 CD |
712 | (popup-menu |
713 | `(nil | |
714 | ,@(org-mouse-keyword-replace-menu stamp-prefixes) | |
36ad1553 CD |
715 | "--" |
716 | ["Check Deadlines" org-check-deadlines t] | |
717 | ))) | |
718 | ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority | |
20908596 | 719 | (popup-menu `(nil ,@(org-mouse-keyword-replace-menu |
36ad1553 CD |
720 | (org-mouse-priority-list) 1 "Priority %s" t)))) |
721 | ((get-context :link) | |
722 | (popup-menu | |
723 | '(nil | |
724 | ["Open" org-open-at-point t] | |
725 | ["Open in Emacs" (org-open-at-point t) t] | |
726 | "--" | |
c8d0cf5c | 727 | ["Copy link" (org-kill-new (match-string 0))] |
20908596 CD |
728 | ["Cut link" |
729 | (progn | |
36ad1553 CD |
730 | (kill-region (match-beginning 0) (match-end 0)) |
731 | (just-one-space))] | |
732 | "--" | |
733 | ["Grep for TODOs" | |
734 | (grep (format "grep -nH -i 'todo\\|fixme' %s*" (match-string 2)))] | |
735 | ; ["Paste file link" ((insert "file:") (yank))] | |
736 | ))) | |
737 | ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags | |
20908596 CD |
738 | (popup-menu |
739 | `(nil | |
36ad1553 CD |
740 | [,(format "Display '%s'" (match-string 1)) |
741 | (org-tags-view nil ,(match-string 1))] | |
742 | [,(format "Sparse Tree '%s'" (match-string 1)) | |
743 | (org-tags-sparse-tree nil ,(match-string 1))] | |
744 | "--" | |
745 | ,@(org-mouse-tag-menu)))) | |
746 | ((org-at-timestamp-p) | |
20908596 | 747 | (popup-menu |
36ad1553 CD |
748 | '(nil |
749 | ["Show Day" org-open-at-point t] | |
750 | ["Change Timestamp" org-time-stamp t] | |
751 | ["Delete Timestamp" (org-mouse-delete-timestamp) t] | |
752 | ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)] | |
753 | "--" | |
754 | ["Set for Today" org-mouse-timestamp-today] | |
755 | ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)] | |
756 | ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)] | |
757 | ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)] | |
758 | ["Set in a Month" (org-mouse-timestamp-today 1 'month)] | |
759 | "--" | |
760 | ["+ 1 Day" (org-timestamp-change 1 'day)] | |
761 | ["+ 1 Week" (org-timestamp-change 7 'day)] | |
762 | ["+ 1 Month" (org-timestamp-change 1 'month)] | |
763 | "--" | |
764 | ["- 1 Day" (org-timestamp-change -1 'day)] | |
765 | ["- 1 Week" (org-timestamp-change -7 'day)] | |
766 | ["- 1 Month" (org-timestamp-change -1 'month)]))) | |
767 | ((get-context :table-special) | |
768 | (let ((mdata (match-data))) | |
769 | (incf (car mdata) 2) | |
770 | (store-match-data mdata)) | |
771 | (message "match: %S" (match-string 0)) | |
20908596 CD |
772 | (popup-menu `(nil ,@(org-mouse-keyword-replace-menu |
773 | '(" " "!" "^" "_" "$" "#" "*" "'") 0 | |
36ad1553 CD |
774 | (lambda (mark) |
775 | (case (string-to-char mark) | |
776 | (? "( ) Nothing Special") | |
777 | (?! "(!) Column Names") | |
778 | (?^ "(^) Field Names Above") | |
779 | (?_ "(^) Field Names Below") | |
780 | (?$ "($) Formula Parameters") | |
781 | (?# "(#) Recalculation: Auto") | |
782 | (?* "(*) Recalculation: Manual") | |
783 | (?' "(') Recalculation: None"))) t)))) | |
784 | ((assq :table contextlist) | |
785 | (popup-menu | |
786 | '(nil | |
787 | ["Align Table" org-ctrl-c-ctrl-c] | |
788 | ["Blank Field" org-table-blank-field] | |
789 | ["Edit Field" org-table-edit-field] | |
790 | "--" | |
791 | ("Column" | |
792 | ["Move Column Left" org-metaleft] | |
793 | ["Move Column Right" org-metaright] | |
794 | ["Delete Column" org-shiftmetaleft] | |
795 | ["Insert Column" org-shiftmetaright] | |
796 | "--" | |
797 | ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :selected org-table-limit-column-width :style toggle]) | |
798 | ("Row" | |
799 | ["Move Row Up" org-metaup] | |
800 | ["Move Row Down" org-metadown] | |
801 | ["Delete Row" org-shiftmetaup] | |
802 | ["Insert Row" org-shiftmetadown] | |
803 | ["Sort lines in region" org-table-sort-lines (org-at-table-p)] | |
804 | "--" | |
805 | ["Insert Hline" org-table-insert-hline]) | |
806 | ("Rectangle" | |
807 | ["Copy Rectangle" org-copy-special] | |
808 | ["Cut Rectangle" org-cut-special] | |
809 | ["Paste Rectangle" org-paste-special] | |
810 | ["Fill Rectangle" org-table-wrap-region]) | |
811 | "--" | |
812 | ["Set Column Formula" org-table-eval-formula] | |
813 | ["Set Field Formula" (org-table-eval-formula '(4))] | |
814 | ["Edit Formulas" org-table-edit-formulas] | |
815 | "--" | |
816 | ["Recalculate Line" org-table-recalculate] | |
817 | ["Recalculate All" (org-table-recalculate '(4))] | |
818 | ["Iterate All" (org-table-recalculate '(16))] | |
819 | "--" | |
820 | ["Toggle Recalculate Mark" org-table-rotate-recalc-marks] | |
821 | ["Sum Column/Rectangle" org-table-sum | |
822 | :active (or (org-at-table-p) (org-region-active-p))] | |
823 | ["Field Info" org-table-field-info] | |
824 | ["Debug Formulas" | |
825 | (setq org-table-formula-debug (not org-table-formula-debug)) | |
826 | :style toggle :selected org-table-formula-debug] | |
827 | ))) | |
828 | ((and (assq :headline contextlist) (not (eolp))) | |
829 | (let ((priority (org-mouse-get-priority t))) | |
830 | (popup-menu | |
831 | `("Headline Menu" | |
20908596 CD |
832 | ("Tags and Priorities" |
833 | ,@(org-mouse-keyword-menu | |
834 | (org-mouse-priority-list) | |
835 | '(lambda (keyword) | |
36ad1553 CD |
836 | (org-mouse-set-priority (string-to-char keyword))) |
837 | priority "Priority %s") | |
838 | "--" | |
839 | ,@(org-mouse-tag-menu)) | |
840 | ("TODO Status" | |
c8d0cf5c | 841 | ,@(org-mouse-todo-menu (org-get-todo-state))) |
20908596 | 842 | ["Show Tags" |
36ad1553 CD |
843 | (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags)) |
844 | :visible (not org-mouse-direct)] | |
20908596 | 845 | ["Show Priority" |
36ad1553 CD |
846 | (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority)) |
847 | :visible (not org-mouse-direct)] | |
848 | ,@(if org-mouse-direct '("--") nil) | |
849 | ["New Heading" org-mouse-insert-heading :visible org-mouse-direct] | |
20908596 CD |
850 | ["Set Deadline" |
851 | (progn (org-mouse-end-headline) (insert " ") (org-deadline)) | |
852 | :active (not (save-excursion | |
36ad1553 | 853 | (org-mouse-re-search-line org-deadline-regexp)))] |
20908596 CD |
854 | ["Schedule Task" |
855 | (progn (org-mouse-end-headline) (insert " ") (org-schedule)) | |
856 | :active (not (save-excursion | |
36ad1553 | 857 | (org-mouse-re-search-line org-scheduled-regexp)))] |
20908596 | 858 | ["Insert Timestamp" |
36ad1553 CD |
859 | (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t] |
860 | ; ["Timestamp (inactive)" org-time-stamp-inactive t] | |
861 | "--" | |
862 | ["Archive Subtree" org-archive-subtree] | |
863 | ["Cut Subtree" org-cut-special] | |
864 | ["Copy Subtree" org-copy-special] | |
865 | ["Paste Subtree" org-paste-special :visible org-mouse-direct] | |
20908596 | 866 | ("Sort Children" |
36ad1553 CD |
867 | ["Alphabetically" (org-sort-entries nil ?a)] |
868 | ["Numerically" (org-sort-entries nil ?n)] | |
869 | ["By Time/Date" (org-sort-entries nil ?t)] | |
870 | "--" | |
871 | ["Reverse Alphabetically" (org-sort-entries nil ?A)] | |
872 | ["Reverse Numerically" (org-sort-entries nil ?N)] | |
873 | ["Reverse By Time/Date" (org-sort-entries nil ?T)]) | |
874 | "--" | |
875 | ["Move Trees" org-mouse-move-tree :active nil] | |
876 | )))) | |
20908596 | 877 | (t |
36ad1553 CD |
878 | (org-mouse-popup-global-menu)))))) |
879 | ||
880 | ;; (defun org-mouse-at-regexp (regexp) | |
881 | ;; (save-excursion | |
882 | ;; (let ((point (point)) | |
33306645 CD |
883 | ;; (bol (progn (beginning-of-line) (point))) |
884 | ;; (eol (progn (end-of-line) (point)))) | |
885 | ;; (goto-char point) | |
886 | ;; (re-search-backward regexp bol 1) | |
887 | ;; (and (not (eolp)) | |
888 | ;; (progn (forward-char) | |
889 | ;; (re-search-forward regexp eol t)) | |
890 | ;; (<= (match-beginning 0) point))))) | |
36ad1553 CD |
891 | |
892 | (defun org-mouse-mark-active () | |
893 | (and mark-active transient-mark-mode)) | |
894 | ||
895 | (defun org-mouse-in-region-p (pos) | |
20908596 CD |
896 | (and (org-mouse-mark-active) |
897 | (>= pos (region-beginning)) | |
36ad1553 CD |
898 | (< pos (region-end)))) |
899 | ||
900 | (defun org-mouse-down-mouse (event) | |
901 | (interactive "e") | |
902 | (setq this-command last-command) | |
903 | (unless (and (= 1 (event-click-count event)) | |
904 | (org-mouse-in-region-p (posn-point (event-start event)))) | |
905 | (mouse-drag-region event))) | |
906 | ||
907 | (add-hook 'org-mode-hook | |
908 | '(lambda () | |
909 | (setq org-mouse-context-menu-function 'org-mouse-context-menu) | |
910 | ||
71d35b24 | 911 | (when (memq 'context-menu org-mouse-features) |
86fbb8ca CD |
912 | (org-defkey org-mouse-map [mouse-3] nil) |
913 | (org-defkey org-mode-map [mouse-3] 'org-mouse-show-context-menu)) | |
914 | (org-defkey org-mode-map [down-mouse-1] 'org-mouse-down-mouse) | |
71d35b24 | 915 | (when (memq 'context-menu org-mouse-features) |
86fbb8ca CD |
916 | (org-defkey org-mouse-map [C-drag-mouse-1] 'org-mouse-move-tree) |
917 | (org-defkey org-mouse-map [C-down-mouse-1] 'org-mouse-move-tree-start)) | |
71d35b24 | 918 | (when (memq 'yank-link org-mouse-features) |
86fbb8ca CD |
919 | (org-defkey org-mode-map [S-mouse-2] 'org-mouse-yank-link) |
920 | (org-defkey org-mode-map [drag-mouse-3] 'org-mouse-yank-link)) | |
71d35b24 | 921 | (when (memq 'move-tree org-mouse-features) |
86fbb8ca CD |
922 | (org-defkey org-mouse-map [drag-mouse-3] 'org-mouse-move-tree) |
923 | (org-defkey org-mouse-map [down-mouse-3] 'org-mouse-move-tree-start)) | |
71d35b24 CD |
924 | |
925 | (when (memq 'activate-stars org-mouse-features) | |
926 | (font-lock-add-keywords | |
927 | nil | |
20908596 CD |
928 | `((,outline-regexp |
929 | 0 `(face org-link mouse-face highlight keymap ,org-mouse-map) | |
71d35b24 | 930 | 'prepend)) |
33306645 | 931 | t)) |
71d35b24 CD |
932 | |
933 | (when (memq 'activate-bullets org-mouse-features) | |
934 | (font-lock-add-keywords | |
935 | nil | |
936 | `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +" | |
937 | (1 `(face org-link keymap ,org-mouse-map mouse-face highlight) | |
938 | 'prepend))) | |
33306645 | 939 | t)) |
71d35b24 CD |
940 | |
941 | (when (memq 'activate-checkboxes org-mouse-features) | |
942 | (font-lock-add-keywords | |
943 | nil | |
944 | `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)" | |
36ad1553 | 945 | (2 `(face bold keymap ,org-mouse-map mouse-face highlight) t))) |
33306645 | 946 | t)) |
36ad1553 CD |
947 | |
948 | (defadvice org-open-at-point (around org-mouse-open-at-point activate) | |
949 | (let ((context (org-context))) | |
20908596 | 950 | (cond |
36ad1553 CD |
951 | ((assq :headline-stars context) (org-cycle)) |
952 | ((assq :checkbox context) (org-toggle-checkbox)) | |
953 | ((assq :item-bullet context) | |
954 | (let ((org-cycle-include-plain-lists t)) (org-cycle))) | |
955 | (t ad-do-it)))))) | |
956 | ||
957 | (defun org-mouse-move-tree-start (event) | |
958 | (interactive "e") | |
959 | (message "Same line: promote/demote, (***):move before, (text): make a child")) | |
960 | ||
961 | ||
962 | (defun org-mouse-make-marker (position) | |
963 | (with-current-buffer (window-buffer (posn-window position)) | |
964 | (copy-marker (posn-point position)))) | |
965 | ||
966 | (defun org-mouse-move-tree (event) | |
967 | ;; todo: handle movements between different buffers | |
968 | (interactive "e") | |
969 | (save-excursion | |
970 | (let* ((start (org-mouse-make-marker (event-start event))) | |
971 | (end (org-mouse-make-marker (event-end event))) | |
972 | (sbuf (marker-buffer start)) | |
973 | (ebuf (marker-buffer end))) | |
974 | ||
975 | (when (and sbuf ebuf) | |
976 | (set-buffer sbuf) | |
977 | (goto-char start) | |
978 | (org-back-to-heading) | |
979 | (if (and (eq sbuf ebuf) | |
20908596 | 980 | (equal |
36ad1553 CD |
981 | (point) |
982 | (save-excursion (goto-char end) (org-back-to-heading) (point)))) | |
983 | ;; if the same line then promote/demote | |
984 | (if (>= end start) (org-demote-subtree) (org-promote-subtree)) | |
985 | ;; if different lines then move | |
986 | (org-cut-subtree) | |
20908596 | 987 | |
36ad1553 CD |
988 | (set-buffer ebuf) |
989 | (goto-char end) | |
990 | (org-back-to-heading) | |
991 | (when (and (eq sbuf ebuf) | |
20908596 | 992 | (equal |
36ad1553 | 993 | (point) |
20908596 | 994 | (save-excursion (goto-char start) |
36ad1553 CD |
995 | (org-back-to-heading) (point)))) |
996 | (outline-end-of-subtree) | |
997 | (end-of-line) | |
998 | (if (eobp) (newline) (forward-char))) | |
20908596 | 999 | |
36ad1553 CD |
1000 | (when (looking-at outline-regexp) |
1001 | (let ((level (- (match-end 0) (match-beginning 0)))) | |
1002 | (when (> end (match-end 0)) | |
1003 | (outline-end-of-subtree) | |
1004 | (end-of-line) | |
1005 | (if (eobp) (newline) (forward-char)) | |
1006 | (setq level (1+ level))) | |
1007 | (org-paste-subtree level) | |
1008 | (save-excursion | |
1009 | (outline-end-of-subtree) | |
1010 | (when (bolp) (delete-char -1)))))))))) | |
1011 | ||
1012 | ||
1013 | (defun org-mouse-transform-to-outline () | |
1014 | (interactive) | |
1015 | (org-back-to-heading) | |
1016 | (let ((minlevel 1000) | |
1017 | (replace-text (concat (match-string 0) "* "))) | |
1018 | (beginning-of-line 2) | |
1019 | (save-excursion | |
1020 | (while (not (or (eobp) (looking-at outline-regexp))) | |
1021 | (when (looking-at org-mouse-plain-list-regexp) | |
1022 | (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1))))) | |
1023 | (forward-line))) | |
1024 | (while (not (or (eobp) (looking-at outline-regexp))) | |
1025 | (when (and (looking-at org-mouse-plain-list-regexp) | |
1026 | (eq minlevel (- (match-end 1) (match-beginning 1)))) | |
1027 | (replace-match replace-text)) | |
1028 | (forward-line)))) | |
1029 | ||
1030 | (defvar _cmd) ;dynamically scoped from `org-with-remote-undo'. | |
1031 | ||
1032 | (defun org-mouse-do-remotely (command) | |
1033 | ; (org-agenda-check-no-diary) | |
1034 | (when (get-text-property (point) 'org-marker) | |
1035 | (let* ((anticol (- (point-at-eol) (point))) | |
1036 | (marker (get-text-property (point) 'org-marker)) | |
1037 | (buffer (marker-buffer marker)) | |
1038 | (pos (marker-position marker)) | |
1039 | (hdmarker (get-text-property (point) 'org-hd-marker)) | |
1040 | (buffer-read-only nil) | |
1041 | (newhead "--- removed ---") | |
1042 | (org-mouse-direct nil) | |
1043 | (org-mouse-main-buffer (current-buffer))) | |
1044 | (when (eq (with-current-buffer buffer major-mode) 'org-mode) | |
81ad75af | 1045 | (let ((endmarker (with-current-buffer buffer |
8bfe682a CD |
1046 | (outline-end-of-subtree) |
1047 | (forward-char 1) | |
1048 | (copy-marker (point))))) | |
36ad1553 CD |
1049 | (org-with-remote-undo buffer |
1050 | (with-current-buffer buffer | |
1051 | (widen) | |
1052 | (goto-char pos) | |
1053 | (org-show-hidden-entry) | |
1054 | (save-excursion | |
1055 | (and (outline-next-heading) | |
1056 | (org-flag-heading nil))) ; show the next heading | |
1057 | (org-back-to-heading) | |
1058 | (setq marker (copy-marker (point))) | |
1059 | (goto-char (max (point-at-bol) (- (point-at-eol) anticol))) | |
1060 | (funcall command) | |
1061 | (message "_cmd: %S" _cmd) | |
1062 | (message "this-command: %S" this-command) | |
1063 | (unless (eq (marker-position marker) (marker-position endmarker)) | |
1064 | (setq newhead (org-get-heading)))) | |
20908596 | 1065 | |
36ad1553 CD |
1066 | (beginning-of-line 1) |
1067 | (save-excursion | |
1068 | (org-agenda-change-all-lines newhead hdmarker 'fixface)))) | |
1069 | t)))) | |
1070 | ||
1071 | (defun org-mouse-agenda-context-menu (&optional event) | |
1072 | (or (org-mouse-do-remotely 'org-mouse-context-menu) | |
20908596 | 1073 | (popup-menu |
36ad1553 CD |
1074 | '("Agenda" |
1075 | ("Agenda Files") | |
1076 | "--" | |
1077 | ["Undo" (progn (message "last command: %S" last-command) (setq this-command 'org-agenda-undo) (org-agenda-undo)) | |
20908596 | 1078 | :visible (if (eq last-command 'org-agenda-undo) |
36ad1553 CD |
1079 | org-agenda-pending-undo-list |
1080 | org-agenda-undo-list)] | |
1081 | ["Rebuild Buffer" org-agenda-redo t] | |
20908596 | 1082 | ["New Diary Entry" |
36ad1553 CD |
1083 | org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline) t] |
1084 | "--" | |
20908596 | 1085 | ["Goto Today" org-agenda-goto-today |
36ad1553 | 1086 | (org-agenda-check-type nil 'agenda 'timeline) t] |
20908596 | 1087 | ["Display Calendar" org-agenda-goto-calendar |
36ad1553 CD |
1088 | (org-agenda-check-type nil 'agenda 'timeline) t] |
1089 | ("Calendar Commands" | |
20908596 | 1090 | ["Phases of the Moon" org-agenda-phases-of-moon |
36ad1553 | 1091 | (org-agenda-check-type nil 'agenda 'timeline)] |
20908596 | 1092 | ["Sunrise/Sunset" org-agenda-sunrise-sunset |
36ad1553 | 1093 | (org-agenda-check-type nil 'agenda 'timeline)] |
20908596 | 1094 | ["Holidays" org-agenda-holidays |
36ad1553 | 1095 | (org-agenda-check-type nil 'agenda 'timeline)] |
20908596 | 1096 | ["Convert" org-agenda-convert-date |
36ad1553 CD |
1097 | (org-agenda-check-type nil 'agenda 'timeline)] |
1098 | "--" | |
1099 | ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]) | |
1100 | "--" | |
20908596 | 1101 | ["Day View" org-agenda-day-view |
36ad1553 | 1102 | :active (org-agenda-check-type nil 'agenda) |
acedf35c | 1103 | :style radio :selected (eq org-agenda-current-span 'day)] |
20908596 | 1104 | ["Week View" org-agenda-week-view |
36ad1553 | 1105 | :active (org-agenda-check-type nil 'agenda) |
acedf35c | 1106 | :style radio :selected (eq org-agenda-current-span 'week)] |
36ad1553 CD |
1107 | "--" |
1108 | ["Show Logbook entries" org-agenda-log-mode | |
20908596 | 1109 | :style toggle :selected org-agenda-show-log |
36ad1553 CD |
1110 | :active (org-agenda-check-type nil 'agenda 'timeline)] |
1111 | ["Include Diary" org-agenda-toggle-diary | |
20908596 | 1112 | :style toggle :selected org-agenda-include-diary |
36ad1553 CD |
1113 | :active (org-agenda-check-type nil 'agenda)] |
1114 | ["Use Time Grid" org-agenda-toggle-time-grid | |
1115 | :style toggle :selected org-agenda-use-time-grid | |
1116 | :active (org-agenda-check-type nil 'agenda)] | |
1117 | ["Follow Mode" org-agenda-follow-mode | |
1118 | :style toggle :selected org-agenda-follow-mode] | |
1119 | "--" | |
1120 | ["Quit" org-agenda-quit t] | |
1121 | ["Exit and Release Buffers" org-agenda-exit t] | |
1122 | )))) | |
1123 | ||
1124 | (defun org-mouse-get-gesture (event) | |
1125 | (let ((startxy (posn-x-y (event-start event))) | |
1126 | (endxy (posn-x-y (event-end event)))) | |
1127 | (if (< (car startxy) (car endxy)) :right :left))) | |
1128 | ||
1129 | ||
1130 | ; (setq org-agenda-mode-hook nil) | |
20908596 | 1131 | (add-hook 'org-agenda-mode-hook |
36ad1553 CD |
1132 | '(lambda () |
1133 | (setq org-mouse-context-menu-function 'org-mouse-agenda-context-menu) | |
86fbb8ca CD |
1134 | (org-defkey org-agenda-mode-map [mouse-3] 'org-mouse-show-context-menu) |
1135 | (org-defkey org-agenda-mode-map [down-mouse-3] 'org-mouse-move-tree-start) | |
1136 | (org-defkey org-agenda-mode-map [C-mouse-4] 'org-agenda-earlier) | |
1137 | (org-defkey org-agenda-mode-map [C-mouse-5] 'org-agenda-later) | |
1138 | (org-defkey org-agenda-mode-map [drag-mouse-3] | |
36ad1553 CD |
1139 | '(lambda (event) (interactive "e") |
1140 | (case (org-mouse-get-gesture event) | |
1141 | (:left (org-agenda-earlier 1)) | |
1142 | (:right (org-agenda-later 1))))))) | |
1143 | ||
1144 | (provide 'org-mouse) | |
fcd094c7 MB |
1145 | |
1146 | ;; arch-tag: ff1ae557-3529-41a3-95c6-baaebdcc280f | |
b349f79f | 1147 | |
86fbb8ca | 1148 | ;;; org-mouse.el ends here |