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