org-mouse.el 38 KB

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