|
1 | 1 | vim9script
|
2 | 2 | # Support scripts for MacVim-specific functionality
|
3 | 3 | # Maintainer: Yee Cheng Chin (macvim-dev@macvim.org)
|
4 |
| -# Last Change: 2022-10-14 |
| 4 | +# Last Change: 2023-03-15 |
5 | 5 |
|
6 | 6 | # Retrieves the text under the selection, without polluting the registers.
|
7 | 7 | # This is easier if we could yank, but we don't know what the user has been
|
@@ -76,4 +76,44 @@ export def ShowDefinitionUnderCursor()
|
76 | 76 | endif
|
77 | 77 | enddef
|
78 | 78 |
|
| 79 | +# Print functionality. We simply show the file in Preview and let the user |
| 80 | +# decide what to do. This allows for more control instead of immediately |
| 81 | +# piping the file to lpr which will actually print the file. |
| 82 | +# |
| 83 | +# PreviewConvertPostScript: |
| 84 | +# Convert the provided PostScript file to PDF, then show in Preview. This is |
| 85 | +# necessary in macOS 13+ as Preview doesn't support .ps files anymore. |
| 86 | +# PreviewPostScript: |
| 87 | +# Directly open PostScript file in Preview. Can use this if |
| 88 | +# PreviewConvertPostScript doesn't work. |
| 89 | +export def PreviewConvertPostScript(deltimer = 10000): number |
| 90 | + # Convert PS to PDF because Preview can't use PS files in macOS 13+ |
| 91 | + system($"pstopdf {v:fname_in} -o {v:fname_in}.pdf") |
| 92 | + if v:shell_error != 0 |
| 93 | + return v:shell_error |
| 94 | + endif |
| 95 | + system($"open -a Preview {v:fname_in}.pdf") |
| 96 | + delete(v:fname_in) |
| 97 | + |
| 98 | + # Delete the file after it's opened in Preview for privacy. We don't have an |
| 99 | + # easy way to detect that Preview has opened the file already, so we just |
| 100 | + # use a generous 10 secs timer. |
| 101 | + # Note that we can't use `open -W` instead because 1) it will block |
| 102 | + # synchronously, and 2) it will only return if Preview.app has closed, which |
| 103 | + # may not happen for a while if it has other unrelated documents opened. |
| 104 | + var to_delete_file = $"{v:fname_in}.pdf" |
| 105 | + timer_start(deltimer, (timer) => delete(to_delete_file)) |
| 106 | + |
| 107 | + return v:shell_error |
| 108 | +enddef |
| 109 | + |
| 110 | +export def PreviewPostScript(deltimer = 10000): number |
| 111 | + system($"open -a Preview {v:fname_in}") |
| 112 | + |
| 113 | + var to_delete_file = v:fname_in |
| 114 | + timer_start(deltimer, (timer) => delete(to_delete_file)) |
| 115 | + |
| 116 | + return v:shell_error |
| 117 | +enddef |
| 118 | + |
79 | 119 | # vim: set sw=2 ts=2 et :
|
0 commit comments