org-colview-xemacs.el 66 KB

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