org-colview.el 57 KB

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