org-colview.el 46 KB

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