org-index.el 105 KB

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