org-colview.el 47 KB

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