Commit 3c593aa98fe5

Vincent Demeester <vincent@sbr.pm>
2024-07-04 18:09:00
tools/emacs: split init-func…
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 77926db
Changed files (3)
tools/emacs/lisp/init-func.el
@@ -14,51 +14,6 @@
   (string-match "[0-9A-Za-z-]+" system-name)
   (substring system-name (match-beginning 0) (match-end 0)))
 
-;; https://endlessparentheses.com/updating-org-mode-include-statements-on-the-fly.html
-;;;###autoload
-(defun save-and-update-includes ()
-  "Update the line numbers of #+INCLUDE:s in current buffer.
-Only looks at INCLUDEs that have either :range-begin or :range-end.
-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: *\"\\([^\"]+\\)\".*:range-\\(begin\\|end\\)"
-              nil 'noerror)
-        (let* ((file (expand-file-name (match-string-no-properties 1)))
-               lines begin end)
-          (forward-line 0)
-          (when (looking-at "^.*:range-begin *\"\\([^\"]+\\)\"")
-            (setq begin (match-string-no-properties 1)))
-          (when (looking-at "^.*:range-end *\"\\([^\"]+\\)\"")
-            (setq end (match-string-no-properties 1)))
-          (setq lines (decide-line-range file begin end))
-          (when lines
-            (if (looking-at ".*:lines *\"\\([-0-9]+\\)\"")
-                (replace-match lines :fixedcase :literal nil 1)
-              (goto-char (line-end-position))
-              (insert " :lines \"" lines "\""))))))))
-
-(defun decide-line-range (file begin end)
-  "Visit FILE and decide which lines to include.
-BEGIN and END are regexps which define the line range to use."
-  (let (l r)
-    (save-match-data
-      (with-temp-buffer
-        (insert-file-contents file)
-        (goto-char (point-min))
-        (if (null begin)
-            (setq l "")
-          (search-forward-regexp begin)
-          (setq l (line-number-at-pos (match-beginning 0))))
-        (if (null end)
-            (setq r "")
-          (search-forward-regexp end)
-          (setq r (1+ (line-number-at-pos (match-end 0)))))
-        (format "%s-%s" (+ l 1) (- r 1)))))) ;; Exclude wrapper
 
 (provide 'init-func)
 ;;; init-func.el ends here
tools/emacs/lisp/org-func.el
@@ -0,0 +1,51 @@
+;;; org-func.el --- -*- lexical-binding: t -*-
+;;
+
+;; https://endlessparentheses.com/updating-org-mode-include-statements-on-the-fly.html
+;;;###autoload
+(defun save-and-update-includes ()
+  "Update the line numbers of #+INCLUDE:s in current buffer.
+Only looks at INCLUDEs that have either :range-begin or :range-end.
+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: *\"\\([^\"]+\\)\".*:range-\\(begin\\|end\\)"
+              nil 'noerror)
+        (let* ((file (expand-file-name (match-string-no-properties 1)))
+               lines begin end)
+          (forward-line 0)
+          (when (looking-at "^.*:range-begin *\"\\([^\"]+\\)\"")
+            (setq begin (match-string-no-properties 1)))
+          (when (looking-at "^.*:range-end *\"\\([^\"]+\\)\"")
+            (setq end (match-string-no-properties 1)))
+          (setq lines (decide-line-range file begin end))
+          (when lines
+            (if (looking-at ".*:lines *\"\\([-0-9]+\\)\"")
+                (replace-match lines :fixedcase :literal nil 1)
+              (goto-char (line-end-position))
+              (insert " :lines \"" lines "\""))))))))
+
+(defun decide-line-range (file begin end)
+  "Visit FILE and decide which lines to include.
+BEGIN and END are regexps which define the line range to use."
+  (let (l r)
+    (save-match-data
+      (with-temp-buffer
+        (insert-file-contents file)
+        (goto-char (point-min))
+        (if (null begin)
+            (setq l "")
+          (search-forward-regexp begin)
+          (setq l (line-number-at-pos (match-beginning 0))))
+        (if (null end)
+            (setq r "")
+          (search-forward-regexp end)
+          (setq r (1+ (line-number-at-pos (match-end 0)))))
+        (format "%s-%s" (+ l 1) (- r 1)))))) ;; Exclude wrapper
+
+(provide 'org-func)
+;;; org-func.el ends here
tools/emacs/lisp/project-func.el
@@ -0,0 +1,14 @@
+;;; project-func.el --- -*- lexical-binding: t -*-
+;;; Commentary:
+;;; Code:
+(require 'project)
+
+;;;###autoload
+(defun vde-project--project-current ()
+  "Return directory from `project-current' based on Emacs version."
+  (if (>= emacs-major-version 29)
+      (project-root (project-current))
+    (cdr (project-current))))
+
+(provide 'project-func)
+;;; project-func.el ends here