org-colview.el 50 KB

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