org-index.el 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  1. ;;; org-index.el --- A personal index for org and beyond
  2. ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. ;; Author: Marc Ihm <org-index@2484.de>
  4. ;; Version: 4.0.0
  5. ;; Keywords: outlines index
  6. ;; This file is not part of GNU Emacs.
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Purpose:
  22. ;;
  23. ;; Fast search for selected org headings and things outside of org.
  24. ;;
  25. ;; This package creates and updates an index table of headings or
  26. ;; keywords, references and ids, where each line points to a heading
  27. ;; within org or references something outside. This table is sorted by
  28. ;; usage count, so that frequently used lines appear among the first
  29. ;; results.
  30. ;;
  31. ;; References are essentially small numbers (e.g. 'R237' or '--455--'), as
  32. ;; created by this package; they are well suited to be used outside of
  33. ;; org, e.g. in folder names, issue trackers or on printed documents.
  34. ;;
  35. ;; On first invocation org-index will guide you to create a dedicated node
  36. ;; for its index table and its configuration flags.
  37. ;;
  38. ;; For basic usage, subcommands 'add' and 'occur' are most important.
  39. ;;
  40. ;;
  41. ;; Setup:
  42. ;;
  43. ;; - Add these lines to your .emacs:
  44. ;;
  45. ;; (require 'org-index)
  46. ;; (org-index-default-keybindings) ; optional
  47. ;;
  48. ;; - Restart your Emacs to make these lines effective.
  49. ;;
  50. ;; - Invoke `org-index', which will assist in creating your index
  51. ;; table. The variable org-index-id will be persisted within your
  52. ;; customization file (typically .emacs).
  53. ;;
  54. ;;
  55. ;; Further reading:
  56. ;;
  57. ;; See the documentation of `org-index', which can also be read
  58. ;; by invoking `org-index' and choosing the help-command.
  59. ;;
  60. ;;
  61. ;; Updates:
  62. ;;
  63. ;; The latest tested version of this file can always be found at:
  64. ;;
  65. ;; http://orgmode.org/w/org-mode.git?p=org-mode.git;a=blob_plain;f=contrib/lisp/org-index.el;hb=HEAD
  66. ;;; Change Log:
  67. ;; [2015-02-26 Th] Version 4.0.0:
  68. ;; - Removed command "leave"; rather go back with org-mark-ring-goto
  69. ;; - Property "org-index-ref" is no longer used or needed
  70. ;; - Renamed column "link" to "id"
  71. ;; - Added maintainance options to find duplicate rows, to check ids,
  72. ;; update index or remove property org-index-ref from nodes
  73. ;;
  74. ;; [2015-01-31 Sa] Version 3.2.0:
  75. ;; - Complete sorting of index only occurs in idle-timer
  76. ;; - Command "add" now updates index, if node is already present
  77. ;; - New command "maintain" with some subcommands
  78. ;;
  79. ;; [2015-01-20 Mo] Version 3.1.1:
  80. ;; - Bugfix for delete within occur
  81. ;;
  82. ;; [2015-01-19 Mo] Version 3.1.0:
  83. ;; - Rewrote command "occur" with overlays in an indirect buffer
  84. ;; - Removed function `org-index-copy-references-from-heading-to-property'
  85. ;; - introduced variable org-index-version
  86. ;;
  87. ;; [2014-12-14 Su] Version 3.0.2:
  88. ;; - Bugfixes in occur mode
  89. ;; - New function `org-index-copy-references-from-heading-to-property'
  90. ;;
  91. ;; [2014-12-10 We] Version 3.0.1:
  92. ;; - Bugfixes related with assistant
  93. ;; - Fix for editing of category
  94. ;;
  95. ;; [2014-12-07 Sa] Version 3.0.0:
  96. ;; - New commands "add" and "delete" to easily add and remove
  97. ;; the current node to or from your index.
  98. ;; - New command "example" to create an example index.
  99. ;; - Moved flags to a list within the same node as the index table;
  100. ;; this breaks compatibility to prior versions of the package.
  101. ;; - Several new flags that are explained within index node.
  102. ;; - Removed commands "reuse", "missing", "put", "goto",
  103. ;; "update", "link", "fill", "unhighlight"
  104. ;; - New function `org-index-default-keybindings'
  105. ;;
  106. ;; [2014-04-26 Sa] Version 2.4.3:
  107. ;; - Some Bug fixes and enhancements for occur-command
  108. ;; - Fixes for assistant to create index table
  109. ;;
  110. ;; [2014-02-01 Sa] Version 2.4.2:
  111. ;; - Follow mode in occur-buffer
  112. ;; - Reorder for x-columns
  113. ;;
  114. ;; [2014-01-02 Th] Version 2.4.0:
  115. ;; - New command "put" to store a nodes reference in a property
  116. ;; - New functions org-index-new-line and org-index-get-line
  117. ;; offer access to org-index from other lisp programs
  118. ;; - New flag p, new columns x1,x2 and x3
  119. ;; - Major Code refactoring
  120. ;; - Regression tests with ert
  121. ;; - Lots of bug fixes
  122. ;;
  123. ;; [2013-10-04 Fr] Version 2.3.2:
  124. ;; - Bug fix: index-table created by assistant is found after
  125. ;; restart of emacs instead of invoking assistant again
  126. ;;
  127. ;; [2013-07-20 Sa] Version 2.3.0:
  128. ;; - Renamed from "org-favtable" to "org-index"
  129. ;; - Added an assistant to set up the index table
  130. ;; - occur is now incremental, searching as you type
  131. ;; - simplified the documentation and help-system
  132. ;; - Saving keystrokes, as "+g237" is now valid input
  133. ;; - Many bug fixes
  134. ;;
  135. ;; [2013-02-28 Th] Version 2.2.0:
  136. ;; - Allowed shortcuts like "h237" for command "head" with argument "237"
  137. ;; - Integrated with org-mark-ring-goto
  138. ;;
  139. ;; [2013-01-25 Fr] Version 2.1.0:
  140. ;; - Added full support for ids
  141. ;; - New commands "statistics"
  142. ;; - Renamed the package from "org-reftable" to "org-favtable"
  143. ;; - Additional columns are required (e.g. "link"). Error messages will
  144. ;; guide you
  145. ;;
  146. ;; [2012-12-07 Fr] Version 2.0.0:
  147. ;; - The format of the table of favorites has changed ! You need to bring
  148. ;; your existing table into the new format by hand (which however is
  149. ;; easy and explained below)
  150. ;; - Reference table can be sorted after usage count or date of last access
  151. ;; - Ask user explicitly, which command to invoke
  152. ;; - Renamed the package from "org-refer-by-number" to "org-reftable"
  153. ;;
  154. ;; [2012-09-22 Sa] Version 1.5.0:
  155. ;; - New command "sort" to sort a buffer or region by reference number
  156. ;; - New commands "highlight" and "unhighlight" to mark references
  157. ;;
  158. ;; [2012-07-13 Fr] Version 1.4.0:
  159. ;; - New command "head" to find a headline with a reference number
  160. ;;
  161. ;; [2012-04-28 Sa] Version 1.3.0:
  162. ;; - New commands occur and multi-occur
  163. ;; - All commands can now be invoked explicitly
  164. ;; - New documentation
  165. ;; - Many bugfixes
  166. ;;
  167. ;; [2011-12-10 Sa] Version 1.2.0:
  168. ;; - Fixed a bug, which lead to a loss of newly created reference numbers
  169. ;; - Introduced single and double prefix arguments
  170. ;; - Started this Change Log
  171. ;;; Code:
  172. (require 'org-table)
  173. (require 'cl)
  174. (defcustom org-index-id nil
  175. "Id of the Org-mode node, which contains the index table."
  176. :group 'org
  177. :group 'org-index)
  178. ;; Version of this package
  179. (defvar org-index-version "4.0.0" "Version of `org-index', format is major.minor.bugfix, where \"major\" is a change in index-table and \"minor\" are new features.")
  180. ;; Variables to hold the configuration of the index table
  181. (defvar org-index--maxref nil "Maximum number from reference table (e.g. \"153\").")
  182. (defvar org-index--head nil "Any header before number (e.g. \"R\").")
  183. (defvar org-index--tail nil "Tail after number (e.g. \"}\" or \")\".")
  184. (defvar org-index--numcols nil "Number of columns in index table.")
  185. (defvar org-index--ref-regex nil "Regular expression to match a reference.")
  186. (defvar org-index--ref-format nil "Format, that can print a reference.")
  187. (defvar org-index--columns nil "Columns of index-table.")
  188. (defvar org-index--special-columns nil "Columns with flags, that may appear only once.")
  189. (defvar org-index--flagged-columns nil "Columns with flags, that may appear multiple times.")
  190. (defvar org-index--buffer nil "Buffer of index table.")
  191. (defvar org-index--point nil "Position at start of headline of index table.")
  192. (defvar org-index--below-hline nil "Position of first cell in first line below hline.")
  193. (defvar org-index--headings nil "Headlines of index-table as a string.")
  194. (defvar org-index-map nil "Keymap for shortcuts for some commands of `org-index'. Can be activated and filled by org-index-default-keybings.")
  195. ;; Variables to hold context and state
  196. (defvar org-index--text-to-yank nil "Text, that can be yanked after call (mostly a reference).")
  197. (defvar org-index--last-ref nil "Last reference created or visited.")
  198. (defvar org-index--category-before nil "Category of node before.")
  199. (defvar org-index--active-region nil "Active region, initially. I.e. what has been marked.")
  200. (defvar org-index--below-cursor nil "Word below cursor.")
  201. (defvar org-index--within-node nil "True, if we are within node of the index table.")
  202. (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
  203. (defvar org-index--occur-help-text nil "Text for help in occur buffer.")
  204. (defvar org-index--occur-help-overlay nil "Overlay for help in occur buffer.")
  205. (defvar org-index--occur-stack nil "Stack with overlays for hiding lines.")
  206. (defvar org-index--occur-tail-overlay nil "Overlay to cover invisible lines.")
  207. (defvar org-index--last-sort nil "Last column, the index has been sorted after.")
  208. (defvar org-index--sort-timer nil "Timer to sort index in correct order.")
  209. (defvar org-index--aligned nil "Remember for this Emacs session, if table has been aligned at least once.")
  210. ;; static information for this program package
  211. (defconst org-index--commands '(occur add delete head enter ref help example sort multi-occur highlight maintain) "List of commands available.")
  212. (defconst org-index--required-flags '(sort) "Flags that are required.")
  213. (defconst org-index--single-flags '(sort point-on-add yank-after-add get-category-on-add get-heading-on-add shift-ref-and-date-on-add) "Flags, that may only appear once; these can appear as special-columns.")
  214. (defconst org-index--multiple-flags '(edit-on-add) "Flags, that might appear multiple times.")
  215. (defconst org-index--all-flags (append org-index--single-flags org-index--multiple-flags) "All flags.")
  216. (defconst org-index--required-headings '(ref id created last-accessed count) "All required headings.")
  217. (defconst org-index--valid-headings (append org-index--required-headings '(keywords category)) "All valid headings.")
  218. (defconst org-index--occur-buffer-name "*org-index-occur*" "Name of occur buffer.")
  219. (defconst org-index--sort-idle-delay 300 "Delay in seconds after which buffer will sorted.")
  220. (defconst org-index--sample-flags
  221. "
  222. - columns-and-flags :: associate columns of index table with flags. Do not remove.
  223. - ref
  224. - yank-after-add
  225. - category
  226. - get-category-on-add
  227. - edit-on-add
  228. - keywords
  229. - get-heading-on-add
  230. - edit-on-add
  231. - point-on-add
  232. - count
  233. - sort
  234. - last-accessed
  235. - created
  236. - id
  237. - all-columns-explained :: All columns of the index table and their meaning.
  238. - ref :: The reference number; will be generated automatically.
  239. - id :: id of the node, that this line represents
  240. - created :: When has this entry been created ?
  241. - last-accessed :: When has this entry been accessed last ?
  242. - count :: How many times has this entry been picked ?
  243. - keywords :: (optional) Suggested column to keep a list of keywords,
  244. which may match your input during occur.
  245. - category :: (optional) Suggested column to get category of node.
  246. - Any name starting with a dot (`.') :: No predefined meaning,
  247. depends on its flags.
  248. - all-flags-explained :: All flags, that can be associated with columns.
  249. - sort :: Sort whole table according to this column.
  250. - yank-after-add :: This column will be yanked after picking this line during
  251. occur.
  252. - edit-on-add :: This field will be presented for editing, when adding
  253. a new line to your index.
  254. - point-on-add :: Point will land here, when adding a new line, e.g. with
  255. command ref.
  256. - get-category-on-add :: This column will receive the nodes category
  257. during command add.
  258. - get-heading-on-add :: This column will receive the nodes heading
  259. during add.
  260. - shift-ref-and-date-on-add :: Remove leading reference and timestamp on add."
  261. "A sample string of flags.")
  262. (defmacro org-index--on (column value &rest body)
  263. "Execute the forms in BODY with point on index line whose COLUMN is VALUE.
  264. The value returned is the value of the last form in BODY or nil,
  265. if VALUE cannot be found."
  266. (declare (indent 2) (debug t))
  267. (let ((pointvar (make-symbol "point")) ; avoid clash with same-named variables in body
  268. (foundvar (make-symbol "found"))
  269. (retvar (make-symbol "ret")))
  270. `(save-current-buffer
  271. (set-buffer org-index--buffer)
  272. (setq ,pointvar (point))
  273. (setq ,foundvar nil)
  274. (setq ,retvar nil)
  275. (setq ,foundvar (org-index--go ,column ,value))
  276. (when ,foundvar
  277. (setq ,retvar (progn ,@body)))
  278. (goto-char ,pointvar)
  279. ,retvar)))
  280. (defun org-index (&optional command search-ref arg)
  281. "Fast search for selected org headings and things outside of org.
  282. This package creates and updates an index table of headings or
  283. keywords, references and ids, where each line points to a heading
  284. within org or references something outside. This table is sorted by
  285. usage count, so that frequently used lines appear among the first
  286. results.
  287. References are essentially small numbers (e.g. 'R237' or '--455--'), as
  288. created by this package; they are well suited to be used outside of
  289. org, e.g. in folder names, issue trackers or on printed documents.
  290. On first invocation `org-index' will guide you to create a dedicated node
  291. for its index table and its configuration flags.
  292. For basic usage, subcommands 'add' and 'occur' are most important.
  293. This is version 4.0.0 of org-index.el.
  294. \\<org-mode-map>
  295. The function `org-index' operates on a dedicated table, the index
  296. table, which lives within its own Org-mode node. The table and
  297. its containing node will be created, when you first invoke
  298. `org-index'. The node also contains a commented list, describing
  299. the columns of the index table and their associated flags. The
  300. node is found through its id, which is stored within the variable
  301. `org-index-id'.
  302. The function `org-index' is the only interactive function of this
  303. package and its main entry point; it will present you with a list
  304. of subcommands to choose from:
  305. occur: Incremental search, that shows matching lines from the
  306. index table. It is updated after every keystroke. You may
  307. enter a list of words seperated by space or comma (`,'), to
  308. select lines that contain all of the given words.
  309. add: Add the current node to your index, so that it can be
  310. found through the subcommand \"occur\". Update index,
  311. if node has already been present.
  312. delete: Delete the current node from your index.
  313. head: Ask for a reference number and search for this heading.
  314. enter: Enter index table and maybe go to a specific reference;
  315. use `org-mark-ring-goto' (\\[org-mark-ring-goto]) to go back.
  316. ref: Create a new reference.
  317. help: Show this text.
  318. example: Create a temporary index, that will not be saved, but
  319. may serve as an example.
  320. sort: Sort lines in index, in region or buffer by contained
  321. reference, or sort index by count, reference or last access.
  322. multi-occur: Apply Emacs standard `multi-occur' operation on all
  323. `org-mode' buffers to search for the given reference.
  324. highlight: Highlight or unhiglight references in active region or buffer.
  325. Call with prefix argument (`C-u') to remove highlights.
  326. maintain: Offers some choices to check, update or fix your index.
  327. If you invoke `org-index' for the first time, an assistant will be
  328. invoked, that helps you to create your own, commented index.
  329. Use `org-index-default-keybindings' to establish convenient
  330. keyboard shortcuts.
  331. See the commented list of flags within your index node for ways to
  332. modify the behaviour of org-index.
  333. Optional arguments for use from elisp: COMMAND is a symbol naming
  334. the command to execute. SEARCH-REF specifies a reference to
  335. search for, if needed. ARG allows passing in a prefix argument
  336. as in interactive calls."
  337. (interactive "P")
  338. (let (search-id ; id to search for
  339. sort-what ; sort what ?
  340. kill-new-text ; text that will be appended to kill ring
  341. message-text) ; text that will be issued as an explanation
  342. ;;
  343. ;; Initialize and parse
  344. ;;
  345. ;; creates index table, if necessary
  346. (org-index--verify-id)
  347. ;; Get configuration of index table
  348. (org-index--parse-table)
  349. ;; store context information
  350. (org-index--retrieve-context)
  351. ;;
  352. ;; Arrange for proper sorting of index
  353. ;;
  354. ;; lets assume, that it has been sorted this way (we try hard to make sure)
  355. (unless org-index--last-sort (setq org-index--last-sort (org-index--special-column 'sort)))
  356. ;; rearrange for index beeing sorted into default sort order after 300 secs of idle time
  357. (unless org-index--sort-timer
  358. (setq org-index--sort-timer
  359. (run-with-idle-timer org-index--sort-idle-delay t 'org-index--sort-silent)))
  360. ;;
  361. ;; Find out, what we are supposed to do
  362. ;;
  363. (when (equal command '(4))
  364. (setq arg command)
  365. (setq command nil))
  366. (if command
  367. (unless (memq command org-index--commands)
  368. (error "Unknown command '%s' passed as argument, valid choices are any of these symbols: %s"
  369. command (mapconcat 'symbol-name org-index--commands ",")))
  370. (setq command (intern (org-completing-read
  371. "Please choose: "
  372. (mapcar 'symbol-name org-index--commands)))))
  373. ;;
  374. ;; Get search string, if required
  375. ;;
  376. ;; These actions need a search string:
  377. (when (memq command '(enter head multi-occur))
  378. ;; search from surrounding text ?
  379. (unless search-ref
  380. (if org-index--within-node
  381. (if (org-at-table-p)
  382. (setq search-ref (org-index--get-or-set-field 'ref)))
  383. (if (and org-index--below-cursor
  384. (string-match (concat "\\(" org-index--ref-regex "\\)")
  385. org-index--below-cursor))
  386. (setq search-ref (match-string 1 org-index--below-cursor)))))
  387. ;; If we still do not have a search string, ask user explicitly
  388. (unless search-ref
  389. (if (eq command 'enter)
  390. (let ((r (org-index--read-search-for-enter)))
  391. (setq search-ref (car r))
  392. (setq search-id (cdr r)))
  393. (setq search-ref (read-from-minibuffer "Search reference number: "))))
  394. ;; Clean up search string
  395. (when search-ref
  396. (setq search-ref (org-trim search-ref))
  397. (if (string-match "^[0-9]+$" search-ref)
  398. (setq search-ref (concat org-index--head search-ref org-index--tail)))
  399. (if (string= search-ref "") (setq search-ref nil)))
  400. (if (and (not search-ref)
  401. (not (eq command 'enter)))
  402. (error "Command %s needs a reference number" command)))
  403. ;;
  404. ;; Command sort needs to know in advance, what to sort for
  405. ;;
  406. (when (eq command 'sort)
  407. (setq sort-what (intern (org-completing-read "You may sort:\n - index : your index table by various columns\n - region : the active region by contained reference\n - buffer : the whole current buffer\nPlease choose what to sort: " (list "index" "region" "buffer") nil t))))
  408. ;;
  409. ;; Enter table
  410. ;;
  411. ;; These commands will leave user in index table after they are finished
  412. (when (or (memq command '(enter ref maintain))
  413. (and (eq command 'sort)
  414. (eq sort-what 'index)))
  415. ;; Support orgmode-standard of going back (buffer and position)
  416. (org-mark-ring-push)
  417. (pop-to-buffer-same-window org-index--buffer)
  418. (goto-char org-index--point)
  419. (org-index--unfold-buffer))
  420. ;;
  421. ;; Actually do, what is requested
  422. ;;
  423. (cond
  424. ((eq command 'help)
  425. ;; bring up help-buffer for this function
  426. (describe-function 'org-index))
  427. ((eq command 'multi-occur)
  428. ;; Construct list of all org-buffers
  429. (let (buff org-buffers)
  430. (dolist (buff (buffer-list))
  431. (set-buffer buff)
  432. (if (string= major-mode "org-mode")
  433. (setq org-buffers (cons buff org-buffers))))
  434. ;; Do multi-occur
  435. (multi-occur org-buffers (org-index--make-guarded-search search-ref))
  436. ;; Present results
  437. (if (get-buffer "*Occur*")
  438. (progn
  439. (setq message-text (format "multi-occur for '%s'" search-ref))
  440. (other-window 1)
  441. (toggle-truncate-lines 1))
  442. (setq message-text (format "Did not find '%s'" search-ref)))))
  443. ((eq command 'add)
  444. (let ((r (org-index--do-add-or-update)))
  445. (setq message-text (car r))
  446. (setq kill-new-text (cdr r))))
  447. ((eq command 'delete)
  448. (setq message-text (org-index--do-delete)))
  449. ((eq command 'head)
  450. (if (and org-index--within-node
  451. (org-at-table-p))
  452. (setq search-id (org-index--get-or-set-field 'id)))
  453. (setq message-text
  454. (if search-id
  455. (org-index--do-head search-ref search-id)
  456. (message "Current line has no id."))))
  457. ((eq command 'enter)
  458. (goto-char org-index--below-hline)
  459. (setq message-text
  460. (if search-ref
  461. (if (org-index--go 'ref search-ref)
  462. (progn
  463. (org-index--update-current-line)
  464. (org-table-goto-column (org-index--column-num 'ref))
  465. (format "Found index line '%s'" search-ref))
  466. (format "Did not find index line with reference '%s'" search-ref))
  467. (if search-id
  468. (if (org-index--go 'id search-id)
  469. (progn
  470. (org-index--update-current-line)
  471. (org-table-goto-column (org-index--column-num 'ref))
  472. (format "Found index line '%s'" (org-index--get-or-set-field 'ref)))
  473. (format "Did not find index line with id '%s'" search-id))
  474. ;; simply go into table
  475. (setq message-text "At index table"))))
  476. (recenter))
  477. ((eq command 'occur)
  478. (set-buffer org-index--buffer)
  479. (org-index--do-occur))
  480. ((eq command 'ref)
  481. (let (new)
  482. ;; add a new row
  483. (setq new (org-index--create-new-line))
  484. ;; fill special columns with standard values
  485. (org-table-goto-column (org-index--column-num 'ref))
  486. (insert new)
  487. (setq org-index--last-ref new)
  488. ;; goto point-field or first empty one or first field
  489. (if (org-index--special-column 'point-on-add)
  490. (org-table-goto-column (org-index--column-num (org-index--special-column 'point-on-add)))
  491. (unless (catch 'empty
  492. (dotimes (col org-index--numcols)
  493. (org-table-goto-column (+ col 1))
  494. (if (string= (org-trim (org-table-get-field)) "")
  495. (throw 'empty t))))
  496. ;; none found, goto first
  497. (org-table-goto-column 1)))
  498. (if org-index--active-region (setq kill-new-text org-index--active-region))
  499. (setq message-text (format "Adding a new row with ref '%s'" new))))
  500. ((eq command 'sort)
  501. (let ((columns (list "ref" "count" "created" "last-accessed" "id"))
  502. sort groups-and-counts)
  503. (cond
  504. ((eq sort-what 'index)
  505. (setq sort
  506. (intern
  507. (org-icompleting-read
  508. "Please choose column to sort index table: "
  509. (append (copy-list columns) (list "group-by"))
  510. nil t nil nil (symbol-name (org-index--special-column 'sort)))))
  511. (when (eq sort 'group-by)
  512. (setq sort
  513. (intern
  514. (org-icompleting-read
  515. "Please choose column to group index table by: "
  516. columns
  517. nil t nil nil (symbol-name (org-index--special-column 'sort)))))
  518. (setq groups-and-counts (org-index--collect-sort-groups sort)))
  519. (org-index--do-sort-index sort (first groups-and-counts))
  520. (org-table-goto-column (org-index--column-num sort))
  521. ;; When saving index, it should again be sorted correctly
  522. (with-current-buffer org-index--buffer
  523. (add-hook 'before-save-hook 'org-index--sort-silent t))
  524. (setq message-text
  525. (format
  526. (concat "Your index has been sorted temporarily by %s and will be sorted again by %s after %d seconds of idle time"
  527. (if groups-and-counts
  528. "; %d groups with equal %s and a total of %d lines have been found"
  529. ""))
  530. (symbol-name sort)
  531. (org-index--special-column 'sort)
  532. org-index--sort-idle-delay
  533. (second groups-and-counts)
  534. (symbol-name sort)
  535. (third groups-and-counts))))
  536. ((memq sort-what '(region buffer))
  537. (org-index--do-sort-lines sort-what)
  538. (setq message-text (format "Sorted %s by contained references" sort-what))))))
  539. ((eq command 'highlight)
  540. (let ((where "buffer"))
  541. (save-excursion
  542. (save-restriction
  543. (when (and transient-mark-mode
  544. mark-active)
  545. (narrow-to-region (region-beginning) (region-end))
  546. (setq where "region"))
  547. (if arg
  548. (progn
  549. (unhighlight-regexp org-index--ref-regex)
  550. (setq message-text (format "Removed highlights for references in %s" where)))
  551. (highlight-regexp org-index--ref-regex 'isearch)
  552. (setq message-text (format "Highlighted references in %s" where)))))))
  553. ((eq command 'maintain)
  554. (setq message-text (org-index--do-maintain)))
  555. ((eq command 'example)
  556. (if (y-or-n-p "This assistant will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
  557. (org-index--create-index t)))
  558. (t (error "Unknown subcommand '%s'" command)))
  559. ;; tell, what we have done and what can be yanked
  560. (if kill-new-text (setq kill-new-text
  561. (substring-no-properties kill-new-text)))
  562. (if (string= kill-new-text "") (setq kill-new-text nil))
  563. (let ((m (concat
  564. message-text
  565. (if (and message-text kill-new-text)
  566. " and r"
  567. (if kill-new-text "R" ""))
  568. (if kill-new-text (format "eady to yank '%s'." kill-new-text) (if message-text "." "")))))
  569. (unless (string= m "")
  570. (message m)
  571. (setq org-index--message-text m)))
  572. (if kill-new-text (kill-new kill-new-text))))
  573. (defun org-index-default-keybindings ()
  574. "Set default keybindings for `org-index'.
  575. Establish the common prefix key `C-c i' Which is followed by the
  576. first letter of a subcommand, so that `C-c i a' invokes the
  577. subcommand \"add\". Subcommands available are occur, add, delete,
  578. head, enter and ref. Special cases: `C-c i ?' shows help and
  579. `C-c i i' invokes `org-index' to let you choose."
  580. (interactive)
  581. (define-prefix-command 'org-index-map)
  582. (global-set-key (kbd "C-c i") 'org-index-map)
  583. (define-key org-index-map (kbd "i") (lambda (arg) (interactive "P") (message nil) (org-index nil nil arg)))
  584. (define-key org-index-map (kbd "o") (lambda (arg) (interactive "P") (message nil) (org-index 'occur nil arg)))
  585. (define-key org-index-map (kbd "a") (lambda (arg) (interactive "P") (message nil) (org-index 'add nil arg)))
  586. (define-key org-index-map (kbd "d") (lambda (arg) (interactive "P") (message nil) (org-index 'delete nil arg)))
  587. (define-key org-index-map (kbd "h") (lambda (arg) (interactive "P") (message nil) (org-index 'head nil arg)))
  588. (define-key org-index-map (kbd "e") (lambda (arg) (interactive "P") (message nil) (org-index 'enter nil arg)))
  589. (define-key org-index-map (kbd "r") (lambda (arg) (interactive "P") (message nil) (org-index 'ref nil arg)))
  590. (define-key org-index-map (kbd "?") (lambda (arg) (interactive "P") (message nil) (org-index 'help nil arg))))
  591. (defun org-index-new-line (&rest keys-values)
  592. "Create a new line within the index table, returning its reference.
  593. The function takes a varying number of argument pairs; each pair
  594. is a symbol for an existing column heading followed by its value.
  595. The return value is the new reference.
  596. Example:
  597. (message \"Created reference %s\"
  598. (org-index-new-line 'keywords \"foo bar\" 'category \"baz\"))
  599. Optional argument KEYS-VALUES specifies content of new line."
  600. (org-index--verify-id)
  601. (org-index--parse-table)
  602. (car (apply 'org-index--do-new-line keys-values)))
  603. (defun org-index--do-new-line (&rest keys-values)
  604. "Do the work for `org-index-new-line'.
  605. Optional argument KEYS-VALUES specifies content of new line."
  606. (save-excursion
  607. (org-index--retrieve-context)
  608. (with-current-buffer org-index--buffer
  609. (goto-char org-index--point)
  610. ;; check arguments early; they might come from lisp-user
  611. (let ((kvs keys-values)
  612. k v)
  613. (while kvs
  614. (setq k (car kvs))
  615. (setq v (cadr kvs))
  616. (if (eq k 'ref)
  617. (unless (memq v '(t nil))
  618. (error "Column 'ref' accepts only \"t\" or \"nil\""))
  619. (if (or (not (symbolp k))
  620. (and (symbolp v) (not (eq v t)) (not (eq v nil))))
  621. (error "Arguments must be alternation of key and value")))
  622. (unless (org-index--column-num k)
  623. (error "Unknown column or column not defined in table: '%s'" (symbol-name k)))
  624. (setq kvs (cddr kvs))))
  625. (let (ref yank)
  626. ;; create new line
  627. (setq ref (org-index--create-new-line))
  628. (plist-put keys-values 'ref ref)
  629. ;; fill columns
  630. (let ((kvs keys-values)
  631. k v n)
  632. (while kvs
  633. (setq k (car kvs))
  634. (setq v (cadr kvs))
  635. (org-table-goto-column (org-index--column-num k))
  636. (insert (org-trim v))
  637. (setq kvs (cddr kvs))))
  638. ;; align table and fontify line
  639. (org-table-align)
  640. (setq org-table--aligned t)
  641. (font-lock-fontify-region (line-beginning-position) (line-end-position))
  642. ;; get column to yank
  643. (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add)))
  644. (cons ref yank)))))
  645. (defun org-index-get-line (column value)
  646. "Retrieve an existing line within the index table by ref or id.
  647. Return its contents as a property list.
  648. The function `plist-get' may be used to retrieve specific elements
  649. from the result.
  650. Example:
  651. (plist-get (org-index-get-line 'ref \"R12\") 'count)
  652. retrieves the value of the count-column for reference number 12.
  653. Argument COLUMN is a symbol, either ref or id,
  654. argument VALUE specifies the value to search for."
  655. ;; check arguments
  656. (unless (memq column '(ref id))
  657. (error "Argument column can only be 'ref' or 'id'"))
  658. (unless value
  659. (error "Need a value to search for"))
  660. (org-index--verify-id)
  661. (org-index--parse-table)
  662. (org-index--get-line column value))
  663. (defun org-index--get-line (column value)
  664. "Find a line by ID, return its contents.
  665. Argument COLUMN and VALUE specify line to get."
  666. (let (content)
  667. (org-index--on
  668. column value
  669. (mapc (lambda (x)
  670. (if (and (numberp (cdr x))
  671. (> (cdr x) 0))
  672. (setq content (cons (car x) (cons (or (org-index--get-or-set-field (car x)) "") content)))))
  673. (reverse org-index--columns)))
  674. content))
  675. (defun org-index--delete-line (id)
  676. "Delete a line specified by ID."
  677. (let (content)
  678. (org-index--on
  679. 'id id
  680. (let ((start (line-beginning-position)))
  681. (beginning-of-line)
  682. (forward-line)
  683. (delete-region start (point))
  684. t))))
  685. (defun org-index--ref-from-id (id)
  686. "Get reference from line ID."
  687. (org-index--on 'id id (org-index--get-or-set-field 'ref)))
  688. (defun org-index--id-from-ref (ref)
  689. "Get id from line REF."
  690. (org-index--on 'ref ref (org-index--get-or-set-field 'id)))
  691. (defun org-index--read-search-for-enter ()
  692. "Special input routine for command enter."
  693. ;; Accept single char commands or switch to reading a sequence of digits
  694. (let (char prompt search-ref search-id)
  695. ;; start with short prompt but give more help on next iteration
  696. (setq prompt "Please specify, where to go in index (0-9.,space,backspace,return or ? for help): ")
  697. ;; read one character
  698. (while (not (memq char (append (number-sequence ?0 ?9) (list ?\d ?\b ?\r ?\j ?\s ?.))))
  699. (setq char (read-char prompt))
  700. (setq prompt "Go to index table and specific position. Digits specify a reference number to got to, <space> goes to top of index, <backspace> or <delete> to last line created and <return> or `.' to index line of current node. Please choose: "))
  701. (if (memq char (number-sequence ?0 ?9))
  702. ;; read rest of digits
  703. (setq search-ref (read-from-minibuffer "Search reference number: " (char-to-string char))))
  704. ;; decode single chars
  705. (if (memq char '(?\r ?\n ?.)) (setq search-id (org-id-get)))
  706. (if (memq char '(?\d ?\b)) (setq search-ref (number-to-string org-index--maxref)))
  707. (cons search-ref search-id)))
  708. (defun org-index--verify-id ()
  709. "Check, that we have a valid id."
  710. ;; Check id
  711. (unless org-index-id
  712. (let ((answer (org-completing-read "Cannot find an index (org-index-id is not set). You may:\n - read-help : to learn more about org-index\n - create-index : invoke an assistant to create an initial index\nPlease choose: " (list "read-help" "create-index") nil t nil nil "read-help")))
  713. (if (string= "create-index" answer)
  714. (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet.")
  715. (describe-function 'org-index))))
  716. ;; Find node
  717. (let (marker)
  718. (setq marker (org-id-find org-index-id 'marker))
  719. (unless marker (org-index--create-missing-index "Cannot find the node with id \"%s\" (as specified by variable org-index-id)." org-index-id))
  720. ; Try again with new node
  721. (setq marker (org-id-find org-index-id 'marker))
  722. (unless marker (error "Could not create node"))
  723. (setq org-index--buffer (marker-buffer marker)
  724. org-index--point (marker-position marker))
  725. (move-marker marker nil)))
  726. (defun org-index--retrieve-context ()
  727. "Collect context information before starting with command."
  728. ;; Get the content of the active region or the word under cursor
  729. (setq org-index--active-region
  730. (if (and transient-mark-mode mark-active)
  731. (buffer-substring (region-beginning) (region-end))
  732. nil))
  733. (setq org-index--below-cursor (thing-at-point 'symbol))
  734. ;; get category of current node
  735. (setq org-index--category-before
  736. (save-excursion ; workaround: org-get-category does not give category when at end of buffer
  737. (beginning-of-line)
  738. (org-get-category)))
  739. ;; Find out, if we are within index table or not
  740. (setq org-index--within-node (string= (org-id-get) org-index-id)))
  741. (defun org-index--parse-table ()
  742. "Parse content of index table."
  743. (let (ref-field
  744. id-field
  745. initial-point
  746. end-of-headings
  747. start-of-headings)
  748. (with-current-buffer org-index--buffer
  749. (setq org-index--maxref 0)
  750. (setq initial-point (point))
  751. (org-index--go-below-hline)
  752. ;; align and fontify table once for this emacs session
  753. (unless org-index--aligned
  754. (org-table-align) ; needs to happen before fontification to be effective ?
  755. (let ((is-modified (buffer-modified-p))
  756. (below (point)))
  757. (while (org-at-table-p)
  758. (forward-line))
  759. (font-lock-fontify-region below (point))
  760. (org-index--go-below-hline)
  761. (setq org-index--aligned t)
  762. (set-buffer-modified-p is-modified)))
  763. (org-index--go-below-hline)
  764. (setq org-index--below-hline (point-marker))
  765. (beginning-of-line)
  766. ;; get headings to display during occur
  767. (setq end-of-headings (point))
  768. (while (org-at-table-p) (forward-line -1))
  769. (forward-line)
  770. (setq start-of-headings (point))
  771. (setq org-index--headings (buffer-substring start-of-headings end-of-headings))
  772. ;; count columns
  773. (org-table-goto-column 100)
  774. (setq org-index--numcols (- (org-table-current-column) 1))
  775. ;; go to top of table
  776. (while (org-at-table-p)
  777. (forward-line -1))
  778. (forward-line)
  779. ;; parse line of headings
  780. (org-index--parse-headings)
  781. ;; parse list of flags
  782. (goto-char org-index--point)
  783. (org-index--parse-flags)
  784. ;; Retrieve any decorations around the number within the first nonempty ref-field
  785. (goto-char org-index--below-hline)
  786. (while (and (org-at-table-p)
  787. (not (setq ref-field (org-index--get-or-set-field 'ref))))
  788. (forward-line))
  789. ;; Some Checking
  790. (unless ref-field
  791. (org-index--report-index-error "Reference column is empty"))
  792. (unless (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\([^0-9]*\\)$" ref-field)
  793. (org-index--report-index-error
  794. "First reference in index table ('%s') does not contain a number" ref-field))
  795. ;; These are the decorations used within the first ref of index
  796. (setq org-index--head (match-string 1 ref-field))
  797. (setq org-index--tail (match-string 3 ref-field))
  798. (setq org-index--ref-regex (concat (regexp-quote org-index--head)
  799. "\\([0-9]+\\)"
  800. (regexp-quote org-index--tail)))
  801. (setq org-index--ref-format (concat org-index--head "%d" org-index--tail))
  802. ;; Go through table to find maximum number and do some checking
  803. (let ((ref 0))
  804. (while (org-at-table-p)
  805. (setq ref-field (org-index--get-or-set-field 'ref))
  806. (setq id-field (org-index--get-or-set-field 'id))
  807. (when (and (not ref-field)
  808. (not id-field))
  809. (kill-whole-line)
  810. (message "Removing line from index-table with both ref and id empty"))
  811. (if ref-field
  812. (if (string-match org-index--ref-regex ref-field)
  813. ;; grab number
  814. (setq ref (string-to-number (match-string 1 ref-field)))
  815. (kill-whole-line)
  816. (message "Removing line from index-table whose ref does not contain a number")))
  817. ;; check, if higher ref
  818. (if (> ref org-index--maxref) (setq org-index--maxref ref))
  819. (forward-line 1)))
  820. ;; go back to initial position
  821. (goto-char initial-point))))
  822. (defun org-index--do-maintain ()
  823. "Choose among and perform some tasks to maintain index."
  824. (let ((check-what) (max-mini-window-height 1.0) message-text)
  825. (setq check-what (intern (org-completing-read "These checks and fixes are available:\n - statistics : compute statistics about index table\n - check : check ids by visiting their nodes\n - duplicates : check index for duplicate rows (any column)\n - clean : remove obsolete property org-index-id\n - update : update content of index lines, with an id \nPlease choose: " (list "statistics" "check" "duplicates" "clean" "update") nil t nil nil "statistics")))
  826. (message nil)
  827. (cond
  828. ((eq check-what 'check)
  829. (setq message-text (or (org-index--check-ids)
  830. "No problems found")))
  831. ((eq check-what 'statistics)
  832. (setq message-text (org-index--do-statistics)))
  833. ((eq check-what 'duplicates)
  834. (setq message-text "Finding duplcates can be done by sorting your index appropriately: Choose 'group-by' and select a column; rows will then be sorted together, if they have the same value within the coosen column."))
  835. ((eq check-what 'clean)
  836. (let ((lines 0))
  837. (org-map-entries
  838. (lambda ()
  839. (when (org-entry-get (point) "org-index-ref")
  840. (incf lines)
  841. (org-entry-delete (point) "org-index-ref")))
  842. nil 'agenda)
  843. (setq message-text (format "Removed property 'org-index-ref' from %d lines" lines))))
  844. ((eq check-what 'update)
  845. (if (and (y-or-n-p "Updating your index will overwrite certain columns with content from the associated heading and category. If unsure, you may try this for a single, already existing line of your index by doing `add' from within your index. Are you sure to proceed for ALL index lines ? ")
  846. (or (not (or (org-index--flag-p 'edit-on-add (org-index--special-column 'get-heading-on-add))
  847. (org-index--flag-p 'edit-on-add (org-index--special-column 'get-category-on-add))))
  848. (y-or-n-p "If you did any editing of keyowrds or category while adding lines to your index before, these edits will now get lost. Do you still want to proceed ? ")))
  849. (setq message-text (org-index--update-all-lines))
  850. (setq message-text "Canceled."))))
  851. message-text))
  852. (defun org-index--do-sort-index (sort &optional groups)
  853. "Sort index table according to SORT, optinally with GROUPS."
  854. (let ((is-modified (buffer-modified-p))
  855. top
  856. bottom
  857. ref-field
  858. count-field)
  859. (unless buffer-read-only
  860. (message "Sorting table for %s..." (symbol-name sort))
  861. (undo-boundary)
  862. (let ((message-log-max nil)) ; we have just issued a message, dont need those of sort-subr
  863. ;; get boundaries of table
  864. (goto-char org-index--below-hline)
  865. (forward-line 0)
  866. (setq top (point))
  867. (while (org-at-table-p) (forward-line))
  868. ;; kill all empty rows at bottom
  869. (while (progn
  870. (forward-line -1)
  871. (org-table-goto-column 1)
  872. (and
  873. (not (org-index--get-or-set-field 'ref))
  874. (not (org-index--get-or-set-field 'id))))
  875. (org-table-kill-row))
  876. (forward-line 1)
  877. (setq bottom (point))
  878. ;; sort lines
  879. (save-restriction
  880. (narrow-to-region top bottom)
  881. (goto-char top)
  882. (sort-subr t
  883. 'forward-line
  884. 'end-of-line
  885. (lambda ()
  886. (concat
  887. (if groups
  888. (format "%06d-" (cdr (assoc (org-index--get-or-set-field sort) groups)))
  889. "")
  890. (org-index--get-sort-key sort t)))
  891. nil
  892. 'string<)
  893. (goto-char (point-min))
  894. ;; restore modification state
  895. (set-buffer-modified-p is-modified)))
  896. (setq org-index--last-sort sort))))
  897. (defun org-index--collect-sort-groups (sort)
  898. "Collect groups to SORT for."
  899. (let ((count-groups 0) (count-lines 0)
  900. groups key key-value)
  901. (org-index--on
  902. nil nil
  903. (while (org-at-table-p)
  904. (setq key (org-index--get-or-set-field sort))
  905. (setq key-value (assoc key groups))
  906. (if key-value
  907. (progn
  908. (incf (cdr key-value)))
  909. (setq groups (cons (cons key 1) groups)))
  910. (forward-line)))
  911. (mapc (lambda (x) (when (> (cdr x) 1)
  912. (incf count-groups)
  913. (incf count-lines (cdr x))))
  914. groups)
  915. (list groups count-groups count-lines)))
  916. (defun org-index--do-sort-lines (what)
  917. "Sort lines in WHAT according to contained reference."
  918. (save-restriction
  919. (cond
  920. ((eq what 'region)
  921. (if (region-active-p)
  922. (narrow-to-region (region-beginning) (region-end))
  923. (error "No active region, cannot sort")))
  924. ((eq what 'buffer)
  925. (unless (y-or-n-p "Sort whole current buffer ? ")
  926. (error "Canceled"))
  927. (narrow-to-region (point-min) (point-max))))
  928. (goto-char (point-min))
  929. (sort-subr nil 'forward-line 'end-of-line
  930. (lambda ()
  931. (if (looking-at (concat ".*"
  932. (org-index--make-guarded-search org-index--ref-regex 'dont-quote)))
  933. (string-to-number (match-string 1))
  934. 0)))))
  935. (defun org-index--go-below-hline ()
  936. "Move below hline in index-table."
  937. (let ((count 0)
  938. (errstring (format "index table within node %s" org-index-id)))
  939. (goto-char org-index--point)
  940. ;; go to heading of node
  941. (while (not (org-at-heading-p)) (forward-line -1))
  942. (forward-line 1)
  943. ;; go to first table, but make sure we do not get into another node
  944. (while (and (not (org-at-table-p))
  945. (not (org-at-heading-p))
  946. (not (eobp)))
  947. (forward-line))
  948. ;; check, if there really is a table
  949. (unless (org-at-table-p)
  950. (org-index--create-missing-index "Cannot find %s." errstring))
  951. ;; go just after hline
  952. (while (and (not (org-at-table-hline-p))
  953. (org-at-table-p))
  954. (forward-line))
  955. (forward-line)
  956. ;; and check
  957. (unless (org-at-table-p)
  958. (org-index--report-index-error "Cannot find a hline within %s" errstring))
  959. (org-table-goto-column 1)))
  960. (defun org-index--parse-headings ()
  961. "Parse headings of index table."
  962. (let (field ;; field content
  963. field-symbol ;; and as a symbol
  964. found)
  965. (setq org-index--columns nil)
  966. ;; For each column
  967. (dotimes (col org-index--numcols)
  968. (setq field (substring-no-properties (downcase (org-trim (org-table-get-field (+ col 1))))))
  969. (if (string= field "")
  970. (error "Heading of column cannot be empty"))
  971. (if (and (not (string= (substring field 0 1) "."))
  972. (not (member (intern field) org-index--valid-headings)))
  973. (if (string= field "link")
  974. ;; Ask user to migrate his index to new version (since [2015-02-11 Mi])
  975. (progn
  976. ;; pop to index buffer
  977. (pop-to-buffer-same-window org-index--buffer)
  978. (goto-char org-index--below-hline)
  979. (org-reveal t)
  980. ;; go to column
  981. (while (org-at-table-p)
  982. (forward-line -1))
  983. (forward-line)
  984. (org-table-goto-column (+ 1 col))
  985. (error "Column 'link' should be named 'id' with recent versions of org-index,\nplease adjust your table (cursor is already positioned right)"))
  986. (error "Column name '%s' is not a valid heading (custom headings may start with a dot, e.g. '.foo')" field)))
  987. (setq field-symbol (intern field))
  988. ;; check if heading has already appeared
  989. (if (assoc field-symbol org-index--columns)
  990. (org-index--report-index-error
  991. "'%s' appears two times as column heading" (downcase field))
  992. ;; add it to list at front, reverse later
  993. (setq org-index--columns (cons (cons field-symbol (+ col 1)) org-index--columns)))))
  994. (setq org-index--columns (reverse org-index--columns))
  995. ;; check if all necessary headings have appeared
  996. (mapc (lambda (head)
  997. (unless (cdr (assoc head org-index--columns))
  998. (org-index--report-index-error "No column has heading '%s'" head)))
  999. org-index--required-headings))
  1000. (defun org-index--parse-flags ()
  1001. "Parse list of flags in index table."
  1002. (let (parent parent-is-comment child)
  1003. ;; reset configuration variables
  1004. (setq org-index--special-columns nil)
  1005. (setq org-index--flagged-columns nil)
  1006. (org-index--goto-list "columns-and-flags" t)
  1007. (forward-line 1)
  1008. ;; outer loop over columns
  1009. (while (and (setq parent (org-index--parse-list-item))
  1010. parent
  1011. (> (cdr (assoc :indent parent)) 0))
  1012. (setq parent-is-comment (member (cdr (assoc :text parent)) '("all-columns-explained" "all-flags-explained")))
  1013. ;; check, that we have a valid heading
  1014. (unless (or parent-is-comment
  1015. (assoc (cdr (assoc :sym parent)) org-index--columns))
  1016. (when (string= "link" (cdr (assoc :text parent)))
  1017. (pop-to-buffer-same-window org-index--buffer)
  1018. (org-reveal t)
  1019. (error "Flag 'link' should be named 'id' with recent versions of org-index,\nplease adjust this flag (cursor is already positioned right)"))
  1020. (org-index--report-index-error "'%s' appears within flags, but not as a index column. " (cdr (assoc :text parent))))
  1021. ;; inner loop over children
  1022. (while (and (forward-line 1)
  1023. (setq child (org-index--parse-list-item))
  1024. child
  1025. (> (cdr (assoc :indent child))
  1026. (cdr (assoc :indent parent))))
  1027. (unless parent-is-comment
  1028. ;; check, that we have a valid flag
  1029. (unless (memq (cdr (assoc :sym child)) org-index--all-flags)
  1030. (org-index--report-index-error "'%s' is not a valid flag" (cdr (assoc :text child))))
  1031. ;; process flag with respect to current index-column
  1032. (if (memq (cdr (assoc :sym child)) org-index--single-flags)
  1033. ;; Check, that none of org-index--single-flags appears twice
  1034. (if (assoc (cdr (assoc :sym child)) org-index--special-columns)
  1035. (org-index--report-index-error
  1036. "More than one column is marked with flag '%s'" (cdr (assoc :text child)))
  1037. ;; add it to list
  1038. (setq org-index--special-columns (cons (cons (cdr (assoc :sym child)) (cdr (assoc :sym parent)))
  1039. org-index--special-columns))))
  1040. ;; all flags are stored in org-index--flagged-columns
  1041. (let ((l (assoc (cdr (assoc :sym child)) org-index--flagged-columns))) ;; list of flag and columns, that carry this flag
  1042. (unless l
  1043. ;; no list of columns with this flag is present, create one
  1044. (setq org-index--flagged-columns
  1045. (cons (cons (cdr (assoc :sym child)) nil)
  1046. org-index--flagged-columns))
  1047. (setq l (car org-index--flagged-columns)))
  1048. ;; prepend this column to list of columns with this flag
  1049. (setcdr l (cons (cdr (assoc :sym parent)) (cdr l)))))))
  1050. ;; check, that all needed flags have been specified
  1051. (mapc (lambda (x)
  1052. (unless (assoc x org-index--special-columns)
  1053. (org-index--report-index-error "Required flag '%s' does not appear" (substring (symbol-name x) 1))))
  1054. org-index--required-flags)))
  1055. (defun org-index--goto-list (name &optional required non-top)
  1056. "Goto list NAME (maybe NON-TOP Level) in index node, err if REQUIRED list is not present."
  1057. (goto-char org-index--point)
  1058. ;; go to heading of node
  1059. (while (not (org-at-heading-p)) (forward-line -1))
  1060. (forward-line 1)
  1061. ;; go to named list
  1062. (while (and (not (let ((item (org-index--parse-list-item)))
  1063. (if item
  1064. (and (or non-top (= (cdr (assoc :indent item)) 0)) ;; accept only toplevel ?
  1065. (string= (cdr (assoc :text item)) name)) ;; with requested name
  1066. nil)))
  1067. (not (org-at-table-p))
  1068. (not (org-at-heading-p))
  1069. (not (eobp)))
  1070. (forward-line 1))
  1071. (if (org-at-item-p)
  1072. t
  1073. (if required
  1074. (org-index--report-index-error "Could not find required list '%s'" name)
  1075. nil)))
  1076. (defun org-index--parse-list-item ()
  1077. "Parse a list item into an assoc array (indent, checkbox, text, value)."
  1078. ;; matche full list-item, maybe with checkbox and double-colon
  1079. (if (looking-at org-list-full-item-re)
  1080. ;; retrieve interesting parts of list item from match data
  1081. (let (indent checkbox text value next-line)
  1082. (setq indent
  1083. (- (save-excursion (goto-char (match-beginning 1)) (current-column)) ; first column
  1084. (save-match-data (org-current-level)) ; indent-level
  1085. 1))
  1086. (setq checkbox (match-string 3))
  1087. (setq text (match-string 4))
  1088. (set (if text 'value 'text) (buffer-substring (match-end 0) (line-end-position))) ; regexp did not capture this
  1089. ;; peek ahead, if item continues on next line
  1090. (forward-line 1)
  1091. (if (looking-at org-list-full-item-re)
  1092. (forward-line -1) ; already at next item; go back
  1093. (setq next-line (buffer-substring (line-beginning-position) (line-end-position))))
  1094. ;; clean up strings
  1095. (mapc (lambda (x)
  1096. (if (stringp (symbol-value x))
  1097. (set x (org-trim (substring-no-properties (symbol-value x))))))
  1098. '(text value next-line))
  1099. (if next-line (setq text (concat text " " next-line))) ; append next line if
  1100. (list (cons :indent indent) (cons :text text) (cons :value value) (cons :sym (intern text))))
  1101. nil))
  1102. (defun org-index--create-missing-index (&rest reasons)
  1103. "Create a new empty index table with detailed explanation. Argument REASONS explains why."
  1104. (org-index--ask-before-create-index "Cannot find your index table: "
  1105. "new permanent" "."
  1106. reasons)
  1107. (org-index--create-index))
  1108. (defun org-index--report-index-error (&rest reasons)
  1109. "Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with."
  1110. (when org-index--buffer
  1111. (pop-to-buffer-same-window org-index--buffer)
  1112. (goto-char org-index--below-hline)
  1113. (org-reveal t))
  1114. (org-index--ask-before-create-index "The existing index contains this error: "
  1115. "temporary" ", to compare with."
  1116. reasons)
  1117. (org-index--create-index t t))
  1118. (defun org-index--ask-before-create-index (explanation type for-what reasons)
  1119. ; checkdoc-params: (explanation type for-what reasons)
  1120. "Ask the user before creating an index or throw error. Arguments specify bits of issued message."
  1121. (let (reason prompt)
  1122. (setq reason (apply 'format reasons))
  1123. (setq prompt (concat explanation reason "\n\n"
  1124. "However, this assistant can help you to create a "
  1125. type " index with detailed comments" for-what "\n\n"
  1126. "Do you want to proceed ?"))
  1127. (unless (let ((max-mini-window-height 1.0))
  1128. (y-or-n-p prompt))
  1129. (error (concat explanation reason)))))
  1130. (defun org-index--create-index (&optional temporary compare)
  1131. "Create a new empty index table with detailed explanation.
  1132. specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index."
  1133. (let (buffer
  1134. title
  1135. firstref
  1136. id)
  1137. (if temporary
  1138. (let ((file-name (concat temporary-file-directory "org-index--example-index.org"))
  1139. (buffer-name "*org-index-example-index*"))
  1140. (setq buffer (get-buffer-create buffer-name))
  1141. (with-current-buffer buffer
  1142. ;; but it needs a file for its index to be found
  1143. (unless (string= (buffer-file-name) file-name)
  1144. (set-visited-file-name file-name))
  1145. (rename-buffer buffer-name) ; name is change by line above
  1146. (erase-buffer)
  1147. (org-mode)))
  1148. (setq buffer (get-buffer (org-completing-read "Please choose the buffer, where the new node for the index table should be created; the new node will be inserted at its end.\n\nBuffer: " (mapcar 'buffer-name (org-buffer-list))))))
  1149. (setq title (read-from-minibuffer "Please enter the title of the index node: "))
  1150. (while (progn
  1151. (setq firstref (read-from-minibuffer "Please enter your first reference-number. This is a number preceeded by some non-digit chars and optionally followed by some more non-digit chars, e.g. 'R1', '-1-' or '#1#' (and your initial number does not need to be '1'). The format of your reference-numbers only needs to make sense for yourself, so that you can spot it easily in your texts or write it on a piece of paper; it should however not already appear to frequently within your existing notes, to avoid too many false hits when searching.\n\nPlease choose: "))
  1152. (let (desc)
  1153. (when (string-match "[[:blank:]]" firstref)
  1154. (setq desc "Contains whitespace"))
  1155. (when (string-match "[[:cntrl:]]" firstref)
  1156. (setq desc "Contains control characters"))
  1157. (unless (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)
  1158. ;; firstref not okay, report details
  1159. (setq desc
  1160. (cond ((string= firstref "") "is empty")
  1161. ((not (string-match "^[^0-9]+" firstref)) "starts with a digit")
  1162. ((not (string-match "^[^0-9]+[0-9]+" firstref)) "does not contain a number")
  1163. ((not (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)) "contains more than one sequence of digits")
  1164. )))
  1165. (if desc
  1166. (progn
  1167. (read-from-minibuffer (format "Your input '%s' does not meet the requirements because it %s.\nPlease hit RET and try again: " firstref desc))
  1168. t)
  1169. nil))))
  1170. (with-current-buffer buffer
  1171. (goto-char (point-max))
  1172. (insert (format "* %s %s\n" firstref title))
  1173. (if temporary
  1174. (insert "
  1175. Below you find your temporary index table, which will not last longer
  1176. than your current emacs session.
  1177. ")
  1178. (insert "
  1179. Below you find your initial index table, which will grow over time.
  1180. "))
  1181. (insert "
  1182. You may start using it by adding some lines. Just move to
  1183. another heading, invoke `org-index' and choose the command
  1184. \"add\". After adding a few nodes, try the command \"occur\"
  1185. to search among them.
  1186. To gain further insight you may invoke the subcommand \"help\", or
  1187. read the description of `org-index'.
  1188. Within the index table below, dhe sequence of columns does not
  1189. matter. You may reorder them any way you like. Columns are
  1190. found by their heading. You may also add your own columns.
  1191. Following these explanations there is the item-list
  1192. `columns-and-flags', which influences the behaviour of
  1193. `org-index'. See the explanations which are part of this list.
  1194. This node needs not be a top level node; its name is completely
  1195. at your choice; it is found through its ID only.
  1196. Remark: These lines of explanation can be removed at any time.
  1197. ")
  1198. (setq id (org-id-get-create))
  1199. (insert (format "
  1200. %s
  1201. | ref | category | keywords | count | last-accessed | created | id |
  1202. | | | | | | | <4> |
  1203. |-----+-----------+----------+-------+---------------+---------+------|
  1204. | %s | | %s | | | %s | %s |
  1205. "
  1206. org-index--sample-flags
  1207. firstref
  1208. "This node"
  1209. (with-temp-buffer (org-insert-time-stamp nil nil t))
  1210. id))
  1211. ;; make sure, that node can be found
  1212. (org-id-add-location id (buffer-file-name))
  1213. (setq buffer-save-without-query t)
  1214. (basic-save-buffer)
  1215. (while (not (org-at-table-p)) (forward-line -1))
  1216. (unless buffer-read-only (org-table-align))
  1217. (while (not (org-at-heading-p)) (forward-line -1))
  1218. ;; read back some info about new index
  1219. (let ((org-index-id id))
  1220. (org-index--verify-id))
  1221. ;; remember at least for this session
  1222. (setq org-index-id id)
  1223. ;; present results to user
  1224. (if temporary
  1225. (progn
  1226. ;; Present existing and temporary index together
  1227. (when compare
  1228. (pop-to-buffer-same-window org-index--buffer)
  1229. (goto-char org-index--point)
  1230. (org-index--unfold-buffer)
  1231. (delete-other-windows)
  1232. (select-window (split-window-vertically)))
  1233. ;; show new index
  1234. (pop-to-buffer-same-window buffer)
  1235. (org-id-goto id)
  1236. (org-index--unfold-buffer)
  1237. (if compare
  1238. (error "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
  1239. (message "This is your new temporary index.")))
  1240. (progn
  1241. ;; Only show the new index
  1242. (pop-to-buffer-same-window buffer)
  1243. (delete-other-windows)
  1244. (org-id-goto id)
  1245. (org-index--unfold-buffer)
  1246. (if (y-or-n-p "This is your new index table. It is already set for this Emacs session, so you may try it out. Do you want to save its id to make it available for future Emacs sessions too ? ")
  1247. (progn
  1248. (customize-save-variable 'org-index-id id)
  1249. (error "Saved org-index-id '%s' to %s" id (or custom-file
  1250. user-init-file)))
  1251. (let (sq)
  1252. (setq sq (format "(setq org-index-id \"%s\")" id))
  1253. (kill-new sq)
  1254. (error "Did not make the id of this new index permanent; you may want to put\n\n %s\n\ninto your own initialization; it is copied already, just yank it" sq))))))))
  1255. (defun org-index--unfold-buffer ()
  1256. "Helper function to unfold buffer."
  1257. (org-show-context)
  1258. (org-show-subtree)
  1259. (recenter 1)
  1260. (save-excursion
  1261. (org-back-to-heading)
  1262. (forward-line) ;; on property drawer
  1263. (org-cycle)
  1264. (org-index--goto-list "columns-and-flags")
  1265. (org-cycle)))
  1266. (defun org-index--update-line (&optional ref-or-id)
  1267. "Update columns count and last-accessed in line REF-OR-ID."
  1268. (let ((newcount 0)
  1269. initial)
  1270. (with-current-buffer org-index--buffer
  1271. (unless buffer-read-only
  1272. ;; search reference or id, if given (or assume, that we are already positioned right)
  1273. (when ref-or-id
  1274. (setq initial (point))
  1275. (goto-char org-index--below-hline)
  1276. (while (and (org-at-table-p)
  1277. (not (or (string= ref-or-id (org-index--get-or-set-field 'ref))
  1278. (string= ref-or-id (org-index--get-or-set-field 'id)))))
  1279. (forward-line)))
  1280. (if (not (org-at-table-p))
  1281. (error "Did not find reference or id '%s'" ref-or-id)
  1282. (org-index--update-current-line))
  1283. (if initial (goto-char initial))))))
  1284. (defun org-index--update-current-line ()
  1285. "Update current lines columns count and last-accessed."
  1286. (let (newcount (count-field (org-index--get-or-set-field 'count)))
  1287. ;; update count field only if number or empty
  1288. (when (or (not count-field)
  1289. (string-match "^[0-9]+$" count-field))
  1290. (setq newcount (+ 1 (string-to-number (or count-field "0"))))
  1291. (org-index--get-or-set-field 'count
  1292. (number-to-string newcount)))
  1293. ;; update timestamp
  1294. (org-table-goto-column (org-index--column-num 'last-accessed))
  1295. (org-table-blank-field)
  1296. (org-insert-time-stamp nil t t)
  1297. ;; move line according to new content
  1298. (org-index--promote-current-line)))
  1299. (defun org-index--promote-current-line ()
  1300. "Move current line up in table according to changed sort fields."
  1301. (let (begin end key
  1302. (to-skip 0))
  1303. (forward-line 0) ; stay at beginning of line
  1304. (setq key (org-index--get-sort-key))
  1305. (setq begin (point))
  1306. (setq end (line-beginning-position 2))
  1307. (forward-line -1)
  1308. (while (and (org-at-table-p)
  1309. (not (org-at-table-hline-p))
  1310. (string< (org-index--get-sort-key) key))
  1311. (incf to-skip)
  1312. (forward-line -1))
  1313. (forward-line 1)
  1314. ;; insert line at new position
  1315. (when (> to-skip 0)
  1316. (insert (delete-and-extract-region begin end))
  1317. (forward-line -1))))
  1318. (defun org-index--get-sort-key (&optional sort with-ref)
  1319. "Get value for sorting from column SORT, optional WITH-REF."
  1320. (let (ref
  1321. ref-field
  1322. key)
  1323. (unless sort (setq sort org-index--last-sort)) ; use default value
  1324. (when (or with-ref
  1325. (eq sort 'ref))
  1326. ;; get reference with leading zeroes, so it can be
  1327. ;; sorted as text
  1328. (setq ref-field (org-index--get-or-set-field 'ref))
  1329. (string-match org-index--ref-regex ref-field)
  1330. (setq ref (format
  1331. "%06d"
  1332. (string-to-number
  1333. (or (match-string 1 ref-field)
  1334. "0")))))
  1335. (setq key
  1336. (cond
  1337. ((eq sort 'count)
  1338. (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))
  1339. ((eq sort 'ref)
  1340. ref)
  1341. ((eq sort 'id)
  1342. (org-index--get-or-set-field sort))
  1343. ((eq sort 'last-accessed)
  1344. (org-index--get-or-set-field sort))
  1345. ((eq sort 'created)
  1346. (org-index--get-or-set-field sort))
  1347. (t (error "This is a bug: unmatched case '%s'" sort))))
  1348. (if with-ref (setq key (concat key ref)))
  1349. key))
  1350. (defun org-index--get-or-set-field (key &optional value)
  1351. "Retrieve field KEY from index table or set it to VALUE."
  1352. (let (field)
  1353. (save-excursion
  1354. (setq field (org-trim (org-table-get-field (cdr (assoc key org-index--columns)) value)))
  1355. (if (string= field "") (setq field nil))
  1356. (org-no-properties field))))
  1357. (defun org-index--column-num (key)
  1358. "Return number of column KEY."
  1359. (if (numberp key)
  1360. key
  1361. (cdr (assoc key org-index--columns))))
  1362. (defun org-index--special-column (key)
  1363. "Return column (not a number) for special column KEY."
  1364. (cdr (assoc key org-index--special-columns)))
  1365. (defun org-index--flag-p (flag column)
  1366. "Check if COLUMN has FLAG set."
  1367. (unless (memq flag org-index--all-flags)
  1368. (error (format "Internal error: unknown flag %s" (symbol-name flag))))
  1369. (memq column (assoc flag org-index--flagged-columns)))
  1370. (defun org-index--make-guarded-search (ref &optional dont-quote)
  1371. "Make robust search string from REF; DONT-QUOTE it, if requested."
  1372. (concat "\\_<" (if dont-quote ref (regexp-quote ref)) "\\_>"))
  1373. (defun org-index--do-statistics ()
  1374. "Compute statistics about index table."
  1375. (let ((total 0)
  1376. ref-field ref min max message)
  1377. ;; go through table and remove all refs, that we see
  1378. (goto-char org-index--below-hline)
  1379. (while (org-at-table-p)
  1380. ;; get ref-field and number
  1381. (setq ref-field (org-index--get-or-set-field 'ref))
  1382. (if (and ref-field
  1383. (string-match org-index--ref-regex ref-field))
  1384. (setq ref (string-to-number (match-string 1 ref-field))))
  1385. ;; record min and max
  1386. (if (or (not min) (< ref min)) (setq min ref))
  1387. (if (or (not max) (> ref max)) (setq max ref))
  1388. ;; count
  1389. (setq total (1+ total))
  1390. (forward-line))
  1391. (setq message (format "First reference is %s, last %s; %d values in between, %d of them are used (%d percent)"
  1392. (format org-index--ref-format min)
  1393. (format org-index--ref-format max)
  1394. (1+ (- max min))
  1395. total
  1396. (truncate (* 100 (/ (float total) (1+ (- max min)))))
  1397. ))
  1398. (goto-char org-index--below-hline)
  1399. message))
  1400. (defun org-index--do-add-or-update ()
  1401. "For current node or current line in index, add a new line to index table or update existing."
  1402. (let* (id ref args yank ref-and-yank)
  1403. ;; do the same things from within index and from outside
  1404. (if org-index--within-node
  1405. (progn
  1406. (unless (org-at-table-p)
  1407. (error "Within index node but not on table"))
  1408. (setq id (org-index--get-or-set-field 'id))
  1409. (setq ref (org-index--get-or-set-field 'ref))
  1410. (setq args (org-index--collect-values-for-add-update-remote id))
  1411. (org-index--write-fields-for-add-update args)
  1412. (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add)))
  1413. (cons (format "Updated index line %s" ref) yank))
  1414. (unless (org-at-heading-p)
  1415. (error "Not at headline"))
  1416. (setq id (org-id-get-create))
  1417. (setq ref (org-index--on 'id id (org-index--get-or-set-field 'ref)))
  1418. (setq args (org-index--collect-values-for-add-update id ref))
  1419. (if ref
  1420. ;; already have a ref, find it in index and update fields
  1421. (let ((kvs args)
  1422. found-and-message)
  1423. (org-index--on
  1424. 'ref ref
  1425. (org-index--write-fields-for-add-update args)
  1426. (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add))))
  1427. (cons (format "Updated index line %s" ref) yank))
  1428. ;; no ref here, create new line in index
  1429. (setq ref-and-yank (apply 'org-index--do-new-line args))
  1430. (cons (format "Added index line %s" (car ref-and-yank)) (cdr ref-and-yank))))))
  1431. (defun org-index--check-ids ()
  1432. "Check, that ids really point to a node."
  1433. (let ((lines 0)
  1434. id ids marker)
  1435. (goto-char org-index--below-hline)
  1436. (catch 'problem
  1437. (while (org-at-table-p)
  1438. (when (setq id (org-index--get-or-set-field 'id))
  1439. ;; check for double ids
  1440. (when (member id ids)
  1441. (org-table-goto-column (org-index--column-num 'id))
  1442. (throw 'problem "This id appears twice in index; please use command 'maintain' to check for duplicate ids"))
  1443. (incf lines)
  1444. (setq ids (cons id ids))
  1445. ;; check, if id is valid
  1446. (setq marker (org-id-find id t))
  1447. (unless marker
  1448. (org-table-goto-column (org-index--column-num 'id))
  1449. (throw 'problem "This id cannot be found")))
  1450. (forward-line))
  1451. (goto-char org-index--below-hline)
  1452. nil)))
  1453. (defun org-index--update-all-lines ()
  1454. "Update all lines of index at once."
  1455. (let ((lines 0)
  1456. id ref kvs)
  1457. ;; check for double ids
  1458. (or
  1459. (org-index--check-ids)
  1460. (progn
  1461. (goto-char org-index--below-hline)
  1462. (while (org-at-table-p)
  1463. ;; update single line
  1464. (when (setq id (org-index--get-or-set-field 'id))
  1465. (setq ref (org-index--get-or-set-field 'ref))
  1466. (setq kvs (org-index--collect-values-for-add-update-remote id))
  1467. (org-index--write-fields-for-add-update kvs)
  1468. (incf lines))
  1469. (forward-line))
  1470. (goto-char org-index--below-hline)
  1471. (org-table-align)
  1472. (format "Update %d lines." lines)))))
  1473. (defun org-index--collect-values-for-add-update (id &optional silent category)
  1474. "Collect values for adding or updating line specified by ID, do not ask if SILENT, use CATEGORY, if given."
  1475. (let ((args (list 'ref t 'id id))
  1476. content)
  1477. (dolist (col-num org-index--columns)
  1478. (setq content "")
  1479. ;; copy heading ?
  1480. (if (org-index--flag-p 'get-heading-on-add (car col-num))
  1481. (setq content (nth 4 (org-heading-components))))
  1482. ;; copy category ?
  1483. (if (org-index--flag-p 'get-category-on-add (car col-num))
  1484. (setq content (or category org-index--category-before)))
  1485. ;; Shift ref and timestamp ?
  1486. (if (org-index--flag-p 'shift-ref-and-date-on-add (car col-num))
  1487. (dotimes (i 2)
  1488. (if (or (string-match (concat "^\\s-*" org-index--ref-regex) content)
  1489. (string-match (concat org-ts-regexp-both) content))
  1490. (setq content (substring content (match-end 0))))))
  1491. (if (and (not silent) ; do not edit, if heading has already been added
  1492. (org-index--flag-p 'edit-on-add (car col-num)))
  1493. (setq content (read-from-minibuffer
  1494. (format "Edit text for column '%s': " (symbol-name (car col-num)))
  1495. content)))
  1496. (if (not (string= content ""))
  1497. (setq args (append (list (car col-num) content) args))))
  1498. args))
  1499. (defun org-index--collect-values-for-add-update-remote (id)
  1500. "Wrap `org-index--collect-values-for-add-update' by prior moving to remote node identified by ID."
  1501. (let (marker point args)
  1502. (setq marker (org-id-find id t))
  1503. ;; enter buffer and collect information
  1504. (with-current-buffer (marker-buffer marker)
  1505. (setq point (point))
  1506. (goto-char marker)
  1507. (setq args (org-index--collect-values-for-add-update id t (org-get-category)))
  1508. (goto-char point))
  1509. args))
  1510. (defun org-index--write-fields-for-add-update (kvs)
  1511. "Update current line with values from KVS (keys-values)."
  1512. (while kvs
  1513. (unless (eq (car kvs) 'ref)
  1514. (org-index--get-or-set-field (car kvs) (org-trim (cadr kvs))))
  1515. (setq kvs (cddr kvs))))
  1516. (defun org-index--do-delete ()
  1517. "Perform command delete."
  1518. (unless (org-at-heading-p)
  1519. (error "Not at headline"))
  1520. (let* ((id (org-entry-get (point) "ID"))
  1521. (ref (org-index--ref-from-id id)))
  1522. ;; maybe delete from heading
  1523. (if ref
  1524. (save-excursion
  1525. (end-of-line)
  1526. (let ((end (point)))
  1527. (beginning-of-line)
  1528. (when (search-forward ref end t)
  1529. (delete-char (- (length ref)))
  1530. (just-one-space)))))
  1531. ;; delete from index table
  1532. (if (org-index--delete-line id)
  1533. (format "Deleted index line %s" ref)
  1534. (format "Did not find id %s in index" id))))
  1535. (defun org-index--go (&optional column value)
  1536. "Position cursor on index line where COLUMN equals VALUE.
  1537. Return t or nil, leave point on line or at top of table, needs to be in buffer initially."
  1538. (let (found text)
  1539. (unless (eq (current-buffer) org-index--buffer)
  1540. (error "This is a bug: Not in index buffer"))
  1541. ;; loop over lines
  1542. (goto-char org-index--below-hline)
  1543. (if column
  1544. (progn
  1545. (forward-line -1)
  1546. (while (and (not found)
  1547. (forward-line)
  1548. (org-at-table-p))
  1549. (setq found (string= value (org-index--get-or-set-field column)))))
  1550. (setq found t))
  1551. ;; return value
  1552. (if found
  1553. t
  1554. (goto-char org-index--below-hline)
  1555. nil)))
  1556. (defun org-index--do-head (ref id &optional other)
  1557. "Perform command head: Find node with REF or ID and present it.
  1558. If OTHER in separate window."
  1559. (setq org-index--last-ref ref)
  1560. (let (message marker)
  1561. (setq marker (org-id-find id t))
  1562. (if marker
  1563. (progn
  1564. (org-index--update-line id)
  1565. (let (cb)
  1566. (if other
  1567. (progn
  1568. (setq cb (current-buffer))
  1569. (pop-to-buffer (marker-buffer marker)))
  1570. (pop-to-buffer-same-window (marker-buffer marker)))
  1571. (goto-char marker)
  1572. (org-reveal t)
  1573. (org-show-entry)
  1574. (recenter))
  1575. (setq message (format "Found headline %s" ref)))
  1576. (setq message (format "Did not find headline %s" ref)))))
  1577. (defun org-index--do-occur ()
  1578. "Perform command occur."
  1579. (let ((word "") ; last word to search for growing and shrinking on keystrokes
  1580. (prompt "Search for: ")
  1581. (lines-wanted (window-body-height))
  1582. (lines-found 0) ; number of lines found
  1583. words ; list words that should match
  1584. occur-buffer
  1585. begin ; position of first line
  1586. narrow ; start of narrowed buffer
  1587. help-text ; cons with help text short and long
  1588. key-help ; for keys with special function
  1589. search-text ; description of text to search for
  1590. done ; true, if loop is done
  1591. in-c-backspace ; true, while processing C-backspace
  1592. show-headings ; true, if headings should be shown
  1593. help-overlay ; Overlay with help text
  1594. last-point ; Last position before end of search
  1595. key ; input from user
  1596. key-sequence) ; as a sequence
  1597. ;; make and show buffer
  1598. (if (get-buffer org-index--occur-buffer-name)
  1599. (kill-buffer org-index--occur-buffer-name))
  1600. (setq occur-buffer (make-indirect-buffer org-index--buffer org-index--occur-buffer-name))
  1601. (pop-to-buffer-same-window occur-buffer)
  1602. ;; avoid modifying direct buffer
  1603. (setq buffer-read-only t)
  1604. (toggle-truncate-lines 1)
  1605. (setq font-lock-keywords-case-fold-search t)
  1606. (setq case-fold-search t)
  1607. ;; reset stack and overlays
  1608. (setq org-index--occur-stack nil)
  1609. (setq org-index--occur-tail-overlay nil)
  1610. ;; narrow to table rows and one line before
  1611. (goto-char (marker-position org-index--below-hline))
  1612. (forward-line 0)
  1613. (setq begin (point))
  1614. (forward-line -1)
  1615. (setq narrow (point))
  1616. (while (org-at-table-p)
  1617. (forward-line))
  1618. (narrow-to-region narrow (point))
  1619. (goto-char (point-min))
  1620. (forward-line)
  1621. ;; initialize help text
  1622. (setq help-text (cons
  1623. "Incremental occur; `?' toggles help and headlines.\n"
  1624. (concat
  1625. (org-index--wrap
  1626. (concat
  1627. "Normal keys add to search word; <space> or <comma> start additional word; <backspace> erases last char, <C-backspace> last word; <return> jumps to heading, <tab> jumps to heading in other window; all other keys end search.\n"))
  1628. org-index--headings)))
  1629. ;; insert overlays for help text and to cover unsearched lines
  1630. (setq help-overlay (make-overlay (point-min) begin))
  1631. (overlay-put help-overlay 'display (car help-text))
  1632. (overlay-put help-overlay 'face 'org-agenda-dimmed-todo-face)
  1633. (setq org-index--occur-tail-overlay (make-overlay (point-max) (point-max)))
  1634. (overlay-put org-index--occur-tail-overlay 'invisible t)
  1635. (while (not done)
  1636. (if in-c-backspace
  1637. (setq key "<backspace>")
  1638. (setq search-text (mapconcat 'identity (reverse (cons word words)) ","))
  1639. ;; read key
  1640. (setq key-sequence
  1641. (vector (read-key
  1642. (format "%s%s%s"
  1643. prompt
  1644. search-text
  1645. (if (string= search-text "") "" " ")))))
  1646. (setq key (key-description key-sequence)))
  1647. (cond
  1648. ((string= key "<C-backspace>")
  1649. (setq in-c-backspace t))
  1650. ((member key (list "<backspace>" "DEL")) ; erase last char
  1651. (if (= (length word) 0)
  1652. ;; nothing more to delete from current word; try next
  1653. (progn
  1654. (setq word (car words))
  1655. (setq words (cdr words))
  1656. (setq in-c-backspace nil))
  1657. ;; unhighlight longer match
  1658. (unhighlight-regexp (regexp-quote word))
  1659. ;; some chars are left; shorten word
  1660. (setq word (substring word 0 -1))
  1661. (when (= (length word) 0) ; when nothing left, use next word from list
  1662. (setq word (car words))
  1663. (setq words (cdr words))
  1664. (setq in-c-backspace nil))
  1665. ;; free top list of overlays and remove list
  1666. (setq lines-found (or (org-index--unhide) lines-wanted))
  1667. (move-overlay org-index--occur-tail-overlay
  1668. (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
  1669. (point-max))
  1670. (point-max))
  1671. ;; highlight shorter word
  1672. (unless (= (length word) 0)
  1673. (highlight-regexp (regexp-quote word) 'isearch))
  1674. ;; make sure, point is still visible
  1675. (goto-char begin)))
  1676. ((member key (list "SPC" ",")) ; space or comma: enter an additional search word
  1677. ;; push current word and clear, no need to change display
  1678. (setq words (cons word words))
  1679. (setq word ""))
  1680. ((string= key "?") ; question mark: toggle display of headlines and help
  1681. (setq help-text (cons (cdr help-text) (car help-text)))
  1682. (overlay-put help-overlay 'display (car help-text)))
  1683. ((and (= (length key) 1)
  1684. (aref printable-chars (elt key 0))) ; any printable char: add to current search word
  1685. ;; unhighlight short word
  1686. (unless (= (length word) 0)
  1687. (unhighlight-regexp (regexp-quote word)))
  1688. ;; add to word
  1689. (setq word (concat word key))
  1690. ;; make overlays to hide lines, that do not match longer word any more
  1691. (goto-char begin)
  1692. (setq lines-found (org-index--hide-with-overlays (cons word words) lines-wanted))
  1693. (move-overlay org-index--occur-tail-overlay
  1694. (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
  1695. (point-max))
  1696. (point-max))
  1697. (goto-char begin)
  1698. ;; highlight longer word
  1699. (highlight-regexp (regexp-quote word) 'isearch)
  1700. ;; make sure, point is on a visible line
  1701. (line-move -1 t)
  1702. (line-move 1 t))
  1703. ;; anything else terminates loop
  1704. (t (setq done t))))
  1705. ;; put back input event, that caused the loop to end
  1706. (unless (string= key "C-g")
  1707. (setq unread-command-events (listify-key-sequence key-sequence))
  1708. (message key))
  1709. ;; postprocessing
  1710. (setq last-point (point))
  1711. ;; For performance reasons do not show matching lines for rest of table. So no code here.
  1712. ;; make permanent copy
  1713. ;; copy visible lines
  1714. (let ((lines-collected 0)
  1715. keymap line all-lines end-of-head)
  1716. (setq cursor-type t)
  1717. (goto-char begin)
  1718. ;; collect all visible lines
  1719. (while (and (not (eobp))
  1720. (< lines-collected lines-wanted))
  1721. ;; skip over invisible lines
  1722. (while (and (invisible-p (point))
  1723. (not (eobp)))
  1724. (goto-char (1+ (overlay-end (car (overlays-at (point)))))))
  1725. (setq line (buffer-substring (line-beginning-position) (line-end-position)))
  1726. (unless (string= line "")
  1727. (incf lines-collected)
  1728. (setq all-lines (cons (concat line
  1729. "\n")
  1730. all-lines)))
  1731. (forward-line 1))
  1732. (kill-buffer org-index--occur-buffer-name) ; cannot keep this buffer; might become stale soon
  1733. ;; create new buffer
  1734. (setq occur-buffer (get-buffer-create org-index--occur-buffer-name))
  1735. (pop-to-buffer-same-window occur-buffer)
  1736. (insert org-index--headings)
  1737. (setq end-of-head (point))
  1738. ;; insert into new buffer
  1739. (save-excursion
  1740. (apply 'insert (reverse all-lines))
  1741. (if (= lines-collected lines-wanted)
  1742. (insert "\n(more lines omitted)\n")))
  1743. (org-mode)
  1744. (setq truncate-lines t)
  1745. (if (org-at-table-p) (org-table-align))
  1746. (font-lock-fontify-buffer)
  1747. ;; prepare help text
  1748. (setq org-index--occur-help-overlay (make-overlay (point-min) end-of-head))
  1749. (setq org-index--occur-help-text
  1750. (cons
  1751. (org-index--wrap
  1752. (concat "Search is done; `?' toggles help and headlines.\n"))
  1753. (concat
  1754. (org-index--wrap (format (concat "Search is done. "
  1755. (if (< lines-collected lines-wanted)
  1756. " Showing all %d matches for "
  1757. " Showing one window of matches for ")
  1758. "\"" search-text
  1759. "\". <return> jumps to heading, <tab> jumps to heading in other window, <S-return> to matching line in index, <space> increments count.\n" )
  1760. (length all-lines)))
  1761. org-index--headings)))
  1762. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))
  1763. (overlay-put org-index--occur-help-overlay 'face 'org-agenda-dimmed-todo-face)
  1764. ;; highlight words
  1765. (setq case-fold-search t)
  1766. (setq font-lock-keywords-case-fold-search t)
  1767. (mapc (lambda (w) (unless (or (not w) (string= w "")) (highlight-regexp (regexp-quote w) 'isearch)))
  1768. (cons word words))
  1769. (setq buffer-read-only t)
  1770. ;; install keyboard-shortcuts
  1771. (setq keymap (make-sparse-keymap))
  1772. (set-keymap-parent keymap org-mode-map)
  1773. (mapc (lambda (x) (define-key keymap (kbd x)
  1774. (lambda () (interactive)
  1775. (message "%s" (org-index--occur-to-head)))))
  1776. (list "<return>" "RET"))
  1777. (define-key keymap (kbd "<tab>")
  1778. (lambda () (interactive)
  1779. (message (org-index--occur-to-head t))))
  1780. (define-key keymap (kbd "SPC")
  1781. (lambda () (interactive)
  1782. ;; increment in index
  1783. (let ((ref (org-index--get-or-set-field 'ref))
  1784. count)
  1785. (org-index--on
  1786. 'ref ref
  1787. (setq count (+ 1 (string-to-number (org-index--get-or-set-field 'count))))
  1788. (org-index--get-or-set-field 'count (number-to-string count))
  1789. (org-index--promote-current-line))
  1790. ;; increment in this buffer
  1791. (let ((inhibit-read-only t))
  1792. (org-index--get-or-set-field 'count (number-to-string count)))
  1793. (message "Incremented count to %d" count))))
  1794. (define-key keymap (kbd "<S-return>")
  1795. (lambda () (interactive)
  1796. (org-index 'enter (org-index--get-or-set-field 'ref))))
  1797. (define-key keymap (kbd "?")
  1798. (lambda () (interactive)
  1799. (setq-local org-index--occur-help-text (cons (cdr org-index--occur-help-text) (car org-index--occur-help-text)))
  1800. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))))
  1801. (use-local-map keymap))))
  1802. (defun org-index--wrap (text)
  1803. "Wrap TEXT at fill column."
  1804. (with-temp-buffer
  1805. (insert text)
  1806. (fill-region (point-min) (point-max) nil t)
  1807. (buffer-string)))
  1808. (defun org-index--occur-to-head (&optional other)
  1809. "Helper for `org-index--occur', find heading with ref or id; if OTHER, in other window."
  1810. (let ((ref (org-index--get-or-set-field 'ref))
  1811. (id (org-index--get-or-set-field 'id)))
  1812. (org-mark-ring-push)
  1813. (if id
  1814. (org-index--do-head ref id other)
  1815. (message "Current line has no id."))))
  1816. (defun org-index--hide-with-overlays (words lines-wanted)
  1817. "Hide text that is currently visible and does not match WORDS by creating overlays; leave LINES-WANTED lines visible."
  1818. (let ((lines-found 0)
  1819. (end-of-visible (point))
  1820. overlay overlays start matched)
  1821. ;; main loop
  1822. (while (and (not (eobp))
  1823. (< lines-found lines-wanted))
  1824. ;; skip invisible lines
  1825. (while (and (not (eobp))
  1826. (and
  1827. (invisible-p (point))
  1828. (< (point) (overlay-start org-index--occur-tail-overlay))))
  1829. (goto-char (overlay-end (car (overlays-at (point))))))
  1830. ;; find stretch of lines, that are currently visible but should be invisible now
  1831. (setq matched nil)
  1832. (setq start (point))
  1833. (while (and (not (eobp))
  1834. (not
  1835. (and
  1836. (invisible-p (point))
  1837. (< (point) (overlay-start org-index--occur-tail-overlay))))
  1838. (not (and (org-index--test-words words)
  1839. (setq matched t)))) ; for its side effect
  1840. (forward-line 1))
  1841. ;; create overlay to hide this stretch
  1842. (when (< start (point)) ; avoid creating an empty overlay
  1843. (setq overlay (make-overlay start (point)))
  1844. (overlay-put overlay 'invisible t)
  1845. (setq overlays (cons overlay overlays)))
  1846. ;; skip and count line, that matched
  1847. (when matched
  1848. (forward-line 1)
  1849. (setq end-of-visible (point))
  1850. (incf lines-found)))
  1851. ;; put new list on top of stack
  1852. (setq org-index--occur-stack
  1853. (cons (list (cons :overlays overlays)
  1854. (cons :end-of-visible end-of-visible)
  1855. (cons :lines lines-found))
  1856. org-index--occur-stack))
  1857. lines-found))
  1858. (defun org-index--unhide ()
  1859. "Unhide text that does has been hidden by `org-index--hide-with-overlays'."
  1860. (when org-index--occur-stack
  1861. ;; delete overlays and make visible again
  1862. (mapc (lambda (y)
  1863. (delete-overlay y))
  1864. (cdr (assoc :overlays (car org-index--occur-stack))))
  1865. ;; remove from stack
  1866. (setq org-index--occur-stack (cdr org-index--occur-stack))
  1867. ;; return number of lines, that are now visible
  1868. (if org-index--occur-stack (cdr (assoc :lines (car org-index--occur-stack))))))
  1869. (defun org-index--test-words (words)
  1870. "Test current line for match against WORDS."
  1871. (let (line)
  1872. (setq line (downcase (buffer-substring (line-beginning-position) (line-beginning-position 2))))
  1873. (catch 'not-found
  1874. (dolist (w words)
  1875. (or (search w line)
  1876. (throw 'not-found nil)))
  1877. t)))
  1878. (defun org-index--create-new-line ()
  1879. "Do the common work for `org-index-new-line' and `org-index'."
  1880. (let (new)
  1881. ;; construct new reference
  1882. (unless new
  1883. (setq new (format "%s%d%s" org-index--head (1+ org-index--maxref) org-index--tail)))
  1884. ;; remember for org-mark-ring-goto
  1885. (setq org-index--text-to-yank new)
  1886. ;; insert ref or id as last or first line, depending on sort-column
  1887. (goto-char org-index--below-hline)
  1888. (if (eq (org-index--special-column 'sort) 'count)
  1889. (progn
  1890. (while (org-at-table-p)
  1891. (forward-line))
  1892. (forward-line -1)
  1893. (org-table-insert-row t))
  1894. (org-table-insert-row))
  1895. ;; insert some of the standard values
  1896. (org-table-goto-column (org-index--column-num 'created))
  1897. (org-insert-time-stamp nil nil t)
  1898. (org-table-goto-column (org-index--column-num 'count))
  1899. (insert "1")
  1900. new))
  1901. (defun org-index--sort-silent ()
  1902. "Sort index for default column to remove any effects of temporary sorting."
  1903. (save-excursion
  1904. (org-index--verify-id)
  1905. (org-index--parse-table)
  1906. (org-index--on nil nil
  1907. (org-index--do-sort-index (org-index--special-column 'sort))
  1908. (org-table-align)
  1909. (remove-hook 'before-save-hook 'org-index--sort-silent))))
  1910. (provide 'org-index)
  1911. ;; Local Variables:
  1912. ;; fill-column: 75
  1913. ;; comment-column: 50
  1914. ;; lexical-binding: t
  1915. ;; End:
  1916. ;;; org-index.el ends here