org-colview.el 46 KB

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