123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006 |
- (require 'org-table)
- (require 'cl-lib)
- (require 'widget)
- (defvar org-index-version "5.1.3" "Version of `org-index', format is major.minor.bugfix, where \"major\" are incompatible changes and \"minor\" are new features.")
- (defgroup org-index nil
- "Options concerning the optional index for org."
- :tag "Org Index"
- :group 'org)
- (defcustom org-index-id nil
- "Id of the Org-mode node, which contains the index table."
- :group 'org-index)
- (defcustom org-index-sort-by 'mixed
- "Strategy for sorting index table (and whence entries in occur).
- Valid values are:
- last-access Sort index by date and time of last access; show
- more recent entries first.
- count Sort by usage count; more often used entries first.
- mixed First, show all index entries, which have been
- used today; sort them by last access. Then show
- older entries sorted by usage count."
- :group 'org-index
- :set (lambda (s v)
- (set-default s v)
- (if (and org-index-id
- org-index--buffer
- (functionp 'org-index--sort-silent))
- (org-index--sort-silent)))
- :initialize 'custom-initialize-default
- :type '(choice
- (const last-accessed)
- (const count)
- (const mixed)))
- (defcustom org-index-yank-after-add 'ref
- "Specifies which column should be yanked after adding a new index row.
- Valid values are some columns of index table."
- :group 'org-index
- :type '(choice
- (const ref)
- (const category)
- (const keywords)))
- (defcustom org-index-point-on-add 'keywords
- "Specifies in which column point will land when adding a new index row.
- Valid values are some columns of index table."
- :group 'org-index
- :type '(choice
- (const category)
- (const keywords)))
- (defcustom org-index-copy-heading-to-keywords t
- "When adding a new node to index: Copy heading to keywords-column ?"
- :group 'org-index
- :type '(choice (const :tag "Yes" t)
- (const :tag "No" nil)))
- (defcustom org-index-strip-ref-and-date-from-heading t
- "When adding a node to index: strip leading ref or timestamps ?
- This can be useful, if you have the habit of adding refs and
- dates to the start of your headings; then, if you change your
- heading and want to update your index, you do not need to remove
- those pieces."
- :group 'org-index
- :type '(choice (const :tag "Yes" t)
- (const :tag "No" nil)))
- (defcustom org-index-edit-on-add '(category keywords)
- "List of columns to edit when adding a new row."
- :group 'org-index
- :type '(repeat (choice
- (const category)
- (const keywords))))
- (defcustom org-index-edit-on-yank '(yank keywords)
- "List of columns to edit when adding new text to yank."
- :group 'org-index
- :type '(repeat (choice
- (const yank)
- (const category)
- (const keywords))))
- (defcustom org-index-edit-on-ref '(category keywords)
- "List of columns to edit when adding new ref."
- :group 'org-index
- :type '(repeat (choice
- (const category)
- (const keywords))))
- (defvar org-index--maxrefnum nil "Maximum number from reference table, e.g. 153.")
- (defvar org-index--nextref nil "Next reference, that can be used, e.g. 'R154'.")
- (defvar org-index--head nil "Header before number (e.g. 'R').")
- (defvar org-index--tail nil "Tail after number (e.g. '}' or ')'.")
- (defvar org-index--numcols nil "Number of columns in index table.")
- (defvar org-index--ref-regex nil "Regular expression to match a reference.")
- (defvar org-index--ref-format nil "Format, that can print a reference.")
- (defvar org-index--columns nil "Columns of index-table.")
- (defvar org-index--buffer nil "Buffer of index table.")
- (defvar org-index--point nil "Position at start of headline of index table.")
- (defvar org-index--below-hline nil "Position of first cell in first line below hline.")
- (defvar org-index--saved-positions nil "Saved positions within current buffer and index buffer; filled by ‘org-index--save-positions’.")
- (defvar org-index--headings nil "Headlines of index-table as a string.")
- (defvar org-index--headings-visible nil "Visible part of headlines of index-table as a string.")
- (defvar org-index--last-fingerprint nil "Fingerprint of last line created.")
- (defvar org-index--category-before nil "Category of node before.")
- (defvar org-index--active-region nil "Active region, initially. I.e. what has been marked.")
- (defvar org-index--below-cursor nil "Word below cursor.")
- (defvar org-index--within-index-node nil "True, if we are within node of the index table.")
- (defvar org-index--within-occur nil "True, if we are within the occur-buffer.")
- (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
- (defvar org-index--occur-help-text nil "Text for help in occur buffer.")
- (defvar org-index--occur-help-overlay nil "Overlay for help in occur buffer.")
- (defvar org-index--occur-stack nil "Stack with overlays for hiding lines.")
- (defvar org-index--occur-tail-overlay nil "Overlay to cover invisible lines.")
- (defvar org-index--occur-lines-collected 0 "Number of lines collected in occur buffer; helpful for tests.")
- (defvar org-index--last-sort nil "Last column, the index has been sorted after.")
- (defvar org-index--sort-timer nil "Timer to sort index in correct order.")
- (defvar org-index--aligned nil "Remember for this Emacs session, if table has been aligned at least once.")
- (defvar org-index--edit-widgets nil "List of widgets used to edit.")
- (defvar org-index--context-index nil "Position and line used for index in edit buffer.")
- (defvar org-index--context-occur nil "Position and line used for occur in edit buffer.")
- (defvar org-index--context-node nil "Buffer and position for node in edit buffer.")
- (defvar org-index--short-help-buffer-name "*org-index commands*" "Name of buffer to display short help.")
- (defvar org-index--display-short-help nil "True, if short help should be displayed.")
- (defvar org-index--short-help-displayed nil "True, if short help message has been displayed.")
- (defvar org-index--minibuffer-saved-key nil "Temporarily save entry of minibuffer keymap.")
- (defconst org-index--commands '(occur add kill head ping index ref yank column edit help short-help example sort find-ref highlight maintain) "List of commands available.")
- (defconst org-index--valid-headings '(ref id created last-accessed count keywords category level yank tags) "All valid headings.")
- (defconst org-index--occur-buffer-name "*org-index-occur*" "Name of occur buffer.")
- (defconst org-index--edit-buffer-name "*org-index-edit*" "Name of edit buffer.")
- (defconst org-index--sort-idle-delay 300 "Delay in seconds after which buffer will sorted.")
- (defvar org-index--short-help-text nil "Cache for result of `org-index--get-short-help-text.")
- (defvar org-index--shortcut-chars nil "Cache for result of `org-index--get-shortcut-chars.")
- (defmacro org-index--on (column value &rest body)
- "Execute the forms in BODY with point on index line whose COLUMN is VALUE.
- The value returned is the value of the last form in BODY or nil,
- if VALUE cannot be found."
- (declare (indent 2) (debug t))
- (let ((pointvar (make-symbol "point"))
- (foundvar (make-symbol "found"))
- (retvar (make-symbol "ret")))
- `(save-current-buffer
- (let ((,pointvar (point))
- ,foundvar
- ,retvar)
- (set-buffer org-index--buffer)
- (setq ,foundvar (org-index--go ,column ,value))
- (when ,foundvar
- (setq ,retvar (progn ,@body)))
- (goto-char ,pointvar)
- ,retvar))))
- (defun org-index (&optional command search-ref arg)
- "Fast search-index for selected org nodes and things outside of org.
- org-index creates and updates an index table with keywords; each line
- either points to a heading in org, references something outside or
- carries a snippet of text to yank. The index table is searched for
- keywords through an incremental occur; results are sorted by usage
- count and date, so that frequently used entries appear first among
- the results.
- References are decorated numbers (e.g. 'R237' or '--455--'); they are
- well suited to be used outside of org, e.g. in folder names, ticket
- systems or on printed documents.
- On first invocation org-index will help to create a dedicated node
- for its index table.
- To start building up your index, use subcommands 'add', 'ref' and
- 'yank' to create entries and use 'occur' to find them.
- This is version 5.1.3 of org-index.el.
- The function `org-index' is the only interactive function of this
- package and its main entry point; it will present you with a list
- of subcommands to choose from:
- \(Note the one-letter shortcuts, e.g. [o]; used like 'C-c i o'.)
- occur: [o] Incrementally show matching lines from index.
- Result is updated after every keystroke. You may enter a
- list of words seperated by space or comma (`,'), to select
- lines that contain all of the given words.
- add: [a] Add the current node to index.
- So that (e.g.) it can be found through the subcommand
- 'occur'. Update index, if node is already present.
- kill: [k] Kill (delete) the current node from index.
- Can be invoked from index, from occur or from a headline.
- head: [h] Search for heading, by ref or from index line.
- If invoked from within index table, go to associated
- node (if any), otherwise ask for ref to search.
- index: [i] Enter index table and maybe go to a specific reference.
- Use `org-mark-ring-goto' (\\[org-mark-ring-goto]) to go back.
- ping: [p] Echo line from index table for current node.
- If current node is not in index, than search among its
- parents.
- ref: [r] Create a new index line with a reference.
- This line will not be associated with a node.
- yank: [y] Store a new string, that can be yanked from occur.
- The index line will not be associated with a node.
- column: [c] From within index table: read char and jump to column.
- Shortcut for column movement; stays within one index line.
- edit: [e] Present current line in edit buffer.
- Can be invoked from index, from occur or from a headline.
- help: Show complete help text of org-index.
- short-help: [?] Show one-line description of each subcommand.
- I.e. show this list but only first sentence each.
- example: Create an example index, that will not be saved.
- May serve as an example.
- sort: Sort lines in index, in region or buffer.
- Region or buffer can be sorted by contained reference; Index
- by count, reference or last access.
- find-ref: Search for given reference in all org-buffers.
- A wrapper to employ emacs standard `multi-occur' function;
- asks for reference.
- highlight: Highlight or unhighlight all references.
- Operates on active region or whole buffer. Call with prefix
- argument (`C-u') to remove highlights.
- maintain: Index maintainance.
- Offers some choices to check, update or fix your index.
- If you invoke `org-index' for the first time, an assistant will be
- invoked, that helps you to create your own index.
- Invoke `org-customize' to tweak the behaviour of org-index.
- Optionally bind `org-index-dispatch' to a key, e.g. 'C-c i' in
- the global keymap to invoke the most important subcommands with
- a single key.
- A numeric prefix argument is used as a reference number for
- commands, that need one (e.g. 'head').
- Use from elisp: Optional argument COMMAND is a symbol naming the
- command to execute. SEARCH-REF specifies a reference to search
- for, if needed. ARG allows passing in a prefix argument as in
- interactive calls."
- (interactive "i\ni\nP")
- (let (search-id
- search-fingerprint
- sort-what
- kill-new-text
- message-text)
- (catch 'new-index
-
-
-
-
- (org-index--verify-id)
-
- (org-index--parse-table)
-
- (org-index--retrieve-context)
-
-
-
-
- (unless org-index--last-sort (setq org-index--last-sort org-index-sort-by))
-
- (unless org-index--sort-timer
- (setq org-index--sort-timer
- (run-with-idle-timer org-index--sort-idle-delay t 'org-index--sort-silent)))
-
-
-
-
- (if (and command (not (eq command 'short-help)))
- (unless (memq command org-index--commands)
- (error "Unknown command '%s' passed as argument, valid choices are any of these symbols: %s"
- command (mapconcat 'symbol-name org-index--commands ",")))
-
- (setq org-index--display-short-help (eq command 'short-help))
- (setq command (org-index--read-command))
- (setq org-index--display-short-help nil))
-
-
-
-
-
- (if (and (not search-ref)
- (numberp arg))
- (setq search-ref (format "%s%d%s" org-index--head arg org-index--tail)))
-
- (when (memq command '(index head find-ref))
-
- (unless search-ref
- (if org-index--within-index-node
- (if (org-at-table-p)
- (setq search-ref (org-index--get-or-set-field 'ref)))
- (if (and org-index--below-cursor
- (string-match (concat "\\(" org-index--ref-regex "\\)")
- org-index--below-cursor))
- (setq search-ref (match-string 1 org-index--below-cursor)))))
-
- (unless search-ref
- (if (eq command 'index)
- (let ((r (org-index--read-search-for-index)))
- (setq search-ref (first r))
- (setq search-id (second r))
- (setq search-fingerprint (third r)))
- (unless (and (eq command 'head)
- org-index--within-index-node
- (org-at-table-p))
- (setq search-ref (read-from-minibuffer "Search reference number: ")))))
-
- (when search-ref
- (setq search-ref (org-trim search-ref))
- (if (string-match "^[0-9]+$" search-ref)
- (setq search-ref (concat org-index--head search-ref org-index--tail)))
- (if (string= search-ref "") (setq search-ref nil)))
- (if (and (not search-ref)
- (not (eq command 'index))
- (not (and (eq command 'head)
- org-index--within-index-node
- (org-at-table-p))))
- (error "Command %s needs a reference number" command)))
-
-
-
- (when (eq command 'sort)
- (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))))
-
-
-
-
- (when (and (memq command '(occur head index example sort maintain))
- (not (string= (buffer-name) org-index--occur-buffer-name)))
- (org-mark-ring-push))
-
- (when (or (memq command '(index maintain))
- (and (eq command 'sort)
- (eq sort-what 'index)))
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--point)
- (org-index--unfold-buffer))
-
-
-
- (cond
- ((eq command 'help)
-
- (describe-function 'org-index))
- ((eq command 'short-help)
- (org-index--display-short-help))
- ((eq command 'find-ref)
-
- (let (org-buffers)
- (dolist (buff (buffer-list))
- (set-buffer buff)
- (if (string= major-mode "org-mode")
- (setq org-buffers (cons buff org-buffers))))
-
- (multi-occur org-buffers (org-index--make-guarded-search search-ref))
-
- (if (get-buffer "*Occur*")
- (progn
- (setq message-text (format "Found '%s'" search-ref))
- (other-window 1)
- (toggle-truncate-lines 1))
- (setq message-text (format "Did not find '%s'" search-ref)))))
- ((eq command 'add)
- (let ((r (org-index--do-add-or-update (if (equal arg '(4)) t nil)
- (if (numberp arg) arg nil))))
- (setq message-text (car r))
- (setq kill-new-text (cdr r))))
- ((eq command 'kill)
- (setq message-text (org-index--do-kill)))
- ((eq command 'head)
- (if (and org-index--within-index-node
- (org-at-table-p))
- (setq search-id (org-index--get-or-set-field 'id)))
- (if (and (not search-id) search-ref)
- (setq search-id (org-index--id-from-ref search-ref)))
- (setq message-text
- (if search-id
- (org-index--find-id search-id)
- "Current line has no id")))
- ((eq command 'index)
- (goto-char org-index--below-hline)
- (setq message-text
- (if search-ref
- (if (org-index--go 'ref search-ref)
- (progn
- (org-index--update-current-line)
- (org-table-goto-column (org-index--column-num 'ref))
- (format "Found index line '%s'" search-ref))
- (format "Did not find index line with reference '%s'" search-ref))
- (if search-id
- (if (org-index--go 'id search-id)
- (progn
- (org-index--update-current-line)
- (org-table-goto-column (org-index--column-num 'ref))
- (format "Found index line '%s'" (org-index--get-or-set-field 'ref)))
- (format "Did not find index line with id '%s'" search-id))
- (if search-fingerprint
- (if (org-index--go 'fingerprint org-index--last-fingerprint)
- (progn
- (org-index--update-current-line)
- (beginning-of-line)
- (format "Found latest index line"))
- (format "Did not find index line"))
-
- "At index table"))))
- (recenter))
- ((eq command 'ping)
- (let ((moved-up 0) id info reached-top)
- (unless (string= major-mode "org-mode") (error "No node at point"))
-
- (setq id (if search-ref
- (org-index--id-from-ref search-ref)
- (org-id-get)))
-
- (save-excursion
- (outline-back-to-heading)
- (while (not (or info
- reached-top))
- (if id
- (setq info (org-index--on 'id id
- (mapcar (lambda (x) (org-index--get-or-set-field x))
- (list 'ref 'count 'created 'last-accessed 'category 'keywords 'ref)))))
- (setq reached-top (= (org-outline-level) 1))
- (unless (or info
- reached-top)
- (outline-up-heading 1 t)
- (cl-incf moved-up))
- (setq id (org-id-get))))
- (if info
- (progn
- (setq message-text
- (apply 'format
- (append (list "'%s'%shas been accessed %s times between %s and %s; category is '%s', keywords are '%s'"
- (pop info)
- (if (> moved-up 0) (format " (parent node, %d level up) " moved-up) " "))
- info)))
- (setq kill-new-text (car (last info))))
- (setq message-text "Neither this node nor any of its parents is part of index"))))
- ((eq command 'occur)
- (set-buffer org-index--buffer)
- (org-index--do-occur))
- ((eq command 'ref)
- (let (args)
- (setq args (org-index--collect-values-from-user org-index-edit-on-ref))
- (setq args (plist-put args 'category "yank"))
- (setq args (plist-put args 'ref org-index--nextref))
- (apply 'org-index--do-new-line args)
- (setq kill-new-text org-index--nextref)
- (setq message-text (format "Added new row with ref '%s'" org-index--nextref))))
- ((eq command 'yank)
- (let (args)
- (setq args (org-index--collect-values-from-user org-index-edit-on-yank))
- (if (plist-get args 'yank)
- (plist-put args 'yank (replace-regexp-in-string "|" "\\vert" (plist-get args 'yank) nil 'literal)))
- (setq args (plist-put args 'category "yank"))
- (apply 'org-index--do-new-line args)
- (setq message-text "Added new row with text to yank")))
- ((eq command 'column)
- (if (and org-index--within-index-node
- (org-at-table-p))
- (let (char col num)
- (setq char (read-char "Please specify, which column to go to (r=ref, k=keywords, c=category, y=yank): "))
- (unless (memq char (list ?r ?k ?c ?y))
- (error (format "Invalid char '%c', cannot goto this column" char)))
- (setq col (cdr (assoc char '((?r . ref) (?k . keywords) (?c . category) (?y . yank)))))
- (setq num (org-index--column-num col))
- (if num
- (progn
- (org-table-goto-column num)
- (setq message-text (format "At column %s" (symbol-name col))))
- (error (format "Column '%s' is not present" col))))
- (error "Need to be in index table to go to a specific column")))
- ((eq command 'edit)
- (setq message-text (org-index--do-edit)))
- ((eq command 'sort)
- (let ((sorts (list "count" "last-accessed" "mixed" "id" "ref"))
- sort groups-and-counts)
- (cond
- ((eq sort-what 'index)
- (setq sort
- (intern
- (completing-read
- "Please choose column to sort index table: "
- (cl-copy-list sorts)
- nil t nil nil (symbol-name org-index-sort-by))))
- (org-index--do-sort-index sort)
- (org-table-goto-column (org-index--column-num (if (eq sort 'mixed) 'last-access sort)))
-
- (with-current-buffer org-index--buffer
- (add-hook 'before-save-hook 'org-index--sort-silent t))
- (setq message-text
- (format
- (concat "Your index has been sorted temporarily by %s and will be sorted again by %s after %d seconds of idle time"
- (if groups-and-counts
- "; %d groups with equal %s and a total of %d lines have been found"
- ""))
- (symbol-name sort)
- org-index-sort-by
- org-index--sort-idle-delay
- (second groups-and-counts)
- (symbol-name sort)
- (third groups-and-counts))))
- ((memq sort-what '(region buffer))
- (org-index--do-sort-lines sort-what)
- (setq message-text (format "Sorted %s by contained references" sort-what))))))
- ((eq command 'highlight)
- (let ((where "buffer"))
- (save-excursion
- (save-restriction
- (when (and transient-mark-mode
- mark-active)
- (narrow-to-region (region-beginning) (region-end))
- (setq where "region"))
- (if arg
- (progn
- (unhighlight-regexp org-index--ref-regex)
- (setq message-text (format "Removed highlights for references in %s" where)))
- (highlight-regexp org-index--ref-regex 'isearch)
- (setq message-text (format "Highlighted references in %s" where)))))))
- ((eq command 'maintain)
- (setq message-text (org-index--do-maintain)))
- ((eq command 'example)
- (if (y-or-n-p "This assistant will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
- (org-index--create-index t)))
- ((not command) (setq message-text "No command given"))
- (t (error "Unknown subcommand '%s'" command)))
-
- (if kill-new-text (setq kill-new-text
- (substring-no-properties kill-new-text)))
- (if (string= kill-new-text "") (setq kill-new-text nil))
- (let ((m (concat
- message-text
- (if (and message-text kill-new-text)
- " and r"
- (if kill-new-text "R" ""))
- (if kill-new-text (format "eady to yank '%s'." kill-new-text) (if message-text "." "")))))
- (unless (string= m "")
- (message m)
- (setq org-index--message-text m)))
- (if kill-new-text (kill-new kill-new-text)))))
- (defun org-index-dispatch (&optional arg)
- "Read additional chars and call subcommands of `org-index'.
- Can be bound in global keyboard map as central entry point.
- Optional argument ARG is passed on."
- (interactive "P")
- (let (char command)
- (if (sit-for 1)
- (message "org-index (? for detailed prompt) -"))
- (setq char (key-description (read-key-sequence nil)))
- (if (string= char "C-g") (keyboard-quit))
- (if (string= char "SPC") (setq char "?"))
- (setq command (cdr (assoc char (org-index--get-shortcut-chars))))
- (unless command
- (message "No subcommand for '%s'; switching to detailed prompt" char)
- (sit-for 1)
- (setq command 'short-help))
- (org-index command nil arg)))
- (defun org-index-new-line (&rest keys-values)
- "Create a new line within the index table, returning its reference.
- The function takes a varying number of argument pairs; each pair
- is a symbol for an existing column heading followed by its value.
- The return value is the new reference.
- Example:
- (message \"Created reference %s\"
- (org-index-new-line 'keywords \"foo bar\" 'category \"baz\"))
- Optional argument KEYS-VALUES specifies content of new line."
- (let ((ref (plist-get keys-values 'ref)))
- (org-index--verify-id)
- (org-index--parse-table)
- (if (not (memq ref '(t nil)))
- (error "Column 'ref' accepts only 't' or 'nil'"))
- (when ref
- (setq ref org-index--nextref)
- (setq keys-values (plist-put keys-values 'ref ref)))
- (apply 'org-index--do-new-line keys-values)
- ref))
- (defun org-index--read-command (&optional with-short-help)
- "Read subcommand for ‘org-index’ from minibuffer.
- Optional argument WITH-SHORT-HELP displays help screen upfront."
- (let (minibuffer-scroll-window
- minibuffer-setup-fun
- command)
- (setq org-index--short-help-displayed nil)
- (add-hook 'minibuffer-setup-hook 'org-index--minibuffer-setup-function)
- (add-hook 'minibuffer-exit-hook 'org-index--minibuffer-exit-function)
- (unwind-protect
- (setq command
- (intern
- (completing-read
- (concat
- "Please choose"
- (if org-index--display-short-help "" " (? for short help)")
- ": ")
- (mapcar 'symbol-name org-index--commands) nil t)))
- (remove-hook 'minibuffer-setup-hook 'org-index--minibuffer-setup-function)
- (remove-hook 'minibuffer-exit-hook 'org-index--minibuffer-exit-function)
- (when org-index--short-help-displayed
- (quit-windows-on org-index--short-help-buffer-name)))
- command))
- (defun org-index--minibuffer-setup-function ()
- "Prepare minibuffer for `org-index--read-command'."
- (setq org-index--minibuffer-saved-key (local-key-binding (kbd "?")))
- (local-set-key (kbd "?") 'org-index--display-short-help)
- (if org-index--display-short-help (org-index--display-short-help)))
- (defun org-index--minibuffer-exit-function ()
- "Restore minibuffer after `org-index--read-command'."
- (local-set-key (kbd "?") org-index--minibuffer-saved-key)
- (setq org-index--minibuffer-saved-key nil))
- (defun org-index--display-short-help ()
- "Helper function to show help in minibuffer."
- (interactive)
- (with-temp-buffer-window
- org-index--short-help-buffer-name nil nil
- (setq org-index--short-help-displayed t)
- (princ "Short help; all subcommands of `org-index', shortcuts in []\n")
- (princ (org-index--get-short-help-text)))
- (with-current-buffer org-index--short-help-buffer-name
- (let ((inhibit-read-only t)
- height-before height-after win)
- (setq win (get-buffer-window))
- (setq height-before (window-height win))
- (shrink-window-if-larger-than-buffer win)
- (setq height-after (window-height win))
- (goto-char (point-min))
- (end-of-line)
- (insert
- (if (> height-before height-after)
- "."
- (concat ", "
- (substitute-command-keys "\\[scroll-other-window]")
- " to scroll:")))
- (goto-char (point-min)))))
- (defun org-index--get-short-help-text ()
- "Extract text for short help message from long help."
- (or org-index--short-help-text
- (with-temp-buffer
- (insert (documentation 'org-index))
- (goto-char (point-min))
- (search-forward (concat " " (symbol-name (first org-index--commands)) ": "))
- (forward-line 0)
- (kill-region (point-min) (point))
- (search-forward (concat " " (symbol-name (car (last org-index--commands))) ": "))
- (forward-line 1)
- (kill-region (point) (point-max))
- (keep-lines "^ [-a-z]+:" (point-min) (point-max))
- (align-regexp (point-min) (point-max) "\\(\\s-*\\):")
- (goto-char (point-min))
- (while (re-search-forward "\\. *$" nil t)
- (replace-match "" nil nil))
- (goto-char (point-min))
- (re-search-forward "short-help")
- (end-of-line)
- (insert " (this text)")
- (goto-char (point-min))
- (unless (= (line-number-at-pos (point-max)) (1+ (length org-index--commands)))
- (error "Internal error, unable to properly extract one-line descriptions of subcommands"))
- (setq org-index--short-help-text (buffer-string)))))
- (defun org-index--get-shortcut-chars ()
- "Collect shortcut chars from short help message."
- (or org-index--shortcut-chars
- (with-temp-buffer
- (insert (org-index--get-short-help-text))
- (goto-char (point-min))
- (while (< (point) (point-max))
- (when (looking-at "^ \\([-a-z]+\\) +: +\\[\\([a-z?]\\)\\] ")
- (setq org-index--shortcut-chars
- (cons (cons (match-string 2) (intern (match-string 1)))
- org-index--shortcut-chars)))
- (forward-line 1))
- (unless (> (length org-index--shortcut-chars) 0)
- (error "Internal error, did not find shortcut chars"))
- org-index--shortcut-chars)))
- (defun org-index--do-edit ()
- "Perform command edit."
- (let ((maxlen 0) cols-vals buffer-keymap field-keymap keywords-pos val)
- (setq org-index--context-node nil)
- (setq org-index--context-occur nil)
-
- (if org-index--within-occur
- (let ((pos (get-text-property (point) 'org-index-lbp)))
- (org-index--occur-test-stale pos)
- (setq org-index--context-occur (cons (point) (org-index--line-in-canonical-form)))
- (set-buffer org-index--buffer)
- (goto-char pos))
-
- (if (not org-index--within-index-node)
- (let ((id (org-id-get)))
- (setq org-index--context-node (cons (current-buffer) (point)))
- (set-buffer org-index--buffer)
- (unless (and id (org-index--go 'id id))
- (setq org-index--context-node nil)
- (error "This node is not in index")))))
-
- (dolist (col (mapcar 'car (reverse org-index--columns)))
- (if (> (length (symbol-name col)) maxlen)
- (setq maxlen (length (symbol-name col))))
- (setq val (org-index--get-or-set-field col))
- (if (and val (eq col 'yank)) (setq val (replace-regexp-in-string (regexp-quote "\\vert") "|" val nil 'literal)))
- (setq cols-vals (cons (cons col val)
- cols-vals)))
-
- (setq buffer-keymap (make-sparse-keymap))
- (set-keymap-parent buffer-keymap widget-keymap)
- (define-key buffer-keymap (kbd "C-c C-c") 'org-index--edit-c-c-c-c)
- (define-key buffer-keymap (kbd "C-c C-k") 'org-index--edit-c-c-c-k)
- (setq field-keymap (make-sparse-keymap))
- (set-keymap-parent field-keymap widget-field-keymap)
- (define-key field-keymap (kbd "C-c C-c") 'org-index--edit-c-c-c-c)
- (define-key field-keymap (kbd "C-c C-k") 'org-index--edit-c-c-c-k)
-
- (setq org-index--context-index (cons (point) (org-index--line-in-canonical-form)))
- (if (get-buffer org-index--edit-buffer-name) (kill-buffer org-index--edit-buffer-name))
- (switch-to-buffer (get-buffer-create org-index--edit-buffer-name))
-
- (setq org-index--edit-widgets nil)
- (widget-insert "Edit this line from index; type C-c C-c when done, C-c C-k to abort.\n\n")
- (dolist (col-val cols-vals)
- (if (eq (car col-val) 'keywords) (setq keywords-pos (point)))
- (setq org-index--edit-widgets (cons
- (cons (car col-val)
- (widget-create 'editable-field
- :format (format (format "%%%ds: %%%%v" maxlen) (symbol-name (car col-val)))
- :keymap field-keymap
- (or (cdr col-val) "")))
- org-index--edit-widgets)))
- (widget-setup)
- (goto-char keywords-pos)
- (beginning-of-line)
- (forward-char (+ maxlen 2))
- (use-local-map buffer-keymap)
- "Editing a single line from index"))
- (defun org-index--edit-c-c-c-c ()
- "Function to invoked on C-c C-c in Edit buffer."
- (interactive)
- (let ((obuf (get-buffer org-index--occur-buffer-name))
- val line)
-
- (org-index--refresh-parse-table)
- (with-current-buffer org-index--buffer
-
- (save-excursion
- (goto-char (car org-index--context-index))
- (unless (string= (cdr org-index--context-index)
- (org-index--line-in-canonical-form))
- (switch-to-buffer org-index--edit-buffer-name)
- (error "Index table has changed: Cannot find line, that this buffer is editing")))
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char (car org-index--context-index))
-
- (dolist (col-widget org-index--edit-widgets)
- (setq val (widget-value (cdr col-widget)))
- (if (eq (car col-widget) 'yank) (setq val (replace-regexp-in-string "|" (regexp-quote "\\vert") val)))
- (org-index--get-or-set-field (car col-widget) val))
- (setq line (org-index--align-and-fontify-current-line))
- (beginning-of-line))
-
- (if org-index--context-occur
- (if obuf
- (if (string= (cdr org-index--context-index)
- (cdr org-index--context-occur))
- (progn
- (pop-to-buffer-same-window obuf)
- (goto-char (car org-index--context-occur))
- (beginning-of-line)
- (let ((inhibit-read-only t))
- (delete-region (line-beginning-position) (line-end-position))
- (insert line)
- (put-text-property (line-beginning-position) (line-end-position)
- 'org-index-lbp (cdr org-index--context-index))))
- (error "Occur buffer and index buffer do not match any longer"))
- (message "Occur buffer has gone, cannot switch back."))
- (setq org-index--context-occur nil))
-
- (when org-index--context-node
- (pop-to-buffer-same-window (car org-index--context-node))
- (goto-char (cdr org-index--context-node)))
-
- (kill-buffer org-index--edit-buffer-name)
- (setq org-index--context-index nil)
- (setq org-index--edit-widgets nil)
- (beginning-of-line)
- (message "Index line has been edited.")))
- (defun org-index--edit-c-c-c-k ()
- "Function invoked on C-c C-k in Edit buffer."
- (interactive)
- (kill-buffer org-index--edit-buffer-name)
- (setq org-index--context-index nil)
- (setq org-index--edit-widgets nil)
- (beginning-of-line)
- (message "Edit aborted."))
- (defun org-index--do-new-line (&rest keys-values)
- "Do the work for `org-index-new-line'.
- Optional argument KEYS-VALUES specifies content of new line."
- (org-index--retrieve-context)
- (with-current-buffer org-index--buffer
- (goto-char org-index--point)
-
- (let ((kvs keys-values)
- k v)
- (while kvs
- (setq k (car kvs))
- (setq v (cadr kvs))
- (if (or (not (symbolp k))
- (and (symbolp v) (not (eq v t)) (not (eq v nil))))
- (error "Arguments must be alternation of key and value"))
- (unless (org-index--column-num k)
- (error "Unknown column or column not defined in table: '%s'" (symbol-name k)))
- (setq kvs (cddr kvs))))
- (let (yank)
-
- (org-index--create-new-line)
-
- (let ((kvs keys-values)
- k v)
- (while kvs
- (setq k (car kvs))
- (setq v (cadr kvs))
- (org-table-goto-column (org-index--column-num k))
- (insert (org-trim (or v "")))
- (setq kvs (cddr kvs))))
-
- (org-index--promote-current-line)
- (org-index--align-and-fontify-current-line)
-
- (setq org-index--last-fingerprint (org-index--get-or-set-field 'fingerprint))
-
- (setq yank (org-index--get-or-set-field org-index-yank-after-add))
- yank)))
- (defun org-index-get-line (column value)
- "Retrieve an existing line within the index table by ref or id.
- Return its contents as a property list.
- The function `plist-get' may be used to retrieve specific elements
- from the result.
- Example:
- (plist-get (org-index-get-line 'ref \"R12\") 'count)
- retrieves the value of the count-column for reference number 12.
- Argument COLUMN is a symbol, either ref or id,
- argument VALUE specifies the value to search for."
-
- (unless (memq column '(ref id))
- (error "Argument column can only be 'ref' or 'id'"))
- (unless value
- (error "Need a value to search for"))
- (org-index--verify-id)
- (org-index--parse-table)
- (org-index--get-line column value))
- (defun org-index--get-line (column value)
- "Find a line by ID, return its contents.
- Argument COLUMN and VALUE specify line to get."
- (let (content)
- (org-index--on
- column value
- (mapc (lambda (x)
- (if (and (numberp (cdr x))
- (> (cdr x) 0))
- (setq content (cons (car x) (cons (or (org-index--get-or-set-field (car x)) "") content)))))
- (reverse org-index--columns)))
- content))
- (defun org-index--ref-from-id (id)
- "Get reference from line ID."
- (org-index--on 'id id (org-index--get-or-set-field 'ref)))
- (defun org-index--id-from-ref (ref)
- "Get id from line REF."
- (org-index--on 'ref ref (org-index--get-or-set-field 'id)))
- (defun org-index--get-fingerprint ()
- "Get fingerprint of current line."
- (replace-regexp-in-string
- "\\s " ""
- (mapconcat (lambda (x) (org-index--get-or-set-field x)) '(id ref yank keywords created) "")))
- (defun org-index--read-search-for-index ()
- "Special input routine for command index."
-
- (let (char prompt search-ref search-id search-fingerprint)
-
- (setq prompt "Please specify, where to go in index (0-9,.,space,backspace,return or ? for short help) - ")
-
- (while (not (memq char (append (number-sequence ?0 ?9) (list ?\d ?\b ?\r ?\j ?\s ?.))))
- (setq char (read-char prompt))
- (setq prompt "Go to specific position in index table. Digits specify a reference number, <space> goes to top of index, <backspace> or <delete> to last line created and <return> or `.' to index line of current node. Please choose - "))
- (if (memq char (number-sequence ?0 ?9))
-
- (setq search-ref (read-from-minibuffer "Search reference number: " (char-to-string char))))
-
- (if (memq char '(?\r ?\n ?.)) (setq search-id (org-id-get)))
- (if (memq char '(?\d ?\b)) (setq search-fingerprint org-index--last-fingerprint))
- (list search-ref search-id search-fingerprint)))
- (defun org-index--verify-id ()
- "Check, that we have a valid id."
-
- (unless org-index-id
- (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")))
- (if (string= answer "create-index")
- (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet.")
- (describe-function 'org-index)
- (throw 'new-index nil))))
-
- (let (marker)
- (setq marker (org-id-find org-index-id 'marker))
- (unless marker (org-index--create-missing-index "Cannot find the node with id \"%s\" (as specified by variable org-index-id)." org-index-id))
-
- (setq marker (org-id-find org-index-id 'marker))
- (unless marker (error "Could not create node"))
- (setq org-index--buffer (marker-buffer marker)
- org-index--point (marker-position marker))
- (move-marker marker nil)))
- (defun org-index--retrieve-context ()
- "Collect context information before starting with command."
-
- (setq org-index--active-region
- (if (and transient-mark-mode mark-active)
- (buffer-substring (region-beginning) (region-end))
- nil))
- (setq org-index--below-cursor (thing-at-point 'symbol))
-
- (setq org-index--category-before
- (save-excursion
- (beginning-of-line)
- (org-get-category (point) t)))
-
- (setq org-index--within-index-node (string= (org-id-get) org-index-id))
- (setq org-index--within-occur (string= (buffer-name) org-index--occur-buffer-name)))
- (defun org-index--parse-table ()
- "Parse content of index table."
- (let (ref-field
- id-field
- initial-point
- end-of-headings
- start-of-headings)
- (with-current-buffer org-index--buffer
- (setq org-index--maxrefnum 0)
- (setq initial-point (point))
- (org-index--go-below-hline)
-
- (unless org-index--aligned
- (org-table-align)
- (let ((is-modified (buffer-modified-p))
- (below (point)))
- (while (org-at-table-p)
- (forward-line))
- (font-lock-fontify-region below (point))
- (org-index--go-below-hline)
- (setq org-index--aligned t)
- (set-buffer-modified-p is-modified)))
- (org-index--go-below-hline)
- (beginning-of-line)
-
- (setq end-of-headings (point))
- (while (org-at-table-p) (forward-line -1))
- (forward-line)
- (setq start-of-headings (point))
- (setq org-index--headings-visible (substring-no-properties (org-index--copy-visible start-of-headings end-of-headings)))
- (setq org-index--headings (buffer-substring start-of-headings end-of-headings))
-
- (org-table-goto-column 100)
- (setq org-index--numcols (- (org-table-current-column) 1))
-
- (while (org-at-table-p)
- (forward-line -1))
- (forward-line)
-
- (org-index--parse-headings)
-
- (goto-char org-index--point)
-
- (goto-char org-index--below-hline)
- (while (and (org-at-table-p)
- (not (setq ref-field (org-index--get-or-set-field 'ref))))
- (forward-line))
-
- (unless ref-field
- (org-index--report-index-error "Reference column is empty"))
- (unless (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\([^0-9]*\\)$" ref-field)
- (org-index--report-index-error
- "First reference in index table ('%s') does not contain a number" ref-field))
-
- (setq org-index--head (match-string 1 ref-field))
- (setq org-index--tail (match-string 3 ref-field))
- (setq org-index--ref-regex (concat (regexp-quote org-index--head)
- "\\([0-9]+\\)"
- (regexp-quote org-index--tail)))
- (setq org-index--ref-format (concat org-index--head "%d" org-index--tail))
-
- (goto-char org-index--below-hline)
- (when (eq org-index-sort-by 'mixed)
- (org-index--go-below-hline)
- (if (string< (org-index--get-or-set-field 'last-accessed)
- (org-index--get-mixed-time))
- (org-index--do-sort-index org-index-sort-by)))
-
- (let ((refnum 0))
- (while (org-at-table-p)
- (setq ref-field (org-index--get-or-set-field 'ref))
- (setq id-field (org-index--get-or-set-field 'id))
- (if ref-field
- (if (string-match org-index--ref-regex ref-field)
-
- (setq refnum (string-to-number (match-string 1 ref-field)))
- (kill-whole-line)
- (message "Removing line from index-table whose ref does not contain a number")))
-
- (if (> refnum org-index--maxrefnum) (setq org-index--maxrefnum refnum))
- (forward-line 1)))
- (setq org-index--nextref (format "%s%d%s" org-index--head (1+ org-index--maxrefnum) org-index--tail))
-
- (goto-char initial-point))))
- (defun org-index--refresh-parse-table ()
- "Fast refresh of selected results of parsing index table."
- (setq org-index--point (marker-position (org-id-find org-index-id 'marker)))
- (with-current-buffer org-index--buffer
- (save-excursion
- (org-index--go-below-hline))))
- (defun org-index--do-maintain ()
- "Choose among and perform some tasks to maintain index."
- (let ((check-what) (max-mini-window-height 1.0) message-text)
- (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")))
- (message nil)
- (cond
- ((eq check-what 'check)
- (setq message-text (or (org-index--check-ids)
- "No problems found")))
- ((eq check-what 'statistics)
- (setq message-text (org-index--do-statistics)))
- ((eq check-what 'duplicates)
- (setq message-text (org-index--find-duplicates)))
- ((eq check-what 'clean)
- (let ((lines 0))
- (org-map-entries
- (lambda ()
- (when (org-entry-get (point) "org-index-ref")
- (cl-incf lines)
- (org-entry-delete (point) "org-index-ref")))
- nil 'agenda)
- (setq message-text (format "Removed property 'org-index-ref' from %d lines" lines))))
- ((eq check-what 'update)
- (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 ? ")
- (setq message-text (org-index--update-all-lines))
- (setq message-text "Canceled."))))
- message-text))
- (defun org-index--get-mixed-time ()
- "Get timestamp for sorting order mixed."
- (format-time-string
- (org-time-stamp-format t t)
- (apply 'encode-time (append '(0 0 0) (nthcdr 3 (decode-time))))))
- (defun org-index--do-sort-index (sort)
- "Sort index table according to SORT."
- (let ((is-modified (buffer-modified-p))
- top
- bottom
- mixed-time)
- (unless buffer-read-only
- (message "Sorting index table for %s..." (symbol-name sort))
- (undo-boundary)
- (let ((message-log-max nil))
-
- (if (eq sort 'mixed)
- (setq mixed-time (org-index--get-mixed-time)))
-
- (org-index--go-below-hline)
- (forward-line 0)
- (setq top (point))
- (while (org-at-table-p) (forward-line))
-
- (while (progn
- (forward-line -1)
- (org-table-goto-column 1)
- (and
- (not (org-index--get-or-set-field 'ref))
- (not (org-index--get-or-set-field 'id))
- (not (org-index--get-or-set-field 'yank))))
- (org-table-kill-row))
- (forward-line 1)
- (setq bottom (point))
-
- (save-restriction
- (narrow-to-region top bottom)
- (goto-char top)
- (sort-subr t
- 'forward-line
- 'end-of-line
- (lambda ()
- (org-index--get-sort-key sort t mixed-time))
- nil
- 'string<)
- (goto-char (point-min))
-
- (set-buffer-modified-p is-modified)))
- (setq org-index--last-sort sort))))
- (defun org-index--do-sort-lines (what)
- "Sort lines in WHAT according to contained reference."
- (save-restriction
- (cond
- ((eq what 'region)
- (if (region-active-p)
- (narrow-to-region (region-beginning) (region-end))
- (error "No active region, cannot sort")))
- ((eq what 'buffer)
- (unless (y-or-n-p "Sort whole current buffer ? ")
- (error "Canceled"))
- (narrow-to-region (point-min) (point-max))))
- (goto-char (point-min))
- (sort-subr nil 'forward-line 'end-of-line
- (lambda ()
- (if (looking-at (concat ".*"
- (org-index--make-guarded-search org-index--ref-regex 'dont-quote)))
- (string-to-number (match-string 1))
- 0)))))
- (defun org-index--go-below-hline ()
- "Move below hline in index-table."
- (let ((errstring (format "index table within node %s" org-index-id)))
- (goto-char org-index--point)
-
- (while (not (org-at-heading-p)) (forward-line -1))
- (forward-line 1)
-
- (while (and (not (org-at-table-p))
- (not (org-at-heading-p))
- (not (eobp)))
- (forward-line))
-
- (unless (org-at-table-p)
- (org-index--create-missing-index "Cannot find %s." errstring))
-
- (while (and (not (org-at-table-hline-p))
- (org-at-table-p))
- (forward-line))
- (forward-line)
-
- (unless (org-at-table-p)
- (org-index--report-index-error "Cannot find a hline within %s" errstring))
- (org-table-goto-column 1)
- (setq org-index--below-hline (point))))
- (defun org-index--parse-headings ()
- "Parse headings of index table."
- (let (field
- field-symbol)
- (setq org-index--columns nil)
-
- (dotimes (col org-index--numcols)
- (setq field (substring-no-properties (downcase (org-trim (org-table-get-field (+ col 1))))))
- (if (string= field "")
- (error "Heading of column cannot be empty"))
- (if (and (not (string= (substring field 0 1) "."))
- (not (member (intern field) org-index--valid-headings)))
- (error "Column name '%s' is not a valid heading (custom headings may start with a dot, e.g. '.foo')" field))
- (setq field-symbol (intern field))
-
- (if (assoc field-symbol org-index--columns)
- (org-index--report-index-error
- "'%s' appears two times as column heading" (downcase field))
-
- (setq org-index--columns (cons (cons field-symbol (+ col 1)) org-index--columns)))))
- (setq org-index--columns (reverse org-index--columns))
-
- (mapc (lambda (head)
- (unless (cdr (assoc head org-index--columns))
- (org-index--report-index-error "No column has heading '%s'" head)))
- org-index--valid-headings))
- (defun org-index--create-missing-index (&rest reasons)
- "Create a new empty index table with detailed explanation. Argument REASONS explains why."
- (org-index--ask-before-create-index "Cannot find index table: "
- "new permanent" "."
- reasons)
- (org-index--create-index))
- (defun org-index--report-index-error (&rest reasons)
- "Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with."
- (when org-index--buffer
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--below-hline)
- (org-reveal t))
- (org-index--ask-before-create-index "The existing index contains this error: "
- "temporary" ", to compare with."
- reasons)
- (org-index--create-index t t))
- (defun org-index--ask-before-create-index (explanation type for-what reasons)
-
- "Ask the user before creating an index or throw error. Arguments specify bits of issued message."
- (let (reason prompt)
- (setq reason (apply 'format reasons))
- (setq prompt (concat explanation reason "\n"
- "However, this assistant can help you to create a "
- type " index with detailed comments" for-what "\n\n"
- "Do you want to proceed ?"))
- (unless (let ((max-mini-window-height 1.0))
- (y-or-n-p prompt))
- (error (concat explanation reason)))))
- (defun org-index--create-index (&optional temporary compare)
- "Create a new empty index table with detailed explanation.
- specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index."
- (let (buffer
- title
- firstref
- id)
- (if temporary
- (let ((file-name (concat temporary-file-directory "org-index--example-index.org"))
- (buffer-name "*org-index-example-index*"))
- (setq buffer (get-buffer-create buffer-name))
- (with-current-buffer buffer
-
- (unless (string= (buffer-file-name) file-name)
- (set-visited-file-name file-name))
- (rename-buffer buffer-name)
- (erase-buffer)
- (org-mode)))
- (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))))))
- (setq title (read-from-minibuffer "Please enter the title of the index node (leave empty for default 'index'): "))
- (if (string= title "") (setq title "index"))
- (while (progn
- (setq firstref (read-from-minibuffer "Please enter your first reference-number. This is an integer number preceeded by some and optionally followed by some non-numeric chars; e.g. 'R1', '-1-' or '#1#' (and your initial number does not need to be '1'). The format of your reference-numbers only needs to make sense for yourself, so that you can spot it easily in your texts or write it on a piece of paper; it should however not already appear frequently within your existing notes, to avoid too many false hits when searching.\n\nPlease choose (leave empty for default 'R1'): "))
- (if (string= firstref "") (setq firstref "R1"))
- (let (desc)
- (when (string-match "[[:blank:]]" firstref)
- (setq desc "Contains whitespace"))
- (when (string-match "[[:cntrl:]]" firstref)
- (setq desc "Contains control characters"))
- (unless (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)
-
- (setq desc
- (cond ((string= firstref "") "is empty")
- ((not (string-match "^[^0-9]+" firstref)) "starts with a digit")
- ((not (string-match "^[^0-9]+[0-9]+" firstref)) "does not contain a number")
- ((not (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)) "contains more than one sequence of digits")
- )))
- (if desc
- (progn
- (read-from-minibuffer (format "Your input '%s' does not meet the requirements because it %s.\nPlease hit RET and try again: " firstref desc))
- t)
- nil))))
- (with-current-buffer buffer
- (goto-char (point-max))
- (insert (format "* %s %s\n" firstref title))
- (if temporary
- (insert "
- Below you find your temporary index table, which WILL NOT LAST LONGER
- THAN YOUR CURRENT EMACS SESSION; please use it only for evaluation.
- ")
- (insert "
- Below you find your initial index table, which will grow over time.
- "))
- (insert " You may start using it by adding some lines. Just
- move to another heading within org, invoke `org-index' and
- choose the command 'add'. After adding a few nodes, try the
- command 'occur' to search among them.
- To gain further insight you may invoke the subcommand 'help', or
- (same content) read the help of `org-index'.
- Within the index table below, the sequence of columns does not
- matter. You may reorder them in any way you like. You may also
- add your own columns, which should start with a dot
- (e.g. '.my-column').
- Invoke `org-customize' to tweak the behaviour of org-index
- (see the group org-index).
- This node needs not be a top level node; its name is completely
- at your choice; it is found through its ID only.
- ")
- (unless temporary
- (insert "
- Remark: These lines of explanation can be removed at any time.
- "))
- (setq id (org-id-get-create))
- (insert (format "
- | ref | category | keywords | tags | count | level | last-accessed | created | id | yank |
- | | | | | | | | | <4> | <4> |
- |-----+----------+----------+------+-------+-------+---------------+---------+-----+------|
- | %s | | %s | | | | | %s | %s | |
- "
- firstref
- title
- (with-temp-buffer (org-insert-time-stamp nil nil t))
- id))
-
- (org-id-add-location id (buffer-file-name))
- (setq buffer-save-without-query t)
- (basic-save-buffer)
- (while (not (org-at-table-p)) (forward-line -1))
- (unless buffer-read-only (org-table-align))
- (while (not (org-at-heading-p)) (forward-line -1))
-
- (let ((org-index-id id))
- (org-index--verify-id))
-
- (setq org-index-id id)
-
- (if temporary
- (progn
-
- (when compare
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--point)
- (org-index--unfold-buffer)
- (delete-other-windows)
- (select-window (split-window-vertically)))
-
- (pop-to-buffer-same-window buffer)
- (org-id-goto id)
- (org-index--unfold-buffer)
- (if compare
- (progn
- (message "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
- (throw 'new-index nil))
- (message "This is your new temporary index, use command add to populate, occur to search.")))
- (progn
-
- (pop-to-buffer-same-window buffer)
- (delete-other-windows)
- (org-id-goto id)
- (org-index--unfold-buffer)
- (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 ? ")
- (progn
- (customize-save-variable 'org-index-id id)
- (message "Saved org-index-id '%s' to %s." id (or custom-file
- user-init-file))
- (throw 'new-index nil))
- (let (sq)
- (setq sq (format "(setq org-index-id \"%s\")" id))
- (kill-new sq)
- (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)
- (throw 'new-index nil))))))))
- (defun org-index--unfold-buffer ()
- "Helper function to unfold buffer."
- (org-show-context)
- (org-show-subtree)
- (recenter 1)
- (save-excursion
- (org-back-to-heading)
- (forward-line)
- (org-cycle)))
- (defun org-index--update-line (&optional ref-or-id-or-pos)
- "Update columns count and last-accessed in line REF-OR-ID-OR-POS."
- (let (initial)
- (with-current-buffer org-index--buffer
- (unless buffer-read-only
-
- (when ref-or-id-or-pos
- (setq initial (point))
- (goto-char org-index--below-hline)
- (while (and (org-at-table-p)
- (not (if (integerp ref-or-id-or-pos)
- (and (>= ref-or-id-or-pos (line-beginning-position))
- (< ref-or-id-or-pos (line-end-position)))
- (or (string= ref-or-id-or-pos (org-index--get-or-set-field 'ref))
- (string= ref-or-id-or-pos (org-index--get-or-set-field 'id))))))
- (forward-line)))
- (if (not (org-at-table-p))
- (error "Did not find reference or id '%s'" ref-or-id-or-pos)
- (org-index--update-current-line))
- (if initial (goto-char initial))))))
- (defun org-index--update-current-line ()
- "Update current lines columns count and last-accessed."
- (let (newcount (count-field (org-index--get-or-set-field 'count)))
-
- (when (or (not count-field)
- (string-match "^[0-9]+$" count-field))
- (setq newcount (+ 1 (string-to-number (or count-field "0"))))
- (org-index--get-or-set-field 'count
- (number-to-string newcount)))
-
- (org-table-goto-column (org-index--column-num 'last-accessed))
- (org-table-blank-field)
- (org-insert-time-stamp nil t t)
-
- (org-index--promote-current-line)
- (org-index--align-and-fontify-current-line)))
- (defun org-index--align-and-fontify-current-line (&optional num)
- "Make current line (or NUM lines) blend well among others."
- (let (lines)
-
- (unless num (setq num 1))
- (setq lines (delete-and-extract-region (line-beginning-position) (line-end-position num)))
-
- (insert (with-temp-buffer
- (org-set-font-lock-defaults)
- (insert org-index--headings-visible)
-
- (goto-char (point-min))
- (search-forward "|")
- (while (search-forward " " (line-end-position) t)
- (replace-match "." nil t))
- (goto-char (point-min))
- (while (search-forward ".|." (line-end-position) t)
- (replace-match " | " nil t))
- (goto-char (point-min))
- (while (search-forward "|." (line-end-position) t)
- (replace-match "| " nil t))
- (goto-char (point-max))
- (insert lines)
- (forward-line 0)
- (let ((start (point)))
- (while (re-search-forward "^\s +|-" nil t)
- (replace-match "| -"))
- (goto-char start))
- (org-mode)
- (org-table-align)
- (font-lock-fontify-region (point-min) (point-max))
- (goto-char (point-max))
- (if (eq -1 (skip-chars-backward "\n"))
- (delete-char 1))
- (forward-line (- 1 num))
- (buffer-substring (line-beginning-position) (line-end-position num))))
- lines))
- (defun org-index--promote-current-line ()
- "Move current line up in table according to changed sort fields."
- (let (begin end key
- (to-skip 0))
- (forward-line 0)
- (setq key (org-index--get-sort-key))
- (setq begin (point))
- (setq end (line-beginning-position 2))
- (forward-line -1)
- (while (and (org-at-table-p)
- (not (org-at-table-hline-p))
- (string< (org-index--get-sort-key) key))
- (cl-incf to-skip)
- (forward-line -1))
- (forward-line 1)
-
- (when (> to-skip 0)
- (insert (delete-and-extract-region begin end))
- (forward-line -1))))
- (defun org-index--get-sort-key (&optional sort with-ref mixed-time)
- "Get value for sorting from column SORT, optional WITH-REF; if mixes use MIXED-TIME."
- (let (ref
- ref-field
- key)
- (unless sort (setq sort org-index--last-sort))
- (when (or with-ref
- (eq sort 'ref))
-
-
- (setq ref-field (org-index--get-or-set-field 'ref))
- (if ref-field
- (progn
- (string-match org-index--ref-regex ref-field)
- (setq ref (format
- "%06d"
- (string-to-number
- (match-string 1 ref-field)))))
- (setq ref "000000")))
- (setq key
- (cond
- ((eq sort 'count)
- (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))
- ((eq sort 'mixed)
- (let ((last-accessed (org-index--get-or-set-field 'last-accessed)))
- (unless mixed-time (setq mixed-time (org-index--get-mixed-time)))
- (concat
- (if (string< mixed-time last-accessed) last-accessed mixed-time)
- (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))))
- ((eq sort 'ref)
- ref)
- ((memq sort '(id last-accessed created))
- (org-index--get-or-set-field sort))
- (t (error "This is a bug: unmatched case '%s'" sort))))
- (if with-ref (setq key (concat key ref)))
- key))
- (defun org-index--get-or-set-field (key &optional value)
- "Retrieve field KEY from index table or set it to VALUE."
- (let (field)
- (save-excursion
- (if (eq key 'fingerprint)
- (progn
- (if value (error "Internal error, pseudo-column fingerprint cannot be set"))
- (setq field (org-index--get-fingerprint)))
- (setq field (org-trim (org-table-get-field (cdr (assoc key org-index--columns)) value))))
- (if (string= field "") (setq field nil))
- (org-no-properties field))))
- (defun org-index--column-num (key)
- "Return number of column KEY."
- (if (numberp key)
- key
- (cdr (assoc key org-index--columns))))
- (defun org-index--make-guarded-search (ref &optional dont-quote)
- "Make robust search string from REF; DONT-QUOTE it, if requested."
- (concat "\\_<" (if dont-quote ref (regexp-quote ref)) "\\_>"))
- (defun org-index--find-duplicates ()
- "Find duplicate references or ids in index table."
- (let (ref-duplicates id-duplicates)
- (setq ref-duplicates (org-index--find-duplicates-helper 'ref))
- (setq id-duplicates (org-index--find-duplicates-helper 'id))
- (goto-char org-index--below-hline)
- (if (or ref-duplicates id-duplicates)
- (progn
-
- (pop-to-buffer-same-window
- (get-buffer-create "*org-index-duplicates*"))
- (when ref-duplicates
- (insert "These references appear more than once:\n")
- (mapc (lambda (x) (insert " " x "\n")) ref-duplicates)
- (insert "\n\n"))
- (when id-duplicates
- (insert "These ids appear more than once:\n")
- (mapc (lambda (x) (insert " " x "\n")) id-duplicates))
- "Some references or ids are duplicates")
- "No duplicate references or ids found")))
- (defun org-index--find-duplicates-helper (column)
- "Helper for `org-index--find-duplicates': Go through table and count given COLUMN."
- (let (counts duplicates field found)
-
- (goto-char org-index--below-hline)
- (while (org-at-table-p)
-
- (setq field (org-index--get-or-set-field column))
-
- (setq found (assoc field counts))
- (if found
- (cl-incf (cdr found))
- (setq counts (cons (cons field 1) counts)))
- (forward-line))
- (mapc (lambda (x) (if (and (> (cdr x) 1)
- (car x))
- (setq duplicates (cons (car x) duplicates)))) counts)
- duplicates))
- (defun org-index--do-statistics ()
- "Compute statistics about index table."
- (let ((total-lines 0) (total-refs 0)
- ref ref-field min max message)
-
- (goto-char org-index--below-hline)
- (while (org-at-table-p)
-
- (setq ref-field (org-index--get-or-set-field 'ref))
- (when ref-field
- (string-match org-index--ref-regex ref-field)
- (setq ref (string-to-number (match-string 1 ref-field)))
-
- (if (or (not min) (< ref min)) (setq min ref))
- (if (or (not max) (> ref max)) (setq max ref))
- (setq total-refs (1+ total-refs)))
-
- (setq total-lines (1+ total-lines))
- (forward-line))
- (setq message (format "%d Lines in index table. First reference is %s, last %s; %d of them are used (%d percent)"
- total-lines
- (format org-index--ref-format min)
- (format org-index--ref-format max)
- total-refs
- (truncate (* 100 (/ (float total-refs) (1+ (- max min)))))))
- (goto-char org-index--below-hline)
- message))
- (defun org-index--do-add-or-update (&optional create-ref tag-with-ref)
- "For current node or current line in index, add or update in index table.
- CREATE-REF and TAG-WITH-REF if given."
- (let* (id id-from-index ref args yank ret)
- (org-index--save-positions)
- (unless (or org-index--within-index-node
- org-index--within-occur)
- (org-back-to-heading))
-
- (if org-index--within-index-node
- (progn
- (unless (org-at-table-p)
- (error "Within index node but not on table"))
- (setq id (org-index--get-or-set-field 'id))
- (setq ref (org-index--get-or-set-field 'ref))
- (setq args (org-index--collect-values-for-add-update-remote id))
- (org-index--write-fields args)
- (setq yank (org-index--get-or-set-field org-index-yank-after-add))
- (setq ret
- (if ref
- (cons (format "Updated index line %s" ref) yank)
- (cons "Updated index line" nil))))
- (setq id (org-id-get-create))
- (org-index--refresh-parse-table)
- (setq id-from-index (org-index--on 'id id id))
- (setq ref (org-index--on 'id id (org-index--get-or-set-field 'ref)))
- (if tag-with-ref
- (org-toggle-tag (format "%s%d%s" org-index--head tag-with-ref org-index--tail) 'on))
- (setq args (org-index--collect-values-for-add-update id))
- (when (and create-ref
- (not ref))
- (setq ref org-index--nextref)
- (setq args (plist-put args 'ref ref)))
- (if id-from-index
-
- (progn
- (org-index--on
- 'id id
- (org-index--write-fields args)
- (setq yank (org-index--get-or-set-field org-index-yank-after-add)))
- (setq ret
- (if ref
- (cons (format "Updated index line %s" ref) yank)
- (cons "Updated index line" nil))))
-
- (if ref (setq ref (plist-put args 'ref org-index--nextref)))
- (setq yank (apply 'org-index--do-new-line args))
- (setq ret
- (if ref
- (cons
- (format "Added new index line %s" ref)
- (concat yank " "))
- (cons
- "Added new index line"
- nil)))))
- (org-index--restore-positions)
- ret))
- (defun org-index--check-ids ()
- "Check, that ids really point to a node."
- (let ((lines 0)
- id ids marker)
- (goto-char org-index--below-hline)
- (catch 'problem
- (while (org-at-table-p)
- (when (setq id (org-index--get-or-set-field 'id))
-
- (when (member id ids)
- (org-table-goto-column (org-index--column-num 'id))
- (throw 'problem "This id appears twice in index; please use command 'maintain' to check for duplicate ids"))
- (cl-incf lines)
- (setq ids (cons id ids))
-
- (setq marker (org-id-find id t))
- (unless marker
- (org-table-goto-column (org-index--column-num 'id))
- (throw 'problem "This id cannot be found")))
- (forward-line))
- (goto-char org-index--below-hline)
- nil)))
- (defun org-index--update-all-lines ()
- "Update all lines of index at once."
- (let ((lines 0)
- id ref kvs)
-
- (or
- (org-index--check-ids)
- (progn
- (goto-char org-index--below-hline)
- (while (org-at-table-p)
-
- (when (setq id (org-index--get-or-set-field 'id))
- (setq ref (org-index--get-or-set-field 'ref))
- (setq kvs (org-index--collect-values-for-add-update-remote id))
- (org-index--write-fields kvs)
- (cl-incf lines))
- (forward-line))
- (goto-char org-index--below-hline)
- (org-table-align)
- (format "Updated %d lines" lines)))))
- (defun org-index--collect-values-for-add-update (id &optional silent category)
- "Collect values for adding or updating line specified by ID, do not ask if SILENT, use CATEGORY, if given."
- (let ((args (list 'id id))
- content)
- (dolist (col (mapcar 'car org-index--columns))
- (setq content "")
- (cond
- ((eq col 'keywords)
- (if org-index-copy-heading-to-keywords
- (setq content (nth 4 (org-heading-components))))
-
- (if org-index-strip-ref-and-date-from-heading
- (dotimes (i 2)
- (if (or (string-match (concat "^\\s-*" org-index--ref-regex) content)
- (string-match (concat "^\\s-*" org-ts-regexp-both) content))
- (setq content (substring content (match-end 0)))))))
- ((eq col 'category)
- (setq content (or category org-index--category-before)))
- ((eq col 'level)
- (setq content (number-to-string (org-outline-level))))
- ((eq col 'tags)
- (setq content (org-get-tags-string))))
- (unless (string= content "")
- (setq args (plist-put args col content))))
- (if (not silent)
- (let ((args-edited (org-index--collect-values-from-user org-index-edit-on-add args)))
- (setq args (append args-edited args))))
- args))
- (defun org-index--collect-values-for-add-update-remote (id)
- "Wrap `org-index--collect-values-for-add-update' by prior moving to remote node identified by ID."
- (let (marker point args)
- (setq marker (org-id-find id t))
-
- (with-current-buffer (marker-buffer marker)
- (setq point (point))
- (goto-char marker)
- (setq args (org-index--collect-values-for-add-update id t (org-get-category (point) t)))
- (goto-char point))
- args))
- (defun org-index--collect-values-from-user (cols &optional defaults)
- "Collect values for adding a new yank-line.
- Argument COLS gives list of columns to edit.
- Optional argument DEFAULTS gives default values."
- (let (content args)
- (dolist (col cols)
- (setq content "")
- (setq content (read-from-minibuffer
- (format "Enter text for column '%s': " (symbol-name col))
- (plist-get col defaults)))
- (unless (string= content "")
- (setq args (plist-put args col content))))
- args))
- (defun org-index--write-fields (kvs)
- "Update current line with values from KVS (keys-values)."
- (while kvs
- (org-index--get-or-set-field (car kvs) (org-trim (cadr kvs)))
- (setq kvs (cddr kvs))))
- (defun org-index--do-kill ()
- "Perform command kill from within occur, index or node."
- (let (id ref chars-deleted-index text-deleted-from pos-in-index)
- (org-index--save-positions)
- (unless (or org-index--within-index-node
- org-index--within-occur)
- (org-back-to-heading))
-
- (if (or org-index--within-occur
- org-index--within-index-node)
- (progn
- (if org-index--within-index-node
-
- (setq pos-in-index (point))
-
- (setq pos-in-index (get-text-property (point) 'org-index-lbp))
- (org-index--occur-test-stale pos-in-index)
- (set-buffer org-index--buffer)
- (goto-char pos-in-index))
-
- (setq id (org-index--get-or-set-field 'id))
- (setq ref (org-index--get-or-set-field 'ref)))
-
- (setq id (org-entry-get (point) "ID"))
- (setq ref (org-index--ref-from-id id))
- (setq pos-in-index (org-index--on 'id id (point)))
- (unless pos-in-index (error "This node is not in index")))
-
-
- (when id
- (let ((m (org-id-find id 'marker)))
- (set-buffer (marker-buffer m))
- (goto-char m)
- (move-marker m nil)
- (unless (string= (org-id-get) id)
- (error "Could not find node with id %s" id)))
- (org-index--delete-any-ref-from-tags)
- (if ref (org-index--delete-ref-from-heading ref))
- (setq text-deleted-from (cons "node" text-deleted-from)))
-
- (set-buffer org-index--buffer)
- (unless pos-in-index "Internal error, pos-in-index should be defined here")
- (goto-char pos-in-index)
- (setq chars-deleted-index (length (delete-and-extract-region (line-beginning-position) (line-beginning-position 2))))
- (setq text-deleted-from (cons "index" text-deleted-from))
-
- (if org-index--within-occur
- (let ((inhibit-read-only t))
- (set-buffer org-index--occur-buffer-name)
- (delete-region (line-beginning-position) (line-beginning-position 2))
-
- (while (org-at-table-p)
- (put-text-property (line-beginning-position) (line-end-position) 'org-index-lbp
- (- (get-text-property (point) 'org-index-lbp) chars-deleted-index))
- (forward-line))
- (setq text-deleted-from (cons "occur" text-deleted-from))))
- (org-index--restore-positions)
- (concat "Deleted from: " (mapconcat 'identity (sort text-deleted-from 'string<) ","))))
- (defun org-index--save-positions ()
- "Save current buffer and positions in index- and current buffer; not in occur-buffer."
- (let (cur-buf cur-mrk idx-pnt idx-mrk)
- (setq cur-buf (current-buffer))
- (setq cur-mrk (point-marker))
- (set-buffer org-index--buffer)
- (if (string= (org-id-get) org-index-id)
- (setq idx-pnt (point))
- (setq idx-mrk (point-marker)))
- (set-buffer cur-buf)
- (setq org-index--saved-positions (list cur-buf cur-mrk idx-pnt idx-mrk))))
- (defun org-index--restore-positions ()
- "Restore positions as saved by `org-index--save-positions'."
- (cl-multiple-value-bind
- (cur-buf cur-mrk idx-pnt idx-mrk buf)
- org-index--saved-positions
- (setq buf (current-buffer))
- (set-buffer cur-buf)
- (goto-char cur-mrk)
- (set-buffer org-index--buffer)
- (goto-char (or idx-pnt idx-mrk))
- (set-buffer buf))
- (setq org-index--saved-positions nil))
- (defun org-index--delete-ref-from-heading (ref)
- "Delete given REF from current heading."
- (save-excursion
- (end-of-line)
- (let ((end (point)))
- (beginning-of-line)
- (when (search-forward ref end t)
- (delete-char (- (length ref)))
- (just-one-space)))))
- (defun org-index--delete-any-ref-from-tags ()
- "Delete any reference from list of tags."
- (let (new-tags)
- (mapc (lambda (tag)
- (unless (string-match org-index--ref-regex tag)
- (setq new-tags (cons tag new-tags) )))
- (org-get-tags))
- (org-set-tags-to new-tags)))
- (defun org-index--go (&optional column value)
- "Position cursor on index line where COLUMN equals VALUE.
- Return t or nil, leave point on line or at top of table, needs to be in buffer initially."
- (let (found)
- (unless (eq (current-buffer) org-index--buffer)
- (error "This is a bug: Not in index buffer"))
-
- (goto-char org-index--below-hline)
- (if column
- (progn
- (forward-line -1)
- (while (and (not found)
- (forward-line)
- (org-at-table-p))
- (setq found (string= value (org-index--get-or-set-field column)))))
- (setq found t))
-
- (if found
- t
- (goto-char org-index--below-hline)
- nil)))
- (defun org-index--find-id (id &optional other)
- "Perform command head: Find node with REF or ID and present it.
- If OTHER in separate window."
- (let (message marker)
- (setq marker (org-id-find id t))
- (if marker
- (progn
- (org-index--update-line id)
- (if other
- (progn
- (pop-to-buffer (marker-buffer marker)))
- (pop-to-buffer-same-window (marker-buffer marker)))
- (goto-char marker)
- (org-reveal t)
- (org-show-entry)
- (recenter)
- (unless (string= (org-id-get) id)
- (setq message (format "Could not go to node with id %s (narrowed ?)" id)))
- (setq message "Found headline"))
- (setq message (format "Did not find node with %s" id)))
- message))
- (defun org-index--do-occur ()
- "Perform command occur."
- (let ((word "")
- (prompt "Search for: ")
- (these-commands " NOTE: If you invoke the org-index subcommands edit or kill from within the occur buffer, the index is updated accordingly.")
- (lines-wanted (window-body-height))
- (lines-found 0)
- words
- occur-buffer
- begin
- narrow
- help-text
- search-text
- done
- in-c-backspace
- help-overlay
- last-point
- initial-frame
- key
- key-sequence
- key-sequence-raw)
-
- (if (get-buffer org-index--occur-buffer-name)
- (kill-buffer org-index--occur-buffer-name))
- (setq occur-buffer (make-indirect-buffer org-index--buffer org-index--occur-buffer-name))
- (pop-to-buffer-same-window occur-buffer)
- (setq initial-frame (selected-frame))
-
- (setq buffer-read-only t)
- (toggle-truncate-lines 1)
- (setq font-lock-keywords-case-fold-search t)
- (setq case-fold-search t)
-
- (setq org-index--occur-stack nil)
- (setq org-index--occur-tail-overlay nil)
-
- (goto-char org-index--below-hline)
- (forward-line 0)
- (setq begin (point))
- (forward-line -1)
- (setq narrow (point))
- (while (org-at-table-p)
- (forward-line))
- (narrow-to-region narrow (point))
- (goto-char (point-min))
- (forward-line)
-
- (setq help-text (cons
- (concat
- (propertize "Incremental occur" 'face 'org-todo)
- (propertize "; ? toggles help and headlines.\n" 'face 'org-agenda-dimmed-todo-face))
- (concat
- (propertize
- (org-index--wrap
- (concat
- "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"))
- 'face 'org-agenda-dimmed-todo-face)
- org-index--headings)))
-
- (setq help-overlay (make-overlay (point-min) begin))
- (overlay-put help-overlay 'display (car help-text))
- (setq org-index--occur-tail-overlay (make-overlay (point-max) (point-max)))
- (overlay-put org-index--occur-tail-overlay 'invisible t)
- (while (not done)
- (if in-c-backspace
- (setq key "<backspace>")
- (setq search-text (mapconcat 'identity (reverse (cons word words)) ","))
- (message "foo")
-
- (if (eq initial-frame (selected-frame))
- (progn
- (setq key-sequence
- (let ((echo-keystrokes 0)
- (full-prompt (format "%s%s%s"
- prompt
- search-text
- (if (string= search-text "") "" " "))))
- (read-key-sequence full-prompt nil nil t t)))
- (setq key (key-description key-sequence))
- (setq key-sequence-raw (this-single-command-raw-keys)))
- (setq done t)
- (setq key-sequence nil)
- (setq key nil)
- (setq key-sequence-raw nil)))
- (cond
- ((string= key "<C-backspace>")
- (setq in-c-backspace t))
- ((member key (list "<backspace>" "DEL"))
- (if (= (length word) 0)
-
- (progn
- (setq word (car words))
- (setq words (cdr words))
- (setq in-c-backspace nil))
-
- (unhighlight-regexp (regexp-quote word))
-
- (setq word (substring word 0 -1))
- (when (= (length word) 0)
- (setq word (car words))
- (setq words (cdr words))
- (setq in-c-backspace nil))
-
- (setq lines-found (or (org-index--unhide) lines-wanted))
- (move-overlay org-index--occur-tail-overlay
- (if org-index--occur-stack (cdr (assq :end-of-visible (car org-index--occur-stack)))
- (point-max))
- (point-max))
-
- (unless (= (length word) 0)
- (highlight-regexp (regexp-quote word) 'isearch))
-
- (goto-char begin)))
- ((member key (list "SPC" ","))
-
- (unless (string= word "")
- (setq words (cons word words))
- (setq word "")))
- ((string= key "?")
- (setq help-text (cons (cdr help-text) (car help-text)))
- (overlay-put help-overlay 'display (car help-text)))
- ((and (= (length key) 1)
- (aref printable-chars (elt key 0)))
-
- (unless (= (length word) 0)
- (unhighlight-regexp (regexp-quote word)))
-
- (setq word (concat word key))
-
- (goto-char begin)
- (setq lines-found (org-index--hide-with-overlays (cons word words) lines-wanted))
- (move-overlay org-index--occur-tail-overlay
- (if org-index--occur-stack (cdr (assq :end-of-visible (car org-index--occur-stack)))
- (point-max))
- (point-max))
- (goto-char begin)
-
- (highlight-regexp (regexp-quote word) 'isearch)
-
- (line-move -1 t)
- (line-move 1 t))
-
- (t (setq done t))))
-
- (unless (string= key "C-g")
- (setq unread-command-events (listify-key-sequence key-sequence-raw))
- (message key))
-
- (setq last-point (point))
-
-
-
- (let ((lines-collected 0)
- keymap line all-lines all-lines-lbp header-lines lbp)
- (setq cursor-type t)
- (goto-char begin)
-
- (while (and (not (eobp))
- (< lines-collected lines-wanted))
-
- (while (and (invisible-p (point))
- (not (eobp)))
- (goto-char (1+ (overlay-end (car (overlays-at (point)))))))
- (setq lbp (line-beginning-position))
- (setq line (buffer-substring-no-properties lbp (line-end-position)))
- (unless (string= line "")
- (cl-incf lines-collected)
- (setq all-lines (cons (concat line
- "\n")
- all-lines))
- (setq all-lines-lbp (cons lbp all-lines-lbp)))
- (forward-line 1))
- (kill-buffer org-index--occur-buffer-name)
-
- (setq occur-buffer (get-buffer-create org-index--occur-buffer-name))
- (pop-to-buffer-same-window occur-buffer)
- (insert org-index--headings)
- (setq header-lines (line-number-at-pos))
-
- (save-excursion
- (apply 'insert (reverse all-lines))
- (if (= lines-collected lines-wanted)
- (insert "\n(more lines omitted)\n")))
- (setq org-index--occur-lines-collected lines-collected)
- (org-mode)
- (setq truncate-lines t)
- (if all-lines (org-index--align-and-fontify-current-line (length all-lines)))
- (font-lock-ensure)
- (font-lock-flush)
- (when all-lines-lbp
- (while (not (org-at-table-p))
- (forward-line -1))
- (while all-lines-lbp
- (put-text-property (line-beginning-position) (line-end-position) 'org-index-lbp (car all-lines-lbp))
- (setq all-lines-lbp (cdr all-lines-lbp))
- (forward-line -1)))
-
- (goto-char (point-min))
- (forward-line (1- header-lines))
- (setq org-index--occur-help-overlay (make-overlay (point-min) (point)))
- (setq org-index--occur-help-text
- (cons
- (org-index--wrap
- (propertize "Search is done; ? toggles help and headlines.\n" 'face 'org-agenda-dimmed-todo-face))
- (concat
- (org-index--wrap
- (propertize
- (format
- (concat "Search is done."
- (if (< lines-collected lines-wanted)
- " Showing all %d matches for "
- " Showing one window of matches for ")
- "\"" search-text
- "\". <return> jumps to heading, <tab> jumps to heading in other window, <S-return> jumps to matching line in index, <space> increments count.\n" these-commands "\n")
- (length all-lines))
- 'face 'org-agenda-dimmed-todo-face))
- org-index--headings)))
- (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))
-
- (setq case-fold-search t)
- (setq font-lock-keywords-case-fold-search t)
- (mapc (lambda (w) (unless (or (not w) (string= w "")) (highlight-regexp (regexp-quote w) 'isearch)))
- (cons word words))
- (setq buffer-read-only t)
-
- (setq keymap (make-sparse-keymap))
- (set-keymap-parent keymap org-mode-map)
- (mapc (lambda (x) (define-key keymap (kbd x)
- (lambda () (interactive)
- (message "%s" (org-index--occur-action)))))
- (list "<return>" "RET"))
- (define-key keymap (kbd "<tab>")
- (lambda () (interactive)
- (message (org-index--occur-action t))))
- (define-key keymap (kbd "SPC")
- (lambda () (interactive)
- (org-index--refresh-parse-table)
-
- (let ((ref (org-index--get-or-set-field 'ref))
- count)
- (org-index--on
- 'ref ref
- (setq count (+ 1 (string-to-number (org-index--get-or-set-field 'count))))
- (org-index--get-or-set-field 'count (number-to-string count))
- (org-index--promote-current-line)
- (org-index--align-and-fontify-current-line))
-
- (let ((inhibit-read-only t))
- (org-index--get-or-set-field 'count (number-to-string count)))
- (message "Incremented count to %d" count))))
- (define-key keymap (kbd "<S-return>")
- (lambda () (interactive)
- (let ((pos (get-text-property (point) 'org-index-lbp)))
- (org-index--refresh-parse-table)
- (org-index--occur-test-stale pos)
- (pop-to-buffer org-index--buffer)
- (goto-char pos)
- (org-reveal t)
- (org-index--update-current-line)
- (beginning-of-line))))
- (define-key keymap (kbd "?")
- (lambda () (interactive)
- (org-index--refresh-parse-table)
- (setq-local org-index--occur-help-text (cons (cdr org-index--occur-help-text) (car org-index--occur-help-text)))
- (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))))
- (use-local-map keymap))))
- (defun org-index--occur-test-stale (pos)
- "Test, if current line in occur buffer has become stale at POS."
- (let (here there)
- (org-index--refresh-parse-table)
- (setq here (org-index--line-in-canonical-form))
- (with-current-buffer org-index--buffer
- (goto-char pos)
- (setq there (org-index--line-in-canonical-form)))
- (unless (string= here there)
- (error "Occur buffer has become stale"))))
- (defun org-index--line-in-canonical-form ()
- "Return current line in its canonical form."
- (org-trim (substring-no-properties (replace-regexp-in-string "\s +" " " (buffer-substring (line-beginning-position) (line-beginning-position 2))))))
- (defun org-index--wrap (text)
- "Wrap TEXT at fill column."
- (with-temp-buffer
- (insert text)
- (fill-region (point-min) (point-max) nil t)
- (buffer-string)))
- (defun org-index--occur-action (&optional other)
- "Helper for `org-index--occur', find heading with ref or id; if OTHER, in other window; or copy yank column."
- (if (org-at-table-p)
- (let ((id (org-index--get-or-set-field 'id))
- (ref (org-index--get-or-set-field 'ref))
- (yank (org-index--get-or-set-field 'yank)))
- (if id
- (org-index--find-id id other)
- (if ref
- (progn
- (org-mark-ring-goto)
- (format "Found reference %s (no node is associated)" ref))
- (if yank
- (progn
- (org-index--update-line (get-text-property (point) 'org-index-lbp))
- (setq yank (replace-regexp-in-string (regexp-quote "\\vert") "|" yank nil 'literal))
- (kill-new yank)
- (org-mark-ring-goto)
- (format "Copied '%s' (no node is associated)" yank))
- (error "Internal error, this line contains neither id, nor reference, nor text to yank")))))
- (message "Not at table")))
- (defun org-index--hide-with-overlays (words lines-wanted)
- "Hide text that is currently visible and does not match WORDS by creating overlays; leave LINES-WANTED lines visible."
- (let ((lines-found 0)
- (end-of-visible (point))
- overlay overlays start matched)
-
- (while (and (not (eobp))
- (< lines-found lines-wanted))
-
- (while (and (not (eobp))
- (and
- (invisible-p (point))
- (< (point) (overlay-start org-index--occur-tail-overlay))))
- (goto-char (overlay-end (car (overlays-at (point))))))
-
- (setq matched nil)
- (setq start (point))
- (while (and (not (eobp))
- (not
- (and
- (invisible-p (point))
- (< (point) (overlay-start org-index--occur-tail-overlay))))
- (not (and (org-index--test-words words)
- (setq matched t))))
- (forward-line 1))
-
- (when (< start (point))
- (setq overlay (make-overlay start (point)))
- (overlay-put overlay 'invisible t)
- (setq overlays (cons overlay overlays)))
-
- (when matched
- (forward-line 1)
- (setq end-of-visible (point))
- (cl-incf lines-found)))
-
- (setq org-index--occur-stack
- (cons (list (cons :overlays overlays)
- (cons :end-of-visible end-of-visible)
- (cons :lines lines-found))
- org-index--occur-stack))
- lines-found))
- (defun org-index--unhide ()
- "Unhide text that does has been hidden by `org-index--hide-with-overlays'."
- (when org-index--occur-stack
-
- (mapc (lambda (y)
- (delete-overlay y))
- (cdr (assq :overlays (car org-index--occur-stack))))
-
- (setq org-index--occur-stack (cdr org-index--occur-stack))
-
- (if org-index--occur-stack (cdr (assq :lines (car org-index--occur-stack))))))
- (defun org-index--test-words (words)
- "Test current line for match against WORDS."
- (let (line)
- (setq line (downcase (buffer-substring (line-beginning-position) (line-beginning-position 2))))
- (catch 'not-found
- (dolist (w words)
- (or (cl-search w line)
- (throw 'not-found nil)))
- t)))
- (defun org-index--create-new-line ()
- "Do the common work for `org-index-new-line' and `org-index'."
-
- (goto-char org-index--below-hline)
- (if (eq org-index-sort-by 'count)
- (progn
- (while (org-at-table-p)
- (forward-line))
- (forward-line -1)
- (org-table-insert-row t))
- (org-table-insert-row))
-
- (org-table-goto-column (org-index--column-num 'created))
- (org-insert-time-stamp nil nil t)
- (org-table-goto-column (org-index--column-num 'count))
- (insert "1"))
- (defun org-index--sort-silent ()
- "Sort index for default column to remove any effects of temporary sorting."
- (save-excursion
- (org-index--verify-id)
- (org-index--parse-table)
- (org-index--on nil nil
- (org-index--do-sort-index org-index-sort-by)
- (org-table-align)
- (remove-hook 'before-save-hook 'org-index--sort-silent))))
- (defun org-index--copy-visible (beg end)
- "Copy the visible parts of the region between BEG and END without adding it to `kill-ring'; copy of `org-copy-visible'."
- (let (snippets s)
- (save-excursion
- (save-restriction
- (narrow-to-region beg end)
- (setq s (goto-char (point-min)))
- (while (not (= (point) (point-max)))
- (goto-char (org-find-invisible))
- (push (buffer-substring s (point)) snippets)
- (setq s (goto-char (org-find-visible))))))
- (apply 'concat (nreverse snippets))))
- (provide 'org-index)
|