org-mouse.el 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. ;;; org-mouse.el --- Better mouse support for org-mode
  2. ;; Copyright (c) 2006 Piotr Zielinski
  3. ;;
  4. ;; Author: Piotr Zielinski <piotr dot zielinski at gmail dot com>
  5. ;; Version: 0.18
  6. ;; $Id: org-mouse.el 254 2006-10-26 21:15:52Z pz215 $
  7. ;;
  8. ;; The latest version of this file is available from
  9. ;;
  10. ;; http://www.cl.cam.ac.uk/~pz215/files/org-mouse.el
  11. ;;
  12. ;; This file is *NOT* part of GNU Emacs.
  13. ;; This file is distributed under the same terms as GNU Emacs.
  14. ;; This program is free software; you can redistribute it and/or
  15. ;; modify it under the terms of the GNU General Public License as
  16. ;; published by the Free Software Foundation; either version 2 of
  17. ;; the License, or (at your option) any later version.
  18. ;; This program is distributed in the hope that it will be
  19. ;; useful, but WITHOUT ANY WARRANTY; without even the implied
  20. ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  21. ;; PURPOSE. See the GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public
  23. ;; License along with this program; if not, write to the Free
  24. ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. ;; MA 02111-1307 USA
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27. ;;
  28. ;;; Commentary:
  29. ;;
  30. ;; Org-mouse provides better mouse support for org-mode. Org-mode is
  31. ;; a mode for keeping notes, maintaining ToDo lists, and doing project
  32. ;; planning with a fast and effective plain-text system. It is
  33. ;; available from
  34. ;;
  35. ;; http://staff.science.uva.nl/~dominik/Tools/org/
  36. ;;
  37. ;; Org-mouse implements the following features:
  38. ;; * following links with the left mouse button (in Emacs 22)
  39. ;; * subtree expansion/collapse (org-cycle) with the left mouse button
  40. ;; * several context menus on the right mouse button:
  41. ;; + general text
  42. ;; + headlines
  43. ;; + timestamps
  44. ;; + priorities
  45. ;; + links
  46. ;; + tags
  47. ;; * promoting/demoting/moving subtrees with mouse-3
  48. ;; + if the drag starts and ends in the same line then promote/demote
  49. ;; + otherwise move the subtree
  50. ;; * date/time extraction from selected text (requires a python script)
  51. ;; (eg. select text from your email and click "Add Appointment")
  52. ;;
  53. ;; The python script that automatically extracts date/time information
  54. ;; from a piece of English text is available from:
  55. ;;
  56. ;; http://www.cl.cam.ac.uk/~pz215/files/timeparser.py
  57. ;;
  58. ;; Use
  59. ;; ------------
  60. ;;
  61. ;; To use this package, put the following line in your .emacs:
  62. ;;
  63. ;; (require 'org-mouse)
  64. ;;
  65. ;; Tested with Emacs 22.0.50, org-mode 4.33
  66. ;; Fixme:
  67. ;; + deal with folding / unfolding issues
  68. ;; TODO (This list is only theoretical, if you'd like to have some
  69. ;; feature implemented or a bug fix please send me an email, even if
  70. ;; something similar appears in the list below. This will help me get
  71. ;; the priorities right.):
  72. ;; + The "New Appointment" menu entry seems out of place. Remove it
  73. ;; and enhance the time/data selection function so that if the text
  74. ;; in the clipboard contains a date/time, then set that date as the
  75. ;; default (instead of "today")
  76. ;; + org-store-link, insert link
  77. ;; + org tables
  78. ;; + occur with the current word/tag (same menu item)
  79. ;; + ctrl-c ctrl-c, for example, renumber the current list
  80. ;; + internal links
  81. ;; Please email me with new feature suggestions / bugs
  82. ;; History:
  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-mode 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-mode 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. (eval-when-compile (require 'cl))
  126. (defvar org-mouse-plain-list-regexp "\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) ")
  127. (defvar org-mouse-direct t)
  128. (defgroup org-mouse nil
  129. "Org-mouse"
  130. :tag "Org Mouse."
  131. :group 'org)
  132. (defcustom org-mouse-punctuation ":"
  133. ""
  134. :group 'org-mouse
  135. :type 'string)
  136. (defun org-mouse-re-search-line (regexp)
  137. "Searches the current line for a given regular expression."
  138. (beginning-of-line)
  139. (re-search-forward regexp (point-at-eol) t))
  140. (defun org-mouse-end-headline ()
  141. "Go to the end of current headline (ignoring tags)."
  142. (interactive)
  143. (end-of-line)
  144. (skip-chars-backward "\t ")
  145. (when (looking-back ":[A-Za-z]+:")
  146. (skip-chars-backward ":A-Za-z")
  147. (skip-chars-backward "\t ")))
  148. (defun org-mouse-show-context-menu (event prefix)
  149. (interactive "@e \nP")
  150. (if (and (= (event-click-count event) 1)
  151. (or (not mark-active)
  152. (sit-for (/ double-click-time 1000.0))))
  153. (progn
  154. (select-window (posn-window (event-start event)))
  155. (goto-char (posn-point (event-start event)))
  156. (when (not (eolp)) (save-excursion (run-hooks 'post-command-hook)))
  157. (let ((redisplay-dont-pause t))
  158. (sit-for 0))
  159. (if (functionp org-mouse-context-menu-function)
  160. (funcall org-mouse-context-menu-function)
  161. (mouse-major-mode-menu event prefix))
  162. )
  163. (setq this-command 'mouse-save-then-kill)
  164. (mouse-save-then-kill event)))
  165. (defun org-mouse-line-position ()
  166. "Returns :beginning :middle :end"
  167. (cond
  168. ((eolp) :end)
  169. ((org-mouse-bolp) :begin)
  170. (t :middle)))
  171. (defun org-mouse-empty-line ()
  172. (save-excursion (beginning-of-line) (looking-at "[ \t]*$")))
  173. (defun org-mouse-next-heading ()
  174. "Goes to the next heading and if there is none, it ensures that the point is at the beginning of an empty line."
  175. (unless (outline-next-heading)
  176. (beginning-of-line)
  177. (unless (org-mouse-empty-line)
  178. (end-of-line)
  179. (newline))))
  180. (defun org-mouse-insert-heading ()
  181. (interactive)
  182. (case (org-mouse-line-position)
  183. (:begin (beginning-of-line)
  184. (org-insert-heading))
  185. (t (org-mouse-next-heading)
  186. (org-insert-heading))))
  187. (defun org-mouse-timestamp-today (&optional shift units)
  188. (interactive)
  189. (flet ((org-read-date (x &optional y) (current-time)))
  190. (org-time-stamp nil))
  191. (when shift
  192. (org-timestamp-change shift units)))
  193. (defun org-mouse-keyword-menu (keywords function &optional selected itemformat)
  194. (mapcar
  195. (lambda (keyword)
  196. (vector (cond
  197. ((functionp itemformat) (funcall itemformat keyword))
  198. ((stringp itemformat) (format itemformat keyword))
  199. (t keyword))
  200. `(funcall ,function ,keyword)
  201. :style (cond
  202. ((null selected) t)
  203. ((functionp selected) 'toggle)
  204. (t 'radio))
  205. :selected `(if (functionp ,selected)
  206. (funcall ,selected ,keyword)
  207. (equal ,selected ,keyword))))
  208. keywords))
  209. (defun org-mouse-remove-match-and-spaces ()
  210. (interactive)
  211. (replace-match "")
  212. (when (looking-at " +")
  213. (replace-match "")))
  214. (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat)
  215. (setq group (or group 0))
  216. (append
  217. (org-mouse-keyword-menu
  218. keywords
  219. `(lambda (keyword) (replace-match keyword t t nil ,group))
  220. `(match-string ,group)
  221. itemformat)
  222. '(["None" org-mouse-remove-match-and-spaces t])))
  223. (defvar org-mouse-context-menu-function nil)
  224. (make-variable-buffer-local 'org-mouse-context-menu-function)
  225. (defun org-mouse-show-headlines ()
  226. (interactive)
  227. (let ((this-command 'org-cycle)
  228. (last-command 'org-cycle)
  229. (org-cycle-global-status nil))
  230. (org-cycle '(4))
  231. (org-cycle '(4))))
  232. (defun org-mouse-show-overview ()
  233. (interactive)
  234. (let ((org-cycle-global-status nil))
  235. (org-cycle '(4))))
  236. (defun org-mouse-set-priority (priority)
  237. (flet ((read-char-exclusive () priority))
  238. (org-priority)))
  239. (defvar org-mouse-priority-regexp "\\[#\\([A-Z]\\)\\]"
  240. "Regular expression matching the priority indicator. Differs from `org-priority-regexp' in that it doesn't contain the leading '.*?'.")
  241. (defun org-mouse-get-priority (&optional default)
  242. (save-excursion
  243. (if (org-mouse-re-search-line org-mouse-priority-regexp)
  244. (match-string 1)
  245. (when default (char-to-string org-default-priority)))))
  246. (defun org-mouse-at-link ()
  247. (and (eq (get-text-property (point) 'face) 'org-link)
  248. (save-excursion
  249. (goto-char (previous-single-property-change (point) 'face))
  250. (or (looking-at org-bracket-link-regexp)
  251. (looking-at org-angle-link-re)
  252. (looking-at org-plain-link-re)))))
  253. (defun org-mouse-delete-timestamp ()
  254. "Deletes the current timestamp as well as the preceding
  255. SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:"
  256. (when (or (org-at-date-range-p) (org-at-timestamp-p))
  257. (replace-match "") ; delete the timestamp
  258. (skip-chars-backward " :A-Z")
  259. (when (looking-at " *[A-Z][A-Z]+:")
  260. (replace-match ""))))
  261. (defun org-mouse-looking-at (regexp skipchars &optional movechars)
  262. (save-excursion
  263. (let ((point (point)))
  264. (if (looking-at regexp) t
  265. (skip-chars-backward skipchars)
  266. (forward-char (or movechars 0))
  267. (when (looking-at regexp)
  268. (> (match-end 0) point))))))
  269. (defun org-mouse-priority-list ()
  270. (let ((ret) (current org-lowest-priority))
  271. (while (>= current ?A)
  272. (push (char-to-string current) ret)
  273. (decf current))
  274. ret))
  275. (defun org-mouse-tag-menu () ;todo
  276. (append
  277. (let ((tags (org-split-string (org-get-tags) ":")))
  278. (org-mouse-keyword-menu
  279. (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
  280. `(lambda (tag)
  281. (org-mouse-set-tags
  282. (sort (if (member tag (quote ,tags))
  283. (delete tag (quote ,tags))
  284. (cons tag (quote ,tags)))
  285. 'string-lessp)))
  286. `(lambda (tag) (member tag (quote ,tags)))
  287. ))
  288. '("--"
  289. ["Align Tags Here" (org-set-tags nil t) t]
  290. ["Align Tags in Buffer" (org-set-tags t t) t]
  291. ["Set Tags ..." (org-set-tags) t])))
  292. (defun org-mouse-set-tags (tags)
  293. (save-excursion
  294. ;; remove existing tags first
  295. (beginning-of-line)
  296. (when (org-mouse-re-search-line ":\\(\\([A-Za-z_]+:\\)+\\)")
  297. (replace-match ""))
  298. ;; set new tags if any
  299. (when tags
  300. (end-of-line)
  301. (insert " :" (mapconcat 'identity tags ":") ":")
  302. (org-set-tags nil t))))
  303. (defun org-mouse-insert-checkbox ()
  304. (interactive)
  305. (and (org-at-item-p)
  306. (goto-char (match-end 0))
  307. (unless (org-at-item-checkbox-p)
  308. (delete-horizontal-space)
  309. (insert " [ ] "))))
  310. (defun org-mouse-agenda-type (type)
  311. (case type
  312. ('tags "Tags: ")
  313. ('todo "TODO: ")
  314. ('tags-tree "Tags tree: ")
  315. ('todo-tree "TODO tree: ")
  316. ('occur-tree "Occur tree: ")
  317. (t "Agenda command ???")))
  318. (defun org-mouse-clip-text (text maxlength)
  319. (if (> (length text) maxlength)
  320. (concat (substring text 0 (- maxlength 3)) "...")
  321. text))
  322. (defun org-mouse-popup-global-menu ()
  323. (popup-menu
  324. `("Main Menu"
  325. ["Show Overview" org-mouse-show-overview t]
  326. ["Show Headlines" org-mouse-show-headlines t]
  327. ["Show All" show-all t]
  328. ["Remove Highlights" org-remove-occur-highlights
  329. :visible org-occur-highlights]
  330. "--"
  331. ["Check Deadlines"
  332. (if (functionp 'org-check-deadlines-and-todos)
  333. (org-check-deadlines-and-todos org-deadline-warning-days)
  334. (org-check-deadlines org-deadline-warning-days)) t]
  335. ["Check TODOs" org-show-todo-tree t]
  336. ("Check Tags"
  337. ,@(org-mouse-keyword-menu
  338. (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
  339. '(lambda (tag) (org-tags-sparse-tree nil tag)))
  340. "--"
  341. ["Custom Tag ..." org-tags-sparse-tree t])
  342. ["Check Phrase ..." org-occur]
  343. "--"
  344. ["Display Agenda" org-agenda-list t]
  345. ["Display Timeline" org-timeline t]
  346. ["Display TODO List" org-todo-list t]
  347. ("Display Tags"
  348. ,@(org-mouse-keyword-menu
  349. (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
  350. '(lambda (tag) (org-tags-view nil tag)))
  351. "--"
  352. ["Custom Tag ..." org-tags-view t])
  353. ["Display Calendar" org-goto-calendar t]
  354. "--"
  355. ;; ("Custom Commands"
  356. ;; ,@(org-mouse-keyword-menu
  357. ;; (mapcar 'car org-agenda-custom-commands)
  358. ;; '(lambda (key)
  359. ;; (eval `(flet ((read-char-exclusive () (string-to-char ,key)))
  360. ;; (let ((current-prefix-arg t))
  361. ;; (org-agenda nil)))))
  362. ;; nil "Agenda (TODO) '%s'")
  363. ;; "--"
  364. ,@(org-mouse-keyword-menu
  365. (mapcar 'car org-agenda-custom-commands)
  366. '(lambda (key)
  367. (eval `(flet ((read-char-exclusive () (string-to-char ,key)))
  368. (org-agenda nil))))
  369. nil
  370. '(lambda (key)
  371. (let ((entry (assoc key org-agenda-custom-commands)))
  372. (org-mouse-clip-text
  373. (cond
  374. ((stringp (nth 1 entry)) (nth 1 entry))
  375. ((stringp (nth 2 entry))
  376. (concat (org-mouse-agenda-type (nth 1 entry))
  377. (nth 2 entry)))
  378. (t "Agenda Command '%s'"))
  379. 30))))
  380. ;; )
  381. "--"
  382. ["Delete Blank Lines" delete-blank-lines
  383. :visible (org-mouse-empty-line)]
  384. ["Insert Checkbox" org-mouse-insert-checkbox
  385. :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
  386. ["Insert Checkboxes"
  387. (org-mouse-for-each-item 'org-mouse-insert-checkbox)
  388. :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
  389. ["Plain List to Outline" org-mouse-transform-to-outline
  390. :visible (org-at-item-p)])))
  391. ; ["Jump" org-goto])))
  392. (defun org-mouse-get-context (contextlist context)
  393. (let ((contextdata (find-if (lambda (x) (eq (car x) context)) contextlist)))
  394. (when contextdata
  395. (save-excursion
  396. (goto-char (nth 1 contextdata))
  397. ; (looking-at regexp)))))
  398. (re-search-forward ".*" (nth 2 contextdata))))))
  399. (defun org-mouse-for-each-item (function)
  400. (save-excursion
  401. (ignore-errors
  402. (while t (org-previous-item)))
  403. (ignore-errors
  404. (while t
  405. (funcall function)
  406. (org-next-item)))))
  407. (defun org-mouse-bolp ()
  408. "Returns true if there only spaces, tabs, and '*', between the beginning of line and the point"
  409. (save-excursion
  410. (skip-chars-backward " \t*") (bolp)))
  411. (defadvice dnd-insert-text (around org-mouse-dnd-insert-text activate)
  412. (if (eq major-mode 'org-mode)
  413. (case (org-mouse-line-position)
  414. (:begin ; insert before
  415. (beginning-of-line)
  416. (looking-at "[ \t]*")
  417. (open-line 1)
  418. (indent-to (- (match-end 0) (match-beginning 0)))
  419. (insert "+ "))
  420. (:middle ; insert after
  421. (end-of-line)
  422. (newline t)
  423. (indent-relative)
  424. (insert "+ "))
  425. (:end ; insert text here
  426. (skip-chars-backward " \t")
  427. (kill-region (point) (point-at-eol))
  428. (unless (looking-back org-mouse-punctuation)
  429. (insert (concat org-mouse-punctuation " "))))
  430. (insert text)
  431. (beginning-of-line))
  432. ad-do-it))
  433. (defun org-mouse-context-menu ()
  434. (let ((stamp-prefixes (list org-deadline-string org-scheduled-string))
  435. (contextlist (org-context)))
  436. (flet ((get-context (context) (org-mouse-get-context contextlist context)))
  437. (cond
  438. ((or (eolp)
  439. (and (looking-at " \\|\t") (looking-back " \\|\t")))
  440. (org-mouse-popup-global-menu))
  441. ;; ((get-context :todo-keyword)
  442. ((get-context :checkbox)
  443. (popup-menu
  444. '(nil
  445. ["Toggle" org-toggle-checkbox t]
  446. ["Remove" org-mouse-remove-match-and-spaces t]
  447. ""
  448. ["All Clear" (org-mouse-for-each-item
  449. (lambda ()
  450. (when (save-excursion (org-at-item-checkbox-p))
  451. (replace-match "[ ]"))))]
  452. ["All Set" (org-mouse-for-each-item
  453. (lambda ()
  454. (when (save-excursion (org-at-item-checkbox-p))
  455. (replace-match "[X]"))))]
  456. ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t]
  457. ["All Remove" (org-mouse-for-each-item
  458. (lambda ()
  459. (when (save-excursion (org-at-item-checkbox-p))
  460. (org-mouse-remove-match-and-spaces))))]
  461. )))
  462. ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_")
  463. (member (match-string 0) org-todo-keywords))
  464. (popup-menu
  465. `(nil
  466. ,@(org-mouse-keyword-replace-menu org-todo-keywords)
  467. "--"
  468. ["Check TODOs" org-show-todo-tree t]
  469. ["Display TODO List" org-todo-list t]
  470. )))
  471. ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z")
  472. (member (match-string 0) stamp-prefixes))
  473. (popup-menu
  474. `(nil
  475. ,@(org-mouse-keyword-replace-menu stamp-prefixes)
  476. "--"
  477. ["Check Deadlines" org-check-deadlines t]
  478. )))
  479. ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority
  480. (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
  481. (org-mouse-priority-list) 1 "Priority %s"))))
  482. ((org-mouse-at-link)
  483. (popup-menu
  484. '(nil
  485. ["Open" org-open-at-point t]
  486. ["Open in Emacs" (org-open-at-point t) t]
  487. "--"
  488. ["Copy link" (kill-new (match-string 0))]
  489. ["Cut link" (kill-region (match-beginning 0) (match-end 0))]
  490. ; ["Paste file link" ((insert "file:") (yank))]
  491. )))
  492. ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags
  493. (popup-menu
  494. `(nil
  495. [,(format "Display '%s'" (match-string 1))
  496. (org-tags-view nil ,(match-string 1))]
  497. [,(format "Narrow to '%s'" (match-string 1))
  498. (org-tags-sparse-tree nil ,(match-string 1))]
  499. "--"
  500. ,@(org-mouse-tag-menu))))
  501. ((org-at-timestamp-p)
  502. (popup-menu
  503. '(nil
  504. ["Show Day" org-open-at-point t]
  505. ["Change Timestamp" org-time-stamp t]
  506. ["Delete Timestamp" (org-mouse-delete-timestamp) t]
  507. ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)]
  508. "--"
  509. ["Set for Today" org-mouse-timestamp-today]
  510. ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)]
  511. ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)]
  512. ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)]
  513. ["Set in a Month" (org-mouse-timestamp-today 1 'month)]
  514. "--"
  515. ["+ 1 Day" (org-timestamp-change 1 'day)]
  516. ["+ 1 Week" (org-timestamp-change 7 'day)]
  517. ["+ 1 Month" (org-timestamp-change 1 'month)]
  518. "--"
  519. ["- 1 Day" (org-timestamp-change -1 'day)]
  520. ["- 1 Week" (org-timestamp-change -7 'day)]
  521. ["- 1 Month" (org-timestamp-change -1 'month)])))
  522. ((and (assq :headline contextlist) (not (eolp)))
  523. (let ((priority (org-mouse-get-priority t)))
  524. (popup-menu
  525. `("Headline Menu"
  526. ("Tags and Priorities"
  527. ,@(org-mouse-keyword-menu
  528. (org-mouse-priority-list)
  529. '(lambda (keyword)
  530. (org-mouse-set-priority (string-to-char keyword)))
  531. priority "Priority %s")
  532. "--"
  533. ,@(org-mouse-tag-menu))
  534. ["Show Tags"
  535. (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags))
  536. :visible (not org-mouse-direct)]
  537. ["Show Priority"
  538. (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority))
  539. :visible (not org-mouse-direct)]
  540. ,@(if org-mouse-direct '("--") nil)
  541. ["New Heading" org-mouse-insert-heading :visible org-mouse-direct]
  542. ;; ["New Appointment" org-mouse-new-appointment :visible org-mouse-direct]
  543. ;; "--"
  544. ["Cycle TODO" org-todo]
  545. ["Set Deadline"
  546. (progn (org-mouse-end-headline) (insert " ") (org-deadline))
  547. :active (not (save-excursion
  548. (org-mouse-re-search-line org-deadline-regexp)))]
  549. ["Schedule Task"
  550. (progn (org-mouse-end-headline) (insert " ") (org-schedule))
  551. :active (not (save-excursion
  552. (org-mouse-re-search-line org-scheduled-regexp)))]
  553. ["Insert Timestamp"
  554. (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t]
  555. ; ["Timestamp (inactive)" org-time-stamp-inactive t]
  556. "--"
  557. ["Archive Subtree" org-archive-subtree]
  558. ["Cut Subtree" org-cut-special]
  559. ["Copy Subtree" org-copy-special]
  560. ["Paste Subtree" org-paste-special :visible org-mouse-direct]
  561. "--"
  562. ;; ["Promote Subtree" org-shiftmetaleft]
  563. ;; ["Demote Subtree" org-shiftmetaright]
  564. ;; ["Promote Heading" org-metaleft]
  565. ;; ["Demote Heading" org-metaright]
  566. ;; "--"
  567. ["Move Trees" org-mouse-move-tree :active nil]
  568. ))))
  569. (t
  570. (org-mouse-popup-global-menu))))))
  571. ;; (defun org-mouse-at-regexp (regexp)
  572. ;; (save-excursion
  573. ;; (let ((point (point))
  574. ;; (bol (progn (beginning-of-line) (point)))
  575. ;; (eol (progn (end-of-line) (point))))
  576. ;; (goto-char point)
  577. ;; (re-search-backward regexp bol 1)
  578. ;; (and (not (eolp))
  579. ;; (progn (forward-char)
  580. ;; (re-search-forward regexp eol t))
  581. ;; (<= (match-beginning 0) point)))))
  582. (defun org-mouse-in-region-p (pos)
  583. (and mark-active (>= pos (region-beginning)) (< pos (region-end))))
  584. (defun org-mouse-down-mouse (event)
  585. (interactive "e")
  586. (setq this-command last-command)
  587. (unless (and transient-mark-mode
  588. (= 1 (event-click-count event))
  589. (org-mouse-in-region-p (posn-point (event-start event))))
  590. (mouse-drag-region event)))
  591. (add-hook 'org-mode-hook
  592. '(lambda ()
  593. (setq org-mouse-context-menu-function 'org-mouse-context-menu)
  594. ; (define-key org-mouse-map [follow-link] 'mouse-face)
  595. (define-key org-mouse-map (if (featurep 'xemacs) [button3] [mouse-3]) nil)
  596. (define-key org-mode-map [mouse-3] 'org-mouse-show-context-menu)
  597. (define-key org-mode-map [down-mouse-1] 'org-mouse-down-mouse)
  598. (define-key org-mouse-map [C-drag-mouse-1] 'org-mouse-move-tree)
  599. (define-key org-mouse-map [C-down-mouse-1] 'org-mouse-move-tree-start)
  600. (define-key org-mouse-map [drag-mouse-3] 'org-mouse-move-tree)
  601. (define-key org-mouse-map [down-mouse-3] 'org-mouse-move-tree-start)
  602. (font-lock-add-keywords nil
  603. `((,outline-regexp
  604. 0 `(face org-link mouse-face highlight keymap ,org-mouse-map)
  605. 'prepend)
  606. ("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +"
  607. (1 `(face org-link keymap ,org-mouse-map mouse-face highlight) 'prepend))
  608. ("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  609. (2 `(face bold keymap ,org-mouse-map mouse-face highlight) t)))
  610. t)
  611. (defadvice org-open-at-point (around org-mouse-open-at-point activate)
  612. (let ((context (org-context)))
  613. (cond
  614. ((assq :headline-stars context) (org-cycle))
  615. ((assq :checkbox context) (org-toggle-checkbox))
  616. ((assq :item-bullet context)
  617. (let ((org-cycle-include-plain-lists t)) (org-cycle)))
  618. (t ad-do-it))))))
  619. (defun org-mouse-move-tree-start (event)
  620. (interactive "e")
  621. (message "Same line: promote/demote, (***):move before, (text): make a child"))
  622. (defun org-mouse-make-marker (position)
  623. (with-current-buffer (window-buffer (posn-window position))
  624. (copy-marker (posn-point position))))
  625. (defun org-mouse-move-tree (event)
  626. ;; todo: handle movements between different buffers
  627. (interactive "e")
  628. (save-excursion
  629. (let* ((start (org-mouse-make-marker (event-start event)))
  630. (end (org-mouse-make-marker (event-end event)))
  631. (sbuf (marker-buffer start))
  632. (ebuf (marker-buffer end)))
  633. (when (and sbuf ebuf)
  634. (set-buffer sbuf)
  635. (goto-char start)
  636. (org-back-to-heading)
  637. (if (and (eq sbuf ebuf)
  638. (equal
  639. (point)
  640. (save-excursion (goto-char end) (org-back-to-heading) (point))))
  641. ;; if the same line then promote/demote
  642. (if (>= end start) (org-demote-subtree) (org-promote-subtree))
  643. ;; if different lines then move
  644. (org-cut-subtree)
  645. (set-buffer ebuf)
  646. (goto-char end)
  647. (org-back-to-heading)
  648. (when (and (eq sbuf ebuf)
  649. (equal
  650. (point)
  651. (save-excursion (goto-char start)
  652. (org-back-to-heading) (point))))
  653. (outline-end-of-subtree)
  654. (end-of-line)
  655. (if (eobp) (newline) (forward-char)))
  656. (when (looking-at outline-regexp)
  657. (let ((level (- (match-end 0) (match-beginning 0))))
  658. (when (> end (match-end 0))
  659. (outline-end-of-subtree)
  660. (end-of-line)
  661. (if (eobp) (newline) (forward-char))
  662. (setq level (1+ level)))
  663. (org-paste-subtree level)
  664. (save-excursion
  665. (outline-end-of-subtree)
  666. (when (bolp) (delete-char -1))))))))))
  667. (defun org-mouse-transform-to-outline ()
  668. (interactive)
  669. (org-back-to-heading)
  670. (let ((minlevel 1000)
  671. (replace-text (concat (match-string 0) "* ")))
  672. (beginning-of-line 2)
  673. (save-excursion
  674. (while (not (or (eobp) (looking-at outline-regexp)))
  675. (when (looking-at org-mouse-plain-list-regexp)
  676. (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1)))))
  677. (forward-line)))
  678. (while (not (or (eobp) (looking-at outline-regexp)))
  679. (when (and (looking-at org-mouse-plain-list-regexp)
  680. (eq minlevel (- (match-end 1) (match-beginning 1))))
  681. (replace-match replace-text))
  682. (forward-line))))
  683. (defun org-mouse-do-remotely (command)
  684. ; (org-agenda-check-no-diary)
  685. (when (get-text-property (point) 'org-marker)
  686. (let* ((anticol (- (point-at-eol) (point)))
  687. (marker (get-text-property (point) 'org-marker))
  688. (buffer (marker-buffer marker))
  689. (pos (marker-position marker))
  690. (hdmarker (get-text-property (point) 'org-hd-marker))
  691. (buffer-read-only nil)
  692. (newhead "--- removed ---")
  693. (org-mouse-direct nil)
  694. (org-mouse-main-buffer (current-buffer)))
  695. (when (eq (with-current-buffer buffer major-mode) 'org-mode)
  696. (let ((endmarker (save-excursion
  697. (set-buffer buffer)
  698. (outline-end-of-subtree)
  699. (forward-char 1)
  700. (copy-marker (point)))))
  701. (with-current-buffer buffer
  702. (widen)
  703. (goto-char pos)
  704. (org-show-hidden-entry)
  705. (save-excursion
  706. (and (outline-next-heading)
  707. (org-flag-heading nil))) ; show the next heading
  708. (org-back-to-heading)
  709. (setq marker (copy-marker (point)))
  710. (goto-char (max (point-at-bol) (- (point-at-eol) anticol)))
  711. (funcall command)
  712. (unless (eq (marker-position marker) (marker-position endmarker))
  713. (setq newhead (org-get-heading))))
  714. (beginning-of-line 1)
  715. (save-excursion
  716. (org-agenda-change-all-lines newhead hdmarker 'fixface)))
  717. t))))
  718. (defun org-mouse-agenda-context-menu ()
  719. (or (org-mouse-do-remotely 'org-mouse-context-menu)
  720. (popup-menu
  721. '("Agenda"
  722. ("Agenda Files")
  723. "--"
  724. ["Rebuild Buffer" org-agenda-redo t]
  725. ["New Diary Entry"
  726. org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  727. "--"
  728. ["Goto Today" org-agenda-goto-today
  729. (org-agenda-check-type nil 'agenda 'timeline)]
  730. ["Display Calendar" org-agenda-goto-calendar
  731. (org-agenda-check-type nil 'agenda 'timeline)]
  732. ("Calendar Commands"
  733. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  734. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  735. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  736. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  737. "--"
  738. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  739. "--"
  740. ["Day View" org-agenda-day-view
  741. :active (org-agenda-check-type nil 'agenda)
  742. :style radio :selected (equal org-agenda-ndays 1)]
  743. ["Week View" org-agenda-week-view
  744. :active (org-agenda-check-type nil 'agenda)
  745. :style radio :selected (equal org-agenda-ndays 7)]
  746. "--"
  747. ["Show Logbook entries" org-agenda-log-mode
  748. :style toggle :selected org-agenda-show-log
  749. :active (org-agenda-check-type nil 'agenda 'timeline)]
  750. ["Include Diary" org-agenda-toggle-diary
  751. :style toggle :selected org-agenda-include-diary
  752. :active (org-agenda-check-type nil 'agenda)]
  753. ["Use Time Grid" org-agenda-toggle-time-grid
  754. :style toggle :selected org-agenda-use-time-grid
  755. :active (org-agenda-check-type nil 'agenda)]
  756. ["Follow Mode" org-agenda-follow-mode
  757. :style toggle :selected org-agenda-follow-mode]
  758. "--"
  759. ["Quit" org-agenda-quit t]
  760. ["Exit and Release Buffers" org-agenda-exit t]
  761. ))))
  762. ; (setq org-agenda-mode-hook nil)
  763. (add-hook 'org-agenda-mode-hook
  764. '(lambda ()
  765. ; (define-key org-agenda-keymap [follow-link] 'mouse-face)
  766. (setq org-mouse-context-menu-function 'org-mouse-agenda-context-menu)
  767. (define-key org-agenda-keymap
  768. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-mouse-show-context-menu)))
  769. (provide 'org-mouse)