วิธีที่ดีที่สุดที่ฉันสามารถคิดได้คือการอัปเดตตัวเลขเหล่านี้ทันทีก่อนส่งออกหรือก่อนประเมินผล
Updater
นี่คือฟังก์ชันที่ต้องผ่านบัฟเฟอร์ คุณสามารถผูกมันไว้กับกุญแจหรือเพิ่มลงในตะขอ โค้ดต่อไปนี้จะอัพเดตบรรทัด
เมื่อใดก็ตามที่คุณบันทึกไฟล์แต่ถ้ากรณีการใช้งานของคุณแตกต่างกันเพียงแค่ค้นหาว่าคุณต้องการ hook อะไร! (โหมดองค์กรเต็มไปด้วย hooks)
(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
นี่คือที่ที่คุณกำหนด regexps ซึ่งจะใช้เป็นบรรทัดแรกและบรรทัดสุดท้ายที่จะรวม คุณสามารถให้รายการ regexps สำหรับแต่ละนามสกุลไฟล์
(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)))))
ผู้ทำงานเบื้องหลัง
นี่คือคนที่ทำงานส่วนใหญ่
(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))))))))