org-colview-xemacs.el 66 KB

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