commit ee08e1504507663f58963b2819d0cb064d123678 from: Omar Polo date: Thu Jun 09 18:47:17 2022 UTC add a trick to open `file:num'-kind of strings with embark commit - 8695a5d9bfadccb039940ce2f1e6b39fafd88395 commit + ee08e1504507663f58963b2819d0cb064d123678 blob - f057217657d3ed13b7a6f62e267a6fd795d1239a blob + 708143f8ec668f46ca8e51e57514b07437a8266e --- emacs/init.el +++ emacs/init.el @@ -594,3 +594,31 @@ buffer." (shackle-mode +1) (define-key global-map (kbd "M-g e") #'embark-act) + +(defun op/target-filename+line () + "Target a file with optional line number: file[:number]." + (save-excursion + (let* ((beg (progn (skip-chars-backward "^[:space:]\n") + (point))) + (end (progn (skip-chars-forward "^[:space:]\n") + (point))) + (str (buffer-substring-no-properties beg end))) + (save-match-data + (when (and (progn (goto-char beg) + (ffap-file-at-point)) + (string-match ".+\\(:[[:digit:]]+\\)?:?" str)) + `(file ,str ,beg . ,end)))))) + +(add-to-list 'embark-target-finders #'op/target-filename+line) + +(defun op/acme-find-file (filename) + "Visit FILENAME like `find-file', but also jump to line if provided." + (save-match-data + (if (not (string-match "\\(.+\\):\\([[:digit:]]+\\)" filename)) + (find-file filename) + (let ((path (match-string 1 filename)) + (line (string-to-number (match-string 2 filename)))) + (with-current-buffer (find-file path) + (goto-line line)))))) + +(define-key embark-file-map (kbd "RET") #'op/acme-find-file)