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