org-colview.el 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. ;;; org-colview.el --- Column View in Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 7.5
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file contains the column view for Org.
  25. ;;; Code:
  26. (eval-when-compile (require 'cl))
  27. (require 'org)
  28. (declare-function org-agenda-redo "org-agenda" ())
  29. (declare-function org-agenda-do-context-action "org-agenda" ())
  30. (when (featurep 'xemacs)
  31. (error "Do not load this file into XEmacs, use 'org-colview-xemacs.el'."))
  32. ;;; Column View
  33. (defvar org-columns-overlays nil
  34. "Holds the list of current column overlays.")
  35. (defvar org-columns-current-fmt nil
  36. "Local variable, holds the currently active column format.")
  37. (make-variable-buffer-local 'org-columns-current-fmt)
  38. (defvar org-columns-current-fmt-compiled nil
  39. "Local variable, holds the currently active column format.
  40. This is the compiled version of the format.")
  41. (make-variable-buffer-local 'org-columns-current-fmt-compiled)
  42. (defvar org-columns-current-widths nil
  43. "Loval variable, holds the currently widths of fields.")
  44. (make-variable-buffer-local 'org-columns-current-widths)
  45. (defvar org-columns-current-maxwidths nil
  46. "Loval variable, holds the currently active maximum column widths.")
  47. (make-variable-buffer-local 'org-columns-current-maxwidths)
  48. (defvar org-columns-begin-marker (make-marker)
  49. "Points to the position where last a column creation command was called.")
  50. (defvar org-columns-top-level-marker (make-marker)
  51. "Points to the position where current columns region starts.")
  52. (defvar org-columns-map (make-sparse-keymap)
  53. "The keymap valid in column display.")
  54. (defun org-columns-content ()
  55. "Switch to contents view while in columns view."
  56. (interactive)
  57. (org-overview)
  58. (org-content))
  59. (org-defkey org-columns-map "c" 'org-columns-content)
  60. (org-defkey org-columns-map "o" 'org-overview)
  61. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  62. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  63. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  64. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  65. (org-defkey org-columns-map "v" 'org-columns-show-value)
  66. (org-defkey org-columns-map "q" 'org-columns-quit)
  67. (org-defkey org-columns-map "r" 'org-columns-redo)
  68. (org-defkey org-columns-map "g" 'org-columns-redo)
  69. (org-defkey org-columns-map [left] 'backward-char)
  70. (org-defkey org-columns-map "\M-b" 'backward-char)
  71. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  72. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  73. (org-defkey org-columns-map "\M-f"
  74. (lambda () (interactive) (goto-char (1+ (point)))))
  75. (org-defkey org-columns-map [right]
  76. (lambda () (interactive) (goto-char (1+ (point)))))
  77. (org-defkey org-columns-map [down]
  78. (lambda () (interactive)
  79. (let ((col (current-column)))
  80. (beginning-of-line 2)
  81. (while (and (org-invisible-p2) (not (eobp)))
  82. (beginning-of-line 2))
  83. (move-to-column col)
  84. (if (eq major-mode 'org-agenda-mode)
  85. (org-agenda-do-context-action)))))
  86. (org-defkey org-columns-map [up]
  87. (lambda () (interactive)
  88. (let ((col (current-column)))
  89. (beginning-of-line 0)
  90. (while (and (org-invisible-p2) (not (bobp)))
  91. (beginning-of-line 0))
  92. (move-to-column col)
  93. (if (eq major-mode 'org-agenda-mode)
  94. (org-agenda-do-context-action)))))
  95. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  96. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  97. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  98. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  99. (org-defkey org-columns-map "<" 'org-columns-narrow)
  100. (org-defkey org-columns-map ">" 'org-columns-widen)
  101. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  102. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  103. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  104. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  105. (dotimes (i 10)
  106. (org-defkey org-columns-map (number-to-string i)
  107. `(lambda () (interactive)
  108. (org-columns-next-allowed-value nil ,i))))
  109. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  110. '("Column"
  111. ["Edit property" org-columns-edit-value t]
  112. ["Next allowed value" org-columns-next-allowed-value t]
  113. ["Previous allowed value" org-columns-previous-allowed-value t]
  114. ["Show full value" org-columns-show-value t]
  115. ["Edit allowed values" org-columns-edit-allowed t]
  116. "--"
  117. ["Edit column attributes" org-columns-edit-attributes t]
  118. ["Increase column width" org-columns-widen t]
  119. ["Decrease column width" org-columns-narrow t]
  120. "--"
  121. ["Move column right" org-columns-move-right t]
  122. ["Move column left" org-columns-move-left t]
  123. ["Add column" org-columns-new t]
  124. ["Delete column" org-columns-delete t]
  125. "--"
  126. ["CONTENTS" org-columns-content t]
  127. ["OVERVIEW" org-overview t]
  128. ["Refresh columns display" org-columns-redo t]
  129. "--"
  130. ["Open link" org-columns-open-link t]
  131. "--"
  132. ["Quit" org-columns-quit t]))
  133. (defun org-columns-new-overlay (beg end &optional string face)
  134. "Create a new column overlay and add it to the list."
  135. (let ((ov (make-overlay beg end)))
  136. (overlay-put ov 'face (or face 'secondary-selection))
  137. (org-overlay-display ov string face)
  138. (push ov org-columns-overlays)
  139. ov))
  140. (defun org-columns-display-here (&optional props dateline)
  141. "Overlay the current line with column display."
  142. (interactive)
  143. (let* ((fmt org-columns-current-fmt-compiled)
  144. (beg (point-at-bol))
  145. (level-face (save-excursion
  146. (beginning-of-line 1)
  147. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  148. (org-get-level-face 2))))
  149. (ref-face (or level-face
  150. (and (eq major-mode 'org-agenda-mode)
  151. (get-text-property (point-at-bol) 'face))
  152. 'default))
  153. (color (list :foreground (face-attribute ref-face :foreground)))
  154. (face (list color 'org-column ref-face))
  155. (face1 (list color 'org-agenda-column-dateline ref-face))
  156. (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
  157. pom property ass width f string ov column val modval s2 title calc)
  158. ;; Check if the entry is in another buffer.
  159. (unless props
  160. (if (eq major-mode 'org-agenda-mode)
  161. (setq pom (or (org-get-at-bol 'org-hd-marker)
  162. (org-get-at-bol 'org-marker))
  163. props (if pom (org-entry-properties pom) nil))
  164. (setq props (org-entry-properties nil))))
  165. ;; Walk the format
  166. (while (setq column (pop fmt))
  167. (setq property (car column)
  168. title (nth 1 column)
  169. ass (if (equal property "ITEM")
  170. (cons "ITEM"
  171. ;; When in a buffer, get the whole line,
  172. ;; we'll clean it later…
  173. (if (org-mode-p)
  174. (save-match-data
  175. (org-no-properties
  176. (org-remove-tabs
  177. (buffer-substring-no-properties
  178. (point-at-bol) (point-at-eol)))))
  179. ;; In agenda, just get the `txt' property
  180. (org-no-properties
  181. (org-get-at-bol 'txt))))
  182. (assoc property props))
  183. width (or (cdr (assoc property org-columns-current-maxwidths))
  184. (nth 2 column)
  185. (length property))
  186. f (format "%%-%d.%ds | " width width)
  187. calc (nth 7 column)
  188. val (or (cdr ass) "")
  189. modval (cond ((and org-columns-modify-value-for-display-function
  190. (functionp
  191. org-columns-modify-value-for-display-function))
  192. (funcall org-columns-modify-value-for-display-function
  193. title val))
  194. ((equal property "ITEM")
  195. (if (org-mode-p)
  196. (org-columns-cleanup-item
  197. val org-columns-current-fmt-compiled)))
  198. ((and calc (functionp calc)
  199. (not (string= val ""))
  200. (not (get-text-property 0 'org-computed val)))
  201. (org-columns-number-to-string
  202. (funcall calc (org-columns-string-to-number
  203. val (nth 4 column)))
  204. (nth 4 column)))))
  205. (setq s2 (org-columns-add-ellipses (or modval val) width))
  206. (setq string (format f s2))
  207. ;; Create the overlay
  208. (org-unmodified
  209. (setq ov (org-columns-new-overlay
  210. beg (setq beg (1+ beg)) string (if dateline face1 face)))
  211. (overlay-put ov 'keymap org-columns-map)
  212. (overlay-put ov 'org-columns-key property)
  213. (overlay-put ov 'org-columns-value (cdr ass))
  214. (overlay-put ov 'org-columns-value-modified modval)
  215. (overlay-put ov 'org-columns-pom pom)
  216. (overlay-put ov 'org-columns-format f)
  217. (overlay-put ov 'line-prefix "")
  218. (overlay-put ov 'wrap-prefix ""))
  219. (if (or (not (char-after beg))
  220. (equal (char-after beg) ?\n))
  221. (let ((inhibit-read-only t))
  222. (save-excursion
  223. (goto-char beg)
  224. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  225. ;; Make the rest of the line disappear.
  226. (org-unmodified
  227. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  228. (overlay-put ov 'invisible t)
  229. (overlay-put ov 'keymap org-columns-map)
  230. (overlay-put ov 'intangible t)
  231. (overlay-put ov 'line-prefix "")
  232. (overlay-put ov 'wrap-prefix "")
  233. (push ov org-columns-overlays)
  234. (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  235. (overlay-put ov 'keymap org-columns-map)
  236. (push ov org-columns-overlays)
  237. (let ((inhibit-read-only t))
  238. (put-text-property (max (point-min) (1- (point-at-bol)))
  239. (min (point-max) (1+ (point-at-eol)))
  240. 'read-only "Type `e' to edit property")))))
  241. (defun org-columns-add-ellipses (string width)
  242. "Truncate STRING with WIDTH characters, with ellipses."
  243. (cond
  244. ((<= (length string) width) string)
  245. ((<= width (length org-columns-ellipses))
  246. (substring org-columns-ellipses 0 width))
  247. (t (concat (substring string 0 (- width (length org-columns-ellipses)))
  248. org-columns-ellipses))))
  249. (defvar org-columns-full-header-line-format nil
  250. "The full header line format, will be shifted by horizontal scrolling." )
  251. (defvar org-previous-header-line-format nil
  252. "The header line format before column view was turned on.")
  253. (defvar org-columns-inhibit-recalculation nil
  254. "Inhibit recomputing of columns on column view startup.")
  255. (defvar org-columns-flyspell-was-active nil
  256. "Remember the state of `flyspell-mode' before column view.
  257. Flyspell-mode can cause problems in columns view, so it is turned off
  258. for the duration of the command.")
  259. (defvar header-line-format)
  260. (defvar org-columns-previous-hscroll 0)
  261. (defun org-columns-display-here-title ()
  262. "Overlay the newline before the current line with the table title."
  263. (interactive)
  264. (let ((fmt org-columns-current-fmt-compiled)
  265. string (title "")
  266. property width f column str widths)
  267. (while (setq column (pop fmt))
  268. (setq property (car column)
  269. str (or (nth 1 column) property)
  270. width (or (cdr (assoc property org-columns-current-maxwidths))
  271. (nth 2 column)
  272. (length str))
  273. widths (push width widths)
  274. f (format "%%-%d.%ds | " width width)
  275. string (format f str)
  276. title (concat title string)))
  277. (setq title (concat
  278. (org-add-props " " nil 'display '(space :align-to 0))
  279. ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  280. (org-add-props title nil 'face 'org-column-title)))
  281. (org-set-local 'org-previous-header-line-format header-line-format)
  282. (org-set-local 'org-columns-current-widths (nreverse widths))
  283. (setq org-columns-full-header-line-format title)
  284. (setq org-columns-previous-hscroll -1)
  285. ; (org-columns-hscoll-title)
  286. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  287. (defun org-columns-hscoll-title ()
  288. "Set the `header-line-format' so that it scrolls along with the table."
  289. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  290. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  291. (setq header-line-format
  292. (concat (substring org-columns-full-header-line-format 0 1)
  293. (substring org-columns-full-header-line-format
  294. (1+ (window-hscroll))))
  295. org-columns-previous-hscroll (window-hscroll))
  296. (force-mode-line-update)))
  297. (defvar org-colview-initial-truncate-line-value nil
  298. "Remember the value of `truncate-lines' across colview.")
  299. (defun org-columns-remove-overlays ()
  300. "Remove all currently active column overlays."
  301. (interactive)
  302. (when (marker-buffer org-columns-begin-marker)
  303. (with-current-buffer (marker-buffer org-columns-begin-marker)
  304. (when (local-variable-p 'org-previous-header-line-format)
  305. (setq header-line-format org-previous-header-line-format)
  306. (kill-local-variable 'org-previous-header-line-format)
  307. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  308. (move-marker org-columns-begin-marker nil)
  309. (move-marker org-columns-top-level-marker nil)
  310. (org-unmodified
  311. (mapc 'delete-overlay org-columns-overlays)
  312. (setq org-columns-overlays nil)
  313. (let ((inhibit-read-only t))
  314. (remove-text-properties (point-min) (point-max) '(read-only t))))
  315. (when org-columns-flyspell-was-active
  316. (flyspell-mode 1))
  317. (when (local-variable-p 'org-colview-initial-truncate-line-value)
  318. (setq truncate-lines org-colview-initial-truncate-line-value)))))
  319. (defun org-columns-cleanup-item (item fmt)
  320. "Remove from ITEM what is a column in the format FMT."
  321. (if (not org-complex-heading-regexp)
  322. item
  323. (when (string-match org-complex-heading-regexp item)
  324. (setq item
  325. (concat
  326. (org-add-props (match-string 1 item) nil
  327. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  328. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  329. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  330. " " (save-match-data (org-columns-compact-links (match-string 4 item)))
  331. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
  332. (add-text-properties
  333. 0 (1+ (match-end 1))
  334. (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  335. item)
  336. item)))
  337. (defun org-columns-compact-links (s)
  338. "Replace [[link][desc]] with [desc] or [link]."
  339. (while (string-match org-bracket-link-regexp s)
  340. (setq s (replace-match
  341. (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
  342. t t s)))
  343. s)
  344. (defun org-columns-show-value ()
  345. "Show the full value of the property."
  346. (interactive)
  347. (let ((value (get-char-property (point) 'org-columns-value)))
  348. (message "Value is: %s" (or value ""))))
  349. (defvar org-agenda-columns-active) ;; defined in org-agenda.el
  350. (defun org-columns-quit ()
  351. "Remove the column overlays and in this way exit column editing."
  352. (interactive)
  353. (org-unmodified
  354. (org-columns-remove-overlays)
  355. (let ((inhibit-read-only t))
  356. (remove-text-properties (point-min) (point-max) '(read-only t))))
  357. (when (eq major-mode 'org-agenda-mode)
  358. (setq org-agenda-columns-active nil)
  359. (message
  360. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  361. (defun org-columns-check-computed ()
  362. "Check if this column value is computed.
  363. If yes, throw an error indicating that changing it does not make sense."
  364. (let ((val (get-char-property (point) 'org-columns-value)))
  365. (when (and (stringp val)
  366. (get-char-property 0 'org-computed val))
  367. (error "This value is computed from the entry's children"))))
  368. (defun org-columns-todo (&optional arg)
  369. "Change the TODO state during column view."
  370. (interactive "P")
  371. (org-columns-edit-value "TODO"))
  372. (defun org-columns-set-tags-or-toggle (&optional arg)
  373. "Toggle checkbox at point, or set tags for current headline."
  374. (interactive "P")
  375. (if (string-match "\\`\\[[ xX-]\\]\\'"
  376. (get-char-property (point) 'org-columns-value))
  377. (org-columns-next-allowed-value)
  378. (org-columns-edit-value "TAGS")))
  379. (defun org-columns-edit-value (&optional key)
  380. "Edit the value of the property at point in column view.
  381. Where possible, use the standard interface for changing this line."
  382. (interactive)
  383. (org-columns-check-computed)
  384. (let* ((col (current-column))
  385. (key (or key (get-char-property (point) 'org-columns-key)))
  386. (value (get-char-property (point) 'org-columns-value))
  387. (bol (point-at-bol)) (eol (point-at-eol))
  388. (pom (or (get-text-property bol 'org-hd-marker)
  389. (point))) ; keep despite of compiler waring
  390. (line-overlays
  391. (delq nil (mapcar (lambda (x)
  392. (and (eq (overlay-buffer x) (current-buffer))
  393. (>= (overlay-start x) bol)
  394. (<= (overlay-start x) eol)
  395. x))
  396. org-columns-overlays)))
  397. (org-columns-time (time-to-number-of-days (current-time)))
  398. nval eval allowed)
  399. (cond
  400. ((equal key "CLOCKSUM")
  401. (error "This special column cannot be edited"))
  402. ((equal key "ITEM")
  403. (setq eval '(org-with-point-at pom
  404. (org-edit-headline))))
  405. ((equal key "TODO")
  406. (setq eval '(org-with-point-at
  407. pom
  408. (call-interactively 'org-todo))))
  409. ((equal key "PRIORITY")
  410. (setq eval '(org-with-point-at pom
  411. (call-interactively 'org-priority))))
  412. ((equal key "TAGS")
  413. (setq eval '(org-with-point-at pom
  414. (let ((org-fast-tag-selection-single-key
  415. (if (eq org-fast-tag-selection-single-key 'expert)
  416. t org-fast-tag-selection-single-key)))
  417. (call-interactively 'org-set-tags)))))
  418. ((equal key "DEADLINE")
  419. (setq eval '(org-with-point-at pom
  420. (call-interactively 'org-deadline))))
  421. ((equal key "SCHEDULED")
  422. (setq eval '(org-with-point-at pom
  423. (call-interactively 'org-schedule))))
  424. ((equal key "BEAMER_env")
  425. (setq eval '(org-with-point-at pom
  426. (call-interactively 'org-beamer-select-environment))))
  427. (t
  428. (setq allowed (org-property-get-allowed-values pom key 'table))
  429. (if allowed
  430. (setq nval (org-icompleting-read
  431. "Value: " allowed nil
  432. (not (get-text-property 0 'org-unrestricted
  433. (caar allowed)))))
  434. (setq nval (read-string "Edit: " value)))
  435. (setq nval (org-trim nval))
  436. (when (not (equal nval value))
  437. (setq eval '(org-entry-put pom key nval)))))
  438. (when eval
  439. (cond
  440. ((equal major-mode 'org-agenda-mode)
  441. (org-columns-eval eval)
  442. ;; The following let preserves the current format, and makes sure
  443. ;; that in only a single file things need to be upated.
  444. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  445. (buffer (marker-buffer pom))
  446. (org-agenda-contributing-files
  447. (list (with-current-buffer buffer
  448. (buffer-file-name (buffer-base-buffer))))))
  449. (org-agenda-columns)))
  450. (t
  451. (let ((inhibit-read-only t))
  452. (org-unmodified
  453. (remove-text-properties
  454. (max (point-min) (1- bol)) eol '(read-only t)))
  455. (unwind-protect
  456. (progn
  457. (setq org-columns-overlays
  458. (org-delete-all line-overlays org-columns-overlays))
  459. (mapc 'delete-overlay line-overlays)
  460. (org-columns-eval eval))
  461. (org-columns-display-here)))
  462. (org-move-to-column col)
  463. (if (and (org-mode-p)
  464. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  465. (org-columns-update key)))))))
  466. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  467. "Edit the current headline, the part without TODO keyword, TAGS."
  468. (org-back-to-heading)
  469. (when (looking-at org-todo-line-regexp)
  470. (let ((pos (point))
  471. (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  472. (txt (match-string 3))
  473. (post "")
  474. txt2)
  475. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
  476. (setq post (match-string 0 txt)
  477. txt (substring txt 0 (match-beginning 0))))
  478. (setq txt2 (read-string "Edit: " txt))
  479. (when (not (equal txt txt2))
  480. (goto-char pos)
  481. (insert pre txt2 post)
  482. (delete-region (point) (point-at-eol))
  483. (org-set-tags nil t)))))
  484. (defun org-columns-edit-allowed ()
  485. "Edit the list of allowed values for the current property."
  486. (interactive)
  487. (let* ((pom (or (org-get-at-bol 'org-marker)
  488. (org-get-at-bol 'org-hd-marker)
  489. (point)))
  490. (key (get-char-property (point) 'org-columns-key))
  491. (key1 (concat key "_ALL"))
  492. (allowed (org-entry-get pom key1 t))
  493. nval)
  494. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  495. ;; FIXME: Write back to #+PROPERTY setting if that is needed.
  496. (setq nval (read-string "Allowed: " allowed))
  497. (org-entry-put
  498. (cond ((marker-position org-entry-property-inherited-from)
  499. org-entry-property-inherited-from)
  500. ((marker-position org-columns-top-level-marker)
  501. org-columns-top-level-marker)
  502. (t pom))
  503. key1 nval)))
  504. (defun org-columns-eval (form)
  505. (let (hidep)
  506. (save-excursion
  507. (beginning-of-line 1)
  508. ;; `next-line' is needed here, because it skips invisible line.
  509. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  510. (setq hidep (org-on-heading-p 1)))
  511. (eval form)
  512. (and hidep (hide-entry))))
  513. (defun org-columns-previous-allowed-value ()
  514. "Switch to the previous allowed value for this column."
  515. (interactive)
  516. (org-columns-next-allowed-value t))
  517. (defun org-columns-next-allowed-value (&optional previous nth)
  518. "Switch to the next allowed value for this column.
  519. When PREVIOUS is set, go to the previous value. When NTH is
  520. an integer, select that value."
  521. (interactive)
  522. (org-columns-check-computed)
  523. (let* ((col (current-column))
  524. (key (get-char-property (point) 'org-columns-key))
  525. (value (get-char-property (point) 'org-columns-value))
  526. (bol (point-at-bol)) (eol (point-at-eol))
  527. (pom (or (get-text-property bol 'org-hd-marker)
  528. (point))) ; keep despite of compiler waring
  529. (line-overlays
  530. (delq nil (mapcar (lambda (x)
  531. (and (eq (overlay-buffer x) (current-buffer))
  532. (>= (overlay-start x) bol)
  533. (<= (overlay-start x) eol)
  534. x))
  535. org-columns-overlays)))
  536. (allowed (or (org-property-get-allowed-values pom key)
  537. (and (memq
  538. (nth 4 (assoc key org-columns-current-fmt-compiled))
  539. '(checkbox checkbox-n-of-m checkbox-percent))
  540. '("[ ]" "[X]"))
  541. (org-colview-construct-allowed-dates value)))
  542. nval)
  543. (when (integerp nth)
  544. (setq nth (1- nth))
  545. (if (= nth -1) (setq nth 9)))
  546. (when (equal key "ITEM")
  547. (error "Cannot edit item headline from here"))
  548. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  549. (error "Allowed values for this property have not been defined"))
  550. (if (member key '("SCHEDULED" "DEADLINE"))
  551. (setq nval (if previous 'earlier 'later))
  552. (if previous (setq allowed (reverse allowed)))
  553. (cond
  554. (nth
  555. (setq nval (nth nth allowed))
  556. (if (not nval)
  557. (error "There are only %d allowed values for property `%s'"
  558. (length allowed) key)))
  559. ((member value allowed)
  560. (setq nval (or (car (cdr (member value allowed)))
  561. (car allowed)))
  562. (if (equal nval value)
  563. (error "Only one allowed value for this property")))
  564. (t (setq nval (car allowed)))))
  565. (cond
  566. ((equal major-mode 'org-agenda-mode)
  567. (org-columns-eval '(org-entry-put pom key nval))
  568. ;; The following let preserves the current format, and makes sure
  569. ;; that in only a single file things need to be upated.
  570. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  571. (buffer (marker-buffer pom))
  572. (org-agenda-contributing-files
  573. (list (with-current-buffer buffer
  574. (buffer-file-name (buffer-base-buffer))))))
  575. (org-agenda-columns)))
  576. (t
  577. (let ((inhibit-read-only t))
  578. (remove-text-properties (1- bol) eol '(read-only t))
  579. (unwind-protect
  580. (progn
  581. (setq org-columns-overlays
  582. (org-delete-all line-overlays org-columns-overlays))
  583. (mapc 'delete-overlay line-overlays)
  584. (org-columns-eval '(org-entry-put pom key nval)))
  585. (org-columns-display-here)))
  586. (org-move-to-column col)
  587. (and (nth 3 (assoc key org-columns-current-fmt-compiled))
  588. (org-columns-update key))))))
  589. (defun org-colview-construct-allowed-dates (s)
  590. "Construct a list of three dates around the date in S.
  591. This respects the format of the time stamp in S, active or non-active,
  592. and also including time or not. S must be just a time stamp, no text
  593. around it."
  594. (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
  595. (let* ((time (org-parse-time-string s 'nodefaults))
  596. (active (equal (string-to-char s) ?<))
  597. (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
  598. time-before time-after)
  599. (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  600. (setf (car time) (or (car time) 0))
  601. (setf (nth 1 time) (or (nth 1 time) 0))
  602. (setf (nth 2 time) (or (nth 2 time) 0))
  603. (setq time-before (copy-sequence time))
  604. (setq time-after (copy-sequence time))
  605. (setf (nth 3 time-before) (1- (nth 3 time)))
  606. (setf (nth 3 time-after) (1+ (nth 3 time)))
  607. (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
  608. (list time-before time time-after)))))
  609. (defun org-verify-version (task)
  610. (cond
  611. ((eq task 'columns)
  612. (if (or (featurep 'xemacs)
  613. (< emacs-major-version 22))
  614. (error "Emacs 22 is required for the columns feature")))))
  615. (defun org-columns-open-link (&optional arg)
  616. (interactive "P")
  617. (let ((value (get-char-property (point) 'org-columns-value)))
  618. (org-open-link-from-string value arg)))
  619. (defun org-columns-get-format-and-top-level ()
  620. (let (fmt)
  621. (when (condition-case nil (org-back-to-heading) (error nil))
  622. (setq fmt (org-entry-get nil "COLUMNS" t)))
  623. (setq fmt (or fmt org-columns-default-format))
  624. (org-set-local 'org-columns-current-fmt fmt)
  625. (org-columns-compile-format fmt)
  626. (if (marker-position org-entry-property-inherited-from)
  627. (move-marker org-columns-top-level-marker
  628. org-entry-property-inherited-from)
  629. (move-marker org-columns-top-level-marker (point)))
  630. fmt))
  631. (defun org-columns ()
  632. "Turn on column view on an org-mode file."
  633. (interactive)
  634. (org-verify-version 'columns)
  635. (org-columns-remove-overlays)
  636. (move-marker org-columns-begin-marker (point))
  637. (let ((org-columns-time (time-to-number-of-days (current-time)))
  638. beg end fmt cache maxwidths)
  639. (setq fmt (org-columns-get-format-and-top-level))
  640. (save-excursion
  641. (goto-char org-columns-top-level-marker)
  642. (setq beg (point))
  643. (unless org-columns-inhibit-recalculation
  644. (org-columns-compute-all))
  645. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  646. (point-max)))
  647. ;; Get and cache the properties
  648. (goto-char beg)
  649. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  650. (save-excursion
  651. (save-restriction
  652. (narrow-to-region beg end)
  653. (org-clock-sum))))
  654. (while (re-search-forward (concat "^" outline-regexp) end t)
  655. (if (and org-columns-skip-archived-trees
  656. (looking-at (concat ".*:" org-archive-tag ":")))
  657. (org-end-of-subtree t)
  658. (push (cons (org-current-line) (org-entry-properties)) cache)))
  659. (when cache
  660. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  661. (org-set-local 'org-columns-current-maxwidths maxwidths)
  662. (org-columns-display-here-title)
  663. (when (org-set-local 'org-columns-flyspell-was-active
  664. (org-bound-and-true-p flyspell-mode))
  665. (flyspell-mode 0))
  666. (unless (local-variable-p 'org-colview-initial-truncate-line-value)
  667. (org-set-local 'org-colview-initial-truncate-line-value
  668. truncate-lines))
  669. (setq truncate-lines t)
  670. (mapc (lambda (x)
  671. (org-goto-line (car x))
  672. (org-columns-display-here (cdr x)))
  673. cache)))))
  674. (eval-when-compile (defvar org-columns-time))
  675. (defvar org-columns-compile-map
  676. '(("none" none +)
  677. (":" add_times +)
  678. ("+" add_numbers +)
  679. ("$" currency +)
  680. ("X" checkbox +)
  681. ("X/" checkbox-n-of-m +)
  682. ("X%" checkbox-percent +)
  683. ("max" max_numbers max)
  684. ("min" min_numbers min)
  685. ("mean" mean_numbers
  686. (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
  687. (":max" max_times max)
  688. (":min" min_times min)
  689. (":mean" mean_times
  690. (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
  691. ("@min" min_age min (lambda (x) (- org-columns-time x)))
  692. ("@max" max_age max (lambda (x) (- org-columns-time x)))
  693. ("@mean" mean_age
  694. (lambda (&rest x) (/ (apply '+ x) (float (length x))))
  695. (lambda (x) (- org-columns-time x)))
  696. ("est+" estimate org-estimate-combine))
  697. "Operator <-> format,function,calc map.
  698. Used to compile/uncompile columns format and completing read in
  699. interactive function `org-columns-new'.
  700. operator string used in #+COLUMNS definition describing the
  701. summary type
  702. format symbol describing summary type selected interactively in
  703. `org-columns-new' and internally in
  704. `org-columns-number-to-string' and
  705. `org-columns-string-to-number'
  706. function called with a list of values as argument to calculate
  707. the summary value
  708. calc function called on every element before summarizing. This is
  709. optional and should only be specified if needed")
  710. (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
  711. "Insert a new column, to the left of the current column."
  712. (interactive)
  713. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  714. cell)
  715. (setq prop (org-icompleting-read
  716. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  717. nil nil prop))
  718. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  719. (setq width (read-string "Column width: " (if width (number-to-string width))))
  720. (if (string-match "\\S-" width)
  721. (setq width (string-to-number width))
  722. (setq width nil))
  723. (setq fmt (org-icompleting-read
  724. "Summary [none]: "
  725. (mapcar (lambda (x) (list (symbol-name (cadr x))))
  726. org-columns-compile-map)
  727. nil t))
  728. (setq fmt (intern fmt)
  729. fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
  730. (if (eq fmt 'none) (setq fmt nil))
  731. (if editp
  732. (progn
  733. (setcar editp prop)
  734. (setcdr editp (list title width nil fmt nil fun)))
  735. (setq cell (nthcdr (1- (current-column))
  736. org-columns-current-fmt-compiled))
  737. (setcdr cell (cons (list prop title width nil fmt nil
  738. (car fun) (cadr fun))
  739. (cdr cell))))
  740. (org-columns-store-format)
  741. (org-columns-redo)))
  742. (defun org-columns-delete ()
  743. "Delete the column at point from columns view."
  744. (interactive)
  745. (let* ((n (current-column))
  746. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  747. (when (y-or-n-p
  748. (format "Are you sure you want to remove column \"%s\"? " title))
  749. (setq org-columns-current-fmt-compiled
  750. (delq (nth n org-columns-current-fmt-compiled)
  751. org-columns-current-fmt-compiled))
  752. (org-columns-store-format)
  753. (org-columns-redo)
  754. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  755. (backward-char 1)))))
  756. (defun org-columns-edit-attributes ()
  757. "Edit the attributes of the current column."
  758. (interactive)
  759. (let* ((n (current-column))
  760. (info (nth n org-columns-current-fmt-compiled)))
  761. (apply 'org-columns-new info)))
  762. (defun org-columns-widen (arg)
  763. "Make the column wider by ARG characters."
  764. (interactive "p")
  765. (let* ((n (current-column))
  766. (entry (nth n org-columns-current-fmt-compiled))
  767. (width (or (nth 2 entry)
  768. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  769. (setq width (max 1 (+ width arg)))
  770. (setcar (nthcdr 2 entry) width)
  771. (org-columns-store-format)
  772. (org-columns-redo)))
  773. (defun org-columns-narrow (arg)
  774. "Make the column narrower by ARG characters."
  775. (interactive "p")
  776. (org-columns-widen (- arg)))
  777. (defun org-columns-move-right ()
  778. "Swap this column with the one to the right."
  779. (interactive)
  780. (let* ((n (current-column))
  781. (cell (nthcdr n org-columns-current-fmt-compiled))
  782. e)
  783. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  784. (error "Cannot shift this column further to the right"))
  785. (setq e (car cell))
  786. (setcar cell (car (cdr cell)))
  787. (setcdr cell (cons e (cdr (cdr cell))))
  788. (org-columns-store-format)
  789. (org-columns-redo)
  790. (forward-char 1)))
  791. (defun org-columns-move-left ()
  792. "Swap this column with the one to the left."
  793. (interactive)
  794. (let* ((n (current-column)))
  795. (when (= n 0)
  796. (error "Cannot shift this column further to the left"))
  797. (backward-char 1)
  798. (org-columns-move-right)
  799. (backward-char 1)))
  800. (defun org-columns-store-format ()
  801. "Store the text version of the current columns format in appropriate place.
  802. This is either in the COLUMNS property of the node starting the current column
  803. display, or in the #+COLUMNS line of the current buffer."
  804. (let (fmt (cnt 0))
  805. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  806. (org-set-local 'org-columns-current-fmt fmt)
  807. (if (marker-position org-columns-top-level-marker)
  808. (save-excursion
  809. (goto-char org-columns-top-level-marker)
  810. (if (and (org-at-heading-p)
  811. (org-entry-get nil "COLUMNS"))
  812. (org-entry-put nil "COLUMNS" fmt)
  813. (goto-char (point-min))
  814. ;; Overwrite all #+COLUMNS lines....
  815. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  816. (setq cnt (1+ cnt))
  817. (replace-match (concat "#+COLUMNS: " fmt) t t))
  818. (unless (> cnt 0)
  819. (goto-char (point-min))
  820. (or (org-on-heading-p t) (outline-next-heading))
  821. (let ((inhibit-read-only t))
  822. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  823. (org-set-local 'org-columns-default-format fmt))))))
  824. (defvar org-agenda-overriding-columns-format nil
  825. "When set, overrides any other format definition for the agenda.
  826. Don't set this, this is meant for dynamic scoping.")
  827. (defun org-columns-get-autowidth-alist (s cache)
  828. "Derive the maximum column widths from the format and the cache."
  829. (let ((start 0) rtn)
  830. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  831. (push (cons (match-string 1 s) 1) rtn)
  832. (setq start (match-end 0)))
  833. (mapc (lambda (x)
  834. (setcdr x (apply 'max
  835. (mapcar
  836. (lambda (y)
  837. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  838. cache))))
  839. rtn)
  840. rtn))
  841. (defun org-columns-compute-all ()
  842. "Compute all columns that have operators defined."
  843. (org-unmodified
  844. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  845. (let ((columns org-columns-current-fmt-compiled)
  846. (org-columns-time (time-to-number-of-days (current-time)))
  847. col)
  848. (while (setq col (pop columns))
  849. (when (nth 3 col)
  850. (save-excursion
  851. (org-columns-compute (car col)))))))
  852. (defun org-columns-update (property)
  853. "Recompute PROPERTY, and update the columns display for it."
  854. (org-columns-compute property)
  855. (let (fmt val pos)
  856. (save-excursion
  857. (mapc (lambda (ov)
  858. (when (equal (overlay-get ov 'org-columns-key) property)
  859. (setq pos (overlay-start ov))
  860. (goto-char pos)
  861. (when (setq val (cdr (assoc property
  862. (get-text-property
  863. (point-at-bol) 'org-summaries))))
  864. (setq fmt (overlay-get ov 'org-columns-format))
  865. (overlay-put ov 'org-columns-value val)
  866. (overlay-put ov 'display (format fmt val)))))
  867. org-columns-overlays))))
  868. (defun org-columns-compute (property)
  869. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  870. (interactive)
  871. (let* ((re (concat "^" outline-regexp))
  872. (lmax 30) ; Does anyone use deeper levels???
  873. (lvals (make-vector lmax nil))
  874. (lflag (make-vector lmax nil))
  875. (level 0)
  876. (ass (assoc property org-columns-current-fmt-compiled))
  877. (format (nth 4 ass))
  878. (printf (nth 5 ass))
  879. (fun (nth 6 ass))
  880. (calc (or (nth 7 ass) 'identity))
  881. (beg org-columns-top-level-marker)
  882. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  883. (save-excursion
  884. ;; Find the region to compute
  885. (goto-char beg)
  886. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  887. (goto-char end)
  888. ;; Walk the tree from the back and do the computations
  889. (while (re-search-backward re beg t)
  890. (setq sumpos (match-beginning 0)
  891. last-level level
  892. level (org-outline-level)
  893. val (org-entry-get nil property)
  894. valflag (and val (string-match "\\S-" val)))
  895. (cond
  896. ((< level last-level)
  897. ;; put the sum of lower levels here as a property
  898. (setq sum (when (aref lvals last-level)
  899. (apply fun (aref lvals last-level)))
  900. flag (aref lflag last-level) ; any valid entries from children?
  901. str (org-columns-number-to-string sum format printf)
  902. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  903. useval (if flag str1 (if valflag val ""))
  904. sum-alist (get-text-property sumpos 'org-summaries))
  905. (if (assoc property sum-alist)
  906. (setcdr (assoc property sum-alist) useval)
  907. (push (cons property useval) sum-alist)
  908. (org-unmodified
  909. (add-text-properties sumpos (1+ sumpos)
  910. (list 'org-summaries sum-alist))))
  911. (when (and val (not (equal val (if flag str val))))
  912. (org-entry-put nil property (if flag str val)))
  913. ;; add current to current level accumulator
  914. (when (or flag valflag)
  915. (push (if flag
  916. sum
  917. (funcall calc (org-columns-string-to-number
  918. (if flag str val) format)))
  919. (aref lvals level))
  920. (aset lflag level t))
  921. ;; clear accumulators for deeper levels
  922. (loop for l from (1+ level) to (1- lmax) do
  923. (aset lvals l nil)
  924. (aset lflag l nil)))
  925. ((>= level last-level)
  926. ;; add what we have here to the accumulator for this level
  927. (when valflag
  928. (push (funcall calc (org-columns-string-to-number val format))
  929. (aref lvals level))
  930. (aset lflag level t)))
  931. (t (error "This should not happen")))))))
  932. (defun org-columns-redo ()
  933. "Construct the column display again."
  934. (interactive)
  935. (message "Recomputing columns...")
  936. (let ((line (org-current-line))
  937. (col (current-column)))
  938. (save-excursion
  939. (if (marker-position org-columns-begin-marker)
  940. (goto-char org-columns-begin-marker))
  941. (org-columns-remove-overlays)
  942. (if (org-mode-p)
  943. (call-interactively 'org-columns)
  944. (org-agenda-redo)
  945. (call-interactively 'org-agenda-columns)))
  946. (org-goto-line line)
  947. (move-to-column col))
  948. (message "Recomputing columns...done"))
  949. (defun org-columns-not-in-agenda ()
  950. (if (eq major-mode 'org-agenda-mode)
  951. (error "This command is only allowed in Org-mode buffers")))
  952. (defun org-string-to-number (s)
  953. "Convert string to number, and interpret hh:mm:ss."
  954. (if (not (string-match ":" s))
  955. (string-to-number s)
  956. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  957. (while l
  958. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  959. sum)))
  960. (defun org-columns-number-to-string (n fmt &optional printf)
  961. "Convert a computed column number to a string value, according to FMT."
  962. (cond
  963. ((memq fmt '(estimate)) (org-estimate-print n printf))
  964. ((not (numberp n)) "")
  965. ((memq fmt '(add_times max_times min_times mean_times))
  966. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  967. (format org-time-clocksum-format h m)))
  968. ((eq fmt 'checkbox)
  969. (cond ((= n (floor n)) "[X]")
  970. ((> n 1.) "[-]")
  971. (t "[ ]")))
  972. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  973. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  974. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  975. (printf (format printf n))
  976. ((eq fmt 'currency)
  977. (format "%.2f" n))
  978. ((memq fmt '(min_age max_age mean_age))
  979. (org-format-time-period n))
  980. (t (number-to-string n))))
  981. (defun org-nofm-to-completion (n m &optional percent)
  982. (if (not percent)
  983. (format "[%d/%d]" n m)
  984. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  985. (defun org-columns-string-to-number (s fmt)
  986. "Convert a column value to a number that can be used for column computing."
  987. (if s
  988. (cond
  989. ((memq fmt '(min_age max_age mean_age))
  990. (cond ((string= s "") org-columns-time)
  991. ((string-match
  992. "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
  993. s)
  994. (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
  995. (string-to-number (match-string 2 s))))
  996. (string-to-number (match-string 3 s))))
  997. (string-to-number (match-string 4 s))))
  998. (t (time-to-number-of-days (apply 'encode-time
  999. (org-parse-time-string s t))))))
  1000. ((string-match ":" s)
  1001. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1002. (while l
  1003. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1004. sum))
  1005. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  1006. (if (equal s "[X]") 1. 0.000001))
  1007. ((memq fmt '(estimate)) (org-string-to-estimate s))
  1008. (t (string-to-number s)))))
  1009. (defun org-columns-uncompile-format (cfmt)
  1010. "Turn the compiled columns format back into a string representation."
  1011. (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
  1012. (while (setq e (pop cfmt))
  1013. (setq prop (car e)
  1014. title (nth 1 e)
  1015. width (nth 2 e)
  1016. op (nth 3 e)
  1017. fmt (nth 4 e)
  1018. printf (nth 5 e)
  1019. fun (nth 6 e)
  1020. calc (nth 7 e))
  1021. (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
  1022. (setq op (car op-match)))
  1023. (if (and op printf) (setq op (concat op ";" printf)))
  1024. (if (equal title prop) (setq title nil))
  1025. (setq s (concat "%" (if width (number-to-string width))
  1026. prop
  1027. (if title (concat "(" title ")"))
  1028. (if op (concat "{" op "}"))))
  1029. (setq rtn (concat rtn " " s)))
  1030. (org-trim rtn)))
  1031. (defun org-columns-compile-format (fmt)
  1032. "Turn a column format string into an alist of specifications.
  1033. The alist has one entry for each column in the format. The elements of
  1034. that list are:
  1035. property the property
  1036. title the title field for the columns
  1037. width the column width in characters, can be nil for automatic
  1038. operator the operator if any
  1039. format the output format for computed results, derived from operator
  1040. printf a printf format for computed values
  1041. fun the lisp function to compute summary values, derived from operator
  1042. calc function to get values from base elements"
  1043. (let ((start 0) width prop title op op-match f printf fun calc)
  1044. (setq org-columns-current-fmt-compiled nil)
  1045. (while (string-match
  1046. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  1047. fmt start)
  1048. (setq start (match-end 0)
  1049. width (match-string 1 fmt)
  1050. prop (match-string 2 fmt)
  1051. title (or (match-string 3 fmt) prop)
  1052. op (match-string 4 fmt)
  1053. f nil
  1054. printf nil
  1055. fun '+
  1056. calc nil)
  1057. (if width (setq width (string-to-number width)))
  1058. (when (and op (string-match ";" op))
  1059. (setq printf (substring op (match-end 0))
  1060. op (substring op 0 (match-beginning 0))))
  1061. (when (setq op-match (assoc op org-columns-compile-map))
  1062. (setq f (cadr op-match)
  1063. fun (caddr op-match)
  1064. calc (cadddr op-match)))
  1065. (push (list prop title width op f printf fun calc)
  1066. org-columns-current-fmt-compiled))
  1067. (setq org-columns-current-fmt-compiled
  1068. (nreverse org-columns-current-fmt-compiled))))
  1069. ;;; Dynamic block for Column view
  1070. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  1071. "Get the column view of the current buffer or subtree.
  1072. The first optional argument MAXLEVEL sets the level limit. A
  1073. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  1074. empty rows, an empty row being one where all the column view
  1075. specifiers except ITEM are empty. This function returns a list
  1076. containing the title row and all other rows. Each row is a list
  1077. of fields."
  1078. (save-excursion
  1079. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  1080. (re-comment (concat "\\*+[ \t]+" org-comment-string "\\>"))
  1081. (re-archive (concat ".*:" org-archive-tag ":"))
  1082. (n (length title)) row tbl)
  1083. (goto-char (point-min))
  1084. (while (re-search-forward "^\\(\\*+\\) " nil t)
  1085. (catch 'next
  1086. (when (and (or (null maxlevel)
  1087. (>= maxlevel
  1088. (if org-odd-levels-only
  1089. (/ (1+ (length (match-string 1))) 2)
  1090. (length (match-string 1)))))
  1091. (get-char-property (match-beginning 0) 'org-columns-key))
  1092. (when (save-excursion
  1093. (goto-char (point-at-bol))
  1094. (or (looking-at re-comment)
  1095. (looking-at re-archive)))
  1096. (org-end-of-subtree t)
  1097. (throw 'next t))
  1098. (setq row nil)
  1099. (loop for i from 0 to (1- n) do
  1100. (push
  1101. (org-quote-vert
  1102. (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  1103. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  1104. ""))
  1105. row))
  1106. (setq row (nreverse row))
  1107. (unless (and skip-empty-rows
  1108. (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
  1109. (push row tbl)))))
  1110. (append (list title 'hline) (nreverse tbl)))))
  1111. (defun org-dblock-write:columnview (params)
  1112. "Write the column view table.
  1113. PARAMS is a property list of parameters:
  1114. :width enforce same column widths with <N> specifiers.
  1115. :id the :ID: property of the entry where the columns view
  1116. should be built. When the symbol `local', call locally.
  1117. When `global' call column view with the cursor at the beginning
  1118. of the buffer (usually this means that the whole buffer switches
  1119. to column view). When \"file:path/to/file.org\", invoke column
  1120. view at the start of that file. Otherwise, the ID is located
  1121. using `org-id-find'.
  1122. :hlines When t, insert a hline before each item. When a number, insert
  1123. a hline before each level <= that number.
  1124. :vlines When t, make each column a colgroup to enforce vertical lines.
  1125. :maxlevel When set to a number, don't capture headlines below this level.
  1126. :skip-empty-rows
  1127. When t, skip rows where all specifiers other than ITEM are empty."
  1128. (let ((pos (move-marker (make-marker) (point)))
  1129. (hlines (plist-get params :hlines))
  1130. (vlines (plist-get params :vlines))
  1131. (maxlevel (plist-get params :maxlevel))
  1132. (content-lines (org-split-string (plist-get params :content) "\n"))
  1133. (skip-empty-rows (plist-get params :skip-empty-rows))
  1134. tbl id idpos nfields tmp recalc line
  1135. id-as-string view-file view-pos)
  1136. (when (setq id (plist-get params :id))
  1137. (setq id-as-string (cond ((numberp id) (number-to-string id))
  1138. ((symbolp id) (symbol-name id))
  1139. ((stringp id) id)
  1140. (t "")))
  1141. (cond ((not id) nil)
  1142. ((eq id 'global) (setq view-pos (point-min)))
  1143. ((eq id 'local))
  1144. ((string-match "^file:\\(.*\\)" id-as-string)
  1145. (setq view-file (match-string 1 id-as-string)
  1146. view-pos 1)
  1147. (unless (file-exists-p view-file)
  1148. (error "No such file: \"%s\"" id-as-string)))
  1149. ((setq idpos (org-find-entry-with-id id))
  1150. (setq view-pos idpos))
  1151. ((setq idpos (org-id-find id))
  1152. (setq view-file (car idpos))
  1153. (setq view-pos (cdr idpos)))
  1154. (t (error "Cannot find entry with :ID: %s" id))))
  1155. (with-current-buffer (if view-file
  1156. (get-file-buffer view-file)
  1157. (current-buffer))
  1158. (save-excursion
  1159. (save-restriction
  1160. (widen)
  1161. (goto-char (or view-pos (point)))
  1162. (org-columns)
  1163. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  1164. (setq nfields (length (car tbl)))
  1165. (org-columns-quit))))
  1166. (goto-char pos)
  1167. (move-marker pos nil)
  1168. (when tbl
  1169. (when (plist-get params :hlines)
  1170. (setq tmp nil)
  1171. (while tbl
  1172. (if (eq (car tbl) 'hline)
  1173. (push (pop tbl) tmp)
  1174. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  1175. (if (and (not (eq (car tmp) 'hline))
  1176. (or (eq hlines t)
  1177. (and (numberp hlines)
  1178. (<= (- (match-end 1) (match-beginning 1))
  1179. hlines))))
  1180. (push 'hline tmp)))
  1181. (push (pop tbl) tmp)))
  1182. (setq tbl (nreverse tmp)))
  1183. (when vlines
  1184. (setq tbl (mapcar (lambda (x)
  1185. (if (eq 'hline x) x (cons "" x)))
  1186. tbl))
  1187. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  1188. (setq pos (point))
  1189. (when content-lines
  1190. (while (string-match "^#" (car content-lines))
  1191. (insert (pop content-lines) "\n")))
  1192. (insert (org-listtable-to-string tbl))
  1193. (when (plist-get params :width)
  1194. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  1195. org-columns-current-widths "|")))
  1196. (while (setq line (pop content-lines))
  1197. (when (string-match "^#" line)
  1198. (insert "\n" line)
  1199. (when (string-match "^[ \t]*#\\+TBLFM" line)
  1200. (setq recalc t))))
  1201. (if recalc
  1202. (progn (goto-char pos) (org-table-recalculate 'all))
  1203. (goto-char pos)
  1204. (org-table-align)))))
  1205. (defun org-listtable-to-string (tbl)
  1206. "Convert a listtable TBL to a string that contains the Org-mode table.
  1207. The table still need to be aligned. The resulting string has no leading
  1208. and tailing newline characters."
  1209. (mapconcat
  1210. (lambda (x)
  1211. (cond
  1212. ((listp x)
  1213. (concat "|" (mapconcat 'identity x "|") "|"))
  1214. ((eq x 'hline) "|-|")
  1215. (t (error "Garbage in listtable: %s" x))))
  1216. tbl "\n"))
  1217. (defun org-insert-columns-dblock ()
  1218. "Create a dynamic block capturing a column view table."
  1219. (interactive)
  1220. (let ((defaults '(:name "columnview" :hlines 1))
  1221. (id (org-icompleting-read
  1222. "Capture columns (local, global, entry with :ID: property) [local]: "
  1223. (append '(("global") ("local"))
  1224. (mapcar 'list (org-property-values "ID"))))))
  1225. (if (equal id "") (setq id 'local))
  1226. (if (equal id "global") (setq id 'global))
  1227. (setq defaults (append defaults (list :id id)))
  1228. (org-create-dblock defaults)
  1229. (org-update-dblock)))
  1230. ;;; Column view in the agenda
  1231. (defvar org-agenda-view-columns-initially nil
  1232. "When set, switch to columns view immediately after creating the agenda.")
  1233. (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
  1234. (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
  1235. (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
  1236. (defun org-agenda-columns ()
  1237. "Turn on or update column view in the agenda."
  1238. (interactive)
  1239. (org-verify-version 'columns)
  1240. (org-columns-remove-overlays)
  1241. (move-marker org-columns-begin-marker (point))
  1242. (let ((org-columns-time (time-to-number-of-days (current-time)))
  1243. cache maxwidths m p a d fmt)
  1244. (cond
  1245. ((and (boundp 'org-agenda-overriding-columns-format)
  1246. org-agenda-overriding-columns-format)
  1247. (setq fmt org-agenda-overriding-columns-format)
  1248. (org-set-local 'org-agenda-overriding-columns-format fmt))
  1249. ((setq m (org-get-at-bol 'org-hd-marker))
  1250. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1251. (with-current-buffer (marker-buffer m)
  1252. org-columns-default-format))))
  1253. ((and (boundp 'org-columns-current-fmt)
  1254. (local-variable-p 'org-columns-current-fmt)
  1255. org-columns-current-fmt)
  1256. (setq fmt org-columns-current-fmt))
  1257. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  1258. (setq m (get-text-property m 'org-hd-marker))
  1259. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1260. (with-current-buffer (marker-buffer m)
  1261. org-columns-default-format)))))
  1262. (setq fmt (or fmt org-columns-default-format))
  1263. (org-set-local 'org-columns-current-fmt fmt)
  1264. (org-columns-compile-format fmt)
  1265. (when org-agenda-columns-compute-summary-properties
  1266. (org-agenda-colview-compute org-columns-current-fmt-compiled))
  1267. (save-excursion
  1268. ;; Get and cache the properties
  1269. (goto-char (point-min))
  1270. (while (not (eobp))
  1271. (when (setq m (or (org-get-at-bol 'org-hd-marker)
  1272. (org-get-at-bol 'org-marker)))
  1273. (setq p (org-entry-properties m))
  1274. (when (or (not (setq a (assoc org-effort-property p)))
  1275. (not (string-match "\\S-" (or (cdr a) ""))))
  1276. ;; OK, the property is not defined. Use appointment duration?
  1277. (when (and org-agenda-columns-add-appointments-to-effort-sum
  1278. (setq d (get-text-property (point) 'duration)))
  1279. (setq d (org-minutes-to-hh:mm-string d))
  1280. (put-text-property 0 (length d) 'face 'org-warning d)
  1281. (push (cons org-effort-property d) p)))
  1282. (push (cons (org-current-line) p) cache))
  1283. (beginning-of-line 2))
  1284. (when cache
  1285. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  1286. (org-set-local 'org-columns-current-maxwidths maxwidths)
  1287. (org-columns-display-here-title)
  1288. (when (org-set-local 'org-columns-flyspell-was-active
  1289. (org-bound-and-true-p flyspell-mode))
  1290. (flyspell-mode 0))
  1291. (mapc (lambda (x)
  1292. (org-goto-line (car x))
  1293. (org-columns-display-here (cdr x)))
  1294. cache)
  1295. (when org-agenda-columns-show-summaries
  1296. (org-agenda-colview-summarize cache))))))
  1297. (defun org-agenda-colview-summarize (cache)
  1298. "Summarize the summarizable columns in column view in the agenda.
  1299. This will add overlays to the date lines, to show the summary for each day."
  1300. (let* ((fmt (mapcar (lambda (x)
  1301. (if (equal (car x) "CLOCKSUM")
  1302. (list "CLOCKSUM" (nth 1 x) (nth 2 x) ":" 'add_times
  1303. nil '+ nil)
  1304. x))
  1305. org-columns-current-fmt-compiled))
  1306. line c c1 stype calc sumfunc props lsum entries prop v title)
  1307. (catch 'exit
  1308. (when (delq nil (mapcar 'cadr fmt))
  1309. ;; OK, at least one summation column, it makes sense to try this
  1310. (goto-char (point-max))
  1311. (while t
  1312. (when (or (get-text-property (point) 'org-date-line)
  1313. (eq (get-text-property (point) 'face)
  1314. 'org-agenda-structure))
  1315. ;; OK, this is a date line that should be used
  1316. (setq line (org-current-line))
  1317. (setq entries nil c cache cache nil)
  1318. (while (setq c1 (pop c))
  1319. (if (> (car c1) line)
  1320. (push c1 entries)
  1321. (push c1 cache)))
  1322. ;; now ENTRIES are the ones we want to use, CACHE is the rest
  1323. ;; Compute the summaries for the properties we want,
  1324. ;; set nil properties for the rest.
  1325. (when (setq entries (mapcar 'cdr entries))
  1326. (setq props
  1327. (mapcar
  1328. (lambda (f)
  1329. (setq prop (car f)
  1330. title (nth 1 f)
  1331. stype (nth 4 f)
  1332. sumfunc (nth 6 f)
  1333. calc (or (nth 7 f) 'identity))
  1334. (cond
  1335. ((equal prop "ITEM")
  1336. (cons prop (buffer-substring (point-at-bol)
  1337. (point-at-eol))))
  1338. ((not stype) (cons prop ""))
  1339. (t ;; do the summary
  1340. (setq lsum nil)
  1341. (dolist (x entries)
  1342. (setq v (cdr (assoc prop x)))
  1343. (if v
  1344. (push
  1345. (funcall
  1346. (if (not (get-text-property 0 'org-computed v))
  1347. calc
  1348. 'identity)
  1349. (org-columns-string-to-number
  1350. v stype))
  1351. lsum)))
  1352. (setq lsum (remove nil lsum))
  1353. (setq lsum
  1354. (cond ((> (length lsum) 1)
  1355. (org-columns-number-to-string
  1356. (apply sumfunc lsum) stype))
  1357. ((eq (length lsum) 1)
  1358. (org-columns-number-to-string
  1359. (car lsum) stype))
  1360. (t "")))
  1361. (put-text-property 0 (length lsum) 'face 'bold lsum)
  1362. (unless (eq calc 'identity)
  1363. (put-text-property 0 (length lsum) 'org-computed t lsum))
  1364. (cons prop lsum))))
  1365. fmt))
  1366. (org-columns-display-here props 'dateline)
  1367. (org-set-local 'org-agenda-columns-active t)))
  1368. (if (bobp) (throw 'exit t))
  1369. (beginning-of-line 0))))))
  1370. (defun org-agenda-colview-compute (fmt)
  1371. "Compute the relevant columns in the contributing source buffers."
  1372. (let ((files org-agenda-contributing-files)
  1373. (org-columns-begin-marker (make-marker))
  1374. (org-columns-top-level-marker (make-marker))
  1375. f fm a b)
  1376. (while (setq f (pop files))
  1377. (setq b (find-buffer-visiting f))
  1378. (with-current-buffer (or (buffer-base-buffer b) b)
  1379. (save-excursion
  1380. (save-restriction
  1381. (widen)
  1382. (org-unmodified
  1383. (remove-text-properties (point-min) (point-max)
  1384. '(org-summaries t)))
  1385. (goto-char (point-min))
  1386. (org-columns-get-format-and-top-level)
  1387. (while (setq fm (pop fmt))
  1388. (if (equal (car fm) "CLOCKSUM")
  1389. (org-clock-sum)
  1390. (when (and (nth 4 fm)
  1391. (setq a (assoc (car fm)
  1392. org-columns-current-fmt-compiled))
  1393. (equal (nth 4 a) (nth 4 fm)))
  1394. (org-columns-compute (car fm)))))))))))
  1395. (defun org-format-time-period (interval)
  1396. "Convert time in fractional days to days/hours/minutes/seconds."
  1397. (if (numberp interval)
  1398. (let* ((days (floor interval))
  1399. (frac-hours (* 24 (- interval days)))
  1400. (hours (floor frac-hours))
  1401. (minutes (floor (* 60 (- frac-hours hours))))
  1402. (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
  1403. (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
  1404. ""))
  1405. (defun org-estimate-mean-and-var (v)
  1406. "Return the mean and variance of an estimate."
  1407. (let* ((low (float (car v)))
  1408. (high (float (cadr v)))
  1409. (mean (/ (+ low high) 2.0))
  1410. (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
  1411. (list mean var)))
  1412. (defun org-estimate-combine (&rest el)
  1413. "Combine a list of estimates, using mean and variance.
  1414. The mean and variance of the result will be the sum of the means
  1415. and variances (respectively) of the individual estimates."
  1416. (let ((mean 0)
  1417. (var 0))
  1418. (mapc (lambda (e)
  1419. (let ((stats (org-estimate-mean-and-var e)))
  1420. (setq mean (+ mean (car stats)))
  1421. (setq var (+ var (cadr stats)))))
  1422. el)
  1423. (let ((stdev (sqrt var)))
  1424. (list (- mean stdev) (+ mean stdev)))))
  1425. (defun org-estimate-print (e &optional fmt)
  1426. "Prepare a string representation of an estimate.
  1427. This formats these numbers as two numbers with a \"-\" between them."
  1428. (if (null fmt) (set 'fmt "%.0f"))
  1429. (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
  1430. (defun org-string-to-estimate (s)
  1431. "Convert a string to an estimate.
  1432. The string should be two numbers joined with a \"-\"."
  1433. (if (string-match "\\(.*\\)-\\(.*\\)" s)
  1434. (list (string-to-number (match-string 1 s))
  1435. (string-to-number(match-string 2 s)))
  1436. (list (string-to-number s) (string-to-number s))))
  1437. (provide 'org-colview)
  1438. ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c
  1439. ;;; org-colview.el ends here