org-index.el 91 KB

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