org-colview.el 56 KB

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