[Question]
  [
## Background
When searches return many pages of results, GitHub avoids cluttering their UI by eliding page links. Instead, their UI lets you select:
1. Pages at the beginning of the results.
2. Pages near the current page.
3. Pages at the end of the results.
In this way, no more than 11 page links are ever displayed at one time. Before describing the precise logic underlying the pager, a few screenshots will help demonstrate it:
[![GitHub page buttons 1 2 3 4 5 ‚Ä¶ 99 100](https://i.stack.imgur.com/N4S8L.png)](https://i.stack.imgur.com/N4S8L.png)
[![GitHub page buttons 1 2 3 4 5 6 7 8 ‚Ä¶ 99 100](https://i.stack.imgur.com/SC1m9.png)](https://i.stack.imgur.com/SC1m9.png)
[![GitHub page buttons 1 2 ‚Ä¶ 5 6 7 8 9 ‚Ä¶ 99 100](https://i.stack.imgur.com/Yx5bw.png)](https://i.stack.imgur.com/Yx5bw.png)
[![GitHub page buttons 1 2 ‚Ä¶ 92 93 [94] 95 96 ‚Ä¶ 99 100](https://i.stack.imgur.com/odr8V.png)](https://i.stack.imgur.com/odr8V.png)
[![GitHub page buttons 1 2 ‚Ä¶ 93 94 [95] 96 97 98 99 100](https://i.stack.imgur.com/8k05a.png)](https://i.stack.imgur.com/8k05a.png)
[![GitHub page buttons 1 2 ‚Ä¶ 96 97 98 99 [100]](https://i.stack.imgur.com/PO6dQ.png)](https://i.stack.imgur.com/PO6dQ.png)
## The logic
Assuming a total of `n` results, and a current page of `p`, the pager will always show:
1. The current page.
2. The 4 pages closest in absolute distance to the current page.
	* That is, it will show a "window" of length 5, centered on the current page, but shifting as needed if the current page is too close to one of the boundaries (the first or last page).
3. The first 2 pages.
4. The last 2 pages.
5. All other contiguous pages besides these will be elided with a single `...`, with one exception:
	* If only 1 page would be elided, simply show that page rather than the `...`, because it's going to take up the same amount of space anyway.
As an aside unrelated to the challenge, it's interesting that [Stack Overflow does not follow the exception to rule 5](https://i.stack.imgur.com/SmNi2.png).
## Task
**Input:** A total number of pages `n` and a current page `p`.
**Output:** A list containing all the pages, in order, to be shown by the GitHub pager, with logic as described above. You may use any value that is not a possible page value to represent the ellipses. For example, -1, infinity or 0 (but 0 is only valid if you are using 1-based indexing).
* You can use 0-based or 1-based indexing.
* You can take input and output in any reasonable format (e.g. output can be an array, 1 number per line, etc.)
This is code golf with standard site rules.
## Test Cases
Format will be `[n, p] => <output list>`, using -1 to represent ellipsis. Test cases use 1-based indexing.
```
[1, 1] => [1]
[2, 1] => [1, 2]
[2, 2] => [1, 2]
[5, 1] => [1, 2, 3, 4, 5]
[5, 5] => [1, 2, 3, 4, 5]
[11, 6] => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[11, 5] => [1, 2, 3, 4, 5, 6, 7, -1, 10, 11]
[12, 6] => [1, 2, 3, 4, 5, 6, 7, 8, -1, 11, 12]
[12, 7] => [1, 2, -1, 5, 6, 7, 8, 9, 10, 11, 12]
[13, 7] => [1, 2, -1, 5, 6, 7, 8, 9, -1, 12, 13]
[100, 1] => [1, 2, 3, 4, 5, -1, 99, 100]
[100, 100] => [1, 2, -1, 96, 97, 98, 99, 100]
[100, 99] => [1, 2, -1, 96, 97, 98, 99, 100]
[100, 98] => [1, 2, -1, 96, 97, 98, 99, 100]
[100, 95] => [1, 2, -1, 93, 94, 95, 96, 97, 98, 99, 100]
[100, 94] => [1, 2, -1, 92, 93, 94, 95, 96, -1, 99, 100]
```
  
[Answer]
# [Python 3.8](https://docs.python.org/3.8/), 103 bytes
```
lambda n,m:[S&{i+1}and i for i in range(n)if{i}&(S:={0,1,2,*range(min(n-5,m-2),max(6,m+4)),n-2,n-1,n})]
```
[Try it online!](https://tio.run/##nZFBasMwEEX3PcVAIUjNGCTZSqxATpFlmoVK6lRQTYzJosX47K7sUohtmZoIRgv9/@aPpPL79nGlNC@rtti/tp/Wv50tEPrd8bCq3Vo2ls7goLhWYXcElaXLOyPuito1K3bY7WuBEhW@/CreEaNEo08UR2@/2Ab9OuMcKVGhJFLDT21ZObqxgkkECWElknN4hqM8Pf1JaiwhqIGq5lUdYRFShAxBD2x6gU2G882cLUgIW4QcwYRUEUoOUf0fmsgIqJZl9mxXashuJ2znjE47gtOFcJ/cfVJ6BwvRv/zM1B1i@lwxZoSYBpoQZ0KeyecwY@AhLH8M0zEs3M5knbioRRZroaZt7p@q/QE "Python 3.8 (pre-release) ‚Äì Try It Online")
0-indexed. Use `set()` for ellipsis. And it can be [97 bytes](https://tio.run/##nZHLasMwEEX3/YqBQpCaMehhJVYgX5FlmoVL61RQTYzJIsH4213JpRA/0poIZqN7z9zRqLyeP0@ks7Jqi@1r@5X7t/ccCP1mv1vUbimb4lSBA0dQ5XT8YMRdUbtmwXabbS1QosKXH8U7YpQY9Inm6PMLW6Ffas6REhVKIjX80JaVozMrmESQEA7n8Ax7eXj6FVRfQFA9Td3TzIhD0AgpgumZzL8mGe5X06YgIKwRMgQb8kQo2QfN32AiJzA1J68jY6k@uR6Q0Tc55wDVs9AuNX6JvkGF6HY9OW8EbJcphoQQwzAbomzIstk9yFp4AMoegcwYCq@yaZRmNUjHDdS4ye2C2m8) if we can output each page numbers as a set.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 22 bytes
```
_2¬´∆ì¬ª3,‚Äò≈ª·∫°‚±Æ¬´4¬ß<11√óR≈íg¬ß
```
[Try it online!](https://tio.run/##ATQAy/9qZWxsef//XzLCq8aTwrszLOKAmMW74bqh4rGuwqs0wqc8MTHDl1LFkmfCp///OTX/MTAw "Jelly ‚Äì Try It Online")
Takes page count as a command-line argument and page number from standard input. 1-based, using 0 for elided pages.
## Algorithm
In order to handle the special case of showing five pages at one end if the active page happens to be there, the page number \$p\$ is clamped into the range \$[3, n-2]\$ (this is impossible if there are 4 or fewer pages, but in that case, the algorithm gives the correct result regardless of the page number because the first two and last two pages cover the whole range). The idea is that instead of having a special case, we can just use the "natural" behaviour of showing pages within two spaces of the current page in order to show the five pages at an end.
For each page, its page number is then compared to \$0\$, the clamped value of \$p\$, and \$n+1\$, looking for the absolute distance. If the page is within two pages of any of these anchors, then we want to display its page number. We also want to display a page if it's within three pages of two of the anchors (if the anchors are on the same side of the page, it'll necessarily be within two pages of one of them because no two anchors can be in the same place; if the anchors are on opposite sides, then they're forming a one-page gap which needs to be displayed rather than elided). To accomplish this check, any absolute distance that's 4 or greater is clamped to 4, and then the three absolute distances are added together. If we're within two pages of any anchor, the sum will be at most \$4+4+2=10\$ (but could be lower). If we're within three pages of two anchors, the sum will be at most \$4+3+3=10\$ (again, it could be lower). If neither of these conditions hold, the sum will be at least \$4+4+3=11\$. So we can check whether a page should be displayed by seeing whether the sum is in the range \$[0,10]\$ or not.
Once the list of pages to include has been determined, they're converted into a list of page numbers of the appropriate format (by pointwise-multiplying the "should be included" boolean by the page number, and then collapsing runs of consecutive zeroes). The runs of consecutive zeroes can be detected by looking for runs of consecutive equal elements, because all the included page numbers will be different from each other.
## Explanation
```
_2¬´∆ì¬ª3,‚Äò≈ª·∫°‚±Æ¬´4¬ß<11√óR≈íg¬ß
   ∆ì                     Read and parse standard input
  ¬´                      Clamp to at most
_2                         {the argument} minus 2
    ¬ª3                   Clamp to at least 3
      ,                  Form a 2-element list with
       ‚Äò                   {the argument} plus 1
        ≈ª                Prepend 0
          ‚±Æ              For each number from 1 to {the argument}
         ·∫°                 take its absolute difference with {each list element}
           ¬´4            Clamp to at most 4
             ¬ß           For each {page}, sum the three clamped differences
              <11        1 for sums below 11, 0 for sums at least 11
                 √ó       Multiply
                  R        {pointwise} by a list from 1 to {the argument}
                   ≈íg    Group consecutive equal elements
                     ¬ß   Replace each {group} with its sum
```
[Answer]
# [Python](https://www.python.org), 98 bytes (@xnor)
```
lambda n,k:[1,2,3*(k<7),*range(max(6,min(k,m:=n-2))-2,min(max(6,m),k+3+2//k)),m*(k+6>n),n-1,n][:n]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=lZLRToMwGIUTL_cUf3bVjh9HC2yUyF4EuWARlACFTGb0WbxZYvSd9GlsYegQ4rKLJuWc75y_bXj9qF-ah0oe3tLg9n3fpKb3uS3icnsXg8TcDxlytBckv1lTXOxieZ-QMn4mKywzSXIs_UCanFKTt8LRopgbtsGXy5xSLFXaWG0kRWkylFHoy6iflFY7KCCTUNWJJBb1Z0DUWIoVBFDGNUme4gKL68e6yBoyh2ADc0pnUO8y2ZAwA5U3GeiaTNekbToKggq7Le0mHb6u9uoqwCJdEbJoFvLfLwTeCXwguAMCwUZwENzOcacdpoTVhKVUhDWChyBUraUWO-JTTT1uslOYn-1ueb34kV-f8tqdPEkfsM8H2gn67WwdsKzpN-o40Q6wfkC1_VMuVLVQ3cIb0UJcAnuXwO4IVscWjnb-DzqjIB-HBzfvfr9v)
### Old [Python](https://www.python.org), 101 bytes
```
lambda n,k:[1,2,3*(k<7),*range(max(4,min(k-2,n-4)),min(max(6,n-2),k+3+2//k)),(n-2)*(k+6>n),n-1,n][:n]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=lZLRToMwFIYTL_cUJ7tqt4OjBTa6yF4EuWARlACFTGb0WbxZYvSd9Gk8hU03IS67aNJz_u__T9v09aN-aR4qvXtLg9v3bZNa_mdSxOX6LgaN-TIUKNGZsPxmwXGyifV9wsr4mblYZprllkRtuZy3lenPqZYc86kzlbNZTgozDQqYzleakypQR-FSR_th67TaQAGZhqpONLP5cgSMJnOsIIAyrlnyFBdYXD_WRdawMQQrGHM-gnqT6YaFGZDfEmBiMhOTtu4oCCrstrybtPu62tJtQEQmIhTRKJS_FYLsGvKk4Z0QCA6Ci-B1ijesCGrMByTqIiwQfARFsTYtsceHkg64JY5heTa75c2Se35xzBt18CQHg3Pe0E4wb-cYg20Pv1HHqXaA_QPS9k-4omhF2crv0UpdAvuXwF4PpmMr1yj_G92eUfbNJzfvvt83)
### Old [Python](https://www.python.org), 110 bytes (@tsh)
```
lambda n,k:range(1,-~n*(n<9))or[1,2,3*(k<7),*range(max(4,min(k-2,n-4)),min(n-2,k+3+2//k)),(n-2)*(k+6>n),n-1,n]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=lZLBToNAEIYTj32KSU-77WDZBVq2KX2RyoHGogRYCFKjF1_ESxOj76RP4yy02gqx6WGTnZnv_2dms68f5XN9X-jdWxzcvG_r2PI_dRbl69sINKbzKtJ3GybQetEjpheK86JaCZTojFi6mHEctUQePTEX80Sz1JKoLZfzJtIUpWNnLCeTlFIm5qQcT5eaEyZQh_uu67ioIINEQ1FuNLP5fACMRuBYQAB5VLLNY5Rhdv1QZknNhhAsYcj5AMoq0TVbJUB6S4CxSYxN3KjDICiwvfK20-7raksrgAiNxUqEg5X8jRBkm5AnCe-EQHAQXASvrXj9FUGJaU-JsggzBB9Bka1NR-zxPqcDboljWJ71bnhz5J6fHfOm2jvJQeCcFzQdzNs5RmDb_W_UcqppYP-AdP1jrshakbfyO7RSl8D-JbDXgWls5ZrK_0K3I5Rd8cnm7ff7Bg)
### Old [Python](https://www.python.org), 112 bytes
```
lambda n,k:range(1,-~n*(n<9))or[1,2,3*(k<7),*range(max(4,k-2--2//(k+~n)),min(n-2,k+3+2//k)),(n-2)*(k+6>n),n-1,n]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=lZLRToMwFIYTL_cUJ7tqx6mjBTa6jL0IcoFxKAEKQWb0Zi_izRKj76RPYwubbkJcdtGk_c_3_-e06etH9dI8lGr3lgQ375smYf5nlcfF7V0MCrNFHav7NeHItmpC1FJSWtYhR4HOhGTLOcVJRxTxM3ExY4IxMZ2SzNoqSrFIFVFMYGY5lpYzLZkz1V5rtlIUFeOoon3f26SsIYdUQVmtFbHpYgRED0GxhACKuCLrpzjH_PqxytOGjCFYwZjSEVR1qhoSpqD9jIOJSU1M0rqjICix29Ku0-7raqMvATwyESGPRqH4PSGIThAngndCIDgILoLXVbzhCtfCbKCkVYQ5go8gdaytF9_jQ0kHnPFjWJzNbnmzxJ6fH_OmOjjJweCcN7QdzNs5xmDbw2_UcbJtYP-AevsnXOpoqbOl36OlvAT2L4G9HqzHlq6p_G90e0bRN5_cvPt-3w)
Thanks to @tsh for spotting typo.
1-based, 0 for ellipsis. Outputs a range object instead of a list if n<9. If that is not acceptable we can add 3 bytes or switch to python 2.
[Answer]
# Python3, 194 bytes:
```
lambda n,p:F([[-1,i][i<3 or p+4-2*(p-2>0)>=i>=p-2-2*(n-p<2)+(n-p==1)or i>n-2]for i in range(1,n+1)])
F=lambda j,l=0:j if[]==j else[[[-1,l+1][l+2 in j],j[0]][j[0]!=-1]]*(j[0]+l!=-2)+F(j[1:],j[0])
```
[Try it online!](https://tio.run/##lZLLbsIwEEXXzVdM2RATp4odUgjCWbLsD7heBJG0joyxkqhSv56OebUUBGJhZx7nznUsu@/@c2PTqWu3tXjfmnK9XJVgqZstQiljRrWSep7CpgUXjWM@Cl3Mi4QUQhcCQ1@xsZtzEvmvEIwgqgsbc1X7CLSFtrQfVciojRhRJFiIg01DjUhmDehaKiEaqExXyZ2riZiSJuJe3SjayEQp6fdnETOlRqGPI4MZGi8wY7M9RbYdCBgMBoFkFJgCUYBkKpD8N6PA9wV@VsjOCAophTGFbN/JrncYFl6vtLBKYUJhSiHHsQkudsCzG3jM/sL87uwd7xc/8JO/vO9ePclRkN4X7Bz83aVekCTX72jP5TuD5ARi@G94jqNznJ1PL@g8fwSePgJnFzAeOx/7zm3h@ELIL8Vnf@4f3unZ19r0VRu@bWxFoXvpnNF9OHy3Q0JmwVNJl/hS16ULq6/SUNBHwHsigkTXVW0PdTgqCQgByyBwrbaI9FXXd@A8sBqS7Q8)
[Answer]
# [Python](https://www.python.org), 151 bytes
```
lambda l,k:re.sub('  +',' . ',' '.join(str(i)if(i<2 or i>l-3or k-3<i<k+3or(i<5and k<6)or(i>l-6and k>l-7))else''for i in range(l))).split(' ')
import re
```
[Attempt This Online!](https://ato.pxeger.com/run?1=nZNLbsIwEIYXXZFTTNk4LibKg1cQ4SIhQgGc1iWEyDZSe4luu-iGRds7taepnRQIgQrRhePxzHz_b1mZt8_8WT6ss-17AsHkYyOT9uDrNY1Xs0UMKVkOObXEZmYigBYiCCzQX2Q9rllmCslNhllispELaw5snLY9tS_b3oiNli0Vq1I3zhawHPWwPqmOXnFWQR9jmgqKUKJZYBnwOLunZooxtkSeMqlsETbYKl9zCZyW1_u-eTE0IamQ03ksKAH6lNO5pIspp2KTSi0VGo0wdAjYERAInSgiOuHuEwTcQ86p57rVPgIegQ6B7qHY-bPoODo-qRLoEegTGBDwlZ-tllMhzujtCGShOuBesigZVXXcCtOrMEXH2VtVIe8yVFrpN_R2lG2ffb3fXr9wsqvNvl838ZWFrzz8wXlgcC3QvxbonACezqrVvQx7J7B7KnD8GtHQaOScZdI8_NfNYDyRTZKYd_scxru2sJi-toOBJcBuAyUHepy0LuwnqjYZkcJjIagapyNVCAL4n6ABUI7ldlvuPw)
Returns a list of strings. Abuses regex to remove duplicate spaces. Elipsed pages are replaced with a `.`
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~45~~ ~~¬†44¬†~~ 38 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
[*Crossed out &nbsp;44&nbsp; is no longer 44 :)*](https://codegolf.stackexchange.com/questions/170188/crossed-out-44-is-still-regular-44)
```
4√ù√ç+¬§I-F<}¬¨(∆í>}3L√¨I2√ù-¬´IL√É√Ø√™D¬•Œò2√§1.√ΩÀú*
```
Inputs in the order \$p,n\$. Outputs with 0 for the ellipsis.
[Try it online](https://tio.run/##AUsAtP9vc2FiaWX//zTDncONK8KkSS1GPH3CrCjGkj59M0zDrEkyw50twqvKkjDigLp9ypJAfcOqRMKlzpgyw6QxLsO9y5wq//85NQoxMDA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWVCqEvYf5PDcw/3ah9aEqHrZlN7aI3GsUl2tcY@h9dEGB2eq3todYTP4ebD6w@vcjm09NwMo8NLDPUO7z09R@u/zv/oaEMdw1gdIGkEJI3ApKGOKZA0BZNmOoaGYA6YAvJACszhlDFYuaGBAYg2MICyLC1hDAsYwxTGMAEzYgE).
**Explanation:**
Step 1: Create list `[1,2,3,p-2,p-1,p,p+1,p+2,n-2,n-1,n]`, while keeping \$p-2\$ and \$p+2\$ in bounds, and remove any overlapping duplicated values:
```
4√ù          # Push list [0,1,2,3,4]
  √ç         # Decrease each by 2 to [-2,-1,0,1,2]
   +        # Add each to the first (implicit) input p: [p-2,p-1,p,p+1,p+2]
    ¬§       # Push the last value / maximum (without popping the list): p+2
     I-     # Decrease it by the second input n
       F }  # (If it's positive) loop that many times:
        <   #  Decrease the values in the list by 1 each iteration
    ¬¨       # Push the first value / minimum (without popping the list): p-2
     (      # Negate it
      ∆í }   # (If it's now non-negative) loop that + 1 amount of times:
       >    #  Increase the values in the list by 1 each iteration
3L√¨         # Prepend-merge list [1,2,3]
 2√ù         # Push list [0,1,2]
I  -        # Subtract each from the second input n: [n,n-1,n-2]
    ¬´       # Merge that list
IL          # Push a list in the range [1,n]
  √É         # Only keep those values from the list, removing out of bounds values
   √Ø        # `√É` converts the values to strings, so convert them back to integers
√™           # Sorted-uniquify the list
```
Step 2: Insert the `0`s where necessary, and output the result:
```
D           # Duplicate this list
 ¬•          # Pop the copy, and push its forward differences
  Œò         # Check for each whether it's equal to 1 (1 if 1; 0 otherwise)
   2√§1.√ΩÀú   # Insert a 1 in the middle of the list:
   2√§       #  Split it into two equal-sized parts
     1.√Ω    #  Intersperse with 1
        Àú   #  Flatten it back to a single list
         *  # Multiply the values at the same positions
            # (after which the result is output implicitly)
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~34~~ ~~32~~ ~~30~~ ~~29~~ 28 bytes
```
R¬©·∫°√û·∏£5;‚Å∏‚Äô¬§;2·∏£‚Å∏·π¨¬µ+3\·ªä≈ª¬¨o√ó¬Æ≈íg¬ß
```
[Try it online!](https://tio.run/##AUQAu/9qZWxsef//UsKp4bqhw57huKM1O@KBuOKAmcKkOzLhuKPigbjhuazCtSszXOG7isW7wqxvw5fCrsWSZ8Kn////MTL/Ng "Jelly ‚Äì Try It Online")
Thanks to Unrelated String for -2 byte.
### Explanation (outdated)
```
R¬©                       Range and copy to register.
   √û                     Sort by...
  ·∫°                        Absolute difference with p.
    ·∏£5                   First 5 elements.
      ;‚Å∏‚Äô¬§;2             Append n-1 and 2.
            ¬Æ≈ì&          Intersection with register, for the special case that n=1.
               ·π¨         Untruth (boolean array from truthy indices).
¬µ                        Chain separator.
·π°3                       All slices with length 3.
  ¬ß                      Sum each slice.
   2=                    True if equals to 2.
     1;                  Prepend 1.
       |                 Vectorized OR with the result of the previous chain.
        √ó¬Æ               Vectorized multiplication by the register.
          ≈íg             Group nearby equal elements. Only possible for the zeros.
            ¬ß            Sum each group.
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 158 bytes
```
.+
$*
\G1
$`1 
¬∂
O`1+
\b(1+) \1\b
<$1>
(?=(1+ ){0,2}<)((1+ |<1+> ){5}).+ 1+( 1+ 1+)
$2...$4
(1+ 1+ )1+ .+(( 1+| <1+>){5})(?<=>( 1+){0,2})
$1...$2
<|>
1+
$.&
```
[Try it online!](https://tio.run/##JU4xDsIwDNzvFR4CSgiK4tAAldIyMjKwdiiVGFgYEBvtt3gAHytOKvnu7HMu8uv@fjxv80pfe5qdhdqgOzNUz4TfF7j0bNENmq2hjrsBSXELfWrEIfPx2zAlo/MwJratWHEyzhJbLZAyUME5pyroMpMROKvzeqScKRF9Sk2bveVPSXFOBaSxBeQG5dbzzMQIBQFRNFIEM@0zSRdyF@gA3mXyXp4U9r5oXS9yXCQuUv0B "Retina 0.8.2 ‚Äì Try It Online") Takes newline-separated values but link is to test suite that splits on spaces for convenience. Explanation:
```
.+
$*
```
Convert to unary.
```
\G1
$`1 
```
Generate all of the page numbers.
```
¬∂
```
Concatenate the current page.
```
O`1+
```
Sort it into position, so it's now a duplicate.
```
\b(1+) \1\b
<$1>
```
Change the duplicate into a marking.
```
(?=(1+ ){0,2}<)((1+ |<1+> ){5}).+ 1+( 1+ 1+)
$2...$4
```
Elide at least two page links starting from three pages after the current page to two pages before the last page, but also not starting before page 6.
```
(1+ 1+ )1+ .+(( 1+| <1+>){5})(?<=>( 1+){0,2})
$1...$2
```
Elide page links before the current page, using the same rules but mirrored.
```
<|>
```
Remove the marking for the current page.
```
1+
$.&
```
Convert to decimal.
[Answer]
# JavaScript (ES6), 106 bytes
Expects `(n)(p)`. The output is 1-indexed, with `0` for ellipsis.
```
n=>g=(p,b=k=i=[])=>++k>n?b:g(p,k<(p<3?6:3)|k>n-(p+3>n?5:2)|(p-=k)*p<9?[...b,...k>++i?[k+~i?0:i]:[],i=k]:b)
```
[Try it online!](https://tio.run/##pZGxboMwEIb3PsWNdjEEm5hghOFBEENIE0QdAWqqTlFfnR50aQAjqgy@wfd/353h/fh1vJ0@6u7Tbdq3c3/RfaPTSpOOldroWucF1anjmLTJyrjCa5OQLgmyMA7oHW9d0jkBNmUs6J10rjb0tUtUlnueVzIsBuk6y43zXWd@XBdxXrBamyIuaX9qm1t7PXvXtiIXAsDpWCjsdpDz4mXaFw99BsISEasRObUwCBjsGUhLVm7K8nH7cCnLIGRwYBAxUAy4j4dbeLnGu9xOiy3TR8FwhEVweBAM8cXllw3BJsO4A7Z4MDVw37f@ml9OjRv4i@BQpqMVDlY4WUXrLDafYKMnWDln8cUKn6zkfzz7uUfMXX@/Yf8D "JavaScript (Node.js) ‚Äì Try It Online")
[Answer]
# [Scala 3](https://www.scala-lang.org/), 160 bytes
Port of [@tsh's Python answer](https://codegolf.stackexchange.com/a/249570/110802) in Scala.
---
```
n=>m=>{val S=Set(0,1,2)++(Math.min(n-5,m-2)to Math.max(6,m+4)-1)++Set(n-2,n-1,n);(0 to n-1).toSeq.filter(i=>S.contains(i)).map(i=>if(S.contains(i+1))i else -1)}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=pZS_TsMwEMYXpj7FCRZbcdo4bdq6KJUYkWCqmKoOpiRglDglMf9U8SQsXWDhicrTcG4ZgMZVKyJFTu6--_m7s-TX92oqM9n-GB_6uniUpT6cLIPi8jaZGjiXSsO80YAHmUE6AHKqDY2HdomHo-RujB8TCjG83ZvU7y8XOh7m8XBu5aN4lBgSMM5C6nnkXJqbZq400X7Ecj-kpoB1TD6RLsu9DvU5Cm2R9kOmfc40PSYBoBB_aNMUuGMzVZlJSqJw_-a00AYdVkRRipyZjaqU_Ex4nFIFSVYlgIyXtc_PgyO4SlLIUUNkeV0N4KQs5fN4ZEqlryd0ABdaGexr3gB8Zhg1mSYp4ZQAtyGEUYBWC85UhV3SP7rQoWPA66ThbtLITWUQMmgz6NTVRHvWcNtmd3sNg4hBl0GPQZ-BwNTGEFaYaDcMnrYDEu7lxXK4TW4MbwXqOUC2rK6lWlB7P9DKkn3DDVIQrE90a2-r0VhDwlEfBC4nAn0INCJ62xFCOJvZGdH_PyJyI3AaAschOnvgOm5cDfL3mF8a33fFYrFevwA)
[Answer]
# [Uiua](https://www.uiua.org/), ~~54~~ ~~52~~ 49 bytes (SBCS)
~~Previous versions:~~
```
‚Üò1‚äú(‚äÇ‚äô'‚äÇ¬Ø1)[]>0+=2‚à©'/+‚çâ‚äÉ=<3‚âê'‚åµ-,‚äÇ‚äÇ0‚Ü•3‚Üß‚äô(‚äÉ'-2'+1‚ßª.)‚à∂+1‚á°
‚Üò1‚äú(‚äÇ‚äô'‚äÇ¬Ø1)[]>0+=2‚à©/+‚äÉ=<3‚à∫'‚åµ-,‚äÇ‚äÇ0‚Ü•3‚Üß‚äô(‚äÉ'-2'+1‚ßª.)‚à∂+1‚á°
```
Current version:
```
‚Üò1‚äú(‚äÇ‚äô'‚äÇ¬Ø1)[]>0+=2‚à©/+‚äÉ=<3‚äÇ‚äü‚áå.,‚åµ-,‚Ü•3‚Üß‚äô(‚äÉ'-2(+1‚á°))‚à∂
```
Note: An extra byte can be saved by reversing the input values (i.e. [p, n] rather than [n, p].
[Try it online!](https://www.uiua.org/pad?src=UGFnZXIg4oaQIOKGmDHiipwo4oqC4oqZJ-KKgsKvMSlbXT4wKz0y4oipJy8r4o2J4oqDPTwz4omQJ-KMtS0s4oqC4oqCMOKGpTPihqfiipko4oqDJy0yJysx4qe7LiniiLYrMeKHoQpQYWdlciDihpAg4oaYMeKKnCjiioLiipkn4oqCwq8xKVtdPjArPTLiiKkvK-KKgz08M-KIuifijLUtLOKKguKKgjDihqUz4oan4oqZKOKKgyctMicrMeKnuy4p4oi2KzHih6EKUGFnZXIg4oaQIOKGmDHiipwo4oqC4oqZJ-KKgsKvMSlbXT4wKz0y4oipLyviioM9PDPiioLiip_ih4wuLOKMtS0s4oalM-KGp-KKmSjiioMnLTIoKzHih6EpKeKItgoKUGFnZXIgMSAxICAgICAjIFsxLCAxXSA9PiBbMV0KUGFnZXIgMiAxICAgICAjIFsyLCAxXSA9PiBbMSwgMl0KUGFnZXIgMiAyICAgICAjIFsyLCAyXSA9PiBbMSwgMl0KUGFnZXIgNSAxICAgICAjIFs1LCAxXSA9PiBbMSwgMiwgMywgNCwgNV0KUGFnZXIgNSA1ICAgICAjIFs1LCA1XSA9PiBbMSwgMiwgMywgNCwgNV0KUGFnZXIgMTEgNiAgICAjIFsxMSwgNl0gPT4gWzEsIDIsIDMsIDQsIDUsIDYsIDcsIDgsIDksIDEwLCAxMV0KUGFnZXIgMTEgNSAgICAjIFsxMSwgNV0gPT4gWzEsIDIsIDMsIDQsIDUsIDYsIDcsIC0xLCAxMCwgMTFdClBhZ2VyIDEyIDYgICAgIyBbMTIsIDZdID0-IFsxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCAtMSwgMTEsIDEyXQpQYWdlciAxMiA3ICAgICMgWzEyLCA3XSA9PiBbMSwgMiwgLTEsIDUsIDYsIDcsIDgsIDksIDEwLCAxMSwgMTJdClBhZ2VyIDEzIDcgICAgIyBbMTMsIDddID0-IFsxLCAyLCAtMSwgNSwgNiwgNywgOCwgOSwgLTEsIDEyLCAxM10KUGFnZXIgMTAwIDEgICAjIFsxMDAsIDFdID0-IFsxLCAyLCAzLCA0LCA1LCAtMSwgOTksIDEwMF0KUGFnZXIgMTAwIDEwMCAjIFsxMDAsIDEwMF0gPT4gWzEsIDIsIC0xLCA5NiwgOTcsIDk4LCA5OSwgMTAwXQpQYWdlciAxMDAgOTkgICMgWzEwMCwgOTldID0-IFsxLCAyLCAtMSwgOTYsIDk3LCA5OCwgOTksIDEwMF0KUGFnZXIgMTAwIDk4ICAjIFsxMDAsIDk4XSA9PiBbMSwgMiwgLTEsIDk2LCA5NywgOTgsIDk5LCAxMDBdClBhZ2VyIDEwMCA5NSAgIyBbMTAwLCA5NV0gPT4gWzEsIDIsIC0xLCA5MywgOTQsIDk1LCA5NiwgOTcsIDk4LCA5OSwgMTAwXQpQYWdlciAxMDAgOTQgICMgWzEwMCwgOTRdID0-IFsxLCAyLCAtMSwgOTIsIDkzLCA5NCwgOTUsIDk2LCAtMSwgOTksIDEwMF0K) (contains all three versions)
**Explanation:**
```
                                                     ‚à∂
# Reverse the top two items on the stack
                                        ‚äô(         )
# Temporarily pop the top item and:
                                           ‚äÉ
# Perform the following two functions on the same values:
                                               (+1‚á°)
# Create an array of numbers from 1 to n
                                            '-2
# Get n - 2
                                      ‚Üß
# Get the minimum of p and n - 2
# (This pushes p away from the end, if needed)
                                    ‚Ü•3
# Get the maximum of 3 and the result
# (This pushes p away from the beginning, if needed)
                                  ‚åµ-,
# Copy the 1 to n array and for each item:
# Get the absolute difference between it and the calculated value
# (Creates a n-length array)
                           ‚äÇ‚äü‚áå.,
# Join the result, the 1 to n array, and its reverse together
# (Creats a 3-by-n matrix)
                       ‚äÉ=<3
# Check if each matrix entry is equal to or less than 3
# (Creates 2 3-by-n matrices)
                    ‚à©/+
# For both matrices, add each column up
# (Results in 2 n-length arrays)
                  =2
# Convert the counts of =3 to 0s and 1s (1 if there are exactly 2 3s)
                 +
# Add the result to the counts of <3
               >0
# Convert the result to 0s and 1s (1 if the count is >0)
‚Üò1‚äú(‚äÇ‚äô'‚äÇ¬Ø1)[]
# Use the result to remove the page numbers we shouldn't see and replace them with a -1
```
**Example:**
```
Input: 12 7       Stack (top on left):
                  # 12 7
:                 # 7 12
‚äô(               # 12
   ‚äÉ
     (
      ‚á°           # [0 1 2 3 4 5 6 7 8 9 10 11]
      +1          # [1 2 3 4 5 6 7 8 9 10 11 12]
     )
     '-2          # 10 [1 2 3 4 5 6 7 8 9 10 11 12]
)                 # 7 10 [1 2 3 4 5 6 7 8 9 10 11 12]
‚Üß                 # 7 [1 2 3 4 5 6 7 8 9 10 11 12]
‚Ü•3                # 7 [1 2 3 4 5 6 7 8 9 10 11 12]
‚åµ-,               # [6 5 4 3 2 1 0 1 2 3 4 5] [1 2 3 4 5 6 7 8 9 10 11 12]
‚äÇ‚äü‚áå.,            # [[12 11 10 9 8 7 6 5 4 3 2 1][1 2 3 4 5 6 7 8 9 10 11 12][6 5 4 3 2 1 0 1 2 3 4 5]] [1 2 3 4 5 6 7 8 9 10 11 12]
‚äÉ=<3              # [[0 0 0 0 0 0 0 0 0 1 0 0][0 0 1 0 0 0 0 0 0 0 0 0][0 0 0 1 0 0 0 0 0 1 0 0]] [[0 0 0 0 0 0 0 0 0 0 0 1 1][1 1 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 1 1 1 1 1 1 0 0 0]] [1 2 3 4 5 6 7 8 9 10 11 12]
‚à©/+               # [0 0 1 1 0 0 0 0 0 2 0 0] [1 1 0 0 1 1 1 1 1 1 0 1 1] [1 2 3 4 5 6 7 8 9 10 11 12]
=2                # [0 0 0 0 0 0 0 0 0 1 0 0] [1 1 0 0 1 1 1 1 1 1 0 1 1] [1 2 3 4 5 6 7 8 9 10 11 12]
+                 # [1 1 0 0 1 1 1 1 1 1 1 1] [1 2 3 4 5 6 7 8 9 10 11 12]
>0                # [1 1 0 0 1 1 1 1 1 1 1 1] [1 2 3 4 5 6 7 8 9 10 11 12]
‚Üò1‚äú(‚äÇ‚äô'‚äÇ¬Ø1)[]   # [1 2 -1 5 6 7 8 9 10 11 12]
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 113 bytes
```
n=>p=>[L=0,1,2,p>6?{}:3,p=p>n-2?n-4:p>2?p-2:1,p+1,p+2,p+3,p+4,p<n-7?{}:n-2,n-1,n].filter(v=>v>L&v<=n?(L=v):v>={})
```
[Try it online!](https://tio.run/##pZFNboMwEEb3PcWsKhDjBBsMGMXmAtwgyiJKIUqFjJVEbKKcnRq6afgTVRbjzXzvzdj@PjbH2@l6MXei66@iLWWrpTJS7XPpI0WGRkXZ45kGaKRRmrBMkzA1imWGsJSi8bqyMc8mvBDNTpO4A2wUNaGoD5vyUt2Lq9NI1aj8s9lJnTm5bNy0UfLxdNtTrW91VWyq@uyUDgB1@8OF7Rb29PAx7LOXPgKbibDFCB9aEAKEEIHPZPmqLO23j6ayCBFCjJAgCATq26IzPF/i7avO0mzN9F7QFZsRxC@CLj65/LQhWGXod7AtGgwN1Pdnv@aXE/0G/iTYHcPRwg4WdrJIllnbfINN3mD5mLU3FvbKgv/HE449bOz6@4btDw "JavaScript (Node.js) ‚Äì Try It Online")
Hardcode
]