org-colview.el 49 KB

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