org-colview-xemacs.el 66 KB

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