Skip to content

Commit 3503b2c

Browse files
committed
hpath:shorten - Shorten paths and make relative to source path
1 parent 0b7e746 commit 3503b2c

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

ChangeLog

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
2024-11-18 bw <bw@norlinux>
2+
3+
* hui-tests.el (hui--ibut-link-directly-to-org-header-first-column):
4+
Remove dir from fileb when comparing to in-buffer filename and
5+
do the same for other tests in this file.
6+
17
2024-11-17 Bob Weiner <[email protected]>
28

9+
* hpath.el (hpath:call): Allow for # sections with # chars embedded.
10+
11+
* hpath.el (hpath:shorten): First make path relative to any optional RELATIVE-TO
12+
path (default = 'default-directory') and expand both paths. This makes
13+
inserting a link from the other window display the minimal path to produce
14+
the link relative to the source path.
15+
16+
* hui.el (hui:link-possible-types): Ensure outline modes trigger only when
17+
'buffer-file-name' is non-nil since is used in the return value. Simplify
18+
'link-to-string-match' and other link types.
19+
20+
* hpath.el (hpath:variables): Add 'hywiki-directory' for use in path substitutions.
21+
322
* hywiki.el (hywiki-word-at, hywiki-maybe-dehighlight-page-name): Fix to exclude
423
any char after the HyWikiWord, if any.
524

hbut.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
;; Author: Bob Weiner
44
;;
55
;; Orig-Date: 18-Sep-91 at 02:57:09
6-
;; Last-Mod: 13-Oct-24 at 20:53:36 by Bob Weiner
6+
;; Last-Mod: 18-Nov-24 at 20:17:13 by Bob Weiner
77
;;
88
;; SPDX-License-Identifier: GPL-3.0-or-later
99
;;

hpath.el

+28-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
;; Author: Bob Weiner
44
;;
55
;; Orig-Date: 1-Nov-91 at 00:44:23
6-
;; Last-Mod: 24-Aug-24 at 01:31:41 by Bob Weiner
6+
;; Last-Mod: 18-Nov-24 at 20:16:58 by Bob Weiner
77
;;
88
;; SPDX-License-Identifier: GPL-3.0-or-later
99
;;
@@ -554,7 +554,7 @@ Used only if the function `image-mode' is defined."
554554
;; link is later resolved.
555555
;;
556556
(defcustom hpath:variables
557-
'(hyperb:dir load-path exec-path Info-directory-list sm-directory)
557+
'(hyperb:dir hywiki-directory load-path exec-path Info-directory-list sm-directory)
558558
"*List of Emacs Lisp variable symbols to substitute within matching link paths.
559559
Each variable value, if bound, must be either a pathname or a list of pathnames.
560560
When embedded within a path, the format is ${variable}."
@@ -940,14 +940,16 @@ if (hpath:remote-available-p) returns nil."
940940

941941
(defun hpath:at-p (&optional type non-exist)
942942
"Return delimited path or non-delimited remote path at point, if any.
943-
Path is expanded and normalized. World-Wide Web urls are ignored
944-
and therefore dealt with by other code. Delimiters may be:
945-
double quotes, open and close single quote, whitespace, or
946-
Texinfo file references. If optional TYPE is the symbol \\='file or
947-
\\='directory, then only that path type is accepted as a match.
948-
Only locally reachable paths are checked for existence. With
949-
optional NON-EXIST, nonexistent local paths are allowed.
950-
Absolute pathnames must begin with a `/' or `~'."
943+
Path is expanded and normalized. See `hpath:is-p' for how the path
944+
is normalized.
945+
946+
World-Wide Web urls are ignored and therefore dealt with by other
947+
code. Delimiters may be: double quotes, open and close single
948+
quote, whitespace, or Texinfo file references. If optional TYPE
949+
is the symbol \\='file or \\='directory, then only that path type
950+
is accepted as a match. Only locally reachable paths are checked
951+
for existence. With optional NON-EXIST, nonexistent local paths
952+
are allowed. Absolute pathnames must begin with a `/' or `~'."
951953
(let ((path (hpath:delimited-possible-path non-exist))
952954
subpath)
953955
(when path
@@ -1063,7 +1065,7 @@ Make any existing path within a file buffer absolute before returning."
10631065
;; match to in-file #anchor references
10641066
(string-match "\\`#[^+\'\"<>#]+\\'" path))
10651067
(setq path (concat mode-prefix buffer-file-name path)))
1066-
((string-match "\\`\\([^#]+\\)\\(#[^#+]*\\)\\'" path)
1068+
((string-match "\\`\\([^#]+\\)\\(#[^#+]*.*\\)\\'" path)
10671069
;; file and #anchor reference
10681070
(setq suffix (match-string 2 path)
10691071
path (match-string 1 path))
@@ -2024,12 +2026,25 @@ prior to calling this function."
20242026
(error ""))
20252027
var-group)))
20262028

2027-
(defun hpath:shorten (path)
2028-
"Shorten and return a PATH.
2029+
(defun hpath:shorten (path &optional relative-to)
2030+
"Shorten and return a PATH optionally RELATIVE-TO other path.
2031+
If RELATIVE-TO is omitted or nil, set it to `default-directory'.
20292032
Replace Emacs Lisp variables and environment variables (format of
20302033
${var}) with their values in PATH. The first matching value for
20312034
variables like `${PATH}' is used. Then abbreviate any remaining
20322035
path."
2036+
(setq path (expand-file-name (hpath:substitute-value path)))
2037+
(unless relative-to
2038+
(setq relative-to default-directory))
2039+
(when (stringp relative-to)
2040+
(setq relative-to (expand-file-name
2041+
(hpath:substitute-value relative-to))
2042+
path
2043+
(cond ((string-equal path relative-to)
2044+
"")
2045+
((string-equal (file-name-directory path) relative-to)
2046+
(file-name-nondirectory path))
2047+
(t (hpath:relative-to path relative-to)))))
20332048
(hpath:abbreviate-file-name (hpath:substitute-var path)))
20342049

20352050
(defun hpath:substitute-value (path)

hui.el

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
;; Author: Bob Weiner
44
;;
55
;; Orig-Date: 19-Sep-91 at 21:42:03
6-
;; Last-Mod: 10-Nov-24 at 15:44:56 by Bob Weiner
6+
;; Last-Mod: 18-Nov-24 at 20:05:31 by Bob Weiner
77
;;
88
;; SPDX-License-Identifier: GPL-3.0-or-later
99
;;
@@ -1929,7 +1929,8 @@ Buffer without File link-to-buffer-tmp"
19291929
;;
19301930
;; If current line starts with an outline-regexp prefix and
19311931
;; has a non-empty heading, use a link-to-string-match.
1932-
((and (derived-mode-p 'outline-mode 'org-mode 'kotl-mode)
1932+
((and buffer-file-name
1933+
(derived-mode-p 'outline-mode 'org-mode 'kotl-mode)
19331934
(stringp outline-regexp)
19341935
(save-excursion
19351936
(beginning-of-line)

test/hui-tests.el

+14-6
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,9 @@ With point on label suggest that ibut for rename."
784784
(find-file filea)
785785
(hui:ibut-link-directly (get-buffer-window)
786786
(get-buffer-window (get-file-buffer fileb)))
787-
(should (string= (buffer-string) (concat "\"" fileb ":L1:C10\""))))
787+
(should (string= (buffer-string) (concat "\""
788+
(file-name-nondirectory fileb)
789+
":L1:C10\""))))
788790
(hy-delete-file-and-buffer filea)
789791
(hy-delete-file-and-buffer fileb))))
790792

@@ -827,7 +829,9 @@ With point on label suggest that ibut for rename."
827829
(find-file filea)
828830
(with-simulated-input "label RET"
829831
(hui:ibut-link-directly (get-buffer-window) (get-buffer-window (get-file-buffer fileb)) 4))
830-
(should (string= (buffer-string) (concat "<[label]> - " "\"" fileb ":L1:C10\""))))
832+
(should (string= (buffer-string) (concat "<[label]> - " "\""
833+
(file-name-nondirectory fileb)
834+
":L1:C10\""))))
831835
(hy-delete-file-and-buffer filea)
832836
(hy-delete-file-and-buffer fileb))))
833837

@@ -844,7 +848,7 @@ With point on label suggest that ibut for rename."
844848
(find-file filea)
845849
(hui:ibut-link-directly (get-buffer-window)
846850
(get-buffer-window (get-file-buffer fileb)))
847-
(should (string= (buffer-string) (concat "\"" fileb "#header\"")))
851+
(should (string= (buffer-string) (concat "\"" (file-name-nondirectory fileb) "#header\"")))
848852
(goto-char (point-min))
849853
(search-forward "#")
850854
(action-key)
@@ -866,7 +870,7 @@ With point on label suggest that ibut for rename."
866870
(find-file filea)
867871
(hui:ibut-link-directly (get-buffer-window)
868872
(get-buffer-window (get-file-buffer fileb)))
869-
(should (string= (buffer-string) (concat "\"" fileb "#header:L1:C1\"")))
873+
(should (string= (buffer-string) (concat "\"" (file-name-nondirectory fileb) "#header:L1:C1\"")))
870874
(goto-char (point-min))
871875
(search-forward "#")
872876
(action-key)
@@ -890,7 +894,9 @@ With point on label suggest that ibut for rename."
890894
(find-file filea)
891895
(hui:ibut-link-directly (get-buffer-window)
892896
(get-buffer-window (get-file-buffer fileb)))
893-
(should (string= (buffer-string) (concat "\"" fileb ":L2\"")))
897+
(should (string= (buffer-string) (concat "\""
898+
(file-name-nondirectory fileb)
899+
":L2\"")))
894900
(goto-char (point-min))
895901
(search-forward ":")
896902
(action-key)
@@ -976,7 +982,9 @@ With point on label suggest that ibut for rename."
976982
(hui:gbut-link-directly t)
977983
(with-current-buffer (find-buffer-visiting global-but-file)
978984
(should (string= (buffer-string)
979-
(concat "First\n<[button]> - \"" file ":L1\""))))))
985+
(concat "First\n<[button]> - \""
986+
(file-name-nondirectory file)
987+
":L1\""))))))
980988
(hy-delete-file-and-buffer global-but-file)
981989
(hy-delete-file-and-buffer file))))
982990

0 commit comments

Comments
 (0)