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