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