Cara terbaik yang dapat saya pikirkan adalah memperbarui nomor-nomor ini segera sebelum mengekspor atau sebelum mengevaluasi.
Pembaru
Ini adalah fungsi yang melewati buffer. Anda dapat mengikatnya ke kunci, atau menambahkannya ke kail. Kode berikut memperbarui baris
setiap kali Anda menyimpan file , tetapi jika kasus penggunaan Anda berbeda, cari tahu yang mana yang Anda butuhkan! (org-mode penuh dengan kait)
(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
Di sinilah Anda mendefinisikan regexps yang akan digunakan sebagai baris pertama dan terakhir yang akan disertakan. Anda dapat memberikan daftar regexps untuk setiap ekstensi file.
(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)))))
Pekerja latar belakang
Ini adalah pria yang melakukan sebagian besar pekerjaan.
(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))))))))