Najlepszym sposobem, jaki mogę wymyślić, jest aktualizacja tych liczb bezpośrednio przed eksportem lub oceną.
Aktualizator
Jest to funkcja przechodząca przez bufor. Możesz powiązać go z kluczem lub dodać do haka. Poniższy kod aktualizuje wiersze
za każdym razem, gdy zapisujesz plik , ale jeśli Twój przypadek użycia jest inny, po prostu dowiedz się, jakiego haka potrzebujesz! (tryb org jest pełen haków)
(add-hook 'before-save-hook #'endless/update-includes)
(defun endless/update-includes (&rest ignore)
"Update the line numbers of all #+INCLUDE:s in current buffer.
Only looks at INCLUDEs that already have a line number listed!
This function does nothing if not in org-mode, so you can safely
add it to `before-save-hook'."
(interactive)
(when (derived-mode-p 'org-mode)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp
"^\\s-*#\\+INCLUDE: *\"\\([^\"]+\\)\".*:lines *\"\\([-0-9]+\\)\""
nil 'noerror)
(let* ((file (expand-file-name (match-string-no-properties 1)))
(lines (endless/decide-line-range file)))
(when lines
(replace-match lines :fixedcase :literal nil 2)))))))
Regexps
W tym miejscu definiujesz wyrażenia regularne, które będą używane jako pierwsza i ostatnia linia do uwzględnienia. Możesz podać listę wyrażeń regularnych dla każdego rozszerzenia pliku.
(defcustom endless/extension-regexp-map
'(("sv" ("^class\\b" . "^endclass\\b") ("^enum\\b" . "^endenum\\b")))
"Alist of regexps to use for each file extension.
Each item should be
(EXTENSION (REGEXP-BEGIN . REGEXP-END) (REGEXP-BEGIN . REGEXP-END))
See `endless/decide-line-range' for more information."
:type '(repeat (cons string (repeat (cons regexp regexp)))))
Pracownik w tle
To facet, który wykonuje większość pracy.
(defun endless/decide-line-range (file)
"Visit FILE and decide which lines to include.
The FILE's extension is used to get a list of cons cells from
`endless/extension-regexp-map'. Each cons cell is a pair of
regexps, which determine the beginning and end of region to be
included. The first one which matches is used."
(let ((regexps (cdr-safe (assoc (file-name-extension file)
endless/extension-regexp-map)))
it l r)
(when regexps
(save-match-data
(with-temp-buffer
(insert-file file)
(while regexps
(goto-char (point-min))
(setq it (pop regexps))
(when (search-forward-regexp (car it) nil 'noerror)
(setq l (line-number-at-pos (match-beginning 0)))
(when (search-forward-regexp (cdr it) nil 'noerror)
(setq regexps nil
r (line-number-at-pos (match-end 0))))))
(when r (format "%s-%s" l (+ r 1))))))))