12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165 |
- (require 'org-table)
- (require 'cl)
- (defcustom org-index-id nil
- "Id of the Org-mode node, which contains the index table."
- :group 'org
- :group 'org-index)
- (defvar org-index--maxref nil "Maximum number from reference table (e.g. \"153\").")
- (defvar org-index--head nil "Any 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--special-columns nil "Columns with flags, that may appear only once.")
- (defvar org-index--flagged-columns nil "Columns with flags, that may appear multiple times.")
- (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--headings nil "Headlines of index-table as a string.")
- (defvar org-index-map nil "Keymap for shortcuts for some commands of `org-index'. Can be activated and filled by org-index-default-keybings.")
- (defvar org-index--last-action nil "Last action performed by `org-index'.")
- (defvar org-index--text-to-yank nil "Text, that can be yanked after call (mostly a reference).")
- (defvar org-index--last-ref nil "Last reference created or visited.")
- (defvar org-index--point-before nil "Point in buffer with index table.")
- (defvar org-index--point-saved nil "Saved point if we want to return.")
- (defvar org-index--category-before nil "Category of node before.")
- (defvar org-index--silent nil "Set to t, if user should not be queried.")
- (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-node nil "True, if we are within node of the index table.")
- (defvar org-index--active-window-index nil "Active window with index table (if any).")
- (defvar org-index--occur-follow-mode nil "True, if follow mode in occur-buffer is on.")
- (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
- (defconst org-index--commands '(occur add delete head enter leave ref help example reorder sort multi-occur highlight statistics) "List of commands available.")
- (defconst org-index--required-flags '(sort) "Flags that are required.")
- (defconst org-index--single-flags '(sort point-on-add yank-after-add) "Flags, that may only appear once; these can appear as special-columns.")
- (defconst org-index--multiple-flags '(edit-on-add get-category-on-add get-heading-on-add) "Flags, that might appear multiple times.")
- (defconst org-index--all-flags (append org-index--single-flags org-index--multiple-flags) "All flags.")
- (defconst org-index--valid-headings '(ref link created last-accessed count keywords) "All valid headings.")
- (defconst org-index--required-headings org-index--valid-headings "All required headings.")
- (defconst org-index--sample-flags
- "
- - columns-and-flags :: associate columns of index table with flags
- - ref
- - yank-after-add
- - .category
- - get-category-on-add
- - edit-on-add
- - keywords
- - get-heading-on-add
- - edit-on-add
- - point-on-add
- - count
- - sort
- - last-accessed
- - created
- - link
- - all-columns-explained :: All columns of the index table and their meaning
- - ref :: The reference number; will be generated automatically
- - link :: link to the node, that this line represents
- - created :: When has this entry been created ?
- - last-accessed :: When has this entry been accessed last ?
- - count :: How many times has this entry been picked ?
- - keywords :: List of keywords, which may match your input during occur
- - all-flags-explained :: All flags, that can be associated with columns
- - sort :: Sort whole table after this column
- - yank-after-add :: Let this column be yanked after picking this line
- - edit-on-add :: Edit this line when adding a new one
- - point-on-add :: Point will land here, when adding a new line
- - get-category-on-add :: This column will receive the nodes category during add
- - get-heading-on-add :: This column will receive the nodes heading during add"
- "A sample string of flags.")
- (defun org-index (&optional command)
- "Mark and find your favorite things and org-locations easily:
- Create and update an index table of references and links. When
- searching, frequently used entries appear at the top and entering
- some keywords narrows down to matching entries only, so that the
- right one can be spotted easily.
- References are essentially small numbers (e.g. \"R237\" or \"-455-\"),
- as created by this package; they are well suited to be used
- outside of org. Links are normal `org-mode' links.
- This is version 3.0 of org-index.el .
- The function `org-index' operates on a dedicated table, the index
- table, which lives within its own Org-mode node. The table and
- its containing node will be created, when you first invoke
- `org-index'. The node will have some comments, describing the
- columns of the index table and their associated flags. The index
- table is found through the id of the containing node, which is
- stored within the variable `org-index-id'.
- The function `org-index' is the only interactive function of this
- package and its main entry point; once invoked it asks, which of
- it subcommands to execute:
- occur: Incremental search, that shows matching lines from the
- index table, 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: Add the current node to your index, so that it can be
- found through the subcommand \"occur\".
- delete: Delete the current node from your index.
- head: Ask for a reference number and search for an entry, which
- either has this reference contained in its heading or within
- its property org-index-ref.
- enter: Enter index table and maybe go to a specific reference.
- leave: Leave the index table and return to your previous location.
- ref: Create a new reference.
- help: Show this text.
- example: Create a temporary index, that will not be saved, but
- may serve as an example.
- reorder: Temporarily reorder the index table, e.g. by count,
- reference or last access.
- sort: Sort a set of lines (either from the active region or the
- whole buffer) by references found in each line.
- multi-occur: Apply Emacs standard `multi-occur' operation on all
- `org-mode' buffers to search for the given reference.
- highlight: Highlight or unhiglight references in active region or buffer.
- statistics : Show some statistics (e.g. minimum and maximum
- reference) about index table.
- If you invoke `org-index' for the first time, an assistant will be
- invoked, that helps you to create your own, commented index.
- Use `org-index-default-keybindings' to establish convenient
- keyboard shortcuts.
- Optional argument COMMAND is a symbol naming the command to execute."
- (interactive "P")
- (let ((org-index--silent nil)
- prefix-arg
- link-id
- search
- guarded-search
- search-ref
- search-link
- reorder-once
- kill-new-text
- initial-ref-or-link
- message-text)
-
-
-
-
- (org-index--verify-id)
-
- (org-index--parse-table)
-
- (org-index--retrieve-context)
-
-
-
- (when (equal command '(4))
- (setq prefix-arg command)
- (setq command nil))
- (if command
- (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 ",")))
- (let ((r (org-index--read-command)))
- (setq command (car r))
- (setq reorder-once (cdr r))))
-
-
-
-
- (when (memq command '(enter head))
-
- (setq search (org-index--get-or-read-search search command))
- (when search
- (when (string-match org-index--ref-regex search)
- (setq search-ref search)
- (setq guarded-search (org-index--make-guarded-search search)))
- (when (string-match "^[a-fA-F0-9]\\{8\\}-[a-fA-F0-9]\\{4\\}-[a-fA-F0-9]\\{4\\}-[a-fA-F0-9]\\{4\\}-[a-fA-F0-9]\\{12\\}$" search)
- (setq search-link search))))
-
-
-
- (when (and (eq command 'head)
- (not search-ref)
- (not search-link))
- (error "Can do 'head' only for a reference or link (not '%s'), try 'occur' to search for text" search))
-
-
-
-
- (if (eq command 'add)
- (setq link-id (org-id-get-create)))
-
- (if (and org-index--within-node
- (org-at-table-p))
- (setq initial-ref-or-link
- (or (org-index--get-field 'ref)
- (org-index--get-field 'link))))
-
- (when (memq command '(occur multi-occur statistics example))
- (set-buffer org-index--buffer)
- (goto-char org-index--point)
-
- (org-index--sort reorder-once)
- (org-index--align))
-
- (when (memq command '(enter ref))
-
- (org-mark-ring-push)
- (org-pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--point)
- (org-index--unfold-buffer)
-
- (org-index--sort reorder-once)
- (org-index--align)
-
- (if org-index--point-before
- (setq org-index--point-saved org-index--point-before)))
-
- (when initial-ref-or-link
- (while (and (org-at-table-p)
- (not (or
- (string= initial-ref-or-link (org-index--get-field 'ref))
- (string= initial-ref-or-link (org-index--get-field 'link)))))
- (forward-line))
-
- (if (not (org-at-table-p)) (goto-char org-index--point)))
-
-
-
- (cond
- ((eq command 'help)
-
- (describe-function 'org-index))
- ((eq command 'multi-occur)
-
- (goto-char org-index--below-hline)
- (let (found (initial (point)))
- (while (and (not found)
- (forward-line)
- (org-at-table-p))
- (save-excursion
- (setq found (string= search
- (org-index--get-field 'ref)))))
- (if found
- (org-index--update-line nil)
- (goto-char initial)))
-
- (let (buff 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 guarded-search)
-
- (if (get-buffer "*Occur*")
- (progn
- (setq message-text (format "multi-occur for '%s'" search))
- (other-window 1)
- (toggle-truncate-lines 1))
- (setq message-text (format "Did not find '%s'" search)))))
- ((eq command 'add)
- (setq kill-new-text (org-index--do-add link-id)))
- ((eq command 'delete)
- (org-index--do-delete))
- ((eq command 'head)
- (let (link)
- (if (and org-index--within-node
- (org-at-table-p))
- (setq link (org-index--get-field 'link))))
- (setq message-text (org-index--do-head search-ref search-link)))
- ((eq command 'leave)
- (setq kill-new-text org-index--text-to-yank)
- (setq org-index--text-to-yank nil)
-
-
- (if (eq org-index--last-action 'leave)
- (let ((this-command nil) (last-command nil))
- (org-mark-ring-goto 1))
- (org-mark-ring-goto))
-
- (when org-index--point-saved
-
- (if (eq (window-buffer org-index--active-window-index)
- org-index--buffer)
- (set-window-point org-index--active-window-index (marker-position org-index--point-saved)))
-
- (with-current-buffer org-index--buffer
- (goto-char org-index--point-saved)))
- (setq org-index--point-saved nil))
- ((eq command 'enter)
-
- (goto-char org-index--below-hline)
- (if search
- (let (found (initial (point)))
- (while (and (not found)
- (forward-line)
- (org-at-table-p))
- (save-excursion
- (setq found
- (string= search
- (org-index--get-field
- (if search-link 'link 'ref))))))
- (if found
- (progn
- (setq message-text (format "Found '%s'" search))
- (org-index--update-line nil)
- (org-table-goto-column (org-index--column-num 'ref))
- (if (looking-back " ") (backward-char))
-
- (setq org-index--text-to-yank
- (org-trim (org-table-get-field (org-index--column-num 'copy)))))
- (setq message-text (format "Did not find '%s'" search))
- (goto-char initial)
- (forward-line)
- (setq command 'missed)))
-
- (setq message-text "At index table"))
- (recenter))
- ((eq command 'occur)
- (org-index--do-occur))
- ((eq command 'ref)
- (let (new)
-
- (setq new (org-index--create-new-line (eq command 'ref)))
-
- (org-table-goto-column (org-index--column-num 'ref))
- (insert new)
- (setq org-index--last-ref new)
- (org-index--align)
-
- (if (org-index--special-column 'point-on-add)
- (org-table-goto-column (org-index--column-num (org-index--special-column 'point-on-add)))
- (unless (catch 'empty
- (dotimes (col org-index--numcols)
- (org-table-goto-column (+ col 1))
- (if (string= (org-trim (org-table-get-field)) "")
- (throw 'empty t))))
-
- (org-table-goto-column 1)))
- (if org-index--active-region (setq kill-new-text org-index--active-region))
- (setq message-text (format "Adding a new row with ref '%s'" new))))
- ((eq command 'sort)
-
- (let (begin end where)
- (catch 'aborted
-
- (if (and transient-mark-mode
- mark-active)
-
- (progn
- (setq begin (region-beginning))
- (setq end (region-end))
- (setq where "region"))
-
- (setq begin (point-min))
- (setq end (point-max))
- (setq where "whole buffer")
-
- (unless (y-or-n-p "Sort whole buffer? ")
- (setq message-text "Sort aborted")
- (throw 'aborted nil)))
- (save-excursion
- (save-restriction
- (goto-char (point-min))
- (narrow-to-region begin end)
- (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))))
- (highlight-regexp org-index--ref-regex 'isearch)
- (setq message-text (format "Sorted %s from character %d to %d, %d lines"
- where begin end
- (count-lines begin end)))))))
- ((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 prefix-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 'statistics)
- (setq message-text (org-index--do-statistics)))
- ((eq command 'example)
- (if (y-or-n-p "This assistand will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
- (org-index--create-index t)))
- (t (error "This is a bug: unmatched case '%s'" command)))
-
- (setq org-index--last-action 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) ""))))
- (unless (string= m "")
- (message m)
- (setq org-index--message-text m)))
- (if kill-new-text (kill-new kill-new-text))))
- (defun org-index-default-keybindings ()
- "Set default keybindings for `org-index'.
- Establish the common prefix key `C-c i' which is followed by the
- first letter of a subcommand, so that `C-c i a' invokes the
- subcommand \"add\". Subcommands available are occur, add, delete,
- head, enter, leave and ref. As a special case `C-c i i' invokes
- `org-index' to let you choose."
- (define-prefix-command 'org-index-map)
- (global-set-key (kbd "C-c i") 'org-index-map)
- (define-key org-index-map (kbd "i") (lambda () (interactive) (org-index)))
- (define-key org-index-map (kbd "o") (lambda () (interactive) (org-index 'occur)))
- (define-key org-index-map (kbd "a") (lambda () (interactive) (org-index 'add)))
- (define-key org-index-map (kbd "d") (lambda () (interactive) (org-index 'delete)))
- (define-key org-index-map (kbd "h") (lambda () (interactive) (org-index 'head)))
- (define-key org-index-map (kbd "e") (lambda () (interactive) (org-index 'enter)))
- (define-key org-index-map (kbd "l") (lambda () (interactive) (org-index 'leave)))
- (define-key org-index-map (kbd "r") (lambda () (interactive) (org-index 'ref))))
- (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.
- Example:
- (org-index-new-line 'ref t 'link \"7f480c3e\")
- Passing \"'ref t\" will make the function create a new reference
- within the new line.
- Optional argument KEYS-VALUES specifies content of new line."
- (org-index--verify-id)
- (org-index--parse-table)
- (car (org-index--do-new-line keys-values)))
- (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."
- (let ((org-index--silent t))
- (save-excursion
- (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 (eq k 'ref)
- (unless (memq v '(t nil))
- (error "Column 'ref' accepts only t or nil"))
- (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))))
- (if (and (not (plist-get keys-values 'ref))
- (not (stringp (plist-get keys-values 'link))))
- (error "Need a link when not creating a ref"))
- (let (ref yank)
-
- (setq ref (org-index--create-new-line (plist-get keys-values 'ref)))
- (plist-put keys-values 'ref (or ref ""))
-
- (let ((kvs keys-values)
- k v n)
- (while kvs
- (setq k (car kvs))
- (setq v (cadr kvs))
- (setq n (org-index--column-num k))
- (org-table-goto-column n)
- (insert v)
- (setq kvs (cddr kvs))))
-
- (setq yank (org-trim (org-table-get-field (org-index--column-num (org-index--special-column 'yank-after-add)))))
- (org-index--sort)
- (cons ref yank))))))
- (defun org-index-get-line (type value)
- "Retrieve an existing line within the index table by ref or link.
- 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 \"12\") 'count)
- retrieves the value of the count-column for reference 12.
- Argument TYPE is a symbol, either ref or link,
- argument VALUE specifies the value to search for."
-
- (unless (memq type '(ref link))
- (error "Argument can only be 'ref' or 'link'"))
- (org-index--verify-id)
- (org-index--parse-table)
- (org-index--get-or-delete-line 'get type value))
- (defun org-index--get-or-delete-line (command type value)
- "Get or delete (according to COMMAND) a line by TYPE and VALUE."
- (let ((org-index--silent t)
- found)
- (save-excursion
- (org-index--retrieve-context)
- (with-current-buffer org-index--buffer
- (goto-char org-index--point)
- (goto-char org-index--below-hline)
- (while (and (not found)
- (org-at-table-p))
- (when (string= (org-index--get-field type)
- value)
-
- (if (eq command 'get)
-
- (mapc (lambda (x)
- (if (and (numberp (cdr x))
- (> (cdr x) 0))
- (setq found (cons (car x) (cons (or (org-index--get-field (car x)) "") found)))
- )) (reverse org-index--columns))
-
- (let ((start (point)))
- (beginning-of-line)
- (forward-line)
- (delete-region start (point)))))
- (forward-line))))
- found))
- (defun org-index--read-command ()
- "Find out, what we are supposed to do."
- (let (reorder-once
- command
- input)
-
- (while (progn
- (setq input
- (org-completing-read
- "Please choose: "
- (mapcar 'symbol-name org-index--commands)
- nil nil))
-
- (setq command (intern input))
-
- (if (eq command 'reorder)
- (setq reorder-once
- (intern
- (org-icompleting-read
- "Please choose column to reorder index table once: "
- (list "ref" "count" "created" "last-accessed")
- nil t))))
-
- (eq command 'reorder)))
- (cons command reorder-once)))
- (defun org-index--get-or-read-search (search command)
- "Get SEARCH string, maybe read from user; respect COMMAND that will be executed."
- (let (search-from-table
- search-from-cursor)
- (unless search
-
-
- (when (and org-index--within-node
- (org-at-table-p))
- (setq search-from-table (or (org-index--get-field 'link)
- (org-index--get-field 'ref))))
-
- (when (and (not org-index--within-node)
- org-index--below-cursor
- (string-match (concat "\\(" org-index--ref-regex "\\)")
- org-index--below-cursor))
- (setq search-from-cursor (match-string 1 org-index--below-cursor)))
-
- (cond ((eq command 'enter)
- (setq search search-from-cursor))
- ((memq command '(head occur))
- (setq search (or search-from-table search-from-cursor)))))
-
- (unless search
- (if org-index--silent (error "Need to specify search, if silence is required"))
- (unless (eq command 'occur)
- (setq search (read-from-minibuffer
- (cond ((eq command 'head)
- "Text or reference number to search for: ")
- ((eq command 'enter)
- "Reference number to search for (or <empty> for id of current node, `l' for last ref created, `t' for top of index table): "))))))
-
- (when (eq command 'enter)
- (if (string= search "") (setq search (org-id-get)))
- (if (string= search "t") (setq search nil))
- (if (string= search "l") (setq search (number-to-string org-index--maxref))))
-
- (when search
- (setq search (org-trim search))
- (if (string= search "") (setq search nil))
- (when search
- (if (string-match "^[0-9]+$" search)
- (setq search (concat org-index--head search org-index--tail)))))
- search))
- (defun org-index--verify-id ()
- "Check, that we have a valid id."
-
- (unless org-index-id
- (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet."))
-
- (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)))
-
- (setq org-index--within-node (string= (org-id-get) org-index-id))
-
- (if (eq (window-buffer) org-index--buffer)
- (setq org-index--active-window-index (selected-window)))
-
- (with-current-buffer org-index--buffer
- (setq org-index--point-before
- (if (string= (org-id-get) org-index-id)
- nil
- (point-marker)))))
- (defun org-index--parse-table ()
- "Parse content of index table."
- (let (ref-field
- link-field
- initial-point
- end-of-headings
- start-of-headings)
- (with-current-buffer org-index--buffer
- (setq org-index--maxref 0)
- (setq initial-point (point))
- (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 (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)
- (forward-line 2)
-
- (goto-char org-index--point)
- (org-index--parse-flags)
- (org-index--go-below-hline)
- (setq org-index--below-hline (point-marker))
-
- (goto-char org-index--below-hline)
- (while (and (org-at-table-p)
- (not (setq ref-field (org-index--get-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))
-
- (let ((ref 0))
- (while (org-at-table-p)
- (setq ref-field (org-index--get-field 'ref))
- (setq link-field (org-index--get-field 'link))
- (when (and (not ref-field)
- (not link-field))
- (kill-whole-line)
- (message "Removing line from index-table with both ref and link empty"))
- (if ref-field
- (if (string-match org-index--ref-regex ref-field)
-
- (setq ref (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 (> ref org-index--maxref) (setq org-index--maxref ref))
- (forward-line 1)))
-
- (goto-char initial-point))))
- (defun org-index--sort (&optional sort-column)
- "Sort index table maybe according to SORT-COLUMN."
- (unless sort-column (setq sort-column (org-index--special-column 'sort)))
- (let ((is-modified (buffer-modified-p))
- top
- bottom
- ref-field
- count-field)
- (unless buffer-read-only
-
- (goto-char org-index--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-field 'ref))
- (not (org-index--get-field 'link))))
- (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 ()
- (let (ref
- (ref-field (or (org-index--get-field 'ref) ""))
- (count-field (or (org-index--get-field 'count) "")))
-
-
- (string-match org-index--ref-regex ref-field)
- (setq ref (format
- "%06d"
- (string-to-number
- (or (match-string 1 ref-field)
- "0"))))
-
-
- (cond
- ((eq sort-column 'count)
- (concat (format
- "%08d"
- (string-to-number (or (org-index--get-field 'count)
- "")))
- ref))
- ((eq sort-column 'ref)
- ref)
- ((eq sort-column 'last-accessed)
- (concat (org-index--get-field sort-column)
- " "
- ref))
- (t (error "This is a bug: unmatched case '%s'" sort-column)))))
- nil 'string<))
-
- (set-buffer-modified-p is-modified))))
- (defun org-index--go-below-hline ()
- "Move below hline in index-table."
- (let ((count 0)
- (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)))
- (defun org-index--align ()
- "Align index table without setting its buffer modified."
- (let ((is-modified (buffer-modified-p)))
- (unless buffer-read-only (org-table-align))
- (set-buffer-modified-p is-modified)
- (org-index--go-below-hline)
- (setq org-index--below-hline (point-marker))))
- (defun org-index--parse-headings ()
- "Parse headings of index table."
- (let (field
- field-symbol
- found)
- (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 "Column name 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--required-headings))
- (defun org-index--parse-flags ()
- "Parse list of flags in index table."
- (let (parent parent-is-comment child)
-
- (setq org-index--special-columns nil)
- (setq org-index--flagged-columns nil)
- (org-index--goto-list "columns-and-flags" t)
- (forward-line 1)
-
- (while (and (setq parent (org-index--parse-list-item))
- parent
- (> (cdr (assoc :indent parent)) 0))
- (setq parent-is-comment (member (cdr (assoc :text parent)) '("all-columns-explained" "all-flags-explained")))
-
- (unless (or parent-is-comment
- (assoc (cdr (assoc :sym parent)) org-index--columns))
- (org-index--report-index-error "'%s' is not a valid column" (cdr (assoc :text parent))))
-
- (while (and (forward-line 1)
- (setq child (org-index--parse-list-item))
- child
- (> (cdr (assoc :indent child))
- (cdr (assoc :indent parent))))
- (unless parent-is-comment
-
- (unless (memq (cdr (assoc :sym child)) org-index--all-flags)
- (org-index--report-index-error "'%s' is not a valid flag" (cdr (assoc :text child))))
-
- (if (memq (cdr (assoc :sym child)) org-index--single-flags)
-
- (if (assoc (cdr (assoc :sym child)) org-index--special-columns)
- (org-index--report-index-error
- "More than one column is marked with flag '%s'" (cdr (assoc :text child)))
-
- (setq org-index--special-columns (cons (cons (cdr (assoc :sym child)) (cdr (assoc :sym parent)))
- org-index--special-columns))))
-
- (let ((l (assoc (cdr (assoc :sym child)) org-index--flagged-columns)))
- (unless l
-
- (setq org-index--flagged-columns
- (cons (cons (cdr (assoc :sym child)) nil)
- org-index--flagged-columns))
- (setq l (car org-index--flagged-columns)))
-
- (setcdr l (cons (cdr (assoc :sym parent)) (cdr l)))))))
-
- (mapc (lambda (x)
- (unless (assoc x org-index--special-columns)
- (org-index--report-index-error "Required flag '%s' does not appear" (substring (symbol-name x) 1))))
- org-index--required-flags)))
- (defun org-index--goto-list (name &optional required non-top)
- "Goto list NAME (maybe NON-TOP Level) in index node, err if REQUIRED list is not present."
- (goto-char org-index--point)
-
- (while (not (org-at-heading-p)) (forward-line -1))
- (forward-line 1)
-
- (while (and (not (let ((item (org-index--parse-list-item)))
- (if item
- (and (or non-top (= (cdr (assoc :indent item)) 0))
- (string= (cdr (assoc :text item)) name))
- nil)))
- (not (org-at-table-p))
- (not (org-at-heading-p))
- (not (eobp)))
- (forward-line 1))
- (if (org-at-item-p)
- t
- (if required
- (org-index--report-index-error "Could not find required list '%s'" name)
- nil)))
- (defun org-index--parse-list-item ()
- "Parse a list item into an assoc array (indent, checkbox, text, value)."
- (if (looking-at org-list-full-item-re)
-
- (let (alist indent checkbox text value)
- (setq indent (save-excursion
- (goto-char (match-beginning 1))
- (current-column)))
- (decf indent (+ (save-match-data (org-current-level)) 1))
- (add-to-list 'alist (cons :indent indent))
- (setq checkbox (match-string 3))
- (setq text (match-string 4))
- (setq value (buffer-substring
- (match-end 0)
- (save-excursion (end-of-line) (point))))
- (when (not text)
- (setq text value)
- (setq value nil))
- (add-to-list 'alist (cons :text text))
- (add-to-list 'alist (cons :value value))
- (add-to-list 'alist (cons :sym (intern text)))
-
- (mapc (lambda (x) (if (stringp (cdr x)) (setf (cdr x) (org-trim (substring-no-properties (cdr x)))))) alist)
- alist)
- nil))
- (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 your 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."
- (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))
- (if org-index--silent (error (concat explanation reason)))
- (setq prompt (concat explanation reason "\n\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 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)) nil nil))))
- (setq title (read-from-minibuffer "Please enter the title of the index node: "))
- (while (progn
- (setq firstref (read-from-minibuffer "Please enter your first reference-number. This is a number preceeded by some non-digit chars and optionally followed by some more non-digit chars, e.g. 'R1', '-1-' or '#1#' (and your initial number does not need to be '1'). The format of your reference-numbers only needs to make sense for yourself, so that you can spot it easily in your texts or write it on a piece of paper; it should however not already appear to frequently within your existing notes, to avoid too many false hits when searching.\n\nPlease choose: "))
- (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.
- ")
- (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, 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
- read the description of `org-index'.
- Within the index table below, dhe sequence of columns does not
- matter. You may reorder them any way you like. Columns are
- found by their heading. You may also add your own columns.
- Following these explanations there is the item-list
- `columns-and-flags', which influences the behaviour of
- `org-index'. See the explanations which are part of this list.
- This node needs not be a top level node; its name is completely
- at your choice; it is found through its ID only.
- Remark: These lines of explanation can be removed at any time.
- ")
- (setq id (org-id-get-create))
- (insert (format "
- %s
- | ref | .category | keywords | count | last-accessed | created | link |
- | | | | | | | <4> |
- |-----+-----------+----------+-------+---------------+---------+------|
- | %s | | %s | | | %s | %s |
- "
- org-index--sample-flags
- firstref
- "This node"
- (with-temp-buffer (org-insert-time-stamp nil nil t))
- id))
- (save-excursion(org-index--goto-list "columns-and-flags")
- )
-
- (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))
-
- (if temporary
- (progn
-
- (when compare
- (org-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)))
-
- (org-pop-to-buffer-same-window buffer)
- (org-id-goto id)
- (org-index--unfold-buffer)
- (if compare
- (error "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
- (message "This is your new temporary index.")))
- (progn
-
- (org-pop-to-buffer-same-window buffer)
- (delete-other-windows)
- (org-id-goto id)
- (org-index--unfold-buffer)
- (setq org-index-id id)
- (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)
- (error "Saved org-index-id '%s' to %s" id custom-file))
- (let (sq)
- (setq sq (format "(setq org-index-id \"%s\")" id))
- (kill-new sq)
- (error "Did not make the id of this new index permanent; you may want to put\n\n %s\n\ninto your own initialization; it is copied already, just yank it" sq))))))))
- (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)
- (org-index--goto-list "columns-and-flags")
- (org-cycle)))
- (defun org-index--update-line (ref-or-link)
- "Update columns count and last-accessed in line REF-OR-LINK."
- (let ((newcount 0)
- initial)
- (with-current-buffer org-index--buffer
- (unless buffer-read-only
-
- (when ref-or-link
- (setq initial (point))
- (goto-char org-index--below-hline)
- (while (and (org-at-table-p)
- (not (or (string= ref-or-link (org-index--get-field 'ref))
- (string= ref-or-link (org-index--get-field 'link)))))
- (forward-line)))
- (if (not (org-at-table-p))
- (error "Did not find reference or link '%s'" ref-or-link)
- (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-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-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)))
- (defun org-index--get-field (key &optional value)
- "Retrieve field KEY from index table or set it to VALUE."
- (let (field)
- (save-excursion
- (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--special-column (key)
- "Return column (not a number) for special column KEY."
- (cdr (assoc key org-index--special-columns)))
- (defun org-index--flag-p (flag column)
- "Check if COLUMN has FLAG set."
- (unless (memq flag org-index--all-flags)
- (error (format "Internal error: unknown flag %s" (symbol-name flag))))
- (memq column (assoc flag org-index--flagged-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--do-statistics ()
- "Compute statistics about index table."
- (let ((total 0)
- ref-field
- ref
- min
- max
- message)
-
- (goto-char org-index--below-hline)
- (while (org-at-table-p)
-
- (setq ref-field (org-index--get-field 'ref))
- (if (and 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 (1+ total))
- (forward-line))
- (setq message (format "Found %d references from %s to %s."
- total
- (format org-index--ref-format min)
- (format org-index--ref-format max)))
- message))
- (defun org-index--do-add (link-id)
- "For current node (with id LINK-ID): add a new line to index table."
- (let ((args (list 'ref t 'link link-id))
- ref-and-yank content)
- (unless (org-at-heading-p)
- (error "Not at headline"))
-
- (dolist (col-num org-index--columns)
- (setq content "")
-
- (if (org-index--flag-p 'get-heading-on-add (car col-num))
- (setq content (nth 4 (org-heading-components))))
-
- (if (org-index--flag-p 'get-category-on-add (car col-num))
- (setq content org-index--category-before))
- (if (org-index--flag-p 'edit-on-add (car col-num))
- (read-from-minibuffer
- (format "Edit text for column '%s': " (symbol-name (car col-num)))
- content))
- (if (not (string= content ""))
- (setq args (append (list (car col-num) content) args))))
-
- (setq ref-and-yank (apply 'org-index--do-new-line args))
-
- (org-entry-put (point) "org-index-ref" (car ref-and-yank))
- (cdr ref-and-yank)))
- (defun org-index--do-delete ()
- "Perform command delete."
- (unless (org-at-heading-p)
- (error "Not at headline"))
- (let ((ref (org-entry-get (point) "org-index-ref")))
-
- (org-index--get-or-delete-line 'delete 'ref ref)
-
- (org-entry-delete (point) "org-index-ref")
-
- (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--do-head (ref link &optional other)
- "Perform command head: Find node with REF or LINK and present it; if OTHER in separate window."
- (if ref (setq org-index--last-ref ref))
- (let (message)
-
- (if link
- (progn
- (org-index--update-line link)
- (org-id-goto link)
- (org-reveal)
- (setq message "Followed link"))
- (message (format "Scanning headlines for '%s' ..." ref))
- (org-index--update-line ref)
- (let ((search (concat ".*" (org-index--make-guarded-search ref)))
- (org-trust-scanner-tags t)
- buffer point)
- (if (catch 'found
- (progn
-
- (org-map-entries
- (lambda ()
- (when (or (looking-at search)
- (eq ref (org-entry-get (point) "org-index-ref")))
-
- (when (< (org-element-property :level (org-element-at-point))
- org-inlinetask-min-level)
-
- (setq buffer (current-buffer))
- (setq point (point))
- (throw 'found t))))
- nil 'agenda)
- nil))
- (progn
- (setq message (format "Found '%s'" (or ref link)))
- (if other
- (progn
- (pop-to-buffer buffer)
- (goto-char point)
- (org-reveal t)
- (recenter)
- (pop-to-buffer "*org-index-occur*"))
- (org-pop-to-buffer-same-window buffer)
- (goto-char point)
- (org-reveal t)
- (recenter)))
- (setq message (format "Did not find '%s'" (or ref link))))))
- message))
- (defun org-index--do-occur ()
- "Perform command occur."
- (let ((occur-buffer-name "*org-index-occur*")
- (word "")
- (prompt "Search for: ")
- (hint "")
- (key-help "<up>, <down> move. <return> finds node, <tab> finds in other window.\n")
- words
- occur-buffer
- lines-to-show
- start-of-lines
- start-of-help
- left-off-at
- after-inserted
- at-end
- lines-visible
- below-hline-bol
- exit-gracefully
- in-c-backspace
- show-headings
- fun-on-ret
- fun-on-s-ret
- fun-on-m-ret
- fun-on-tab
- ret from to key)
-
- (if (get-buffer "*org-index-occur*")
- (kill-buffer occur-buffer-name))
- (setq occur-buffer (get-buffer-create "*org-index-occur*"))
-
- (with-current-buffer occur-buffer
- (let ((keymap (make-sparse-keymap)))
- (set-keymap-parent keymap org-mode-map)
- (setq fun-on-ret (lambda () (interactive) (org-index--occur-find-heading nil)))
- (define-key keymap [return] fun-on-ret)
- (setq fun-on-tab (lambda () (interactive)
- (org-index--occur-find-heading t)
- (setq org-index--occur-follow-mode (not org-index--occur-follow-mode))))
- (define-key keymap [tab] fun-on-tab)
- (define-key keymap [(control ?i)] fun-on-tab)
- (define-key keymap [up] (lambda () (interactive)
- (forward-line -1)
- (if org-index--occur-follow-mode (org-index--occur-find-heading t))))
- (define-key keymap [down] (lambda () (interactive)
- (forward-line 1)
- (if org-index--occur-follow-mode (org-index--occur-find-heading t))))
- (use-local-map keymap)))
- (with-current-buffer org-index--buffer
- (let ((initial (point)))
- (goto-char org-index--below-hline)
- (forward-line 0)
- (setq below-hline-bol (point))
- (goto-char initial)))
- (org-pop-to-buffer-same-window occur-buffer)
- (toggle-truncate-lines 1)
- (unwind-protect
- (progn
-
- (erase-buffer)
- (insert (concat "Incremental search, showing one window of matches. '?' toggles help.\n\n"))
- (setq start-of-lines (point-marker))
- (setq start-of-help start-of-lines)
- (setq cursor-type 'hollow)
-
- (setq lines-to-show (+ (- (window-body-height) (line-number-at-pos)) 1))
-
- (setq ret (org-index--get-matching-lines nil lines-to-show below-hline-bol))
- (when (nth 0 ret)
- (insert (nth 1 ret))
- (setq left-off-at (cons (nth 0 ret) nil))
- (setq after-inserted (cons (point) nil)))
-
- (while
- (progn
- (goto-char start-of-lines)
- (setq lines-visible 0)
- (if in-c-backspace
- (setq key 'backspace)
- (let ((search-text (mapconcat 'identity (reverse (cons word words)) ",")))
- (setq key (read-key
- (format "%s%s%s%s"
- prompt
- search-text
- (if (string= search-text "") "" " ")
- hint))))
- (setq hint "")
- (setq exit-gracefully (member key (list 'up 'down 'left 'right 'RET ?\C-g ?\C-m
- 'C-return 'S-return ?\C-i 'TAB))))
- (not exit-gracefully))
- (cond
- ((eq key 'C-backspace)
- (setq in-c-backspace t))
- ((member key (list 'backspace 'deletechar ?\C-?))
- (if (= (length word) 0)
-
- (progn
- (setq word (car words))
- (setq words (cdr words))
- (setq in-c-backspace nil))
-
- (let ((case-fold-search t))
- (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))
-
- (when (cdr after-inserted)
- (setq after-inserted (cdr after-inserted))
- (goto-char (car after-inserted))
- (delete-region (point) (point-max)))
-
- (when (cdr left-off-at)
- (setq left-off-at (cdr left-off-at)))
-
- (goto-char start-of-lines)
- (while (< (point) (point-max))
- (if (outline-invisible-p)
- (progn
- (setq from (line-beginning-position)
- to (line-beginning-position 2))
-
- (when (org-index--test-words (cons word words) (buffer-substring from to))
- (when (<= lines-visible lines-to-show)
- (outline-flag-region from to nil)
- (incf lines-visible))))
-
- (incf lines-visible))
- (forward-line 1))
-
- (unless (= (length word) 0)
- (let ((case-fold-search t))
- (highlight-regexp (regexp-quote word) 'isearch)))))
- ((member key (list ?\s ?,))
-
- (setq words (cons word words))
- (setq word ""))
- ((eq key ??)
- (setq show-headings (not show-headings))
- (goto-char start-of-lines)
- (if show-headings
- (progn
- (forward-line -1)
- (setq start-of-help (point-marker))
- (insert "Normal keys add to search word, SPACE or COMMA start new word, BACKSPACE and C-BACKSPACE erase char or word. Every other key ends search. ")
- (insert key-help)
- (goto-char start-of-help)
- (fill-paragraph)
- (goto-char start-of-lines)
- (insert org-index--headings))
- (delete-region start-of-help start-of-lines)
- (insert "\n\n"))
- (setq start-of-lines (point-marker)))
- ((and (integerp key)
- (aref printable-chars key))
-
- (unless (= (length word) 0)
- (let ((case-fold-search t))
- (unhighlight-regexp (regexp-quote word))))
-
- (setq word (concat word (char-to-string key)))
-
- (while (< (point) (point-max))
- (unless (outline-invisible-p)
- (setq from (line-beginning-position)
- to (line-beginning-position 2))
-
- (if (org-index--test-words (list word) (buffer-substring from to))
- (incf lines-visible)
- (outline-flag-region from to t)))
- (forward-line 1))
-
- (setq left-off-at (cons (car left-off-at) left-off-at))
- (setq after-inserted (cons (car after-inserted) after-inserted))
-
- (when (< lines-visible lines-to-show)
- (setq ret (org-index--get-matching-lines (cons word words)
- (- lines-to-show lines-visible)
- (car left-off-at)))
- (when (nth 0 ret)
- (insert (nth 1 ret))
- (setq at-end (nth 2 ret))
- (setcar left-off-at (nth 0 ret))
- (setcar after-inserted (point))))
-
- (let ((case-fold-search t))
- (highlight-regexp (regexp-quote word) 'isearch)))
- (t
- (setq hint (format "(cannot search for key '%s', use %s to quit)"
- (if (symbolp key)
- key
- (key-description (char-to-string key)))
- (substitute-command-keys "\\[keyboard-quit]"))))))
-
-
- (goto-char start-of-lines)
- (while (< (point) (point-max))
- (if (outline-invisible-p)
- (delete-region (line-beginning-position) (line-beginning-position 2))
- (forward-line 1))))
-
- (setq cursor-type t)
-
- (let ((numlines (count-lines (point) start-of-lines)))
- (goto-char start-of-lines)
- (delete-region (point-min) (point))
- (insert (format (concat (if exit-gracefully "Search is done;" "Search aborted;")
- (if at-end
- " showing all %d matches. "
- " showing one window of matches. ")
- key-help)
- numlines))
- (insert "\n")
- (setq start-of-lines (point-marker))
- (goto-char (point-min))
- (fill-paragraph)
- (goto-char start-of-lines)
- (if show-headings (insert "\n\n" org-index--headings)))
- (forward-line))
-
- (forward-line -1)
- (cond
- ((member key (list 'RET ?\C-m))
- (funcall fun-on-ret))
- ((member key (list 'TAB ?\C-i))
- (funcall fun-on-tab))
- ((eq key 'up)
- (forward-line -1))
- ((eq key 'down)
- (forward-line 1)))))
- (defun org-index--occur-find-heading (x)
- "Helper for keymap of occur: find heading, if X in other window."
- (interactive)
- (save-excursion
- (let ((ref (org-index--get-field 'ref))
- (link (org-index--get-field 'link)))
- (message (org-index--do-head ref link x)))))
- (defun org-index--create-new-line (create-ref)
- "Do the common work for `org-index-new-line' and `org-index'. CREATE-REF asks for new reference."
- (let (new)
- (when create-ref
-
- (unless new
- (setq new (format "%s%d%s" org-index--head (1+ org-index--maxref) org-index--tail)))
-
- (setq org-index--text-to-yank new))
-
- (goto-char org-index--below-hline)
- (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")
- new))
- (defun org-index--get-matching-lines (words numlines start-from)
- "Helper for occur: search for WORDS and get NUMLINES lines from index table, starting at START-FROM."
- (let ((numfound 0)
- pos
- initial line lines at-end)
- (with-current-buffer org-index--buffer
-
- (setq initial (point))
- (goto-char start-from)
-
- (while (and (or (< numfound numlines)
- (= numlines 0))
- (org-at-table-p))
-
- (setq line (buffer-substring (line-beginning-position) (line-beginning-position 2)))
- (when (org-index--test-words words line)
- (setq lines (concat lines line))
- (incf numfound))
- (forward-line 1)
- (setq pos (point)))
- (setq at-end (not (org-at-table-p)))
-
- (goto-char initial))
- (unless lines (setq lines ""))
- (list pos lines at-end)))
- (defun org-index--test-words (words line)
- "Test LINE for match against WORDS."
- (let ((found-all t))
- (setq line (downcase line))
- (catch 'not-found
- (dolist (w words)
- (or (search w line)
- (throw 'not-found nil)))
- t)))
- (defadvice org-mark-ring-goto (after org-index--advice-text-to-yank activate)
- "Make text from `org-index' available for yank."
- (when org-index--text-to-yank
- (kill-new org-index--text-to-yank)
- (message (format "Ready to yank '%s'" org-index--text-to-yank))
- (setq org-index--text-to-yank nil)))
- (provide 'org-index)
|