org-colview-xemacs.el 66 KB

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