org-colview-xemacs.el 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. ;;; org-colview-xemacs.el --- Column View in Org-mode, XEmacs-specific version
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  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: 6.30trans
  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. "Loval variable, holds the currently widths of fields.")
  160. (make-variable-buffer-local 'org-columns-current-widths)
  161. (defvar org-columns-current-maxwidths nil
  162. "Loval 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 (org-make-overlay beg end)))
  252. (if (featurep 'xemacs)
  253. (progn
  254. (org-overlay-put ov 'face (or face 'org-columns-space1))
  255. (org-overlay-put ov 'start-open t)
  256. (if string
  257. (org-overlay-display ov string (or face 'org-columns-space1))))
  258. (org-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 (or (get-text-property (point-at-bol) 'prefix-length) 0))
  289. (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
  290. pom property ass width f string ov column val modval s2 title)
  291. ;; Check if the entry is in another buffer.
  292. (unless props
  293. (if (eq major-mode 'org-agenda-mode)
  294. (setq pom (or (org-get-at-bol 'org-hd-marker)
  295. (org-get-at-bol 'org-marker))
  296. props (if pom (org-entry-properties pom) nil))
  297. (setq props (org-entry-properties nil))))
  298. ;; Walk the format
  299. (while (setq column (pop fmt))
  300. (setq property (car column)
  301. title (nth 1 column)
  302. ass (if (equal property "ITEM")
  303. (cons "ITEM" item)
  304. (assoc property props))
  305. width (or (cdr (assoc property org-columns-current-maxwidths))
  306. (nth 2 column)
  307. (length property))
  308. f (format (if (featurep 'xemacs) "%%-%d.%ds |" "%%-%d.%ds | ")
  309. width width)
  310. val (or (cdr ass) "")
  311. modval (or (and org-columns-modify-value-for-display-function
  312. (functionp
  313. org-columns-modify-value-for-display-function)
  314. (funcall
  315. org-columns-modify-value-for-display-function
  316. title val))
  317. (if (equal property "ITEM")
  318. (if (org-mode-p)
  319. (org-columns-cleanup-item
  320. val org-columns-current-fmt-compiled)
  321. (org-agenda-columns-cleanup-item
  322. val pl cphr org-columns-current-fmt-compiled)))))
  323. (setq s2 (org-columns-add-ellipses (or modval val) width))
  324. (setq string (format f s2))
  325. ;; Create the overlay
  326. (org-unmodified
  327. (setq ov (org-columns-new-overlay
  328. beg (setq beg (1+ beg)) string face))
  329. (org-overlay-put ov 'keymap org-columns-map)
  330. (org-overlay-put ov 'org-columns-key property)
  331. (org-overlay-put ov 'org-columns-value (cdr ass))
  332. (org-overlay-put ov 'org-columns-value-modified modval)
  333. (org-overlay-put ov 'org-columns-pom pom)
  334. (org-overlay-put ov 'org-columns-format f)
  335. (when (featurep 'xemacs)
  336. (if (or (not (char-after beg))
  337. (equal (char-after beg) ?\n))
  338. (let ((inhibit-read-only t))
  339. (save-excursion
  340. (goto-char beg)
  341. (org-unmodified (insert " "))
  342. ;; FIXME: add props and remove later?
  343. )))
  344. (goto-char beg)
  345. (org-columns-new-overlay
  346. beg (1+ beg) nil 'org-columns-space)
  347. (setq beg (1+ beg))))
  348. (if (or (not (char-after beg))
  349. (equal (char-after beg) ?\n))
  350. (let ((inhibit-read-only t))
  351. (save-excursion
  352. (goto-char beg)
  353. ;; FIXME: add props and remove later?
  354. (org-unmodified (insert " "))))))
  355. ;; Make the rest of the line disappear.
  356. (org-unmodified
  357. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  358. (org-overlay-put ov 'invisible t)
  359. (org-overlay-put ov 'keymap org-columns-map)
  360. (org-overlay-put ov 'intangible t)
  361. (push ov org-columns-overlays)
  362. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  363. (org-overlay-put ov 'keymap org-columns-map)
  364. (push ov org-columns-overlays)
  365. (let ((inhibit-read-only t))
  366. (put-text-property (max (point-min) (1- (point-at-bol)))
  367. (min (point-max) (1+ (point-at-eol)))
  368. 'read-only "Type `e' to edit property")))))
  369. (defun org-columns-add-ellipses (string width)
  370. "Truncate STRING with WIDTH characters, with ellipses."
  371. (cond
  372. ((<= (length string) width) string)
  373. ((<= width (length org-columns-ellipses))
  374. (substring org-columns-ellipses 0 width))
  375. (t (concat (substring string 0 (- width (length org-columns-ellipses)))
  376. org-columns-ellipses))))
  377. (defvar org-columns-full-header-line-format nil
  378. "The full header line format, will be shifted by horizontal scrolling." )
  379. (defvar org-previous-header-line-format nil
  380. "The header line format before column view was turned on.")
  381. (defvar org-columns-inhibit-recalculation nil
  382. "Inhibit recomputing of columns on column view startup.")
  383. (defvar header-line-format)
  384. (defvar org-columns-previous-hscroll 0)
  385. (defun org-columns-display-here-title ()
  386. "Overlay the newline before the current line with the table title."
  387. (interactive)
  388. (let ((fmt org-columns-current-fmt-compiled)
  389. string (title "")
  390. property width f column str widths)
  391. (while (setq column (pop fmt))
  392. (setq property (car column)
  393. str (or (nth 1 column) property)
  394. width (or (cdr (assoc property org-columns-current-maxwidths))
  395. (nth 2 column)
  396. (length str))
  397. widths (push width widths)
  398. f (format "%%-%d.%ds | " width width)
  399. string (format f str)
  400. title (concat title string)))
  401. (if (featurep 'xemacs)
  402. (let ((ext (make-extent nil nil)))
  403. (set-extent-endpoints ext 0 (length title) title)
  404. (set-extent-face ext (list 'bold 'underline 'org-columns-space1))
  405. (org-set-local 'org-previous-header-line-format
  406. (specifier-specs top-gutter))
  407. (org-set-local 'org-columns-current-widths (nreverse widths))
  408. (set-specifier top-gutter (make-gutter-specifier
  409. (cons (current-buffer) title))))
  410. (setq title (concat
  411. (org-add-props " " nil 'display '(space :align-to 0))
  412. (org-add-props title nil 'face '(:weight bold :underline t))))
  413. (org-set-local 'org-previous-header-line-format header-line-format)
  414. (org-set-local 'org-columns-current-widths (nreverse widths))
  415. (setq org-columns-full-header-line-format title)
  416. (setq org-columns-previous-hscroll -1)
  417. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local))))
  418. (defun org-columns-hscoll-title ()
  419. "Set the header-line-format so that it scrolls along with the table."
  420. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  421. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  422. (setq header-line-format
  423. (concat (substring org-columns-full-header-line-format 0 1)
  424. (substring org-columns-full-header-line-format
  425. (1+ (window-hscroll))))
  426. org-columns-previous-hscroll (window-hscroll))
  427. (force-mode-line-update)))
  428. (defvar org-colview-initial-truncate-line-value nil
  429. "Remember the value of `truncate-lines' across colview.")
  430. (defun org-columns-remove-overlays ()
  431. "Remove all currently active column overlays."
  432. (interactive)
  433. (when (marker-buffer org-columns-begin-marker)
  434. (with-current-buffer (marker-buffer org-columns-begin-marker)
  435. (when (local-variable-p 'org-previous-header-line-format (current-buffer))
  436. (if (featurep 'xemacs)
  437. (set-specifier top-gutter
  438. (make-gutter-specifier
  439. (cons (current-buffer)
  440. (cdar org-previous-header-line-format))))
  441. (setq header-line-format org-previous-header-line-format)
  442. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  443. (kill-local-variable 'org-previous-header-line-format))
  444. (move-marker org-columns-begin-marker nil)
  445. (move-marker org-columns-top-level-marker nil)
  446. (org-unmodified
  447. (mapc 'org-delete-overlay org-columns-overlays)
  448. (setq org-columns-overlays nil)
  449. (let ((inhibit-read-only t))
  450. (remove-text-properties (point-min) (point-max) '(read-only t))))
  451. (when (local-variable-p 'org-colview-initial-truncate-line-value
  452. (current-buffer))
  453. (setq truncate-lines org-colview-initial-truncate-line-value)))))
  454. (defun org-columns-cleanup-item (item fmt)
  455. "Remove from ITEM what is a column in the format FMT."
  456. (if (not org-complex-heading-regexp)
  457. item
  458. (when (string-match org-complex-heading-regexp item)
  459. (setq item
  460. (concat
  461. (org-add-props (match-string 1 item) nil
  462. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  463. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  464. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  465. " " (save-match-data (org-columns-compact-links (match-string 4 item)))
  466. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
  467. (add-text-properties
  468. 0 (1+ (match-end 1))
  469. (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  470. item)
  471. item)))
  472. (defun org-columns-compact-links (s)
  473. "Replace [[link][desc]] with [desc] or [link]."
  474. (while (string-match org-bracket-link-regexp s)
  475. (setq s (replace-match
  476. (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
  477. t t s)))
  478. s)
  479. (defvar org-agenda-columns-remove-prefix-from-item)
  480. (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
  481. "Cleanup the time property for agenda column view.
  482. See also the variable `org-agenda-columns-remove-prefix-from-item'."
  483. (let* ((org-complex-heading-regexp cphr)
  484. (prefix (substring item 0 pl))
  485. (rest (substring item pl))
  486. (fake (concat "* " rest))
  487. (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
  488. (if org-agenda-columns-remove-prefix-from-item
  489. cleaned
  490. (concat prefix cleaned))))
  491. (defun org-columns-show-value ()
  492. "Show the full value of the property."
  493. (interactive)
  494. (let ((value (get-char-property (point) 'org-columns-value)))
  495. (message "Value is: %s" (or value ""))))
  496. (defvar org-agenda-columns-active) ;; defined in org-agenda.el
  497. (defun org-columns-quit ()
  498. "Remove the column overlays and in this way exit column editing."
  499. (interactive)
  500. (org-unmodified
  501. (org-columns-remove-overlays)
  502. (let ((inhibit-read-only t))
  503. (remove-text-properties (point-min) (point-max) '(read-only t))))
  504. (when (eq major-mode 'org-agenda-mode)
  505. (setq org-agenda-columns-active nil)
  506. (message
  507. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  508. (defun org-columns-check-computed ()
  509. "Check if this column value is computed.
  510. If yes, throw an error indicating that changing it does not make sense."
  511. (let ((val (get-char-property (point) 'org-columns-value)))
  512. (when (and (stringp val)
  513. (get-char-property 0 'org-computed val))
  514. (error "This value is computed from the entry's children"))))
  515. (defun org-columns-todo (&optional arg)
  516. "Change the TODO state during column view."
  517. (interactive "P")
  518. (org-columns-edit-value "TODO"))
  519. (defun org-columns-set-tags-or-toggle (&optional arg)
  520. "Toggle checkbox at point, or set tags for current headline."
  521. (interactive "P")
  522. (if (string-match "\\`\\[[ xX-]\\]\\'"
  523. (get-char-property (point) 'org-columns-value))
  524. (org-columns-next-allowed-value)
  525. (org-columns-edit-value "TAGS")))
  526. (defun org-columns-edit-value (&optional key)
  527. "Edit the value of the property at point in column view.
  528. Where possible, use the standard interface for changing this line."
  529. (interactive)
  530. (org-columns-check-computed)
  531. (let* ((col (current-column))
  532. (key (or key (get-char-property (point) 'org-columns-key)))
  533. (value (get-char-property (point) 'org-columns-value))
  534. (bol (point-at-bol)) (eol (point-at-eol))
  535. (pom (or (get-text-property bol 'org-hd-marker)
  536. (point))) ; keep despite of compiler warning
  537. (line-overlays
  538. (delq nil (mapcar (lambda (x)
  539. (and (eq (org-overlay-buffer x) (current-buffer))
  540. (>= (org-overlay-start x) bol)
  541. (<= (org-overlay-start x) eol)
  542. x))
  543. org-columns-overlays)))
  544. nval eval allowed)
  545. (cond
  546. ((equal key "CLOCKSUM")
  547. (error "This special column cannot be edited"))
  548. ((equal key "ITEM")
  549. (setq eval '(org-with-point-at pom (org-edit-headline))))
  550. ((equal key "TODO")
  551. (setq eval '(org-with-point-at
  552. pom
  553. (call-interactively 'org-todo))))
  554. ((equal key "PRIORITY")
  555. (setq eval '(org-with-point-at pom
  556. (call-interactively 'org-priority))))
  557. ((equal key "TAGS")
  558. (setq eval '(org-with-point-at
  559. pom
  560. (let ((org-fast-tag-selection-single-key
  561. (if (eq org-fast-tag-selection-single-key 'expert)
  562. t org-fast-tag-selection-single-key)))
  563. (call-interactively 'org-set-tags)))))
  564. ((equal key "DEADLINE")
  565. (setq eval '(org-with-point-at
  566. pom
  567. (call-interactively 'org-deadline))))
  568. ((equal key "SCHEDULED")
  569. (setq eval '(org-with-point-at
  570. pom
  571. (call-interactively 'org-schedule))))
  572. (t
  573. (setq allowed (org-property-get-allowed-values pom key 'table))
  574. (if allowed
  575. (setq nval (org-icompleting-read "Value: " allowed nil t))
  576. (setq nval (read-string "Edit: " value)))
  577. (setq nval (org-trim nval))
  578. (when (not (equal nval value))
  579. (setq eval '(org-entry-put pom key nval)))))
  580. (when eval
  581. (cond
  582. ((equal major-mode 'org-agenda-mode)
  583. (org-columns-eval eval)
  584. ;; The following let preserves the current format, and makes sure
  585. ;; that in only a single file things need to be upated.
  586. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  587. (buffer (marker-buffer pom))
  588. (org-agenda-contributing-files
  589. (list (with-current-buffer buffer
  590. (buffer-file-name (buffer-base-buffer))))))
  591. (org-agenda-columns)))
  592. (t
  593. (let ((inhibit-read-only t))
  594. (org-unmodified
  595. (remove-text-properties
  596. (max (point-min) (1- bol)) eol '(read-only t)))
  597. (unwind-protect
  598. (progn
  599. (setq org-columns-overlays
  600. (org-delete-all line-overlays org-columns-overlays))
  601. (mapc 'org-delete-overlay line-overlays)
  602. (org-columns-eval eval))
  603. (org-columns-display-here)))
  604. (org-move-to-column col)
  605. (if (and (org-mode-p)
  606. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  607. (org-columns-update key)))))))
  608. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  609. "Edit the current headline, the part without TODO keyword, TAGS."
  610. (org-back-to-heading)
  611. (when (looking-at org-todo-line-regexp)
  612. (let ((pos (point))
  613. (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  614. (txt (match-string 3))
  615. (post "")
  616. txt2)
  617. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  618. (setq post (match-string 0 txt)
  619. txt (substring txt 0 (match-beginning 0))))
  620. (setq txt2 (read-string "Edit: " txt))
  621. (when (not (equal txt txt2))
  622. (goto-char pos)
  623. (insert pre txt2 post)
  624. (delete-region (point) (point-at-eol))
  625. (org-set-tags nil t)))))
  626. (defun org-columns-edit-allowed ()
  627. "Edit the list of allowed values for the current property."
  628. (interactive)
  629. (let* ((pom (or (org-get-at-bol 'org-marker)
  630. (org-get-at-bol 'org-hd-marker)
  631. (point)))
  632. (key (get-char-property (point) 'org-columns-key))
  633. (key1 (concat key "_ALL"))
  634. (allowed (org-entry-get pom key1 t))
  635. nval)
  636. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  637. ;; FIXME: Write back to #+PROPERTY setting if that is needed.
  638. (setq nval (read-string "Allowed: " allowed))
  639. (org-entry-put
  640. (cond ((marker-position org-entry-property-inherited-from)
  641. org-entry-property-inherited-from)
  642. ((marker-position org-columns-top-level-marker)
  643. org-columns-top-level-marker)
  644. (t pom))
  645. key1 nval)))
  646. (defun org-columns-eval (form)
  647. (let (hidep)
  648. (save-excursion
  649. (beginning-of-line 1)
  650. ;; `next-line' is needed here, because it skips invisible line.
  651. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  652. (setq hidep (org-on-heading-p 1)))
  653. (eval form)
  654. (and hidep (hide-entry))))
  655. (defun org-columns-previous-allowed-value ()
  656. "Switch to the previous allowed value for this column."
  657. (interactive)
  658. (org-columns-next-allowed-value t))
  659. (defun org-columns-next-allowed-value (&optional previous nth)
  660. "Switch to the next allowed value for this column.
  661. When PREVIOUS is set, go to the previous value. When NTH is
  662. an integer, select that value."
  663. (interactive)
  664. (org-columns-check-computed)
  665. (let* ((col (current-column))
  666. (key (get-char-property (point) 'org-columns-key))
  667. (value (get-char-property (point) 'org-columns-value))
  668. (bol (point-at-bol)) (eol (point-at-eol))
  669. (pom (or (get-text-property bol 'org-hd-marker)
  670. (point))) ; keep despite of compiler waring
  671. (line-overlays
  672. (delq nil (mapcar (lambda (x)
  673. (and (eq (org-overlay-buffer x) (current-buffer))
  674. (>= (org-overlay-start x) bol)
  675. (<= (org-overlay-start x) eol)
  676. x))
  677. org-columns-overlays)))
  678. (allowed (or (org-property-get-allowed-values pom key)
  679. (and (memq
  680. (nth 4 (assoc key org-columns-current-fmt-compiled))
  681. '(checkbox checkbox-n-of-m checkbox-percent))
  682. '("[ ]" "[X]"))
  683. (org-colview-construct-allowed-dates value)))
  684. nval)
  685. (when (integerp nth)
  686. (setq nth (1- nth))
  687. (if (= nth -1) (setq nth 9)))
  688. (when (equal key "ITEM")
  689. (error "Cannot edit item headline from here"))
  690. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  691. (error "Allowed values for this property have not been defined"))
  692. (if (member key '("SCHEDULED" "DEADLINE"))
  693. (setq nval (if previous 'earlier 'later))
  694. (if previous (setq allowed (reverse allowed)))
  695. (cond
  696. (nth
  697. (setq nval (nth nth allowed))
  698. (if (not nval)
  699. (error "There are only %d allowed values for property `%s'"
  700. (length allowed) key)))
  701. ((member value allowed)
  702. (setq nval (or (car (cdr (member value allowed)))
  703. (car allowed)))
  704. (if (equal nval value)
  705. (error "Only one allowed value for this property")))
  706. (t (setq nval (car allowed)))))
  707. (cond
  708. ((equal major-mode 'org-agenda-mode)
  709. (org-columns-eval '(org-entry-put pom key nval))
  710. ;; The following let preserves the current format, and makes sure
  711. ;; that in only a single file things need to be upated.
  712. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  713. (buffer (marker-buffer pom))
  714. (org-agenda-contributing-files
  715. (list (with-current-buffer buffer
  716. (buffer-file-name (buffer-base-buffer))))))
  717. (org-agenda-columns)))
  718. (t
  719. (let ((inhibit-read-only t))
  720. (remove-text-properties (1- bol) eol '(read-only t))
  721. (unwind-protect
  722. (progn
  723. (setq org-columns-overlays
  724. (org-delete-all line-overlays org-columns-overlays))
  725. (mapc 'org-delete-overlay line-overlays)
  726. (org-columns-eval '(org-entry-put pom key nval)))
  727. (org-columns-display-here)))
  728. (org-move-to-column col)
  729. (and (nth 3 (assoc key org-columns-current-fmt-compiled))
  730. (org-columns-update key))))))
  731. (defun org-colview-construct-allowed-dates (s)
  732. "Construct a list of three dates around the date in S.
  733. This respects the format of the time stamp in S, active or non-active,
  734. and also including time or not. S must be just a time stamp, no text
  735. around it."
  736. (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
  737. (let* ((time (org-parse-time-string s 'nodefaults))
  738. (active (equal (string-to-char s) ?<))
  739. (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
  740. time-before time-after)
  741. (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  742. (setf (car time) (or (car time) 0))
  743. (setf (nth 1 time) (or (nth 1 time) 0))
  744. (setf (nth 2 time) (or (nth 2 time) 0))
  745. (setq time-before (copy-sequence time))
  746. (setq time-after (copy-sequence time))
  747. (setf (nth 3 time-before) (1- (nth 3 time)))
  748. (setf (nth 3 time-after) (1+ (nth 3 time)))
  749. (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
  750. (list time-before time time-after)))))
  751. (defun org-verify-version (task)
  752. (cond
  753. ((eq task 'columns)
  754. (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
  755. (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
  756. (error "This version of Emacs cannot run Column View")))))
  757. (defun org-columns-open-link (&optional arg)
  758. (interactive "P")
  759. (let ((value (get-char-property (point) 'org-columns-value)))
  760. (org-open-link-from-string value arg)))
  761. (defun org-columns-get-format-and-top-level ()
  762. (let (fmt)
  763. (when (condition-case nil (org-back-to-heading) (error nil))
  764. (setq fmt (org-entry-get nil "COLUMNS" t)))
  765. (setq fmt (or fmt org-columns-default-format))
  766. (org-set-local 'org-columns-current-fmt fmt)
  767. (org-columns-compile-format fmt)
  768. (if (marker-position org-entry-property-inherited-from)
  769. (move-marker org-columns-top-level-marker
  770. org-entry-property-inherited-from)
  771. (move-marker org-columns-top-level-marker (point)))
  772. fmt))
  773. (defun org-columns ()
  774. "Turn on column view on an org-mode file."
  775. (interactive)
  776. (org-verify-version 'columns)
  777. (when (featurep 'xemacs)
  778. (set-face-foreground 'org-columns-space
  779. (face-background 'org-columns-space)))
  780. (org-columns-remove-overlays)
  781. (move-marker org-columns-begin-marker (point))
  782. (let (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 (concat "^" outline-regexp) end t)
  799. (if (and org-columns-skip-arrchived-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. (defvar org-columns-compile-map
  817. '(("none" none +)
  818. (":" add_times +)
  819. ("+" add_numbers +)
  820. ("$" currency +)
  821. ("X" checkbox +)
  822. ("X/" checkbox-n-of-m +)
  823. ("X%" checkbox-percent +)
  824. ("max" max_numbers max)
  825. ("min" min_numbers min)
  826. ("mean" mean_numbers (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
  827. (":max" max_times max)
  828. (":min" min_times min)
  829. (":mean" mean_times (lambda (&rest x) (/ (apply '+ x) (float (length x))))))
  830. "Operator <-> format,fuction map.
  831. Used to compile/uncompile columns format and completing read in
  832. interactive function org-columns-new.")
  833. (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
  834. "Insert a new column, to the left of the current column."
  835. (interactive)
  836. (let ((n (org-columns-current-column))
  837. (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  838. cell)
  839. (setq prop (org-icompleting-read
  840. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  841. nil nil prop))
  842. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  843. (setq width (read-string "Column width: " (if width (number-to-string width))))
  844. (if (string-match "\\S-" width)
  845. (setq width (string-to-number width))
  846. (setq width nil))
  847. (setq fmt (org-icompleting-read "Summary [none]: "
  848. (mapcar (lambda (x) (list (symbol-name (cadr x)))) org-columns-compile-map)
  849. nil t))
  850. (setq fmt (intern fmt)
  851. fun (cadr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
  852. (if (eq fmt 'none) (setq fmt nil))
  853. (if editp
  854. (progn
  855. (setcar editp prop)
  856. (setcdr editp (list title width nil fmt nil fun)))
  857. (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
  858. (setcdr cell (cons (list prop title width nil fmt nil fun)
  859. (cdr cell))))
  860. (org-columns-store-format)
  861. (org-columns-redo)))
  862. (defun org-columns-delete ()
  863. "Delete the column at point from columns view."
  864. (interactive)
  865. (let* ((n (org-columns-current-column))
  866. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  867. (when (y-or-n-p
  868. (format "Are you sure you want to remove column \"%s\"? " title))
  869. (setq org-columns-current-fmt-compiled
  870. (delq (nth n org-columns-current-fmt-compiled)
  871. org-columns-current-fmt-compiled))
  872. (org-columns-store-format)
  873. (org-columns-redo)
  874. (if (>= (org-columns-current-column)
  875. (length org-columns-current-fmt-compiled))
  876. (org-columns-backward-char)))))
  877. (defun org-columns-edit-attributes ()
  878. "Edit the attributes of the current column."
  879. (interactive)
  880. (let* ((n (org-columns-current-column))
  881. (info (nth n org-columns-current-fmt-compiled)))
  882. (apply 'org-columns-new info)))
  883. (defun org-columns-widen (arg)
  884. "Make the column wider by ARG characters."
  885. (interactive "p")
  886. (let* ((n (org-columns-current-column))
  887. (entry (nth n org-columns-current-fmt-compiled))
  888. (width (or (nth 2 entry)
  889. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  890. (setq width (max 1 (+ width arg)))
  891. (setcar (nthcdr 2 entry) width)
  892. (org-columns-store-format)
  893. (org-columns-redo)))
  894. (defun org-columns-narrow (arg)
  895. "Make the column narrower by ARG characters."
  896. (interactive "p")
  897. (org-columns-widen (- arg)))
  898. (defun org-columns-move-right ()
  899. "Swap this column with the one to the right."
  900. (interactive)
  901. (let* ((n (org-columns-current-column))
  902. (cell (nthcdr n org-columns-current-fmt-compiled))
  903. e)
  904. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  905. (error "Cannot shift this column further to the right"))
  906. (setq e (car cell))
  907. (setcar cell (car (cdr cell)))
  908. (setcdr cell (cons e (cdr (cdr cell))))
  909. (org-columns-store-format)
  910. (org-columns-redo)
  911. (org-columns-forward-char)))
  912. (defun org-columns-move-left ()
  913. "Swap this column with the one to the left."
  914. (interactive)
  915. (let* ((n (org-columns-current-column)))
  916. (when (= n 0)
  917. (error "Cannot shift this column further to the left"))
  918. (org-columns-backward-char)
  919. (org-columns-move-right)
  920. (org-columns-backward-char)))
  921. (defun org-columns-store-format ()
  922. "Store the text version of the current columns format in appropriate place.
  923. This is either in the COLUMNS property of the node starting the current column
  924. display, or in the #+COLUMNS line of the current buffer."
  925. (let (fmt (cnt 0))
  926. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  927. (org-set-local 'org-columns-current-fmt fmt)
  928. (if (marker-position org-columns-top-level-marker)
  929. (save-excursion
  930. (goto-char org-columns-top-level-marker)
  931. (if (and (org-at-heading-p)
  932. (org-entry-get nil "COLUMNS"))
  933. (org-entry-put nil "COLUMNS" fmt)
  934. (goto-char (point-min))
  935. ;; Overwrite all #+COLUMNS lines....
  936. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  937. (setq cnt (1+ cnt))
  938. (replace-match (concat "#+COLUMNS: " fmt) t t))
  939. (unless (> cnt 0)
  940. (goto-char (point-min))
  941. (or (org-on-heading-p t) (outline-next-heading))
  942. (let ((inhibit-read-only t))
  943. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  944. (org-set-local 'org-columns-default-format fmt))))))
  945. (defvar org-agenda-overriding-columns-format nil
  946. "When set, overrides any other format definition for the agenda.
  947. Don't set this, this is meant for dynamic scoping.")
  948. (defun org-columns-get-autowidth-alist (s cache)
  949. "Derive the maximum column widths from the format and the cache."
  950. (let ((start 0) rtn)
  951. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  952. (push (cons (match-string 1 s) 1) rtn)
  953. (setq start (match-end 0)))
  954. (mapc (lambda (x)
  955. (setcdr x (apply 'max
  956. (mapcar
  957. (lambda (y)
  958. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  959. cache))))
  960. rtn)
  961. rtn))
  962. (defun org-columns-compute-all ()
  963. "Compute all columns that have operators defined."
  964. (org-unmodified
  965. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  966. (let ((columns org-columns-current-fmt-compiled) col)
  967. (while (setq col (pop columns))
  968. (when (nth 3 col)
  969. (save-excursion
  970. (org-columns-compute (car col)))))))
  971. (defun org-columns-update (property)
  972. "Recompute PROPERTY, and update the columns display for it."
  973. (org-columns-compute property)
  974. (let (fmt val pos face)
  975. (save-excursion
  976. (mapc (lambda (ov)
  977. (when (equal (org-overlay-get ov 'org-columns-key) property)
  978. (setq pos (org-overlay-start ov))
  979. (goto-char pos)
  980. (when (setq val (cdr (assoc property
  981. (get-text-property
  982. (point-at-bol) 'org-summaries))))
  983. (setq fmt (org-overlay-get ov 'org-columns-format))
  984. (org-overlay-put ov 'org-columns-value val)
  985. (if (featurep 'xemacs)
  986. (progn
  987. (setq face (glyph-face (extent-end-glyph ov)))
  988. (org-overlay-display ov (format fmt val) face))
  989. (org-overlay-display ov (format fmt val))))))
  990. org-columns-overlays))))
  991. (defun org-columns-compute (property)
  992. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  993. (interactive)
  994. (let* ((re (concat "^" outline-regexp))
  995. (lmax 30) ; Does anyone use deeper levels???
  996. (lvals (make-vector lmax nil))
  997. (lflag (make-vector lmax nil))
  998. (level 0)
  999. (ass (assoc property org-columns-current-fmt-compiled))
  1000. (format (nth 4 ass))
  1001. (printf (nth 5 ass))
  1002. (fun (nth 6 ass))
  1003. (beg org-columns-top-level-marker)
  1004. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  1005. (save-excursion
  1006. ;; Find the region to compute
  1007. (goto-char beg)
  1008. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  1009. (goto-char end)
  1010. ;; Walk the tree from the back and do the computations
  1011. (while (re-search-backward re beg t)
  1012. (setq sumpos (match-beginning 0)
  1013. last-level level
  1014. level (org-outline-level)
  1015. val (org-entry-get nil property)
  1016. valflag (and val (string-match "\\S-" val)))
  1017. (cond
  1018. ((< level last-level)
  1019. ;; put the sum of lower levels here as a property
  1020. (setq sum (when (aref lvals last-level)
  1021. (apply fun (aref lvals last-level)))
  1022. flag (aref lflag last-level) ; any valid entries from children?
  1023. str (org-columns-number-to-string sum format printf)
  1024. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  1025. useval (if flag str1 (if valflag val ""))
  1026. sum-alist (get-text-property sumpos 'org-summaries))
  1027. (if (assoc property sum-alist)
  1028. (setcdr (assoc property sum-alist) useval)
  1029. (push (cons property useval) sum-alist)
  1030. (org-unmodified
  1031. (add-text-properties sumpos (1+ sumpos)
  1032. (list 'org-summaries sum-alist))))
  1033. (when (and val (not (equal val (if flag str val))))
  1034. (org-entry-put nil property (if flag str val)))
  1035. ;; add current to current level accumulator
  1036. (when (or flag valflag)
  1037. (push (if flag sum
  1038. (org-column-string-to-number (if flag str val) format))
  1039. (aref lvals level))
  1040. (aset lflag level t))
  1041. ;; clear accumulators for deeper levels
  1042. (loop for l from (1+ level) to (1- lmax) do
  1043. (aset lvals l nil)
  1044. (aset lflag l nil)))
  1045. ((>= level last-level)
  1046. ;; add what we have here to the accumulator for this level
  1047. (when valflag
  1048. (push (org-column-string-to-number val format)
  1049. (aref lvals level))
  1050. (aset lflag level t)))
  1051. (t (error "This should not happen")))))))
  1052. (defun org-columns-redo ()
  1053. "Construct the column display again."
  1054. (interactive)
  1055. (message "Recomputing columns...")
  1056. (save-excursion
  1057. (if (marker-position org-columns-begin-marker)
  1058. (goto-char org-columns-begin-marker))
  1059. (org-columns-remove-overlays)
  1060. (if (org-mode-p)
  1061. (call-interactively 'org-columns)
  1062. (org-agenda-redo)
  1063. (call-interactively 'org-agenda-columns)))
  1064. (when (featurep 'xemacs)
  1065. (while (not (or (eolp)
  1066. (member (extent-at (point)) org-columns-overlays)))
  1067. (forward-char)))
  1068. (message "Recomputing columns...done"))
  1069. (defun org-columns-not-in-agenda ()
  1070. (if (eq major-mode 'org-agenda-mode)
  1071. (error "This command is only allowed in Org-mode buffers")))
  1072. (defun org-string-to-number (s)
  1073. "Convert string to number, and interpret hh:mm:ss."
  1074. (if (not (string-match ":" s))
  1075. (string-to-number s)
  1076. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1077. (while l
  1078. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1079. sum)))
  1080. (defun org-columns-number-to-string (n fmt &optional printf)
  1081. "Convert a computed column number to a string value, according to FMT."
  1082. (cond
  1083. ((not (numberp n)) "")
  1084. ((memq fmt '(add_times max_times min_times mean_times))
  1085. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  1086. (format org-time-clocksum-format h m)))
  1087. ((eq fmt 'checkbox)
  1088. (cond ((= n (floor n)) "[X]")
  1089. ((> n 1.) "[-]")
  1090. (t "[ ]")))
  1091. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  1092. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  1093. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  1094. (printf (format printf n))
  1095. ((eq fmt 'currency)
  1096. (format "%.2f" n))
  1097. (t (number-to-string n))))
  1098. (defun org-nofm-to-completion (n m &optional percent)
  1099. (if (not percent)
  1100. (format "[%d/%d]" n m)
  1101. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  1102. (defun org-column-string-to-number (s fmt)
  1103. "Convert a column value to a number that can be used for column computing."
  1104. (cond
  1105. ((string-match ":" s)
  1106. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1107. (while l
  1108. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1109. sum))
  1110. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  1111. (if (equal s "[X]") 1. 0.000001))
  1112. (t (string-to-number s))))
  1113. (defun org-columns-uncompile-format (cfmt)
  1114. "Turn the compiled columns format back into a string representation."
  1115. (let ((rtn "") e s prop title op op-match width fmt printf fun)
  1116. (while (setq e (pop cfmt))
  1117. (setq prop (car e)
  1118. title (nth 1 e)
  1119. width (nth 2 e)
  1120. op (nth 3 e)
  1121. fmt (nth 4 e)
  1122. printf (nth 5 e)
  1123. fun (nth 6 e))
  1124. (when (setq op-match (rassoc (list fmt fun) org-columns-compile-map))
  1125. (setq op (car op-match)))
  1126. (if (and op printf) (setq op (concat op ";" printf)))
  1127. (if (equal title prop) (setq title nil))
  1128. (setq s (concat "%" (if width (number-to-string width))
  1129. prop
  1130. (if title (concat "(" title ")"))
  1131. (if op (concat "{" op "}"))))
  1132. (setq rtn (concat rtn " " s)))
  1133. (org-trim rtn)))
  1134. (defun org-columns-compile-format (fmt)
  1135. "Turn a column format string into an alist of specifications.
  1136. The alist has one entry for each column in the format. The elements of
  1137. that list are:
  1138. property the property
  1139. title the title field for the columns
  1140. width the column width in characters, can be nil for automatic
  1141. operator the operator if any
  1142. format the output format for computed results, derived from operator
  1143. printf a printf format for computed values
  1144. fun the lisp function to compute values, derived from operator"
  1145. (let ((start 0) width prop title op op-match f printf fun)
  1146. (setq org-columns-current-fmt-compiled nil)
  1147. (while (string-match
  1148. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  1149. fmt start)
  1150. (setq start (match-end 0)
  1151. width (match-string 1 fmt)
  1152. prop (match-string 2 fmt)
  1153. title (or (match-string 3 fmt) prop)
  1154. op (match-string 4 fmt)
  1155. f nil
  1156. printf nil
  1157. fun '+)
  1158. (if width (setq width (string-to-number width)))
  1159. (when (and op (string-match ";" op))
  1160. (setq printf (substring op (match-end 0))
  1161. op (substring op 0 (match-beginning 0))))
  1162. (when (setq op-match (assoc op org-columns-compile-map))
  1163. (setq f (cadr op-match)
  1164. fun (caddr op-match)))
  1165. (push (list prop title width op f printf fun) org-columns-current-fmt-compiled))
  1166. (setq org-columns-current-fmt-compiled
  1167. (nreverse org-columns-current-fmt-compiled))))
  1168. ;;; Dynamic block for Column view
  1169. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  1170. "Get the column view of the current buffer or subtree.
  1171. The first optional argument MAXLEVEL sets the level limit. A
  1172. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  1173. empty rows, an empty row being one where all the column view
  1174. specifiers except ITEM are empty. This function returns a list
  1175. containing the title row and all other rows. Each row is a list
  1176. of fields."
  1177. (if (featurep 'xemacs)
  1178. (save-excursion
  1179. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  1180. (re-comment (concat "\\*+[ \t]+" org-comment-string "\\>"))
  1181. (re-archive (concat ".*:" org-archive-tag ":"))
  1182. (n (length title)) row tbl)
  1183. (goto-char (point-min))
  1184. (while (re-search-forward "^\\(\\*+\\) " nil t)
  1185. (catch 'next
  1186. (when (and (or (null maxlevel)
  1187. (>= maxlevel
  1188. (if org-odd-levels-only
  1189. (/ (1+ (length (match-string 1))) 2)
  1190. (length (match-string 1)))))
  1191. (get-char-property (match-beginning 0) 'org-columns-key))
  1192. (goto-char (match-beginning 0))
  1193. (when (save-excursion
  1194. (goto-char (point-at-bol))
  1195. (or (looking-at re-comment)
  1196. (looking-at re-archive)))
  1197. (org-end-of-subtree t)
  1198. (throw 'next t))
  1199. (setq row nil)
  1200. (loop for i from 0 to (1- n) do
  1201. (push
  1202. (org-quote-vert
  1203. (or (get-char-property (point)
  1204. 'org-columns-value-modified)
  1205. (get-char-property (point) 'org-columns-value)
  1206. ""))
  1207. row)
  1208. (org-columns-forward-char))
  1209. (setq row (nreverse row))
  1210. (unless (and skip-empty-rows
  1211. (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
  1212. (push row tbl)))))
  1213. (append (list title 'hline) (nreverse tbl))))
  1214. (save-excursion
  1215. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  1216. (n (length title)) row tbl)
  1217. (goto-char (point-min))
  1218. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  1219. (or (null maxlevel)
  1220. (>= maxlevel
  1221. (if org-odd-levels-only
  1222. (/ (1+ (length (match-string 1))) 2)
  1223. (length (match-string 1))))))
  1224. (when (get-char-property (match-beginning 0) 'org-columns-key)
  1225. (setq row nil)
  1226. (loop for i from 0 to (1- n) do
  1227. (push (or (get-char-property (+ (match-beginning 0) i)
  1228. 'org-columns-value-modified)
  1229. (get-char-property (+ (match-beginning 0) i)
  1230. 'org-columns-value)
  1231. "")
  1232. row))
  1233. (setq row (nreverse row))
  1234. (unless (and skip-empty-rows
  1235. (eq 1 (length (delete "" (delete-dups row)))))
  1236. (push row tbl))))
  1237. (append (list title 'hline) (nreverse tbl))))))
  1238. (defun org-dblock-write:columnview (params)
  1239. "Write the column view table.
  1240. PARAMS is a property list of parameters:
  1241. :width enforce same column widths with <N> specifiers.
  1242. :id the :ID: property of the entry where the columns view
  1243. should be built. When the symbol `local', call locally.
  1244. When `global' call column view with the cursor at the beginning
  1245. of the buffer (usually this means that the whole buffer switches
  1246. to column view). When \"file:path/to/file.org\", invoke column
  1247. view at the start of that file. Otherwise, the ID is located
  1248. using `org-id-find'.
  1249. :hlines When t, insert a hline before each item. When a number, insert
  1250. a hline before each level <= that number.
  1251. :vlines When t, make each column a colgroup to enforce vertical lines.
  1252. :maxlevel When set to a number, don't capture headlines below this level.
  1253. :skip-empty-rows
  1254. When t, skip rows where all specifiers other than ITEM are empty."
  1255. (let ((pos (move-marker (make-marker) (point)))
  1256. (hlines (plist-get params :hlines))
  1257. (vlines (plist-get params :vlines))
  1258. (maxlevel (plist-get params :maxlevel))
  1259. (content-lines (org-split-string (plist-get params :content) "\n"))
  1260. (skip-empty-rows (plist-get params :skip-empty-rows))
  1261. tbl id idpos nfields tmp recalc line
  1262. id-as-string view-file view-pos)
  1263. (when (setq id (plist-get params :id))
  1264. (setq id-as-string (cond ((numberp id) (number-to-string id))
  1265. ((symbolp id) (symbol-name id))
  1266. ((stringp id) id)
  1267. (t "")))
  1268. (cond ((not id) nil)
  1269. ((eq id 'global) (setq view-pos (point-min)))
  1270. ((eq id 'local))
  1271. ((string-match "^file:\\(.*\\)" id-as-string)
  1272. (setq view-file (match-string 1 id-as-string)
  1273. view-pos 1)
  1274. (unless (file-exists-p view-file)
  1275. (error "No such file: \"%s\"" id-as-string)))
  1276. ((setq idpos (org-find-entry-with-id id))
  1277. (setq view-pos idpos))
  1278. ((setq idpos (org-id-find id))
  1279. (setq view-file (car idpos))
  1280. (setq view-pos (cdr idpos)))
  1281. (t (error "Cannot find entry with :ID: %s" id))))
  1282. (with-current-buffer (if view-file
  1283. (get-file-buffer view-file)
  1284. (current-buffer))
  1285. (save-excursion
  1286. (save-restriction
  1287. (widen)
  1288. (goto-char (or view-pos (point)))
  1289. (org-columns)
  1290. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  1291. (setq nfields (length (car tbl)))
  1292. (org-columns-quit))))
  1293. (goto-char pos)
  1294. (move-marker pos nil)
  1295. (when tbl
  1296. (when (plist-get params :hlines)
  1297. (setq tmp nil)
  1298. (while tbl
  1299. (if (eq (car tbl) 'hline)
  1300. (push (pop tbl) tmp)
  1301. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  1302. (if (and (not (eq (car tmp) 'hline))
  1303. (or (eq hlines t)
  1304. (and (numberp hlines)
  1305. (<= (- (match-end 1) (match-beginning 1))
  1306. hlines))))
  1307. (push 'hline tmp)))
  1308. (push (pop tbl) tmp)))
  1309. (setq tbl (nreverse tmp)))
  1310. (when vlines
  1311. (setq tbl (mapcar (lambda (x)
  1312. (if (eq 'hline x) x (cons "" x)))
  1313. tbl))
  1314. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  1315. (setq pos (point))
  1316. (when content-lines
  1317. (while (string-match "^#" (car content-lines))
  1318. (insert (pop content-lines) "\n")))
  1319. (insert (org-listtable-to-string tbl))
  1320. (when (plist-get params :width)
  1321. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  1322. org-columns-current-widths "|")))
  1323. (while (setq line (pop content-lines))
  1324. (when (string-match "^#" line)
  1325. (insert "\n" line)
  1326. (when (string-match "^[ \t]*#\\+TBLFM" line)
  1327. (setq recalc t))))
  1328. (if recalc
  1329. (progn (goto-char pos) (org-table-recalculate 'all))
  1330. (goto-char pos)
  1331. (org-table-align)))))
  1332. (defun org-listtable-to-string (tbl)
  1333. "Convert a listtable TBL to a string that contains the Org-mode table.
  1334. The table still need to be aligned. The resulting string has no leading
  1335. and tailing newline characters."
  1336. (mapconcat
  1337. (lambda (x)
  1338. (cond
  1339. ((listp x)
  1340. (concat "|" (mapconcat 'identity x "|") "|"))
  1341. ((eq x 'hline) "|-|")
  1342. (t (error "Garbage in listtable: %s" x))))
  1343. tbl "\n"))
  1344. (defun org-insert-columns-dblock ()
  1345. "Create a dynamic block capturing a column view table."
  1346. (interactive)
  1347. (when (featurep 'xemacs) (org-columns-quit))
  1348. (let ((defaults '(:name "columnview" :hlines 1))
  1349. (id (org-icompleting-read
  1350. "Capture columns (local, global, entry with :ID: property) [local]: "
  1351. (append '(("global") ("local"))
  1352. (mapcar 'list (org-property-values "ID"))))))
  1353. (if (equal id "") (setq id 'local))
  1354. (if (equal id "global") (setq id 'global))
  1355. (setq defaults (append defaults (list :id id)))
  1356. (org-create-dblock defaults)
  1357. (org-update-dblock)))
  1358. ;;; Column view in the agenda
  1359. (defvar org-agenda-view-columns-initially nil
  1360. "When set, switch to columns view immediately after creating the agenda.")
  1361. (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
  1362. (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
  1363. (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
  1364. (defun org-agenda-columns ()
  1365. "Turn on or update column view in the agenda."
  1366. (interactive)
  1367. (org-verify-version 'columns)
  1368. (org-columns-remove-overlays)
  1369. (move-marker org-columns-begin-marker (point))
  1370. (let (fmt cache maxwidths m p a d)
  1371. (cond
  1372. ((and (boundp 'org-agenda-overriding-columns-format)
  1373. org-agenda-overriding-columns-format)
  1374. (setq fmt org-agenda-overriding-columns-format)
  1375. (org-set-local 'org-agenda-overriding-columns-format fmt))
  1376. ((setq m (org-get-at-bol 'org-hd-marker))
  1377. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1378. (with-current-buffer (marker-buffer m)
  1379. org-columns-default-format))))
  1380. ((and (boundp 'org-columns-current-fmt)
  1381. (local-variable-p 'org-columns-current-fmt (current-buffer))
  1382. org-columns-current-fmt)
  1383. (setq fmt org-columns-current-fmt))
  1384. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  1385. (setq m (get-text-property m 'org-hd-marker))
  1386. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1387. (with-current-buffer (marker-buffer m)
  1388. org-columns-default-format)))))
  1389. (setq fmt (or fmt org-columns-default-format))
  1390. (org-set-local 'org-columns-current-fmt fmt)
  1391. (org-columns-compile-format fmt)
  1392. (when org-agenda-columns-compute-summary-properties
  1393. (org-agenda-colview-compute org-columns-current-fmt-compiled))
  1394. (save-excursion
  1395. ;; Get and cache the properties
  1396. (goto-char (point-min))
  1397. (while (not (eobp))
  1398. (when (setq m (or (org-get-at-bol 'org-hd-marker)
  1399. (org-get-at-bol 'org-marker)))
  1400. (setq p (org-entry-properties m))
  1401. (when (or (not (setq a (assoc org-effort-property p)))
  1402. (not (string-match "\\S-" (or (cdr a) ""))))
  1403. ;; OK, the property is not defined. Use appointment duration?
  1404. (when (and org-agenda-columns-add-appointments-to-effort-sum
  1405. (setq d (get-text-property (point) 'duration)))
  1406. (setq d (org-minutes-to-hh:mm-string d))
  1407. (put-text-property 0 (length d) 'face 'org-warning d)
  1408. (push (cons org-effort-property d) p)))
  1409. (push (cons (org-current-line) p) cache))
  1410. (beginning-of-line 2))
  1411. (when cache
  1412. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  1413. (org-set-local 'org-columns-current-maxwidths maxwidths)
  1414. (org-columns-display-here-title)
  1415. (mapc (lambda (x)
  1416. (org-goto-line (car x))
  1417. (org-columns-display-here (cdr x)))
  1418. cache)
  1419. (when org-agenda-columns-show-summaries
  1420. (org-agenda-colview-summarize cache))))))
  1421. (defun org-agenda-colview-summarize (cache)
  1422. "Summarize the summarizable columns in column view in the agenda.
  1423. This will add overlays to the date lines, to show the summary for each day."
  1424. (let* ((fmt (mapcar (lambda (x)
  1425. (list (car x) (if (equal (car x) "CLOCKSUM")
  1426. 'add_times (nth 4 x))))
  1427. org-columns-current-fmt-compiled))
  1428. line c c1 stype props lsum entries prop v)
  1429. (catch 'exit
  1430. (when (delq nil (mapcar 'cadr fmt))
  1431. ;; OK, at least one summation column, it makes sense to try this
  1432. (goto-char (point-max))
  1433. (while t
  1434. (when (or (get-text-property (point) 'org-date-line)
  1435. (eq (get-text-property (point) 'face)
  1436. 'org-agenda-structure))
  1437. ;; OK, this is a date line that should be used
  1438. (setq line (org-current-line))
  1439. (setq entries nil c cache cache nil)
  1440. (while (setq c1 (pop c))
  1441. (if (> (car c1) line)
  1442. (push c1 entries)
  1443. (push c1 cache)))
  1444. ;; now ENTRIES are the ones we want to use, CACHE is the rest
  1445. ;; Compute the summaries for the properties we want,
  1446. ;; set nil properties for the rest.
  1447. (when (setq entries (mapcar 'cdr entries))
  1448. (setq props
  1449. (mapcar
  1450. (lambda (f)
  1451. (setq prop (car f) stype (nth 1 f))
  1452. (cond
  1453. ((equal prop "ITEM")
  1454. (cons prop (buffer-substring (point-at-bol)
  1455. (point-at-eol))))
  1456. ((not stype) (cons prop ""))
  1457. (t
  1458. ;; do the summary
  1459. (setq lsum 0)
  1460. (mapc (lambda (x)
  1461. (setq v (cdr (assoc prop x)))
  1462. (if v (setq lsum (+ lsum
  1463. (org-column-string-to-number
  1464. v stype)))))
  1465. entries)
  1466. (setq lsum (org-columns-number-to-string lsum stype))
  1467. (put-text-property
  1468. 0 (length lsum) 'face 'bold lsum)
  1469. (cons prop lsum))))
  1470. fmt))
  1471. (org-columns-display-here props)
  1472. (org-set-local 'org-agenda-columns-active t)))
  1473. (if (bobp) (throw 'exit t))
  1474. (beginning-of-line 0))))))
  1475. (defun org-agenda-colview-compute (fmt)
  1476. "Compute the relevant columns in the contributing source buffers."
  1477. (let ((files org-agenda-contributing-files)
  1478. (org-columns-begin-marker (make-marker))
  1479. (org-columns-top-level-marker (make-marker))
  1480. f fm a b)
  1481. (while (setq f (pop files))
  1482. (setq b (find-buffer-visiting f))
  1483. (with-current-buffer (or (buffer-base-buffer b) b)
  1484. (save-excursion
  1485. (save-restriction
  1486. (widen)
  1487. (org-unmodified
  1488. (remove-text-properties (point-min) (point-max)
  1489. '(org-summaries t)))
  1490. (goto-char (point-min))
  1491. (org-columns-get-format-and-top-level)
  1492. (while (setq fm (pop fmt))
  1493. (if (equal (car fm) "CLOCKSUM")
  1494. (org-clock-sum)
  1495. (when (and (nth 4 fm)
  1496. (setq a (assoc (car fm)
  1497. org-columns-current-fmt-compiled))
  1498. (equal (nth 4 a) (nth 4 fm)))
  1499. (org-columns-compute (car fm)))))))))))
  1500. (provide 'org-colview)
  1501. (provide 'org-colview-xemacs)
  1502. ;;; org-colview-xemacs.el ends here