;; TOC = Table of Contents. ;; Emacs-Time-stamp: "2007-10-25 20:11:59" (setq file-stamp "Emacs-File-stamp: \"/home/ysverdlov/leninist.biz/lb-footn.el\"") ;; (defun tx-check-footnotes (&optional arg1fromhere arg1not-interactive) " ... Check footnotes from point-min. ARG1, if C-u, checks from current point. ARG1, if greater than 4, checks from page ARG1. Optional ARG2, if non-nil when called from tx-check-footnotes//lb-tx-make-or-refresh-indextx//lb-ht-tenderize-tx, will suppress attempts to insert _-_-_ while checking foonotes" ;; (interactive "p") (let (lb-pg lb-str-^^ lb-pt-page-end lb-cons-tx-footnote-end lb-pt-start lb-pt-stop-checking lb-pt-footnote-end _rc) ;; 2007.10.17: fix: ;; in footnotes area, look for stuff like "." after "
\n" & before "^^3^^" ;; 2007.10.25: fix: ;; Check for matching pairs: __NOTE__ Footnote cont. on page 157. (save-match-data (progn (write-region (point-min) (point-max) "~/foofootnotes")) (cond ((null arg1fromhere) (setq lb-pt-start (point-min))) ((= 1 arg1fromhere) (setq lb-pt-start (point-min))) ((= 4 arg1fromhere) (setq lb-pt-start (point))) ((< 4 arg1fromhere) ;; Reposition: (lb-tx-goto-page arg1fromhere) (setq lb-pt-start (point))) (t (setq lb-pt-start (point-min)))) (save-excursion (setq lb-pt-stop-checking (save-excursion (goto-char (point-min)) (if (search-forward-string "__ALPHA_LVL0__" nil t) (point) (point-max)))) ;; 2006.10.16 (goto-char lb-pt-start) (message "%s" (concat "Checking footnotes from page " (lb-tx-what-page) "...")) (while (search-forward-regexp lb-re-bracketed-para-integer ;; Do not check past __ALPHA_LVL0__ ;; 2006.11.10 ;; nil lb-pt-stop-checking t) (lb-tx-check-^^ arg1not-interactive (match-string-no-properties 0)) ) ;; 2006.10.16 (goto-char lb-pt-start) (while (search-forward-string "__NOTE__ Footnote" nil t) (goto-char (cdr (_-where-double-newlines))) (_-move-forward-whitespace) (if (and (looking-at "
to __PRINTERS_P_999_COMMENT__ ?" (_-buffer-substring-from-)))) ;; 2007.06.07 - getting "missing
" deep into processing. (goto-char lb-pt-start) (while (search-forward-string lb-str-footnote-_-_-_ nil t) (setq lb-pt-page-end (lb-tx-page-point "page-end")) (setq lb-pg (lb-tx-what-page)) ;; (while (search-forward-regexp _-re-footnote-marker-global lb-pt-page-end t) (setq lb-str-^^ (match-string 1)) (progn (goto-char (car ;; cdr is page number if footnote continued on next page. (setq lb-cons-tx-footnote-end (lb-tx-footnote-end)))) (_-move-backward-whitespace t)) ;; (if (and (not (cdr lb-cons-tx-footnote-end)) (not (looking-at-backward "")) ;; 2007.07.12 ) (error "%s: %s" (concat "Page " lb-pg "," " missing in " lb-str-^^) ;; (car (_-para)) (_-buffer-substring-from-) )))) ) (message "%s" "Checking footnotes... done.") )_rc)) ;; (defun lb-ht-footnote-pop-it nil "Pop, or delete and return, entire or remaining footnote text starting from current point. Jump to next page if lb-re-footnote-continued found at end of footnotes region. Assumes footnote is in raw .tx format. Does not fetch end of footnote from next section, but this function is called by function that handles such cases. Deletes _-_-_ if no alphas found between _-_-_ and end of footnotes region" ;; (let (lb-flag-break lb-str-pg-previous lb-cons-tx-footnote-end lb-pt lb-re lb-str lb-int lb-end lb-rc) ;; fix? ;; save-excursion? save-match-data? ;; where is point before first iteration? ;; lb-ht-footnote-pop-from-next-section: ;; ;; fix! ;; Check that point is in footnotes area: _-_-_ before "page-beg" or ;; point after "footnotes-beg". ;; where is point before first iteration? ;; lb-ht-footnotes-anchor-and-move: ;; (_-move-forward-whitespace) (while (and (setq lb-end (car (setq lb-cons-tx-footnote-end (lb-tx-footnote-end)))) ;; 2006.09.05 - May be at %%div-class-notes-start%% ! (not (= (point) lb-end)) (not (_-just-say-nil 'lb-flag-break))) ;; fix? ;; insert real page number if part of footnote text from other page? ;; Pop text. (setq lb-rc (concat (_-just-say-nil 'lb-rc) ;; ASSUMPTION: If not empty, lb-rc ends w/ blank line. ;; "\n\n" (progn (setq lb-str (buffer-substring-no-properties (setq lb-beg (point)) lb-end)) (delete-region lb-beg lb-end) ;; Point might be to left of page number ;; for this page if numbers at bottom. ;; ;; (save-excursion (insert "\n\n\n")) lb-str) ;; "\n\n" )) ;; If all footnote text is deleted from a page, delete _-_-_ divider. (when (not (string-match "[a-zA-Z]" (buffer-substring (lb-tx-page-point "footnotes-beg") (lb-tx-page-point "footnotes-end")))) (goto-char (lb-tx-page-point "footnotes-beg")) (if (looking-at lb-re-footnote-div) (replace-match "") (error "%s: %s" "Expecting" lb-re-footnote-div))) ;; Reached true/real end of footnote text? (if (not (cdr lb-cons-tx-footnote-end)) ;; It does not end with lb-re-footnote-continued, break out. (setq lb-flag-break t) ;; Otherwise, footnote text continues on next page. ;; Check where it says to look for continuation. (if (/= ;; Page number (string) where continuation is expected. (string-to-int (setq lb-str (cdr lb-cons-tx-footnote-end))) ;; Add 1 to current page (before goto next page). (1+ (setq lb-int (string-to-int (setq lb-str-pg-previous (lb-tx-what-page)))))) (error "%s: %s" (concat "Expecting __NOTE__ to say contined on page " (int-to-string (1+ lb-int)) ", not") lb-str) ;; (lb-tx-goto-page (1+ (string-to-int lb-str-pg-previous)))) ;; fix? ;; Insert actual page number of next page? ;; Or pretend continuation of footnote is on same page as its start? ;; Chance that footnote continues in next section already ;; accounted for by lb-ht-footnote-pop-from-next-section. ;; Jump to footnote area. (goto-char (lb-tx-page-point "footnotes-beg")) ;; Move past footnotes bar. (goto-char (cdr (_-where-double-newlines))) (_-move-forward-whitespace) ;; Check continuation note to see if it mentions correct page number. (if (not (looking-at (setq lb-re lb-re-footnote-continued))) (error "%s: %s" (concat "On page " (lb-tx-what-page) " regexp not found") (concat lb-re "\n" (_-buffer-substring-from-))) (if (not (string= "from" (match-string-no-properties 2))) (error "%s: %s" "Expecting 'from' variation of" (concat lb-re "\n" (_-buffer-substring-from-))) (if (not (string= lb-str-pg-previous (match-string-no-properties 3))) (error "%s: %s" (concat "Expecting cont. on page " lb-str-pg-previous) (concat "\n" (_-buffer-substring-from-))))))) ) ;; while lb-rc)) ;; (defun lb-ht-footnote-pop-from-next-section (arg1str-or-re) ;)(06 b ;)(08 b "This called by two functions: lb-ht-footnotes-balancer-anchor-mismatch lb-ht-footnotes-balancer-next-page-next-section This pops first footnote from section (1+ ARG2) where footnote must begin with regexp ARG1. Due to lb-ht-toc-get-section, either re-uses pre-existing lbg-next-section or creates a new one using next section from index.txt. Sets lbg-next-section to remainder of buffer after footnote popped." ;; (_-dfun-hook "lb-ht-footnote-pop-from-next-section") ;; (let (lb-bound lb-rc) (with-temp-buffer (setq _-where-page-numbers _-where-page-numbers-global) ;; If lbg-next-section set, lb-ht-toc-get-section will return it *and* ;; set to nil. ;; Use non-nil optional 2nd arg to suppress recursion. (insert (lb-ht-toc-get-section (1+ lbg-i-from1) t)) ;)(07 a. 2 ;; (progn (goto-char (point-min)) (if (not (search-forward-regexp lb-re-footnote-div nil t)) (error "%s: %s" "Not found" (concat lb-re-footnote-div "\n" (_-buffer-substring-from-)))) (setq lb-bound (lb-tx-page-point "page-end"))) ;; Always look for specific footnote. (or (and (string-match "^\\([*]+\\|[0-9]+\\)$" arg1str-or-re) (search-forward-string (setq arg1str-or-re (concat "^^" arg1str-or-re "^^")) lb-bound t)) (and (string-match "ootnote cont" arg1str-or-re) (search-forward-regexp arg1str-or-re lb-bound t)) (error "%s: %s" (concat "Did not find footnote " arg1str-or-re " on page " (lb-tx-what-page)) (concat "\n" (_-buffer-substring-from-)))) (goto-char (car (_-where-double-newlines))) ;; Get footnote, including __NOTE__. (setq lb-rc (lb-ht-footnote-pop-it)) ;; Pass along whatever is leftover of next section after removing ;; footnote. REMEMBER use of non-nil ARG2 in lb-ht-toc-get-section (above). (setq lbg-next-section (buffer-string))) lb-rc)) ;; (lb-ht-footnotes-anchor-and-move) (defun lb-ht-footnotes-anchor-and-move nil ;)(03 f. "Anchor footnotes after inserting .tx into HTML" (_-dfun-hook "lb-ht-footnotes-anchor-and-move" nil) ;t) ;; (let (lb-str-marker lb-str-visible-in-body lb-str-visible-in-note lb-str-href-in-body lb-str-href-in-note lb-pt-body-^^ lb-footnote lb-pg lb-str lb-str-^^ lb-str-n lb-str-page lb-pt-notes-bound lb-n-note lb-str-2b-inserted lb-rc) (goto-char (point-min)) ;; Insert set of anchors. (while (and ;; Very long AND! (progn ;; Reset. ;; Reset when debugging: (makunbound 'lb-n-note) (when (null (_-just-say-nil 'lb-n-note)) (goto-char (point-min)) (setq lb-n-note 0) (save-excursion ;; Bound for while loop. (if (not (search-forward-string (setq lb-str ;; fix? use other %% ? ;; "%%notes%%" "%%div-class-notes-start%%" ) nil t)) (error "%s: %s" "search-forward-string" lb-str)) (setq lb-pt-notes-bound (point)))) t) (if ;; Search for next footnote. (not (search-forward-regexp _-re-footnote-marker-global nil ;; This has to change every time! ;; lb-pt-notes-bound t)) ;; If not found do cleanup. (progn (_-compress-multiple-newlines) nil) ;; Found. ;; Set strings for... (setq lb-str-^^ (match-string 1)) (setq lb-pt-body-^^ (match-beginning 0)) (setq lb-n-note (1+ lb-n-note)) ;; Set strings for visible and href. (setq lb-str-visible-in-note (setq lb-str-n (int-to-string lb-n-note))) (setq lb-str-visible-in-body (concat (setq lb-str-page (lb-tx-what-page)) lb-ht-footnote-page-and-marker-separator lb-str-^^)) (setq lb-str-href-in-body (setq lb-str-href-in-note (concat lb-str-n "page" lb-str-page))) (setq lb-str-href-in-note (concat lb-ht-footnote-href-prefix-fw lb-str-href-in-note)) (setq lb-str-href-in-body (concat lb-ht-footnote-href-prefix-bk lb-str-href-in-body)) ;; BODY forw anchor. ;; BODY back anchor. ;; Insert anchor to get to note and get back to body from note. (replace-match (concat ;; 2006.12.14 - using word-spacing 3px in #main ! ;; " " " " ;; "[" lb-str-visible-in-body ;; "]" " " ;; This causes leading spaces at beginning ;; of line when footnote anchor is at end ;; of previous line: ;; " " ;; Do *not* append newline; can cause ^M ;; to be at beginning of line. ;; "\n" )) t) ;; Very long AND! ) (if ;; Pop footnote. (not (search-forward-string (setq lb-str-marker (concat "^^" lb-str-^^ "^^")) ;; If last footnote on last page, this will be end of HTML! (lb-tx-page-point "page-end") t)) (error "%s: %s" (concat "On page " (lb-tx-what-page) " did not find foonote") (concat lb-str-^^ "\n" (_-buffer-substring-from-))) (progn ;; Prepare for footnote pop. (search-backward " "
"["
""
;; lb-str-visible-in-note
lb-str-visible-in-body
"]"
;; How to separate number from start of P?
;;" "
;; "
"
"\n" " "
;; 2006.11.05
;; "• "
"\n"
))
;; Insert anchor and link back to body.
(goto-char (point-max))
(if (not (search-backward "
". Add P attributes. ;; Modify '
' in footnotes. ;; fix? ;; Use variable to search for start of notes? (goto-char (point-min)) (search-forward-string "%%div-class-notes-start%%" nil nil) (while (search-forward-string "
" nil t) ;; fix! ;; Use CSS (replace-match (concat "
")))) ;; Check for leftover .tx parts. (progn (goto-char (point-min)) (if (search-forward-regexp lb-re-footnote-div nil t) ;; __-_-_-__ is OK. (if (not (looking-at "[-]__")) (error "%s: %s" (concat "On page " (lb-tx-what-page) " found leftover " lb-re-footnote-div) (_-buffer-substring-from- nil 200))) )) ;; lb-rc)) ;;; ;)( (defun lb-ht-footnotes-balancer-anchor-mismatch nil ;)(05 c "Does last page have a footnote anchor in body but footnote text is absent due to start of new section inbetween those two things? This may call lb-ht-footnote-pop-from-next-section which would set lbg-next-section. ABCDE" ;; Are there footnote markers in body near end of this section but ;; footnote text marker missing? Maybe, if (beginning of) footnote text ;; appears *after* beginning of *next* section. ;; Does not alter anything when pair of markers is present but end of ;; footnote text is in next section. (_-dfun-hook "lb-ht-footnotes-balancer-anchor-mismatch") ;; (let (lb-pt lb-pt-while-backward-pt lb-pt-while-backward-bound lb-msnp1 lb-bound lb-flag lb-rc) ;; Move to end of body of last page (raw section w/o inserts). (progn (lb-tx-goto-page lbg-original-page-last) (goto-char (setq lb-pt-while-backward-pt (lb-tx-page-point "body-end"))) (setq lb-pt-while-backward-bound (lb-tx-page-point "page-beg"))) ;; Just check last page b/c next-to-last page always has matching "*". (while (and (goto-char lb-pt-while-backward-pt) (setq lb-pt-while-backward-pt (search-backward-regexp _-re-footnote-marker-global lb-pt-while-backward-bound t)) (setq lb-msnp1 (match-string-no-properties 1))) ;; After finding anchor in body, check for footnote. ;; Does _-_-_ exist below? If not, insert it. (if (not (setq lb-pt (lb-tx-page-point "footnotes-beg"))) (goto-char (lb-tx-page-point "footnotes-beg" t)) (goto-char lb-pt)) ;; Move past _-_-_ (progn (goto-char (cdr (_-where-double-newlines))) (_-move-forward-whitespace)) ;; Point is now after footnote divider. ;; Got footnote text? (progn (when (not (save-excursion (setq lb-bound (lb-tx-page-point "page-end")) (setq lb-flag nil) (while (search-forward-regexp _-re-footnote-marker-global lb-bound t) (if (string= lb-msnp1 (match-string-no-properties 1)) (setq lb-flag t))) lb-flag)) ;; Anchor in body but missing footnote. ;; This will create and/or modify lbg-next-section. (insert ;; "\n\n" (lb-ht-footnote-pop-from-next-section lb-msnp1) ;)(08 b 2 ;; "\n\n" )))) lb-rc)) ;; (lb-ht-footnotes-cont-checker) (defun lb-ht-footnotes-cont-checker nil ;)(05 a. "Finds all lb-re-footnote-continued starting from end of buffer. Returns list of lists with lb-re-footnote-continued pairs found in .tx file. Each pair is a list of six: 0/3 string `__NOTE__ Footnote cont. on/from page 999` 1/4 string page (from above) `999` 2/5 actual page number 998 Example of return code when count = 1 and CONTINUED FROM not found: `__NOTE__ Footnote cont. on page 9` `9` 8 nil nil nil Example of return code when count = 1 and CONTINUED FROM found: `__NOTE__ Footnote cont. on page 9` `9` 8 `__NOTE__ Footnote cont. from page 8` `8` 9" ;; ;; fix! do this in tx-check-buffer! 2007.05.10. (let ((lb-pt-search-bound (point-max)) (lb-cnt 0) lb-pt-resume-loop lb-str-on lb-str-from lb-beg lb-end lb-page-on lb-page-from lb-is-above-para-cont-on ;; Next two are integers: lb-page-on-actual (lb-page-from-actual "-1") lb-rc) (save-excursion (goto-char (point-max)) ;; __NOTE__ Footnote cont. on pg 2 (while (search-backward-regexp lb-re-footnote-continued nil t) (when (string= "on" (match-string-no-properties 2)) (setq lb-cnt (1+ lb-cnt)) (setq lb-pt-resume-loop (point)) (goto-char (match-end 0)) ;; If these stay nil, return code will be non-nil. (progn (setq lb-str-on (match-string-no-properties 0)) (setq lb-str-from nil) ;; (setq lb-page-on (match-string-no-properties 3)) (setq lb-page-on-actual nil) ;; (setq lb-page-from nil) (setq lb-page-from-actual (lb-tx-what-page))) ;; __NOTE__ Footnote cont. from pg 1 (if (not (search-forward-regexp lb-re-footnote-continued lb-pt-search-bound t)) (if (> lb-cnt 1) (error "%s: %s" "Expecting a 'from' search-forward-regexp" (concat lb-re-footnote-continued (_-buffer-substring-from-))) ;; BREAK. ;; Last footnote may continue on 1st page of next section. (goto-char (point-min))) ;;------------------------------------------------------- (progn (setq lb-str-from (match-string-no-properties 0)) (setq lb-page-from (match-string-no-properties 3)) (setq lb-page-on-actual (lb-tx-what-page)) (setq lb-beg (match-beginning 0)) ;; Returns 't' if above paragraph is "cont. on" paragraph. (setq lb-is-above-para-cont-on ;; 2006.11.29 - why this case on page 573--574? ;; ;; ~/leninist.biz/en/1976/GPSPW2PP/20060821/599.tx ;;;Grosse in ein andere Grosse, sondern Uebergang vom Qualitativen in das ;;; ;;;__NOTE__ Footnote cont. on page 574. ;;; ;;;__NOTE__ Footnote cont. from page 573<<<----- point here; ... ----->>>. ;;; ;;;__NOTE__ Missing two lines here: ;;;Quantitative und umgekehrt sind: ;;; ------------------------------------------------------- (save-excursion (save-match-data (goto-char lb-beg) (_-move-backward-whitespace) (goto-char (car (_-where-double-newlines))) (if (looking-at lb-re-footnote-continued) (match-string-no-properties 2)))))) ;; ------------------------------------------------------- (if (and (not (= (string-to-int lb-page-on) (string-to-int lb-page-on-actual))) ;; Above paragraph is "cont. on" NOTE? (not (string= "on" lb-is-above-para-cont-on))) (error "%s: %s" "Exp/Act ON values do not match" (concat lb-page-on " /= " lb-page-on-actual "\n" (_-buffer-substring-from-) ))) (if (not (= (string-to-int lb-page-from) (string-to-int lb-page-from-actual))) (error "%s: %s" "Exp/Act FROM values do not match" (concat lb-page-from " /= " lb-page-from-actual "\n" (_-buffer-substring-from-) ))) (if (and (not (= (1- (string-to-int lb-page-on-actual)) (string-to-int lb-page-from-actual))) ;; Above paragraph is "cont. on" NOTE? (not (string= "on" lb-is-above-para-cont-on))) (error "%s: %s" "Footnote should continue on next page" (concat lb-page-from " <-> " lb-page-on "\n" (_-buffer-substring-from-) )))) (_-app 'lb-rc (list (list lb-str-on lb-page-on lb-page-from-actual lb-str-from lb-page-from lb-page-on-actual))) ;; NEXT ITERATION (setq lb-pt-search-bound (goto-char lb-pt-resume-loop))))) lb-rc)) ;;; ;)( (defun lb-ht-footnotes-balancer-next-page-next-section nil ;)(05 b "SPECIAL CASE: Last footnote marker and beginning of footnote text are on page N-1. Section ends on page N *AND* footnote text continues on page N underneath beginning of next section. This function will grab/insert last portion of text of last footnote" (_-dfun-hook "lb-ht-footnotes-balancer-next-page-next-section") ;; (let (lb-list lb-list-footnotes-cont ;; Not used here: ;; lb-str-from lb-page-from lb-page-on-actual ;; lb-str-on lb-page-on lb-page-from-actual lb-rc) ;; If 4th element of last pair is nil, assume footnote continues ;; on next page where next section ALSO BEGINS. (when (and (> (length (setq lb-list-footnotes-cont (lb-ht-footnotes-cont-checker))) ;)(06 a. 0) (null (nth 3 (setq lb-list (nth 0 lb-list-footnotes-cont))))) ;; fix! ? ! ? (if nil (error "%s: %s" "THIS IS SO RARE" ;; Pages 117-118: ;; ~/leninist.biz/en/1976/GPSPW3PP/20060825/199.tx (concat (progn (delete-file "~/foo") (write-region (point-min) (point-max) "~/foo") nil) "had better insert a sticky into book and test NOW." "\n\n" (buffer-substring (max (- (point) 100) (point-min)) (min (+ 100 (point)) (point-max))) ))) (progn (setq lb-str-on (nth 0 lb-list)) (setq lb-page-on (nth 1 lb-list)) (setq lb-page-from-actual (nth 2 lb-list))) ;; Maybe insert page. (if (not (lb-tx-goto-page lb-page-on)) (lb-tx-goto-page lb-page-on t)) ;; Does _-_-_ exist below? If not, insert it. (if (not (setq lb-pt (lb-tx-page-point "footnotes-beg"))) ;; fix! ;; fix here and elsewhere! ;; This goto-char should be enough. (goto-char (lb-tx-page-point "footnotes-beg" t)) (goto-char lb-pt)) ;; Insert footnote. Point is to left of whitespace to left of bar. (progn (goto-char (cdr (_-where-double-newlines))) (_-move-forward-whitespace) (if (looking-at "~") (replace-match ""))) (insert (lb-ht-footnote-pop-from-next-section ;)(06 b 1 (concat lb-re-footnote-continued-from-page lb-page-from-actual))) ;; fix! ;; footnote may continue a 2nd time. ) lb-rc)) (defun lb-ht-insert-^^-around-footnotes-asteriks nil "" ;; (interactive) (let (_pt _rc) (save-match-data (save-excursion (when (setq _pt (lb-tx-page-point "footnotes-beg")) ;; Toss "^^" around "*" at beginning of paragraphs. (save-excursion (goto-char _pt) (while (search-forward-regexp (concat ;; "[ \t\n\r]*" "
]*[>]" "[ \t\n" "~" "]*" ;; The other case, numeric markers, was already ^^9^^ ! "\\([*)]+\\)" ) (lb-tx-page-point "page-end") t) (replace-match (concat "^^" (match-string-no-properties 1) "^^") t t nil 1)))))) _rc)) (provide 'lb-footn) ;;; ;