org-index.el 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321
  1. ;;; org-index.el --- A personal adaptive index for org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2018 Free Software Foundation, Inc.
  3. ;; Author: Marc Ihm <org-index@2484.de>
  4. ;; Version: 5.5.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 nodes and things outside of org.
  24. ;;
  25. ;; org-index creates and updates an index table with keywords; each line
  26. ;; either points to a heading in org, references something outside or
  27. ;; carries a snippet of text to yank. When searching the index, the set
  28. ;; of matching lines is updated with every keystroke; results are sorted
  29. ;; by usage count and date, so that frequently used entries appear first
  30. ;; in the list of results.
  31. ;;
  32. ;; References are decorated numbers (e.g. 'R237' or '--455--'); they are
  33. ;; well suited to be used outside of org, e.g. in folder names, ticket
  34. ;; systems or on printed documents.
  35. ;;
  36. ;; On first invocation org-index will assist you in creating the index
  37. ;; table.
  38. ;;
  39. ;; To start using your index, invoke subcommands 'add', 'ref' and 'yank'
  40. ;; to create entries and 'occur' to find them.
  41. ;;
  42. ;;
  43. ;; Setup:
  44. ;;
  45. ;; - Place this file in a directory of your load-path,
  46. ;; e.g. org-mode/contrib/lisp.
  47. ;;
  48. ;; - Add these lines to your .emacs:
  49. ;;
  50. ;; (require 'org-index)
  51. ;;
  52. ;; - Restart your Emacs to make this effective.
  53. ;;
  54. ;; - Invoke `org-index'; on first run it will assist in creating your
  55. ;; index table.
  56. ;;
  57. ;; - Optionally invoke `M-x org-customize', group 'Org Index', to tune
  58. ;; some settings, e.g. the global prefix key 'C-c i'.
  59. ;;
  60. ;;
  61. ;; Further information:
  62. ;;
  63. ;; - Watch the screencast at http://2484.de/org-index.html.
  64. ;;
  65. ;; - See the documentation of `org-index', which can also be read by
  66. ;; invoking `org-index' and choosing the command help or '?'.
  67. ;;
  68. ;;
  69. ;; Updates:
  70. ;;
  71. ;; The latest published version of this file can always be found at:
  72. ;;
  73. ;; http://orgmode.org/w/?p=org-mode.git;a=blob_plain;f=contrib/lisp/org-index.el;hb=HEAD
  74. ;;
  75. ;; Development version under:
  76. ;;
  77. ;; https://github.com/marcIhm/org-index
  78. ;;; Change Log:
  79. ;; [2017-09-03 So] Version 5.5.0
  80. ;; - In occur: case-sensitive search for upcase letters
  81. ;; - Better handling of nested focus nodes
  82. ;; - Bugfixes
  83. ;;
  84. ;; [2017-06-06 Tu] Version 5.4.2
  85. ;; - Dedicated submenu for focus operations
  86. ;; - Occur accepts a numeric argument as a day span
  87. ;; - New customization `org-index-clock-into-focus'
  88. ;; - Fixed delay after choosing an index line
  89. ;; - (Re)introduced lexical binding
  90. ;; - Bugfixes
  91. ;;
  92. ;; [2017-03-26 Su] Version 5.3.0
  93. ;; - Focused can now be on a list of nodes (instead of a single one)
  94. ;; - Cleaned up undeclared dependencies
  95. ;;
  96. ;; [2017-02-18 Sa] Version 5.2.3
  97. ;; - New command 'focus'
  98. ;; - Speeded up org-index--parse-table with the stored property "max-ref"
  99. ;; - Speeded up org-index--on with search
  100. ;; - Added org-index-prepare-when-idle
  101. ;; - Fixed compatibility issue with emacs 24 (font-lock-ensure)
  102. ;; - Added more customizations
  103. ;; - Bugfixes
  104. ;;
  105. ;; [2016-10-19 We] Version 5.1.4
  106. ;; - Bugfixes
  107. ;;
  108. ;; [2016-08-26 Fr] Version 5.1.3
  109. ;; - Offering help during query for subcommands
  110. ;; - Removed org-index-default-keybindings
  111. ;; - Renamed subcommand multi-occur to find-ref
  112. ;; - Subcommands add needs no longer be invoked from heading
  113. ;; - Many Bugfixes
  114. ;;
  115. ;; [2015-12-29 Tu] Version 5.0.2
  116. ;; - New commands yank, column and edit
  117. ;; - New column tags
  118. ;; - All columns are now required
  119. ;; - References are now optional
  120. ;; - Subcommand enter has been renamed to index
  121. ;; - Subcommands kill and edit can be invoked from an occur buffer
  122. ;; - Many Bugfixes
  123. ;; - Added link to screencast
  124. ;;
  125. ;; [2015-08-20 Th] Version 4.3.0
  126. ;; - Configuration is done now via standard customize
  127. ;; - New sorting strategy 'mixed'
  128. ;; - Silenced some compiler warnings
  129. ;;
  130. ;; [2015-03-18 We] Version 4.2.1
  131. ;; - No garbage in kill-ring
  132. ;; - No recentering after add
  133. ;;
  134. ;; [2015-03-08 Su] Version 4.2.0
  135. ;; - Reference numbers for subcommands can be passed as a prefix argument
  136. ;; - New variable org-index-default-keybindings-list with a list of
  137. ;; default keybindings for org-index-default-keybindings
  138. ;; - Added new column level
  139. ;; - removed flags get-category-on-add and get-heading-on-add
  140. ;;
  141. ;; [2015-02-26 Th] to [2015-03-05 Th] Version 4.0.0 to 4.1.2
  142. ;; - Removed command "leave"; rather go back with org-mark-ring-goto
  143. ;; - Renamed column "link" to "id"
  144. ;; - Added maintainance options to find duplicate rows, to check ids,
  145. ;; update index or remove property org-index-ref from nodes
  146. ;; - Shortened versin history
  147. ;;
  148. ;; [2014-12-08 Mo] to [2015-01-31 Sa] Version 3.0.0 to 3.2.0:
  149. ;; - Complete sorting of index only occurs in idle-timer
  150. ;; - New command "maintain" with some subcommands
  151. ;; - Rewrote command "occur" with overlays in an indirect buffer
  152. ;; - Command "add" updates index, if node is already present
  153. ;; - New commands "add" and "delete" to easily add and remove
  154. ;; the current node to or from your index.
  155. ;; - New command "example" to create an example index.
  156. ;; - Several new flags that are explained within index node.
  157. ;; - Removed commands "reuse", "missing", "put", "goto",
  158. ;; "update", "link", "fill", "unhighlight"
  159. ;; - New function `org-index-default-keybindings'
  160. ;;
  161. ;; [2012-12-07 Fr] to [2014-04-26 Sa] Version 2.0.0 to 2.4.3:
  162. ;; - New functions org-index-new-line and org-index-get-line
  163. ;; offer access to org-index from other lisp programs
  164. ;; - Regression tests with ert
  165. ;; - Renamed from "org-favtable" to "org-index"
  166. ;; - Added an assistant to set up the index table
  167. ;; - occur is now incremental, searching as you type
  168. ;; - Integrated with org-mark-ring-goto
  169. ;; - Added full support for ids
  170. ;; - Renamed the package from "org-reftable" to "org-favtable"
  171. ;; - Additional columns are required (e.g. "link"). Error messages will
  172. ;; guide you
  173. ;; - Ask user explicitly, which command to invoke
  174. ;; - Renamed the package from "org-refer-by-number" to "org-reftable"
  175. ;;
  176. ;; [2011-12-10 Sa] to [2012-09-22 Sa] Version Version 1.2.0 to 1.5.0:
  177. ;; - New command "sort" to sort a buffer or region by reference number
  178. ;; - New commands "highlight" and "unhighlight" to mark references
  179. ;; - New command "head" to find a headline with a reference number
  180. ;; - New commands occur and multi-occur
  181. ;; - Started this Change Log
  182. ;;; Code:
  183. (require 'org-table)
  184. (require 'org-id)
  185. (require 'cl-lib)
  186. (require 'widget)
  187. ;; Version of this package
  188. (defvar org-index-version "5.5.0" "Version of `org-index', format is major.minor.bugfix, where \"major\" are incompatible changes and \"minor\" are new features.")
  189. ;; customizable options
  190. (defgroup org-index nil
  191. "Options concerning the optional index for org."
  192. :tag "Org Index"
  193. :group 'org)
  194. (defcustom org-index-id nil
  195. "Id of the Org-mode node, which contains the index table."
  196. :group 'org-index)
  197. (defcustom org-index-sort-by 'mixed
  198. "Strategy for sorting index table (and whence entries in occur).
  199. Valid values are:
  200. last-access Sort index by date and time of last access; show
  201. more recent entries first.
  202. count Sort by usage count; more often used entries first.
  203. mixed First, show all index entries, which have been
  204. used today; sort them by last access. Then show
  205. older entries sorted by usage count."
  206. :group 'org-index
  207. :set (lambda (s v)
  208. (set-default s v)
  209. (if (and org-index-id
  210. org-index--buffer
  211. (functionp 'org-index--sort-silent))
  212. (org-index--sort-silent)))
  213. :initialize 'custom-initialize-default
  214. :type '(choice
  215. (const last-accessed)
  216. (const count)
  217. (const mixed)))
  218. (defcustom org-index-dispatch-key "i"
  219. "Key to invoke ‘org-index-dispatch’, which is the central entry function for ‘org-index’."
  220. :group 'org-index
  221. :initialize 'custom-initialize-set
  222. :set (lambda (var val)
  223. (set-default var val)
  224. (global-set-key org-index-dispatch-key 'org-index-dispatch))
  225. :type 'key-sequence)
  226. (defcustom org-index-idle-delay 68
  227. "Delay in seconds after which buffer will sorted or fontified when Emacs is idle."
  228. :group 'org-index
  229. :type 'integer)
  230. (defcustom org-index-prepare-when-idle nil
  231. "Fontify and sort index-table when idle to make first call faster.
  232. You only need this if your index has grown so large, that first
  233. invocation of `org-index' needs a noticable amount of time."
  234. :group 'org-index
  235. :initialize 'custom-initialize-set
  236. :set (lambda (var val)
  237. (set-default var val)
  238. (when val
  239. (setq org-index--align-interactive 200)
  240. (run-with-idle-timer org-index-idle-delay nil 'org-index--idle-prepare)))
  241. :type 'boolean)
  242. (defcustom org-index-yank-after-add 'ref
  243. "Specifies which column should be yanked after adding a new index row.
  244. Valid values are some columns of index table."
  245. :group 'org-index
  246. :type '(choice
  247. (const ref)
  248. (const category)
  249. (const keywords)))
  250. (defcustom org-index-copy-heading-to-keywords t
  251. "When adding a new node to index: Copy heading to keywords-column ?"
  252. :group 'org-index
  253. :type '(choice (const :tag "Yes" t)
  254. (const :tag "No" nil)))
  255. (defcustom org-index-strip-ref-and-date-from-heading t
  256. "When adding a node to index: strip leading ref or timestamps ?
  257. This can be useful, if you have the habit of adding refs and
  258. dates to the start of your headings; then, if you change your
  259. heading and want to update your index, you do not need to remove
  260. those pieces."
  261. :group 'org-index
  262. :type '(choice (const :tag "Yes" t)
  263. (const :tag "No" nil)))
  264. (defcustom org-index-edit-on-add '(category keywords)
  265. "List of columns to edit when adding a new row."
  266. :group 'org-index
  267. :type '(repeat (choice
  268. (const category)
  269. (const keywords))))
  270. (defcustom org-index-edit-on-yank '(keywords yank)
  271. "List of columns to edit when adding new text to yank."
  272. :group 'org-index
  273. :type '(repeat (choice
  274. (const yank)
  275. (const category)
  276. (const keywords))))
  277. (defcustom org-index-edit-on-ref '(category keywords)
  278. "List of columns to edit when adding new ref."
  279. :group 'org-index
  280. :type '(repeat (choice
  281. (const category)
  282. (const keywords))))
  283. (defcustom org-index-clock-into-focus nil
  284. "Clock into focused node."
  285. :group 'org-index
  286. :type 'boolean)
  287. ;; Variables to hold the configuration of the index table
  288. (defvar org-index--head nil "Header before number (e.g. 'R').")
  289. (defvar org-index--tail nil "Tail after number (e.g. '}' or ')'.")
  290. (defvar org-index--numcols nil "Number of columns in index table.")
  291. (defvar org-index--ref-regex nil "Regular expression to match a reference.")
  292. (defvar org-index--ref-format nil "Format, that can print a reference.")
  293. (defvar org-index--columns nil "Columns of index-table.")
  294. (defvar org-index--buffer nil "Buffer of index table.")
  295. (defvar org-index--point nil "Position at start of headline of index table.")
  296. (defvar org-index--below-hline nil "Position of first cell in first line below hline.")
  297. (defvar org-index--saved-positions nil "Saved positions within current buffer and index buffer; filled by ‘org-index--save-positions’.")
  298. (defvar org-index--headings nil "Headlines of index-table as a string.")
  299. (defvar org-index--headings-visible nil "Visible part of headlines of index-table as a string.")
  300. (defvar org-index--ids-focused-nodes nil "Ids of focused node (if any).")
  301. (defvar org-index--id-last-goto-focus nil "Id of last node, that has been focused to.")
  302. ;; Variables to hold context and state
  303. (defvar org-index--last-fingerprint nil "Fingerprint of last line created.")
  304. (defvar org-index--category-before nil "Category of node before.")
  305. (defvar org-index--active-region nil "Active region, initially. I.e. what has been marked.")
  306. (defvar org-index--below-cursor nil "Word below cursor.")
  307. (defvar org-index--within-index-node nil "True, if we are within node of the index table.")
  308. (defvar org-index--within-occur nil "True, if we are within the occur-buffer.")
  309. (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
  310. (defvar org-index--occur-help-text nil "Text for help in occur buffer.")
  311. (defvar org-index--occur-help-overlay nil "Overlay for help in occur buffer.")
  312. (defvar org-index--occur-stack nil "Stack with overlays for hiding lines.")
  313. (defvar org-index--occur-tail-overlay nil "Overlay to cover invisible lines.")
  314. (defvar org-index--occur-lines-collected 0 "Number of lines collected in occur buffer; helpful for tests.")
  315. (defvar org-index--last-sort-assumed nil "Last column, the index has been sorted after (best guess).")
  316. (defvar org-index--sort-timer nil "Timer to sort index in correct order.")
  317. (defvar org-index--inhibit-sort-idle nil "If set, index will not be sorted in idle background.")
  318. (defvar org-index--aligned 0 "For this Emacs session: remember number of table lines aligned.")
  319. (defvar org-index--align-interactive most-positive-fixnum "Number of rows to align in ‘org-index--parse-table’.")
  320. (defvar org-index--edit-widgets nil "List of widgets used to edit.")
  321. (defvar org-index--context-index nil "Position and line used for index in edit buffer.")
  322. (defvar org-index--context-occur nil "Position and line used for occur in edit buffer.")
  323. (defvar org-index--context-node nil "Buffer and position for node in edit buffer.")
  324. (defvar org-index--short-help-buffer-name "*org-index commands*" "Name of buffer to display short help.")
  325. (defvar org-index--display-short-help nil "True, if short help should be displayed.")
  326. (defvar org-index--short-help-displayed nil "True, if short help message has been displayed.")
  327. (defvar org-index--prefix-arg nil "True, if prefix argument has been received during input.")
  328. (defvar org-index--minibuffer-saved-key nil "Temporarily save entry of minibuffer keymap.")
  329. (defvar org-index--after-focus-timer nil "Timer to clock in or update focused node after a delay.")
  330. (defvar org-index--after-focus-context nil "Context for after focus action.")
  331. (defvar org-index--this-command nil "Subcommand, that is currently excecuted.")
  332. (defvar org-index--last-command nil "Subcommand, that hast been excecuted last.")
  333. ;; static information for this program package
  334. (defconst org-index--commands '(occur add kill head ping index ref yank column edit help short-help focus example sort find-ref highlight maintain) "List of commands available.")
  335. (defconst org-index--valid-headings '(ref id created last-accessed count keywords category level yank tags) "All valid headings.")
  336. (defconst org-index--occur-buffer-name "*org-index-occur*" "Name of occur buffer.")
  337. (defconst org-index--edit-buffer-name "*org-index-edit*" "Name of edit buffer.")
  338. (defvar org-index--short-help-text nil "Cache for result of `org-index--get-short-help-text.")
  339. (defvar org-index--shortcut-chars nil "Cache for result of `org-index--get-shortcut-chars.")
  340. (defvar org-index--after-focus-delay 10 "Number of seconds to wait before invoking after-focus action.")
  341. (defmacro org-index--on (column value &rest body)
  342. "Execute the forms in BODY with point on index line whose COLUMN is VALUE.
  343. The value returned is the value of the last form in BODY or nil,
  344. if VALUE cannot be found."
  345. (declare (indent 2) (debug t))
  346. (let ((pointvar (make-symbol "point"))
  347. (foundvar (make-symbol "found"))
  348. (retvar (make-symbol "ret")))
  349. `(save-current-buffer
  350. (let ((,pointvar (point))
  351. ,foundvar
  352. ,retvar)
  353. (set-buffer org-index--buffer)
  354. (setq ,foundvar (org-index--go ,column ,value))
  355. (when ,foundvar
  356. (setq ,retvar (progn ,@body)))
  357. (goto-char ,pointvar)
  358. ,retvar))))
  359. (defun org-index (&optional command search-ref arg)
  360. "Fast search-index for selected org nodes and things outside.
  361. This function creates and updates an index table with keywords;
  362. each line either points to a heading in org, references something
  363. outside or carries a snippet of text to yank. The index table is
  364. searched for keywords by means of an incremental occur; results
  365. are sorted by usage count and date, so that frequently used
  366. entries appear first.
  367. References are decorated numbers (e.g. 'R237' or '--455--'); they are
  368. well suited to be used outside of org, e.g. in folder names, ticket
  369. systems or on printed documents.
  370. On first invocation this function will help to create a dedicated node
  371. for its index table.
  372. To start building up your index, use subcommands 'add', 'ref' and
  373. 'yank' to create entries and use 'occur' to find them.
  374. This is version 5.5.0 of org-index.el.
  375. The function `org-index' is the only interactive function of this
  376. package and its main entry point; it will present you with a list
  377. of subcommands to choose from:
  378. \(Note the one-letter shortcuts, e.g. [o]; used like `\\[org-index-dispatch] o'.)
  379. occur: [o] Incrementally show matching lines from index.
  380. Result is updated after every keystroke. You may enter a
  381. list of words seperated by space or comma (`,'), to select
  382. lines that contain all of the given words. With a numeric
  383. prefix argument, show lines, which have been accessed at
  384. most this many days ago.
  385. add: [a] Add the current node to index.
  386. So that (e.g.) it can be found through the subcommand
  387. 'occur'. Update index, if node is already present.
  388. kill: [k] Kill (delete) the current node from index.
  389. Can be invoked from index, from occur or from a headline.
  390. head: [h] Search for heading, by ref or from index line.
  391. If invoked from within index table, go to associated
  392. node (if any), otherwise ask for ref to search.
  393. index: [i] Enter index table and maybe go to a specific reference.
  394. Use `org-mark-ring-goto' (\\[org-mark-ring-goto]) to go back.
  395. ping: [p] Echo line from index table for current node.
  396. If current node is not in index, than search among its
  397. parents.
  398. ref: [r] Create a new index line with a reference.
  399. This line will not be associated with a node.
  400. yank: [y] Store a new string, that can be yanked from occur.
  401. The index line will not be associated with a node.
  402. column: [c] From within index table: read char and jump to column.
  403. Shortcut for column movement; stays within one index line.
  404. edit: [e] Present current line in edit buffer.
  405. Can be invoked from index, from occur or from a headline.
  406. focus: [f] Return to first focused node; repeat to see them all.
  407. The focused nodes are kept in a short list; they need not be
  408. part of the index though. This command visits one focus node
  409. after the other, as long as you invoke it in quick succession
  410. and without moving to other nodes; otherwise it returns to
  411. the focus node, where you left off. Finally, with a prefix
  412. argument, this command offers more options, e.g. to set focus
  413. in the first place.
  414. help: Show complete help text of `org-index'.
  415. I.e. this text.
  416. short-help: [?] Show this one-line description of each subcommand.
  417. I.e. from the complete help, show only the first line for each
  418. subcommand.
  419. example: Create an example index, that will not be saved.
  420. May serve as an example.
  421. sort: Sort lines in index, in region or buffer.
  422. Region or buffer can be sorted by contained reference; Index
  423. by count, reference or last access.
  424. find-ref: Search for given reference in all org-buffers.
  425. A wrapper to employ Emacs standard `multi-occur' function;
  426. asks for reference.
  427. highlight: Highlight or unhighlight all references.
  428. Operates on active region or whole buffer. Call with prefix
  429. argument (`C-u') to remove highlights.
  430. maintain: Index maintainance.
  431. Offers some choices to check, update or fix your index.
  432. If you invoke `org-index' for the first time, an assistant will be
  433. invoked, that helps you to create your own index.
  434. Invoke `org-customize' to tweak the behaviour of `org-index'.
  435. This includes the global key `org-index-dispatch-key' to invoke
  436. the most important subcommands with one additional key.
  437. A numeric prefix argument is used as a reference number for
  438. commands, that need one (e.g. 'head') or to modify their
  439. behaviour (e.g. 'occur').
  440. Also, a single prefix argument may be specified just before the
  441. final character (e.g. like `C-c i C-u f') or by just typing an
  442. upper case letter (e.g. `C-c i F').
  443. Use from elisp: Optional argument COMMAND is a symbol naming the
  444. command to execute. SEARCH-REF specifies a reference to search
  445. for, if needed. ARG allows passing in a prefix argument as in
  446. interactive calls."
  447. (interactive "i\ni\nP")
  448. (let (search-id ; id to search for
  449. search-fingerprint ; fingerprint to search for
  450. sort-what ; sort what ?
  451. kill-new-text ; text that will be appended to kill ring
  452. message-text) ; text that will be issued as an explanation
  453. (catch 'new-index
  454. ;;
  455. ;; Initialize and parse
  456. ;;
  457. ;; creates index table, if necessary
  458. (org-index--verify-id)
  459. ;; Get configuration of index table
  460. (org-index--parse-table org-index--align-interactive t)
  461. ;; store context information
  462. (org-index--retrieve-context)
  463. ;;
  464. ;; Arrange for proper sorting of index
  465. ;;
  466. ;; lets assume, that it has been sorted this way (we try hard to make sure)
  467. (unless org-index--last-sort-assumed (setq org-index--last-sort-assumed org-index-sort-by))
  468. ;; rearrange for index beeing sorted into default sort order after 300 secs of idle time
  469. (unless org-index--sort-timer
  470. (setq org-index--sort-timer
  471. (run-with-idle-timer org-index-idle-delay t 'org-index--sort-silent)))
  472. ;;
  473. ;; Find out, what we are supposed to do
  474. ;;
  475. ;; Check or read command
  476. (if (and command (not (eq command 'short-help)))
  477. (unless (memq command org-index--commands)
  478. (error "Unknown command '%s' passed as argument, valid choices are any of these symbols: %s"
  479. command (mapconcat 'symbol-name org-index--commands ",")))
  480. ;; read command; if requested display help in read-loop
  481. (setq org-index--display-short-help (eq command 'short-help))
  482. (setq command (org-index--read-command))
  483. (setq org-index--last-command org-index--this-command)
  484. (setq org-index--this-command command)
  485. (if org-index--prefix-arg (setq arg (or arg '(4))))
  486. (setq org-index--display-short-help nil))
  487. ;;
  488. ;; Get search string, if required; process possible sources one after
  489. ;; another (lisp argument, prefix argument, user input).
  490. ;;
  491. ;; Try prefix, if no lisp argument given
  492. (if (and (not search-ref)
  493. (numberp arg))
  494. (setq search-ref (format "%s%d%s" org-index--head arg org-index--tail)))
  495. ;; These actions really need a search string and may even prompt for it
  496. (when (memq command '(index head find-ref))
  497. ;; search from surrounding text ?
  498. (unless search-ref
  499. (if org-index--within-index-node
  500. (if (org-at-table-p)
  501. (setq search-ref (org-index--get-or-set-field 'ref)))
  502. (if (and org-index--below-cursor
  503. (string-match (concat "\\(" org-index--ref-regex "\\)")
  504. org-index--below-cursor))
  505. (setq search-ref (match-string 1 org-index--below-cursor)))))
  506. ;; If we still do not have a search string, ask user explicitly
  507. (unless search-ref
  508. (if (eq command 'index)
  509. (let ((r (org-index--read-search-for-index)))
  510. (setq search-ref (cl-first r))
  511. (setq search-id (cl-second r))
  512. (setq search-fingerprint (cl-third r)))
  513. (unless (and (eq command 'head)
  514. org-index--within-index-node
  515. (org-at-table-p))
  516. (setq search-ref (read-from-minibuffer "Search reference number: ")))))
  517. ;; Clean up search string
  518. (when search-ref
  519. (setq search-ref (org-trim search-ref))
  520. (if (string-match "^[0-9]+$" search-ref)
  521. (setq search-ref (concat org-index--head search-ref org-index--tail)))
  522. (if (string= search-ref "") (setq search-ref nil)))
  523. (if (and (not search-ref)
  524. (not (eq command 'index))
  525. (not (and (eq command 'head)
  526. org-index--within-index-node
  527. (org-at-table-p))))
  528. (error "Command %s needs a reference number" command)))
  529. ;;
  530. ;; Command sort needs to know in advance, what to sort for
  531. ;;
  532. (when (eq command 'sort)
  533. (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))))
  534. ;;
  535. ;; Enter table
  536. ;;
  537. ;; Arrange for beeing able to return
  538. (when (and (memq command '(occur head index example sort maintain focus))
  539. (not (string= (buffer-name) org-index--occur-buffer-name)))
  540. (org-mark-ring-push))
  541. ;; These commands will leave user in index table after they are finished
  542. (when (or (memq command '(index maintain))
  543. (and (eq command 'sort)
  544. (eq sort-what 'index)))
  545. (pop-to-buffer-same-window org-index--buffer)
  546. (goto-char org-index--point)
  547. (org-index--unfold-buffer))
  548. ;;
  549. ;; Actually do, what is requested
  550. ;;
  551. (cond
  552. ((eq command 'help)
  553. ;; bring up help-buffer for this function
  554. (describe-function 'org-index))
  555. ((eq command 'short-help)
  556. (org-index--display-short-help))
  557. ((eq command 'find-ref)
  558. ;; Construct list of all org-buffers
  559. (let (org-buffers)
  560. (dolist (buff (buffer-list))
  561. (set-buffer buff)
  562. (if (string= major-mode "org-mode")
  563. (setq org-buffers (cons buff org-buffers))))
  564. ;; Do multi-occur
  565. (multi-occur org-buffers (org-index--make-guarded-search search-ref))
  566. ;; Present results
  567. (if (get-buffer "*Occur*")
  568. (progn
  569. (setq message-text (format "Found '%s'" search-ref))
  570. (other-window 1)
  571. (toggle-truncate-lines 1))
  572. (setq message-text (format "Did not find '%s'" search-ref)))))
  573. ((eq command 'add)
  574. (let ((r (org-index--do-add-or-update (if (equal arg '(4)) t nil)
  575. (if (numberp arg) arg nil))))
  576. (setq message-text (car r))
  577. (setq kill-new-text (cdr r))))
  578. ((eq command 'kill)
  579. (setq message-text (org-index--do-kill)))
  580. ((eq command 'head)
  581. (if (and org-index--within-index-node
  582. (org-at-table-p))
  583. (setq search-id (org-index--get-or-set-field 'id)))
  584. (if (and (not search-id) search-ref)
  585. (setq search-id (org-index--id-from-ref search-ref)))
  586. (setq message-text
  587. (if search-id
  588. (org-index--find-id search-id)
  589. "Current line has no id")))
  590. ((eq command 'index)
  591. (goto-char org-index--below-hline)
  592. (setq message-text
  593. (if search-ref
  594. (if (org-index--go 'ref search-ref)
  595. (progn
  596. (org-index--update-current-line)
  597. (org-table-goto-column (org-index--column-num 'ref))
  598. (format "Found index line '%s'" search-ref))
  599. (format "Did not find index line with reference '%s'" search-ref))
  600. (if search-id
  601. (if (org-index--go 'id search-id)
  602. (progn
  603. (org-index--update-current-line)
  604. (org-table-goto-column (org-index--column-num 'ref))
  605. (format "Found index line '%s'" (org-index--get-or-set-field 'ref)))
  606. (format "Did not find index line with id '%s'" search-id))
  607. (if search-fingerprint
  608. (if (org-index--go 'fingerprint org-index--last-fingerprint)
  609. (progn
  610. (org-index--update-current-line)
  611. (beginning-of-line)
  612. (format "Found latest index line"))
  613. (format "Did not find index line"))
  614. ;; simply go into table
  615. "At index table"))))
  616. (recenter))
  617. ((eq command 'ping)
  618. (let ((moved-up 0) id info reached-top done)
  619. (unless (string= major-mode "org-mode") (error "No node at point"))
  620. ;; take id from current node or reference
  621. (setq id (if search-ref
  622. (org-index--id-from-ref search-ref)
  623. (org-id-get)))
  624. ;; move up until we find a node in index
  625. (save-excursion
  626. (outline-back-to-heading)
  627. (while (not done)
  628. (if id
  629. (setq info (org-index--on 'id id
  630. (mapcar (lambda (x) (org-index--get-or-set-field x))
  631. (list 'keywords 'count 'created 'last-accessed 'category 'ref)))))
  632. (setq reached-top (= (org-outline-level) 1))
  633. (if (or info reached-top)
  634. (setq done t)
  635. (outline-up-heading 1 t)
  636. (cl-incf moved-up))
  637. (setq id (org-id-get))))
  638. (if info
  639. (progn
  640. (setq message-text
  641. (apply 'format
  642. (append (list "'%s'%s has been accessed %s times between %s and %s; category is '%s', reference is '%s'"
  643. (pop info)
  644. (if (> moved-up 0) (format " (parent node, %d level up)" moved-up) ""))
  645. info)))
  646. (setq kill-new-text (car (last info))))
  647. (setq message-text "Neither this node nor any of its parents is part of index"))))
  648. ((eq command 'occur)
  649. (set-buffer org-index--buffer)
  650. (org-index--do-occur (if (numberp arg) arg nil)))
  651. ((eq command 'ref)
  652. (let (args newref)
  653. (setq args (org-index--collect-values-from-user org-index-edit-on-ref))
  654. (setq newref (org-index--get-save-maxref))
  655. (setq args (plist-put args 'ref newref))
  656. (apply 'org-index--do-new-line args)
  657. (setq kill-new-text newref)
  658. (setq message-text (format "Added new row with ref '%s'" newref))))
  659. ((eq command 'yank)
  660. (let (args)
  661. (setq args (org-index--collect-values-from-user org-index-edit-on-yank))
  662. (if (plist-get args 'yank)
  663. (plist-put args 'yank (replace-regexp-in-string "|" "\\vert" (plist-get args 'yank) nil 'literal)))
  664. (setq args (plist-put args 'category "yank"))
  665. (apply 'org-index--do-new-line args)
  666. (setq message-text "Added new row with text to yank")))
  667. ((eq command 'column)
  668. (if (and org-index--within-index-node
  669. (org-at-table-p))
  670. (let (char col num)
  671. (setq char (read-char "Please specify, which column to go to (r=ref, k=keywords, c=category, y=yank): "))
  672. (unless (memq char (list ?r ?k ?c ?y))
  673. (error (format "Invalid char '%c', cannot goto this column" char)))
  674. (setq col (cdr (assoc char '((?r . ref) (?k . keywords) (?c . category) (?y . yank)))))
  675. (setq num (org-index--column-num col))
  676. (if num
  677. (progn
  678. (org-table-goto-column num)
  679. (setq message-text (format "At column %s" (symbol-name col))))
  680. (error (format "Column '%s' is not present" col))))
  681. (error "Need to be in index table to go to a specific column")))
  682. ((eq command 'edit)
  683. (setq message-text (org-index--do-edit)))
  684. ((eq command 'sort)
  685. (let ((sorts (list "count" "last-accessed" "mixed" "id" "ref"))
  686. sort groups-and-counts)
  687. (cond
  688. ((eq sort-what 'index)
  689. (setq sort
  690. (intern
  691. (completing-read
  692. "Please choose column to sort index table: "
  693. (cl-copy-list sorts)
  694. nil t nil nil (symbol-name org-index-sort-by))))
  695. (org-index--do-sort-index sort)
  696. (org-table-goto-column (org-index--column-num (if (eq sort 'mixed) 'last-access sort)))
  697. ;; When saving index, it should again be sorted correctly
  698. (with-current-buffer org-index--buffer
  699. (add-hook 'before-save-hook 'org-index--sort-silent t))
  700. (setq message-text
  701. (format
  702. (concat "Your index has been sorted temporarily by %s and will be sorted again by %s after %d seconds of idle time"
  703. (if groups-and-counts
  704. "; %d groups with equal %s and a total of %d lines have been found"
  705. ""))
  706. (symbol-name sort)
  707. org-index-sort-by
  708. org-index-idle-delay
  709. (cl-second groups-and-counts)
  710. (symbol-name sort)
  711. (cl-third groups-and-counts))))
  712. ((memq sort-what '(region buffer))
  713. (org-index--do-sort-lines sort-what)
  714. (setq message-text (format "Sorted %s by contained references" sort-what))))))
  715. ((eq command 'highlight)
  716. (let ((where "buffer"))
  717. (save-excursion
  718. (save-restriction
  719. (when (and transient-mark-mode
  720. mark-active)
  721. (narrow-to-region (region-beginning) (region-end))
  722. (setq where "region"))
  723. (if arg
  724. (progn
  725. (unhighlight-regexp org-index--ref-regex)
  726. (setq message-text (format "Removed highlights for references in %s" where)))
  727. (highlight-regexp org-index--ref-regex 'isearch)
  728. (setq message-text (format "Highlighted references in %s" where)))))))
  729. ((eq command 'focus)
  730. (setq message-text (if arg
  731. (org-index--more-focus-commands)
  732. (org-index--goto-focus))))
  733. ((eq command 'maintain)
  734. (setq message-text (org-index--do-maintain)))
  735. ((eq command 'example)
  736. (if (y-or-n-p "This assistant will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
  737. (org-index--create-index t)))
  738. ((not command) (setq message-text "No command given"))
  739. (t (error "Unknown subcommand '%s'" command)))
  740. ;; tell, what we have done and what can be yanked
  741. (if kill-new-text (setq kill-new-text
  742. (substring-no-properties kill-new-text)))
  743. (if (string= kill-new-text "") (setq kill-new-text nil))
  744. (let ((m (concat
  745. message-text
  746. (if (and message-text kill-new-text)
  747. " and r"
  748. (if kill-new-text "R" ""))
  749. (if kill-new-text (format "eady to yank '%s'." kill-new-text) (if message-text "." "")))))
  750. (unless (string= m "")
  751. (message m)
  752. (setq org-index--message-text m)))
  753. (if kill-new-text (kill-new kill-new-text)))))
  754. (defun org-index-dispatch (&optional arg)
  755. "Read additional chars and call subcommands of `org-index'.
  756. Can be bound in global keyboard map as central entry point.
  757. Optional argument ARG is passed on."
  758. (interactive "P")
  759. (let (char command (c-u-text (if arg " C-u " "")))
  760. (while (not char)
  761. (if (sit-for 1)
  762. (message (concat "org-index (? for detailed prompt) -" c-u-text)))
  763. (setq char (key-description (read-key-sequence nil)))
  764. (if (string= char "C-g") (keyboard-quit))
  765. (if (string= char "SPC") (setq char "?"))
  766. (when (string= char (upcase char))
  767. (setq char (downcase char))
  768. (setq arg (or arg '(4))))
  769. (when (string= char "C-u")
  770. (setq arg (or arg '(4)))
  771. (setq c-u-text " C-u ")
  772. (setq char nil)))
  773. (setq command (cdr (assoc char (org-index--get-shortcut-chars))))
  774. (unless command
  775. (message "No subcommand for '%s'; switching to detailed prompt" char)
  776. (sit-for 1)
  777. (setq command 'short-help))
  778. (org-index command nil arg)))
  779. (defun org-index-new-line (&rest keys-values)
  780. "Create a new line within the index table, returning its reference.
  781. The function takes a varying number of argument pairs; each pair
  782. is a symbol for an existing column heading followed by its value.
  783. The return value is the new reference.
  784. Example:
  785. (message \"Created reference %s\"
  786. (org-index-new-line 'keywords \"foo bar\" 'category \"baz\"))
  787. Optional argument KEYS-VALUES specifies content of new line."
  788. (let ((ref (plist-get keys-values 'ref)))
  789. (org-index--verify-id)
  790. (org-index--parse-table)
  791. (if (not (memq ref '(t nil)))
  792. (error "Column 'ref' accepts only 't' or 'nil'"))
  793. (when ref
  794. (setq ref (org-index--get-save-maxref))
  795. (setq keys-values (plist-put keys-values 'ref ref)))
  796. (apply 'org-index--do-new-line keys-values)
  797. ref))
  798. (defun org-index--read-command ()
  799. "Read subcommand for ‘org-index’ from minibuffer."
  800. (let (minibuffer-scroll-window
  801. command)
  802. (setq org-index--short-help-displayed nil)
  803. (setq org-index--prefix-arg nil)
  804. (add-hook 'minibuffer-setup-hook 'org-index--minibuffer-setup-function)
  805. (add-hook 'minibuffer-exit-hook 'org-index--minibuffer-exit-function)
  806. (unwind-protect
  807. (setq command
  808. (completing-read
  809. (concat
  810. "Please choose"
  811. (if org-index--display-short-help "" " (? for short help)")
  812. ": ")
  813. (append (mapcar 'symbol-name org-index--commands)
  814. (mapcar 'upcase-initials (mapcar 'symbol-name org-index--commands)))
  815. nil t))
  816. (remove-hook 'minibuffer-setup-hook 'org-index--minibuffer-setup-function)
  817. (remove-hook 'minibuffer-exit-hook 'org-index--minibuffer-exit-function)
  818. (unless (string= command (downcase command))
  819. (if command (setq command (downcase command)))
  820. (setq org-index--prefix-arg '(4)))
  821. (setq command (intern command))
  822. (when org-index--short-help-displayed
  823. (quit-windows-on org-index--short-help-buffer-name)))
  824. command))
  825. (defun org-index--minibuffer-setup-function ()
  826. "Prepare minibuffer for `org-index--read-command'."
  827. (setq org-index--minibuffer-saved-key (local-key-binding (kbd "?")))
  828. (local-set-key (kbd "?") 'org-index--display-short-help)
  829. (local-set-key (kbd "C-u") (lambda () (interactive)
  830. (setq org-index--prefix-arg t)
  831. (message "C-u")))
  832. (if org-index--display-short-help (org-index--display-short-help)))
  833. (defun org-index--minibuffer-exit-function ()
  834. "Restore minibuffer after `org-index--read-command'."
  835. (local-set-key (kbd "?") org-index--minibuffer-saved-key)
  836. (local-set-key (kbd "C-u") 'universal-argument)
  837. (setq org-index--minibuffer-saved-key nil))
  838. (defun org-index--display-short-help ()
  839. "Helper function to show help in minibuffer."
  840. (interactive)
  841. (with-temp-buffer-window
  842. org-index--short-help-buffer-name nil nil
  843. (setq org-index--short-help-displayed t)
  844. (princ "Short help; shortcuts in []; capital letter acts like C-u.\n")
  845. (princ (org-index--get-short-help-text)))
  846. (with-current-buffer org-index--short-help-buffer-name
  847. (let ((inhibit-read-only t)
  848. win)
  849. (setq win (get-buffer-window))
  850. (shrink-window-if-larger-than-buffer win)
  851. (goto-char (point-min))
  852. (end-of-line)
  853. (goto-char (point-min)))))
  854. (defun org-index--get-short-help-text ()
  855. "Extract text for short help message from long help."
  856. (or org-index--short-help-text
  857. (with-temp-buffer
  858. (insert (documentation 'org-index))
  859. (goto-char (point-min))
  860. (search-forward (concat " " (symbol-name (cl-first org-index--commands)) ": "))
  861. (forward-line 0)
  862. (kill-region (point-min) (point))
  863. (search-forward (concat " " (symbol-name (car (last org-index--commands))) ": "))
  864. (forward-line 1)
  865. (kill-region (point) (point-max))
  866. (keep-lines "^ [-a-z]+:" (point-min) (point-max))
  867. (align-regexp (point-min) (point-max) "\\(\\s-*\\):")
  868. (goto-char (point-min))
  869. (while (re-search-forward "\\. *$" nil t)
  870. (replace-match "" nil nil))
  871. (goto-char (point-min))
  872. (re-search-forward "short-help")
  873. (end-of-line)
  874. (insert " (this text)")
  875. (goto-char (point-min))
  876. (unless (= (line-number-at-pos (point-max)) (1+ (length org-index--commands)))
  877. (error "Internal error, unable to properly extract one-line descriptions of subcommands"))
  878. (setq org-index--short-help-text (buffer-string)))))
  879. (defun org-index--get-shortcut-chars ()
  880. "Collect shortcut chars from short help message."
  881. (or org-index--shortcut-chars
  882. (with-temp-buffer
  883. (insert (org-index--get-short-help-text))
  884. (goto-char (point-min))
  885. (while (< (point) (point-max))
  886. (when (looking-at "^ \\([-a-z]+\\)[ \t]+: +\\[\\([a-z?]\\)\\] ")
  887. (setq org-index--shortcut-chars
  888. (cons (cons (match-string 2) (intern (match-string 1)))
  889. org-index--shortcut-chars)))
  890. (forward-line 1))
  891. (unless (> (length org-index--shortcut-chars) 0)
  892. (error "Internal error, did not find shortcut chars"))
  893. org-index--shortcut-chars)))
  894. (defun org-index--goto-focus ()
  895. "Goto focus node, one after the other."
  896. (if org-index--ids-focused-nodes
  897. (let (this-id target-id following-id last-id again explain marker)
  898. (setq again (and (eq this-command last-command)
  899. (eq org-index--this-command org-index--last-command)))
  900. (setq last-id (or org-index--id-last-goto-focus
  901. (car (last org-index--ids-focused-nodes))))
  902. (setq this-id (org-id-get))
  903. (setq following-id (car (or (cdr-safe (member last-id
  904. (append org-index--ids-focused-nodes
  905. org-index--ids-focused-nodes)))
  906. org-index--ids-focused-nodes)))
  907. (if again
  908. (progn
  909. (setq target-id following-id)
  910. (setq explain "Jumped to next"))
  911. (setq target-id last-id)
  912. (setq explain "Jumped back to current"))
  913. (if (member target-id (org-index--ids-up-to-top))
  914. (setq explain "Staying below current")
  915. (unless (setq marker (org-id-find target-id 'marker))
  916. (setq org-index--id-last-goto-focus nil)
  917. (error "Could not find focus-node with id %s" target-id))
  918. (pop-to-buffer-same-window (marker-buffer marker))
  919. (goto-char (marker-position marker))
  920. (org-index--unfold-buffer)
  921. (move-marker marker nil))
  922. (when org-index-clock-into-focus
  923. (if org-index--after-focus-timer (cancel-timer org-index--after-focus-timer))
  924. (setq org-index--after-focus-context target-id)
  925. (setq org-index--after-focus-timer
  926. (run-at-time org-index--after-focus-delay nil
  927. (lambda ()
  928. (if org-index--after-focus-context
  929. (if org-index-clock-into-focus
  930. (save-excursion
  931. (org-id-goto org-index--after-focus-context)
  932. (org-clock-in)))
  933. (org-index--update-line org-index--after-focus-context t)
  934. (setq org-index--after-focus-context nil))))))
  935. (setq org-index--id-last-goto-focus target-id)
  936. (if (cdr org-index--ids-focused-nodes)
  937. (format "%s focus node (out of %d)"
  938. explain
  939. (length org-index--ids-focused-nodes))
  940. "Jumped to single focus-node"))
  941. "No nodes in focus, use set-focus"))
  942. (defun org-index--more-focus-commands ()
  943. "More commands for handling focused nodes."
  944. (let (id text more-text char prompt ids-up-to-top)
  945. (setq prompt "Please specify action on the list focused nodes: set, append, delete (s,a,d or ? for short help) - ")
  946. (while (not (memq char (list ?s ?a ?d)))
  947. (setq char (read-char prompt))
  948. (setq prompt "Actions on list of focused nodes: s)et single focus on this node, a)ppend this node to list, d)elete this node from list. Please choose - "))
  949. (setq text
  950. (cond
  951. ((eq char ?s)
  952. (setq id (org-id-get-create))
  953. (setq org-index--ids-focused-nodes (list id))
  954. (setq org-index--id-last-goto-focus id)
  955. (if org-index-clock-into-focus (org-clock-in))
  956. "Focus has been set on current node%s (1 node in focus)")
  957. ((eq char ?a)
  958. (setq id (org-id-get-create))
  959. (unless (member id org-index--ids-focused-nodes)
  960. ;; remove any children, that are already in list of focused nodes
  961. (setq org-index--ids-focused-nodes
  962. (delete nil (mapcar (lambda (x)
  963. (if (member id (org-with-point-at (org-id-find x t)
  964. (org-index--ids-up-to-top)))
  965. (progn
  966. (setq more-text ", removing its children")
  967. nil)
  968. x))
  969. org-index--ids-focused-nodes)))
  970. ;; remove parent, if already in list of focused nodes
  971. (setq ids-up-to-top (org-index--ids-up-to-top))
  972. (when (seq-intersection ids-up-to-top org-index--ids-focused-nodes)
  973. (setq org-index--ids-focused-nodes (seq-difference org-index--ids-focused-nodes ids-up-to-top))
  974. (setq more-text (concat more-text ", replacing its parent")))
  975. (setq org-index--ids-focused-nodes (cons id org-index--ids-focused-nodes)))
  976. (setq org-index--id-last-goto-focus id)
  977. (setq org-index--id-last-goto-focus id)
  978. (if org-index-clock-into-focus (org-clock-in))
  979. "Current node has been appended to list of focused nodes%s (%d node%s in focus)")
  980. ((eq char ?d)
  981. (setq id (org-id-get))
  982. (if (and id (member id org-index--ids-focused-nodes))
  983. (progn
  984. (setq org-index--id-last-goto-focus
  985. (or (car-safe (cdr-safe (member id (reverse (append org-index--ids-focused-nodes
  986. org-index--ids-focused-nodes)))))
  987. org-index--id-last-goto-focus))
  988. (setq org-index--ids-focused-nodes (delete id org-index--ids-focused-nodes))
  989. (setq org-index--id-last-goto-focus nil)
  990. "Current node has been removed from list of focused nodes%s (%d node%s in focus)")
  991. "Current node has not been in list of focused nodes%s (%d node%s in focus)"))))
  992. (with-current-buffer org-index--buffer
  993. (org-entry-put org-index--point "ids-focused-nodes" (string-join org-index--ids-focused-nodes " ")))
  994. (format text (or more-text "") (length org-index--ids-focused-nodes) (if (cdr org-index--ids-focused-nodes) "s" ""))))
  995. (defun org-index--ids-up-to-top ()
  996. "Get list of all ids from current node up to top level"
  997. (when (string= major-mode "org-mode")
  998. (let (ancestors id level start-level)
  999. (save-excursion
  1000. (ignore-errors
  1001. (outline-back-to-heading)
  1002. (setq id (org-id-get))
  1003. (if id (setq ancestors (cons id ancestors)))
  1004. (setq start-level (org-outline-level))
  1005. (if (<= start-level 1)
  1006. nil
  1007. (while (> start-level 1)
  1008. (setq level start-level)
  1009. (while (>= level start-level)
  1010. (outline-previous-heading)
  1011. (setq level (org-outline-level)))
  1012. (setq start-level level)
  1013. (setq id (org-id-get))
  1014. (if id (setq ancestors (cons id ancestors))))
  1015. ancestors))))))
  1016. (defun org-index--do-edit ()
  1017. "Perform command edit."
  1018. (let ((maxlen 0) cols-vals buffer-keymap field-keymap keywords-pos val)
  1019. (setq org-index--context-node nil)
  1020. (setq org-index--context-occur nil)
  1021. ;; change to index, if whithin occur
  1022. (if org-index--within-occur
  1023. (let ((pos (get-text-property (point) 'org-index-lbp)))
  1024. (org-index--occur-test-stale pos)
  1025. (setq org-index--context-occur (cons (point) (org-index--line-in-canonical-form)))
  1026. (set-buffer org-index--buffer)
  1027. (goto-char pos))
  1028. ;; change to index, if still not within
  1029. (if (not org-index--within-index-node)
  1030. (let ((id (org-id-get)))
  1031. (setq org-index--context-node (cons (current-buffer) (point)))
  1032. (set-buffer org-index--buffer)
  1033. (unless (and id (org-index--go 'id id))
  1034. (setq org-index--context-node nil)
  1035. (error "This node is not in index")))))
  1036. ;; retrieve current content of index line
  1037. (dolist (col (mapcar 'car (reverse org-index--columns)))
  1038. (if (> (length (symbol-name col)) maxlen)
  1039. (setq maxlen (length (symbol-name col))))
  1040. (setq val (org-index--get-or-set-field col))
  1041. (if (and val (eq col 'yank)) (setq val (replace-regexp-in-string (regexp-quote "\\vert") "|" val nil 'literal)))
  1042. (setq cols-vals (cons (cons col val)
  1043. cols-vals)))
  1044. ;; we need two different keymaps
  1045. (setq buffer-keymap (make-sparse-keymap))
  1046. (set-keymap-parent buffer-keymap widget-keymap)
  1047. (define-key buffer-keymap (kbd "C-c C-c") 'org-index--edit-accept)
  1048. (define-key buffer-keymap (kbd "C-c C-k") 'org-index--edit-abort)
  1049. (setq field-keymap (make-sparse-keymap))
  1050. (set-keymap-parent field-keymap widget-field-keymap)
  1051. (define-key field-keymap (kbd "C-c C-c") 'org-index--edit-accept)
  1052. (define-key field-keymap (kbd "C-c C-k") 'org-index--edit-abort)
  1053. ;; prepare buffer
  1054. (setq org-index--context-index (cons (point) (org-index--line-in-canonical-form)))
  1055. (if (get-buffer org-index--edit-buffer-name) (kill-buffer org-index--edit-buffer-name))
  1056. (switch-to-buffer (get-buffer-create org-index--edit-buffer-name))
  1057. ;; create and fill widgets
  1058. (setq org-index--edit-widgets nil)
  1059. (widget-insert "Edit this line from index; type C-c C-c when done, C-c C-k to abort.\n\n")
  1060. (dolist (col-val cols-vals)
  1061. (if (eq (car col-val) 'keywords) (setq keywords-pos (point)))
  1062. (setq org-index--edit-widgets (cons
  1063. (cons (car col-val)
  1064. (widget-create 'editable-field
  1065. :format (format (format "%%%ds: %%%%v" maxlen) (symbol-name (car col-val)))
  1066. :keymap field-keymap
  1067. (or (cdr col-val) "")))
  1068. org-index--edit-widgets)))
  1069. (widget-setup)
  1070. (goto-char keywords-pos)
  1071. (beginning-of-line)
  1072. (forward-char (+ maxlen 2))
  1073. (use-local-map buffer-keymap)
  1074. (setq org-index--inhibit-sort-idle t)
  1075. "Editing a single line from index"))
  1076. (defun org-index--edit-accept ()
  1077. "Function to accept editing in Edit buffer."
  1078. (interactive)
  1079. (let ((obuf (get-buffer org-index--occur-buffer-name))
  1080. val line)
  1081. ;; Time might have passed
  1082. (org-index--refresh-parse-table)
  1083. (with-current-buffer org-index--buffer
  1084. ;; check, if buffer has become stale
  1085. (save-excursion
  1086. (goto-char (car org-index--context-index))
  1087. (unless (string= (cdr org-index--context-index)
  1088. (org-index--line-in-canonical-form))
  1089. (switch-to-buffer org-index--edit-buffer-name)
  1090. (error "Index table has changed: Cannot find line, that this buffer is editing")))
  1091. (pop-to-buffer-same-window org-index--buffer)
  1092. (goto-char (car org-index--context-index))
  1093. ;; write back line to index
  1094. (dolist (col-widget org-index--edit-widgets)
  1095. (setq val (widget-value (cdr col-widget)))
  1096. (if (eq (car col-widget) 'yank) (setq val (replace-regexp-in-string "|" (regexp-quote "\\vert") val)))
  1097. (org-index--get-or-set-field (car col-widget) val))
  1098. (setq line (org-index--align-and-fontify-current-line))
  1099. (beginning-of-line))
  1100. ;; write line to occur if appropriate
  1101. (if org-index--context-occur
  1102. (if obuf
  1103. (if (string= (cdr org-index--context-index)
  1104. (cdr org-index--context-occur))
  1105. (progn
  1106. (pop-to-buffer-same-window obuf)
  1107. (goto-char (car org-index--context-occur))
  1108. (beginning-of-line)
  1109. (let ((inhibit-read-only t))
  1110. (delete-region (line-beginning-position) (line-end-position))
  1111. (insert line)
  1112. (put-text-property (line-beginning-position) (line-end-position)
  1113. 'org-index-lbp (car org-index--context-index))))
  1114. (error "Occur buffer and index buffer do not match any longer"))
  1115. (message "Occur buffer has gone, cannot switch back."))
  1116. (setq org-index--context-occur nil))
  1117. ;; return to node, if invoked from there
  1118. (when org-index--context-node
  1119. (pop-to-buffer-same-window (car org-index--context-node))
  1120. (goto-char (cdr org-index--context-node)))
  1121. ;; clean up
  1122. (kill-buffer org-index--edit-buffer-name)
  1123. (setq org-index--inhibit-sort-idle nil)
  1124. (setq org-index--context-index nil)
  1125. (setq org-index--edit-widgets nil)
  1126. (beginning-of-line)
  1127. (message "Index line has been edited.")))
  1128. (defun org-index--edit-abort ()
  1129. "Function to abort editing in Edit buffer."
  1130. (interactive)
  1131. (kill-buffer org-index--edit-buffer-name)
  1132. (setq org-index--context-index nil)
  1133. (setq org-index--edit-widgets nil)
  1134. (beginning-of-line)
  1135. (message "Edit aborted."))
  1136. (defun org-index--do-new-line (&rest keys-values)
  1137. "Do the work for `org-index-new-line'.
  1138. Optional argument KEYS-VALUES specifies content of new line."
  1139. (org-index--retrieve-context)
  1140. (with-current-buffer org-index--buffer
  1141. (goto-char org-index--point)
  1142. ;; check arguments early; they might come from userland
  1143. (let ((kvs keys-values)
  1144. k v)
  1145. (while kvs
  1146. (setq k (car kvs))
  1147. (setq v (cadr kvs))
  1148. (if (or (not (symbolp k))
  1149. (and (symbolp v) (not (eq v t)) (not (eq v nil))))
  1150. (error "Arguments must be alternation of key and value"))
  1151. (unless (org-index--column-num k)
  1152. (error "Unknown column or column not defined in table: '%s'" (symbol-name k)))
  1153. (setq kvs (cddr kvs))))
  1154. (let (yank)
  1155. ;; create new line
  1156. (org-index--create-new-line)
  1157. ;; fill columns
  1158. (let ((kvs keys-values)
  1159. k v)
  1160. (while kvs
  1161. (setq k (car kvs))
  1162. (setq v (cadr kvs))
  1163. (org-table-goto-column (org-index--column-num k))
  1164. (insert (org-trim (or v "")))
  1165. (setq kvs (cddr kvs))))
  1166. ;; align and fontify line
  1167. (org-index--promote-current-line)
  1168. (org-index--align-and-fontify-current-line)
  1169. ;; remember fingerprint to be able to return
  1170. (setq org-index--last-fingerprint (org-index--get-or-set-field 'fingerprint))
  1171. ;; get column to yank
  1172. (setq yank (org-index--get-or-set-field org-index-yank-after-add))
  1173. yank)))
  1174. (defun org-index-get-line (column value)
  1175. "Retrieve an existing line within the index table by ref or id.
  1176. Return its contents as a property list.
  1177. The function `plist-get' may be used to retrieve specific elements
  1178. from the result.
  1179. Example:
  1180. (plist-get (org-index-get-line 'ref \"R12\") 'count)
  1181. retrieves the value of the count-column for reference number 12.
  1182. Argument COLUMN is a symbol, either ref or id,
  1183. argument VALUE specifies the value to search for."
  1184. ;; check arguments
  1185. (unless (memq column '(ref id keywords 'yank))
  1186. (error "Argument column can only be 'ref', 'id', 'keywords' or 'yank'"))
  1187. (unless value
  1188. (error "Need a value to search for"))
  1189. (org-index--verify-id)
  1190. (org-index--parse-table)
  1191. (org-index--get-line column value))
  1192. (defun org-index--get-line (column value)
  1193. "Find a line by ID, return its contents.
  1194. Argument COLUMN and VALUE specify line to get."
  1195. (let (content)
  1196. (org-index--on
  1197. column value
  1198. (mapc (lambda (x)
  1199. (if (and (numberp (cdr x))
  1200. (> (cdr x) 0))
  1201. (setq content (cons (car x) (cons (or (org-index--get-or-set-field (car x)) "") content)))))
  1202. (reverse org-index--columns)))
  1203. content))
  1204. (defun org-index--ref-from-id (id)
  1205. "Get reference from line ID."
  1206. (org-index--on 'id id (org-index--get-or-set-field 'ref)))
  1207. (defun org-index--id-from-ref (ref)
  1208. "Get id from line REF."
  1209. (org-index--on 'ref ref (org-index--get-or-set-field 'id)))
  1210. (defun org-index--get-fingerprint ()
  1211. "Get fingerprint of current line."
  1212. (replace-regexp-in-string
  1213. "\\s " ""
  1214. (mapconcat (lambda (x) (org-index--get-or-set-field x)) '(id ref yank keywords created) "")))
  1215. (defun org-index--read-search-for-index ()
  1216. "Special input routine for command index."
  1217. ;; Accept single char commands or switch to reading a sequence of digits
  1218. (let (char prompt search-ref search-id search-fingerprint)
  1219. ;; start with short prompt but give more help on next iteration
  1220. (setq prompt "Please specify, where to go in index (0-9,.,space,backspace,return or ? for short help) - ")
  1221. ;; read one character
  1222. (while (not (memq char (append (number-sequence ?0 ?9) (list ?\d ?\b ?\r ?\j ?\s ?.))))
  1223. (setq char (read-char prompt))
  1224. (setq prompt "Go to specific position in index table. Digits specify a reference number, <space> goes to top of index, <backspace> or <delete> to last line created and <return> or `.' to index line of current node. Please choose - "))
  1225. (if (memq char (number-sequence ?0 ?9))
  1226. ;; read rest of digits
  1227. (setq search-ref (read-from-minibuffer "Search reference number: " (char-to-string char))))
  1228. ;; decode single chars
  1229. (if (memq char '(?\r ?\n ?.)) (setq search-id (org-id-get)))
  1230. (if (memq char '(?\d ?\b)) (setq search-fingerprint org-index--last-fingerprint))
  1231. (list search-ref search-id search-fingerprint)))
  1232. (defun org-index--verify-id ()
  1233. "Check, that we have a valid id."
  1234. ;; Check id
  1235. (unless org-index-id
  1236. (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")))
  1237. (if (string= answer "create-index")
  1238. (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet.")
  1239. (describe-function 'org-index)
  1240. (throw 'new-index nil))))
  1241. ;; Find node
  1242. (let (marker)
  1243. (setq marker (org-id-find org-index-id 'marker))
  1244. (unless marker (org-index--create-missing-index "Cannot find the node with id \"%s\" (as specified by variable org-index-id)." org-index-id))
  1245. ; Try again with new node
  1246. (setq marker (org-id-find org-index-id 'marker))
  1247. (unless marker (error "Could not create node"))
  1248. (setq org-index--buffer (marker-buffer marker)
  1249. org-index--point (marker-position marker))
  1250. (move-marker marker nil)))
  1251. (defun org-index--retrieve-context ()
  1252. "Collect context information before starting with command."
  1253. ;; Get the content of the active region or the word under cursor
  1254. (setq org-index--active-region
  1255. (if (and transient-mark-mode mark-active)
  1256. (buffer-substring (region-beginning) (region-end))
  1257. nil))
  1258. (setq org-index--below-cursor (thing-at-point 'symbol))
  1259. ;; get category of current node
  1260. (setq org-index--category-before
  1261. (save-excursion ; workaround: org-get-category does not give category when at end of buffer
  1262. (beginning-of-line)
  1263. (org-get-category (point) t)))
  1264. ;; Find out, if we are within index table or occur buffer
  1265. (setq org-index--within-index-node (string= (org-id-get) org-index-id))
  1266. (setq org-index--within-occur (string= (buffer-name) org-index--occur-buffer-name)))
  1267. (defun org-index--parse-table (&optional num-lines-to-format check-sort-mixed)
  1268. "Parse content of index table.
  1269. Optional argument NUM-LINES-TO-FORMAT limits formatting effort and duration.
  1270. Optional argument CHECK-SORT-MIXED triggers resorting if mixed and stale."
  1271. (let (initial-point
  1272. end-of-headings
  1273. start-of-headings)
  1274. (unless num-lines-to-format (setq num-lines-to-format 0))
  1275. (with-current-buffer org-index--buffer
  1276. (setq initial-point (point))
  1277. (org-index--go-below-hline)
  1278. (org-reveal)
  1279. ;; if table is sorted mixed and it was sorted correctly yesterday, it could still be wrong today; so check
  1280. (when (and check-sort-mixed (eq org-index-sort-by 'mixed))
  1281. (goto-char org-index--below-hline)
  1282. (let (count-first-line count-second-line)
  1283. (setq count-first-line (string-to-number (concat (org-index--get-or-set-field 'count) " 0")))
  1284. (forward-line)
  1285. (setq count-second-line (string-to-number (concat (org-index--get-or-set-field 'count) " 0")))
  1286. (forward-line -1)
  1287. (if (and (string< (org-index--get-or-set-field 'last-accessed)
  1288. (org-index--get-mixed-time))
  1289. (< count-first-line count-second-line))
  1290. (org-index--do-sort-index org-index-sort-by)))
  1291. (org-index--go-below-hline))
  1292. ;; align and fontify table once for this emacs session
  1293. (when (> num-lines-to-format org-index--aligned)
  1294. (org-index--go-below-hline)
  1295. (message "Aligning and fontifying %s lines of index table (once per emacs session)..."
  1296. (if (= num-lines-to-format most-positive-fixnum) "all" (format "%d" num-lines-to-format)))
  1297. (save-restriction
  1298. (let (from to)
  1299. (forward-line -3)
  1300. (setq from (point))
  1301. (setq to (org-table-end))
  1302. (when (< num-lines-to-format most-positive-fixnum)
  1303. (forward-line (+ 3 num-lines-to-format))
  1304. (narrow-to-region from (point))
  1305. (setq to (min (point) to)))
  1306. (goto-char org-index--below-hline)
  1307. (org-table-align)
  1308. (setq to (min (point-max) to))
  1309. (font-lock-fontify-region from to)))
  1310. (setq org-index--aligned num-lines-to-format)
  1311. (org-index--go-below-hline)
  1312. (message "Done."))
  1313. (beginning-of-line)
  1314. ;; get headings to display during occur
  1315. (setq end-of-headings (point))
  1316. (goto-char (org-table-begin))
  1317. (setq start-of-headings (point))
  1318. (setq org-index--headings-visible (substring-no-properties (org-index--copy-visible start-of-headings end-of-headings)))
  1319. (setq org-index--headings (buffer-substring start-of-headings end-of-headings))
  1320. ;; count columns
  1321. (org-table-goto-column 100)
  1322. (setq org-index--numcols (- (org-table-current-column) 1))
  1323. ;; go to top of table
  1324. (goto-char (org-table-begin))
  1325. ;; parse line of headings
  1326. (org-index--parse-headings)
  1327. ;; read property or go through table to find maximum number
  1328. (goto-char org-index--below-hline)
  1329. (setq max-ref-field (or (org-entry-get org-index--point "max-ref")
  1330. (org-index--migrate-maxref-to-property)))
  1331. (unless org-index--head (org-index--get-decoration-from-ref-field max-ref-field))
  1332. ;; Get ids of focused node (if any)
  1333. (setq org-index--ids-focused-nodes (split-string (or (org-entry-get nil "ids-focused-nodes") "")))
  1334. (org-entry-delete (point) "id-focused-node") ; migrate (kind of) from previous versions
  1335. ;; save position below hline
  1336. (org-index--go-below-hline)
  1337. ;; go back to initial position
  1338. (goto-char initial-point))))
  1339. (defun org-index--get-decoration-from-ref-field (ref-field)
  1340. "Extract decoration from a REF-FIELD."
  1341. (unless (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\([^0-9]*\\)$" ref-field)
  1342. (org-index--report-index-error
  1343. "Reference in index table ('%s') does not contain a number" ref-field))
  1344. ;; These are the decorations used within the first ref of index
  1345. (setq org-index--head (match-string 1 ref-field))
  1346. (setq org-index--tail (match-string 3 ref-field))
  1347. (setq org-index--ref-regex (concat (regexp-quote org-index--head)
  1348. "\\([0-9]+\\)"
  1349. (regexp-quote org-index--tail)))
  1350. (setq org-index--ref-format (concat org-index--head "%d" org-index--tail)))
  1351. (defun org-index--extract-refnum (ref-field)
  1352. "Extract the number from a complete reference REF-FIELD like 'R102'."
  1353. (unless (string-match org-index--ref-regex ref-field)
  1354. (org-index--report-index-error
  1355. "Reference '%s' is not formatted properly (does not match '%s')" ref-field org-index--ref-regex))
  1356. (string-to-number (match-string 1 ref-field)))
  1357. (defun org-index--migrate-maxref-to-property ()
  1358. "One-time migration: No property; need to go through whole table once to find max."
  1359. (org-index--go-below-hline)
  1360. (let ((max-ref-num 0)
  1361. ref-field ref-num ref)
  1362. (message "One-time migration to set index-property maxref...")
  1363. (while (org-at-table-p)
  1364. (setq ref-field (org-index--get-or-set-field 'ref))
  1365. (when ref-field
  1366. (unless org-index--head (org-index--get-decoration-from-ref-field ref-field))
  1367. (setq ref-num (org-index--extract-refnum ref-field))
  1368. (if (> ref-num max-ref-num) (setq max-ref-num ref-num)))
  1369. (forward-line))
  1370. (unless (> max-ref-num 0)
  1371. (org-index--report-index-error "No reference found in property max-ref and none in index"))
  1372. (setq ref-field (format org-index--ref-format max-ref-num))
  1373. (org-index--go-below-hline)
  1374. (org-entry-put org-index--point "max-ref" ref-field)
  1375. (message "Done.")
  1376. ref-field))
  1377. (defun org-index--get-save-maxref (&optional no-inc)
  1378. "Get next reference, increment number and store it in index.
  1379. Optional argument NO-INC skips automatic increment on maxref."
  1380. (let (ref-field)
  1381. (with-current-buffer org-index--buffer
  1382. (setq ref-field (org-entry-get org-index--point "max-ref"))
  1383. (unless no-inc
  1384. (setq ref-field (format org-index--ref-format (1+ (org-index--extract-refnum ref-field))))
  1385. (org-entry-put org-index--point "max-ref" ref-field)))
  1386. ref-field))
  1387. (defun org-index--refresh-parse-table ()
  1388. "Fast refresh of selected results of parsing index table."
  1389. (setq org-index--point (marker-position (org-id-find org-index-id 'marker)))
  1390. (with-current-buffer org-index--buffer
  1391. (save-excursion
  1392. (org-index--go-below-hline))))
  1393. (defun org-index--do-maintain ()
  1394. "Choose among and perform some tasks to maintain index."
  1395. (let ((check-what) (max-mini-window-height 1.0) message-text)
  1396. (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 (ref or id)\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")))
  1397. (message nil)
  1398. (cond
  1399. ((eq check-what 'check)
  1400. (setq message-text (or (org-index--check-ids)
  1401. "No problems found")))
  1402. ((eq check-what 'statistics)
  1403. (setq message-text (org-index--do-statistics)))
  1404. ((eq check-what 'duplicates)
  1405. (setq message-text (org-index--find-duplicates)))
  1406. ((eq check-what 'clean)
  1407. (let ((lines 0))
  1408. (org-map-entries
  1409. (lambda ()
  1410. (when (org-entry-get (point) "org-index-ref")
  1411. (cl-incf lines)
  1412. (org-entry-delete (point) "org-index-ref")))
  1413. nil 'agenda)
  1414. (setq message-text (format "Removed property 'org-index-ref' from %d lines" lines))))
  1415. ((eq check-what 'update)
  1416. (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 invoking `add'. Are you SURE to proceed for ALL INDEX LINES ? ")
  1417. (setq message-text (org-index--update-all-lines))
  1418. (setq message-text "Canceled."))))
  1419. message-text))
  1420. (defun org-index--get-mixed-time ()
  1421. "Get timestamp for sorting order mixed."
  1422. (format-time-string
  1423. (org-time-stamp-format t t)
  1424. (apply 'encode-time (append '(0 0 0) (nthcdr 3 (decode-time))))))
  1425. (defun org-index--do-sort-index (sort)
  1426. "Sort index table according to SORT."
  1427. (let ((is-modified (buffer-modified-p))
  1428. top
  1429. bottom
  1430. mixed-time)
  1431. (unless buffer-read-only
  1432. (message "Sorting index table for %s..." (symbol-name sort))
  1433. (undo-boundary)
  1434. (let ((message-log-max nil)) ; we have just issued a message, dont need those of sort-subr
  1435. ;; if needed for mixed sort
  1436. (if (eq sort 'mixed)
  1437. (setq mixed-time (org-index--get-mixed-time)))
  1438. ;; get boundaries of table
  1439. (org-index--go-below-hline)
  1440. (forward-line 0)
  1441. (setq top (point))
  1442. (goto-char (org-table-end))
  1443. ;; kill all empty rows at bottom
  1444. (while (progn
  1445. (forward-line -1)
  1446. (org-table-goto-column 1)
  1447. (and
  1448. (not (org-index--get-or-set-field 'ref))
  1449. (not (org-index--get-or-set-field 'id))
  1450. (not (org-index--get-or-set-field 'yank))))
  1451. (org-table-kill-row))
  1452. (forward-line 1)
  1453. (setq bottom (point))
  1454. ;; sort lines
  1455. (save-restriction
  1456. (narrow-to-region top bottom)
  1457. (goto-char top)
  1458. (sort-subr t
  1459. 'forward-line
  1460. 'end-of-line
  1461. (lambda ()
  1462. (org-index--get-sort-key sort t mixed-time))
  1463. nil
  1464. 'string<)
  1465. (goto-char (point-min))
  1466. ;; restore modification state
  1467. (set-buffer-modified-p is-modified)))
  1468. (setq org-index--last-sort-assumed sort))))
  1469. (defun org-index--do-sort-lines (what)
  1470. "Sort lines in WHAT according to contained reference."
  1471. (save-restriction
  1472. (cond
  1473. ((eq what 'region)
  1474. (if (region-active-p)
  1475. (narrow-to-region (region-beginning) (region-end))
  1476. (error "No active region, cannot sort")))
  1477. ((eq what 'buffer)
  1478. (unless (y-or-n-p "Sort whole current buffer ? ")
  1479. (error "Canceled"))
  1480. (narrow-to-region (point-min) (point-max))))
  1481. (goto-char (point-min))
  1482. (sort-subr nil 'forward-line 'end-of-line
  1483. (lambda ()
  1484. (if (looking-at (concat ".*"
  1485. (org-index--make-guarded-search org-index--ref-regex 'dont-quote)))
  1486. (string-to-number (match-string 1))
  1487. 0)))))
  1488. (defun org-index--go-below-hline ()
  1489. "Move below hline in index-table."
  1490. (let ((errstring (format "index table within node %s" org-index-id)))
  1491. (goto-char org-index--point)
  1492. ;; go to heading of node
  1493. (while (not (org-at-heading-p)) (forward-line -1))
  1494. (forward-line 1)
  1495. ;; go to first table, but make sure we do not get into another node
  1496. (while (and (not (org-at-table-p))
  1497. (not (org-at-heading-p))
  1498. (not (eobp)))
  1499. (forward-line))
  1500. ;; check, if there really is a table
  1501. (unless (org-at-table-p)
  1502. (org-index--create-missing-index "Cannot find %s." errstring))
  1503. ;; go just after hline
  1504. (while (and (not (org-at-table-hline-p))
  1505. (org-at-table-p))
  1506. (forward-line))
  1507. (forward-line)
  1508. ;; and check
  1509. (unless (org-at-table-p)
  1510. (org-index--report-index-error "Cannot find a hline within %s" errstring))
  1511. (org-table-goto-column 1)
  1512. (setq org-index--below-hline (point))))
  1513. (defun org-index--parse-headings ()
  1514. "Parse headings of index table."
  1515. (let (field ;; field content
  1516. field-symbol) ;; and as a symbol
  1517. (setq org-index--columns nil)
  1518. ;; For each column
  1519. (dotimes (col org-index--numcols)
  1520. (setq field (substring-no-properties (downcase (org-trim (org-table-get-field (+ col 1))))))
  1521. (if (string= field "")
  1522. (error "Heading of column cannot be empty"))
  1523. (if (and (not (string= (substring field 0 1) "."))
  1524. (not (member (intern field) org-index--valid-headings)))
  1525. (error "Column name '%s' is not a valid heading (custom headings may start with a dot, e.g. '.foo')" field))
  1526. (setq field-symbol (intern field))
  1527. ;; check if heading has already appeared
  1528. (if (assoc field-symbol org-index--columns)
  1529. (org-index--report-index-error
  1530. "'%s' appears two times as column heading" (downcase field))
  1531. ;; add it to list at front, reverse later
  1532. (setq org-index--columns (cons (cons field-symbol (+ col 1)) org-index--columns)))))
  1533. (setq org-index--columns (reverse org-index--columns))
  1534. ;; check if all necessary headings have appeared
  1535. (mapc (lambda (head)
  1536. (unless (cdr (assoc head org-index--columns))
  1537. (org-index--report-index-error "No column has heading '%s'" head)))
  1538. org-index--valid-headings))
  1539. (defun org-index--create-missing-index (&rest reasons)
  1540. "Create a new empty index table with detailed explanation. Argument REASONS explains why."
  1541. (org-index--ask-before-create-index "Cannot find index table: "
  1542. "new permanent" "."
  1543. reasons)
  1544. (org-index--create-index))
  1545. (defun org-index--report-index-error (&rest reasons)
  1546. "Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with."
  1547. (when org-index--buffer
  1548. (pop-to-buffer-same-window org-index--buffer)
  1549. (goto-char org-index--below-hline)
  1550. (org-reveal t))
  1551. (org-index--ask-before-create-index "The existing index contains this error: "
  1552. "temporary" ", to compare with."
  1553. reasons)
  1554. (org-index--create-index t t))
  1555. (defun org-index--ask-before-create-index (explanation type for-what reasons)
  1556. ; checkdoc-params: (explanation type for-what reasons)
  1557. "Ask the user before creating an index or throw error. Arguments specify bits of issued message."
  1558. (let (reason prompt)
  1559. (setq reason (apply 'format reasons))
  1560. (setq prompt (concat explanation reason "\n"
  1561. "However, this assistant can help you to create a "
  1562. type " index with detailed comments" for-what "\n\n"
  1563. "Do you want to proceed ?"))
  1564. (unless (let ((max-mini-window-height 1.0))
  1565. (y-or-n-p prompt))
  1566. (error (concat explanation reason)))))
  1567. (defun org-index--create-index (&optional temporary compare)
  1568. "Create a new empty index table with detailed explanation.
  1569. specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index."
  1570. (let (buffer
  1571. title
  1572. firstref
  1573. id)
  1574. (if temporary
  1575. (let ((file-name (concat temporary-file-directory "org-index--example-index.org"))
  1576. (buffer-name "*org-index-example-index*"))
  1577. (setq buffer (get-buffer-create buffer-name))
  1578. (with-current-buffer buffer
  1579. ;; but it needs a file for its index to be found
  1580. (unless (string= (buffer-file-name) file-name)
  1581. (set-visited-file-name file-name))
  1582. (rename-buffer buffer-name) ; name is change by line above
  1583. (erase-buffer)
  1584. (org-mode)))
  1585. (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))))))
  1586. (setq title (read-from-minibuffer "Please enter the title of the index node (leave empty for default 'index'): "))
  1587. (if (string= title "") (setq title "index"))
  1588. (while (progn
  1589. (setq firstref (read-from-minibuffer "Please enter your first reference-number. This is an integer number preceeded by some and optionally followed by some non-numeric 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 frequently within your existing notes, to avoid too many false hits when searching.\n\nPlease choose (leave empty for default 'R1'): "))
  1590. (if (string= firstref "") (setq firstref "R1"))
  1591. (let (desc)
  1592. (when (string-match "[[:blank:]]" firstref)
  1593. (setq desc "Contains whitespace"))
  1594. (when (string-match "[[:cntrl:]]" firstref)
  1595. (setq desc "Contains control characters"))
  1596. (unless (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)
  1597. ;; firstref not okay, report details
  1598. (setq desc
  1599. (cond ((string= firstref "") "is empty")
  1600. ((not (string-match "^[^0-9]+" firstref)) "starts with a digit")
  1601. ((not (string-match "^[^0-9]+[0-9]+" firstref)) "does not contain a number")
  1602. ((not (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)) "contains more than one sequence of digits")
  1603. )))
  1604. (if desc
  1605. (progn
  1606. (read-from-minibuffer (format "Your input '%s' does not meet the requirements because it %s.\nPlease hit RET and try again: " firstref desc))
  1607. t)
  1608. nil))))
  1609. (with-current-buffer buffer
  1610. (goto-char (point-max))
  1611. (insert (format "* %s %s\n" firstref title))
  1612. (org-entry-put org-index--point "max-ref" firstref)
  1613. (if temporary
  1614. (insert "
  1615. Below you find your temporary index table, which WILL NOT LAST LONGER
  1616. THAN YOUR CURRENT EMACS SESSION; please use it only for evaluation.
  1617. ")
  1618. (insert "
  1619. Below you find your initial index table, which will grow over time.
  1620. "))
  1621. (insert " You may start using it by adding some lines. Just
  1622. move to another heading within org, invoke `org-index' and
  1623. choose the command 'add'. After adding a few nodes, try the
  1624. command 'occur' to search among them.
  1625. To gain further insight you may invoke the subcommand 'help', or
  1626. (same content) read the help of `org-index'.
  1627. Within the index table below, the sequence of columns does not
  1628. matter. You may reorder them in any way you like. You may also
  1629. add your own columns, which should start with a dot
  1630. (e.g. '.my-column').
  1631. Invoke `org-customize' to tweak the behaviour of org-index
  1632. (see the group org-index).
  1633. This node needs not be a top level node; its name is completely
  1634. at your choice; it is found through its ID only.
  1635. ")
  1636. (unless temporary
  1637. (insert "
  1638. Remark: These lines of explanation can be removed at any time.
  1639. "))
  1640. (setq id (org-id-get-create))
  1641. (insert (format "
  1642. | ref | category | keywords | tags | count | level | last-accessed | created | id | yank |
  1643. | | | | | | | | | <4> | <4> |
  1644. |-----+----------+----------+------+-------+-------+---------------+---------+-----+------|
  1645. | %s | | %s | | | | | %s | %s | |
  1646. "
  1647. firstref
  1648. title
  1649. (with-temp-buffer (org-insert-time-stamp nil nil t))
  1650. id))
  1651. ;; make sure, that node can be found
  1652. (org-id-add-location id (buffer-file-name))
  1653. (setq buffer-save-without-query t)
  1654. (basic-save-buffer)
  1655. (while (not (org-at-table-p)) (forward-line -1))
  1656. (unless buffer-read-only (org-table-align))
  1657. (while (not (org-at-heading-p)) (forward-line -1))
  1658. ;; read back some info about new index
  1659. (let ((org-index-id id))
  1660. (org-index--verify-id))
  1661. ;; remember at least for this session
  1662. (setq org-index-id id)
  1663. ;; present results to user
  1664. (if temporary
  1665. (progn
  1666. ;; Present existing and temporary index together
  1667. (when compare
  1668. (pop-to-buffer-same-window org-index--buffer)
  1669. (goto-char org-index--point)
  1670. (org-index--unfold-buffer)
  1671. (delete-other-windows)
  1672. (select-window (split-window-vertically)))
  1673. ;; show new index
  1674. (pop-to-buffer-same-window buffer)
  1675. (org-id-goto id)
  1676. (org-index--unfold-buffer)
  1677. (if compare
  1678. (progn
  1679. (message "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
  1680. (throw 'new-index nil))
  1681. (message "This is your new temporary index, use command add to populate, occur to search.")))
  1682. (progn
  1683. ;; Only show the new index
  1684. (pop-to-buffer-same-window buffer)
  1685. (delete-other-windows)
  1686. (org-id-goto id)
  1687. (org-index--unfold-buffer)
  1688. (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 ? ")
  1689. (progn
  1690. (customize-save-variable 'org-index-id id)
  1691. (message "Saved org-index-id '%s' to %s." id (or custom-file
  1692. user-init-file))
  1693. (throw 'new-index nil))
  1694. (let (sq)
  1695. (setq sq (format "(setq org-index-id \"%s\")" id))
  1696. (kill-new sq)
  1697. (message "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)
  1698. (throw 'new-index nil))))))))
  1699. (defun org-index--unfold-buffer ()
  1700. "Helper function to unfold buffer."
  1701. (org-show-context)
  1702. (org-show-subtree)
  1703. (recenter 1)
  1704. (save-excursion
  1705. (org-back-to-heading)
  1706. (forward-line) ;; on property drawer
  1707. (org-cycle)))
  1708. (defun org-index--update-line (&optional id-or-pos no-error)
  1709. "Update columns count and last-accessed in line ID-OR-POS.
  1710. Optional argument NO-ERROR suppresses error."
  1711. (let (initial)
  1712. (with-current-buffer org-index--buffer
  1713. (unless buffer-read-only
  1714. (setq initial (point))
  1715. (if (if (integerp id-or-pos)
  1716. (goto-char id-or-pos)
  1717. (org-index--go 'id id-or-pos))
  1718. (org-index--update-current-line)
  1719. (unless no-error (error "Did not find reference or id '%s'" (list id-or-pos))))
  1720. (goto-char initial)))))
  1721. (defun org-index--update-current-line ()
  1722. "Update current lines columns count and last-accessed."
  1723. (let (newcount (count-field (org-index--get-or-set-field 'count)))
  1724. ;; update count field only if number or empty
  1725. (when (or (not count-field)
  1726. (string-match "^[0-9]+$" count-field))
  1727. (setq newcount (+ 1 (string-to-number (or count-field "0"))))
  1728. (org-index--get-or-set-field 'count
  1729. (number-to-string newcount)))
  1730. ;; update timestamp
  1731. (org-table-goto-column (org-index--column-num 'last-accessed))
  1732. (org-table-blank-field)
  1733. (org-insert-time-stamp nil t t)
  1734. ;; move line according to new content
  1735. (org-index--promote-current-line)
  1736. (org-index--align-and-fontify-current-line)))
  1737. (defun org-index--align-and-fontify-current-line (&optional num)
  1738. "Make current line (or NUM lines) blend well among others."
  1739. (let (lines lines-fontified)
  1740. ;; get current content
  1741. (unless num (setq num 1))
  1742. (setq lines (delete-and-extract-region (line-beginning-position) (line-end-position num)))
  1743. ;; create minimum table with fixed-width columns to align and fontify new line
  1744. (insert
  1745. (setq
  1746. lines-fontified
  1747. (with-temp-buffer
  1748. (org-set-font-lock-defaults)
  1749. (insert org-index--headings-visible)
  1750. ;; fill columns, so that aligning cannot shrink them
  1751. (goto-char (point-min))
  1752. (search-forward "|")
  1753. (while (search-forward " " (line-end-position) t)
  1754. (replace-match "." nil t))
  1755. (goto-char (point-min))
  1756. (while (search-forward ".|." (line-end-position) t)
  1757. (replace-match " | " nil t))
  1758. (goto-char (point-min))
  1759. (while (search-forward "|." (line-end-position) t)
  1760. (replace-match "| " nil t))
  1761. (goto-char (point-max))
  1762. (insert lines)
  1763. (forward-line 0)
  1764. (let ((start (point)))
  1765. (while (re-search-forward "^\s +|-" nil t)
  1766. (replace-match "| -"))
  1767. (goto-char start))
  1768. (org-mode)
  1769. (org-table-align)
  1770. (font-lock-fontify-region (point-min) (point-max))
  1771. (goto-char (point-max))
  1772. (if (eq -1 (skip-chars-backward "\n"))
  1773. (delete-char 1))
  1774. (forward-line (- 1 num))
  1775. (buffer-substring (line-beginning-position) (line-end-position num)))))
  1776. lines-fontified))
  1777. (defun org-index--promote-current-line ()
  1778. "Move current line up in table according to changed sort fields."
  1779. (let (begin end key
  1780. (to-skip 0))
  1781. (forward-line 0) ; stay at beginning of line
  1782. (setq key (org-index--get-sort-key))
  1783. (setq begin (point))
  1784. (setq end (line-beginning-position 2))
  1785. (forward-line -1)
  1786. (while (and (org-at-table-p)
  1787. (not (org-at-table-hline-p))
  1788. (string< (org-index--get-sort-key) key))
  1789. (cl-incf to-skip)
  1790. (forward-line -1))
  1791. (forward-line 1)
  1792. ;; insert line at new position
  1793. (when (> to-skip 0)
  1794. (insert (delete-and-extract-region begin end))
  1795. (forward-line -1))))
  1796. (defun org-index--get-sort-key (&optional sort with-ref mixed-time)
  1797. "Get value for sorting from column SORT, optional WITH-REF; if mixes use MIXED-TIME."
  1798. (let (ref
  1799. ref-field
  1800. key)
  1801. (unless sort (setq sort org-index--last-sort-assumed)) ; use default value
  1802. (when (or with-ref
  1803. (eq sort 'ref))
  1804. ;; get reference with leading zeroes, so it can be
  1805. ;; sorted as text
  1806. (setq ref-field (org-index--get-or-set-field 'ref))
  1807. (if ref-field
  1808. (progn
  1809. (string-match org-index--ref-regex ref-field)
  1810. (setq ref (format
  1811. "%06d"
  1812. (string-to-number
  1813. (match-string 1 ref-field)))))
  1814. (setq ref "000000")))
  1815. (setq key
  1816. (cond
  1817. ((eq sort 'count)
  1818. (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))
  1819. ((eq sort 'mixed)
  1820. (let ((last-accessed (org-index--get-or-set-field 'last-accessed)))
  1821. (unless mixed-time (setq mixed-time (org-index--get-mixed-time)))
  1822. (concat
  1823. (if (string< mixed-time last-accessed) last-accessed mixed-time)
  1824. (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))))
  1825. ((eq sort 'ref)
  1826. ref)
  1827. ((memq sort '(id last-accessed created))
  1828. (org-index--get-or-set-field sort))
  1829. (t (error "This is a bug: unmatched case '%s'" sort))))
  1830. (if with-ref (setq key (concat key ref)))
  1831. key))
  1832. (defun org-index--get-or-set-field (key &optional value)
  1833. "Retrieve field KEY from index table or set it to VALUE."
  1834. (let (field)
  1835. (save-excursion
  1836. (if (eq key 'fingerprint)
  1837. (progn
  1838. (if value (error "Internal error, pseudo-column fingerprint cannot be set"))
  1839. (setq field (org-index--get-fingerprint)))
  1840. (setq field (org-trim (org-table-get-field (cdr (assoc key org-index--columns)) value))))
  1841. (if (string= field "") (setq field nil))
  1842. (org-no-properties field))))
  1843. (defun org-index--column-num (key)
  1844. "Return number of column KEY."
  1845. (if (numberp key)
  1846. key
  1847. (cdr (assoc key org-index--columns))))
  1848. (defun org-index--make-guarded-search (ref &optional dont-quote)
  1849. "Make robust search string from REF; DONT-QUOTE it, if requested."
  1850. (concat "\\_<" (if dont-quote ref (regexp-quote ref)) "\\_>"))
  1851. (defun org-index--find-duplicates ()
  1852. "Find duplicate references or ids in index table."
  1853. (let (ref-duplicates id-duplicates)
  1854. (setq ref-duplicates (org-index--find-duplicates-helper 'ref))
  1855. (setq id-duplicates (org-index--find-duplicates-helper 'id))
  1856. (goto-char org-index--below-hline)
  1857. (if (or ref-duplicates id-duplicates)
  1858. (progn
  1859. ;; show results
  1860. (pop-to-buffer-same-window
  1861. (get-buffer-create "*org-index-duplicates*"))
  1862. (when ref-duplicates
  1863. (insert "These references appear more than once:\n")
  1864. (mapc (lambda (x) (insert " " x "\n")) ref-duplicates)
  1865. (insert "\n\n"))
  1866. (when id-duplicates
  1867. (insert "These ids appear more than once:\n")
  1868. (mapc (lambda (x) (insert " " x "\n")) id-duplicates))
  1869. "Some references or ids are duplicates")
  1870. "No duplicate references or ids found")))
  1871. (defun org-index--find-duplicates-helper (column)
  1872. "Helper for `org-index--find-duplicates': Go through table and count given COLUMN."
  1873. (let (counts duplicates field found)
  1874. ;; go through table
  1875. (goto-char org-index--below-hline)
  1876. (while (org-at-table-p)
  1877. ;; get column
  1878. (setq field (org-index--get-or-set-field column))
  1879. ;; and increment
  1880. (setq found (assoc field counts))
  1881. (if found
  1882. (cl-incf (cdr found))
  1883. (setq counts (cons (cons field 1) counts)))
  1884. (forward-line))
  1885. (mapc (lambda (x) (if (and (> (cdr x) 1)
  1886. (car x))
  1887. (setq duplicates (cons (car x) duplicates)))) counts)
  1888. duplicates))
  1889. (defun org-index--do-statistics ()
  1890. "Compute statistics about index table."
  1891. (let ((total-lines 0) (total-refs 0)
  1892. ref ref-field min max message)
  1893. ;; go through table
  1894. (goto-char org-index--below-hline)
  1895. (while (org-at-table-p)
  1896. ;; get ref
  1897. (setq ref-field (org-index--get-or-set-field 'ref))
  1898. (when ref-field
  1899. (string-match org-index--ref-regex ref-field)
  1900. (setq ref (string-to-number (match-string 1 ref-field)))
  1901. ;; record min and max
  1902. (if (or (not min) (< ref min)) (setq min ref))
  1903. (if (or (not max) (> ref max)) (setq max ref))
  1904. (setq total-refs (1+ total-refs)))
  1905. ;; count
  1906. (setq total-lines (1+ total-lines))
  1907. (forward-line))
  1908. (setq message (format "%d Lines in index table. First reference is %s, last %s; %d of them are used (%d percent)"
  1909. total-lines
  1910. (format org-index--ref-format min)
  1911. (format org-index--ref-format max)
  1912. total-refs
  1913. (truncate (* 100 (/ (float total-refs) (1+ (- max min)))))))
  1914. (goto-char org-index--below-hline)
  1915. message))
  1916. (defun org-index--do-add-or-update (&optional create-ref tag-with-ref)
  1917. "For current node or current line in index, add or update in index table.
  1918. CREATE-REF and TAG-WITH-REF if given."
  1919. (let* (id id-from-index ref args yank ret)
  1920. (org-index--save-positions)
  1921. (unless (or org-index--within-index-node
  1922. org-index--within-occur)
  1923. (org-back-to-heading))
  1924. ;; try to do the same things from within index and from outside
  1925. (if org-index--within-index-node
  1926. (progn
  1927. (unless (org-at-table-p)
  1928. (error "Within index node but not on table"))
  1929. (setq id (org-index--get-or-set-field 'id))
  1930. (setq ref (org-index--get-or-set-field 'ref))
  1931. (setq args (org-index--collect-values-for-add-update-remote id))
  1932. (org-index--write-fields args)
  1933. (setq yank (org-index--get-or-set-field org-index-yank-after-add))
  1934. (setq ret
  1935. (if ref
  1936. (cons (format "Updated index line %s" ref) yank)
  1937. (cons "Updated index line" nil))))
  1938. (setq id (org-id-get-create))
  1939. (org-index--refresh-parse-table)
  1940. (setq id-from-index (org-index--on 'id id id))
  1941. (setq ref (org-index--on 'id id (org-index--get-or-set-field 'ref)))
  1942. (if tag-with-ref
  1943. (org-toggle-tag (format "%s%d%s" org-index--head tag-with-ref org-index--tail) 'on))
  1944. (setq args (org-index--collect-values-for-add-update id))
  1945. (when (and create-ref
  1946. (not ref))
  1947. (setq ref (org-index--get-save-maxref))
  1948. (setq args (plist-put args 'ref ref)))
  1949. (if id-from-index
  1950. ;; already have an id in index, find it and update fields
  1951. (progn
  1952. (org-index--on
  1953. 'id id
  1954. (org-index--write-fields args)
  1955. (setq yank (org-index--get-or-set-field org-index-yank-after-add)))
  1956. (setq ret
  1957. (if ref
  1958. (cons (format "Updated index line %s" ref) yank)
  1959. (cons "Updated index line" nil))))
  1960. ;; no id here, create new line in index
  1961. (if ref (setq args (plist-put args 'ref ref)))
  1962. (setq yank (apply 'org-index--do-new-line args))
  1963. (setq ret
  1964. (if ref
  1965. (cons
  1966. (format "Added new index line %s" ref)
  1967. (concat yank " "))
  1968. (cons
  1969. "Added new index line"
  1970. nil)))))
  1971. (org-index--restore-positions)
  1972. ret))
  1973. (defun org-index--check-ids ()
  1974. "Check, that ids really point to a node."
  1975. (let ((lines 0)
  1976. id ids marker)
  1977. (goto-char org-index--below-hline)
  1978. (catch 'problem
  1979. (while (org-at-table-p)
  1980. (when (setq id (org-index--get-or-set-field 'id))
  1981. ;; check for double ids
  1982. (when (member id ids)
  1983. (org-table-goto-column (org-index--column-num 'id))
  1984. (throw 'problem "This id appears twice in index; please use command 'maintain' to check for duplicate ids"))
  1985. (cl-incf lines)
  1986. (setq ids (cons id ids))
  1987. ;; check, if id is valid
  1988. (setq marker (org-id-find id t))
  1989. (unless marker
  1990. (org-table-goto-column (org-index--column-num 'id))
  1991. (throw 'problem "This id cannot be found")))
  1992. (forward-line))
  1993. (goto-char org-index--below-hline)
  1994. nil)))
  1995. (defun org-index--update-all-lines ()
  1996. "Update all lines of index at once."
  1997. (let ((lines 0)
  1998. id kvs)
  1999. ;; check for double ids
  2000. (or
  2001. (org-index--check-ids)
  2002. (progn
  2003. (goto-char org-index--below-hline)
  2004. (while (org-at-table-p)
  2005. ;; update single line
  2006. (when (setq id (org-index--get-or-set-field 'id))
  2007. (setq kvs (org-index--collect-values-for-add-update-remote id))
  2008. (org-index--write-fields kvs)
  2009. (cl-incf lines))
  2010. (forward-line))
  2011. (goto-char org-index--below-hline)
  2012. (org-table-align)
  2013. (format "Updated %d lines" lines)))))
  2014. (defun org-index--collect-values-for-add-update (id &optional silent category)
  2015. "Collect values for adding or updating line specified by ID, do not ask if SILENT, use CATEGORY, if given."
  2016. (let ((args (list 'id id))
  2017. content)
  2018. (dolist (col (mapcar 'car org-index--columns))
  2019. (setq content "")
  2020. (cond
  2021. ((eq col 'keywords)
  2022. (if org-index-copy-heading-to-keywords
  2023. (setq content (nth 4 (org-heading-components))))
  2024. ;; Shift ref and timestamp ?
  2025. (if org-index-strip-ref-and-date-from-heading
  2026. (dotimes (_i 2)
  2027. (if (or (string-match (concat "^\\s-*" org-index--ref-regex) content)
  2028. (string-match (concat "^\\s-*" org-ts-regexp-both) content))
  2029. (setq content (substring content (match-end 0)))))))
  2030. ((eq col 'category)
  2031. (setq content (or category org-index--category-before)))
  2032. ((eq col 'level)
  2033. (setq content (number-to-string (org-outline-level))))
  2034. ((eq col 'tags)
  2035. (setq content (org-get-tags-string))))
  2036. (unless (string= content "")
  2037. (setq args (plist-put args col content))))
  2038. (if (not silent)
  2039. (let ((args-edited (org-index--collect-values-from-user org-index-edit-on-add args)))
  2040. (setq args (append args-edited args))))
  2041. args))
  2042. (defun org-index--collect-values-for-add-update-remote (id)
  2043. "Wrap `org-index--collect-values-for-add-update' by prior moving to remote node identified by ID."
  2044. (let (marker point args)
  2045. (setq marker (org-id-find id t))
  2046. ;; enter buffer and collect information
  2047. (with-current-buffer (marker-buffer marker)
  2048. (setq point (point))
  2049. (goto-char marker)
  2050. (setq args (org-index--collect-values-for-add-update id t (org-get-category (point) t)))
  2051. (goto-char point))
  2052. args))
  2053. (defun org-index--collect-values-from-user (cols &optional defaults)
  2054. "Collect values for adding a new line.
  2055. Argument COLS gives list of columns to edit.
  2056. Optional argument DEFAULTS gives default values."
  2057. (let (content args)
  2058. (dolist (col cols)
  2059. (setq content "")
  2060. (setq content (read-from-minibuffer
  2061. (format "Enter text for column '%s': " (symbol-name col))
  2062. (plist-get col defaults)))
  2063. (unless (string= content "")
  2064. (setq args (plist-put args col content))))
  2065. args))
  2066. (defun org-index--write-fields (kvs)
  2067. "Update current line with values from KVS (keys-values)."
  2068. (while kvs
  2069. (org-index--get-or-set-field (car kvs) (org-trim (cadr kvs)))
  2070. (setq kvs (cddr kvs))))
  2071. (defun org-index--do-kill ()
  2072. "Perform command kill from within occur, index or node."
  2073. (let (id ref chars-deleted-index text-deleted-from pos-in-index)
  2074. (org-index--save-positions)
  2075. (unless (or org-index--within-index-node
  2076. org-index--within-occur)
  2077. (org-back-to-heading))
  2078. ;; Collect information: What should be deleted ?
  2079. (if (or org-index--within-occur
  2080. org-index--within-index-node)
  2081. (progn
  2082. (if org-index--within-index-node
  2083. ;; In index
  2084. (setq pos-in-index (point))
  2085. ;; In occur
  2086. (setq pos-in-index (get-text-property (point) 'org-index-lbp))
  2087. (org-index--occur-test-stale pos-in-index)
  2088. (set-buffer org-index--buffer)
  2089. (goto-char pos-in-index))
  2090. ;; In Index (maybe moved there)
  2091. (setq id (org-index--get-or-set-field 'id))
  2092. (setq ref (org-index--get-or-set-field 'ref)))
  2093. ;; At a headline
  2094. (setq id (org-entry-get (point) "ID"))
  2095. (setq ref (org-index--ref-from-id id))
  2096. (setq pos-in-index (org-index--on 'id id (point)))
  2097. (unless pos-in-index (error "This node is not in index")))
  2098. ;; Remark: Current buffer is not certain here, but we have all the information to delete
  2099. ;; Delete from node
  2100. (when id
  2101. (let ((m (org-id-find id 'marker)))
  2102. (set-buffer (marker-buffer m))
  2103. (goto-char m)
  2104. (move-marker m nil)
  2105. (unless (string= (org-id-get) id)
  2106. (error "Could not find node with id %s" id)))
  2107. (org-index--delete-any-ref-from-tags)
  2108. (if ref (org-index--delete-ref-from-heading ref))
  2109. (setq text-deleted-from (cons "node" text-deleted-from)))
  2110. ;; Delete from index
  2111. (set-buffer org-index--buffer)
  2112. (unless pos-in-index "Internal error, pos-in-index should be defined here")
  2113. (goto-char pos-in-index)
  2114. (setq chars-deleted-index (length (delete-and-extract-region (line-beginning-position) (line-beginning-position 2))))
  2115. (setq text-deleted-from (cons "index" text-deleted-from))
  2116. ;; Delete from occur only if we started there, accept that it will be stale otherwise
  2117. (if org-index--within-occur
  2118. (let ((inhibit-read-only t))
  2119. (set-buffer org-index--occur-buffer-name)
  2120. (delete-region (line-beginning-position) (line-beginning-position 2))
  2121. ;; correct positions
  2122. (while (org-at-table-p)
  2123. (put-text-property (line-beginning-position) (line-end-position) 'org-index-lbp
  2124. (- (get-text-property (point) 'org-index-lbp) chars-deleted-index))
  2125. (forward-line))
  2126. (setq text-deleted-from (cons "occur" text-deleted-from))))
  2127. (org-index--restore-positions)
  2128. (concat "Deleted from: " (mapconcat 'identity (sort text-deleted-from 'string<) ","))))
  2129. (defun org-index--save-positions ()
  2130. "Save current buffer and positions in index- and current buffer; not in occur-buffer."
  2131. (let (cur-buf cur-mrk idx-pnt idx-mrk)
  2132. (setq cur-buf (current-buffer))
  2133. (setq cur-mrk (point-marker))
  2134. (set-buffer org-index--buffer)
  2135. (if (string= (org-id-get) org-index-id)
  2136. (setq idx-pnt (point))
  2137. (setq idx-mrk (point-marker)))
  2138. (set-buffer cur-buf)
  2139. (setq org-index--saved-positions (list cur-buf cur-mrk idx-pnt idx-mrk))))
  2140. (defun org-index--restore-positions ()
  2141. "Restore positions as saved by `org-index--save-positions'."
  2142. (cl-multiple-value-bind
  2143. (cur-buf cur-mrk idx-pnt idx-mrk buf)
  2144. org-index--saved-positions
  2145. (setq buf (current-buffer))
  2146. (set-buffer cur-buf)
  2147. (goto-char cur-mrk)
  2148. (set-buffer org-index--buffer)
  2149. (goto-char (or idx-pnt idx-mrk))
  2150. (set-buffer buf))
  2151. (setq org-index--saved-positions nil))
  2152. (defun org-index--delete-ref-from-heading (ref)
  2153. "Delete given REF from current heading."
  2154. (save-excursion
  2155. (end-of-line)
  2156. (let ((end (point)))
  2157. (beginning-of-line)
  2158. (when (search-forward ref end t)
  2159. (delete-char (- (length ref)))
  2160. (just-one-space)))))
  2161. (defun org-index--delete-any-ref-from-tags ()
  2162. "Delete any reference from list of tags."
  2163. (let (new-tags)
  2164. (mapc (lambda (tag)
  2165. (unless (or (string-match org-index--ref-regex tag)
  2166. (string= tag ""))
  2167. (setq new-tags (cons tag new-tags))))
  2168. (org-get-tags))
  2169. (org-set-tags-to new-tags)))
  2170. (defun org-index--go (column value)
  2171. "Position cursor on index line where COLUMN equals VALUE.
  2172. Return t or nil, leave point on line or at top of table, needs to be in buffer initially."
  2173. (let (found)
  2174. (unless (eq (current-buffer) org-index--buffer)
  2175. (error "This is a bug: Not in index buffer"))
  2176. (unless value
  2177. (error "Cannot search for nil"))
  2178. (if (string= value "")
  2179. (error "Cannot search for empty string"))
  2180. (if (<= (length value) 2)
  2181. (warn "Searching for short string '%s' will be slow" value))
  2182. (goto-char org-index--below-hline)
  2183. (forward-line 0)
  2184. (save-restriction
  2185. (narrow-to-region (point) (org-table-end))
  2186. (while (and (not found)
  2187. (search-forward value nil t))
  2188. (setq found (string= value (org-index--get-or-set-field column)))))
  2189. ;; return value
  2190. (if found
  2191. t
  2192. (goto-char org-index--below-hline)
  2193. nil)))
  2194. (defun org-index--find-id (id &optional other)
  2195. "Perform command head: Find node with ID and present it.
  2196. If OTHER in separate window."
  2197. (let (message marker)
  2198. (setq marker (org-id-find id t))
  2199. (if marker
  2200. (progn
  2201. (org-index--update-line id)
  2202. (if other
  2203. (progn
  2204. (pop-to-buffer (marker-buffer marker)))
  2205. (pop-to-buffer-same-window (marker-buffer marker)))
  2206. (goto-char marker)
  2207. (org-reveal t)
  2208. (org-show-entry)
  2209. (recenter)
  2210. (unless (string= (org-id-get) id)
  2211. (setq message (format "Could not go to node with id %s (narrowed ?)" id)))
  2212. (setq message "Found headline"))
  2213. (setq message (format "Did not find node with %s" id)))
  2214. message))
  2215. (defun org-index--do-occur (&optional days)
  2216. "Perform command occur; optional narrow to DAYS back."
  2217. (let ((word "") ; last word to search for growing and shrinking on keystrokes
  2218. (prompt "Search for: ")
  2219. (these-commands " NOTE: If you invoke the subcommands edit (`e') or kill (`C-c i k') from within this buffer, the index is updated accordingly")
  2220. (lines-wanted (window-body-height))
  2221. words ; list words that should match
  2222. occur-buffer
  2223. begin ; position of first line
  2224. help-text ; cons with help text short and long
  2225. search-text ; description of text to search for
  2226. done ; true, if loop is done
  2227. in-c-backspace ; true, while processing C-backspace
  2228. help-overlay ; Overlay with help text
  2229. initial-frame ; Frame when starting occur
  2230. key ; input from user in various forms
  2231. key-sequence
  2232. key-sequence-raw
  2233. days-clause) ; clause to display for days back search
  2234. ;; make and show buffer
  2235. (if (get-buffer org-index--occur-buffer-name)
  2236. (kill-buffer org-index--occur-buffer-name))
  2237. (setq occur-buffer (make-indirect-buffer org-index--buffer org-index--occur-buffer-name))
  2238. (pop-to-buffer-same-window occur-buffer)
  2239. (setq initial-frame (selected-frame))
  2240. ;; avoid modifying direct buffer
  2241. (setq buffer-read-only t)
  2242. (toggle-truncate-lines 1)
  2243. ;; reset stack and overlays
  2244. (setq org-index--occur-stack nil)
  2245. (setq org-index--occur-tail-overlay nil)
  2246. ;; narrow to table rows and one line before
  2247. (goto-char org-index--below-hline)
  2248. (forward-line 0)
  2249. (setq begin (point))
  2250. (forward-line -1)
  2251. (narrow-to-region (point) (org-table-end))
  2252. (forward-line)
  2253. ;; initialize help text
  2254. (setq days-clause (if days (format " (%d days back)" days) ""))
  2255. (setq help-text (cons
  2256. (concat
  2257. (propertize (format "Incremental occur%s" days-clause) 'face 'org-todo)
  2258. (propertize "; ? toggles help and headlines.\n" 'face 'org-agenda-dimmed-todo-face))
  2259. (concat
  2260. (propertize
  2261. (org-index--wrap
  2262. (concat
  2263. "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, <S-return> jumps to matching line in index; all other keys end search." these-commands "\n"))
  2264. 'face 'org-agenda-dimmed-todo-face)
  2265. org-index--headings)))
  2266. ;; insert overlays for help text and to cover unsearched lines
  2267. (setq help-overlay (make-overlay (point-min) begin))
  2268. (overlay-put help-overlay 'display (car help-text))
  2269. (setq org-index--occur-tail-overlay (make-overlay (point-max) (point-max)))
  2270. (overlay-put org-index--occur-tail-overlay 'invisible t)
  2271. ;; do not enter loop if number of days is requested
  2272. (when days
  2273. (goto-char begin)
  2274. (org-index--hide-with-overlays (cons word words) lines-wanted days)
  2275. (move-overlay org-index--occur-tail-overlay (org-index--occur-end-of-visible) (point-max))
  2276. (goto-char begin)
  2277. (setq done t))
  2278. ;; main loop
  2279. (while (not done)
  2280. (if in-c-backspace
  2281. (setq key "<backspace>")
  2282. (setq search-text (mapconcat 'identity (reverse (cons word words)) ","))
  2283. ;; read key, if selected frame has not changed
  2284. (if (eq initial-frame (selected-frame))
  2285. (progn
  2286. (setq key-sequence
  2287. (let ((echo-keystrokes 0)
  2288. (full-prompt (format "%s%s%s"
  2289. prompt
  2290. search-text
  2291. (if (string= search-text "") "" " "))))
  2292. (read-key-sequence full-prompt nil nil t t)))
  2293. (setq key (key-description key-sequence))
  2294. (setq key-sequence-raw (this-single-command-raw-keys)))
  2295. (setq done t)
  2296. (setq key-sequence nil)
  2297. (setq key nil)
  2298. (setq key-sequence-raw nil)))
  2299. (cond
  2300. ((string= key "<C-backspace>")
  2301. (setq in-c-backspace t))
  2302. ((member key (list "<backspace>" "DEL")) ; erase last char
  2303. (if (= (length word) 0)
  2304. ;; nothing more to delete from current word; try next
  2305. (progn
  2306. (setq word (car words))
  2307. (setq words (cdr words))
  2308. (setq in-c-backspace nil))
  2309. ;; some chars are left; shorten word
  2310. (setq word (substring word 0 -1))
  2311. (when (= (length word) 0) ; when nothing left, use next word from list
  2312. (setq word (car words))
  2313. (setq words (cdr words))
  2314. (setq in-c-backspace nil))
  2315. ;; free top list of overlays and remove list
  2316. (org-index--unhide)
  2317. (move-overlay org-index--occur-tail-overlay
  2318. (org-index--occur-end-of-visible)
  2319. (point-max))
  2320. ;; make sure, point is still visible
  2321. (goto-char begin)))
  2322. ((member key (list "SPC" ",")) ; space or comma: enter an additional search word
  2323. ;; push current word and clear, no need to change display
  2324. (unless (string= word "")
  2325. (setq words (cons word words))
  2326. (setq word "")))
  2327. ((string= key "?") ; question mark: toggle display of headlines and help
  2328. (setq help-text (cons (cdr help-text) (car help-text)))
  2329. (overlay-put help-overlay 'display (car help-text)))
  2330. ((and (= (length key) 1)
  2331. (aref printable-chars (elt key 0))) ; any printable char: add to current search word
  2332. ;; add to word
  2333. (setq word (concat word key))
  2334. ;; make overlays to hide lines, that do not match longer word any more
  2335. (goto-char begin)
  2336. (org-index--hide-with-overlays (cons word words) lines-wanted days)
  2337. (move-overlay org-index--occur-tail-overlay
  2338. (org-index--occur-end-of-visible)
  2339. (point-max))
  2340. (goto-char begin)
  2341. ;; make sure, point is on a visible line
  2342. (line-move -1 t)
  2343. (line-move 1 t))
  2344. ;; anything else terminates input loop
  2345. (t (setq done t))))
  2346. ;; put back input event, that caused the loop to end
  2347. (unless (string= key "C-g")
  2348. (setq unread-command-events (listify-key-sequence key-sequence-raw))
  2349. (message key))
  2350. ;; For performance reasons do not show matching lines for rest of table. So no code here.
  2351. ;; make permanent copy
  2352. ;; copy visible lines
  2353. (let ((lines-collected 0)
  2354. keymap line all-lines all-lines-lbp header-lines lbp)
  2355. (setq cursor-type t)
  2356. (goto-char begin)
  2357. (let ((inhibit-read-only t))
  2358. (put-text-property begin (org-table-end) 'face nil))
  2359. ;; collect all visible lines
  2360. (while (and (not (eobp))
  2361. (< lines-collected lines-wanted))
  2362. ;; skip over invisible lines
  2363. (while (and (invisible-p (point))
  2364. (not (eobp)))
  2365. (goto-char (1+ (overlay-end (car (overlays-at (point)))))))
  2366. (setq lbp (line-beginning-position))
  2367. (setq line (buffer-substring-no-properties lbp (line-end-position)))
  2368. (unless (string= line "")
  2369. (cl-incf lines-collected)
  2370. (setq all-lines (cons (concat line
  2371. "\n")
  2372. all-lines))
  2373. (setq all-lines-lbp (cons lbp all-lines-lbp)))
  2374. (forward-line 1))
  2375. (kill-buffer org-index--occur-buffer-name) ; cannot keep this buffer; might become stale soon
  2376. ;; create new buffer
  2377. (setq occur-buffer (get-buffer-create org-index--occur-buffer-name))
  2378. (pop-to-buffer-same-window occur-buffer)
  2379. (insert org-index--headings)
  2380. (setq header-lines (line-number-at-pos))
  2381. ;; insert into new buffer
  2382. (save-excursion
  2383. (apply 'insert (reverse all-lines))
  2384. (if (= lines-collected lines-wanted)
  2385. (insert "\n(more lines omitted)\n")))
  2386. (setq org-index--occur-lines-collected lines-collected)
  2387. (org-mode)
  2388. (setq truncate-lines t)
  2389. (if all-lines (org-index--align-and-fontify-current-line (length all-lines)))
  2390. (when (fboundp 'font-lock-ensure)
  2391. (font-lock-ensure)
  2392. (font-lock-flush))
  2393. (when all-lines-lbp
  2394. (while (not (org-at-table-p))
  2395. (forward-line -1))
  2396. (while all-lines-lbp
  2397. (put-text-property (line-beginning-position) (line-end-position) 'org-index-lbp (car all-lines-lbp))
  2398. (setq all-lines-lbp (cdr all-lines-lbp))
  2399. (forward-line -1)))
  2400. ;; prepare help text
  2401. (goto-char (point-min))
  2402. (forward-line (1- header-lines))
  2403. (setq org-index--occur-help-overlay (make-overlay (point-min) (point)))
  2404. (setq org-index--occur-help-text
  2405. (cons
  2406. (org-index--wrap
  2407. (propertize (format "Search is done%s; ? toggles help and headlines.\n" days-clause) 'face 'org-agenda-dimmed-todo-face))
  2408. (concat
  2409. (org-index--wrap
  2410. (propertize
  2411. (format
  2412. (concat (format "Search is done%s." days-clause)
  2413. (if (< lines-collected lines-wanted)
  2414. " Showing all %d matches for "
  2415. " Showing one window of matches for ")
  2416. "\"" search-text
  2417. "\". <return> jumps to heading, <tab> jumps to heading in other window, <S-return> jumps to matching line in index, <space> increments count.\n" these-commands "\n")
  2418. (length all-lines))
  2419. 'face 'org-agenda-dimmed-todo-face))
  2420. org-index--headings)))
  2421. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))
  2422. ;; highlight words
  2423. (mapc (lambda (w) (unless (or (not w) (string= w ""))
  2424. (let ((case-fold-search (not (string= w (downcase w)))))
  2425. (highlight-regexp (regexp-quote w) 'isearch))))
  2426. (cons word words))
  2427. (setq buffer-read-only t)
  2428. ;; install keyboard-shortcuts
  2429. (setq keymap (make-sparse-keymap))
  2430. (set-keymap-parent keymap org-mode-map)
  2431. (mapc (lambda (x) (define-key keymap (kbd x)
  2432. (lambda () (interactive)
  2433. (message "%s" (org-index--occur-action)))))
  2434. (list "<return>" "RET"))
  2435. (define-key keymap (kbd "<tab>")
  2436. (lambda () (interactive)
  2437. (message (org-index--occur-action t))))
  2438. (define-key keymap (kbd "e")
  2439. (lambda () (interactive)
  2440. (message (org-index 'edit))))
  2441. (define-key keymap (kbd "SPC")
  2442. (lambda () (interactive)
  2443. (org-index--refresh-parse-table)
  2444. ;; increment in index
  2445. (let ((ref (org-index--get-or-set-field 'ref))
  2446. count)
  2447. (org-index--on
  2448. 'ref ref
  2449. (setq count (+ 1 (string-to-number (org-index--get-or-set-field 'count))))
  2450. (org-index--get-or-set-field 'count (number-to-string count))
  2451. (org-index--promote-current-line)
  2452. (org-index--align-and-fontify-current-line))
  2453. ;; increment in this buffer
  2454. (let ((inhibit-read-only t))
  2455. (org-index--get-or-set-field 'count (number-to-string count)))
  2456. (message "Incremented count to %d" count))))
  2457. (define-key keymap (kbd "<S-return>")
  2458. (lambda () (interactive)
  2459. (let ((pos (get-text-property (point) 'org-index-lbp)))
  2460. (org-index--refresh-parse-table)
  2461. (org-index--occur-test-stale pos)
  2462. (pop-to-buffer org-index--buffer)
  2463. (goto-char pos)
  2464. (org-reveal t)
  2465. (org-index--update-current-line)
  2466. (beginning-of-line))))
  2467. (define-key keymap (kbd "?")
  2468. (lambda () (interactive)
  2469. (org-index--refresh-parse-table)
  2470. (setq-local org-index--occur-help-text (cons (cdr org-index--occur-help-text) (car org-index--occur-help-text)))
  2471. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))))
  2472. (use-local-map keymap))))
  2473. (defun org-index--occur-end-of-visible ()
  2474. "End of visible stretch during occur"
  2475. (if org-index--occur-stack
  2476. (cdr (assoc :end-of-visible (car org-index--occur-stack)))
  2477. (point-max)))
  2478. (defun org-index--occur-test-stale (pos)
  2479. "Test, if current line in occur buffer has become stale at POS."
  2480. (let (here there)
  2481. (org-index--refresh-parse-table)
  2482. (setq here (org-index--line-in-canonical-form))
  2483. (with-current-buffer org-index--buffer
  2484. (goto-char pos)
  2485. (setq there (org-index--line-in-canonical-form)))
  2486. (unless (string= here there)
  2487. (error "Occur buffer has become stale; please repeat search"))))
  2488. (defun org-index--line-in-canonical-form ()
  2489. "Return current line in its canonical form."
  2490. (org-trim (substring-no-properties (replace-regexp-in-string "\s +" " " (buffer-substring (line-beginning-position) (line-beginning-position 2))))))
  2491. (defun org-index--wrap (text)
  2492. "Wrap TEXT at fill column."
  2493. (with-temp-buffer
  2494. (insert text)
  2495. (fill-region (point-min) (point-max) nil t)
  2496. (buffer-string)))
  2497. (defun org-index--occur-action (&optional other)
  2498. "Helper for `org-index--occur', find heading with ref or id; if OTHER, in other window; or copy yank column."
  2499. (if (org-at-table-p)
  2500. (let ((id (org-index--get-or-set-field 'id))
  2501. (ref (org-index--get-or-set-field 'ref))
  2502. (yank (org-index--get-or-set-field 'yank)))
  2503. (if id
  2504. (org-index--find-id id other)
  2505. (if ref
  2506. (progn
  2507. (org-mark-ring-goto)
  2508. (format "Found reference %s (no node is associated)" ref))
  2509. (if yank
  2510. (progn
  2511. (org-index--update-line (get-text-property (point) 'org-index-lbp))
  2512. (setq yank (replace-regexp-in-string (regexp-quote "\\vert") "|" yank nil 'literal))
  2513. (kill-new yank)
  2514. (org-mark-ring-goto)
  2515. (if (and (>= (length yank) 4) (string= (substring yank 0 4) "http"))
  2516. (progn
  2517. (browse-url yank)
  2518. (format "Opened '%s' in browser (and copied it too)" yank))
  2519. (format "Copied '%s' (no node is associated)" yank)))
  2520. (error "Internal error, this line contains neither id, nor reference, nor text to yank")))))
  2521. (message "Not at table")))
  2522. (defun org-index--hide-with-overlays (words lines-wanted days)
  2523. "Hide lines that are currently visible and do not match WORDS;
  2524. leave LINES-WANTED lines visible.
  2525. Argument DAYS hides older lines."
  2526. (let ((lines-found 0)
  2527. (end-of-visible (point))
  2528. overlay overlays start matched places all-places)
  2529. ;; main loop
  2530. (while (and (not (eobp))
  2531. (< lines-found lines-wanted))
  2532. ;; skip invisible lines
  2533. (while (and (not (eobp))
  2534. (and
  2535. (invisible-p (point))
  2536. (< (point) (overlay-start org-index--occur-tail-overlay))))
  2537. (goto-char (overlay-end (car (overlays-at (point))))))
  2538. ;; find stretch of lines, that are currently visible but should be invisible now
  2539. (setq matched nil)
  2540. (setq places nil)
  2541. (setq start (point))
  2542. (while (and (not (eobp))
  2543. (not (and
  2544. (invisible-p (point))
  2545. (< (point) (overlay-start org-index--occur-tail-overlay))))
  2546. ;; either regard words or days, but not both
  2547. (if days
  2548. (let ((last-accessed (org-index--get-or-set-field 'last-accessed)))
  2549. (if last-accessed
  2550. (not (and
  2551. (<= (- (time-to-days (current-time))
  2552. (time-to-days (org-read-date nil t last-accessed nil)))
  2553. days)
  2554. (setq matched t))) ; for its side effect
  2555. t))
  2556. (not (and (setq places (org-index--test-words words))
  2557. (setq matched t))))) ; for its side effect
  2558. (forward-line 1))
  2559. (setq all-places (append places all-places))
  2560. ;; create overlay to hide this stretch
  2561. (when (< start (point)) ; avoid creating an empty overlay
  2562. (setq overlay (make-overlay start (point)))
  2563. (overlay-put overlay 'invisible t)
  2564. (setq overlays (cons overlay overlays)))
  2565. ;; skip and count line, that matched
  2566. (when matched
  2567. (let ((inhibit-read-only t) (lbp (line-beginning-position)))
  2568. (put-text-property lbp (line-end-position) 'face nil)
  2569. (while places
  2570. (put-text-property (caar places) (+ (caar places) (cdar places)) 'face 'isearch)
  2571. (setq places (cdr places))))
  2572. (forward-line 1)
  2573. (setq end-of-visible (point))
  2574. (cl-incf lines-found)))
  2575. ;; put new list on top of stack
  2576. (setq org-index--occur-stack
  2577. (cons (list (cons :overlays overlays)
  2578. (cons :end-of-visible end-of-visible)
  2579. (cons :lines lines-found)
  2580. (cons :places all-places))
  2581. org-index--occur-stack))
  2582. lines-found))
  2583. (defun org-index--unhide ()
  2584. "Unhide text that does has been hidden by `org-index--hide-with-overlays'."
  2585. (let (places)
  2586. (when org-index--occur-stack
  2587. ;; delete overlays and make visible again
  2588. (mapc (lambda (y)
  2589. (delete-overlay y))
  2590. (cdr (assoc :overlays (car org-index--occur-stack))))
  2591. ;; remove latest highlights
  2592. (setq places (cdr (assoc :places (car org-index--occur-stack))))
  2593. (while places
  2594. (let ((inhibit-read-only t))
  2595. (put-text-property (caar places) (+ (caar places) (cdar places)) 'face nil))
  2596. (setq places (cdr places)))
  2597. ;; remove top of stack
  2598. (setq org-index--occur-stack (cdr org-index--occur-stack))
  2599. ;; redo older highlights
  2600. (setq places (cdr (assoc :places (car org-index--occur-stack))))
  2601. (while places
  2602. (let ((inhibit-read-only t))
  2603. (put-text-property (caar places) (+ (caar places) (cdar places)) 'face 'isearch))
  2604. (setq places (cdr places))))))
  2605. (defun org-index--test-words (words)
  2606. "Test current line for match against WORDS."
  2607. (let ((lbp (line-beginning-position))
  2608. line dc-line places index)
  2609. (setq line (buffer-substring lbp (line-beginning-position 2)))
  2610. (setq dc-line (downcase line))
  2611. (catch 'not-found
  2612. (dolist (word words)
  2613. (if (setq index (cl-search word (if (string= word (downcase word)) dc-line line)))
  2614. (setq places (cons (cons (+ lbp index) (length word)) places))
  2615. (throw 'not-found nil)))
  2616. places)))
  2617. (defun org-index--create-new-line ()
  2618. "Do the common work for `org-index-new-line' and `org-index'."
  2619. ;; insert ref or id as last or first line, depending on sort-column
  2620. (goto-char org-index--below-hline)
  2621. (if (eq org-index-sort-by 'count)
  2622. (progn
  2623. (goto-char (org-table-end))
  2624. (forward-line -1)
  2625. (org-table-insert-row t))
  2626. (org-table-insert-row))
  2627. ;; insert some of the standard values
  2628. (org-table-goto-column (org-index--column-num 'created))
  2629. (org-insert-time-stamp nil nil t)
  2630. (org-table-goto-column (org-index--column-num 'count))
  2631. (insert "1"))
  2632. (defun org-index--sort-silent ()
  2633. "Sort index for default column to remove any effects of temporary sorting."
  2634. (unless org-index--inhibit-sort-idle
  2635. (save-excursion
  2636. (org-index--verify-id)
  2637. (org-index--parse-table)
  2638. (with-current-buffer org-index--buffer
  2639. (save-excursion
  2640. (goto-char org-index--below-hline)
  2641. (org-index--do-sort-index org-index-sort-by)
  2642. (remove-hook 'before-save-hook 'org-index--sort-silent))))))
  2643. (defun org-index--idle-prepare ()
  2644. "For parsing table when idle."
  2645. (org-index--verify-id)
  2646. (org-index--parse-table most-positive-fixnum t))
  2647. (defun org-index--copy-visible (beg end)
  2648. "Copy the visible parts of the region between BEG and END without adding it to `kill-ring'; copy of `org-copy-visible'."
  2649. (let (snippets s)
  2650. (save-excursion
  2651. (save-restriction
  2652. (narrow-to-region beg end)
  2653. (setq s (goto-char (point-min)))
  2654. (while (not (= (point) (point-max)))
  2655. (goto-char (org-find-invisible))
  2656. (push (buffer-substring s (point)) snippets)
  2657. (setq s (goto-char (org-find-visible))))))
  2658. (apply 'concat (nreverse snippets))))
  2659. (provide 'org-index)
  2660. ;; Local Variables:
  2661. ;; fill-column: 75
  2662. ;; comment-column: 50
  2663. ;; End:
  2664. ;;; org-index.el ends here