org-mouse.el 38 KB

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